Skip to content

Commit

Permalink
feat: added ability to limit amount of undos/redos (#4872)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc authored Aug 5, 2022
1 parent e15abb4 commit 897ee0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/undomanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var UndoManager = function() {
this.$maxRev = 0;
this.$fromUndo = false;
this.$undoDepth = Infinity;
this.reset();
};

Expand All @@ -36,6 +37,10 @@ var UndoManager = function() {
if (!this.$keepRedoStack) this.$redoStack.length = 0;
if (allowMerge === false || !this.lastDeltas) {
this.lastDeltas = [];
var undoStackLength = this.$undoStack.length;
if (undoStackLength > this.$undoDepth - 1) {
this.$undoStack.splice(0, undoStackLength - this.$undoDepth + 1);
}
this.$undoStack.push(this.lastDeltas);
delta.id = this.$rev = ++this.$maxRev;
}
Expand Down
21 changes: 21 additions & 0 deletions src/undomanager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,27 @@ module.exports = {
assert.equal(editor.getValue(), "");
editor.redo();
assert.equal(editor.getValue(), "\n\n\n\n");
},
"test: limit possible undos amount": function() {
editor.setValue("");
undoManager.startNewGroup();
editor.insert("a");
undoManager.startNewGroup();
editor.insert("b");
undoManager.startNewGroup();
editor.insert("c");
assert.equal(undoManager.$undoStack.length, 3);

undoManager.$undoDepth = 1;
editor.setValue("");
undoManager.startNewGroup();
editor.insert("a");
undoManager.startNewGroup();
editor.insert("b");
undoManager.startNewGroup();
editor.insert("c");
assert.equal(undoManager.$undoStack[0][0].lines[0], "c");
assert.equal(undoManager.$undoStack.length, undoManager.$undoDepth);
}
};

Expand Down

0 comments on commit 897ee0a

Please sign in to comment.