Caret/js/ui/keys.js

110 lines
3.3 KiB
JavaScript
Raw Normal View History

define([
"settings!keys,user",
"command",
"editor",
"util/dom2",
"util/aceLoad!js/ace/keybinding-vim.js"
], function(Settings, command, editor) {
2013-08-31 20:23:41 +00:00
2013-09-01 04:21:21 +00:00
var keycodes = {
2013-12-16 22:58:37 +00:00
9: "Tab",
13: "Return",
27: "Esc",
2013-12-16 22:58:37 +00:00
32: "Space",
33: "PageUp",
34: "PageDown",
35: "End",
36: "Home",
37: "Left",
39: "Right",
38: "Up",
40: "Down",
186: ";",
187: "=",
189: "-",
190: ".",
192: "`",
220: "\\",
222: "'"
};
2013-09-01 04:21:21 +00:00
var defaultAceCommands = ace.require("./commands/default_commands").commands;
var AceCommandManager = ace.require("./commands/command_manager").CommandManager;
var vimHandler = ace.require("ace/keyboard/vim").handler;
//back-compat: we now use Ace-style bindings (Ctrl-X) instead of Vim-style (^-x)
var normalizeKeys = function(config) {
var converted = {};
for (var key in config) {
var value = config[key];
//detect old syntax
if (key.indexOf("^-") > -1 || key.indexOf("M-") > -1) {
key = key
.replace(/\^-/g, "Ctrl-")
.replace(/M-/g, "Alt-")
.replace(/-([A-Z]+)$/, "-Shift-$1")
.replace(/-([a-z]+)$/, function(match) { return match.toUpperCase() });
}
converted[key.toLowerCase()] = value;
}
return converted;
};
//need to auto-bind Ace keys, remove Ace conflicts
var bindAce = function() {
while (editor.keyBinding.removeKeyboardHandler(editor.getKeyboardHandler()));
var handler = new AceCommandManager("win", defaultAceCommands);
var bindings = normalizeKeys(Settings.get("keys"));
var ckb = handler.commandKeyBinding;
for (var k in bindings) {
var action = bindings[k];
//if (!action.ace) continue;
var parsed = handler.parseKeys(k);
var existing = handler.findKeyCommand(parsed.hashId, parsed.key);
var aceCommand = action.command == "ace:command" ? action.argument : action.ace;
if (!aceCommand) {
delete ckb[parsed.hashId][parsed.key];
} else {
handler.bindKey(k, aceCommand);
}
}
handler.commandKeyBinding = ckb;
editor.keyBinding.setDefaultHandler(handler);
if (Settings.get("user").emulateVim) {
editor.setKeyboardHandler(vimHandler);
}
};
command.on("init:startup", bindAce);
command.on("init:restart", bindAce);
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];
}
var prefixes = [];
if (e.ctrlKey) prefixes.push("Ctrl");
if (e.altKey) prefixes.push("Alt");
if (e.shiftKey) prefixes.push("Shift");
var combo = prefixes.length ? prefixes.join("-") + "-" + char : char;
combo = combo.toLowerCase();
var keyConfig = normalizeKeys(Settings.get("keys"));
2013-08-31 20:23:41 +00:00
if (combo in keyConfig) {
e.preventDefault();
var action = keyConfig[combo];
if (typeof action == "string") {
action = {
command: action
};
}
if (action.ace) {
//we're going to bind these directly on startup
//so we shouldn't act on them
return;// editor.execCommand(action.ace);
}
command.fire(action.command, action.argument);
2013-08-31 20:23:41 +00:00
}
});
2013-08-20 00:53:03 +00:00
});