Added undoAll() and redoAll() methods
This commit is contained in:
parent
180d56294a
commit
b69ef10691
@ -248,14 +248,15 @@
|
||||
|
||||
/**
|
||||
* The main undo/redo function.
|
||||
*
|
||||
* @param {String} which Either "undo" or "redo"
|
||||
* @param {UndoManager} manager The UndoManager-instance on which an "undo"/"redo"-Event is triggered afterwards
|
||||
* @param {UndoStack} stack The UndoStack on which we perform
|
||||
* @param {Boolean} magic If true, undoes / redoes all actions with the same magicFusionIndex
|
||||
* @return {undefined}
|
||||
*/
|
||||
function managerUndoRedo (which, manager, stack, magic) {
|
||||
*
|
||||
* @param {String} which Either "undo" or "redo"
|
||||
* @param {UndoManager} manager The UndoManager-instance on which an "undo"/"redo"-Event is triggered afterwards
|
||||
* @param {UndoStack} stack The UndoStack on which we perform
|
||||
* @param {Boolean} magic If true, undoes / redoes all actions with the same magicFusionIndex
|
||||
* @param {Boolean} everything If true, undoes / redoes every actions that have been tracked
|
||||
* @return {undefined}
|
||||
*/
|
||||
function managerUndoRedo (which, manager, stack, magic, everything) {
|
||||
if (stack.isCurrentlyUndoRedoing ||
|
||||
(which === "undo" && stack.pointer === -1) ||
|
||||
(which === "redo" && stack.pointer === stack.length - 1)) {
|
||||
@ -272,7 +273,11 @@
|
||||
stack.pointer++;
|
||||
action = stack.at(stack.pointer);
|
||||
}
|
||||
actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action];
|
||||
if (everything)
|
||||
actions = _.clone(stack.models);
|
||||
else
|
||||
actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action];
|
||||
|
||||
stack.pointer += (isUndo ? -1 : 1) * (actions.length - 1);
|
||||
while (action = isUndo ? actions.pop() : actions.shift()) {
|
||||
// Here we're calling the Action's undo / redo method
|
||||
@ -675,6 +680,11 @@
|
||||
undo: function (magic) {
|
||||
managerUndoRedo("undo", this, this.stack, magic);
|
||||
},
|
||||
|
||||
undoAll: function () {
|
||||
managerUndoRedo("undo", this, this.stack, false, true);
|
||||
},
|
||||
|
||||
/**
|
||||
* Redoes a previously undone action or a set of actions.
|
||||
* @param {Boolean} [magic] If true, all actions that happened basically at the same time are redone together
|
||||
@ -683,6 +693,10 @@
|
||||
redo: function (magic) {
|
||||
managerUndoRedo("redo", this, this.stack, magic);
|
||||
},
|
||||
|
||||
redoAll: function () {
|
||||
managerUndoRedo("redo", this, this.stack, false, true);
|
||||
},
|
||||
/**
|
||||
* Checks if there's an action in the stack that can be undone / redone
|
||||
* @param {String} type Either "undo" or "redo"
|
||||
|
@ -306,6 +306,29 @@ test("Clearing all actions", function () {
|
||||
deepEqual(model.toJSON(), {"t": 2}, "Clearing actions before redoing was successful");
|
||||
})
|
||||
|
||||
test("Undoing all actions", function () {
|
||||
var model = new Backbone.Model({
|
||||
"t": 1
|
||||
});
|
||||
|
||||
var UndoManager = new Backbone.UndoManager({
|
||||
track: true,
|
||||
register: model
|
||||
});
|
||||
|
||||
model.set("t", 2);
|
||||
model.set("t", 3);
|
||||
model.set("t", 4);
|
||||
|
||||
UndoManager.undoAll();
|
||||
|
||||
deepEqual(model.toJSON(), {"t": 1}, "Calling undoAll was successful");
|
||||
|
||||
UndoManager.redoAll();
|
||||
|
||||
deepEqual(model.toJSON(), {"t": 4}, "Calling redoAll was successful");
|
||||
})
|
||||
|
||||
/**
|
||||
* Async tests for magic condensation
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user