Fix: undo and redo state management
This commit is contained in:
@@ -609,26 +609,45 @@ export const DockBuilder = () => {
|
|||||||
|
|
||||||
const onUndoClick = useCallback(() => {
|
const onUndoClick = useCallback(() => {
|
||||||
const undoResult = stack.undo();
|
const undoResult = stack.undo();
|
||||||
if (undoResult && undoResult.currentState) {
|
if (undoResult.currentCount === -1) {
|
||||||
editorMemo.loadFromJSON(undoResult.currentState, () => {
|
return 0;
|
||||||
editorMemo.renderAll();
|
}
|
||||||
|
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]);
|
}, [editorMemo]);
|
||||||
|
|
||||||
const onRedoClick = useCallback(() => {
|
const onRedoClick = useCallback(() => {
|
||||||
const redoResult = stack.redo();
|
const redoResult = stack.redo();
|
||||||
if (redoResult && redoResult.currentState) {
|
if (!redoResult.currentState) {
|
||||||
// currentState is an array, get the first element
|
return 0;
|
||||||
const state = Array.isArray(redoResult.currentState)
|
|
||||||
? redoResult.currentState[0]
|
|
||||||
: redoResult.currentState;
|
|
||||||
if (state) {
|
|
||||||
editorMemo.loadFromJSON(state, () => {
|
|
||||||
editorMemo.renderAll();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localStorage.setItem("dock", redoResult.currentState);
|
||||||
|
editorMemo.loadFromJSON(
|
||||||
|
JSON.parse(redoResult.currentState),
|
||||||
|
() => {
|
||||||
|
editorMemo.renderAll();
|
||||||
|
},
|
||||||
|
(o) => {}
|
||||||
|
);
|
||||||
}, [editorMemo]);
|
}, [editorMemo]);
|
||||||
|
|
||||||
const onPrintScreen = useCallback(() => {
|
const onPrintScreen = useCallback(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user