From 003776da8f7ee4434547285b81024cbfc330cb52 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 28 Aug 2011 00:23:56 -0700 Subject: [PATCH] boom. errors with scripts and line numbers now show up in web inspector https://img.skitch.com/20110828-rrfnqet2y2yrxq6witst6mrscx.png fixes #3 atomicity modules that want to export an api must use define(). this mirrors ace's module definition style so it should fit in fine. $, _, and CoffeeScript are now global scripts show up as loaded before: https://img.skitch.com/20110828-japr2cp4ucbr4a1kmp9degs3t.png after: https://img.skitch.com/20110828-txc782k47gn4rb67mhbujsgkw5.png --- src/atomicity.coffee | 153 ------------------------ src/bootstrap.coffee | 3 +- src/editor.coffee | 154 ++++++++++++++++++++++++ src/osx.coffee | 277 +++++++++++++++++++++---------------------- src/require.coffee | 25 ++-- static/index.html | 5 +- 6 files changed, 308 insertions(+), 309 deletions(-) delete mode 100644 src/atomicity.coffee create mode 100644 src/editor.coffee diff --git a/src/atomicity.coffee b/src/atomicity.coffee deleted file mode 100644 index beebedbeb..000000000 --- a/src/atomicity.coffee +++ /dev/null @@ -1,153 +0,0 @@ -# nice! - -{Chrome, File, Process, Dir} = require 'osx' - -ace = require 'ace/ace' -canon = require 'pilot/canon' - -$ = require 'jquery' -{CoffeeScript} = require 'coffee-script' - -Chrome.addPane 'main', '
' - -editor = ace.edit "editor" -editor.setTheme require "ace/theme/twilight" -JavaScriptMode = require("ace/mode/javascript").Mode -CoffeeMode = require("ace/mode/coffee").Mode -HTMLMode = require("ace/mode/html").Mode -editor.getSession().setMode new JavaScriptMode -editor.getSession().setUseSoftTabs true -editor.getSession().setTabSize 2 - -# fuuuuu, ui bug -setTimeout -> - editor.focus() - editor.resize() -, 50 - -if css = File.read "~/.atomicity/twilight.css" - head = $('head')[0] - style = document.createElement 'style' - rules = document.createTextNode css - style.type = 'text/css' - style.appendChild rules - head.appendChild style - -_.map Dir.list("~/.atomicity/"), (path) -> - if /\.js$/.test path - $.getScript path - else if /\.coffee/.test path - eval CoffeeScript.compile File.read path - -filename = null -editor.getSession().on 'change', -> - Chrome.setDirty true -save = -> - File.write filename, editor.getSession().getValue() - setMode() - Chrome.setDirty false -open = -> - if /png|jpe?g|gif/i.test filename - Chrome.openURL filename - else - Chrome.title _.last filename.split('/') - editor.getSession().setValue File.read filename - setMode() - Chrome.setDirty false -setMode = -> - if /\.js$/.test filename - editor.getSession().setMode new JavaScriptMode - else if /\.coffee$/.test filename - editor.getSession().setMode new CoffeeMode - else if /\.html/.test filename - editor.getSession().setMode new HTMLMode -saveAs = -> - if file = Chrome.savePanel() - filename = file - Chrome.title _.last filename.split('/') - save() -bindKey = (name, shortcut, callback) -> - canon.addCommand - name: name - exec: callback - bindKey: - win: null - mac: shortcut - sender: 'editor' - - -bindKey 'open', 'Command-O', (env, args, request) -> - if file = Chrome.openPanel() - filename = file - open() - -bindKey 'openURL', 'Command-Shift-O', (env, args, request) -> - if url = prompt "Enter URL:" - Chrome.openURL url - -bindKey 'saveAs', 'Command-Shift-S', (env, args, request) -> - saveAs() - -bindKey 'save', 'Command-S', (env, args, request) -> - if filename then save() else saveAs() - -bindKey 'new', 'Command-N', (env, args, request) -> - Chrome.createWindow() - -bindKey 'copy', 'Command-C', (env, args, request) -> - text = editor.getSession().doc.getTextRange editor.getSelectionRange() - Chrome.writeToPasteboard text - -bindKey 'cut', 'Command-X', (env, args, request) -> - text = editor.getSession().doc.getTextRange editor.getSelectionRange() - Chrome.writeToPasteboard text - editor.session.remove editor.getSelectionRange() - -bindKey 'eval', 'Command-R', (env, args, request) -> - eval env.editor.getSession().getValue() - -# textmate - -bindKey 'togglecomment', 'Command-/', (env) -> - env.editor.toggleCommentLines() - -bindKey 'tmoutdent', 'Command-[', (env) -> - env.editor.blockOutdent() - -bindKey 'tmindent', 'Command-]', (env) -> - env.editor.indent() - -# emacs > you - -bindKey 'moveforward', 'Alt-F', (env) -> - env.editor.navigateWordRight() - -bindKey 'moveback', 'Alt-B', (env) -> - env.editor.navigateWordLeft() - -bindKey 'deleteword', 'Alt-D', (env) -> - env.editor.removeWordRight() - -bindKey 'selectwordright', 'Alt-B', (env) -> - env.editor.navigateWordLeft() - -bindKey 'home', 'Alt-Shift-,', (env) -> - env.editor.navigateFileStart() - -bindKey 'end', 'Alt-Shift-.', (env) -> - env.editor.navigateFileEnd() - -bindKey 'fullscreen', 'Command-Shift-Return', (env) -> - Chrome.toggleFullscreen() - -# HAX -# this should go in coffee.coffee or something -bindKey 'consolelog', 'Ctrl-L', (env) -> - env.editor.insert 'console.log ""' - env.editor.navigateLeft() - -exports.bindKey = bindKey - -## load plugins -plugins = _.map Dir.list(Chrome.appRoot() + "/plugins"), (plugin) -> - require plugin diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index a0e6b7506..143689f66 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -16,5 +16,4 @@ console.log require.resolve 'underscore' console.log require.resolve 'osx' console.log require.resolve 'ace/requirejs/text!ace/css/editor.css' console.log require.resolve 'ace/keyboard/keybinding' - -this._ = require 'underscore' +console.log '--------------' diff --git a/src/editor.coffee b/src/editor.coffee new file mode 100644 index 000000000..333afe3f1 --- /dev/null +++ b/src/editor.coffee @@ -0,0 +1,154 @@ +# nice! + +define (require, exports, module) -> + {Chrome, File, Process, Dir} = require 'osx' + + ace = require 'ace/ace' + canon = require 'pilot/canon' + + Chrome.addPane 'main', '
' + + editor = ace.edit "editor" + editor.setTheme require "ace/theme/twilight" + JavaScriptMode = require("ace/mode/javascript").Mode + CoffeeMode = require("ace/mode/coffee").Mode + HTMLMode = require("ace/mode/html").Mode + editor.getSession().setMode new JavaScriptMode + editor.getSession().setUseSoftTabs true + editor.getSession().setTabSize 2 + + # fuuuuu, ui bug + setTimeout -> + editor.focus() + editor.resize() + , 50 + + if css = File.read "~/.atomicity/twilight.css" + head = $('head')[0] + style = document.createElement 'style' + rules = document.createTextNode css + style.type = 'text/css' + style.appendChild rules + head.appendChild style + + _.map Dir.list("~/.atomicity/"), (path) -> + if /\.js$/.test path + $.getScript path + else if /\.coffee/.test path + eval CoffeeScript.compile File.read path + + filename = null + editor.getSession().on 'change', -> + Chrome.setDirty true + save = -> + File.write filename, editor.getSession().getValue() + setMode() + Chrome.setDirty false + open = -> + if /png|jpe?g|gif/i.test filename + Chrome.openURL filename + else + Chrome.title _.last filename.split('/') + editor.getSession().setValue File.read filename + setMode() + Chrome.setDirty false + setMode = -> + if /\.js$/.test filename + editor.getSession().setMode new JavaScriptMode + else if /\.coffee$/.test filename + editor.getSession().setMode new CoffeeMode + else if /\.html/.test filename + editor.getSession().setMode new HTMLMode + saveAs = -> + if file = Chrome.savePanel() + filename = file + Chrome.title _.last filename.split('/') + save() + bindKey = (name, shortcut, callback) -> + canon.addCommand + name: name + exec: callback + bindKey: + win: null + mac: shortcut + sender: 'editor' + + + bindKey 'open', 'Command-O', (env, args, request) -> + if file = Chrome.openPanel() + filename = file + open() + + bindKey 'openURL', 'Command-Shift-O', (env, args, request) -> + if url = prompt "Enter URL:" + Chrome.openURL url + + bindKey 'saveAs', 'Command-Shift-S', (env, args, request) -> + saveAs() + + bindKey 'save', 'Command-S', (env, args, request) -> + if filename then save() else saveAs() + + bindKey 'new', 'Command-N', (env, args, request) -> + Chrome.createWindow() + + bindKey 'copy', 'Command-C', (env, args, request) -> + text = editor.getSession().doc.getTextRange editor.getSelectionRange() + Chrome.writeToPasteboard text + + bindKey 'cut', 'Command-X', (env, args, request) -> + text = editor.getSession().doc.getTextRange editor.getSelectionRange() + Chrome.writeToPasteboard text + editor.session.remove editor.getSelectionRange() + + bindKey 'eval', 'Command-R', (env, args, request) -> + eval env.editor.getSession().getValue() + + # textmate + + bindKey 'togglecomment', 'Command-/', (env) -> + env.editor.toggleCommentLines() + + bindKey 'tmoutdent', 'Command-[', (env) -> + env.editor.blockOutdent() + + bindKey 'tmindent', 'Command-]', (env) -> + env.editor.indent() + + # emacs > you + + bindKey 'moveforward', 'Alt-F', (env) -> + env.editor.navigateWordRight() + + bindKey 'moveback', 'Alt-B', (env) -> + env.editor.navigateWordLeft() + + bindKey 'deleteword', 'Alt-D', (env) -> + env.editor.removeWordRight() + + bindKey 'selectwordright', 'Alt-B', (env) -> + env.editor.navigateWordLeft() + + bindKey 'home', 'Alt-Shift-,', (env) -> + env.editor.navigateFileStart() + + bindKey 'end', 'Alt-Shift-.', (env) -> + env.editor.navigateFileEnd() + + bindKey 'fullscreen', 'Command-Shift-Return', (env) -> + Chrome.toggleFullscreen() + + exports.bindKey = bindKey + + # HAX + # this should go in coffee.coffee or something + bindKey 'consolelog', 'Ctrl-L', (env) -> + env.editor.insert 'console.log ""' + env.editor.navigateLeft() + + bindKey 'toggleProjectDrawer', 'Command-Ctrl-N', (env) -> + Project.toggle() + + ## load plugins + plugins = _.map Dir.list(Chrome.appRoot() + "/plugins"), (plugin) -> + require plugin diff --git a/src/osx.coffee b/src/osx.coffee index a9e6aafb8..bc811d2f6 100644 --- a/src/osx.coffee +++ b/src/osx.coffee @@ -1,160 +1,157 @@ # This is the CoffeeScript API that wraps all of Cocoa. +define (require, exports, module) -> + # Handles the UI chrome + Chrome = + addPane: (position, html) -> + verticalDiv = $('#app-vertical') + horizontalDiv = $('#app-horizontal') -$ = require 'jquery' + el = document.createElement("div") + el.setAttribute('class', "pane " + position) + el.innerHTML = html -# Handles the UI chrome -Chrome = - addPane: (position, html) -> - verticalDiv = $('#app-vertical') - horizontalDiv = $('#app-horizontal') + switch position + when 'top', 'main' + verticalDiv.prepend(el) + when 'left' + horizontalDiv.prepend(el) + when 'bottom' + verticalDiv.append(el) + when 'right' + horizontalDiv.append(el) + else + NSLog("I DON'T KNOW HOW TO DEAL WITH #{position}") - el = document.createElement("div") - el.setAttribute('class', "pane " + position) - el.innerHTML = html + # path - Optional. The String path to the file to base it on. + createWindow: (path) -> + c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow" + c.window + c.window.makeKeyAndOrderFront null - switch position - when 'top', 'main' - verticalDiv.prepend(el) - when 'left' - horizontalDiv.prepend(el) - when 'bottom' - verticalDiv.append(el) - when 'right' - horizontalDiv.append(el) + # Set the active window's dirty status. + setDirty: (bool) -> + Chrome.activeWindow().setDocumentEdited bool + + # Returns a boolean + dirty: -> + Chrome.activeWindow().isDocumentEdited() + + # Returns the active NSWindow object + activeWindow: -> + OSX.NSApplication.sharedApplication.keyWindow + + # Returns null or a file path. + openPanel: -> + panel = OSX.NSOpenPanel.openPanel + panel.setCanChooseDirectories(true) + if panel.runModal isnt OSX.NSFileHandlingPanelOKButton + return null + panel.filenames.lastObject + + # Returns null or a file path. + savePanel: -> + panel = OSX.NSSavePanel.savePanel + if panel.runModal isnt OSX.NSFileHandlingPanelOKButton + return null + panel.filenames.lastObject + + writeToPasteboard: (text) -> + pb = OSX.NSPasteboard.generalPasteboard + pb.declareTypes_owner [OSX.NSStringPboardType], null + pb.setString_forType text, OSX.NSStringPboardType + + openURL: (url) -> + window.location = url + Chrome.title _.last url.replace(/\/$/,'').split '/' + + title: (text) -> + WindowController.window.title = text + + toggleFullscreen: -> + if Chrome.fullscreen? + Chrome.leaveFullscreen() else - NSLog("I DON'T KNOW HOW TO DEAL WITH #{position}") + Chrome.enterFullscreen() - # path - Optional. The String path to the file to base it on. - createWindow: (path) -> - c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow" - c.window - c.window.makeKeyAndOrderFront null + leaveFullscreen: -> + Chrome.fullscreen = false - # Set the active window's dirty status. - setDirty: (bool) -> - Chrome.activeWindow().setDocumentEdited bool + OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible + window = WindowController.window - # Returns a boolean - dirty: -> - Chrome.activeWindow().isDocumentEdited() + enterFullscreen: -> + Chrome.fullscreen = true - # Returns the active NSWindow object - activeWindow: -> - OSX.NSApplication.sharedApplication.keyWindow + OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible + window = WindowController.window - # Returns null or a file path. - openPanel: -> - panel = OSX.NSOpenPanel.openPanel - panel.setCanChooseDirectories(true) - if panel.runModal isnt OSX.NSFileHandlingPanelOKButton - return null - panel.filenames.lastObject + fullscreenWindow = OSX.NSWindow.alloc. + initWithContentRect_styleMask_backing_defer_screen( + window.contentRectForFrameRect(window.frame), + OSX.NSBorderlessWindowMask, + OSX.NSBackingStoreBuffered, + true, + window.screen) - # Returns null or a file path. - savePanel: -> - panel = OSX.NSSavePanel.savePanel - if panel.runModal isnt OSX.NSFileHandlingPanelOKButton - return null - panel.filenames.lastObject + contentView = window.contentView + window.setContentView OSX.NSView.alloc.init - writeToPasteboard: (text) -> - pb = OSX.NSPasteboard.generalPasteboard - pb.declareTypes_owner [OSX.NSStringPboardType], null - pb.setString_forType text, OSX.NSStringPboardType + fullscreenWindow.setHidesOnDeactivate true + fullscreenWindow.setLevel OSX.NSFloatingWindowLevel + fullscreenWindow.setContentView contentView + fullscreenWindow.setTitle window.title + fullscreenWindow.makeFirstResponder null - openURL: (url) -> - window.location = url - Chrome.title _.last url.replace(/\/$/,'').split '/' + fullscreenWindow.makeKeyAndOrderFront null + frame = fullscreenWindow.frameRectForContentRect(fullscreenWindow.screen.frame) + fullscreenWindow.setFrame_display_animate frame, true, true - title: (text) -> - WindowController.window.title = text + appRoot: -> + OSX.NSBundle.mainBundle.resourcePath - toggleFullscreen: -> - if Chrome.fullscreen? - Chrome.leaveFullscreen() - else - Chrome.enterFullscreen() + # Handles the file system + File = + read: (path) -> + OSX.NSString.stringWithContentsOfFile File.expand path + write: (path, contents) -> + str = OSX.NSString.stringWithString contents + str.writeToFile_atomically File.expand(path), true + expand: (path) -> + if /~/.test path + OSX.NSString.stringWithString(path).stringByExpandingTildeInPath + else if path.indexOf('./') is 0 + "#{Chrome.appRoot}/#{path}" + else + path + isFile: (path) -> + isDir = new outArgument + exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir) + exists and not isDir.valueOf() - leaveFullscreen: -> - Chrome.fullscreen = false + Dir = + list: (path, recursive) -> + path = File.expand path + fm = OSX.NSFileManager.defaultManager + if recursive + paths = fm.subpathsAtPath path + else + paths = fm.contentsOfDirectoryAtPath_error path, null + _.map paths, (entry) -> "#{path}/#{entry}" + isDir: (path) -> + isDir = new outArgument + exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir) + exists and isDir.valueOf() - OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible - window = WindowController.window + Process = + cwd: (path) -> + if dir? + OSX.NSFileManager.defaultManager.changeCurrentDirectoryPath(path) + else + OSX.NSFileManager.defaultManager.currentDirectoryPath() - enterFullscreen: -> - Chrome.fullscreen = true + env: -> + OSX.NSProcess.processInfo.environment() - OSX.NSMenu.setMenuBarVisible not OSX.NSMenu.menuBarVisible - window = WindowController.window - - fullscreenWindow = OSX.NSWindow.alloc. - initWithContentRect_styleMask_backing_defer_screen( - window.contentRectForFrameRect(window.frame), - OSX.NSBorderlessWindowMask, - OSX.NSBackingStoreBuffered, - true, - window.screen) - - contentView = window.contentView - window.setContentView OSX.NSView.alloc.init - - fullscreenWindow.setHidesOnDeactivate true - fullscreenWindow.setLevel OSX.NSFloatingWindowLevel - fullscreenWindow.setContentView contentView - fullscreenWindow.setTitle window.title - fullscreenWindow.makeFirstResponder null - - fullscreenWindow.makeKeyAndOrderFront null - frame = fullscreenWindow.frameRectForContentRect(fullscreenWindow.screen.frame) - fullscreenWindow.setFrame_display_animate frame, true, true - - appRoot: -> - OSX.NSBundle.mainBundle.resourcePath - -# Handles the file system -File = - read: (path) -> - OSX.NSString.stringWithContentsOfFile File.expand path - write: (path, contents) -> - str = OSX.NSString.stringWithString contents - str.writeToFile_atomically File.expand(path), true - expand: (path) -> - if /~/.test path - OSX.NSString.stringWithString(path).stringByExpandingTildeInPath - else if path.indexOf('./') is 0 - "#{Chrome.appRoot}/#{path}" - else - path - isFile: (path) -> - isDir = new outArgument - exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir) - exists and not isDir.valueOf() - -Dir = - list: (path, recursive) -> - path = File.expand path - fm = OSX.NSFileManager.defaultManager - if recursive - paths = fm.subpathsAtPath path - else - paths = fm.contentsOfDirectoryAtPath_error path, null - _.map paths, (entry) -> "#{path}/#{entry}" - isDir: (path) -> - isDir = new outArgument - exists = OSX.NSFileManager.defaultManager.fileExistsAtPath_isDirectory(path, isDir) - exists and isDir.valueOf() - -Process = - cwd: (path) -> - if dir? - OSX.NSFileManager.defaultManager.changeCurrentDirectoryPath(path) - else - OSX.NSFileManager.defaultManager.currentDirectoryPath() - - env: -> - OSX.NSProcess.processInfo.environment() - -exports ?= this # Do we even need this anymore? DOUBT IT! -exports.Chrome = Chrome -exports.File = File -exports.Dir = Dir + exports.Chrome = Chrome + exports.File = File + exports.Dir = Dir diff --git a/src/require.coffee b/src/require.coffee index 57302bc49..c227677e0 100644 --- a/src/require.coffee +++ b/src/require.coffee @@ -17,22 +17,20 @@ require = (file) -> __modules[file] = exts[ext]? file __modules[file] +defines = [] +define = (cb) -> + defines.push -> + exports = {} + module = exports: exports + cb.call exports, require, exports, module + exports + exts = css: (file) -> __read file js: (file) -> - code = __read file - exports = __modules[file] # Use existing object (if one exists) - module = exports: exports - - src = "function define(cb){cb.call(this,require,exports)};" - src += """(function(exports, define, module){ - #{code} - /*close open comments*/ - }).call(exports, exports, define, module); - """ - eval src - - module.exports + code = __read file + __jsc__.evalJSString_withScriptPath code, file + defines.pop()?.call() resolve = (file) -> if /!/.test file @@ -83,6 +81,7 @@ __modules = {} this.require = require +this.define = define this.require.paths = paths this.require.exts = exts diff --git a/static/index.html b/static/index.html index acfb944cb..0838cb8e0 100644 --- a/static/index.html +++ b/static/index.html @@ -71,7 +71,10 @@