properly handle replay/toggleRecording while in replay mode

This commit is contained in:
nightwing 2011-11-20 18:08:04 +04:00
commit 262df95a3d

View file

@ -9,7 +9,7 @@ var CommandManager = function(platform, commands) {
this.platform = platform; this.platform = platform;
this.commands = {}; this.commands = {};
this.commmandKeyBinding = {}; this.commmandKeyBinding = {};
if (commands) if (commands)
commands.forEach(this.addCommand, this); commands.forEach(this.addCommand, this);
}; };
@ -23,10 +23,10 @@ var CommandManager = function(platform, commands) {
this.commands[command.name] = command; this.commands[command.name] = command;
if (command.bindKey) { if (command.bindKey) {
this._buildKeyHash(command); this._buildKeyHash(command);
} }
}; };
this.removeCommand = function(command) { this.removeCommand = function(command) {
var name = (typeof command === 'string' ? command : command.name); var name = (typeof command === 'string' ? command : command.name);
command = this.commands[name]; command = this.commands[name];
@ -51,7 +51,7 @@ var CommandManager = function(platform, commands) {
if(!binding[this.platform]) { if(!binding[this.platform]) {
return; return;
} }
key.split("|").forEach(function(keyPart) { key.split("|").forEach(function(keyPart) {
var binding = parseKeys(keyPart, command); var binding = parseKeys(keyPart, command);
var hashId = binding.hashId; var hashId = binding.hashId;
@ -70,11 +70,11 @@ var CommandManager = function(platform, commands) {
else else
key = parts[i] || "-"; //when empty, the splitSafe removed a '-' key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
} }
return { return {
key: key, key: key,
hashId: hashId hashId: hashId
} }
} }
function splitSafe(s, separator) { function splitSafe(s, separator) {
@ -96,7 +96,7 @@ var CommandManager = function(platform, commands) {
this.exec = function(command, editor, args) { this.exec = function(command, editor, args) {
if (typeof command === 'string') if (typeof command === 'string')
command = this.commands[command]; command = this.commands[command];
if (!command) if (!command)
return false; return false;
@ -108,11 +108,14 @@ var CommandManager = function(platform, commands) {
}; };
this.toggleRecording = function() { this.toggleRecording = function() {
if (this.$inReplay)
return;
if (this.recording) { if (this.recording) {
this.macro.pop();
this.exec = this.normal_exec; this.exec = this.normal_exec;
return this.recording = false; return this.recording = false;
} }
this.macro = [] this.macro = [];
this.normal_exec = this.exec; this.normal_exec = this.exec;
this.exec = function(command, editor, args) { this.exec = function(command, editor, args) {
this.macro.push([command, args]); this.macro.push([command, args]);
@ -120,23 +123,37 @@ var CommandManager = function(platform, commands) {
}; };
return this.recording = true; return this.recording = true;
}; };
this.replay = function(editor){
if (!this.macro) {
this.toggleRecording();
return
}
if (this.recording) { this.replay = function(editor) {
this.toggleRecording(); if (this.$inReplay)
this.macro.pop() return;
if (!this.macro || this.recording)
return this.toggleRecording();
try {
this.$inReplay = true;
this.macro.forEach(function(x) {
if (typeof x == "string")
this.exec(x, editor);
else
this.exec(x[0], editor, x[1]);
}, this)
} finally {
this.$inReplay = false;
} }
this.macro.forEach(function(x) {
this.exec(x[0], editor, x[1]);
}, this)
}; };
this.trimMacro = function(m) {
return m.map(function(x){
if (typeof x[0] != "string")
x[0] = x[0].name;
if (!x[1])
x = x[0];
return x
})
}
}).call(CommandManager.prototype); }).call(CommandManager.prototype);
exports.CommandManager = CommandManager; exports.CommandManager = CommandManager;