diff --git a/src/undomanager.js b/src/undomanager.js index 77f1df72076..a068acd76bc 100644 --- a/src/undomanager.js +++ b/src/undomanager.js @@ -13,6 +13,7 @@ var UndoManager = function() { this.$maxRev = 0; this.$fromUndo = false; + this.$undoDepth = Infinity; this.reset(); }; @@ -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; } diff --git a/src/undomanager_test.js b/src/undomanager_test.js index e3bfd384021..fbb4404a6b0 100644 --- a/src/undomanager_test.js +++ b/src/undomanager_test.js @@ -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); } };