fix(mermaid): not recentering when using the sample switcher

This commit is contained in:
Elian Doran 2026-03-14 10:37:26 +02:00
parent 5f1486cf6a
commit dce9f50911
No known key found for this signature in database

View File

@ -152,6 +152,7 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,
function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg: string | undefined) {
const lastPanZoom = useRef<{ pan: SvgPanZoom.Point, zoom: number }>();
const lastNoteId = useRef<string>();
const wasEmpty = useRef<boolean>(false);
const zoomRef = useRef<SvgPanZoom.Instance>();
const width = useElementSize(containerRef);
@ -159,9 +160,14 @@ function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg
useEffect(() => {
if (zoomRef.current || width?.width === 0) return;
const shouldPreservePanZoom = (lastNoteId.current === noteId);
const shouldPreservePanZoom = (lastNoteId.current === noteId) && !wasEmpty.current;
const svgEl = containerRef.current?.querySelector("svg");
if (!svgEl) return;
if (!svgEl) {
if (svg?.trim().length === 0) {
wasEmpty.current = true;
}
return;
};
const zoomInstance = svgPanZoom(svgEl, {
zoomEnabled: true,
@ -187,7 +193,7 @@ function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg
zoomRef.current = undefined;
zoomInstance.destroy();
};
}, [ svg, width ]);
}, [ containerRef, noteId, svg, width ]);
// React to container changes.
useEffect(() => {