Skip to content

Commit 897ee0a

Browse files
authored
feat: added ability to limit amount of undos/redos (#4872)
1 parent e15abb4 commit 897ee0a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/undomanager.js

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
var UndoManager = function() {
1414
this.$maxRev = 0;
1515
this.$fromUndo = false;
16+
this.$undoDepth = Infinity;
1617
this.reset();
1718
};
1819

@@ -36,6 +37,10 @@ var UndoManager = function() {
3637
if (!this.$keepRedoStack) this.$redoStack.length = 0;
3738
if (allowMerge === false || !this.lastDeltas) {
3839
this.lastDeltas = [];
40+
var undoStackLength = this.$undoStack.length;
41+
if (undoStackLength > this.$undoDepth - 1) {
42+
this.$undoStack.splice(0, undoStackLength - this.$undoDepth + 1);
43+
}
3944
this.$undoStack.push(this.lastDeltas);
4045
delta.id = this.$rev = ++this.$maxRev;
4146
}

src/undomanager_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,27 @@ module.exports = {
369369
assert.equal(editor.getValue(), "");
370370
editor.redo();
371371
assert.equal(editor.getValue(), "\n\n\n\n");
372+
},
373+
"test: limit possible undos amount": function() {
374+
editor.setValue("");
375+
undoManager.startNewGroup();
376+
editor.insert("a");
377+
undoManager.startNewGroup();
378+
editor.insert("b");
379+
undoManager.startNewGroup();
380+
editor.insert("c");
381+
assert.equal(undoManager.$undoStack.length, 3);
382+
383+
undoManager.$undoDepth = 1;
384+
editor.setValue("");
385+
undoManager.startNewGroup();
386+
editor.insert("a");
387+
undoManager.startNewGroup();
388+
editor.insert("b");
389+
undoManager.startNewGroup();
390+
editor.insert("c");
391+
assert.equal(undoManager.$undoStack[0][0].lines[0], "c");
392+
assert.equal(undoManager.$undoStack.length, undoManager.$undoDepth);
372393
}
373394
};
374395

0 commit comments

Comments
 (0)