import React, { useRef, useState, useEffect, useCallback } from 'react'; import { createPortal } from 'react-dom'; import mermaid from 'mermaid'; import type { Block } from '../types'; mermaid.initialize({ startOnLoad: false, securityLevel: 'strict', theme: 'dark', themeVariables: { primaryColor: '#3b82f6', primaryTextColor: '#f8fafc', primaryBorderColor: '#475569', lineColor: '#64748b', secondaryColor: '#1e293b', tertiaryColor: '#0f172a', background: '#1e293b', mainBkg: '#1e293b', nodeBorder: '#475569', clusterBkg: '#1e293b', clusterBorder: '#475569', titleColor: '#f8fafc', edgeLabelBackground: '#1e293b', }, flowchart: { htmlLabels: true, curve: 'basis', }, }); interface ViewBox { x: number; y: number; width: number; height: number; } const ZOOM_STEP = 0.25; const MIN_ZOOM = 0.25; const MAX_ZOOM = 8; function parseViewBox(svgEl: SVGSVGElement): ViewBox | null { const raw = svgEl.getAttribute('viewBox'); if (!raw) return null; const values = raw .trim() .split(/[\s,]+/) .map((value) => Number.parseFloat(value)); if (values.length !== 4 || values.some((value) => Number.isNaN(value))) { return null; } const [x, y, width, height] = values; if (width <= 0 || height <= 0) return null; return { x, y, width, height }; } // Parse base viewBox from Mermaid SVG markup before DOM mount function parseViewBoxFromMarkup(markup: string): ViewBox | null { const viewBoxMatch = markup.match(/viewBox\s*=\s*"([^"]+)"/i); if (viewBoxMatch?.[1]) { const values = viewBoxMatch[1] .trim() .split(/[\s,]+/) .map((value) => Number.parseFloat(value)); if (values.length === 4 && values.every((value) => Number.isFinite(value))) { const [x, y, width, height] = values; if (width > 0 && height > 0) { return { x, y, width, height }; } } } const widthMatch = markup.match(/\bwidth\s*=\s*"([0-9.]+)(?:px)?"/i); const heightMatch = markup.match(/\bheight\s*=\s*"([0-9.]+)(?:px)?"/i); const width = widthMatch?.[1] ? Number.parseFloat(widthMatch[1]) : NaN; const height = heightMatch?.[1] ? Number.parseFloat(heightMatch[1]) : NaN; if (Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0) { return { x: 0, y: 0, width, height }; } return null; } // Apply calculated viewBox from zoom and pan state function applyView(svgEl: SVGSVGElement, base: ViewBox, zoom: number, pan: { x: number; y: number }): void { const zoomedWidth = base.width / zoom; const zoomedHeight = base.height / zoom; const centerX = base.x + base.width / 2; const centerY = base.y + base.height / 2; const vbX = centerX - zoomedWidth / 2 + pan.x; const vbY = centerY - zoomedHeight / 2 + pan.y; svgEl.setAttribute('viewBox', `${vbX} ${vbY} ${zoomedWidth} ${zoomedHeight}`); } // Compute a fitted base viewBox for the current container ratio function fitBoundsToContainer(bounds: ViewBox, containerRect: DOMRect): ViewBox { const containerWidth = Math.max(containerRect.width, 1); const containerHeight = Math.max(containerRect.height, 1); const contentRatio = bounds.width / bounds.height; const containerRatio = containerWidth / containerHeight; if (contentRatio > containerRatio) { const targetHeight = bounds.width / containerRatio; const extra = (targetHeight - bounds.height) / 2; return { x: bounds.x, y: bounds.y - extra, width: bounds.width, height: targetHeight, }; } const targetWidth = bounds.height * containerRatio; const extra = (targetWidth - bounds.width) / 2; return { x: bounds.x - extra, y: bounds.y, width: targetWidth, height: bounds.height, }; } /** * Renders a mermaid diagram block with zoom controls. */ export const MermaidBlock: React.FC<{ block: Block }> = ({ block }) => { const containerRef = useRef(null); const expandedOverlayRef = useRef(null); const [svg, setSvg] = useState(''); const [error, setError] = useState(null); const [showSource, setShowSource] = useState(false); const [isExpanded, setIsExpanded] = useState(false); // All zoom/pan state as refs to avoid re-renders const zoomLevelRef = useRef(1); const isDraggingRef = useRef(false); const naturalBoundsRef = useRef(null); const baseViewBoxRef = useRef(null); const panOffsetRef = useRef({ x: 0, y: 0 }); const dragStartRef = useRef({ x: 0, y: 0 }); const panStartRef = useRef({ x: 0, y: 0 }); // UI refs for zoom controls const zoomInBtnRef = useRef(null); const zoomOutBtnRef = useRef(null); const zoomDisplayRef = useRef(null); // Update zoom level, viewBox, and UI without React re-render const updateZoom = useCallback((newZoom: number) => { zoomLevelRef.current = newZoom; if (containerRef.current && baseViewBoxRef.current) { const svgEl = containerRef.current.querySelector('svg'); if (svgEl instanceof SVGSVGElement) { applyView(svgEl, baseViewBoxRef.current, newZoom, panOffsetRef.current); } } if (zoomInBtnRef.current) zoomInBtnRef.current.disabled = newZoom >= MAX_ZOOM; if (zoomOutBtnRef.current) zoomOutBtnRef.current.disabled = newZoom <= MIN_ZOOM; if (zoomDisplayRef.current) { const show = Math.abs(newZoom - 1) > 0.001; zoomDisplayRef.current.textContent = show ? `${Math.round(newZoom * 100)}%` : ''; zoomDisplayRef.current.hidden = !show; } }, []); const fitToCurrentViewport = useCallback(() => { if (!containerRef.current || !naturalBoundsRef.current) return; const svgEl = containerRef.current.querySelector('svg'); if (!(svgEl instanceof SVGSVGElement)) return; const fitted = fitBoundsToContainer(naturalBoundsRef.current, containerRef.current.getBoundingClientRect()); baseViewBoxRef.current = fitted; panOffsetRef.current = { x: 0, y: 0 }; updateZoom(1); applyView(svgEl, fitted, 1, { x: 0, y: 0 }); }, [updateZoom]); useEffect(() => { let cancelled = false; // Render mermaid diagram const renderDiagram = async () => { try { const id = `mermaid-${block.id}`; const { svg: renderedSvg } = await mermaid.render(id, block.content); if (!cancelled) { naturalBoundsRef.current = parseViewBoxFromMarkup(renderedSvg); setSvg(renderedSvg); setError(null); } } catch (err) { if (!cancelled) { setError(err instanceof Error ? err.message : 'Failed to render diagram'); setSvg(''); } } }; renderDiagram(); return () => { cancelled = true; }; }, [block.content, block.id]); // Reset zoom and pan when content changes useEffect(() => { zoomLevelRef.current = 1; naturalBoundsRef.current = null; baseViewBoxRef.current = null; panOffsetRef.current = { x: 0, y: 0 }; setIsExpanded(false); }, [block.content]); // Reset zoom and pan when switching from source back to diagram useEffect(() => { if (showSource) { setIsExpanded(false); return; } zoomLevelRef.current = 1; panOffsetRef.current = { x: 0, y: 0 }; baseViewBoxRef.current = null; }, [showSource]); useEffect(() => { if (!isExpanded) return undefined; const previousOverflow = document.body.style.overflow; document.body.style.overflow = 'hidden'; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { setIsExpanded(false); } }; window.addEventListener('keydown', handleKeyDown); return () => { document.body.style.overflow = previousOverflow; window.removeEventListener('keydown', handleKeyDown); }; }, [isExpanded]); // Compute base viewBox from rendered SVG and apply initial view useEffect(() => { if (!svg || showSource || !containerRef.current) return; const svgEl = containerRef.current.querySelector('svg'); if (!(svgEl instanceof SVGSVGElement)) return; svgEl.style.maxWidth = 'none'; svgEl.style.width = '100%'; svgEl.style.height = '100%'; svgEl.style.display = 'block'; svgEl.style.filter = 'none'; svgEl.style.willChange = 'auto'; svgEl.setAttribute('width', '100%'); svgEl.setAttribute('height', '100%'); svgEl.setAttribute('preserveAspectRatio', 'xMidYMid meet'); let cancelled = false; const applyInitialView = () => { if (cancelled) return; try { const base = naturalBoundsRef.current ?? parseViewBox(svgEl); if (!base) return; naturalBoundsRef.current = base; fitToCurrentViewport(); } catch { setError('Failed to measure diagram bounds'); setSvg(''); } }; const raf = requestAnimationFrame(() => requestAnimationFrame(applyInitialView)); const timer = window.setTimeout(applyInitialView, 120); return () => { cancelled = true; cancelAnimationFrame(raf); window.clearTimeout(timer); }; }, [fitToCurrentViewport, isExpanded, showSource, svg]); const applyWheelZoomDelta = useCallback((deltaY: number) => { if (Math.abs(deltaY) < 0.1) { return; } const delta = deltaY > 0 ? -ZOOM_STEP : ZOOM_STEP; const newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, zoomLevelRef.current + delta)); updateZoom(newZoom); }, [updateZoom]); useEffect(() => { if (showSource || !containerRef.current) return; const container = containerRef.current; const handleWheel = (event: WheelEvent) => { if (Math.abs(event.deltaY) < 0.1) return; event.preventDefault(); applyWheelZoomDelta(event.deltaY); }; container.addEventListener('wheel', handleWheel, { passive: false }); return () => { container.removeEventListener('wheel', handleWheel); }; }, [applyWheelZoomDelta, showSource]); useEffect(() => { if (showSource || !isExpanded) return; const handleExpandedPinchWheel = (event: WheelEvent) => { if (!event.ctrlKey && !event.metaKey) return; if (!expandedOverlayRef.current) return; const eventTarget = event.target; if (!(eventTarget instanceof Node) || !expandedOverlayRef.current.contains(eventTarget)) return; event.preventDefault(); event.stopPropagation(); applyWheelZoomDelta(event.deltaY); }; window.addEventListener('wheel', handleExpandedPinchWheel, { passive: false, capture: true }); return () => { window.removeEventListener('wheel', handleExpandedPinchWheel, { capture: true }); }; }, [applyWheelZoomDelta, isExpanded, showSource]); const handleZoomIn = useCallback(() => { updateZoom(Math.min(zoomLevelRef.current + ZOOM_STEP, MAX_ZOOM)); }, [updateZoom]); const handleZoomOut = useCallback(() => { updateZoom(Math.max(zoomLevelRef.current - ZOOM_STEP, MIN_ZOOM)); }, [updateZoom]); const handleFitToScreen = useCallback(() => { fitToCurrentViewport(); }, [fitToCurrentViewport]); useEffect(() => { if (showSource || !containerRef.current || !naturalBoundsRef.current) return; if (typeof ResizeObserver === 'undefined') return; const observer = new ResizeObserver(() => { if (Math.abs(zoomLevelRef.current - 1) > 0.001) return; fitToCurrentViewport(); }); observer.observe(containerRef.current); return () => observer.disconnect(); }, [fitToCurrentViewport, isExpanded, showSource, svg]); // Drag-to-pan handlers (all ref-based to avoid re-renders) const handleMouseDown = useCallback((event: React.MouseEvent) => { if (event.button !== 0) return; event.preventDefault(); isDraggingRef.current = true; dragStartRef.current = { x: event.clientX, y: event.clientY }; panStartRef.current = { ...panOffsetRef.current }; if (containerRef.current) containerRef.current.style.cursor = 'grabbing'; }, []); const handleMouseMove = useCallback((event: React.MouseEvent) => { if (!isDraggingRef.current || !containerRef.current || !baseViewBoxRef.current) return; const svgEl = containerRef.current.querySelector('svg'); if (!(svgEl instanceof SVGSVGElement)) return; const rect = svgEl.getBoundingClientRect(); const base = baseViewBoxRef.current; const zoom = zoomLevelRef.current; const scaleX = (base.width / zoom) / rect.width; const scaleY = (base.height / zoom) / rect.height; const dx = event.clientX - dragStartRef.current.x; const dy = event.clientY - dragStartRef.current.y; panOffsetRef.current = { x: panStartRef.current.x - dx * scaleX, y: panStartRef.current.y - dy * scaleY, }; applyView(svgEl, base, zoom, panOffsetRef.current); }, []); const stopDragging = useCallback(() => { if (!isDraggingRef.current) return; isDraggingRef.current = false; if (containerRef.current) containerRef.current.style.cursor = 'grab'; }, []); if (error) { return (
Mermaid Error
{error}
          {block.content}
        
); } const controls = ( /* Controls container */
{/* Toggle source/diagram button */} {!showSource && svg && ( <> {/* Diagram interaction controls */}
{/* Expand/exit expanded button */} {/* Zoom in button */} {/* Fit to view button */} {/* Zoom out button */}
{/* Zoom percentage badge */}
); const inlineSource = (
      {block.content}
    
); const diagramBody = (
); return ( <>
{!isExpanded && controls} {showSource || !svg ? inlineSource : !isExpanded ? diagramBody :
}
{!showSource && svg && isExpanded && typeof document !== 'undefined' && createPortal(
Mermaid diagram
{controls} {diagramBody}
, document.body )} ); };