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

View file

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

View file

@ -124,14 +124,14 @@ var EditSession = require("./edit_session").EditSession;
var removed = this.rangeList.merge();
if (removed.length)
this.$onRemoveRange(removed);
else
this.fromOrientedRange(this.ranges[0]);
else if(this.ranges[0])
this.fromOrientedRange(this.ranges[0]);
};
this.$onAddRange = function(range) {
this.rangeCount = this.rangeList.ranges.length;
this.ranges.unshift(range);
this.fromOrientedRange(range);
this.fromOrientedRange(range);
this._emit("addRange", {range: range});
};
@ -269,7 +269,7 @@ var Editor = require("./editor").Editor;
this.renderer.updateCursor();
this.renderer.updateBackMarkers();
};
this.addSelectionMarker = function(orientedRange) {
if (!orientedRange.cursor)
orientedRange.cursor = orientedRange.end;
@ -312,13 +312,11 @@ var Editor = require("./editor").Editor;
this.setStyle("multiselect");
this.keyBinding.addKeyboardHandler(exports.commands.keyboardHandler);
// FixMe
this.commands.__SingleSelectionExec = this.commands.exec;
this.commands.exec = exports.exec;
this.commands.on("exec", this.$onMultiSelectExec);
this.renderer.updateCursor();
this.renderer.updateBackMarkers();
};
this.$onSingleSelect = function(e) {
if (this.session.multiSelect.inVirtualMode)
return;
@ -327,11 +325,29 @@ var Editor = require("./editor").Editor;
this.unsetStyle("multiselect");
this.keyBinding.removeKeyboardHandler(exports.commands.keyboardHandler);
this.commands.exec = this.commands.__SingleSelectionExec;
this.commands.removeEventListener("exec", this.$onMultiSelectExec);
this.renderer.updateCursor();
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) {
if (this.inVirtualSelectionMode)
return;
@ -499,36 +515,9 @@ var Editor = require("./editor").Editor;
this.multiSelect.substractPoint(range.cursor);
}
}).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) {
return p1.row == p2.row && p1.column == p2.column
}