diff --git a/public/js/undo-redo.js b/public/js/undo-redo.js index e5b74c162a3e3f199f797cbfb4a1bb113377c657..c4846939afcba5ff48cbe4aeba068fcecf3e7df6 100644 --- a/public/js/undo-redo.js +++ b/public/js/undo-redo.js @@ -2,6 +2,10 @@ // current unsaved state var diagramState; +// last action +var lastAction; +// quick changes timer +var lastActionTimestamp = Date.now(); // past states var undoStack = []; // reverted states @@ -46,6 +50,11 @@ function selectObjectByIndex(objectIndex) { * Push the current state into the undo stack and then capture the current state */ function saveDiagramStateToUndoStack() { + var triggerAction = JSON.stringify({ + function: arguments.callee.caller.toString(), + object: getSelectedObjectIndex() + }); + var triggerActionTimestamp = Date.now(); markDiagramAsUnsaved(); if (!isRestoringDiagram) { @@ -55,8 +64,15 @@ function saveDiagramStateToUndoStack() { $('#undo').prop('disabled', true); // initial call won't have a state if (diagramState) { - // Save the state into the stack - undoStack.push(diagramState); + // Only preserve the status when there was a new action + // (or when more than 5 seconds ellapsed since the last action) + if (lastAction != triggerAction + || triggerActionTimestamp - lastActionTimestamp > 5000) { + // Save the state into the stack + undoStack.push(diagramState); + } + lastAction = triggerAction; + lastActionTimestamp = triggerActionTimestamp; $('#undo').prop('disabled', false); }