Almost working draft of basic undo coalescence
This commit is contained in:
parent
7798de7bd3
commit
277675525b
4 changed files with 64 additions and 4 deletions
|
|
@ -406,10 +406,11 @@ var EditSession = function(text, mode) {
|
||||||
if (self.$deltas.length > 0) {
|
if (self.$deltas.length > 0) {
|
||||||
undoManager.execute({
|
undoManager.execute({
|
||||||
action: "aceupdate",
|
action: "aceupdate",
|
||||||
args: [self.$deltas, self]
|
args: [self.$deltas, self],
|
||||||
|
merge: self.coalesceDeltas
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
self.coalesceDeltas = false;
|
||||||
self.$deltas = [];
|
self.$deltas = [];
|
||||||
}
|
}
|
||||||
this.$informUndoManager = lang.delayedCall(this.$syncInformUndoManager);
|
this.$informUndoManager = lang.delayedCall(this.$syncInformUndoManager);
|
||||||
|
|
|
||||||
|
|
@ -701,8 +701,14 @@ var Editor = function(renderer, session) {
|
||||||
if (this.getBehavioursEnabled()) {
|
if (this.getBehavioursEnabled()) {
|
||||||
// Get a transform if the current mode wants one.
|
// Get a transform if the current mode wants one.
|
||||||
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) {
|
||||||
|
this.session.coalesceDeltas = false;
|
||||||
|
this.coalesceNextCommand = false;
|
||||||
|
}
|
||||||
text = transform.text;
|
text = transform.text;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text = text.replace("\t", this.session.getTabString());
|
text = text.replace("\t", this.session.getTabString());
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,28 @@ 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
|
||||||
|
|
@ -123,8 +145,36 @@ 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);
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,9 @@ var UndoManager = function() {
|
||||||
this.execute = function(options) {
|
this.execute = function(options) {
|
||||||
var deltas = options.args[0];
|
var deltas = options.args[0];
|
||||||
this.$doc = options.args[1];
|
this.$doc = options.args[1];
|
||||||
|
if (options.merge && this.hasUndo()){
|
||||||
|
deltas = this.$undoStack.pop().concat(deltas);
|
||||||
|
}
|
||||||
this.$undoStack.push(deltas);
|
this.$undoStack.push(deltas);
|
||||||
this.$redoStack = [];
|
this.$redoStack = [];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue