@plannotator/ui 0.40.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HANDOFF.md +46 -5
- package/README.md +6 -2
- package/components/DiagramBlock.tsx +376 -0
- package/components/GraphvizBlock.tsx +22 -597
- package/components/MermaidBlock.tsx +17 -664
- package/components/Viewer.tsx +42 -2
- package/components/diagram/DiagramCanvas.tsx +431 -0
- package/components/diagram/DiagramComposer.tsx +135 -0
- package/components/diagram/DiagramOverlay.tsx +215 -0
- package/components/diagram/DiagramPopout.tsx +70 -0
- package/components/diagram/DiagramSourcePane.tsx +244 -0
- package/components/diagram/DiagramViewer.tsx +276 -0
- package/components/diagram/anchorClaims.ts +71 -0
- package/components/diagram/index.ts +36 -0
- package/components/diagram/useDiagramComments.ts +341 -0
- package/components/diagram/useDiagramRender.ts +91 -0
- package/components/diagram/useDiagramSourceDraft.ts +143 -0
- package/components/diagram/useDiagramViewport.ts +156 -0
- package/components/html-viewer/bridge-script.asset.js +25 -0
- package/components/html-viewer/bridge-script.lite.ts +1 -1
- package/components/html-viewer/bridge-script.ts +37 -0
- package/hooks/useAnnotationHighlighter.ts +5 -0
- package/package.json +5 -3
- package/styles.css +1 -1
- package/types.ts +5 -0
- package/utils/diagram-anchor-graphviz.ts +143 -0
- package/utils/diagram-anchor.ts +401 -0
- package/utils/diagram-projection.ts +66 -0
- package/utils/diagram-render.ts +668 -0
- package/utils/graphviz.ts +93 -0
- package/utils/parser.ts +15 -1
- package/components/mermaidSvg.ts +0 -33
|
@@ -1,674 +1,27 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
4
|
-
import type { Block } from '../types';
|
|
5
|
-
import { normalizeMermaidSvgMarkup } from './mermaidSvg';
|
|
6
|
-
import {
|
|
7
|
-
MERMAID_CONFIG,
|
|
8
|
-
getMermaidRetryDelayMs,
|
|
9
|
-
loadMermaidRuntime,
|
|
10
|
-
__setMermaidRuntimeLoaderForTests,
|
|
11
|
-
} from '../utils/mermaid';
|
|
12
|
-
import { loadMathRenderer } from '../utils/math';
|
|
13
|
-
import { hasMermaidMath } from '../utils/mermaid-math-slot';
|
|
14
|
-
import { applyMermaidTheme, mermaidThemeKey } from '../utils/mermaidTheme';
|
|
15
|
-
import { createRuntimeRetryEpoch } from '../utils/runtimeRetry';
|
|
16
|
-
import { useTheme } from './ThemeProvider';
|
|
17
|
-
|
|
18
|
-
/** One Retry re-attempts every block whose runtime import failed (see utils/runtimeRetry). */
|
|
19
|
-
const mermaidRetryEpoch = createRuntimeRetryEpoch();
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { MERMAID_CONFIG, __setMermaidRuntimeLoaderForTests } from '../utils/mermaid';
|
|
3
|
+
import { DiagramBlock, type DiagramBlockProps } from './DiagramBlock';
|
|
20
4
|
|
|
21
5
|
// Re-exported: the config pin test and the lazy-retry test import them from here.
|
|
22
6
|
export { MERMAID_CONFIG, __setMermaidRuntimeLoaderForTests };
|
|
23
7
|
|
|
24
8
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* failure, never as a placeholder.
|
|
31
|
-
*/
|
|
32
|
-
const getMermaid = loadMermaidRuntime;
|
|
33
|
-
|
|
34
|
-
const wait = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
35
|
-
|
|
36
|
-
interface ViewBox {
|
|
37
|
-
x: number;
|
|
38
|
-
y: number;
|
|
39
|
-
width: number;
|
|
40
|
-
height: number;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const ZOOM_STEP = 0.25;
|
|
44
|
-
const MIN_ZOOM = 0.25;
|
|
45
|
-
const MAX_ZOOM = 8;
|
|
46
|
-
|
|
47
|
-
function parseViewBox(svgEl: SVGSVGElement): ViewBox | null {
|
|
48
|
-
const raw = svgEl.getAttribute('viewBox');
|
|
49
|
-
if (!raw) return null;
|
|
50
|
-
|
|
51
|
-
const values = raw
|
|
52
|
-
.trim()
|
|
53
|
-
.split(/[\s,]+/)
|
|
54
|
-
.map((value) => Number.parseFloat(value));
|
|
55
|
-
|
|
56
|
-
if (values.length !== 4 || values.some((value) => Number.isNaN(value))) {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const [x, y, width, height] = values;
|
|
61
|
-
if (width <= 0 || height <= 0) return null;
|
|
62
|
-
return { x, y, width, height };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Parse base viewBox from Mermaid SVG markup before DOM mount
|
|
66
|
-
function parseViewBoxFromMarkup(markup: string): ViewBox | null {
|
|
67
|
-
const viewBoxMatch = markup.match(/viewBox\s*=\s*"([^"]+)"/i);
|
|
68
|
-
if (viewBoxMatch?.[1]) {
|
|
69
|
-
const values = viewBoxMatch[1]
|
|
70
|
-
.trim()
|
|
71
|
-
.split(/[\s,]+/)
|
|
72
|
-
.map((value) => Number.parseFloat(value));
|
|
73
|
-
|
|
74
|
-
if (values.length === 4 && values.every((value) => Number.isFinite(value))) {
|
|
75
|
-
const [x, y, width, height] = values;
|
|
76
|
-
if (width > 0 && height > 0) {
|
|
77
|
-
return { x, y, width, height };
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const widthMatch = markup.match(/\bwidth\s*=\s*"([0-9.]+)(?:px)?"/i);
|
|
83
|
-
const heightMatch = markup.match(/\bheight\s*=\s*"([0-9.]+)(?:px)?"/i);
|
|
84
|
-
const width = widthMatch?.[1] ? Number.parseFloat(widthMatch[1]) : NaN;
|
|
85
|
-
const height = heightMatch?.[1] ? Number.parseFloat(heightMatch[1]) : NaN;
|
|
86
|
-
if (Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0) {
|
|
87
|
-
return { x: 0, y: 0, width, height };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Apply calculated viewBox from zoom and pan state
|
|
94
|
-
function applyView(svgEl: SVGSVGElement, base: ViewBox, zoom: number, pan: { x: number; y: number }): void {
|
|
95
|
-
const zoomedWidth = base.width / zoom;
|
|
96
|
-
const zoomedHeight = base.height / zoom;
|
|
97
|
-
const centerX = base.x + base.width / 2;
|
|
98
|
-
const centerY = base.y + base.height / 2;
|
|
99
|
-
const vbX = centerX - zoomedWidth / 2 + pan.x;
|
|
100
|
-
const vbY = centerY - zoomedHeight / 2 + pan.y;
|
|
101
|
-
svgEl.setAttribute('viewBox', `${vbX} ${vbY} ${zoomedWidth} ${zoomedHeight}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Compute a fitted base viewBox for the current container ratio
|
|
105
|
-
function fitBoundsToContainer(bounds: ViewBox, containerRect: DOMRect): ViewBox {
|
|
106
|
-
const containerWidth = Math.max(containerRect.width, 1);
|
|
107
|
-
const containerHeight = Math.max(containerRect.height, 1);
|
|
108
|
-
const contentRatio = bounds.width / bounds.height;
|
|
109
|
-
const containerRatio = containerWidth / containerHeight;
|
|
110
|
-
|
|
111
|
-
if (contentRatio > containerRatio) {
|
|
112
|
-
const targetHeight = bounds.width / containerRatio;
|
|
113
|
-
const extra = (targetHeight - bounds.height) / 2;
|
|
114
|
-
return {
|
|
115
|
-
x: bounds.x,
|
|
116
|
-
y: bounds.y - extra,
|
|
117
|
-
width: bounds.width,
|
|
118
|
-
height: targetHeight,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const targetWidth = bounds.height * containerRatio;
|
|
123
|
-
const extra = (targetWidth - bounds.width) / 2;
|
|
124
|
-
return {
|
|
125
|
-
x: bounds.x - extra,
|
|
126
|
-
y: bounds.y,
|
|
127
|
-
width: targetWidth,
|
|
128
|
-
height: bounds.height,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Renders a mermaid diagram block with zoom controls.
|
|
9
|
+
* A mermaid fence. The engine, the canvas, the comment overlay and the
|
|
10
|
+
* popout are `DiagramBlock`'s (one code path with `GraphvizBlock`); this
|
|
11
|
+
* file keeps the fence's name and the test hooks. The runtime comes from the
|
|
12
|
+
* slot in `utils/mermaid`: loaded lazily on the first diagram, or already
|
|
13
|
+
* filled by a host that imported `utils/mermaid-eager`.
|
|
134
14
|
*/
|
|
135
|
-
const MermaidBlockImpl: React.FC<{ block: Block }> = ({ block }) => {
|
|
136
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
137
|
-
const expandedOverlayRef = useRef<HTMLDivElement>(null);
|
|
138
|
-
const [svg, setSvg] = useState('');
|
|
139
|
-
const [error, setError] = useState<string | null>(null);
|
|
140
|
-
// True when the failure was the runtime import itself (a chunking host's
|
|
141
|
-
// fetch), which is the only failure a Retry can change; a diagram syntax
|
|
142
|
-
// error keeps the panel exactly as it always was.
|
|
143
|
-
const [runtimeUnavailable, setRuntimeUnavailable] = useState(false);
|
|
144
|
-
const [retryToken, setRetryToken] = useState(0);
|
|
145
|
-
const [showSource, setShowSource] = useState(false);
|
|
146
|
-
const [isExpanded, setIsExpanded] = useState(false);
|
|
147
|
-
// The (palette, mode) the diagram must follow: the same resolution the
|
|
148
|
-
// code fences use (see useFenceTheme). Outside a ThemeProvider the default
|
|
149
|
-
// context yields the Plannotator dark pair, and with no theme tokens on the
|
|
150
|
-
// document `applyMermaidTheme` keeps the static config, so a host without
|
|
151
|
-
// the provider renders exactly as before. A key change re-runs the render
|
|
152
|
-
// effect below, which is what re-themes an already rendered diagram.
|
|
153
|
-
const { colorTheme, resolvedMode } = useTheme();
|
|
154
|
-
const themeKey = mermaidThemeKey(colorTheme, resolvedMode === 'light' ? 'light' : 'dark');
|
|
155
|
-
// A sibling's Retry re-attempts this block too, but only while its own
|
|
156
|
-
// failure was the shared runtime import; a healthy block or a diagram
|
|
157
|
-
// syntax error is left alone.
|
|
158
|
-
const runtimeUnavailableRef = useRef(runtimeUnavailable);
|
|
159
|
-
runtimeUnavailableRef.current = runtimeUnavailable;
|
|
160
|
-
useEffect(() => mermaidRetryEpoch.subscribe(() => {
|
|
161
|
-
if (!runtimeUnavailableRef.current) return;
|
|
162
|
-
setError(null);
|
|
163
|
-
setRetryToken((token) => token + 1);
|
|
164
|
-
}), []);
|
|
165
|
-
|
|
166
|
-
// All zoom/pan state as refs to avoid re-renders
|
|
167
|
-
const zoomLevelRef = useRef(1);
|
|
168
|
-
const isDraggingRef = useRef(false);
|
|
169
|
-
const naturalBoundsRef = useRef<ViewBox | null>(null);
|
|
170
|
-
const baseViewBoxRef = useRef<ViewBox | null>(null);
|
|
171
|
-
const panOffsetRef = useRef({ x: 0, y: 0 });
|
|
172
|
-
const dragStartRef = useRef({ x: 0, y: 0 });
|
|
173
|
-
const panStartRef = useRef({ x: 0, y: 0 });
|
|
174
|
-
|
|
175
|
-
// UI refs for zoom controls
|
|
176
|
-
const zoomInBtnRef = useRef<HTMLButtonElement>(null);
|
|
177
|
-
const zoomOutBtnRef = useRef<HTMLButtonElement>(null);
|
|
178
|
-
const zoomDisplayRef = useRef<HTMLSpanElement>(null);
|
|
179
|
-
|
|
180
|
-
// Update zoom level, viewBox, and UI without React re-render
|
|
181
|
-
const updateZoom = useCallback((newZoom: number) => {
|
|
182
|
-
zoomLevelRef.current = newZoom;
|
|
183
|
-
|
|
184
|
-
if (containerRef.current && baseViewBoxRef.current) {
|
|
185
|
-
const svgEl = containerRef.current.querySelector('svg');
|
|
186
|
-
if (svgEl instanceof SVGSVGElement) {
|
|
187
|
-
applyView(svgEl, baseViewBoxRef.current, newZoom, panOffsetRef.current);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (zoomInBtnRef.current) zoomInBtnRef.current.disabled = newZoom >= MAX_ZOOM;
|
|
192
|
-
if (zoomOutBtnRef.current) zoomOutBtnRef.current.disabled = newZoom <= MIN_ZOOM;
|
|
193
|
-
if (zoomDisplayRef.current) {
|
|
194
|
-
const show = Math.abs(newZoom - 1) > 0.001;
|
|
195
|
-
zoomDisplayRef.current.textContent = show ? `${Math.round(newZoom * 100)}%` : '';
|
|
196
|
-
zoomDisplayRef.current.hidden = !show;
|
|
197
|
-
}
|
|
198
|
-
}, []);
|
|
199
|
-
|
|
200
|
-
const fitToCurrentViewport = useCallback(() => {
|
|
201
|
-
if (!containerRef.current || !naturalBoundsRef.current) return;
|
|
202
|
-
|
|
203
|
-
const svgEl = containerRef.current.querySelector('svg');
|
|
204
|
-
if (!(svgEl instanceof SVGSVGElement)) return;
|
|
205
|
-
|
|
206
|
-
const fitted = fitBoundsToContainer(naturalBoundsRef.current, containerRef.current.getBoundingClientRect());
|
|
207
|
-
baseViewBoxRef.current = fitted;
|
|
208
|
-
panOffsetRef.current = { x: 0, y: 0 };
|
|
209
|
-
updateZoom(1);
|
|
210
|
-
applyView(svgEl, fitted, 1, { x: 0, y: 0 });
|
|
211
|
-
}, [updateZoom]);
|
|
212
|
-
|
|
213
|
-
useEffect(() => {
|
|
214
|
-
let cancelled = false;
|
|
215
|
-
|
|
216
|
-
// Render mermaid diagram
|
|
217
|
-
const renderDiagram = async () => {
|
|
218
|
-
let mermaid: Mermaid;
|
|
219
|
-
try {
|
|
220
|
-
try {
|
|
221
|
-
mermaid = await getMermaid();
|
|
222
|
-
} catch {
|
|
223
|
-
// Transient chunk failure on a chunking host: one automatic
|
|
224
|
-
// re-attempt with a fresh import() after a short delay. In a
|
|
225
|
-
// single-file build the first await never rejects, so this branch
|
|
226
|
-
// is unreachable there and the success path is unchanged.
|
|
227
|
-
await wait(getMermaidRetryDelayMs());
|
|
228
|
-
if (cancelled) return;
|
|
229
|
-
mermaid = await getMermaid();
|
|
230
|
-
}
|
|
231
|
-
} catch (err) {
|
|
232
|
-
if (!cancelled) {
|
|
233
|
-
setError(err instanceof Error ? err.message : 'Failed to render diagram');
|
|
234
|
-
setRuntimeUnavailable(true);
|
|
235
|
-
setSvg('');
|
|
236
|
-
}
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
try {
|
|
240
|
-
// A `$$` label makes Mermaid render KaTeX. On a host that redirects
|
|
241
|
-
// Mermaid's `katex` import to `utils/mermaid-math-slot` the label is
|
|
242
|
-
// typeset through the math slot, which must be filled by then: warm
|
|
243
|
-
// it with the registered loader first. A filled slot (Plannotator's
|
|
244
|
-
// eager entry) resolves at once; a load failure is left to the
|
|
245
|
-
// render, whose error panel names it with the source.
|
|
246
|
-
if (hasMermaidMath(block.content)) {
|
|
247
|
-
try {
|
|
248
|
-
await loadMathRenderer();
|
|
249
|
-
} catch {
|
|
250
|
-
// Reported by the render below.
|
|
251
|
-
}
|
|
252
|
-
if (cancelled) return;
|
|
253
|
-
}
|
|
254
|
-
// Global initialize, once per (palette, mode) change, before render.
|
|
255
|
-
applyMermaidTheme(mermaid, themeKey);
|
|
256
|
-
const id = `mermaid-${block.id}`;
|
|
257
|
-
const { svg: renderedSvg } = await mermaid.render(id, block.content);
|
|
258
|
-
if (!cancelled) {
|
|
259
|
-
const normalizedSvg = normalizeMermaidSvgMarkup(renderedSvg);
|
|
260
|
-
naturalBoundsRef.current = parseViewBoxFromMarkup(normalizedSvg);
|
|
261
|
-
setSvg(normalizedSvg);
|
|
262
|
-
setError(null);
|
|
263
|
-
setRuntimeUnavailable(false);
|
|
264
|
-
}
|
|
265
|
-
} catch (err) {
|
|
266
|
-
if (!cancelled) {
|
|
267
|
-
setError(err instanceof Error ? err.message : 'Failed to render diagram');
|
|
268
|
-
setRuntimeUnavailable(false);
|
|
269
|
-
setSvg('');
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
renderDiagram();
|
|
275
|
-
|
|
276
|
-
return () => {
|
|
277
|
-
cancelled = true;
|
|
278
|
-
};
|
|
279
|
-
}, [block.content, block.id, retryToken, themeKey]);
|
|
280
|
-
|
|
281
|
-
// Reset zoom and pan when content changes
|
|
282
|
-
useEffect(() => {
|
|
283
|
-
zoomLevelRef.current = 1;
|
|
284
|
-
naturalBoundsRef.current = null;
|
|
285
|
-
baseViewBoxRef.current = null;
|
|
286
|
-
panOffsetRef.current = { x: 0, y: 0 };
|
|
287
|
-
setIsExpanded(false);
|
|
288
|
-
}, [block.content]);
|
|
289
|
-
|
|
290
|
-
// Reset zoom and pan when switching from source back to diagram
|
|
291
|
-
useEffect(() => {
|
|
292
|
-
if (showSource) {
|
|
293
|
-
setIsExpanded(false);
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
zoomLevelRef.current = 1;
|
|
298
|
-
panOffsetRef.current = { x: 0, y: 0 };
|
|
299
|
-
baseViewBoxRef.current = null;
|
|
300
|
-
}, [showSource]);
|
|
301
|
-
|
|
302
|
-
useEffect(() => {
|
|
303
|
-
if (!isExpanded) return undefined;
|
|
304
|
-
|
|
305
|
-
const previousOverflow = document.body.style.overflow;
|
|
306
|
-
document.body.style.overflow = 'hidden';
|
|
307
|
-
|
|
308
|
-
const handleKeyDown = (event: KeyboardEvent) => {
|
|
309
|
-
if (event.key === 'Escape') {
|
|
310
|
-
setIsExpanded(false);
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
window.addEventListener('keydown', handleKeyDown);
|
|
315
|
-
|
|
316
|
-
return () => {
|
|
317
|
-
document.body.style.overflow = previousOverflow;
|
|
318
|
-
window.removeEventListener('keydown', handleKeyDown);
|
|
319
|
-
};
|
|
320
|
-
}, [isExpanded]);
|
|
321
|
-
|
|
322
|
-
// Compute base viewBox from rendered SVG and apply initial view
|
|
323
|
-
useEffect(() => {
|
|
324
|
-
if (!svg || showSource || !containerRef.current) return;
|
|
325
|
-
|
|
326
|
-
const svgEl = containerRef.current.querySelector('svg');
|
|
327
|
-
if (!(svgEl instanceof SVGSVGElement)) return;
|
|
328
|
-
|
|
329
|
-
svgEl.style.maxWidth = 'none';
|
|
330
|
-
svgEl.style.width = '100%';
|
|
331
|
-
svgEl.style.height = '100%';
|
|
332
|
-
svgEl.style.display = 'block';
|
|
333
|
-
svgEl.style.filter = 'none';
|
|
334
|
-
svgEl.style.willChange = 'auto';
|
|
335
|
-
svgEl.setAttribute('width', '100%');
|
|
336
|
-
svgEl.setAttribute('height', '100%');
|
|
337
|
-
svgEl.setAttribute('preserveAspectRatio', 'xMidYMid meet');
|
|
338
|
-
|
|
339
|
-
let cancelled = false;
|
|
340
|
-
|
|
341
|
-
const applyInitialView = () => {
|
|
342
|
-
if (cancelled) return;
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
const base = naturalBoundsRef.current ?? parseViewBox(svgEl);
|
|
346
|
-
|
|
347
|
-
if (!base) return;
|
|
348
|
-
|
|
349
|
-
naturalBoundsRef.current = base;
|
|
350
|
-
fitToCurrentViewport();
|
|
351
|
-
} catch {
|
|
352
|
-
setError('Failed to measure diagram bounds');
|
|
353
|
-
setSvg('');
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
const raf = requestAnimationFrame(() => requestAnimationFrame(applyInitialView));
|
|
358
|
-
const timer = window.setTimeout(applyInitialView, 120);
|
|
359
|
-
|
|
360
|
-
return () => {
|
|
361
|
-
cancelled = true;
|
|
362
|
-
cancelAnimationFrame(raf);
|
|
363
|
-
window.clearTimeout(timer);
|
|
364
|
-
};
|
|
365
|
-
}, [fitToCurrentViewport, isExpanded, showSource, svg]);
|
|
366
|
-
|
|
367
|
-
const applyWheelZoomDelta = useCallback((deltaY: number) => {
|
|
368
|
-
if (Math.abs(deltaY) < 0.1) {
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
const delta = deltaY > 0 ? -ZOOM_STEP : ZOOM_STEP;
|
|
373
|
-
const newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, zoomLevelRef.current + delta));
|
|
374
|
-
updateZoom(newZoom);
|
|
375
|
-
}, [updateZoom]);
|
|
376
|
-
|
|
377
|
-
useEffect(() => {
|
|
378
|
-
if (showSource || !containerRef.current) return;
|
|
379
|
-
|
|
380
|
-
const container = containerRef.current;
|
|
381
|
-
const handleWheel = (event: WheelEvent) => {
|
|
382
|
-
if (Math.abs(event.deltaY) < 0.1) return;
|
|
383
|
-
event.preventDefault();
|
|
384
|
-
applyWheelZoomDelta(event.deltaY);
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
container.addEventListener('wheel', handleWheel, { passive: false });
|
|
388
|
-
|
|
389
|
-
return () => {
|
|
390
|
-
container.removeEventListener('wheel', handleWheel);
|
|
391
|
-
};
|
|
392
|
-
}, [applyWheelZoomDelta, showSource]);
|
|
393
|
-
|
|
394
|
-
useEffect(() => {
|
|
395
|
-
if (showSource || !isExpanded) return;
|
|
396
|
-
|
|
397
|
-
const handleExpandedPinchWheel = (event: WheelEvent) => {
|
|
398
|
-
if (!event.ctrlKey && !event.metaKey) return;
|
|
399
|
-
if (!expandedOverlayRef.current) return;
|
|
400
|
-
|
|
401
|
-
const eventTarget = event.target;
|
|
402
|
-
if (!(eventTarget instanceof Node) || !expandedOverlayRef.current.contains(eventTarget)) return;
|
|
403
|
-
|
|
404
|
-
event.preventDefault();
|
|
405
|
-
event.stopPropagation();
|
|
406
|
-
applyWheelZoomDelta(event.deltaY);
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
window.addEventListener('wheel', handleExpandedPinchWheel, { passive: false, capture: true });
|
|
410
|
-
|
|
411
|
-
return () => {
|
|
412
|
-
window.removeEventListener('wheel', handleExpandedPinchWheel, { capture: true });
|
|
413
|
-
};
|
|
414
|
-
}, [applyWheelZoomDelta, isExpanded, showSource]);
|
|
415
|
-
|
|
416
|
-
const handleZoomIn = useCallback(() => {
|
|
417
|
-
updateZoom(Math.min(zoomLevelRef.current + ZOOM_STEP, MAX_ZOOM));
|
|
418
|
-
}, [updateZoom]);
|
|
419
|
-
|
|
420
|
-
const handleZoomOut = useCallback(() => {
|
|
421
|
-
updateZoom(Math.max(zoomLevelRef.current - ZOOM_STEP, MIN_ZOOM));
|
|
422
|
-
}, [updateZoom]);
|
|
423
|
-
|
|
424
|
-
const handleFitToScreen = useCallback(() => {
|
|
425
|
-
fitToCurrentViewport();
|
|
426
|
-
}, [fitToCurrentViewport]);
|
|
427
|
-
|
|
428
|
-
useEffect(() => {
|
|
429
|
-
if (showSource || !containerRef.current || !naturalBoundsRef.current) return;
|
|
430
|
-
if (typeof ResizeObserver === 'undefined') return;
|
|
431
|
-
|
|
432
|
-
const observer = new ResizeObserver(() => {
|
|
433
|
-
if (Math.abs(zoomLevelRef.current - 1) > 0.001) return;
|
|
434
|
-
fitToCurrentViewport();
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
observer.observe(containerRef.current);
|
|
438
|
-
return () => observer.disconnect();
|
|
439
|
-
}, [fitToCurrentViewport, isExpanded, showSource, svg]);
|
|
440
|
-
|
|
441
|
-
// Drag-to-pan handlers (all ref-based to avoid re-renders)
|
|
442
|
-
const handleMouseDown = useCallback((event: React.MouseEvent) => {
|
|
443
|
-
if (event.button !== 0) return;
|
|
444
|
-
event.preventDefault();
|
|
445
|
-
isDraggingRef.current = true;
|
|
446
|
-
dragStartRef.current = { x: event.clientX, y: event.clientY };
|
|
447
|
-
panStartRef.current = { ...panOffsetRef.current };
|
|
448
|
-
if (containerRef.current) containerRef.current.style.cursor = 'grabbing';
|
|
449
|
-
}, []);
|
|
450
|
-
|
|
451
|
-
const handleMouseMove = useCallback((event: React.MouseEvent) => {
|
|
452
|
-
if (!isDraggingRef.current || !containerRef.current || !baseViewBoxRef.current) return;
|
|
453
|
-
|
|
454
|
-
const svgEl = containerRef.current.querySelector('svg');
|
|
455
|
-
if (!(svgEl instanceof SVGSVGElement)) return;
|
|
456
|
-
|
|
457
|
-
const rect = svgEl.getBoundingClientRect();
|
|
458
|
-
const base = baseViewBoxRef.current;
|
|
459
|
-
const zoom = zoomLevelRef.current;
|
|
460
|
-
const scaleX = (base.width / zoom) / rect.width;
|
|
461
|
-
const scaleY = (base.height / zoom) / rect.height;
|
|
462
|
-
|
|
463
|
-
const dx = event.clientX - dragStartRef.current.x;
|
|
464
|
-
const dy = event.clientY - dragStartRef.current.y;
|
|
465
|
-
|
|
466
|
-
panOffsetRef.current = {
|
|
467
|
-
x: panStartRef.current.x - dx * scaleX,
|
|
468
|
-
y: panStartRef.current.y - dy * scaleY,
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
applyView(svgEl, base, zoom, panOffsetRef.current);
|
|
472
|
-
}, []);
|
|
473
|
-
|
|
474
|
-
const stopDragging = useCallback(() => {
|
|
475
|
-
if (!isDraggingRef.current) return;
|
|
476
|
-
isDraggingRef.current = false;
|
|
477
|
-
if (containerRef.current) containerRef.current.style.cursor = 'grab';
|
|
478
|
-
}, []);
|
|
479
|
-
|
|
480
|
-
if (error) {
|
|
481
|
-
return (
|
|
482
|
-
<div className="my-5 rounded-lg border border-destructive/30 bg-destructive/5 overflow-hidden">
|
|
483
|
-
<div className="px-3 py-2 bg-destructive/10 border-b border-destructive/20 flex items-center gap-2">
|
|
484
|
-
<svg className="w-4 h-4 text-destructive" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
485
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
|
|
486
|
-
</svg>
|
|
487
|
-
<span className="text-xs text-destructive font-medium">Mermaid Error</span>
|
|
488
|
-
{runtimeUnavailable && (
|
|
489
|
-
<button
|
|
490
|
-
type="button"
|
|
491
|
-
onClick={() => mermaidRetryEpoch.bump()}
|
|
492
|
-
className="ml-auto rounded-md border border-destructive/30 px-2 py-0.5 text-xs text-destructive hover:bg-destructive/10"
|
|
493
|
-
title="Retry loading the diagram renderer"
|
|
494
|
-
>
|
|
495
|
-
Retry
|
|
496
|
-
</button>
|
|
497
|
-
)}
|
|
498
|
-
</div>
|
|
499
|
-
<pre className="p-3 text-xs text-destructive/80 overflow-x-auto">{error}</pre>
|
|
500
|
-
<pre className="p-3 text-xs text-muted-foreground bg-muted/30 border-t border-border/30 overflow-x-auto">
|
|
501
|
-
<code>{block.content}</code>
|
|
502
|
-
</pre>
|
|
503
|
-
</div>
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
const controls = (
|
|
508
|
-
/* Controls container */
|
|
509
|
-
<div className={`absolute top-2 right-2 flex flex-col gap-1 items-center z-10 ${isExpanded ? 'opacity-100' : 'opacity-0 group-hover:opacity-100 transition-opacity'}`}>
|
|
510
|
-
{/* Toggle source/diagram button */}
|
|
511
|
-
<button
|
|
512
|
-
onClick={() => setShowSource(!showSource)}
|
|
513
|
-
className="p-1.5 rounded-md bg-muted/85 hover:bg-muted text-muted-foreground hover:text-foreground"
|
|
514
|
-
title={showSource ? 'Show diagram' : 'Show source'}
|
|
515
|
-
>
|
|
516
|
-
{showSource ? (
|
|
517
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
518
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
|
|
519
|
-
</svg>
|
|
520
|
-
) : (
|
|
521
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
522
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
|
|
523
|
-
</svg>
|
|
524
|
-
)}
|
|
525
|
-
</button>
|
|
526
|
-
|
|
527
|
-
{!showSource && svg && (
|
|
528
|
-
<>
|
|
529
|
-
{/* Diagram interaction controls */}
|
|
530
|
-
<div className="flex w-10 flex-col items-center gap-0.5 bg-muted/85 rounded-md p-0.5">
|
|
531
|
-
{/* Expand/exit expanded button */}
|
|
532
|
-
<button
|
|
533
|
-
onClick={() => setIsExpanded(!isExpanded)}
|
|
534
|
-
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground"
|
|
535
|
-
title={isExpanded ? 'Exit expanded view' : 'Expand diagram'}
|
|
536
|
-
aria-label={isExpanded ? 'Exit expanded view' : 'Expand diagram'}
|
|
537
|
-
>
|
|
538
|
-
{isExpanded ? (
|
|
539
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
540
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M6 10h4V6M18 10h-4V6M6 14h4v4M18 14h-4v4" />
|
|
541
|
-
</svg>
|
|
542
|
-
) : (
|
|
543
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
544
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5" />
|
|
545
|
-
</svg>
|
|
546
|
-
)}
|
|
547
|
-
</button>
|
|
548
|
-
|
|
549
|
-
{/* Zoom in button */}
|
|
550
|
-
<button
|
|
551
|
-
ref={zoomInBtnRef}
|
|
552
|
-
onClick={handleZoomIn}
|
|
553
|
-
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground disabled:opacity-30 disabled:cursor-not-allowed"
|
|
554
|
-
title="Zoom in"
|
|
555
|
-
aria-label="Zoom in"
|
|
556
|
-
>
|
|
557
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
558
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M12 5v14M5 12h14" />
|
|
559
|
-
</svg>
|
|
560
|
-
</button>
|
|
561
|
-
|
|
562
|
-
{/* Fit to view button */}
|
|
563
|
-
<button
|
|
564
|
-
onClick={handleFitToScreen}
|
|
565
|
-
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground"
|
|
566
|
-
title="Fit to view"
|
|
567
|
-
aria-label="Fit to view"
|
|
568
|
-
>
|
|
569
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
570
|
-
<circle cx="12" cy="12" r="4" strokeLinecap="round" strokeLinejoin="round" />
|
|
571
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M12 2v4M12 18v4M2 12h4M18 12h4" />
|
|
572
|
-
</svg>
|
|
573
|
-
</button>
|
|
574
|
-
|
|
575
|
-
{/* Zoom out button */}
|
|
576
|
-
<button
|
|
577
|
-
ref={zoomOutBtnRef}
|
|
578
|
-
onClick={handleZoomOut}
|
|
579
|
-
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground disabled:opacity-30 disabled:cursor-not-allowed"
|
|
580
|
-
title="Zoom out"
|
|
581
|
-
aria-label="Zoom out"
|
|
582
|
-
>
|
|
583
|
-
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
|
584
|
-
<path strokeLinecap="round" strokeLinejoin="round" d="M5 12h14" />
|
|
585
|
-
</svg>
|
|
586
|
-
</button>
|
|
587
|
-
</div>
|
|
588
|
-
|
|
589
|
-
{/* Zoom percentage badge */}
|
|
590
|
-
<span
|
|
591
|
-
ref={zoomDisplayRef}
|
|
592
|
-
hidden
|
|
593
|
-
className="min-w-10 rounded bg-muted/85 px-1 py-0.5 text-[10px] text-center text-muted-foreground tabular-nums leading-tight"
|
|
594
|
-
/>
|
|
595
|
-
</>
|
|
596
|
-
)}
|
|
597
|
-
</div>
|
|
598
|
-
);
|
|
599
|
-
|
|
600
|
-
const inlineSource = (
|
|
601
|
-
<pre className="rounded-lg text-[13px] overflow-x-auto bg-muted/50 border border-border/30 p-4">
|
|
602
|
-
<code className="pn-code font-mono language-mermaid">{block.content}</code>
|
|
603
|
-
</pre>
|
|
604
|
-
);
|
|
605
|
-
|
|
606
|
-
// First render still in flight (the runtime import on the lazy path, then
|
|
607
|
-
// the render itself): the source stays readable under a quiet status line.
|
|
608
|
-
// A re-render for a theme change keeps the previous SVG, so this shows only
|
|
609
|
-
// before the first diagram lands.
|
|
610
|
-
const pendingSource = (
|
|
611
|
-
<>
|
|
612
|
-
<div
|
|
613
|
-
role="status"
|
|
614
|
-
aria-live="polite"
|
|
615
|
-
data-mermaid-pending=""
|
|
616
|
-
className="mb-1.5 flex items-center gap-1.5 text-xs text-muted-foreground"
|
|
617
|
-
>
|
|
618
|
-
<span className="inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-muted-foreground/70" aria-hidden="true" />
|
|
619
|
-
Rendering diagram…
|
|
620
|
-
</div>
|
|
621
|
-
{inlineSource}
|
|
622
|
-
</>
|
|
623
|
-
);
|
|
624
|
-
|
|
625
|
-
const diagramBody = (
|
|
626
|
-
<div
|
|
627
|
-
ref={containerRef}
|
|
628
|
-
data-pinpoint-ignore=""
|
|
629
|
-
className={`rounded-xl bg-muted/30 border border-border/30 overflow-hidden select-none cursor-grab ${isExpanded ? 'h-full min-h-0' : 'h-[min(65vh,36rem)] min-h-[20rem]'}`}
|
|
630
|
-
dangerouslySetInnerHTML={{ __html: svg }}
|
|
631
|
-
onMouseDown={handleMouseDown}
|
|
632
|
-
onMouseMove={handleMouseMove}
|
|
633
|
-
onMouseUp={stopDragging}
|
|
634
|
-
onMouseLeave={stopDragging}
|
|
635
|
-
/>
|
|
636
|
-
);
|
|
637
|
-
|
|
638
|
-
return (
|
|
639
|
-
<>
|
|
640
|
-
<div className="my-5 group relative" data-block-id={block.id}>
|
|
641
|
-
{!isExpanded && controls}
|
|
642
|
-
{showSource ? inlineSource : !svg ? pendingSource : !isExpanded ? diagramBody : <div className="rounded-xl border border-border/30 bg-muted/10 h-[min(65vh,36rem)] min-h-[20rem]" />}
|
|
643
|
-
</div>
|
|
644
|
-
|
|
645
|
-
{!showSource && svg && isExpanded && typeof document !== 'undefined' && createPortal(
|
|
646
|
-
<div ref={expandedOverlayRef} className="fixed inset-0 z-[9999] bg-background/90 backdrop-blur-sm p-4 md:p-6">
|
|
647
|
-
<div className="mx-auto flex h-full max-w-[min(96vw,110rem)] flex-col gap-3">
|
|
648
|
-
<div className="flex items-center justify-between gap-4 text-xs text-muted-foreground">
|
|
649
|
-
<span className="truncate">Mermaid diagram</span>
|
|
650
|
-
<button
|
|
651
|
-
onClick={() => setIsExpanded(false)}
|
|
652
|
-
className="rounded-md border border-border/60 bg-card/70 px-2.5 py-1.5 text-foreground hover:bg-card"
|
|
653
|
-
>
|
|
654
|
-
Close
|
|
655
|
-
</button>
|
|
656
|
-
</div>
|
|
657
|
-
<div className="group relative flex-1 min-h-0">
|
|
658
|
-
{controls}
|
|
659
|
-
{diagramBody}
|
|
660
|
-
</div>
|
|
661
|
-
</div>
|
|
662
|
-
</div>,
|
|
663
|
-
document.body
|
|
664
|
-
)}
|
|
665
|
-
</>
|
|
666
|
-
);
|
|
667
|
-
};
|
|
668
|
-
|
|
669
15
|
export const MermaidBlock = React.memo(
|
|
670
|
-
|
|
16
|
+
(props: DiagramBlockProps) => <DiagramBlock kind="mermaid" {...props} />,
|
|
671
17
|
(prev, next) =>
|
|
672
18
|
prev.block.id === next.block.id &&
|
|
673
|
-
prev.block.content === next.block.content
|
|
19
|
+
prev.block.content === next.block.content &&
|
|
20
|
+
prev.block.startLine === next.block.startLine &&
|
|
21
|
+
prev.annotations === next.annotations &&
|
|
22
|
+
prev.selectedAnnotationId === next.selectedAnnotationId &&
|
|
23
|
+
prev.onSelectAnnotation === next.onSelectAnnotation &&
|
|
24
|
+
prev.onAddAnnotation === next.onAddAnnotation &&
|
|
25
|
+
prev.readOnly === next.readOnly &&
|
|
26
|
+
prev.onRestoreReport === next.onRestoreReport,
|
|
674
27
|
);
|