Added ability to cancel the placeholder related changes + test.

This commit is contained in:
Zef Hemel 2011-12-01 16:29:27 +01:00
commit 3beed6da5b
2 changed files with 32 additions and 0 deletions

View file

@ -58,6 +58,8 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass)
};
this.$pos = pos;
// Used for reset
this.$undoStackDepth = session.getUndoManager().$undoStack ? session.getUndoManager().$undoStack.length : -1;
this.setup();
session.selection.on("changeCursor", this.$onCursorChange);
@ -188,6 +190,16 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass)
this.others[i].detach();
}
};
this.cancel = function() {
if(this.$undoStackDepth === -1)
throw Error("Canceling placeholders only supported with undo manager attached to session.");
var undoManager = this.session.getUndoManager();
var undosRequired = undoManager.$undoStack.length - this.$undoStackDepth;
for (var i = 0; i < undosRequired; i++) {
undoManager.undo(true);
}
};
}).call(PlaceHolder.prototype);

View file

@ -49,6 +49,7 @@ var MockRenderer = require("./test/mockrenderer").MockRenderer;
var assert = require("./test/assertions");
var JavaScriptMode = require("./mode/javascript").Mode;
var PlaceHolder = require('./placeholder').PlaceHolder;
var UndoManager = require('./undomanager').UndoManager;
module.exports = {
@ -133,6 +134,25 @@ module.exports = {
editor.moveCursorTo(1, 0);
p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
assert.ok(left);
},
"test: cancel": function(next) {
var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
session.setUndoManager(new UndoManager());
var editor = new Editor(new MockRenderer(), session);
var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
editor.moveCursorTo(0, 5);
editor.insert('b');
editor.insert('cd');
editor.remove('left');
assert.equal(session.doc.getValue(), "var abc = 10;\nconsole.log(abc, abc);");
// Wait a little for the changes to enter the undo stack
setTimeout(function() {
p.cancel();
assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);");
next();
}, 80);
}
};