Merge pull request #545 from ajaxorg/placeholder-cancel
Added ability to cancel (=undo) the placeholder changes
This commit is contained in:
commit
a82c478d6d
2 changed files with 32 additions and 0 deletions
|
|
@ -58,6 +58,8 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass)
|
||||||
};
|
};
|
||||||
|
|
||||||
this.$pos = pos;
|
this.$pos = pos;
|
||||||
|
// Used for reset
|
||||||
|
this.$undoStackDepth = session.getUndoManager().$undoStack ? session.getUndoManager().$undoStack.length : -1;
|
||||||
this.setup();
|
this.setup();
|
||||||
|
|
||||||
session.selection.on("changeCursor", this.$onCursorChange);
|
session.selection.on("changeCursor", this.$onCursorChange);
|
||||||
|
|
@ -188,6 +190,16 @@ var PlaceHolder = function(session, length, pos, others, mainClass, othersClass)
|
||||||
this.others[i].detach();
|
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);
|
}).call(PlaceHolder.prototype);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ var MockRenderer = require("./test/mockrenderer").MockRenderer;
|
||||||
var assert = require("./test/assertions");
|
var assert = require("./test/assertions");
|
||||||
var JavaScriptMode = require("./mode/javascript").Mode;
|
var JavaScriptMode = require("./mode/javascript").Mode;
|
||||||
var PlaceHolder = require('./placeholder').PlaceHolder;
|
var PlaceHolder = require('./placeholder').PlaceHolder;
|
||||||
|
var UndoManager = require('./undomanager').UndoManager;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
|
|
@ -133,6 +134,25 @@ module.exports = {
|
||||||
editor.moveCursorTo(1, 0);
|
editor.moveCursorTo(1, 0);
|
||||||
p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
|
p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
|
||||||
assert.ok(left);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue