Merge pull request #2073 from ajaxorg/multihash

Allow binding multiple commands to one key
This commit is contained in:
Lennart Kats 2014-10-10 15:41:23 +02:00
commit a02ca596b9
9 changed files with 183 additions and 155 deletions

View file

@ -2,13 +2,12 @@ define(function(require, exports, module) {
"use strict"; "use strict";
var oop = require("../lib/oop"); var oop = require("../lib/oop");
var HashHandler = require("../keyboard/hash_handler").HashHandler; var MultiHashHandler = require("../keyboard/hash_handler").MultiHashHandler;
var EventEmitter = require("../lib/event_emitter").EventEmitter; var EventEmitter = require("../lib/event_emitter").EventEmitter;
/** /**
* @class CommandManager * @class CommandManager
* *
*
**/ **/
/** /**
@ -19,20 +18,27 @@ var EventEmitter = require("../lib/event_emitter").EventEmitter;
**/ **/
var CommandManager = function(platform, commands) { var CommandManager = function(platform, commands) {
HashHandler.call(this, commands, platform); MultiHashHandler.call(this, commands, platform);
this.byName = this.commands; this.byName = this.commands;
this.setDefaultHandler("exec", function(e) { this.setDefaultHandler("exec", function(e) {
return e.command.exec(e.editor, e.args || {}); return e.command.exec(e.editor, e.args || {});
}); });
}; };
oop.inherits(CommandManager, HashHandler); oop.inherits(CommandManager, MultiHashHandler);
(function() { (function() {
oop.implement(this, EventEmitter); oop.implement(this, EventEmitter);
this.exec = function(command, editor, args) { this.exec = function(command, editor, args) {
if (Array.isArray(command)) {
for (var i = command.length; i--; ) {
if (this.exec(command[i], editor, args)) return true;
}
return false;
}
if (typeof command === 'string') if (typeof command === 'string')
command = this.commands[command]; command = this.commands[command];
@ -43,10 +49,10 @@ oop.inherits(CommandManager, HashHandler);
return false; return false;
var e = {editor: editor, command: command, args: args}; var e = {editor: editor, command: command, args: args};
var retvalue = this._emit("exec", e); e.returnValue = this._emit("exec", e);
this._signal("afterExec", e); this._signal("afterExec", e);
return retvalue === false ? false : true; return e.returnValue === false ? false : true;
}; };
this.toggleRecording = function(editor) { this.toggleRecording = function(editor) {

View file

@ -188,7 +188,7 @@ module.exports = {
assert.equal(command, "cm2"); assert.equal(command, "cm2");
var command = this.cm.findKeyCommand(0, "return"); var command = this.cm.findKeyCommand(0, "return");
assert.equal(command, "cm3"); assert.equal(command + "", ["cm4", "cm3"] + "");
} }
}; };

View file

@ -115,30 +115,8 @@ var Editor = function(renderer, session) {
function last(a) {return a[a.length - 1]} function last(a) {return a[a.length - 1]}
this.selections = []; this.selections = [];
this.commands.on("exec", function(e) { this.commands.on("exec", this.startOperation.bind(this), true);
this.startOperation(e); this.commands.on("afterExec", this.endOperation.bind(this), true);
var command = e.command;
if (command.aceCommandGroup == "fileJump") {
var prev = this.prevOp;
if (!prev || prev.command.aceCommandGroup != "fileJump") {
this.lastFileJumpPos = last(this.selections);
}
} else {
this.lastFileJumpPos = null;
}
}.bind(this), true);
this.commands.on("afterExec", function(e) {
var command = e.command;
if (command.aceCommandGroup == "fileJump") {
if (this.lastFileJumpPos && !this.curOp.selectionChanged) {
this.selection.fromJSON(this.lastFileJumpPos);
}
}
this.endOperation(e);
}.bind(this), true);
this.$opResetTimer = lang.delayedCall(this.endOperation.bind(this)); this.$opResetTimer = lang.delayedCall(this.endOperation.bind(this));
@ -173,18 +151,16 @@ var Editor = function(renderer, session) {
scrollTop: this.renderer.scrollTop scrollTop: this.renderer.scrollTop
}; };
var command = this.curOp.command; // this.selections.push(this.selection.toJSON());
if (command && command.scrollIntoView)
this.$blockScrolling++;
this.selections.push(this.selection.toJSON());
}; };
this.endOperation = function() { this.endOperation = function(e) {
if (this.curOp) { if (this.curOp) {
if (e && e.returnValue === false)
return this.curOp = null;
var command = this.curOp.command; var command = this.curOp.command;
if (command && command.scrollIntoView) { if (command && command.scrollIntoView) {
this.$blockScrolling--;
switch (command.scrollIntoView) { switch (command.scrollIntoView) {
case "center": case "center":
this.renderer.scrollCursorIntoView(null, 0.5); this.renderer.scrollCursorIntoView(null, 0.5);
@ -255,19 +231,19 @@ var Editor = function(renderer, session) {
* @param {String} keyboardHandler The new key handler * @param {String} keyboardHandler The new key handler
* *
**/ **/
this.setKeyboardHandler = function(keyboardHandler) { this.setKeyboardHandler = function(keyboardHandler, cb) {
if (!keyboardHandler) { if (keyboardHandler && typeof keyboardHandler === "string") {
this.keyBinding.setKeyboardHandler(null);
} else if (typeof keyboardHandler === "string") {
this.$keybindingId = keyboardHandler; this.$keybindingId = keyboardHandler;
var _self = this; var _self = this;
config.loadModule(["keybinding", keyboardHandler], function(module) { config.loadModule(["keybinding", keyboardHandler], function(module) {
if (_self.$keybindingId == keyboardHandler) if (_self.$keybindingId == keyboardHandler)
_self.keyBinding.setKeyboardHandler(module && module.handler); _self.keyBinding.setKeyboardHandler(module && module.handler);
cb && cb();
}); });
} else { } else {
this.$keybindingId = null; this.$keybindingId = null;
this.keyBinding.setKeyboardHandler(keyboardHandler); this.keyBinding.setKeyboardHandler(keyboardHandler);
cb && cb();
} }
}; };
@ -931,9 +907,8 @@ var Editor = function(renderer, session) {
this.insert(e.text, true); this.insert(e.text, true);
}; };
this.execCommand = function(command, args) { this.execCommand = function(command, args) {
this.commands.exec(command, this, args); return this.commands.exec(command, this, args);
}; };
/** /**

View file

@ -34,15 +34,7 @@ var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
var Editor = require("ace/editor").Editor; var Editor = require("ace/editor").Editor;
var snippetManager = require("ace/snippets").snippetManager; var snippetManager = require("ace/snippets").snippetManager;
var Range = require("ace/range").Range; var Range = require("ace/range").Range;
var emmet; var emmet, emmetPath;
Editor.prototype.indexToPosition = function(index) {
return this.session.doc.indexToPosition(index);
};
Editor.prototype.positionToIndex = function(pos) {
return this.session.doc.positionToIndex(pos);
};
/** /**
* Implementation of {@link IEmmetEditor} interface for Ace * Implementation of {@link IEmmetEditor} interface for Ace
@ -72,9 +64,10 @@ AceEmmetEditor.prototype = {
getSelectionRange: function() { getSelectionRange: function() {
// TODO should start be caret position instead? // TODO should start be caret position instead?
var range = this.ace.getSelectionRange(); var range = this.ace.getSelectionRange();
var doc = this.ace.session.doc;
return { return {
start: this.ace.positionToIndex(range.start), start: doc.positionToIndex(range.start),
end: this.ace.positionToIndex(range.end) end: doc.positionToIndex(range.end)
}; };
}, },
@ -91,9 +84,10 @@ AceEmmetEditor.prototype = {
* editor.createSelection(15); * editor.createSelection(15);
*/ */
createSelection: function(start, end) { createSelection: function(start, end) {
var doc = this.ace.session.doc;
this.ace.selection.setRange({ this.ace.selection.setRange({
start: this.ace.indexToPosition(start), start: doc.indexToPosition(start),
end: this.ace.indexToPosition(end) end: doc.indexToPosition(end)
}); });
}, },
@ -106,9 +100,10 @@ AceEmmetEditor.prototype = {
* alert(range.start + ', ' + range.end); * alert(range.start + ', ' + range.end);
*/ */
getCurrentLineRange: function() { getCurrentLineRange: function() {
var row = this.ace.getCursorPosition().row; var ace = this.ace;
var lineLength = this.ace.session.getLine(row).length; var row = ace.getCursorPosition().row;
var index = this.ace.positionToIndex({row: row, column: 0}); var lineLength = ace.session.getLine(row).length;
var index = ace.session.doc.positionToIndex({row: row, column: 0});
return { return {
start: index, start: index,
end: index + lineLength end: index + lineLength
@ -121,7 +116,7 @@ AceEmmetEditor.prototype = {
*/ */
getCaretPos: function(){ getCaretPos: function(){
var pos = this.ace.getCursorPosition(); var pos = this.ace.getCursorPosition();
return this.ace.positionToIndex(pos); return this.ace.session.doc.positionToIndex(pos);
}, },
/** /**
@ -129,7 +124,7 @@ AceEmmetEditor.prototype = {
* @param {Number} index Caret position * @param {Number} index Caret position
*/ */
setCaretPos: function(index){ setCaretPos: function(index){
var pos = this.ace.indexToPosition(index); var pos = this.ace.session.doc.indexToPosition(index);
this.ace.selection.moveToPosition(pos); this.ace.selection.moveToPosition(pos);
}, },
@ -169,14 +164,15 @@ AceEmmetEditor.prototype = {
start = 0; start = 0;
var editor = this.ace; var editor = this.ace;
var range = Range.fromPoints(editor.indexToPosition(start), editor.indexToPosition(end)); var doc = editor.session.doc;
var range = Range.fromPoints(doc.indexToPosition(start), doc.indexToPosition(end));
editor.session.remove(range); editor.session.remove(range);
range.end = range.start; range.end = range.start;
//editor.selection.setRange(range); //editor.selection.setRange(range);
value = this.$updateTabstops(value); value = this.$updateTabstops(value);
snippetManager.insertSnippet(editor, value) snippetManager.insertSnippet(editor, value);
}, },
/** /**
@ -292,7 +288,7 @@ AceEmmetEditor.prototype = {
lastZero = range.create(data.start, result); lastZero = range.create(data.start, result);
} }
return result return result;
}, },
escape: function(ch) { escape: function(ch) {
if (ch == '$') return '\\$'; if (ch == '$') return '\\$';
@ -363,12 +359,17 @@ exports.runEmmetCommand = function(editor) {
}, 0); }, 0);
} }
var pos = editor.selection.lead;
var token = editor.session.getTokenAt(pos.row, pos.column);
if (token && /\btag\b/.test(token.type))
return false;
try { try {
var result = actions.run(this.action, editorProxy); var result = actions.run(this.action, editorProxy);
} catch(e) { } catch(e) {
editor._signal("changeStatus", typeof e == "string" ? e : e.message); editor._signal("changeStatus", typeof e == "string" ? e : e.message);
console.log(e); console.log(e);
result = false result = false;
} }
return result; return result;
}; };
@ -383,21 +384,36 @@ for (var command in keymap) {
}); });
} }
exports.updateCommands = function(editor, enabled) {
if (enabled) {
editor.keyBinding.addKeyboardHandler(exports.commands);
} else {
editor.keyBinding.removeKeyboardHandler(exports.commands);
}
};
exports.isSupportedMode = function(modeId) {
return modeId && /css|less|scss|sass|stylus|html|php|twig/.test(modeId);
};
var onChangeMode = function(e, target) { var onChangeMode = function(e, target) {
var editor = target; var editor = target;
if (!editor) if (!editor)
return; return;
var modeId = editor.session.$modeId; var enabled = exports.isSupportedMode(editor.session.$modeId);
var enabled = modeId && /css|less|scss|sass|stylus|html|php/.test(modeId);
if (e.enableEmmet === false) if (e.enableEmmet === false)
enabled = false; enabled = false;
if (enabled) if (enabled) {
editor.keyBinding.addKeyboardHandler(exports.commands); if (typeof emmetPath == "string") {
else require("ace/config").loadModule(emmetPath, function() {
editor.keyBinding.removeKeyboardHandler(exports.commands);
});
emmetPath = null;
}
}
exports.updateCommands(editor, enabled);
}; };
exports.AceEmmetEditor = AceEmmetEditor; exports.AceEmmetEditor = AceEmmetEditor;
require("ace/config").defineOptions(Editor.prototype, "editor", { require("ace/config").defineOptions(Editor.prototype, "editor", {
enableEmmet: { enableEmmet: {
@ -409,7 +425,11 @@ require("ace/config").defineOptions(Editor.prototype, "editor", {
} }
}); });
exports.setCore = function(e) {
exports.setCore = function(e) {emmet = e;}; if (typeof e == "string")
emmetPath = e;
else
emmet = e;
};
}); });

View file

@ -95,9 +95,7 @@ exports.snippetCompleter = snippetCompleter;
var expandSnippet = { var expandSnippet = {
name: "expandSnippet", name: "expandSnippet",
exec: function(editor) { exec: function(editor) {
var success = snippetManager.expandWithTab(editor); return snippetManager.expandWithTab(editor);
if (!success)
editor.execCommand("indent");
}, },
bindKey: "Tab" bindKey: "Tab"
}; };

View file

@ -69,29 +69,20 @@ module.exports.getEditorKeybordShortcuts = function(editor) {
editor.keyBinding.$handlers.forEach(function(handler) { editor.keyBinding.$handlers.forEach(function(handler) {
var ckb = handler.commandKeyBinding; var ckb = handler.commandKeyBinding;
for (var i in ckb) { for (var i in ckb) {
var modifier = parseInt(i); var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase(); });
if (modifier == -1) { var commands = ckb[i];
modifier = ""; if (!Array.isArray(commands))
} else if(isNaN(modifier)) { commands = [commands];
modifier = i; commands.forEach(function(command) {
} else {
modifier = "" +
(modifier & KEY_MODS.command ? "Cmd-" : "") +
(modifier & KEY_MODS.ctrl ? "Ctrl-" : "") +
(modifier & KEY_MODS.alt ? "Alt-" : "") +
(modifier & KEY_MODS.shift ? "Shift-" : "");
}
for (var key in ckb[i]) {
var command = ckb[i][key]
if (typeof command != "string") if (typeof command != "string")
command = command.name command = command.name
if (commandMap[command]) { if (commandMap[command]) {
commandMap[command].key += "|" + modifier + key; commandMap[command].key += "|" + key;
} else { } else {
commandMap[command] = {key: modifier+key, command: command}; commandMap[command] = {key: key, command: command};
keybindings.push(commandMap[command]); keybindings.push(commandMap[command]);
} }
} });
} }
}); });
return keybindings; return keybindings;

View file

@ -64,7 +64,7 @@ exports.handler.attach = function(editor) {
initialized = true; initialized = true;
dom.importCssString('\ dom.importCssString('\
.emacs-mode .ace_cursor{\ .emacs-mode .ace_cursor{\
border: 2px rgba(50,250,50,0.8) solid!important;\ border: 1px rgba(50,250,50,0.8) solid!important;\
-moz-box-sizing: border-box!important;\ -moz-box-sizing: border-box!important;\
-webkit-box-sizing: border-box!important;\ -webkit-box-sizing: border-box!important;\
box-sizing: border-box!important;\ box-sizing: border-box!important;\
@ -195,6 +195,8 @@ exports.handler.onPaste = function(e, editor) {
}; };
exports.handler.bindKey = function(key, command) { exports.handler.bindKey = function(key, command) {
if (typeof key == "object")
key = key[this.platform];
if (!key) if (!key)
return; return;

View file

@ -33,37 +33,25 @@ define(function(require, exports, module) {
var keyUtil = require("../lib/keys"); var keyUtil = require("../lib/keys");
var useragent = require("../lib/useragent"); var useragent = require("../lib/useragent");
var KEY_MODS = keyUtil.KEY_MODS;
function HashHandler(config, platform) { function HashHandler(config, platform) {
this.platform = platform || (useragent.isMac ? "mac" : "win"); this.platform = platform || (useragent.isMac ? "mac" : "win");
this.commands = {}; this.commands = {};
this.commandKeyBinding = {}; this.commandKeyBinding = {};
// todo remove this after a while
if (this.__defineGetter__ && this.__defineSetter__ && typeof console != "undefined" && console.error) {
var warned = false;
var warn = function() {
if (!warned) {
warned = true;
console.error("commmandKeyBinding has too many m's. use commandKeyBinding");
}
};
this.__defineGetter__("commmandKeyBinding", function() {
warn();
return this.commandKeyBinding;
});
this.__defineSetter__("commmandKeyBinding", function(val) {
warn();
return this.commandKeyBinding = val;
});
} else {
this.commmandKeyBinding = this.commandKeyBinding;
}
this.addCommands(config); this.addCommands(config);
}; this.$singleCommand = true;
}
function MultiHashHandler(config, platform) {
HashHandler.call(this, config, platform);
this.$singleCommand = false;
}
MultiHashHandler.prototype = HashHandler.prototype;
(function() { (function() {
this.addCommand = function(command) { this.addCommand = function(command) {
if (this.commands[command.name]) if (this.commands[command.name])
@ -75,37 +63,76 @@ function HashHandler(config, platform) {
this._buildKeyHash(command); this._buildKeyHash(command);
}; };
this.removeCommand = function(command) { this.removeCommand = function(command, keepCommand) {
var name = (typeof command === 'string' ? command : command.name); var name = command && (typeof command === 'string' ? command : command.name);
command = this.commands[name]; command = this.commands[name];
delete this.commands[name]; if (!keepCommand)
delete this.commands[name];
// exhaustive search is brute force but since removeCommand is // exhaustive search is brute force but since removeCommand is
// not a performance critical operation this should be OK // not a performance critical operation this should be OK
var ckb = this.commandKeyBinding; var ckb = this.commandKeyBinding;
for (var hashId in ckb) { for (var keyId in ckb) {
for (var key in ckb[hashId]) { var cmdGroup = ckb[keyId];
if (ckb[hashId][key] == command) if (cmdGroup == command) {
delete ckb[hashId][key]; delete ckb[keyId];
} else if (Array.isArray(cmdGroup)) {
var i = cmdGroup.indexOf(command);
if (i != -1) {
cmdGroup.splice(i, 1);
if (cmdGroup.length == 1)
ckb[keyId] = cmdGroup[0];
}
} }
} }
}; };
this.bindKey = function(key, command) { this.bindKey = function(key, command, asDefault) {
if(!key) if (typeof key == "object")
key = key[this.platform];
if (!key)
return; return;
if (typeof command == "function") { if (typeof command == "function")
this.addCommand({exec: command, bindKey: key, name: command.name || key}); return this.addCommand({exec: command, bindKey: key, name: command.name || key});
return;
}
var ckb = this.commandKeyBinding;
key.split("|").forEach(function(keyPart) { key.split("|").forEach(function(keyPart) {
var binding = this.parseKeys(keyPart, command); var chain = "";
var hashId = binding.hashId; if (keyPart.indexOf(" ") != -1) {
(ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command; var parts = keyPart.split(/\s+/);
keyPart = parts.pop();
parts.forEach(function(keyPart) {
var binding = this.parseKeys(keyPart);
var id = KEY_MODS[binding.hashId] + binding.key;
chain += (chain ? " " : "") + id;
this._addCommandToBinding(chain, "chainKeys");
}, this);
chain += " ";
}
var binding = this.parseKeys(keyPart);
var id = KEY_MODS[binding.hashId] + binding.key;
this._addCommandToBinding(chain + id, command, asDefault);
}, this); }, this);
}; };
this._addCommandToBinding = function(keyId, command, asDefault) {
var ckb = this.commandKeyBinding, i;
if (!command) {
delete ckb[keyId];
} else if (!ckb[keyId] || this.$singleCommand) {
ckb[keyId] = command;
} else {
if (!Array.isArray(ckb[keyId])) {
ckb[keyId] = [ckb[keyId]];
} else if ((i = ckb[keyId].indexOf(command)) != -1) {
ckb[keyId].splice(i, 1);
}
if (asDefault || command.isDefault)
ckb[keyId].unshift(command);
else
ckb[keyId].push(command);
}
};
this.addCommands = function(commands) { this.addCommands = function(commands) {
commands && Object.keys(commands).forEach(function(name) { commands && Object.keys(commands).forEach(function(name) {
@ -142,21 +169,12 @@ function HashHandler(config, platform) {
}; };
this._buildKeyHash = function(command) { this._buildKeyHash = function(command) {
var binding = command.bindKey; this.bindKey(command.bindKey, command);
if (!binding)
return;
var key = typeof binding == "string" ? binding: binding[this.platform];
this.bindKey(key, command);
}; };
// accepts keys in the form ctrl+Enter or ctrl-Enter // accepts keys in the form ctrl+Enter or ctrl-Enter
// keys without modifiers or shift only // keys without modifiers or shift only
this.parseKeys = function(keys) { this.parseKeys = function(keys) {
// todo support keychains
if (keys.indexOf(" ") != -1)
keys = keys.split(/\s+/).pop();
var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x}); var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x});
var key = parts.pop(); var key = parts.pop();
@ -173,7 +191,7 @@ function HashHandler(config, platform) {
var modifier = keyUtil.KEY_MODS[parts[i]]; var modifier = keyUtil.KEY_MODS[parts[i]];
if (modifier == null) { if (modifier == null) {
if (typeof console != "undefined") if (typeof console != "undefined")
console.error("invalid modifier " + parts[i] + " in " + keys); console.error("invalid modifier " + parts[i] + " in " + keys);
return false; return false;
} }
hashId |= modifier; hashId |= modifier;
@ -182,17 +200,32 @@ function HashHandler(config, platform) {
}; };
this.findKeyCommand = function findKeyCommand(hashId, keyString) { this.findKeyCommand = function findKeyCommand(hashId, keyString) {
var ckbr = this.commandKeyBinding; var key = KEY_MODS[hashId] + keyString;
return ckbr[hashId] && ckbr[hashId][keyString]; return this.commandKeyBinding[key];
}; };
this.handleKeyboard = function(data, hashId, keyString, keyCode) { this.handleKeyboard = function(data, hashId, keyString, keyCode) {
return { var key = KEY_MODS[hashId] + keyString;
command: this.findKeyCommand(hashId, keyString) var command = this.commandKeyBinding[key];
}; if (data.$keyChain) {
data.$keyChain += " " + key;
command = this.commandKeyBinding[data.$keyChain] || command;
}
if (command) {
if (command == "chainKeys" || command[command.length - 1] == "chainKeys") {
data.$keyChain = data.$keyChain || key;
return {command: "null"};
}
}
if (data.$keyChain && keyCode > 0)
data.$keyChain = "";
return {command: command};
}; };
}).call(HashHandler.prototype) }).call(HashHandler.prototype);
exports.HashHandler = HashHandler; exports.HashHandler = HashHandler;
exports.MultiHashHandler = MultiHashHandler;
}); });

View file

@ -145,6 +145,9 @@ var Keys = (function() {
} }
})(); })();
ret.KEY_MODS[0] = "";
ret.KEY_MODS[-1] = "input";
return ret; return ret;
})(); })();
oop.mixin(exports, Keys); oop.mixin(exports, Keys);