emit event before executing a command

This commit is contained in:
nightwing 2012-04-08 11:46:20 +04:00
commit 2021536794
3 changed files with 41 additions and 44 deletions

View file

@ -3,6 +3,7 @@ define(function(require, exports, module) {
var oop = require("../lib/oop"); var oop = require("../lib/oop");
var HashHandler = require("../keyboard/hash_handler").HashHandler; var HashHandler = require("../keyboard/hash_handler").HashHandler;
var EventEmitter = require("../lib/event_emitter").EventEmitter;
var CommandManager = function(platform, commands) { var CommandManager = function(platform, commands) {
this.platform = platform; this.platform = platform;
@ -10,12 +11,17 @@ var CommandManager = function(platform, commands) {
this.commmandKeyBinding = {}; this.commmandKeyBinding = {};
this.addCommands(commands); this.addCommands(commands);
this.setDefaultHandler("exec", function(e) {
e.command.exec(e.editor, e.args || {})
});
}; };
oop.inherits(CommandManager, HashHandler); oop.inherits(CommandManager, HashHandler);
(function() { (function() {
oop.implement(this, EventEmitter);
this.exec = function(command, editor, args) { this.exec = function(command, editor, args) {
if (typeof command === 'string') if (typeof command === 'string')
@ -27,7 +33,7 @@ oop.inherits(CommandManager, HashHandler);
if (editor && editor.$readOnly && !command.readOnly) if (editor && editor.$readOnly && !command.readOnly)
return false; return false;
command.exec(editor, args || {}); this._emit("exec", {editor: editor, command: command, args: args});
return true; return true;
}; };
@ -36,20 +42,22 @@ oop.inherits(CommandManager, HashHandler);
return; return;
if (this.recording) { if (this.recording) {
this.macro.pop(); this.macro.pop();
this.exec = this.normal_exec; this.removeEventListener("exec", this.$addCommandToMacro);
if (!this.macro.length) if (!this.macro.length)
this.macro = this.oldMacro; this.macro = this.oldMacro;
return this.recording = false; return this.recording = false;
} }
if (!this.$addCommandToMacro) {
this.$addCommandToMacro = function(e) {
this.macro.push([e.command, e.args]);
}.bind(this);
}
this.oldMacro = this.macro; this.oldMacro = this.macro;
this.macro = []; this.macro = [];
this.normal_exec = this.exec; this.on("exec", this.$addCommandToMacro);
this.exec = function(command, editor, args) {
this.macro.push([command, args]);
return this.normal_exec(command, editor, args);
};
return this.recording = true; return this.recording = true;
}; };

View file

@ -45,7 +45,7 @@ require("./default_commands").commands.forEach(function(command) {
"golinedown", "golineup", "gotoend", "gotoleft", "gotolineend", "gotolinestart", "golinedown", "golineup", "gotoend", "gotoleft", "gotolineend", "gotolinestart",
"gotoright", "gotostart", "gotowordleft", "gotowordright", "gotoright", "gotostart", "gotowordleft", "gotowordright",
"indent", "insertstring", "inserttext", "jumptomatching", "outdent", "indent", "insertstring", "inserttext", "jumptomatching", "outdent",
"removetolineend", "removetolinestart", "removewordleft", "removewordright", "removetolineend", "removetolinestart", "removewordleft", "removewordright", "removeline",
"selectdown", "selectleft", "selectlineend", "selectlinestart", "selectright", "selectdown", "selectleft", "selectlineend", "selectlinestart", "selectright",
"selecttoend", "selecttolineend", "selecttolinestart", "selecttostart", "selecttoend", "selecttolineend", "selecttolinestart", "selecttostart",
"selectup", "selectwordleft", "selectwordright", "selectup", "selectwordleft", "selectwordright",

View file

@ -124,14 +124,14 @@ var EditSession = require("./edit_session").EditSession;
var removed = this.rangeList.merge(); var removed = this.rangeList.merge();
if (removed.length) if (removed.length)
this.$onRemoveRange(removed); this.$onRemoveRange(removed);
else else if(this.ranges[0])
this.fromOrientedRange(this.ranges[0]); this.fromOrientedRange(this.ranges[0]);
}; };
this.$onAddRange = function(range) { this.$onAddRange = function(range) {
this.rangeCount = this.rangeList.ranges.length; this.rangeCount = this.rangeList.ranges.length;
this.ranges.unshift(range); this.ranges.unshift(range);
this.fromOrientedRange(range); this.fromOrientedRange(range);
this._emit("addRange", {range: range}); this._emit("addRange", {range: range});
}; };
@ -269,7 +269,7 @@ var Editor = require("./editor").Editor;
this.renderer.updateCursor(); this.renderer.updateCursor();
this.renderer.updateBackMarkers(); this.renderer.updateBackMarkers();
}; };
this.addSelectionMarker = function(orientedRange) { this.addSelectionMarker = function(orientedRange) {
if (!orientedRange.cursor) if (!orientedRange.cursor)
orientedRange.cursor = orientedRange.end; orientedRange.cursor = orientedRange.end;
@ -312,13 +312,11 @@ var Editor = require("./editor").Editor;
this.setStyle("multiselect"); this.setStyle("multiselect");
this.keyBinding.addKeyboardHandler(exports.commands.keyboardHandler); this.keyBinding.addKeyboardHandler(exports.commands.keyboardHandler);
// FixMe this.commands.on("exec", this.$onMultiSelectExec);
this.commands.__SingleSelectionExec = this.commands.exec;
this.commands.exec = exports.exec;
this.renderer.updateCursor(); this.renderer.updateCursor();
this.renderer.updateBackMarkers(); this.renderer.updateBackMarkers();
}; };
this.$onSingleSelect = function(e) { this.$onSingleSelect = function(e) {
if (this.session.multiSelect.inVirtualMode) if (this.session.multiSelect.inVirtualMode)
return; return;
@ -327,11 +325,29 @@ var Editor = require("./editor").Editor;
this.unsetStyle("multiselect"); this.unsetStyle("multiselect");
this.keyBinding.removeKeyboardHandler(exports.commands.keyboardHandler); this.keyBinding.removeKeyboardHandler(exports.commands.keyboardHandler);
this.commands.exec = this.commands.__SingleSelectionExec; this.commands.removeEventListener("exec", this.$onMultiSelectExec);
this.renderer.updateCursor(); this.renderer.updateCursor();
this.renderer.updateBackMarkers(); this.renderer.updateBackMarkers();
}; };
this.$onMultiSelectExec = function(e) {
var command = e.command;
var editor = e.editor;
if (!command.multiSelectAction) {
command.exec(editor, e.args || {});
editor.multiSelect.mergeOverlappingRanges();
} else if (command.multiSelectAction == "forEach") {
editor.forEachSelection(command, e.args);
} else if (command.multiSelectAction == "single") {
editor.exitMultiSelectMode();
command.exec(editor, e.args || {});
} else {
command.multiSelectAction(editor, e.args || {});
}
e.preventDefault();
};
this.forEachSelection = function(cmd, args) { this.forEachSelection = function(cmd, args) {
if (this.inVirtualSelectionMode) if (this.inVirtualSelectionMode)
return; return;
@ -499,36 +515,9 @@ var Editor = require("./editor").Editor;
this.multiSelect.substractPoint(range.cursor); this.multiSelect.substractPoint(range.cursor);
} }
}).call(Editor.prototype); }).call(Editor.prototype);
// Todo emit event before exec?
exports.exec = function(command, editor, args) {
if (typeof command === 'string')
command = this.commands[command];
if (!command)
return false;
if (editor && editor.$readOnly && !command.readOnly)
return false;
if (!command.multiSelectAction) {
command.exec(editor, args || {});
} else if (command.multiSelectAction == "forEach") {
editor.forEachSelection(command, args);
} else if (command.multiSelectAction == "single") {
editor.exitMultiSelectMode();
command.exec(editor, args || {});
} else {
command.multiSelectAction(editor, args || {});
}
return true;
};
// mouse
function isSamePoint(p1, p2) { function isSamePoint(p1, p2) {
return p1.row == p2.row && p1.column == p2.column return p1.row == p2.row && p1.column == p2.column
} }