move undo delta merging to the editor
This commit is contained in:
parent
277675525b
commit
ef2e2cdd11
3 changed files with 64 additions and 55 deletions
|
|
@ -407,10 +407,10 @@ var EditSession = function(text, mode) {
|
||||||
undoManager.execute({
|
undoManager.execute({
|
||||||
action: "aceupdate",
|
action: "aceupdate",
|
||||||
args: [self.$deltas, self],
|
args: [self.$deltas, self],
|
||||||
merge: self.coalesceDeltas
|
merge: self.mergeUndoDeltas
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
self.coalesceDeltas = false;
|
self.mergeUndoDeltas = false;
|
||||||
self.$deltas = [];
|
self.$deltas = [];
|
||||||
}
|
}
|
||||||
this.$informUndoManager = lang.delayedCall(this.$syncInformUndoManager);
|
this.$informUndoManager = lang.delayedCall(this.$syncInformUndoManager);
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,9 @@ var Editor = function(renderer, session) {
|
||||||
wrap: true
|
wrap: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.$historyTracker = this.$historyTracker.bind(this);
|
||||||
|
this.commands.on("exec", this.$historyTracker);
|
||||||
|
|
||||||
this.setSession(session || new EditSession(""));
|
this.setSession(session || new EditSession(""));
|
||||||
config.resetOptions(this);
|
config.resetOptions(this);
|
||||||
config._emit("editor", this);
|
config._emit("editor", this);
|
||||||
|
|
@ -97,6 +100,62 @@ var Editor = function(renderer, session) {
|
||||||
|
|
||||||
oop.implement(this, EventEmitter);
|
oop.implement(this, EventEmitter);
|
||||||
|
|
||||||
|
this.$historyTracker = function(e) {
|
||||||
|
if (e.command.name == "insertstring") {
|
||||||
|
var text = e.args;
|
||||||
|
if (this.mergeNextCommand === undefined) {
|
||||||
|
this.mergeNextCommand = true;
|
||||||
|
}
|
||||||
|
var previousCommand = this.previousCommand || {};
|
||||||
|
if (
|
||||||
|
// previous command was the same
|
||||||
|
"insertstring" == previousCommand.name
|
||||||
|
// cursor was not moved since last command
|
||||||
|
&& !this.cursorMoved
|
||||||
|
// previous command allows to coalesce with
|
||||||
|
&& this.mergeNextCommand
|
||||||
|
// previous insertion was not a new line or a whitespace
|
||||||
|
// or this is a sequence of a new line or a whitespace insertions
|
||||||
|
&& (!/ |\r\n|\r|\n/.test(text) || / |\r\n|\r|\n/.test(previousCommand.text))
|
||||||
|
// the sequence is not too long
|
||||||
|
&& (new Date()) - this.sequenceStartTime < 2000
|
||||||
|
)
|
||||||
|
this.session.mergeUndoDeltas = true;
|
||||||
|
else
|
||||||
|
this.sequenceStartTime = new Date().getTime();
|
||||||
|
|
||||||
|
this.previousCommand = {
|
||||||
|
name: "insertstring",
|
||||||
|
text: text
|
||||||
|
}
|
||||||
|
this.cursorMoved = false;
|
||||||
|
this.mergeNextCommand = true;
|
||||||
|
} else {
|
||||||
|
var mergeableCommands = ["backspace", "del"];
|
||||||
|
var previousCommand = this.previousCommand || {};
|
||||||
|
if (
|
||||||
|
// previous command was the same
|
||||||
|
e.command.name == previousCommand.name
|
||||||
|
// the command is mergeable
|
||||||
|
&& mergeableCommands.indexOf(e.command.name) !== -1
|
||||||
|
// cursor was not moved since last command
|
||||||
|
&& !this.cursorMoved
|
||||||
|
// the sequence is not too long
|
||||||
|
&& (new Date()) - this.sequenceStartTime < 2000
|
||||||
|
)
|
||||||
|
this.session.mergeUndoDeltas = true
|
||||||
|
else
|
||||||
|
if (mergeableCommands.indexOf(e.command.name) !== -1)
|
||||||
|
this.sequenceStartTime = new Date().getTime();
|
||||||
|
|
||||||
|
this.previousCommand = {
|
||||||
|
name: e.command.name,
|
||||||
|
text: e.command.text
|
||||||
|
}
|
||||||
|
this.cursorMoved = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new key handler, such as "vim" or "windows".
|
* Sets a new key handler, such as "vim" or "windows".
|
||||||
* @param {String} keyboardHandler The new key handler
|
* @param {String} keyboardHandler The new key handler
|
||||||
|
|
@ -703,8 +762,8 @@ var Editor = function(renderer, session) {
|
||||||
var transform = mode.transformAction(session.getState(cursor.row), 'insertion', this, session, text);
|
var transform = mode.transformAction(session.getState(cursor.row), 'insertion', this, session, text);
|
||||||
if (transform) {
|
if (transform) {
|
||||||
if (text !== transform.text) {
|
if (text !== transform.text) {
|
||||||
this.session.coalesceDeltas = false;
|
this.session.mergeUndoDeltas = false;
|
||||||
this.coalesceNextCommand = false;
|
this.$mergeNextCommand = false;
|
||||||
}
|
}
|
||||||
text = transform.text;
|
text = transform.text;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,28 +105,6 @@ var KeyBinding = function(editor) {
|
||||||
if (toExecute.command == "null") {
|
if (toExecute.command == "null") {
|
||||||
success = true;
|
success = true;
|
||||||
} else {
|
} else {
|
||||||
var mergeableCommands = ["backspace", "del"];
|
|
||||||
var previousCommand = this.$editor.previousCommand || {};
|
|
||||||
if (
|
|
||||||
// previous command was the same
|
|
||||||
toExecute.command.name == previousCommand.name
|
|
||||||
// the command is mergeable
|
|
||||||
&& mergeableCommands.indexOf(toExecute.command.name) !== -1
|
|
||||||
// cursor was not moved since last command
|
|
||||||
&& !this.$editor.cursorMoved
|
|
||||||
// the sequence is not too long
|
|
||||||
&& (new Date()) - this.$editor.sequenceStartTime < 2000
|
|
||||||
)
|
|
||||||
this.$editor.session.coalesceDeltas = true
|
|
||||||
else
|
|
||||||
if (mergeableCommands.indexOf(toExecute.command.name) !== -1)
|
|
||||||
this.$editor.sequenceStartTime = new Date().getTime();
|
|
||||||
|
|
||||||
this.$editor.previousCommand = {
|
|
||||||
name: toExecute.command.name,
|
|
||||||
text: toExecute.command.text
|
|
||||||
}
|
|
||||||
this.$editor.cursorMoved = false;
|
|
||||||
success = commands.exec(toExecute.command, this.$editor, toExecute.args, e);
|
success = commands.exec(toExecute.command, this.$editor, toExecute.args, e);
|
||||||
}
|
}
|
||||||
// do not stop input events to not break repeating
|
// do not stop input events to not break repeating
|
||||||
|
|
@ -145,36 +123,8 @@ var KeyBinding = function(editor) {
|
||||||
|
|
||||||
this.onTextInput = function(text) {
|
this.onTextInput = function(text) {
|
||||||
var success = this.$callKeyboardHandlers(-1, text);
|
var success = this.$callKeyboardHandlers(-1, text);
|
||||||
if (!success) {
|
if (!success)
|
||||||
if (this.$editor.coalesceNextCommand === undefined) {
|
|
||||||
this.$editor.coalesceNextCommand = true;
|
|
||||||
}
|
|
||||||
var previousCommand = this.$editor.previousCommand || {};
|
|
||||||
if (
|
|
||||||
// previous command was the same
|
|
||||||
"insertstring" == previousCommand.name
|
|
||||||
// cursor was not moved since last command
|
|
||||||
&& !this.$editor.cursorMoved
|
|
||||||
// previous command allows to coalesce with
|
|
||||||
&& this.$editor.coalesceNextCommand
|
|
||||||
// previous insertion was not a new line or a whitespace
|
|
||||||
// or this is a sequence of a new line or a whitespace insertions
|
|
||||||
&& (!/ |\r\n|\r|\n/.test(text) || / |\r\n|\r|\n/.test(previousCommand.text))
|
|
||||||
// the sequence is not too long
|
|
||||||
&& (new Date()) - this.$editor.sequenceStartTime < 2000
|
|
||||||
)
|
|
||||||
this.$editor.session.coalesceDeltas = true;
|
|
||||||
else
|
|
||||||
this.$editor.sequenceStartTime = new Date().getTime();
|
|
||||||
|
|
||||||
this.$editor.previousCommand = {
|
|
||||||
name: "insertstring",
|
|
||||||
text: text
|
|
||||||
}
|
|
||||||
this.$editor.cursorMoved = false;
|
|
||||||
this.$editor.coalesceNextCommand = true;
|
|
||||||
this.$editor.commands.exec("insertstring", this.$editor, text);
|
this.$editor.commands.exec("insertstring", this.$editor, text);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}).call(KeyBinding.prototype);
|
}).call(KeyBinding.prototype);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue