From cb50f8da0968b699b8507bcd3b9254c7a3e2c2b5 Mon Sep 17 00:00:00 2001 From: Oliver Sartun Date: Wed, 17 Sep 2014 16:13:17 +0200 Subject: [PATCH] Fixing problems of undoAll / redoAll Fixing the severe problems caused by mixing undoAll / redoAll with regular undo / redo calls --- Backbone.Undo.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Backbone.Undo.js b/Backbone.Undo.js index fad2ee3..5bf8b42 100644 --- a/Backbone.Undo.js +++ b/Backbone.Undo.js @@ -266,20 +266,19 @@ } stack.isCurrentlyUndoRedoing = true; var action, actions, isUndo = which === "undo"; - if (isUndo) { - action = stack.at(stack.pointer); - stack.pointer--; - } else { - stack.pointer++; - action = stack.at(stack.pointer); - } if (everything) { - actions = _.clone(stack.models); + // Undo / Redo all steps until you reach the stack's beginning / end + actions = isUndo && stack.pointer === stack.length - 1 || // If at the stack's end calling undo + !isUndo && stack.pointer === -1 ? // or at the stack's beginning calling redo + _.clone(stack.models) : // => Take all the models. Otherwise: + core_slice.apply(stack.models, isUndo ? [0, stack.pointer] : [stack.pointer, stack.length - 1]); } else { + // Undo / Redo only one step + action = stack.at(isUndo ? stack.pointer : stack.pointer + 1); actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action]; } - stack.pointer += (isUndo ? -1 : 1) * (actions.length - 1); + stack.pointer += (isUndo ? -1 : 1) * actions.length; while (action = isUndo ? actions.pop() : actions.shift()) { // Here we're calling the Action's undo / redo method action[which]();