make undo coalescence configurable
off by default
This commit is contained in:
parent
ef2e2cdd11
commit
b18b86b6c9
1 changed files with 31 additions and 48 deletions
|
|
@ -50,8 +50,6 @@ var defaultCommands = require("./commands/default_commands").commands;
|
||||||
var config = require("./config");
|
var config = require("./config");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
*
|
|
||||||
* The main entry point into the Ace functionality.
|
* The main entry point into the Ace functionality.
|
||||||
*
|
*
|
||||||
* The `Editor` manages the [[EditSession]] (which manages [[Document]]s), as well as the [[VirtualRenderer]], which draws everything to the screen.
|
* The `Editor` manages the [[EditSession]] (which manages [[Document]]s), as well as the [[VirtualRenderer]], which draws everything to the screen.
|
||||||
|
|
@ -101,59 +99,40 @@ var Editor = function(renderer, session) {
|
||||||
oop.implement(this, EventEmitter);
|
oop.implement(this, EventEmitter);
|
||||||
|
|
||||||
this.$historyTracker = function(e) {
|
this.$historyTracker = function(e) {
|
||||||
|
if (!this.$mergeUndoDeltas)
|
||||||
|
return;
|
||||||
|
var previous = this.previousCommand || {command:{}};
|
||||||
|
var mergeableCommands = ["backspace", "del", "insertstring"];
|
||||||
if (e.command.name == "insertstring") {
|
if (e.command.name == "insertstring") {
|
||||||
var text = e.args;
|
var text = e.args;
|
||||||
if (this.mergeNextCommand === undefined) {
|
if (this.mergeNextCommand === undefined)
|
||||||
this.mergeNextCommand = true;
|
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 = {
|
var shouldMerge = e.command.name == previous.command.name // previous command was the same
|
||||||
name: "insertstring",
|
&& !this.cursorMoved // cursor was not moved since last command
|
||||||
text: text
|
&& this.mergeNextCommand // previous command allows to coalesce with
|
||||||
}
|
&& (!/\s/.test(text) || /\s/.test(previous.args)) // previous insertion was of same type
|
||||||
this.cursorMoved = false;
|
|
||||||
this.mergeNextCommand = true;
|
this.mergeNextCommand = true;
|
||||||
} else {
|
} else {
|
||||||
var mergeableCommands = ["backspace", "del"];
|
var shouldMerge = e.command.name == previous.command.name // previous command was the same
|
||||||
var previousCommand = this.previousCommand || {};
|
&& mergeableCommands.indexOf(e.command.name) !== -1// the command is mergeable
|
||||||
if (
|
&& !this.cursorMoved // cursor was not moved since last command
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.$mergeUndoDeltas != "always"
|
||||||
|
&& Date.now() - this.sequenceStartTime < 2000
|
||||||
|
) {
|
||||||
|
shouldMerge = false; // the sequence is too long
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldMerge)
|
||||||
|
this.session.mergeUndoDeltas = true;
|
||||||
|
else if (mergeableCommands.indexOf(e.command.name) !== -1)
|
||||||
|
this.sequenceStartTime = Date.now();
|
||||||
|
this.cursorMoved = false;
|
||||||
|
this.previousCommand = e;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -2308,6 +2287,10 @@ config.defineOptions(Editor.prototype, "editor", {
|
||||||
values: ["ace", "slim", "smooth", "wide"],
|
values: ["ace", "slim", "smooth", "wide"],
|
||||||
initialValue: "ace"
|
initialValue: "ace"
|
||||||
},
|
},
|
||||||
|
mergeUndoDeltas: {
|
||||||
|
values: [false, true, "always"],
|
||||||
|
initialValue: false
|
||||||
|
},
|
||||||
behavioursEnabled: {initialValue: true},
|
behavioursEnabled: {initialValue: true},
|
||||||
wrapBehavioursEnabled: {initialValue: true},
|
wrapBehavioursEnabled: {initialValue: true},
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue