Fix: undo and redo state management

This commit is contained in:
Ayobami
2025-07-06 21:06:47 +01:00
parent 2100daadeb
commit 12546d02c4
+32 -13
View File
@@ -609,26 +609,45 @@ export const DockBuilder = () => {
const onUndoClick = useCallback(() => {
const undoResult = stack.undo();
if (undoResult && undoResult.currentState) {
editorMemo.loadFromJSON(undoResult.currentState, () => {
editorMemo.renderAll();
if (undoResult.currentCount === -1) {
return 0;
}
if (undoResult.currentCount === 0) {
editorMemo.getObjects().forEach((obj) => {
if (obj !== editorMemo.backgroundImage) {
if (obj.type == "image") {
editorMemo.remove(obj).renderAll();
}
}
const data = editorMemo.toJSON(["dockData"]);
localStorage.setItem("dock", JSON.stringify(data));
});
} else if (undoResult.currentCount > 0) {
const json = JSON.parse(undoResult.currentState);
editorMemo.loadFromJSON(
json,
() => {
editorMemo.renderAll();
},
(obj) => {}
);
}
}, [editorMemo]);
const onRedoClick = useCallback(() => {
const redoResult = stack.redo();
if (redoResult && redoResult.currentState) {
// currentState is an array, get the first element
const state = Array.isArray(redoResult.currentState)
? redoResult.currentState[0]
: redoResult.currentState;
if (state) {
editorMemo.loadFromJSON(state, () => {
if (!redoResult.currentState) {
return 0;
}
localStorage.setItem("dock", redoResult.currentState);
editorMemo.loadFromJSON(
JSON.parse(redoResult.currentState),
() => {
editorMemo.renderAll();
});
}
}
},
(o) => {}
);
}, [editorMemo]);
const onPrintScreen = useCallback(() => {