From 3746f17943e7445d4922b03edb8f0fa6526da874 Mon Sep 17 00:00:00 2001 From: Cristian G Guerrero Date: Sun, 8 Sep 2024 19:30:18 +0200 Subject: [PATCH 1/2] Only save the status when a different action is perform --- public/js/undo-redo.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/js/undo-redo.js b/public/js/undo-redo.js index e5b74c1..e2deedb 100644 --- a/public/js/undo-redo.js +++ b/public/js/undo-redo.js @@ -2,6 +2,8 @@ // current unsaved state var diagramState; +// last action +var lastAction; // past states var undoStack = []; // reverted states @@ -46,6 +48,10 @@ 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() + }); markDiagramAsUnsaved(); if (!isRestoringDiagram) { @@ -55,8 +61,13 @@ function saveDiagramStateToUndoStack() { $('#undo').prop('disabled', true); // initial call won't have a state if (diagramState) { - // Save the state into the stack - undoStack.push(diagramState); + if (lastAction != triggerAction) { + // Save the state into the stack + undoStack.push(diagramState); + console.log("new action!!"); + } + lastAction = triggerAction; + console.log(lastAction); $('#undo').prop('disabled', false); } -- GitLab From 432816519d84a865b0153aa5f66fa80eac6c6fb1 Mon Sep 17 00:00:00 2001 From: Cristian G Guerrero Date: Sun, 8 Sep 2024 19:30:44 +0200 Subject: [PATCH 2/2] Also preserve actions if more than 5 seconds ellapsed since the previous one --- public/js/undo-redo.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/public/js/undo-redo.js b/public/js/undo-redo.js index e2deedb..c484693 100644 --- a/public/js/undo-redo.js +++ b/public/js/undo-redo.js @@ -4,6 +4,8 @@ var diagramState; // last action var lastAction; +// quick changes timer +var lastActionTimestamp = Date.now(); // past states var undoStack = []; // reverted states @@ -52,6 +54,7 @@ function saveDiagramStateToUndoStack() { function: arguments.callee.caller.toString(), object: getSelectedObjectIndex() }); + var triggerActionTimestamp = Date.now(); markDiagramAsUnsaved(); if (!isRestoringDiagram) { @@ -61,13 +64,15 @@ function saveDiagramStateToUndoStack() { $('#undo').prop('disabled', true); // initial call won't have a state if (diagramState) { - if (lastAction != triggerAction) { + // 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); - console.log("new action!!"); } lastAction = triggerAction; - console.log(lastAction); + lastActionTimestamp = triggerActionTimestamp; $('#undo').prop('disabled', false); } -- GitLab