Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mermaid): restore SVG viewBox so gantt diagrams survive split resizes
matt wilkiea3b451ba0e43
diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsxindex 4e90b48dbc..10847abb4f 100644--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx@@ -175,6 +175,13 @@ function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svgreturn;};+ // svg-pan-zoom strips the SVG's viewBox attribute on init and never restores it on+ // destroy(), so a re-init (e.g. triggered by a `width` change below) would otherwise+ // fit from `getBBox()` instead. For mermaid gantt diagrams the bounding box is inflated+ // by an off-screen "today" marker, which makes the re-fit shrink the chart to invisibility+ // (issue #9749). Save it here and restore it in the cleanup below.+ const viewBox = svgEl.getAttribute("viewBox");+const zoomInstance = svgPanZoom(svgEl, {zoomEnabled: true,controlIconsEnabled: false@@ -198,6 +205,9 @@ function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg};zoomRef.current = undefined;zoomInstance.destroy();+ if (viewBox !== null) {+ svgEl.setAttribute("viewBox", viewBox);+ }};}, [ containerRef, noteId, svg, width ]);diff --git a/packages/trilium-core/src/test/fixtures/document.db b/packages/trilium-core/src/test/fixtures/document.dbindex deff1cafed..f6f1272683 100644Binary files a/packages/trilium-core/src/test/fixtures/document.db and b/packages/trilium-core/src/test/fixtures/document.db differdiff --git a/packages/trilium-e2e/src/note_types/mermaid.spec.ts b/packages/trilium-e2e/src/note_types/mermaid.spec.tsindex f64a447d78..eb648c84fc 100644--- a/packages/trilium-e2e/src/note_types/mermaid.spec.ts+++ b/packages/trilium-e2e/src/note_types/mermaid.spec.ts@@ -1,4 +1,4 @@-import { test, expect, Page, BrowserContext } from "@playwright/test";+import { test, expect, Page, BrowserContext, Locator } from "@playwright/test";import App from "../support/app";test("renders ELK flowchart", async ({ page, context }) => {@@ -67,3 +67,86 @@ async function testAriaSnapshot({ page, context, noteTitle, snapshot }: AriaTestawait expect(svgData).toBeVisible();await expect(svgData).toMatchAriaSnapshot(snapshot);}++test("gantt diagram survives split divider drag (issue 9749)", async ({ page, context }) => {+ await testDividerDragSurvival({ page, context, noteTitle: "Gantt" });+});++test("gantt diagram with pathological date range survives split divider drag (issue 9749)", async ({ page, context }) => {+ await testDividerDragSurvival({ page, context, noteTitle: "Bar chart" });+});++/**+ * Regression test for issue #9749: mermaid gantt-based diagrams (which mermaid renders with a+ * SVG `viewBox` inflated far beyond the visible chart, because of an off-screen "today" marker)+ * would collapse to a sub-pixel sliver after the split divider was dragged. The root cause was+ * that `svg-pan-zoom` strips the SVG's `viewBox` attribute on init and never restores it on+ * `destroy()`, so every divider-drag-triggered resize re-initialized pan/zoom by fitting from+ * `getBBox()` instead of the original `viewBox`, each time shrinking the effective scale further.+ *+ * The regression only shows up once the divider returns to (approximately) its original position:+ * a one-directional drag legitimately shrinks the rendered diagram proportionally to the smaller+ * container, so this asserts on a round-trip drag (out and back) and compares the final height to+ * the height before dragging. Height is asserted rather than width because the gantt bounding box+ * width is dominated by the off-screen "today" marker even in the healthy case.+ */+async function testDividerDragSurvival({ page, context, noteTitle }: { page: Page; context: BrowserContext; noteTitle: string }) {+ const app = new App(page, context);+ await app.goto();+ await app.goToNoteInNewTab(noteTitle);++ const svgData = app.currentNoteSplit.locator(".render-container svg");+ await expect(svgData).toBeVisible();++ const viewport = app.currentNoteSplit.locator(".render-container svg .svg-pan-zoom_viewport");+ await expect(viewport).toBeVisible();++ // Let the initial pan/zoom fit settle (mounting can itself trigger one resize-driven re-init+ // as the ResizeObserver reports the container's real size shortly after mount).+ await page.waitForTimeout(500);++ const beforeBox = requireBoundingBox(await viewport.boundingBox(), "viewport (before drag)");+ expect(beforeBox.height).toBeGreaterThan(0);++ const gutter = app.currentNoteSplit.locator(".gutter");+ await expect(gutter).toHaveCount(1);+ const gutterBox = requireBoundingBox(await gutter.boundingBox(), "gutter");++ const startX = gutterBox.x + gutterBox.width / 2;+ const startY = gutterBox.y + gutterBox.height / 2;+ const dragDistance = 150;+ const stepSize = 30;++ // Drag the divider out and back so it ends up near its original position. ResizeObserver+ // fires repeatedly along the way, triggering multiple destroy/re-init cycles.+ await page.mouse.move(startX, startY);+ await page.mouse.down();+ for (let offset = stepSize; offset <= dragDistance; offset += stepSize) {+ await page.mouse.move(startX + offset, startY, { steps: 5 });+ }+ for (let offset = dragDistance - stepSize; offset >= 0; offset -= stepSize) {+ await page.mouse.move(startX + offset, startY, { steps: 5 });+ }+ await page.mouse.up();++ await page.waitForTimeout(500);++ const afterBox = requireBoundingBox(await viewport.boundingBox(), "viewport (after drag)");++ // With the bug, this collapses to a sub-pixel sliver (observed ratio ~0.001 or less) even+ // though the container returned to its original size. A healthy re-fit reproduces (close to)+ // the original height.+ expect(afterBox.height).toBeGreaterThan(beforeBox.height * 0.5);+}++/**+ * Playwright's `boundingBox()` returns `null` when the element isn't attached/visible. The tests+ * above only call this once visibility has already been asserted, so a `null` here means something+ * unexpected happened - fail loudly with context instead of silently narrowing with `!`.+ */+function requireBoundingBox(box: Awaited<ReturnType<Locator["boundingBox"]>>, label: string) {+ if (!box) {+ throw new Error(`Expected a bounding box for ${label}, but the element was not visible/attached.`);+ }+ return box;+}