better api for adding commands + unit tests
This commit is contained in:
parent
c00341b444
commit
970dccdca4
2 changed files with 112 additions and 18 deletions
|
|
@ -43,20 +43,53 @@ var CommandManager = function(platform, commands) {
|
|||
}
|
||||
};
|
||||
|
||||
this._buildKeyHash = function(command) {
|
||||
var binding = command.bindKey;
|
||||
var key = binding[this.platform];
|
||||
var ckb = this.commmandKeyBinding;
|
||||
this.addCommands = function(commands) {
|
||||
Object.keys(commands).forEach(function(name) {
|
||||
var command = commands[name];
|
||||
if (typeof command === "string")
|
||||
return this.bindKey(command, name);
|
||||
|
||||
if(!binding[this.platform]) {
|
||||
if (typeof command === "function")
|
||||
command = { exec: command };
|
||||
|
||||
if (!command.name)
|
||||
command.name = name;
|
||||
|
||||
this.addCommand(command);
|
||||
}, this);
|
||||
};
|
||||
|
||||
this.removeCommands = function(commands) {
|
||||
Object.keys(commands).forEach(function(name) {
|
||||
this.removeCommand(commands[name]);
|
||||
}, this);
|
||||
};
|
||||
|
||||
this.bindKey = function(key, command) {
|
||||
if(!key)
|
||||
return;
|
||||
}
|
||||
|
||||
var ckb = this.commmandKeyBinding;
|
||||
key.split("|").forEach(function(keyPart) {
|
||||
var binding = parseKeys(keyPart, command);
|
||||
var hashId = binding.hashId;
|
||||
(ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command;
|
||||
});
|
||||
};
|
||||
|
||||
this.bindKeys = function(keyList) {
|
||||
Object.keys(keyList).forEach(function(key) {
|
||||
this.bindKey(key, keyList[key]);
|
||||
}, this);
|
||||
};
|
||||
|
||||
this._buildKeyHash = function(command) {
|
||||
var binding = command.bindKey;
|
||||
if (!binding)
|
||||
return;
|
||||
|
||||
var key = typeof binding == "string" ? binding: binding[this.platform];
|
||||
this.bindKey(key, command);
|
||||
}
|
||||
|
||||
function parseKeys(keys, val, ret) {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ module.exports = {
|
|||
this.cm.exec("gotoline");
|
||||
assert.ok(this.command.called);
|
||||
},
|
||||
|
||||
|
||||
"test: mac hotkeys": function() {
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
|
||||
assert.equal(command, this.command);
|
||||
|
|
@ -73,10 +73,10 @@ module.exports = {
|
|||
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "L");
|
||||
assert.equal(command, undefined);
|
||||
},
|
||||
|
||||
|
||||
"test: win hotkeys": function() {
|
||||
cm = new CommandManager("win", [this.command]);
|
||||
|
||||
var cm = new CommandManager("win", [this.command]);
|
||||
|
||||
var command = cm.findKeyCommand(keys.KEY_MODS.command, "L");
|
||||
assert.equal(command, undefined);
|
||||
|
||||
|
|
@ -86,24 +86,24 @@ module.exports = {
|
|||
|
||||
"test: remove command by object": function() {
|
||||
this.cm.removeCommand(this.command);
|
||||
|
||||
|
||||
this.cm.exec("gotoline");
|
||||
assert.ok(!this.command.called);
|
||||
|
||||
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
|
||||
assert.equal(command, null);
|
||||
},
|
||||
|
||||
|
||||
"test: remove command by name": function() {
|
||||
this.cm.removeCommand("gotoline");
|
||||
|
||||
|
||||
this.cm.exec("gotoline");
|
||||
assert.ok(!this.command.called);
|
||||
|
||||
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L");
|
||||
assert.equal(command, null);
|
||||
},
|
||||
|
||||
|
||||
"test: adding a new command with the same name as an existing one should remove the old one first": function() {
|
||||
var command = {
|
||||
name: "gotoline",
|
||||
|
|
@ -115,12 +115,73 @@ module.exports = {
|
|||
exec: function(editor) { this.called = true; }
|
||||
}
|
||||
this.cm.addCommand(command);
|
||||
|
||||
|
||||
this.cm.exec("gotoline");
|
||||
assert.ok(command.called);
|
||||
assert.ok(!this.command.called);
|
||||
|
||||
|
||||
assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "L"), command);
|
||||
},
|
||||
|
||||
"test: adding commands and recording a macro": function() {
|
||||
var called = "";
|
||||
this.cm.addCommands({
|
||||
togglerecording: function(editor) {
|
||||
editor.cm.toggleRecording()
|
||||
},
|
||||
replay: function(editor) {
|
||||
editor.cm.replay()
|
||||
},
|
||||
cm1: function(editor, arg) {
|
||||
called += "1" + (arg || "")
|
||||
},
|
||||
cm2: function(editor) {
|
||||
called += "2"
|
||||
}
|
||||
});
|
||||
|
||||
this.cm.exec("togglerecording", this);
|
||||
assert.ok(this.cm.recording);
|
||||
|
||||
this.cm.exec("cm1", this, "-");
|
||||
this.cm.exec("cm2");
|
||||
this.cm.exec("replay", this);
|
||||
assert.ok(!this.cm.recording);
|
||||
assert.equal(called, "1-2");
|
||||
|
||||
called = "";
|
||||
this.cm.exec("replay", this);
|
||||
assert.equal(called, "1-2");
|
||||
},
|
||||
|
||||
"test: bindkeys": function() {
|
||||
var called = "";
|
||||
this.cm.addCommands({
|
||||
cm1: function(editor, arg) {
|
||||
called += "1" + (arg || "")
|
||||
},
|
||||
cm2: function(editor) {
|
||||
called += "2"
|
||||
}
|
||||
});
|
||||
|
||||
this.cm.bindKeys({
|
||||
"Ctrl-L|Command-C": "cm1",
|
||||
"Ctrl-R": "cm2",
|
||||
})
|
||||
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "C");
|
||||
assert.equal(command, "cm1");
|
||||
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R");
|
||||
assert.equal(command, "cm2");
|
||||
|
||||
this.cm.bindKeys({
|
||||
"Ctrl-R": null
|
||||
})
|
||||
|
||||
var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R");
|
||||
assert.equal(command, null);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue