diff --git a/locales/en.yml b/locales/en.yml index d7229a3b..5cbb79e1 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -2975,6 +2975,16 @@ tools: label-your-mermaid-to-convert: "Your Mermaid to convert:" title-preview: Preview tag-invalid-mermaid-syntax: Invalid Mermaid syntax + tag-showing-last-valid-diagram: Showing the last valid diagram + tag-hint-ctrl-scroll-to-zoom: Hold Ctrl (or ⌘) and scroll to zoom + label-diagram-preview: Diagram preview — drag or use the arrow keys to pan, + and - to zoom + label-zoom-level-reset: "Zoom {zoom}%, click to reset to 100%" + title-zoom-in: Zoom in + title-zoom-out: Zoom out + title-reset-zoom: Reset zoom to 100% + title-fit-view: Fit diagram to view + title-fullscreen: Fullscreen + title-exit-fullscreen: Exit fullscreen tag-export-as-png: Export as PNG tag-export-as-jpg: Export as JPG tag-export-as-svg: Export as SVG diff --git a/locales/fr.yml b/locales/fr.yml index 80fa3310..985109b4 100644 --- a/locales/fr.yml +++ b/locales/fr.yml @@ -4563,6 +4563,16 @@ tools: tag-export-as-png: Exporter comme PNG tag-export-as-svg: Exporter comme SVG tag-invalid-mermaid-syntax: Syntaxe de Mermaid non valide + tag-showing-last-valid-diagram: Affichage du dernier diagramme valide + tag-hint-ctrl-scroll-to-zoom: Maintenez Ctrl (ou ⌘) et faites défiler pour zoomer + label-diagram-preview: Prévisualisation du diagramme — faites glisser ou utilisez les flèches pour vous déplacer, + et - pour zoomer + label-zoom-level-reset: "Zoom {zoom} %, cliquez pour réinitialiser à 100 %" + title-zoom-in: Zoomer + title-zoom-out: Dézoomer + title-reset-zoom: Réinitialiser le zoom à 100 % + title-fit-view: Ajuster le diagramme à la vue + title-fullscreen: Plein écran + title-exit-fullscreen: Quitter le plein écran title-input-mermaid: Mermaid d'entrée title-preview: Prévisualisation title: Exportateur de Mermaid diff --git a/src/tools/mermaid-exporter/mermaid-exporter.e2e.spec.ts b/src/tools/mermaid-exporter/mermaid-exporter.e2e.spec.ts index dd4149d3..3eee86b2 100644 --- a/src/tools/mermaid-exporter/mermaid-exporter.e2e.spec.ts +++ b/src/tools/mermaid-exporter/mermaid-exporter.e2e.spec.ts @@ -6,13 +6,45 @@ test.describe('Mermaid Diagram Renderer', () => { }); test('should render the initial diagram', async ({ page }) => { - const diagramContainer = page.locator('.diagram-container svg'); + const diagramContainer = page.locator('.viewport svg'); // mermaid is a large lazy-loaded chunk; cold CI browsers (webkit // especially) can exceed the default 5s expect timeout await expect(diagramContainer).toBeVisible({ timeout: 30_000 }); await expect(diagramContainer).toHaveAttribute('xmlns', 'http://www.w3.org/2000/svg'); }); + test('should zoom from the toolbar and reset to 100%', async ({ page }) => { + await expect(page.locator('.viewport svg')).toBeVisible({ timeout: 30_000 }); + + // the default diagram is small enough to fit, so it starts at 1:1 + const zoomLevel = page.getByTestId('zoom-level'); + await expect(zoomLevel).toHaveText('100%'); + + await page.getByTestId('zoom-in').click(); + await expect(zoomLevel).toHaveText('125%'); + + await page.getByTestId('zoom-out').click(); + await expect(zoomLevel).toHaveText('100%'); + + await page.getByTestId('zoom-in').click(); + await zoomLevel.click(); + await expect(zoomLevel).toHaveText('100%'); + }); + + test('should pan with the keyboard once the preview is focused', async ({ page }) => { + await expect(page.locator('.viewport svg')).toBeVisible({ timeout: 30_000 }); + + const canvas = page.locator('.canvas'); + const before = await canvas.evaluate(el => getComputedStyle(el).transform); + + await page.locator('.viewport').focus(); + await page.keyboard.press('ArrowRight'); + + await expect + .poll(async () => canvas.evaluate(el => getComputedStyle(el).transform)) + .not.toBe(before); + }); + test('should update diagram when user edits Mermaid code', async ({ page }) => { const input = page.getByLabel('Your Mermaid to convert:'); const updatedMermaid = ` @@ -24,7 +56,7 @@ test.describe('Mermaid Diagram Renderer', () => { await input.fill(updatedMermaid); await page.waitForTimeout(500); - const svg = page.locator('.diagram-container svg'); + const svg = page.locator('.viewport svg'); await expect(svg).toContainText('Start'); await expect(svg).toContainText('Success!'); }); @@ -38,4 +70,18 @@ test.describe('Mermaid Diagram Renderer', () => { await button.click(); } }); + + test('should keep the last valid diagram but refuse to export it on a syntax error', async ({ page }) => { + await expect(page.locator('.viewport svg')).toBeVisible({ timeout: 30_000 }); + + await page.getByLabel('Your Mermaid to convert:').fill('graph TD\nA --> not valid {{{'); + + // the rendered diagram stays on screen, flagged as out of date + await expect(page.locator('.error-overlay')).toBeVisible(); + await expect(page.locator('.canvas.stale')).toBeVisible(); + await expect(page.locator('.viewport svg')).toBeVisible(); + + // exporting now would hand back a file that does not match the code + await expect(page.getByRole('button', { name: /Export as PNG/i })).toBeDisabled(); + }); }); diff --git a/src/tools/mermaid-exporter/mermaid-exporter.service.test.ts b/src/tools/mermaid-exporter/mermaid-exporter.service.test.ts new file mode 100644 index 00000000..6f96d61a --- /dev/null +++ b/src/tools/mermaid-exporter/mermaid-exporter.service.test.ts @@ -0,0 +1,127 @@ +import { describe, expect, it } from 'vitest'; +import { + centerTransform, + clampZoom, + exceedsRefitThreshold, + fitTransform, + wheelZoomFactor, + zoomAround, +} from './mermaid-exporter.service'; + +const MIN = 0.05; +const MAX = 10; + +describe('mermaid-exporter service', () => { + describe('clampZoom', () => { + it('leaves a value inside the range untouched', () => { + expect(clampZoom(1.5, MIN, MAX)).toBe(1.5); + }); + + it('clamps to the bounds', () => { + expect(clampZoom(0.001, MIN, MAX)).toBe(MIN); + expect(clampZoom(1000, MIN, MAX)).toBe(MAX); + }); + + it('clamps a non-finite value to the maximum', () => { + expect(clampZoom(Number.POSITIVE_INFINITY, MIN, MAX)).toBe(MAX); + }); + }); + + describe('zoomAround', () => { + it('keeps the anchor point fixed', () => { + const before = { zoom: 1, x: 0, y: 0 }; + const after = zoomAround(before, 2, 100, 50, MIN, MAX); + + // the content point under the anchor must not move: at zoom z the point + // is at (anchor - pan) / z in content space + expect((100 - before.x) / before.zoom).toBeCloseTo((100 - after.x) / after.zoom, 6); + expect((50 - before.y) / before.zoom).toBeCloseTo((50 - after.y) / after.zoom, 6); + expect(after.zoom).toBe(2); + }); + + it('does not drift the view once the zoom is clamped', () => { + const atMax = { zoom: MAX, x: -120, y: -80 }; + expect(zoomAround(atMax, 4, 200, 100, MIN, MAX)).toEqual(atMax); + + const atMin = { zoom: MIN, x: 10, y: 20 }; + expect(zoomAround(atMin, 0.1, 200, 100, MIN, MAX)).toEqual(atMin); + }); + }); + + describe('fitTransform', () => { + it('scales a large diagram down and centres it', () => { + const view = fitTransform({ width: 500, height: 400 }, { width: 4000, height: 2000 }, 20, MIN, MAX); + + // width is the binding constraint: (500 - 20) / 4000 + expect(view.zoom).toBeCloseTo(0.12, 6); + expect(view.x).toBeCloseTo((500 - 4000 * 0.12) / 2, 6); + expect(view.y).toBeCloseTo((400 - 2000 * 0.12) / 2, 6); + }); + + it('never scales a small diagram above 1:1', () => { + const view = fitTransform({ width: 800, height: 600 }, { width: 100, height: 50 }, 20, MIN, MAX); + + expect(view.zoom).toBe(1); + expect(view.x).toBe(350); + expect(view.y).toBe(275); + }); + + it('honours the minimum zoom for a diagram that cannot fit', () => { + const view = fitTransform({ width: 200, height: 200 }, { width: 100000, height: 100 }, 20, MIN, MAX); + + expect(view.zoom).toBe(MIN); + }); + }); + + describe('centerTransform', () => { + it('centres the scaled content', () => { + expect(centerTransform({ width: 600, height: 400 }, { width: 200, height: 100 }, 2)).toEqual({ + zoom: 2, + x: 100, + y: 100, + }); + }); + }); + + describe('wheelZoomFactor', () => { + it('zooms in on a negative delta and out on a positive one', () => { + expect(wheelZoomFactor(-100, 0, 0.0015)).toBeGreaterThan(1); + expect(wheelZoomFactor(100, 0, 0.0015)).toBeLessThan(1); + }); + + it('gives a line-mode notch the same weight as a pixel-mode one', () => { + // Firefox reports ~3 lines where Chrome reports ~100 pixels + const chrome = wheelZoomFactor(-100, 0, 0.0015); + const firefox = wheelZoomFactor(-3, 1, 0.0015); + + expect(firefox).toBeCloseTo(chrome, 1); + expect(firefox).toBeGreaterThan(1.1); + }); + + it('scales page-mode deltas up as well', () => { + expect(wheelZoomFactor(-1, 2, 0.0015)).toBeGreaterThan(wheelZoomFactor(-1, 0, 0.0015)); + }); + }); + + describe('exceedsRefitThreshold', () => { + it('ignores a change under the threshold', () => { + expect(exceedsRefitThreshold({ width: 1000, height: 500 }, { width: 1010, height: 505 }, 0.02)).toBe(false); + }); + + it('reports a change over the threshold in either dimension', () => { + expect(exceedsRefitThreshold({ width: 1000, height: 500 }, { width: 1100, height: 500 }, 0.02)).toBe(true); + expect(exceedsRefitThreshold({ width: 1000, height: 500 }, { width: 1000, height: 600 }, 0.02)).toBe(true); + }); + + it('treats the first render, measured against a zero size, as a change', () => { + expect(exceedsRefitThreshold({ width: 0, height: 0 }, { width: 300, height: 200 }, 0.02)).toBe(true); + }); + + it('accumulates: small edits measured against the fitted size eventually refit', () => { + const fitted = { width: 1000, height: 500 }; + // ten 1.5% edits never trip the threshold against the previous render, + // but together they are 15% away from the size the view was fitted to + expect(exceedsRefitThreshold(fitted, { width: 1150, height: 500 }, 0.02)).toBe(true); + }); + }); +}); diff --git a/src/tools/mermaid-exporter/mermaid-exporter.service.ts b/src/tools/mermaid-exporter/mermaid-exporter.service.ts new file mode 100644 index 00000000..f9efdba9 --- /dev/null +++ b/src/tools/mermaid-exporter/mermaid-exporter.service.ts @@ -0,0 +1,85 @@ +export interface ViewTransform { zoom: number; x: number; y: number } +export interface Size { width: number; height: number } + +/** + * Keeps a zoom factor inside the supported range. + */ +export function clampZoom(value: number, min: number, max: number): number { + return Math.min(max, Math.max(min, value)); +} + +/** + * Zooms by `factor` while keeping the content point under (originX, originY) — + * viewport coordinates, typically the cursor — anchored in place. + */ +export function zoomAround( + view: ViewTransform, + factor: number, + originX: number, + originY: number, + min: number, + max: number, +): ViewTransform { + const zoom = clampZoom(view.zoom * factor, min, max); + const ratio = zoom / view.zoom; + + return { + zoom, + x: originX - (originX - view.x) * ratio, + y: originY - (originY - view.y) * ratio, + }; +} + +/** + * Scales the content down to fit entirely inside the viewport (never up past + * 1:1) and centres it, leaving `padding` pixels of margin. + */ +export function fitTransform(viewport: Size, content: Size, padding: number, min: number, max: number): ViewTransform { + const zoom = clampZoom( + Math.min( + (viewport.width - padding) / content.width, + (viewport.height - padding) / content.height, + 1, + ), + min, + max, + ); + + return centerTransform(viewport, content, zoom); +} + +/** + * Centres the content in the viewport at the given zoom. + */ +export function centerTransform(viewport: Size, content: Size, zoom: number): ViewTransform { + return { + zoom, + x: (viewport.width - content.width * zoom) / 2, + y: (viewport.height - content.height * zoom) / 2, + }; +} + +/** + * Converts a wheel event delta into a zoom factor. `deltaY` is normalised + * because browsers report wheel notches in different units — Firefox in lines, + * Chrome in pixels — which would otherwise make one of them barely zoom. + */ +export function wheelZoomFactor(deltaY: number, deltaMode: number, sensitivity: number): number { + const LINE_HEIGHT = 33; + const PAGE_HEIGHT = 400; + const unit = deltaMode === 1 ? LINE_HEIGHT : deltaMode === 2 ? PAGE_HEIGHT : 1; + + return Math.exp(-deltaY * unit * sensitivity); +} + +/** + * Whether a newly rendered diagram differs enough from the one the view was + * last fitted to that the view should be re-fitted. Comparing against the + * fitted size rather than the previous render keeps a series of small edits + * from drifting the diagram out of frame without ever crossing the threshold. + */ +export function exceedsRefitThreshold(fitted: Size, rendered: Size, ratio: number): boolean { + const changed = (from: number, to: number) => Math.abs(to - from) / (from || 1) > ratio; + + return changed(fitted.width, rendered.width) || changed(fitted.height, rendered.height); +} diff --git a/src/tools/mermaid-exporter/mermaid-exporter.vue b/src/tools/mermaid-exporter/mermaid-exporter.vue index 7154dcf3..c168cea4 100644 --- a/src/tools/mermaid-exporter/mermaid-exporter.vue +++ b/src/tools/mermaid-exporter/mermaid-exporter.vue @@ -1,11 +1,22 @@