Fixed undo not working correctly after first undo

This commit is contained in:
Lucas Oliveira 2023-10-20 19:00:57 -03:00
commit 3229487cf0

View file

@ -52,25 +52,23 @@ export function UndoRedoProvider({ children }) {
const takeSnapshot = useCallback(() => { const takeSnapshot = useCallback(() => {
// push the current graph to the past state // push the current graph to the past state
setPast((old) => { let newPast = cloneDeep(past);
let newPast = cloneDeep(old); let newState = {
let newState = { nodes: cloneDeep(getNodes()),
nodes: cloneDeep(getNodes()), edges: cloneDeep(getEdges()),
edges: cloneDeep(getEdges()), };
}; if (
if ( past[tabIndex] &&
old[tabIndex] && JSON.stringify(past[tabIndex][past[tabIndex].length - 1]) !==
JSON.stringify(old[tabIndex][old[tabIndex].length - 1]) !== JSON.stringify(newState)
JSON.stringify(newState) ) {
) { newPast[tabIndex] = past[tabIndex].slice(
newPast[tabIndex] = old[tabIndex].slice( past[tabIndex].length - defaultOptions.maxHistorySize + 1,
old[tabIndex].length - defaultOptions.maxHistorySize + 1, past[tabIndex].length
old[tabIndex].length );
); newPast[tabIndex].push(newState);
newPast[tabIndex].push(newState); }
} setPast(newPast);
return newPast;
});
// whenever we take a new snapshot, the redo operations need to be cleared to avoid state mismatches // whenever we take a new snapshot, the redo operations need to be cleared to avoid state mismatches
setFuture((old) => { setFuture((old) => {