Skip to content

Commit 63bd1d5

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 36a6a1f commit 63bd1d5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ts/components/MediaEditor.dom.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
@@ -2327,5 +2327,12 @@
23272327
"line": " message.innerHTML = window.SignalContext.i18n('icu:optimizingApplication');",
23282328
"reasonCategory": "usageTrusted",
23292329
"updated": "2021-09-17T21:02:59.414Z"
2330+
},
2331+
{
2332+
"rule": "React-useRef",
2333+
"path": "ts/components/MediaEditor.tsx",
2334+
"line": " const initialSnapshotTaken = useRef(false);",
2335+
"reasonCategory": "usageTrusted",
2336+
"updated": "2025-10-10T09:45:25.353Z"
23302337
}
23312338
]

0 commit comments

Comments
 (0)