main
parent
be9a045b4a
commit
bfc4b61b83
|
@ -143,29 +143,31 @@ const DrawingCanvas = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const deepCopyShapes = (shapesArr) => JSON.parse(JSON.stringify(shapesArr));
|
||||||
|
|
||||||
const handlePointerUp = () => {
|
const handlePointerUp = () => {
|
||||||
setIsDrawing(false);
|
setIsDrawing(false);
|
||||||
setLastPosition(null);
|
setLastPosition(null);
|
||||||
// Save a snapshot of shapes for undo/redo
|
// Save a deep copy of shapes for undo/redo
|
||||||
setHistory(prev => [...prev, shapes]);
|
setHistory(prev => [...prev, deepCopyShapes(shapes)]);
|
||||||
setUndoHistory([]);
|
setUndoHistory([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Undo: restore the previous snapshot
|
// Undo: restore the previous snapshot
|
||||||
const handleUndo = () => {
|
const handleUndo = () => {
|
||||||
if (history.length === 0) return;
|
if (history.length === 0) return;
|
||||||
setUndoHistory(prev => [...prev, shapes]);
|
setUndoHistory(prev => [...prev, deepCopyShapes(shapes)]);
|
||||||
const prevShapes = history[history.length - 1];
|
const prevShapes = history[history.length - 1];
|
||||||
setShapes(prevShapes);
|
setShapes(deepCopyShapes(prevShapes));
|
||||||
setHistory(prev => prev.slice(0, -1));
|
setHistory(prev => prev.slice(0, -1));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Redo: restore the next snapshot
|
// Redo: restore the next snapshot
|
||||||
const handleRedo = () => {
|
const handleRedo = () => {
|
||||||
if (undoHistory.length === 0) return;
|
if (undoHistory.length === 0) return;
|
||||||
setHistory(prev => [...prev, shapes]);
|
setHistory(prev => [...prev, deepCopyShapes(shapes)]);
|
||||||
const nextShapes = undoHistory[undoHistory.length - 1];
|
const nextShapes = undoHistory[undoHistory.length - 1];
|
||||||
setShapes(nextShapes);
|
setShapes(deepCopyShapes(nextShapes));
|
||||||
setUndoHistory(prev => prev.slice(0, -1));
|
setUndoHistory(prev => prev.slice(0, -1));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue