refactor undo handling

This commit is contained in:
Fabian Jakobs 2011-07-22 14:38:41 +02:00
commit 14b7b5ad4b
4 changed files with 37 additions and 65 deletions

View file

@ -183,14 +183,12 @@ var Document = function(text) {
var firstLine = lines.splice(0, 1)[0]; var firstLine = lines.splice(0, 1)[0];
var lastLine = lines.length == 0 ? null : lines.splice(lines.length - 1, 1)[0]; var lastLine = lines.length == 0 ? null : lines.splice(lines.length - 1, 1)[0];
this._dispatchEvent("changeStart");
position = this.insertInLine(position, firstLine); position = this.insertInLine(position, firstLine);
if (lastLine !== null) { if (lastLine !== null) {
position = this.insertNewLine(position); // terminate first line position = this.insertNewLine(position); // terminate first line
position = this.insertLines(position.row, lines); position = this.insertLines(position.row, lines);
position = this.insertInLine(position, lastLine || ""); position = this.insertInLine(position, lastLine || "");
} }
this._dispatchEvent("changeEnd");
return position; return position;
}; };
@ -202,7 +200,6 @@ var Document = function(text) {
args.push.apply(args, lines); args.push.apply(args, lines);
this.$lines.splice.apply(this.$lines, args); this.$lines.splice.apply(this.$lines, args);
this._dispatchEvent("changeStart");
var range = new Range(row, 0, row + lines.length, 0); var range = new Range(row, 0, row + lines.length, 0);
var delta = { var delta = {
action: "insertLines", action: "insertLines",
@ -210,7 +207,6 @@ var Document = function(text) {
lines: lines lines: lines
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
return range.end; return range.end;
}, },
@ -218,7 +214,6 @@ var Document = function(text) {
position = this.$clipPosition(position); position = this.$clipPosition(position);
var line = this.$lines[position.row] || ""; var line = this.$lines[position.row] || "";
this._dispatchEvent("changeStart");
this.$lines[position.row] = line.substring(0, position.column); this.$lines[position.row] = line.substring(0, position.column);
this.$lines.splice(position.row + 1, 0, line.substring(position.column, line.length)); this.$lines.splice(position.row + 1, 0, line.substring(position.column, line.length));
@ -233,7 +228,6 @@ var Document = function(text) {
text: this.getNewLineCharacter() text: this.getNewLineCharacter()
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
return end; return end;
}; };
@ -244,7 +238,6 @@ var Document = function(text) {
var line = this.$lines[position.row] || ""; var line = this.$lines[position.row] || "";
this._dispatchEvent("changeStart");
this.$lines[position.row] = line.substring(0, position.column) + text this.$lines[position.row] = line.substring(0, position.column) + text
+ line.substring(position.column); + line.substring(position.column);
@ -259,7 +252,6 @@ var Document = function(text) {
text: text text: text
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
return end; return end;
}; };
@ -275,7 +267,6 @@ var Document = function(text) {
var firstRow = range.start.row; var firstRow = range.start.row;
var lastRow = range.end.row; var lastRow = range.end.row;
this._dispatchEvent("changeStart");
if (range.isMultiLine()) { if (range.isMultiLine()) {
var firstFullRow = range.start.column == 0 ? firstRow : firstRow + 1; var firstFullRow = range.start.column == 0 ? firstRow : firstRow + 1;
var lastFullRow = lastRow - 1; var lastFullRow = lastRow - 1;
@ -294,7 +285,6 @@ var Document = function(text) {
else { else {
this.removeInLine(firstRow, range.start.column, range.end.column); this.removeInLine(firstRow, range.start.column, range.end.column);
} }
this._dispatchEvent("changeEnd");
return range.start; return range.start;
}; };
@ -306,7 +296,6 @@ var Document = function(text) {
var line = this.getLine(row); var line = this.getLine(row);
var removed = line.substring(startColumn, endColumn); var removed = line.substring(startColumn, endColumn);
var newLine = line.substring(0, startColumn) + line.substring(endColumn, line.length); var newLine = line.substring(0, startColumn) + line.substring(endColumn, line.length);
this._dispatchEvent("changeStart");
this.$lines.splice(row, 1, newLine); this.$lines.splice(row, 1, newLine);
var delta = { var delta = {
@ -315,7 +304,6 @@ var Document = function(text) {
text: removed text: removed
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
return range.start; return range.start;
}; };
@ -327,7 +315,6 @@ var Document = function(text) {
* @return {String[]} The removed lines * @return {String[]} The removed lines
*/ */
this.removeLines = function(firstRow, lastRow) { this.removeLines = function(firstRow, lastRow) {
this._dispatchEvent("changeStart");
var range = new Range(firstRow, 0, lastRow + 1, 0); var range = new Range(firstRow, 0, lastRow + 1, 0);
var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1); var removed = this.$lines.splice(firstRow, lastRow - firstRow + 1);
@ -338,7 +325,6 @@ var Document = function(text) {
lines: removed lines: removed
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
return removed; return removed;
}; };
@ -349,7 +335,6 @@ var Document = function(text) {
var range = new Range(row, firstLine.length, row+1, 0); var range = new Range(row, firstLine.length, row+1, 0);
var line = firstLine + secondLine; var line = firstLine + secondLine;
this._dispatchEvent("changeStart");
this.$lines.splice(row, 2, line); this.$lines.splice(row, 2, line);
var delta = { var delta = {
@ -358,7 +343,6 @@ var Document = function(text) {
text: this.getNewLineCharacter() text: this.getNewLineCharacter()
}; };
this._dispatchEvent("change", { data: delta }); this._dispatchEvent("change", { data: delta });
this._dispatchEvent("changeEnd");
}; };
this.replace = function(range, text) { this.replace = function(range, text) {
@ -370,7 +354,6 @@ var Document = function(text) {
if (text == this.getTextRange(range)) if (text == this.getTextRange(range))
return range.end; return range.end;
this._dispatchEvent("changeStart");
this.remove(range); this.remove(range);
if (text) { if (text) {
var end = this.insert(range.start, text); var end = this.insert(range.start, text);
@ -378,7 +361,6 @@ var Document = function(text) {
else { else {
end = range.start; end = range.start;
} }
this._dispatchEvent("changeEnd");
return end; return end;
}; };

View file

@ -65,7 +65,6 @@ var EditSession = function(text, mode) {
}); });
return str; return str;
} }
this.$docChangeCounter = 0;
if (text instanceof Document) { if (text instanceof Document) {
this.setDocument(text); this.setDocument(text);
@ -91,8 +90,6 @@ var EditSession = function(text, mode) {
this.doc = doc; this.doc = doc;
doc.on("change", this.onChange.bind(this)); doc.on("change", this.onChange.bind(this));
doc.on("changeStart", this.onChangeStart.bind(this));
doc.on("changeEnd", this.onChangeEnd.bind(this));
this.on("changeFold", this.onChangeFold.bind(this)); this.on("changeFold", this.onChangeFold.bind(this));
}; };
@ -100,10 +97,6 @@ var EditSession = function(text, mode) {
return this.doc; return this.doc;
}; };
this.onChangeStart = function() {
this.$docChangeCounter ++;
};
this.$resetRowCache = function(row) { this.$resetRowCache = function(row) {
if (row == 0) { if (row == 0) {
this.$rowCache = []; this.$rowCache = [];
@ -116,29 +109,6 @@ var EditSession = function(text, mode) {
return; return;
} }
} }
}
this.onChangeEnd = function() {
this.$docChangeCounter --;
if (this.$docChangeCounter == 0
&& !this.$fromUndo && this.$undoManager)
{
if (this.$deltasFold.length) {
this.$deltas.push({
group: "fold",
deltas: this.$deltasFold
});
this.$deltasFold = [];
}
if (this.$deltasDoc) {
this.$deltas.push({
group: "doc",
deltas: this.$deltasDoc
});
this.$deltasDoc = [];
}
this.$informUndoManager.schedule();
}
}; };
this.onChangeFold = function(e) { this.onChangeFold = function(e) {
@ -161,6 +131,8 @@ var EditSession = function(text, mode) {
folds: removedFolds folds: removedFolds
}); });
} }
this.$informUndoManager.schedule();
} }
this.bgTokenizer.start(delta.range.start.row); this.bgTokenizer.start(delta.range.start.row);
@ -203,19 +175,37 @@ var EditSession = function(text, mode) {
this.$deltasDoc = []; this.$deltasDoc = [];
this.$deltasFold = []; this.$deltasFold = [];
if (this.$informUndoManager) { if (this.$informUndoManager)
this.$informUndoManager.cancel(); this.$informUndoManager.cancel();
}
if (undoManager) { if (undoManager) {
var self = this; var self = this;
this.$syncInformUndoManager = function() { this.$syncInformUndoManager = function() {
self.$informUndoManager.cancel(); self.$informUndoManager.cancel();
if (self.$deltas.length > 0)
undoManager.execute({ if (self.$deltasFold.length) {
action : "aceupdate", self.$deltas.push({
args : [self.$deltas, self] group: "fold",
deltas: self.$deltasFold
}); });
self.$deltasFold = [];
}
if (self.$deltasDoc.length) {
self.$deltas.push({
group: "doc",
deltas: self.$deltasDoc
});
self.$deltasDoc = [];
}
if (self.$deltas.length > 0) {
undoManager.execute({
action: "aceupdate",
args: [self.$deltas, self]
});
}
self.$deltas = []; self.$deltas = [];
} }
this.$informUndoManager = this.$informUndoManager =

View file

@ -562,9 +562,10 @@ module.exports = {
"test fold one-line text insert": function() { "test fold one-line text insert": function() {
// These are mostly test for the FoldLine.addRemoveChars function. // These are mostly test for the FoldLine.addRemoveChars function.
var session = createFoldTestSession(), var session = createFoldTestSession();
undoManager = session.getUndoManager(), var undoManager = session.getUndoManager();
foldLines = session.$foldData; var foldLines = session.$foldData;
function insert(row, column, text) { function insert(row, column, text) {
session.insert({row: row, column: column}, text); session.insert({row: row, column: column}, text);

View file

@ -382,33 +382,32 @@ module.exports = {
editor.removeLines(); editor.removeLines();
var step1 = session.toString(); var step1 = session.toString();
assert.equal(step1, "222\n333"); assert.equal(step1, "222\n333");
session.$informUndoManager.call(); session.$syncInformUndoManager();
editor.removeLines(); editor.removeLines();
var step2 = session.toString(); var step2 = session.toString();
assert.equal(step2, "333"); assert.equal(step2, "333");
session.$informUndoManager.call(); session.$syncInformUndoManager();
editor.removeLines(); editor.removeLines();
var step3 = session.toString(); var step3 = session.toString();
assert.equal(step3, ""); assert.equal(step3, "");
session.$informUndoManager.call(); session.$syncInformUndoManager();
undoManager.undo(); undoManager.undo();
session.$informUndoManager.call(); session.$syncInformUndoManager();
assert.equal(session.toString(), step2); assert.equal(session.toString(), step2);
undoManager.undo(); undoManager.undo();
session.$informUndoManager.call(); session.$syncInformUndoManager();
assert.equal(session.toString(), step1); assert.equal(session.toString(), step1);
undoManager.undo(); undoManager.undo();
session.$informUndoManager.call(); session.$syncInformUndoManager();
assert.equal(session.toString(), initialText); assert.equal(session.toString(), initialText);
undoManager.undo(); undoManager.undo();
session.$informUndoManager.call(); session.$syncInformUndoManager();
assert.equal(session.toString(), initialText); assert.equal(session.toString(), initialText);
}, },