From 46bf8f4e4fd0d07d613762854d592a7ac3d5d455 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Tue, 21 Sep 2010 17:03:30 +0200 Subject: [PATCH] externalize commands and make them pluggable --- src/ace/KeyBinding.js | 173 +++------------------------- src/ace/PluginManager.js | 12 ++ src/ace/commands/DefaultCommands.js | 155 +++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 160 deletions(-) create mode 100644 src/ace/PluginManager.js create mode 100644 src/ace/commands/DefaultCommands.js diff --git a/src/ace/KeyBinding.js b/src/ace/KeyBinding.js index d5b4842d..80c8a8cb 100644 --- a/src/ace/KeyBinding.js +++ b/src/ace/KeyBinding.js @@ -1,9 +1,12 @@ -require.def("ace/KeyBinding", - ["ace/ace", "ace/conf/keybindings/default_mac", "ace/conf/keybindings/default_win"], - function(ace, default_mac, default_win) { +require.def("ace/KeyBinding", + ["ace/ace", + "ace/conf/keybindings/default_mac", + "ace/conf/keybindings/default_win", + "ace/PluginManager", + "ace/commands/DefaultCommands"], + function(ace, default_mac, default_win, PluginManager) { var KeyBinding = function(element, editor, config) { - this.editor = editor; this.setConfig(config); var _self = this; @@ -12,11 +15,12 @@ var KeyBinding = function(element, editor, config) { | (e.shiftKey ? 4 : 0) | (e.metaKey ? 8 : 0); var key = _self.keyNames[e.keyCode]; - var command = _self[(_self.config.reverse[hashId] || {})[(key - || String.fromCharCode(e.keyCode)).toLowerCase()]]; + var commandName = (_self.config.reverse[hashId] || {})[(key + || String.fromCharCode(e.keyCode)).toLowerCase()]; + var command = PluginManager.commands[commandName]; + if (command) { - _self.selection = editor.getSelection(); - command.call(_self); + command(editor, editor.getSelection()); return ace.stopEvent(e); } }); @@ -55,7 +59,7 @@ var KeyBinding = function(element, editor, config) { "122": "F11", "123": "F12" }; - + function splitSafe(s, separator, limit, bLowerCase) { return (bLowerCase && s.toLowerCase() || s) .replace(/(?:^\s+|\n|\s+$)/g, "") @@ -105,157 +109,6 @@ var KeyBinding = function(element, editor, config) { this.config.reverse = objectReverse.call(this, this.config, "|"); }; - this["selectall"] = function() { - this.selection.selectAll(); - }; - this["removeline"] = function() { - this.editor.removeLines(); - }; - this["gotoline"] = function() { - var line = parseInt(prompt("Enter line number:")); - if (!isNaN(line)) { - this.editor.gotoLine(line); - } - }; - this["togglecomment"] = function() { - this.editor.toggleCommentLines(); - }; - this["findnext"] = function() { - this.editor.findNext(); - }; - this["findprevious"] = function() { - this.editor.findPrevious(); - }; - this["find"] = function() { - var needle = prompt("Find:"); - this.editor.find(needle); - }; - this["undo"] = function() { - this.editor.undo(); - }; - this["redo"] = function() { - this.editor.redo(); - }; - this["redo"] = function() { - this.editor.redo(); - }; - this["overwrite"] = function() { - this.editor.toggleOverwrite(); - }; - this["copylinesup"] = function() { - this.editor.copyLinesUp(); - }; - this["movelinesup"] = function() { - this.editor.moveLinesUp(); - }; - this["selecttostart"] = function() { - this.selection.selectFileStart(); - }; - this["gotostart"] = this["Control-Up"] = function() { - this.editor.navigateFileStart(); - }; - this["selectup"] = function() { - this.selection.selectUp(); - }; - this["golineup"] = function() { - this.editor.navigateUp(); - }; - this["copylinesdown"] = function() { - this.editor.copyLinesDown(); - }; - this["movelinsedown"] = function() { - this.editor.moveLinesDown(); - }; - this["selecttoend"] = function() { - this.selection.selectFileEnd(); - }; - this["gotoend"] = this["Control-Down"] = function() { - this.editor.navigateFileEnd(); - }; - this["selectdown"] = function() { - this.selection.selectDown(); - }; - this["godown"] = function() { - this.editor.navigateDown(); - }; - this["selectwordleft"] = function() { - this.selection.selectWordLeft(); - }; - this["gotowordleft"] = function() { - this.editor.navigateWordLeft(); - }; - this["selecttolinestart"] = function() { - this.selection.selectLineStart(); - }; - this["gotolinestart"] = function() { - this.editor.navigateLineStart(); - }; - this["selectleft"] = function() { - this.selection.selectLeft(); - }; - this["gotoleft"] = function() { - this.editor.navigateLeft(); - }; - this["selectwordright"] = function() { - this.selection.selectWordRight(); - }; - this["gotowordright"] = function() { - this.editor.navigateWordRight(); - }; - this["selecttolineend"] = function() { - this.selection.selectLineEnd(); - }; - this["gotolineend"] = function() { - this.editor.navigateLineEnd(); - }; - this["selectright"] = function() { - this.selection.selectRight(); - }; - this["gotoright"] = function() { - this.editor.navigateRight(); - }; - this["selectpagedown"] = function() { - this.editor.selectPageDown(); - }; - this["pagedown"] = function() { - this.editor.scrollPageDown(); - }; - this["selectpageup"] = function() { - this.editor.selectPageUp(); - }; - this["pageup"] = function() { - this.editor.scrollPageUp(); - }; - this["selectlinestart"] = function() { - this.selection.selectLineStart(); - }; - this["gotolinestart"] = function() { - this.editor.navigateLineStart(); - }; - this["selectlineend"] = function() { - this.selection.selectLineEnd(); - }; - this["gotolineend"] = function() { - this.editor.navigateLineEnd(); - }; - this["del"] = function() { - this.editor.removeRight(); - }; - this["backspace"] = function() { - this.editor.removeLeft(); - }; - this["outdent"] = function() { - this.editor.blockOutdent(); - }; - this["indent"] = function() { - if (this.selection.isMultiLine()) { - this.editor.blockIndent(); - } - else { - this.editor.onTextInput("\t"); - } - }; - }).call(KeyBinding.prototype); return KeyBinding; diff --git a/src/ace/PluginManager.js b/src/ace/PluginManager.js new file mode 100644 index 00000000..44e92cbc --- /dev/null +++ b/src/ace/PluginManager.js @@ -0,0 +1,12 @@ +require.def("ace/PluginManager", [], function() { + +var PluginManager = { + commands : {}, + + registerCommand : function(name, command) { + this.commands[name] = command; + } +}; + +return PluginManager; +}); \ No newline at end of file diff --git a/src/ace/commands/DefaultCommands.js b/src/ace/commands/DefaultCommands.js new file mode 100644 index 00000000..53ac9171 --- /dev/null +++ b/src/ace/commands/DefaultCommands.js @@ -0,0 +1,155 @@ +require.def("ace/commands/DefaultCommands", + ["ace/PluginManager"], function(PluginManager) { + +PluginManager.registerCommand("selectall", function(editor, selection) { + selection.selectAll(); +}); +PluginManager.registerCommand("removeline", function(editor, selection) { + editor.removeLines(); +}); +PluginManager.registerCommand("gotoline", function(editor, selection) { + var line = parseInt(prompt("Enter line number:")); + if (!isNaN(line)) { + editor.gotoLine(line); + } +}); +PluginManager.registerCommand("togglecomment", function(editor, selection) { + editor.toggleCommentLines(); +}); +PluginManager.registerCommand("findnext", function(editor, selection) { + editor.findNext(); +}); +PluginManager.registerCommand("findprevious", function(editor, selection) { + editor.findPrevious(); +}); +PluginManager.registerCommand("find", function(editor, selection) { + var needle = prompt("Find:"); + editor.find(needle); +}); +PluginManager.registerCommand("undo", function(editor, selection) { + editor.undo(); +}); +PluginManager.registerCommand("redo", function(editor, selection) { + editor.redo(); +}); +PluginManager.registerCommand("redo", function(editor, selection) { + editor.redo(); +}); +PluginManager.registerCommand("overwrite", function(editor, selection) { + editor.toggleOverwrite(); +}); +PluginManager.registerCommand("copylinesup", function(editor, selection) { + editor.copyLinesUp(); +}); +PluginManager.registerCommand("movelinesup", function(editor, selection) { + editor.moveLinesUp(); +}); +PluginManager.registerCommand("selecttostart", function(editor, selection) { + selection.selectFileStart(); +}); +PluginManager.registerCommand("gotostart", function(editor, selection) { + editor.navigateFileStart(); +}); +PluginManager.registerCommand("selectup", function(editor, selection) { + selection.selectUp(); +}); +PluginManager.registerCommand("golineup", function(editor, selection) { + editor.navigateUp(); +}); +PluginManager.registerCommand("copylinesdown", function(editor, selection) { + editor.copyLinesDown(); +}); +PluginManager.registerCommand("movelinsedown", function(editor, selection) { + editor.moveLinesDown(); +}); +PluginManager.registerCommand("selecttoend", function(editor, selection) { + selection.selectFileEnd(); +}); +PluginManager.registerCommand("gotoend", function(editor, selection) { + editor.navigateFileEnd(); +}); +PluginManager.registerCommand("selectdown", function(editor, selection) { + selection.selectDown(); +}); +PluginManager.registerCommand("godown", function(editor, selection) { + editor.navigateDown(); +}); +PluginManager.registerCommand("selectwordleft", function(editor, selection) { + selection.selectWordLeft(); +}); +PluginManager.registerCommand("gotowordleft", function(editor, selection) { + editor.navigateWordLeft(); +}); +PluginManager.registerCommand("selecttolinestart", function(editor, selection) { + selection.selectLineStart(); +}); +PluginManager.registerCommand("gotolinestart", function(editor, selection) { + editor.navigateLineStart(); +}); +PluginManager.registerCommand("selectleft", function(editor, selection) { + selection.selectLeft(); +}); +PluginManager.registerCommand("gotoleft", function(editor, selection) { + editor.navigateLeft(); +}); +PluginManager.registerCommand("selectwordright", function(editor, selection) { + selection.selectWordRight(); +}); +PluginManager.registerCommand("gotowordright", function(editor, selection) { + editor.navigateWordRight(); +}); +PluginManager.registerCommand("selecttolineend", function(editor, selection) { + selection.selectLineEnd(); +}); +PluginManager.registerCommand("gotolineend", function(editor, selection) { + editor.navigateLineEnd(); +}); +PluginManager.registerCommand("selectright", function(editor, selection) { + selection.selectRight(); +}); +PluginManager.registerCommand("gotoright", function(editor, selection) { + editor.navigateRight(); +}); +PluginManager.registerCommand("selectpagedown", function(editor, selection) { + editor.selectPageDown(); +}); +PluginManager.registerCommand("pagedown", function(editor, selection) { + editor.scrollPageDown(); +}); +PluginManager.registerCommand("selectpageup", function(editor, selection) { + editor.selectPageUp(); +}); +PluginManager.registerCommand("pageup", function(editor, selection) { + editor.scrollPageUp(); +}); +PluginManager.registerCommand("selectlinestart", function(editor, selection) { + selection.selectLineStart(); +}); +PluginManager.registerCommand("gotolinestart", function(editor, selection) { + editor.navigateLineStart(); +}); +PluginManager.registerCommand("selectlineend", function(editor, selection) { + selection.selectLineEnd(); +}); +PluginManager.registerCommand("gotolineend", function(editor, selection) { + editor.navigateLineEnd(); +}); +PluginManager.registerCommand("del", function(editor, selection) { + editor.removeRight(); +}); +PluginManager.registerCommand("backspace", function(editor, selection) { + editor.removeLeft(); +}); +PluginManager.registerCommand("outdent", function(editor, selection) { + editor.blockOutdent(); +}); +PluginManager.registerCommand("indent", function(editor, selection) { + if (selection.isMultiLine()) { + editor.blockIndent(); + } + else { + editor.onTextInput("\t"); + } +}); + +}); \ No newline at end of file