diff --git a/demo/demo.js b/demo/demo.js index b1d1e0a1..56b6025a 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -41,7 +41,6 @@ define(function(require, exports, module) { exports.launch = function(env) { - var commands = require("ace/commands/command_manager"); var event = require("pilot/event"); var Range = require("ace/range").Range; var Editor = require("ace/editor").Editor; @@ -554,6 +553,8 @@ exports.launch = function(env) { */ // Fake-Save, works from the editor and the command line. + var commands = env.editor.commands; + commands.addCommand({ name: "save", bindKey: { diff --git a/lib/ace/commands/command_manager.js b/lib/ace/commands/command_manager.js index a6ed31a2..511b0a4d 100644 --- a/lib/ace/commands/command_manager.js +++ b/lib/ace/commands/command_manager.js @@ -3,70 +3,82 @@ define(function(require, exports, module) { var keyUtil = require('pilot/keys'); var useragent = require('pilot/useragent'); -var commands = {}; -var commmandKeyBinding = {}; - -exports.addCommand = function(command) { - commands[command.name] = command; - - if (command.bindKey) { - buildKeyHash(command); - } +var CommandManager = function(commands) { + this.commands = {}; + this.commmandKeyBinding = {}; + + if (commands) + commands.forEach(this.addCommand, this); }; -var platform = useragent.isMac ? "mac" : "win"; -function buildKeyHash(command) { - var binding = command.bindKey; - var key = binding[platform]; - var ckb = commmandKeyBinding; +(function() { - if(!binding[platform]) { - return; - } + this.addCommand = function(command) { + this.commands[command.name] = command; + + if (command.bindKey) { + this._buildKeyHash(command); + } + }; + + var platform = useragent.isMac ? "mac" : "win"; - key.split("|").forEach(function(keyPart) { - var binding = parseKeys(keyPart, command); - var hashId = binding.hashId; - (ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command; - }); -} + this._buildKeyHash = function(command) { + var binding = command.bindKey; + var key = binding[platform]; + var ckb = this.commmandKeyBinding; -function parseKeys(keys, val, ret) { - var key; - var hashId = 0; - var parts = splitSafe(keys, "\\-", null, true); - - for (var i=0, l = parts.length; i < l; i++) { - if (keyUtil.KEY_MODS[parts[i]]) - hashId = hashId | keyUtil.KEY_MODS[parts[i]]; - else - key = parts[i] || "-"; //when empty, the splitSafe removed a '-' - } + if(!binding[platform]) { + return; + } - return { - key: key, - hashId: hashId - } -} - -function splitSafe(s, separator, limit, bLowerCase) { - return (bLowerCase && s.toLowerCase() || s) - .replace(/(?:^\s+|\n|\s+$)/g, "") - .split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999); -} - -exports.findKeyCommand = function findKeyCommand(env, hashId, textOrKey) { - // Convert keyCode to the string representation. - if (typeof textOrKey == "number") { - textOrKey = keyUtil.keyCodeToString(textOrKey); + key.split("|").forEach(function(keyPart) { + var binding = parseKeys(keyPart, command); + var hashId = binding.hashId; + (ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command; + }); } - - var ckbr = commmandKeyBinding; - return ckbr[hashId] && ckbr[hashId][textOrKey]; -} -exports.exec = function(command, env, args) { - command.exec(env, args || {}); -}; + function parseKeys(keys, val, ret) { + var key; + var hashId = 0; + var parts = splitSafe(keys, "\\-", null, true); + + for (var i=0, l = parts.length; i < l; i++) { + if (keyUtil.KEY_MODS[parts[i]]) + hashId = hashId | keyUtil.KEY_MODS[parts[i]]; + else + key = parts[i] || "-"; //when empty, the splitSafe removed a '-' + } + + return { + key: key, + hashId: hashId + } + } + + function splitSafe(s, separator, limit, bLowerCase) { + return (bLowerCase && s.toLowerCase() || s) + .replace(/(?:^\s+|\n|\s+$)/g, "") + .split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999); + } + + this.findKeyCommand = function findKeyCommand(env, hashId, textOrKey) { + // Convert keyCode to the string representation. + if (typeof textOrKey == "number") { + textOrKey = keyUtil.keyCodeToString(textOrKey); + } + + var ckbr = this.commmandKeyBinding; + return ckbr[hashId] && ckbr[hashId][textOrKey]; + } + + this.exec = function(command, env, args) { + command.exec(env, args || {}); + }; + +}).call(CommandManager.prototype); + +exports.CommandManager = CommandManager; }); diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 80c69228..9e328b16 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -41,7 +41,6 @@ define(function(require, exports, module) { var lang = require("pilot/lang"); -var commands = require("ace/commands/command_manager"); function bindKey(win, mac) { return { @@ -51,22 +50,15 @@ function bindKey(win, mac) { }; } -commands.addCommand({ - name: "null", - exec: function(env, args) { } -}); - -commands.addCommand({ +exports.commands = [{ name: "selectall", bindKey: bindKey("Ctrl-A", "Command-A"), exec: function(env, args) { env.editor.selectAll(); } -}); -commands.addCommand({ +}, { name: "removeline", bindKey: bindKey("Ctrl-D", "Command-D"), exec: function(env, args) { env.editor.removeLines(); } -}); -commands.addCommand({ +}, { name: "gotoline", bindKey: bindKey("Ctrl-L", "Command-L"), exec: function(env, args) { @@ -75,31 +67,26 @@ commands.addCommand({ env.editor.gotoLine(line); } } -}); -commands.addCommand({ +}, { name: "togglecomment", bindKey: bindKey("Ctrl-7", "Command-7"), exec: function(env, args) { env.editor.toggleCommentLines(); } -}); -commands.addCommand({ +}, { name: "findnext", bindKey: bindKey("Ctrl-K", "Command-G"), exec: function(env, args) { env.editor.findNext(); } -}); -commands.addCommand({ +}, { name: "findprevious", bindKey: bindKey("Ctrl-Shift-K", "Command-Shift-G"), exec: function(env, args) { env.editor.findPrevious(); } -}); -commands.addCommand({ +}, { name: "find", bindKey: bindKey("Ctrl-F", "Command-F"), exec: function(env, args) { var needle = prompt("Find:"); env.editor.find(needle); } -}); -commands.addCommand({ +}, { name: "replace", bindKey: bindKey("Ctrl-R", "Command-Option-F"), exec: function(env, args) { @@ -111,8 +98,7 @@ commands.addCommand({ return; env.editor.replace(replacement, {needle: needle}); } -}); -commands.addCommand({ +}, { name: "replaceall", bindKey: bindKey("Ctrl-Shift-R", "Command-Shift-Option-F"), exec: function(env, args) { @@ -124,245 +110,198 @@ commands.addCommand({ return; env.editor.replaceAll(replacement, {needle: needle}); } -}); -commands.addCommand({ +}, { name: "undo", bindKey: bindKey("Ctrl-Z", "Command-Z"), exec: function(env, args) { env.editor.undo(); } -}); -commands.addCommand({ +}, { name: "redo", bindKey: bindKey("Ctrl-Shift-Z|Ctrl-Y", "Command-Shift-Z|Command-Y"), exec: function(env, args) { env.editor.redo(); } -}); -commands.addCommand({ +}, { name: "overwrite", bindKey: bindKey("Insert", "Insert"), exec: function(env, args) { env.editor.toggleOverwrite(); } -}); -commands.addCommand({ +}, { name: "copylinesup", bindKey: bindKey("Ctrl-Alt-Up", "Command-Option-Up"), exec: function(env, args) { env.editor.copyLinesUp(); } -}); -commands.addCommand({ +}, { name: "movelinesup", bindKey: bindKey("Alt-Up", "Option-Up"), exec: function(env, args) { env.editor.moveLinesUp(); } -}); -commands.addCommand({ +}, { name: "selecttostart", bindKey: bindKey("Ctrl-Shift-Home|Alt-Shift-Up", "Command-Shift-Up"), exec: function(env, args) { env.editor.getSelection().selectFileStart(); } -}); -commands.addCommand({ +}, { name: "gotostart", bindKey: bindKey("Ctrl-Home|Ctrl-Up", "Command-Home|Command-Up"), exec: function(env, args) { env.editor.navigateFileStart(); } -}); -commands.addCommand({ +}, { name: "selectup", bindKey: bindKey("Shift-Up", "Shift-Up"), exec: function(env, args) { env.editor.getSelection().selectUp(); } -}); -commands.addCommand({ +}, { name: "golineup", bindKey: bindKey("Up", "Up|Ctrl-P"), exec: function(env, args) { env.editor.navigateUp(args.times); } -}); -commands.addCommand({ +}, { name: "copylinesdown", bindKey: bindKey("Ctrl-Alt-Down", "Command-Option-Down"), exec: function(env, args) { env.editor.copyLinesDown(); } -}); -commands.addCommand({ +}, { name: "movelinesdown", bindKey: bindKey("Alt-Down", "Option-Down"), exec: function(env, args) { env.editor.moveLinesDown(); } -}); -commands.addCommand({ +}, { name: "selecttoend", bindKey: bindKey("Ctrl-Shift-End|Alt-Shift-Down", "Command-Shift-Down"), exec: function(env, args) { env.editor.getSelection().selectFileEnd(); } -}); -commands.addCommand({ +}, { name: "gotoend", bindKey: bindKey("Ctrl-End|Ctrl-Down", "Command-End|Command-Down"), exec: function(env, args) { env.editor.navigateFileEnd(); } -}); -commands.addCommand({ +}, { name: "selectdown", bindKey: bindKey("Shift-Down", "Shift-Down"), exec: function(env, args) { env.editor.getSelection().selectDown(); } -}); -commands.addCommand({ +}, { name: "golinedown", bindKey: bindKey("Down", "Down|Ctrl-N"), exec: function(env, args) { env.editor.navigateDown(args.times); } -}); -commands.addCommand({ +}, { name: "selectwordleft", bindKey: bindKey("Ctrl-Shift-Left", "Option-Shift-Left"), exec: function(env, args) { env.editor.getSelection().selectWordLeft(); } -}); -commands.addCommand({ +}, { name: "gotowordleft", bindKey: bindKey("Ctrl-Left", "Option-Left"), exec: function(env, args) { env.editor.navigateWordLeft(); } -}); -commands.addCommand({ +}, { name: "selecttolinestart", bindKey: bindKey("Alt-Shift-Left", "Command-Shift-Left"), exec: function(env, args) { env.editor.getSelection().selectLineStart(); } -}); -commands.addCommand({ +}, { name: "gotolinestart", bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"), exec: function(env, args) { env.editor.navigateLineStart(); } -}); -commands.addCommand({ +}, { name: "selectleft", bindKey: bindKey("Shift-Left", "Shift-Left"), exec: function(env, args) { env.editor.getSelection().selectLeft(); } -}); -commands.addCommand({ +}, { name: "gotoleft", bindKey: bindKey("Left", "Left|Ctrl-B"), exec: function(env, args) { env.editor.navigateLeft(args.times); } -}); -commands.addCommand({ +}, { name: "selectwordright", bindKey: bindKey("Ctrl-Shift-Right", "Option-Shift-Right"), exec: function(env, args) { env.editor.getSelection().selectWordRight(); } -}); -commands.addCommand({ +}, { name: "gotowordright", bindKey: bindKey("Ctrl-Right", "Option-Right"), exec: function(env, args) { env.editor.navigateWordRight(); } -}); -commands.addCommand({ +}, { name: "selecttolineend", bindKey: bindKey("Alt-Shift-Right", "Command-Shift-Right"), exec: function(env, args) { env.editor.getSelection().selectLineEnd(); } -}); -commands.addCommand({ +}, { name: "gotolineend", bindKey: bindKey("Alt-Right|End", "Command-Right|End|Ctrl-E"), exec: function(env, args) { env.editor.navigateLineEnd(); } -}); -commands.addCommand({ +}, { name: "selectright", bindKey: bindKey("Shift-Right", "Shift-Right"), exec: function(env, args) { env.editor.getSelection().selectRight(); } -}); -commands.addCommand({ +}, { name: "gotoright", bindKey: bindKey("Right", "Right|Ctrl-F"), exec: function(env, args) { env.editor.navigateRight(args.times); } -}); -commands.addCommand({ +}, { name: "selectpagedown", bindKey: bindKey("Shift-PageDown", "Shift-PageDown"), exec: function(env, args) { env.editor.selectPageDown(); } -}); -commands.addCommand({ +}, { name: "pagedown", bindKey: bindKey(null, "PageDown"), exec: function(env, args) { env.editor.scrollPageDown(); } -}); -commands.addCommand({ +}, { name: "gotopagedown", bindKey: bindKey("PageDown", "Option-PageDown|Ctrl-V"), exec: function(env, args) { env.editor.gotoPageDown(); } -}); -commands.addCommand({ +}, { name: "selectpageup", bindKey: bindKey("Shift-PageUp", "Shift-PageUp"), exec: function(env, args) { env.editor.selectPageUp(); } -}); -commands.addCommand({ +}, { name: "pageup", bindKey: bindKey(null, "PageUp"), exec: function(env, args) { env.editor.scrollPageUp(); } -}); -commands.addCommand({ +}, { name: "gotopageup", bindKey: bindKey("PageUp", "Option-PageUp"), exec: function(env, args) { env.editor.gotoPageUp(); } -}); -commands.addCommand({ +}, { name: "selectlinestart", bindKey: bindKey("Shift-Home", "Shift-Home"), exec: function(env, args) { env.editor.getSelection().selectLineStart(); } -}); -commands.addCommand({ +}, { name: "selectlineend", bindKey: bindKey("Shift-End", "Shift-End"), exec: function(env, args) { env.editor.getSelection().selectLineEnd(); } -}); -commands.addCommand({ +}, { name: "del", bindKey: bindKey("Delete", "Delete|Ctrl-D"), exec: function(env, args) { env.editor.removeRight(); } -}); -commands.addCommand({ +}, { name: "backspace", bindKey: bindKey( "Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace", "Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H" ), exec: function(env, args) { env.editor.removeLeft(); } -}); -commands.addCommand({ +}, { name: "removetolinestart", bindKey: bindKey(null, "Option-Backspace"), exec: function(env, args) { env.editor.removeToLineStart(); } -}); -commands.addCommand({ +}, { name: "removetolineend", bindKey: bindKey(null, "Ctrl-K"), exec: function(env, args) { env.editor.removeToLineEnd(); } -}); -commands.addCommand({ +}, { name: "removewordleft", bindKey: bindKey("Ctrl-Backspace", "Alt-Backspace|Ctrl-Alt-Backspace"), exec: function(env, args) { env.editor.removeWordLeft(); } -}); -commands.addCommand({ +}, { name: "removewordright", bindKey: bindKey(null, "Alt-Delete"), exec: function(env, args) { env.editor.removeWordRight(); } -}); -commands.addCommand({ +}, { name: "outdent", bindKey: bindKey("Shift-Tab", "Shift-Tab"), exec: function(env, args) { env.editor.blockOutdent(); } -}); -commands.addCommand({ +}, { name: "indent", bindKey: bindKey("Tab", "Tab"), exec: function(env, args) { env.editor.indent(); } -}); -commands.addCommand({ +}, { name: "inserttext", exec: function(env, args) { env.editor.insert(lang.stringRepeat(args.text || "", args.times || 1)); } -}); -commands.addCommand({ +}, { name: "centerselection", bindKey: bindKey(null, "Ctrl-L"), exec: function(env, args) { env.editor.centerSelection(); } -}); -commands.addCommand({ +}, { name: "splitline", bindKey: bindKey(null, "Ctrl-O"), exec: function(env, args) { env.editor.splitLine(); } -}); -commands.addCommand({ +}, { name: "transposeletters", bindKey: bindKey("Ctrl-T", "Ctrl-T"), exec: function(env, args) { env.editor.transposeLetters(); } -}); +}]; }); \ No newline at end of file diff --git a/lib/ace/editor.js b/lib/ace/editor.js index af3ec259..012384f9 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -54,8 +54,10 @@ var EditSession = require("ace/edit_session").EditSession; var Search = require("ace/search").Search; var Range = require("ace/range").Range; var EventEmitter = require("pilot/event_emitter").EventEmitter; +var CommandManager = require("ace/commands/command_manager").CommandManager; +var defaultCommands = require("ace/commands/default_commands").commands; -var Editor =function(renderer, session) { +var Editor = function(renderer, session) { var container = renderer.getContainerElement(); this.container = container; this.renderer = renderer; @@ -75,6 +77,7 @@ var Editor =function(renderer, session) { wrap: true }); + this.commands = new CommandManager(defaultCommands); this.setSession(session || new EditSession("")); }; diff --git a/lib/ace/keyboard/keybinding.js b/lib/ace/keyboard/keybinding.js index 9d6b0e10..ccd42fe2 100644 --- a/lib/ace/keyboard/keybinding.js +++ b/lib/ace/keyboard/keybinding.js @@ -41,7 +41,6 @@ define(function(require, exports, module) { var useragent = require("pilot/useragent"); var keyUtil = require("pilot/keys"); var event = require("pilot/event"); -var commands = require("ace/commands/command_manager"); require("ace/commands/default_commands"); var KeyBinding = function(editor) { @@ -63,14 +62,16 @@ var KeyBinding = function(editor) { }; this.$callKeyboardHandler = function (e, hashId, keyOrText, keyCode) { - var env = {editor: this.$editor}, - toExecute; + var env = {editor: this.$editor}; + var toExecute; + var commands = this.$editor.commands; if (this.$keyboardHandler) { toExecute = this.$keyboardHandler.handleKeyboard(this.$data, hashId, keyOrText, keyCode, e); } + // If there is nothing to execute yet, then use the default keymapping. if (!toExecute || !toExecute.command) { if (hashId != 0 || keyCode != 0) {