Skip to content

Commit f1c4cc1

Browse files
committed
Fix: background dissapears after undo first action
Initial snapshot was takken before background image was set to canvas. When undoing this caused canvas to restore to a state without background. Now wait for background image to exist in fabric canvas and take initial snaphot in separate effect.
1 parent fac1635 commit f1c4cc1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ts/components/MediaEditor.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ export function MediaEditor({
325325
width: img.width,
326326
};
327327
setImageState(newImageState);
328-
takeSnapshot('initial state', newImageState, canvas);
329328
};
330329
img.onerror = (
331330
event: Event | string,
@@ -588,6 +587,20 @@ export function MediaEditor({
588587
drawFabricBackgroundImage({ fabricCanvas, image, imageState });
589588
}, [fabricCanvas, image, imageState]);
590589

590+
const initialSnapshotTaken = useRef(false);
591+
useEffect(() => {
592+
if (
593+
!fabricCanvas ||
594+
!fabricCanvas.backgroundImage ||
595+
initialSnapshotTaken.current
596+
) {
597+
return;
598+
}
599+
600+
takeSnapshot('initial state', imageState, fabricCanvas);
601+
initialSnapshotTaken.current = true;
602+
}, [fabricCanvas, imageState, takeSnapshot]);
603+
591604
const [canCrop, setCanCrop] = useState(false);
592605
const [cropAspectRatioLock, setCropAspectRatioLock] = useState(false);
593606
const [drawTool, setDrawTool] = useState<DrawTool>(DrawTool.Pen);

ts/util/lint/exceptions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,5 +2320,12 @@
23202320
"line": " message.innerHTML = window.i18n('icu:optimizingApplication');",
23212321
"reasonCategory": "usageTrusted",
23222322
"updated": "2021-09-17T21:02:59.414Z"
2323+
},
2324+
{
2325+
"rule": "React-useRef",
2326+
"path": "ts/components/MediaEditor.tsx",
2327+
"line": " const initialSnapshotTaken = useRef(false);",
2328+
"reasonCategory": "usageTrusted",
2329+
"updated": "2025-10-10T09:45:25.353Z"
23232330
}
23242331
]

0 commit comments

Comments
 (0)