From b48ca2859cde9ba9434764ba92d7be0ed8b38d73 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Thu, 18 Aug 2011 23:55:27 -0700 Subject: [PATCH] Chrome.bindKey --- HTML/atomicity.coffee | 25 +++++++------------------ HTML/atomicity.js | 28 ++++++++-------------------- HTML/osx.coffee | 18 ++++++++++++++++++ HTML/osx.js | 14 +++++++++++++- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/HTML/atomicity.coffee b/HTML/atomicity.coffee index 954df8243..9e27e6cb0 100644 --- a/HTML/atomicity.coffee +++ b/HTML/atomicity.coffee @@ -17,39 +17,28 @@ saveAs = -> App.window.title = _.last filename.split('/') save() -canon = require 'pilot/canon' - -bindKey = (name, shortcut, callback) -> - canon.addCommand - name: name - exec: callback - bindKey: - win: null - mac: shortcut - sender: 'editor' - -bindKey 'open', 'Command-O', (env, args, request) -> +Chrome.bindKey 'open', 'Command-O', (env, args, request) -> if file = Chrome.openPanel() filename = file App.window.title = _.last filename.split('/') code = File.read file env.editor.getSession().setValue code -bindKey 'saveAs', 'Command-Shift-S', (env, args, request) -> +Chrome.bindKey 'saveAs', 'Command-Shift-S', (env, args, request) -> saveAs() -bindKey 'save', 'Command-S', (env, args, request) -> +Chrome.bindKey 'save', 'Command-S', (env, args, request) -> if filename then save() else saveAs() -bindKey 'copy', 'Command-C', (env, args, request) -> +Chrome.bindKey 'copy', 'Command-C', (env, args, request) -> text = editor.getSession().doc.getTextRange editor.getSelectionRange() Chrome.writeToPasteboard text -bindKey 'eval', 'Command-R', (env, args, request) -> +Chrome.bindKey 'eval', 'Command-R', (env, args, request) -> eval env.editor.getSession().getValue() -bindKey 'togglecomment', 'Command-/', (env) -> +Chrome.bindKey 'togglecomment', 'Command-/', (env) -> env.editor.toggleCommentLines() -bindKey 'fullscreen', 'Command-Return', (env) -> +Chrome.bindKey 'fullscreen', 'Command-Return', (env) -> OSX.NSLog 'coming soon' diff --git a/HTML/atomicity.js b/HTML/atomicity.js index 5000e0630..d4ea7e6c2 100644 --- a/HTML/atomicity.js +++ b/HTML/atomicity.js @@ -1,5 +1,5 @@ (function() { - var JavaScriptMode, bindKey, canon, editor, filename, save, saveAs; + var JavaScriptMode, editor, filename, save, saveAs; console.log = OSX.NSLog; editor = ace.edit("editor"); editor.setTheme("ace/theme/twilight"); @@ -19,19 +19,7 @@ return save(); } }; - canon = require('pilot/canon'); - bindKey = function(name, shortcut, callback) { - return canon.addCommand({ - name: name, - exec: callback, - bindKey: { - win: null, - mac: shortcut, - sender: 'editor' - } - }); - }; - bindKey('open', 'Command-O', function(env, args, request) { + Chrome.bindKey('open', 'Command-O', function(env, args, request) { var code, file; if (file = Chrome.openPanel()) { filename = file; @@ -40,28 +28,28 @@ return env.editor.getSession().setValue(code); } }); - bindKey('saveAs', 'Command-Shift-S', function(env, args, request) { + Chrome.bindKey('saveAs', 'Command-Shift-S', function(env, args, request) { return saveAs(); }); - bindKey('save', 'Command-S', function(env, args, request) { + Chrome.bindKey('save', 'Command-S', function(env, args, request) { if (filename) { return save(); } else { return saveAs(); } }); - bindKey('copy', 'Command-C', function(env, args, request) { + Chrome.bindKey('copy', 'Command-C', function(env, args, request) { var text; text = editor.getSession().doc.getTextRange(editor.getSelectionRange()); return Chrome.writeToPasteboard(text); }); - bindKey('eval', 'Command-R', function(env, args, request) { + Chrome.bindKey('eval', 'Command-R', function(env, args, request) { return eval(env.editor.getSession().getValue()); }); - bindKey('togglecomment', 'Command-/', function(env) { + Chrome.bindKey('togglecomment', 'Command-/', function(env) { return env.editor.toggleCommentLines(); }); - bindKey('fullscreen', 'Command-Return', function(env) { + Chrome.bindKey('fullscreen', 'Command-Return', function(env) { return OSX.NSLog('coming soon'); }); }).call(this); diff --git a/HTML/osx.coffee b/HTML/osx.coffee index 08805857b..a5236b384 100644 --- a/HTML/osx.coffee +++ b/HTML/osx.coffee @@ -1,5 +1,7 @@ # This is the CoffeeScript API that wraps all of Cocoa. +canon = require 'pilot/canon' + # Handles the UI chrome Chrome = # Returns null or a file path. @@ -21,6 +23,22 @@ Chrome = pb.declareTypes_owner [OSX.NSStringPboardType], null pb.setString_forType text, OSX.NSStringPboardType + # name - Command name, like "Find in file" + # shortcut - String command name, e.g. + # "Command-T" + # "Command-Shift-F" + # "Ctrl-I" + # callback - (env, args, request) + # + # Returns nothing. + bindKey: (name, shortcut, callback) -> + canon.addCommand + name: name + exec: callback + bindKey: + win: null + mac: shortcut + sender: 'editor' # Handles the file system diff --git a/HTML/osx.js b/HTML/osx.js index 49a5037a2..11c64bce4 100644 --- a/HTML/osx.js +++ b/HTML/osx.js @@ -1,5 +1,6 @@ (function() { - var Chrome, File; + var Chrome, File, canon; + canon = require('pilot/canon'); Chrome = { openPanel: function() { var panel; @@ -22,6 +23,17 @@ pb = OSX.NSPasteboard.generalPasteboard; pb.declareTypes_owner([OSX.NSStringPboardType], null); return pb.setString_forType(text, OSX.NSStringPboardType); + }, + bindKey: function(name, shortcut, callback) { + return canon.addCommand({ + name: name, + exec: callback, + bindKey: { + win: null, + mac: shortcut, + sender: 'editor' + } + }); } }; File = {