2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"settings!keys",
|
|
|
|
"command",
|
|
|
|
"editor",
|
|
|
|
"util/dom2"
|
|
|
|
], function(Settings, command, editor) {
|
2013-08-31 20:23:41 +00:00
|
|
|
|
2013-09-01 04:21:21 +00:00
|
|
|
var keycodes = {
|
2013-09-05 15:11:19 +00:00
|
|
|
9: "TAB",
|
|
|
|
13: "RETURN",
|
|
|
|
32: "SPACE",
|
2013-12-13 16:25:10 +00:00
|
|
|
33: "PAGEUP",
|
|
|
|
34: "PAGEDOWN",
|
|
|
|
35: "END",
|
|
|
|
36: "HOME",
|
2013-09-05 15:11:19 +00:00
|
|
|
37: "LEFT",
|
|
|
|
39: "RIGHT",
|
|
|
|
38: "UP",
|
2013-11-19 16:20:05 +00:00
|
|
|
40: "DOWN",
|
|
|
|
187: "=",
|
|
|
|
189: "-"
|
2013-09-03 15:47:59 +00:00
|
|
|
};
|
2013-09-01 04:21:21 +00:00
|
|
|
|
2013-09-05 15:24:46 +00:00
|
|
|
//need to remove existing Ace conflicts on init:start
|
2013-09-06 02:07:01 +00:00
|
|
|
var bindAce = function() {
|
|
|
|
var handler = editor.getKeyboardHandler();
|
|
|
|
var bindings = Settings.get("keys");
|
|
|
|
for (var k in bindings) {
|
|
|
|
var action = bindings[k];
|
|
|
|
if (!action.ace) continue;
|
2013-10-03 18:04:39 +00:00
|
|
|
k = k.replace("^", "Ctrl").replace("M", "Alt").replace(/-[A-Z]$/, function(match) {
|
|
|
|
return "-Shift" + match.toUpperCase();
|
|
|
|
}).replace(/-[a-z]$/, function(match) {
|
|
|
|
return match.toUpperCase();
|
|
|
|
});
|
2013-09-06 02:07:01 +00:00
|
|
|
handler.bindKey(k, action.ace);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
command.on("init:startup", bindAce);
|
|
|
|
command.on("init:restart", bindAce);
|
|
|
|
|
2013-09-05 15:24:46 +00:00
|
|
|
|
2013-09-01 04:21:21 +00:00
|
|
|
//we have to listen on keydown, because keypress will get caught by the window manager
|
2013-08-31 20:23:41 +00:00
|
|
|
window.on("keydown", function(e) {
|
|
|
|
var char = String.fromCharCode(e.keyCode);
|
2013-09-01 04:21:21 +00:00
|
|
|
if (e.keyCode in keycodes) {
|
|
|
|
char = keycodes[e.keyCode];
|
|
|
|
}
|
2013-09-05 15:11:19 +00:00
|
|
|
if (!e.shiftKey) char = char.toLowerCase();
|
2013-09-06 02:44:42 +00:00
|
|
|
var prefix = "";
|
2013-10-12 23:25:25 +00:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2013-09-06 02:44:42 +00:00
|
|
|
prefix += "^";
|
|
|
|
}
|
|
|
|
if (e.altKey) {
|
|
|
|
prefix += "M";
|
|
|
|
}
|
2013-12-13 17:48:56 +00:00
|
|
|
var combo = prefix ? prefix + "-" + char : char;
|
2013-09-03 15:47:59 +00:00
|
|
|
var keyConfig = Settings.get("keys");
|
2013-08-31 20:23:41 +00:00
|
|
|
if (combo in keyConfig) {
|
|
|
|
e.preventDefault();
|
2013-09-05 15:11:19 +00:00
|
|
|
var action = keyConfig[combo];
|
|
|
|
if (typeof action == "string") {
|
|
|
|
action = {
|
|
|
|
command: action
|
|
|
|
};
|
|
|
|
}
|
2013-09-05 15:24:46 +00:00
|
|
|
if (action.ace) {
|
2013-09-06 02:07:01 +00:00
|
|
|
//we're going to bind these directly on startup
|
|
|
|
//so we shouldn't act on them
|
|
|
|
return;// editor.execCommand(action.ace);
|
2013-09-05 15:24:46 +00:00
|
|
|
}
|
2013-09-05 15:11:19 +00:00
|
|
|
command.fire(action.command, action.argument);
|
2013-08-31 20:23:41 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-20 00:53:03 +00:00
|
|
|
|
|
|
|
});
|