From a56be6ae14e25a24a2455cc3fc14a570b976fff3 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Fri, 14 Mar 2025 18:00:12 -0700 Subject: [PATCH] fix: Flickering when rendering, PanZoom & Rough mode combined --- src/lib/components/View.svelte | 35 +++++++++------------------------- src/routes/edit/+page.svelte | 8 ++------ 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/lib/components/View.svelte b/src/lib/components/View.svelte index f3fcf380..0fb12dd7 100644 --- a/src/lib/components/View.svelte +++ b/src/lib/components/View.svelte @@ -5,6 +5,7 @@ import { inputStateStore, stateStore, updateCodeStore } from '$lib/util/state'; import { logEvent, saveStatistics } from '$lib/util/stats'; import { cmdKey } from '$lib/util/util'; + import uniqueID from 'lodash-es/uniqueId'; import type { MermaidConfig } from 'mermaid'; import { onMount } from 'svelte'; import panzoom from 'svg-pan-zoom'; @@ -17,7 +18,6 @@ let view: HTMLDivElement | undefined = $state(); let error = $state(false); let outOfSync = $state(false); - let hide = $state(false); let manualUpdate = true; let panZoomEnabled = $stateStore.panZoom; let pzoom: typeof panzoom | undefined; @@ -31,18 +31,13 @@ logEvent('panZoom'); }; - const handlePanZoom = (state: State) => { + const handlePanZoom = (state: State, graphDiv: SVGSVGElement) => { if (!state.panZoom) { return; } - hide = true; pzoom?.destroy(); pzoom = undefined; void Promise.resolve().then(() => { - const graphDiv = document.querySelector('#graph-div'); - if (!graphDiv) { - return; - } pzoom = panzoom(graphDiv, { onPan: handlePanZoomChange, onZoom: handlePanZoomChange, @@ -55,7 +50,6 @@ pzoom.zoom(zoom); pzoom.pan(pan); } - hide = false; }); }; @@ -95,20 +89,16 @@ rough = state.rough; const scroll = view?.parentElement?.scrollTop; delete container.dataset.processed; + const viewID = uniqueID('graph-'); const { svg, bindFunctions, diagramType: detectedDiagramType - } = await renderDiagram( - Object.assign({}, JSON.parse(state.mermaid)) as MermaidConfig, - code, - 'graph-div' - ); + } = await renderDiagram(JSON.parse(state.mermaid) as MermaidConfig, code, viewID); diagramType = detectedDiagramType; if (svg.length > 0) { - handlePanZoom(state); container.innerHTML = svg; - const graphDiv = document.querySelector('#graph-div'); + let graphDiv = document.querySelector(`#${viewID}`); if (!graphDiv) { throw new Error('graph-div not found'); } @@ -117,7 +107,7 @@ svg2roughjs.svg = graphDiv; await svg2roughjs.sketch(); graphDiv.remove(); - const sketch = document.querySelector('#container > svg'); + const sketch = document.querySelector('#container > svg'); if (!sketch) { throw new Error('sketch not found'); } @@ -127,6 +117,7 @@ sketch.setAttribute('width', '100%'); sketch.setAttribute('viewBox', `0 0 ${width} ${height}`); sketch.style.maxWidth = '100%'; + graphDiv = sketch; } else { graphDiv.setAttribute('height', '100%'); graphDiv.style.maxWidth = '100%'; @@ -134,6 +125,7 @@ bindFunctions(graphDiv); } } + handlePanZoom(state, graphDiv); } if (view?.parentElement && scroll) { view.parentElement.scrollTop = scroll; @@ -183,24 +175,15 @@ {/if}
-
+
diff --git a/src/routes/edit/+page.svelte b/src/routes/edit/+page.svelte index 0df73c5c..e8c19b47 100644 --- a/src/routes/edit/+page.svelte +++ b/src/routes/edit/+page.svelte @@ -192,7 +192,7 @@
-