fix keybinding in a slightly less kludgy way

This commit is contained in:
Joe Walker 2011-04-06 13:34:42 +01:00
commit b6e3f52faf
4 changed files with 91 additions and 183 deletions

View file

@ -397,8 +397,7 @@ exports.launch = function(env) {
name: "focuscli", name: "focuscli",
bindKey: { bindKey: {
win: "Ctrl-J", win: "Ctrl-J",
mac: "Command-J", mac: "Command-J"
sender: "editor"
}, },
exec: function() { exec: function() {
env.cli.cliView.element.focus(); env.cli.cliView.element.focus();
@ -410,8 +409,7 @@ exports.launch = function(env) {
name: "focuseditor", name: "focuseditor",
bindKey: { bindKey: {
win: "Ctrl-J", win: "Ctrl-J",
mac: "Command-J", mac: "Command-J"
sender: "cli"
}, },
exec: function() { exec: function() {
env.editor.focus(); env.editor.focus();
@ -423,32 +421,12 @@ exports.launch = function(env) {
name: "save", name: "save",
bindKey: { bindKey: {
win: "Ctrl-S", win: "Ctrl-S",
mac: "Command-S", mac: "Command-S"
sender: "editor|cli"
}, },
exec: function() { exec: function() {
alert("Fake Save File"); alert("Fake Save File");
} }
}); });
// Fake-Print with custom lookup-sender-match function.
canon.addCommand({
name: "save",
bindKey: {
win: "Ctrl-P",
mac: "Command-P",
sender: function(env, sender, hashId, keyString) {
if (sender == "editor") {
return true;
} else {
alert("Sorry, can only print from the editor");
}
}
},
exec: function() {
alert("Fake Print File");
}
});
}; };
}); });

View file

@ -43,14 +43,6 @@ define(function(require, exports, module) {
var lang = require("pilot/lang"); var lang = require("pilot/lang");
var gcli = require("cockpit/index"); var gcli = require("cockpit/index");
function bindKey(win, mac) {
return {
win: win,
mac: mac,
sender: "editor"
};
}
/** /**
* TODO: This could be done more concisely and reversibly * TODO: This could be done more concisely and reversibly
*/ */
@ -62,18 +54,18 @@ exports.startup = function() {
gcli.addCommand({ gcli.addCommand({
name: "selectall", name: "selectall",
bindKey: bindKey("Ctrl-A", "Command-A"), bindKey: { win: "Ctrl-A", mac: "Command-A" },
exec: function(env, args) { env.editor.selectAll(); } exec: function(env, args) { env.editor.selectAll(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "removeline", name: "removeline",
bindKey: bindKey("Ctrl-D", "Command-D"), bindKey: { win: "Ctrl-D", mac: "Command-D" },
exec: function(env, args) { env.editor.removeLines(); } exec: function(env, args) { env.editor.removeLines(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotoline", name: "gotoline",
description: "Move the cursor to the given line", description: "Move the cursor to the given line",
bindKey: bindKey("Ctrl-L", "Command-L"), bindKey: { win: "Ctrl-L", mac: "Command-L" },
params: [ params: [
{ name: "line", type: "number", description: "The line number to jump to" } { name: "line", type: "number", description: "The line number to jump to" }
], ],
@ -89,23 +81,23 @@ exports.startup = function() {
}); });
gcli.addCommand({ gcli.addCommand({
name: "togglecomment", name: "togglecomment",
bindKey: bindKey("Ctrl-7", "Command-7"), bindKey: { win: "Ctrl-7", mac: "Command-7" },
exec: function(env, args) { env.editor.toggleCommentLines(); } exec: function(env, args) { env.editor.toggleCommentLines(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "findnext", name: "findnext",
bindKey: bindKey("Ctrl-K", "Command-G"), bindKey: { win: "Ctrl-K", mac: "Command-G" },
exec: function(env, args) { env.editor.findNext(); } exec: function(env, args) { env.editor.findNext(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "findprevious", name: "findprevious",
bindKey: bindKey("Ctrl-Shift-K", "Command-Shift-G"), bindKey: { win: "Ctrl-Shift-K", mac: "Command-Shift-G" },
exec: function(env, args) { env.editor.findPrevious(); } exec: function(env, args) { env.editor.findPrevious(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "find", name: "find",
description: "Search for the next instance of a string", description: "Search for the next instance of a string",
bindKey: bindKey("Ctrl-F", "Command-F"), bindKey: { win: "Ctrl-F", mac: "Command-F" },
params: [ params: [
{ name: "findWhat", type: "string", description: "The text to search for" } { name: "findWhat", type: "string", description: "The text to search for" }
], ],
@ -120,7 +112,7 @@ exports.startup = function() {
gcli.addCommand({ gcli.addCommand({
name: "replace", name: "replace",
description: "Replace the next instance of a string with a given replacement", description: "Replace the next instance of a string with a given replacement",
bindKey: bindKey("Ctrl-R", "Command-Option-F"), bindKey: { win: "Ctrl-R", mac: "Command-Option-F" },
params: [ params: [
{ name: "findWhat", type: "string", description: "The text to search for" }, { name: "findWhat", type: "string", description: "The text to search for" },
{ name: "replacement", type: "string", description: "The replacement text" } { name: "replacement", type: "string", description: "The replacement text" }
@ -143,7 +135,7 @@ exports.startup = function() {
gcli.addCommand({ gcli.addCommand({
name: "replaceall", name: "replaceall",
description: "Replace all instances of a string with a given replacement", description: "Replace all instances of a string with a given replacement",
bindKey: bindKey("Ctrl-Shift-R", "Command-Shift-Option-F"), bindKey: { win: "Ctrl-Shift-R", mac: "Command-Shift-Option-F" },
params: [ params: [
{ name: "findWhat", type: "string", description: "The text to search for" }, { name: "findWhat", type: "string", description: "The text to search for" },
{ name: "replacement", type: "string", description: "The replacement text" } { name: "replacement", type: "string", description: "The replacement text" }
@ -165,220 +157,220 @@ exports.startup = function() {
}); });
gcli.addCommand({ gcli.addCommand({
name: "undo", name: "undo",
bindKey: bindKey("Ctrl-Z", "Command-Z"), bindKey: { win: "Ctrl-Z", mac: "Command-Z" },
exec: function(env, args) { env.editor.undo(); } exec: function(env, args) { env.editor.undo(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "redo", name: "redo",
bindKey: bindKey("Ctrl-Shift-Z|Ctrl-Y", "Command-Shift-Z|Command-Y"), bindKey: { win: "Ctrl-Shift-Z|Ctrl-Y", mac: "Command-Shift-Z|Command-Y" },
exec: function(env, args) { env.editor.redo(); } exec: function(env, args) { env.editor.redo(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "overwrite", name: "overwrite",
bindKey: bindKey("Insert", "Insert"), bindKey: { win: "Insert", mac: "Insert" },
exec: function(env, args) { env.editor.toggleOverwrite(); } exec: function(env, args) { env.editor.toggleOverwrite(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "copylinesup", name: "copylinesup",
bindKey: bindKey("Ctrl-Alt-Up", "Command-Option-Up"), bindKey: { win: "Ctrl-Alt-Up", mac: "Command-Option-Up" },
exec: function(env, args) { env.editor.copyLinesUp(); } exec: function(env, args) { env.editor.copyLinesUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "movelinesup", name: "movelinesup",
bindKey: bindKey("Alt-Up", "Option-Up"), bindKey: { win: "Alt-Up", mac: "Option-Up" },
exec: function(env, args) { env.editor.moveLinesUp(); } exec: function(env, args) { env.editor.moveLinesUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selecttostart", name: "selecttostart",
bindKey: bindKey("Alt-Shift-Up", "Command-Shift-Up"), bindKey: { win: "Alt-Shift-Up", mac: "Command-Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectFileStart(); } exec: function(env, args) { env.editor.getSelection().selectFileStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotostart", name: "gotostart",
bindKey: bindKey("Ctrl-Home|Ctrl-Up", "Command-Home|Command-Up"), bindKey: { win: "Ctrl-Home|Ctrl-Up", mac: "Command-Home|Command-Up" },
exec: function(env, args) { env.editor.navigateFileStart(); } exec: function(env, args) { env.editor.navigateFileStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectup", name: "selectup",
bindKey: bindKey("Shift-Up", "Shift-Up"), bindKey: { win: "Shift-Up", mac: "Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectUp(); } exec: function(env, args) { env.editor.getSelection().selectUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "golineup", name: "golineup",
bindKey: bindKey("Up", "Up|Ctrl-P"), bindKey: { win: "Up", mac: "Up|Ctrl-P" },
exec: function(env, args) { env.editor.navigateUp(args.times); } exec: function(env, args) { env.editor.navigateUp(args.times); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "copylinesdown", name: "copylinesdown",
bindKey: bindKey("Ctrl-Alt-Down", "Command-Option-Down"), bindKey: { win: "Ctrl-Alt-Down", mac: "Command-Option-Down" },
exec: function(env, args) { env.editor.copyLinesDown(); } exec: function(env, args) { env.editor.copyLinesDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "movelinesdown", name: "movelinesdown",
bindKey: bindKey("Alt-Down", "Option-Down"), bindKey: { win: "Alt-Down", mac: "Option-Down" },
exec: function(env, args) { env.editor.moveLinesDown(); } exec: function(env, args) { env.editor.moveLinesDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selecttoend", name: "selecttoend",
bindKey: bindKey("Alt-Shift-Down", "Command-Shift-Down"), bindKey: { win: "Alt-Shift-Down", mac: "Command-Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectFileEnd(); } exec: function(env, args) { env.editor.getSelection().selectFileEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotoend", name: "gotoend",
bindKey: bindKey("Ctrl-End|Ctrl-Down", "Command-End|Command-Down"), bindKey: { win: "Ctrl-End|Ctrl-Down", mac: "Command-End|Command-Down" },
exec: function(env, args) { env.editor.navigateFileEnd(); } exec: function(env, args) { env.editor.navigateFileEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectdown", name: "selectdown",
bindKey: bindKey("Shift-Down", "Shift-Down"), bindKey: { win: "Shift-Down", mac: "Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectDown(); } exec: function(env, args) { env.editor.getSelection().selectDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "golinedown", name: "golinedown",
bindKey: bindKey("Down", "Down|Ctrl-N"), bindKey: { win: "Down", mac: "Down|Ctrl-N" },
exec: function(env, args) { env.editor.navigateDown(args.times); } exec: function(env, args) { env.editor.navigateDown(args.times); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectwordleft", name: "selectwordleft",
bindKey: bindKey("Ctrl-Shift-Left", "Option-Shift-Left"), bindKey: { win: "Ctrl-Shift-Left", mac: "Option-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectWordLeft(); } exec: function(env, args) { env.editor.getSelection().selectWordLeft(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotowordleft", name: "gotowordleft",
bindKey: bindKey("Ctrl-Left", "Option-Left"), bindKey: { win: "Ctrl-Left", mac: "Option-Left" },
exec: function(env, args) { env.editor.navigateWordLeft(); } exec: function(env, args) { env.editor.navigateWordLeft(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selecttolinestart", name: "selecttolinestart",
bindKey: bindKey("Alt-Shift-Left", "Command-Shift-Left"), bindKey: { win: "Alt-Shift-Left", mac: "Command-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); } exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotolinestart", name: "gotolinestart",
bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"), bindKey: { win: "Alt-Left|Home", mac: "Command-Left|Home|Ctrl-A" },
exec: function(env, args) { env.editor.navigateLineStart(); } exec: function(env, args) { env.editor.navigateLineStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectleft", name: "selectleft",
bindKey: bindKey("Shift-Left", "Shift-Left"), bindKey: { win: "Shift-Left", mac: "Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLeft(); } exec: function(env, args) { env.editor.getSelection().selectLeft(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotoleft", name: "gotoleft",
bindKey: bindKey("Left", "Left|Ctrl-B"), bindKey: { win: "Left", mac: "Left|Ctrl-B" },
exec: function(env, args) { env.editor.navigateLeft(args.times); } exec: function(env, args) { env.editor.navigateLeft(args.times); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectwordright", name: "selectwordright",
bindKey: bindKey("Ctrl-Shift-Right", "Option-Shift-Right"), bindKey: { win: "Ctrl-Shift-Right", mac: "Option-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectWordRight(); } exec: function(env, args) { env.editor.getSelection().selectWordRight(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotowordright", name: "gotowordright",
bindKey: bindKey("Ctrl-Right", "Option-Right"), bindKey: { win: "Ctrl-Right", mac: "Option-Right" },
exec: function(env, args) { env.editor.navigateWordRight(); } exec: function(env, args) { env.editor.navigateWordRight(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selecttolineend", name: "selecttolineend",
bindKey: bindKey("Alt-Shift-Right", "Command-Shift-Right"), bindKey: { win: "Alt-Shift-Right", mac: "Command-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); } exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotolineend", name: "gotolineend",
bindKey: bindKey("Alt-Right|End", "Command-Right|End|Ctrl-E"), bindKey: { win: "Alt-Right|End", mac: "Command-Right|End|Ctrl-E" },
exec: function(env, args) { env.editor.navigateLineEnd(); } exec: function(env, args) { env.editor.navigateLineEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectright", name: "selectright",
bindKey: bindKey("Shift-Right", "Shift-Right"), bindKey: { win: "Shift-Right", mac: "Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectRight(); } exec: function(env, args) { env.editor.getSelection().selectRight(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotoright", name: "gotoright",
bindKey: bindKey("Right", "Right|Ctrl-F"), bindKey: { win: "Right", mac: "Right|Ctrl-F" },
exec: function(env, args) { env.editor.navigateRight(args.times); } exec: function(env, args) { env.editor.navigateRight(args.times); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectpagedown", name: "selectpagedown",
bindKey: bindKey("Shift-PageDown", "Shift-PageDown"), bindKey: { win: "Shift-PageDown", mac: "Shift-PageDown" },
exec: function(env, args) { env.editor.selectPageDown(); } exec: function(env, args) { env.editor.selectPageDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "pagedown", name: "pagedown",
bindKey: bindKey(null, "PageDown"), bindKey: { win: null, mac: "PageDown" },
exec: function(env, args) { env.editor.scrollPageDown(); } exec: function(env, args) { env.editor.scrollPageDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotopagedown", name: "gotopagedown",
bindKey: bindKey("PageDown", "Option-PageDown|Ctrl-V"), bindKey: { win: "PageDown", mac: "Option-PageDown|Ctrl-V" },
exec: function(env, args) { env.editor.gotoPageDown(); } exec: function(env, args) { env.editor.gotoPageDown(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectpageup", name: "selectpageup",
bindKey: bindKey("Shift-PageUp", "Shift-PageUp"), bindKey: { win: "Shift-PageUp", mac: "Shift-PageUp" },
exec: function(env, args) { env.editor.selectPageUp(); } exec: function(env, args) { env.editor.selectPageUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "pageup", name: "pageup",
bindKey: bindKey(null, "PageUp"), bindKey: { win: null, mac: "PageUp" },
exec: function(env, args) { env.editor.scrollPageUp(); } exec: function(env, args) { env.editor.scrollPageUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "gotopageup", name: "gotopageup",
bindKey: bindKey("PageUp", "Option-PageUp"), bindKey: { win: "PageUp", mac: "Option-PageUp" },
exec: function(env, args) { env.editor.gotoPageUp(); } exec: function(env, args) { env.editor.gotoPageUp(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectlinestart", name: "selectlinestart",
bindKey: bindKey("Shift-Home", "Shift-Home"), bindKey: { win: "Shift-Home", mac: "Shift-Home" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); } exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "selectlineend", name: "selectlineend",
bindKey: bindKey("Shift-End", "Shift-End"), bindKey: { win: "Shift-End", mac: "Shift-End" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); } exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "del", name: "del",
bindKey: bindKey("Delete", "Delete|Ctrl-D"), bindKey: { win: "Delete", mac: "Delete|Ctrl-D" },
exec: function(env, args) { env.editor.removeRight(); } exec: function(env, args) { env.editor.removeRight(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "backspace", name: "backspace",
bindKey: bindKey( bindKey: {
"Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace", win: "Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace",
"Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H" mac: "Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H"
), },
exec: function(env, args) { env.editor.removeLeft(); } exec: function(env, args) { env.editor.removeLeft(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "removetolinestart", name: "removetolinestart",
bindKey: bindKey(null, "Option-Backspace"), bindKey: { win: null, mac: "Option-Backspace" },
exec: function(env, args) { env.editor.removeToLineStart(); } exec: function(env, args) { env.editor.removeToLineStart(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "removetolineend", name: "removetolineend",
bindKey: bindKey(null, "Ctrl-K"), bindKey: { win: null, mac: "Ctrl-K" },
exec: function(env, args) { env.editor.removeToLineEnd(); } exec: function(env, args) { env.editor.removeToLineEnd(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "removewordleft", name: "removewordleft",
bindKey: bindKey(null, "Alt-Backspace|Ctrl-Alt-Backspace"), bindKey: { win: null, mac: "Alt-Backspace|Ctrl-Alt-Backspace" },
exec: function(env, args) { env.editor.removeWordLeft(); } exec: function(env, args) { env.editor.removeWordLeft(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "removewordright", name: "removewordright",
bindKey: bindKey(null, "Alt-Delete"), bindKey: { win: null, mac: "Alt-Delete" },
exec: function(env, args) { env.editor.removeWordRight(); } exec: function(env, args) { env.editor.removeWordRight(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "outdent", name: "outdent",
bindKey: bindKey("Shift-Tab", "Shift-Tab"), bindKey: { win: "Shift-Tab", mac: "Shift-Tab" },
exec: function(env, args) { env.editor.blockOutdent(); } exec: function(env, args) { env.editor.blockOutdent(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "indent", name: "indent",
bindKey: bindKey("Tab", "Tab"), bindKey: { win: "Tab", mac: "Tab" },
exec: function(env, args) { env.editor.indent(); } exec: function(env, args) { env.editor.indent(); }
}); });
gcli.addCommand({ gcli.addCommand({
@ -389,17 +381,17 @@ exports.startup = function() {
}); });
gcli.addCommand({ gcli.addCommand({
name: "centerselection", name: "centerselection",
bindKey: bindKey(null, "Ctrl-L"), bindKey: { win: null, mac: "Ctrl-L" },
exec: function(env, args) { env.editor.centerSelection(); } exec: function(env, args) { env.editor.centerSelection(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "splitline", name: "splitline",
bindKey: bindKey(null, "Ctrl-O"), bindKey: { win: null, mac: "Ctrl-O" },
exec: function(env, args) { env.editor.splitLine(); } exec: function(env, args) { env.editor.splitLine(); }
}); });
gcli.addCommand({ gcli.addCommand({
name: "transposeletters", name: "transposeletters",
bindKey: bindKey("Ctrl-T", "Ctrl-T"), bindKey: { win: "Ctrl-T", mac: "Ctrl-T" },
exec: function(env, args) { env.editor.transposeLetters(); } exec: function(env, args) { env.editor.transposeLetters(); }
}); });
}; };

View file

@ -77,7 +77,7 @@ var KeyBinding = function(editor) {
if (!toExecute || !toExecute.command) { if (!toExecute || !toExecute.command) {
if (hashId != 0 || keyCode != 0) { if (hashId != 0 || keyCode != 0) {
toExecute = { toExecute = {
command: findKeyCommand(env, "editor", hashId, keyOrText) command: findKeyCommand(hashId, keyOrText)
}; };
} else { } else {
toExecute = { toExecute = {
@ -91,7 +91,7 @@ var KeyBinding = function(editor) {
if (toExecute) { if (toExecute) {
var success = canon.exec(toExecute.command, var success = canon.exec(toExecute.command,
env, "editor", toExecute.args); env, toExecute.args);
if (success) { if (success) {
return event.stopEvent(e); return event.stopEvent(e);
} }
@ -113,14 +113,9 @@ exports.KeyBinding = KeyBinding;
/** /**
* A lookup has for command key bindings that use a string as sender. * A lookup has for command key bindings.
*/ */
var commmandKeyBinding = {}; var commmandKeyBinding = { };
/**
* Array with command key bindings that use a function to determine the sender.
*/
var commandKeyBindingFunc = { };
function splitSafe(s, separator, limit, bLowerCase) { function splitSafe(s, separator, limit, bLowerCase) {
return (bLowerCase && s.toLowerCase() || s) return (bLowerCase && s.toLowerCase() || s)
@ -128,112 +123,55 @@ function splitSafe(s, separator, limit, bLowerCase) {
.split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999); .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 (keyUtil.KEY_MODS[parts[i]])
hashId = hashId | keyUtil.KEY_MODS[parts[i]];
else
key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
}
if (ret == null) {
return {
key: key,
hashId: hashId
};
} else {
(ret[hashId] || (ret[hashId] = {}))[key] = val;
}
}
var platform = useragent.isMac ? "mac" : "win"; var platform = useragent.isMac ? "mac" : "win";
function buildKeyHash(command) {
var binding = command.bindKey,
key = binding[platform],
ckb = commmandKeyBinding,
ckbf = commandKeyBindingFunc;
if (!binding.sender) { function buildKeyHash(binding, command) {
throw new Error('All key bindings must have a sender');
}
if (!binding.mac && binding.mac !== null) { if (!binding.mac && binding.mac !== null) {
throw new Error('All key bindings must have a mac key binding'); throw new Error('All key bindings must have a mac key binding');
} }
if (!binding.win && binding.win !== null) { if (!binding.win && binding.win !== null) {
throw new Error('All key bindings must have a windows key binding'); throw new Error('All key bindings must have a windows key binding');
} }
if(!binding[platform]) { if (!binding[platform]) {
// No key mapping for this platform. // No key mapping for this platform.
return; return;
} }
if (typeof binding.sender == 'string') {
var targets = splitSafe(binding.sender, "\\|", null, true);
targets.forEach(function(target) {
if (!ckb[target]) {
ckb[target] = { };
}
key.split("|").forEach(function(keyPart) {
parseKeys(keyPart, command, ckb[target]);
});
});
} else if (typecheck.isFunction(binding.sender)) {
var val = {
command: command,
sender: binding.sender
};
keyData = parseKeys(key); binding[platform].split("|").forEach(function(keyPart) {
if (!ckbf[keyData.hashId]) { parseKeys(keyPart, command);
ckbf[keyData.hashId] = { }; });
} }
if (!ckbf[keyData.hashId][keyData.key]) {
ckbf[keyData.hashId][keyData.key] = [ val ]; function parseKeys(keys, command) {
} else { var key;
ckbf[keyData.hashId][keyData.key].push(val); var hashId = 0;
}
} else { var parts = splitSafe(keys, "\\-", null, true);
throw new Error('Key binding must have a sender that is a string or function'); parts.forEach(function(part) {
if (keyUtil.KEY_MODS[part])
hashId = hashId | keyUtil.KEY_MODS[part];
else
key = part || "-"; // when empty, the splitSafe removed a '-'
});
if (commmandKeyBinding[hashId] == null) {
commmandKeyBinding[hashId] = {};
} }
commmandKeyBinding[hashId][key] = command;
} }
canon.buildKeyHash = buildKeyHash; canon.buildKeyHash = buildKeyHash;
function findKeyCommand(env, sender, hashId, textOrKey) { function findKeyCommand(hashId, textOrKey) {
// Convert keyCode to the string representation. // Convert keyCode to the string representation.
if (typecheck.isNumber(textOrKey)) { if (typecheck.isNumber(textOrKey)) {
textOrKey = keyUtil.keyCodeToString(textOrKey); textOrKey = keyUtil.keyCodeToString(textOrKey);
} }
// Check bindings with functions as sender first. return commmandKeyBinding[hashId] && commmandKeyBinding[hashId][textOrKey];
var bindFuncs = (commandKeyBindingFunc[hashId] || {})[textOrKey] || [];
for (var i = 0; i < bindFuncs.length; i++) {
if (bindFuncs[i].sender(env, sender, hashId, textOrKey)) {
return bindFuncs[i].command;
}
}
var ckbr = commmandKeyBinding[sender];
return ckbr && ckbr[hashId] && ckbr[hashId][textOrKey];
} }
exports.findKeyCommand = findKeyCommand;
function execKeyCommand(env, sender, hashId, textOrKey) {
var command = findKeyCommand(env, sender, hashId, textOrKey);
if (command) {
return exec(command, env, sender, { });
} else {
return false;
}
}
exports.execKeyCommand = execKeyCommand;
}); });

@ -1 +1 @@
Subproject commit 81d897e6248d4304655dc068353af40a94e35c7a Subproject commit 5ad5e2778ad09ad602e6c15c0b3dfa6fdf388e79