From 9b8f6c6c95931352237eb00a47484371f1570ab4 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 12 Apr 2010 17:47:10 +0200 Subject: [PATCH] extract key binding into a separate files --- demo/editor.html | 3 +- src/Editor.js | 174 +--------------------------------------------- src/KeyBinding.js | 172 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 173 deletions(-) create mode 100644 src/KeyBinding.js diff --git a/demo/editor.html b/demo/editor.html index 22f3241c..20bc64a7 100644 --- a/demo/editor.html +++ b/demo/editor.html @@ -29,7 +29,8 @@ - + + diff --git a/src/Editor.js b/src/Editor.js index 615ebb51..af0e11b0 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -1,180 +1,12 @@ if (!window.ace) ace = {}; -(function() { - -var keys = { - UP : 38, - RIGHT : 39, - DOWN : 40, - LEFT : 37, - PAGEUP : 33, - PAGEDOWN : 34, - POS1 : 36, - END : 35, - DELETE : 46, - BACKSPACE : 8, - TAB : 9, - A : 65, - D: 68 -}; - -var KeyBinding = function(element, host) { - ace.addListener(element, "keydown", function(e) { - var key = e.keyCode; - - switch (key) { - case keys.A: - if (e.metaKey) { - host.selectAll(); - return ace.stopEvent(e); - } - break; - - case keys.D: - if (e.metaKey) { - host.removeLine(); - return ace.stopEvent(e); - } - break; - - case keys.UP: - if (e.metaKey && e.shiftKey) { - host.selectFileStart(); - } - else if (e.metaKey) { - host.navigateFileStart(); - } - if (e.shiftKey) { - host.selectUp(); - } - else { - host.navigateUp(); - } - return ace.stopEvent(e); - - case keys.DOWN: - if (e.metaKey && e.shiftKey) { - host.selectFileEnd(); - } - else if (e.metaKey) { - host.navigateFileEnd(); - } - if (e.shiftKey) { - host.selectDown(); - } - else { - host.navigateDown(); - } - return ace.stopEvent(e); - - case keys.LEFT: - if (e.altKey && e.shiftKey) { - host.selectWordLeft(); - } - else if (e.altKey) { - host.navigateWordLeft(); - } - else if (e.metaKey && e.shiftKey) { - host.selectLineStart(); - } - else if (e.metaKey) { - host.navigateLineStart(); - } - else if (e.shiftKey) { - host.selectLeft(); - } - else { - host.navigateLeft(); - } - return ace.stopEvent(e); - - case keys.RIGHT: - if (e.altKey && e.shiftKey) { - host.selectWordRight(); - } - else if (e.altKey) { - host.navigateWordRight(); - } - else if (e.metaKey && e.shiftKey) { - host.selectLineEnd(); - } - else if (e.metaKey) { - host.navigateLineEnd(); - } - else if (e.shiftKey) { - host.selectRight(); - } - else { - host.navigateRight(); - } - return ace.stopEvent(e); - - case keys.PAGEDOWN: - if (e.shiftKey) { - host.selectPageDown(); - } - else { - host.scrollPageDown(); - } - return ace.stopEvent(e); - - case keys.PAGEUP: - if (e.shiftKey) { - host.selectPageUp(); - } - else { - host.scrollPageUp(); - } - return ace.stopEvent(e); - - case keys.POS1: - if (e.shiftKey) { - host.selectLineStart(); - } - else { - host.navigateLineStart(); - } - return ace.stopEvent(e); - - case keys.END: - if (e.shiftKey) { - host.selectLineEnd(); - } - else { - host.navigateLineEnd(); - } - return ace.stopEvent(e); - - case keys.DELETE: - host.removeRight(); - return ace.stopEvent(e); - - case keys.BACKSPACE: - host.removeLeft(); - return ace.stopEvent(e); - - case keys.TAB: - if (host.hasMultiLineSelection()) { - if (e.shiftKey) { - host.blockOutdent(); - } else { - host.blockIndent(); - } - } else { - host.onTextInput(" "); - } - return ace.stopEvent(e); - } - }); -}; - ace.Editor = function(doc, renderer) { var container = renderer.getContainerElement(); this.renderer = renderer; this.textInput = new ace.TextInput(container, this); - new KeyBinding(container, this); + new ace.KeyBinding(container, this); ace.addListener(container, "mousedown", ace .bind(this.onMouseDown, this)); @@ -819,6 +651,4 @@ ace.Editor.prototype.selectLine = function() { this._moveSelection(function() { this.moveCursorTo(this.cursor.row + 1, 0); }); -}; - -})(); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/KeyBinding.js b/src/KeyBinding.js new file mode 100644 index 00000000..a347f6f6 --- /dev/null +++ b/src/KeyBinding.js @@ -0,0 +1,172 @@ +if (!window.ace) + ace = {}; + +(function() { + +var keys = { + UP : 38, + RIGHT : 39, + DOWN : 40, + LEFT : 37, + PAGEUP : 33, + PAGEDOWN : 34, + POS1 : 36, + END : 35, + DELETE : 46, + BACKSPACE : 8, + TAB : 9, + A : 65, + D: 68 +}; + +ace.KeyBinding = function(element, host) { + ace.addListener(element, "keydown", function(e) { + var key = e.keyCode; + + switch (key) { + case keys.A: + if (e.metaKey) { + host.selectAll(); + return ace.stopEvent(e); + } + break; + + case keys.D: + if (e.metaKey) { + host.removeLine(); + return ace.stopEvent(e); + } + break; + + case keys.UP: + if (e.metaKey && e.shiftKey) { + host.selectFileStart(); + } + else if (e.metaKey) { + host.navigateFileStart(); + } + if (e.shiftKey) { + host.selectUp(); + } + else { + host.navigateUp(); + } + return ace.stopEvent(e); + + case keys.DOWN: + if (e.metaKey && e.shiftKey) { + host.selectFileEnd(); + } + else if (e.metaKey) { + host.navigateFileEnd(); + } + if (e.shiftKey) { + host.selectDown(); + } + else { + host.navigateDown(); + } + return ace.stopEvent(e); + + case keys.LEFT: + if (e.altKey && e.shiftKey) { + host.selectWordLeft(); + } + else if (e.altKey) { + host.navigateWordLeft(); + } + else if (e.metaKey && e.shiftKey) { + host.selectLineStart(); + } + else if (e.metaKey) { + host.navigateLineStart(); + } + else if (e.shiftKey) { + host.selectLeft(); + } + else { + host.navigateLeft(); + } + return ace.stopEvent(e); + + case keys.RIGHT: + if (e.altKey && e.shiftKey) { + host.selectWordRight(); + } + else if (e.altKey) { + host.navigateWordRight(); + } + else if (e.metaKey && e.shiftKey) { + host.selectLineEnd(); + } + else if (e.metaKey) { + host.navigateLineEnd(); + } + else if (e.shiftKey) { + host.selectRight(); + } + else { + host.navigateRight(); + } + return ace.stopEvent(e); + + case keys.PAGEDOWN: + if (e.shiftKey) { + host.selectPageDown(); + } + else { + host.scrollPageDown(); + } + return ace.stopEvent(e); + + case keys.PAGEUP: + if (e.shiftKey) { + host.selectPageUp(); + } + else { + host.scrollPageUp(); + } + return ace.stopEvent(e); + + case keys.POS1: + if (e.shiftKey) { + host.selectLineStart(); + } + else { + host.navigateLineStart(); + } + return ace.stopEvent(e); + + case keys.END: + if (e.shiftKey) { + host.selectLineEnd(); + } + else { + host.navigateLineEnd(); + } + return ace.stopEvent(e); + + case keys.DELETE: + host.removeRight(); + return ace.stopEvent(e); + + case keys.BACKSPACE: + host.removeLeft(); + return ace.stopEvent(e); + + case keys.TAB: + if (host.hasMultiLineSelection()) { + if (e.shiftKey) { + host.blockOutdent(); + } else { + host.blockIndent(); + } + } else { + host.onTextInput(" "); + } + return ace.stopEvent(e); + } + }); +}; + +})(); \ No newline at end of file