refactored ace keybindings

This commit is contained in:
mikedeboer 2010-09-20 14:52:36 +02:00
commit 1d564140a5

View file

@ -5,26 +5,15 @@ require.def("ace/KeyBinding",
var KeyBinding = function(element, editor, config) { var KeyBinding = function(element, editor, config) {
this.editor = editor; this.editor = editor;
this.setConfig(config); this.setConfig(config);
var keys = this.keys;
var _self = this; var _self = this;
ace.addKeyListener(element, function(e) { ace.addKeyListener(element, function(e) {
var key = []; var hashId = 0 | (e.ctrlKey ? 1 : 0) | (e.altKey ? 2 : 0)
if (e.ctrlKey) { | (e.shiftKey ? 4 : 0) | (e.metaKey ? 8 : 0);
key.push("Ctrl"); var key = _self.keyNames[e.keyCode];
}
if (e.metaKey) {
key.push("Meta");
}
if (e.altKey) {
key.push("Alt");
}
if (e.shiftKey) {
key.push("Shift");
}
key.push(keys[e.keyCode] || String.fromCharCode(e.keyCode));
var command = _self[_self.config.reverse[key.join("-")]]; var command = _self[(_self.config.reverse[hashId] || {})[(key
|| String.fromCharCode(e.keyCode)).toLowerCase()]];
if (command) { if (command) {
_self.selection = editor.getSelection(); _self.selection = editor.getSelection();
command.call(_self); command.call(_self);
@ -34,24 +23,62 @@ var KeyBinding = function(element, editor, config) {
}; };
(function() { (function() {
this.keys = { this.keyMods = {"ctrl": 1, "alt": 2, "option" : 2, "shift": 4, "meta": 8, "command": 8};
8 : "Backspace",
9 : "Tab", this.keyNames = {
16 : "Shift", "8" : "Backspace",
17 : "Ctrl", "9" : "Tab",
18 : "Alt", "13" : "Enter",
33 : "PageUp", "27" : "Esc",
34 : "PageDown", "32" : "Space",
35 : "End", "33" : "PageUp",
36 : "Home", "34" : "PageDown",
37 : "Left", "35" : "End",
38 : "Up", "36" : "Home",
39 : "Right", "37" : "Left",
40 : "Down", "38" : "Up",
45 : "Insert", "39" : "Right",
46 : "Delete", "40" : "Down",
91 : "Meta" "45" : "Insert",
"46" : "Del",
"107": "+",
"112": "F1",
"113": "F2",
"114": "F3",
"115": "F4",
"116": "F5",
"117": "F6",
"118": "F7",
"119": "F8",
"120": "F9",
"121": "F10",
"122": "F11",
"123": "F12"
}; };
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);
}
function parseKeys(keys, val, ret) {
var key,
hashId = 0,
parts = splitSafe(keys, "\\-", null, true),
i = 0,
l = parts.length;
for (; i < l; ++i) {
if (this.keyMods[parts[i]])
hashId = hashId | this.keyMods[parts[i]];
else
key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
}
(ret[hashId] || (ret[hashId] = {}))[key] = val;
return ret;
}
function objectReverse(obj, keySplit) { function objectReverse(obj, keySplit) {
var i, j, l, key, var i, j, l, key,
@ -61,10 +88,10 @@ var KeyBinding = function(element, editor, config) {
if (keySplit && typeof key == "string") { if (keySplit && typeof key == "string") {
key = key.split(keySplit); key = key.split(keySplit);
for (j = 0, l = key.length; j < l; ++j) for (j = 0, l = key.length; j < l; ++j)
ret[key[j].replace(/Command/i, "Meta").replace(/Option/i, "Alt")] = i; parseKeys.call(this, key[j], i, ret);
} }
else { else {
ret[key.replace(/Command/i, "Meta").replace(/Option/i, "Alt")] = i; parseKeys.call(this, key, i, ret);
} }
} }
return ret; return ret;
@ -75,7 +102,7 @@ var KeyBinding = function(element, editor, config) {
? default_mac ? default_mac
: default_win); : default_win);
if (typeof this.config.reverse == "undefined") if (typeof this.config.reverse == "undefined")
this.config.reverse = objectReverse(this.config, "|"); this.config.reverse = objectReverse.call(this, this.config, "|");
}; };
this["selectall"] = function() { this["selectall"] = function() {