From 55b8d8d8980065eefb353b8d5ddff3caa077164d Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Fri, 20 Nov 2026 09:57:44 +0100 Subject: [PATCH] ui: full-screen diagram preview that zooms around what is on screen, inert diagrams (#32591) * ui: keep Pele diagrams inert Pele turns a node with `click ... href` into a live link, where Mermaid's strict security level left diagrams without links. Unwrap every link in Pele's output, keeping its children, so the inline block, the live-preview editor, the preview dialog and the downloaded svg stay inert. The Diagrams story's flowchart now carries click directives and its play test checks that no link or href is left. Assisted-by: Claude (Opus 5.5 Medium) * ui: open diagrams full screen, zoom around what is on screen The diagram preview dialog fills the window on every device. The preview places the diagram from its top left corner, so a zoom keeps a screen point fixed: the wheel and a trackpad pinch zoom around the pointer, the buttons and +/- keys around the middle of the screen, wherever the diagram has been panned to. Two-finger pinch, double-click and arrow keys work too. The diagram opens fitted and keeps fitting until panned or zoomed; Reset view fits it again. Adds MermaidPreview and DialogMermaidPreview stories with play tests and a unit test for the zoom math. Assisted-by: Claude (Opus 5.5 Medium) * ui: add a diagrams trace to the demo fixtures Assisted-by: Claude (Opus 5.5 Medium) --- ui/apps/chat/demo/fixtures/traces.ts | 30 +++ .../MarkdownContent/markdown-diagrams.ts | 8 + .../app/content/MermaidPreview.svelte | 255 ++++++++++++++---- .../app/content/MermaidPreviewControls.svelte | 2 +- .../src/lib/components/app/content/index.ts | 16 +- .../app/dialogs/DialogMermaidPreview.svelte | 3 +- .../src/lib/components/app/dialogs/index.ts | 24 +- .../chat/src/lib/utils/diagram-viewport.ts | 75 ++++++ .../content/MarkdownContent.stories.svelte | 28 +- .../content/MermaidPreview.stories.svelte | 132 +++++++++ .../DialogMermaidPreview.stories.svelte | 41 +++ .../stories/fixtures/mermaid-diagrams.ts | 5 +- .../tests/stories/fixtures/mermaid-preview.ts | 30 +++ .../chat/tests/unit/diagram-viewport.test.ts | 111 ++++++++ 14 files changed, 673 insertions(+), 87 deletions(-) create mode 100644 ui/apps/chat/src/lib/utils/diagram-viewport.ts create mode 100644 ui/apps/chat/tests/stories/content/MermaidPreview.stories.svelte create mode 100644 ui/apps/chat/tests/stories/dialogs/DialogMermaidPreview.stories.svelte create mode 100644 ui/apps/chat/tests/stories/fixtures/mermaid-preview.ts create mode 100644 ui/apps/chat/tests/unit/diagram-viewport.test.ts diff --git a/ui/apps/chat/demo/fixtures/traces.ts b/ui/apps/chat/demo/fixtures/traces.ts index cc7ed8979..dc766458e 100644 --- a/ui/apps/chat/demo/fixtures/traces.ts +++ b/ui/apps/chat/demo/fixtures/traces.ts @@ -301,9 +301,39 @@ function agenticDebugging(): Trace { return trace; } +/** A reply with a diagram, for diagram preview demos. */ +function diagrams(): Trace { + const trace = new Trace('How a request flows through llama-server'); + + trace.user('Can you draw how a chat request flows through llama-server?'); + trace.assistant( + [ + 'Here is the path a request takes:', + '', + '```mermaid', + 'flowchart LR', + ' U[Browser] -->|POST /v1/chat/completions| S(llama-server)', + ' S --> Q{Slot free?}', + ' Q -->|yes| P[Prompt processing]', + ' Q -->|no| W[Queue]', + ' W --> P', + ' P --> G[Sample token]', + ' G -->|more| G', + ' G -->|SSE tokens| U', + ' click S href "https://github.com/ggml-org/llama.cpp" "llama.cpp on GitHub" _blank', + '```', + '', + 'Requests wait in the queue until a slot is free, then tokens stream back as they are sampled.' + ].join('\n') + ); + + return trace; +} + /** Built-in traces, by name. */ export const DEMO_TRACES: Record Trace> = { 'agentic-debugging': agenticDebugging, + diagrams, 'long-chat': longChat }; diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts index ba607bfbc..ec3e35dd9 100644 --- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts +++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-diagrams.ts @@ -43,6 +43,13 @@ function diagramWidth(node: HTMLElement): number | undefined { return width > 0 ? width : undefined; } +// Pele wraps a node with a `click ... href` directive in a link. Diagrams come from model +// output, so they stay inert, as they were under Mermaid's strict security level: each link +// gives way to its children. The preview dialog and the downloaded file copy this svg. +function unwrapLinks(root: HTMLElement) { + for (const link of root.querySelectorAll('a')) link.replaceWith(...link.childNodes); +} + /** * Renders the mermaid diagrams in `container` that haven't been rendered yet, with Pele. * Marks nodes immediately to prevent duplicate renders during streaming. Colors come @@ -77,6 +84,7 @@ export async function renderMermaidDiagrams(container: HTMLElement) { }); node.innerHTML = svg; + unwrapLinks(node); } catch (error) { // the message quotes the diagram source, so it goes in as text const message = document.createElement('div'); diff --git a/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte b/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte index d981f67d1..934802581 100644 --- a/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte +++ b/ui/apps/chat/src/lib/components/app/content/MermaidPreview.svelte @@ -1,7 +1,16 @@ + + +
-
zoomBy(DIAGRAM_ZOOM_FACTOR)} + onZoomOut={() => zoomBy(1 / DIAGRAM_ZOOM_FACTOR)} + scale={view.scale} {svgHtml} />
diff --git a/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte b/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte index 1899db389..18f96c529 100644 --- a/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte +++ b/ui/apps/chat/src/lib/components/app/content/MermaidPreviewControls.svelte @@ -31,7 +31,7 @@