mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 00:20:51 +00:00
fix: load mermaid diagrams without source flashes or layout jumps (#12319)
* fix: load mermaid diagrams without source flashes or layout jumps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: use the shared skeleton for the mermaid placeholder Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5e89ba180e
commit
fecc8a6bfd
@@ -1,27 +1,60 @@
|
||||
<script lang="ts">
|
||||
<script module lang="ts">
|
||||
import { randomUUID } from '$lib/utils/uuid'
|
||||
|
||||
const MAX_REMEMBERED_DIAGRAMS = 50
|
||||
const STREAM_QUIET_MS = 300
|
||||
const remembered = new Map<string, { id: string; svg: string }>()
|
||||
|
||||
function newDiagramId() {
|
||||
return `mermaid-${randomUUID()}`
|
||||
}
|
||||
|
||||
function recall(key: string): string | undefined {
|
||||
const entry = remembered.get(key)
|
||||
if (!entry) return undefined
|
||||
remembered.delete(key)
|
||||
remembered.set(key, entry)
|
||||
// The id scopes the SVG's styles and arrow markers, and a `url(#id)` resolves to the first
|
||||
// match in the document: sharing it with a copy in a hidden preview tab loses the arrows.
|
||||
return entry.svg.replaceAll(entry.id, newDiagramId())
|
||||
}
|
||||
|
||||
function remember(key: string, id: string, svg: string) {
|
||||
remembered.set(key, { id, svg })
|
||||
if (remembered.size > MAX_REMEMBERED_DIAGRAMS) {
|
||||
remembered.delete(remembered.keys().next().value!)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte'
|
||||
import { slide } from 'svelte/transition'
|
||||
import { useIsDarkMode } from '$lib/components/DarkModeObserver.svelte'
|
||||
import { Button } from '$lib/components/common'
|
||||
import { Button, Skeleton } from '$lib/components/common'
|
||||
import Modal from '$lib/components/common/modal/Modal.svelte'
|
||||
import { copyToClipboard, download } from '$lib/utils'
|
||||
import { useReducedMotion } from '$lib/svelte5Utils.svelte'
|
||||
import { ClipboardCopy, Download, Maximize2, Minus, Plus, RotateCcw } from 'lucide-svelte'
|
||||
import type { PanZoom } from 'panzoom'
|
||||
|
||||
let { code, forceLight = false }: { code: string; forceLight?: boolean } = $props()
|
||||
|
||||
const isDarkMode = useIsDarkMode()
|
||||
const reducedMotion = useReducedMotion()
|
||||
|
||||
let svg = $state<string | undefined>(undefined)
|
||||
// The exact source that produced `svg`. The diagram is only shown while this
|
||||
// still matches the current `code`, so a later edit that fails to parse falls
|
||||
// back to the raw source instead of leaving a stale, mismatched diagram.
|
||||
let renderedCode = $state<string | undefined>(undefined)
|
||||
// The latest finished render had nothing to draw: show the source rather than a diagram that
|
||||
// no longer matches it. Held across a re-render so partial blocks streaming in stay as text.
|
||||
let showSource = $state(false)
|
||||
// Monotonic token so an earlier-started render that resolves late can't
|
||||
// overwrite the result of a newer one (out-of-order async on rapid code/theme changes).
|
||||
let renderSeq = 0
|
||||
// False while a render is in flight, true once it drew or failed. Exposed as
|
||||
// data-mermaid-pending so a print can wait for diagrams without counting fences.
|
||||
let settled = $state(false)
|
||||
// Only the placeholder-to-diagram swap animates, and a print must not capture it midway.
|
||||
let revealMs = $state(0)
|
||||
|
||||
let expanded = $state(false)
|
||||
|
||||
@@ -29,7 +62,17 @@
|
||||
const seq = ++renderSeq
|
||||
if (!source?.trim()) {
|
||||
svg = undefined
|
||||
renderedCode = undefined
|
||||
revealMs = 0
|
||||
showSource = true
|
||||
settled = true
|
||||
return
|
||||
}
|
||||
const key = `${dark}:${source}`
|
||||
const known = recall(key)
|
||||
if (known !== undefined) {
|
||||
revealMs = 0
|
||||
svg = known
|
||||
showSource = false
|
||||
settled = true
|
||||
return
|
||||
}
|
||||
@@ -51,36 +94,61 @@
|
||||
suppressErrorRendering: true
|
||||
})
|
||||
// mermaid.render needs a fresh element id per attempt to avoid id collisions.
|
||||
const result = await mermaid.render(`mermaid-${randomUUID()}`, source)
|
||||
let id = newDiagramId()
|
||||
let result = await mermaid.render(id, source)
|
||||
if (seq !== renderSeq) return
|
||||
svg = result.svg
|
||||
renderedCode = source
|
||||
// Mermaid sizes each node by measuring its label, so a font still in flight yields
|
||||
// boxes cut to fallback metrics that the real glyphs then overflow. Drawing the
|
||||
// diagram is itself what asks for the fonts its glyphs need — including the emoji
|
||||
// subsets — so let them settle and lay it out again against the true metrics.
|
||||
// subsets — so let them settle and lay it out again against the true metrics before
|
||||
// anything is shown.
|
||||
if (document.fonts?.status === 'loading') {
|
||||
await document.fonts.ready
|
||||
if (seq !== renderSeq) return
|
||||
const settled = await mermaid.render(`mermaid-${randomUUID()}`, source)
|
||||
id = newDiagramId()
|
||||
result = await mermaid.render(id, source)
|
||||
if (seq !== renderSeq) return
|
||||
svg = settled.svg
|
||||
}
|
||||
remember(key, id, result.svg)
|
||||
revealMs = showSource || forceLight || reducedMotion.val ? 0 : 200
|
||||
svg = result.svg
|
||||
showSource = false
|
||||
} catch {
|
||||
// Parse failure (often a partial block still streaming in): fall back to the
|
||||
// raw source. `showSvg` already hides any previous diagram since `renderedCode`
|
||||
// no longer matches the current `code`.
|
||||
// Parse failure (often a partial block still streaming in): fall back to the raw source.
|
||||
if (seq === renderSeq) {
|
||||
revealMs = 0
|
||||
showSource = true
|
||||
}
|
||||
} finally {
|
||||
if (seq === renderSeq) settled = true
|
||||
}
|
||||
}
|
||||
|
||||
let quietTimer: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
// Partial text alternates between parsing and not, so drawing each chunk of a stream flips
|
||||
// the block between text and diagram: a change landing soon after another stays text until
|
||||
// the source goes quiet. A lone change (an edit, a version switch) still draws at once.
|
||||
$effect(() => {
|
||||
void render(code, !forceLight && isDarkMode.val)
|
||||
const source = code
|
||||
const dark = !forceLight && isDarkMode.val
|
||||
const burst = quietTimer !== undefined
|
||||
clearTimeout(quietTimer)
|
||||
quietTimer = setTimeout(() => {
|
||||
quietTimer = undefined
|
||||
if (burst) void render(source, dark)
|
||||
}, STREAM_QUIET_MS)
|
||||
if (burst) {
|
||||
renderSeq++
|
||||
revealMs = 0
|
||||
showSource = true
|
||||
settled = false
|
||||
} else {
|
||||
void render(source, dark)
|
||||
}
|
||||
})
|
||||
|
||||
// Only show the diagram while it corresponds to the current source.
|
||||
let showSvg = $derived(svg !== undefined && renderedCode === code)
|
||||
onDestroy(() => clearTimeout(quietTimer))
|
||||
|
||||
function downloadSvg() {
|
||||
if (svg) {
|
||||
@@ -136,8 +204,16 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if showSvg}
|
||||
<div class="relative" data-mermaid-pending={!settled}>
|
||||
{#if showSource}
|
||||
<pre class="overflow-auto max-h-screen text-xs p-2" data-mermaid-pending={!settled}>{code}</pre>
|
||||
{:else if svg === undefined}
|
||||
<!-- Outgoing slide paired with the diagram's incoming one, so the block's height eases from
|
||||
this to the diagram's instead of jumping and shoving the document below it. -->
|
||||
<div class="p-2" data-mermaid-pending="true" out:slide={{ duration: revealMs }}>
|
||||
<Skeleton layout={[[10]]} />
|
||||
</div>
|
||||
{:else}
|
||||
<div class="relative" data-mermaid-pending={!settled} in:slide={{ duration: revealMs }}>
|
||||
<div class="absolute top-2 right-2 z-20 flex flex-row gap-1 print:hidden">
|
||||
<Button
|
||||
onclick={() => copyToClipboard(code)}
|
||||
@@ -218,7 +294,4 @@
|
||||
{/if}
|
||||
</div>
|
||||
</Modal>
|
||||
{:else}
|
||||
<!-- Fallback while loading or when rendering fails: show the raw source -->
|
||||
<pre class="overflow-auto max-h-screen text-xs p-2" data-mermaid-pending={!settled}>{code}</pre>
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user