| 1 | import { |
| 2 | ArrowPathRoundedSquareIcon, |
| 3 | MagnifyingGlassMinusIcon, |
| 4 | MagnifyingGlassPlusIcon, |
| 5 | } from "@heroicons/react/24/outline"; |
| 6 | // @ts-ignore |
| 7 | import Panzoom from "@panzoom/panzoom"; |
| 8 | // @ts-ignore |
| 9 | import mermaid from "mermaid"; |
| 10 | import { useEffect, useRef, useState } from "react"; |
| 11 | import { useDebouncedEffect } from "../find/useDebounce"; |
| 12 | import { ToolTip } from "../gui/Tooltip"; |
| 13 | |
| 14 | const MINIMUM_ZOOM_STEP = 0.05; |
| 15 | |
| 16 | const MERMAID_THEME_COLORS = { |
| 17 | background: "#1e1e1e", |
| 18 | primaryColor: "#4d8bf0", |
| 19 | primaryTextColor: "#ffffff", |
| 20 | primaryBorderColor: "#4d8bf0", |
| 21 | secondaryColor: "#3a6db3", |
| 22 | secondaryTextColor: "#ffffff", |
| 23 | secondaryBorderColor: "#3a6db3", |
| 24 | tertiaryColor: "#59bc89", |
| 25 | tertiaryTextColor: "#ffffff", |
| 26 | tertiaryBorderColor: "#59bc89", |
| 27 | noteBkgColor: "#2d2d2d", |
| 28 | noteTextColor: "#e6e6e6", |
| 29 | noteBorderColor: "#555555", |
| 30 | lineColor: "#8c8c8c", |
| 31 | textColor: "#e6e6e6", |
| 32 | mainBkg: "#252525", |
| 33 | errorBkgColor: "#f44336", |
| 34 | errorTextColor: "#ffffff", |
| 35 | nodeBorder: "#555555", |
| 36 | clusterBkg: "#2a2a2a", |
| 37 | clusterBorder: "#555555", |
| 38 | defaultLinkColor: "#8c8c8c", |
| 39 | titleColor: "#e6e6e6", |
| 40 | edgeLabelBackground: "#252525", |
| 41 | activeTaskBkgColor: "#4caf50", |
| 42 | activeTaskBorderColor: "#388e3c", |
| 43 | doneTaskBkgColor: "#388e3c", |
| 44 | doneTaskBorderColor: "#2e7d32", |
| 45 | critBkgColor: "#f44336", |
| 46 | critBorderColor: "#d32f2f", |
| 47 | taskTextColor: "#e6e6e6", |
| 48 | taskTextOutsideColor: "#e6e6e6", |
| 49 | taskTextLightColor: "#b3b3b3", |
| 50 | sectionBkgColor: "#2a2a2a", |
| 51 | altSectionBkgColor: "#303030", |
| 52 | sectionBkgColor2: "#252525", |
| 53 | excludeBkgColor: "#2d2d2d", |
| 54 | fillType0: "#264f78", |
| 55 | fillType1: "#3a6db3", |
| 56 | fillType2: "#59bc89", |
| 57 | fillType3: "#4d8bf0", |
| 58 | fillType4: "#3a6db3", |
| 59 | fillType5: "#264f78", |
| 60 | fillType6: "#59bc89", |
| 61 | fillType7: "#4d8bf0", |
| 62 | }; |
| 63 | |
| 64 | mermaid.initialize({ |
| 65 | startOnLoad: false, |
| 66 | securityLevel: "loose", |
| 67 | theme: "dark", |
| 68 | themeVariables: { |
| 69 | ...MERMAID_THEME_COLORS, |
| 70 | fontSize: "14px", |
| 71 | fontFamily: "var(--vscode-font-family)", |
| 72 | }, |
| 73 | }); |
| 74 | |
| 75 | export default function MermaidDiagram({ code }: { code: string }) { |
| 76 | const mermaidRenderContainerRef = useRef<HTMLDivElement>(null); |
| 77 | const zoomInButtonRef = useRef<SVGSVGElement>(null); |
| 78 | const zoomOutButtonRef = useRef<SVGSVGElement>(null); |
| 79 | const resetZoomButtonRef = useRef<SVGSVGElement>(null); |
| 80 | |
| 81 | const [isLoading, setIsLoading] = useState(false); |
| 82 | const [error, setError] = useState(""); |
| 83 | |
| 84 | useEffect(() => { |
| 85 | setIsLoading(true); |
| 86 | }, [code]); |
| 87 | |
| 88 | useDebouncedEffect( |
| 89 | () => { |
| 90 | if (mermaidRenderContainerRef.current) { |
| 91 | mermaidRenderContainerRef.current.innerHTML = ""; |
| 92 | } |
| 93 | const diagramId = `mermaid-id-${Math.random().toString(36).substring(2)}`; |
| 94 | void (async () => { |
| 95 | if (!mermaidRenderContainerRef.current) return; |
| 96 | try { |
| 97 | await mermaid.parse(code); |
| 98 | const renderedSVG = await mermaid.render(diagramId, code); |
| 99 | mermaidRenderContainerRef.current.innerHTML = renderedSVG.svg; |
| 100 | setError(""); |
| 101 | const panzoom = Panzoom(mermaidRenderContainerRef.current, { |
| 102 | step: MINIMUM_ZOOM_STEP, |
| 103 | }); |
| 104 | zoomInButtonRef.current?.addEventListener("click", () => |
| 105 | panzoom.zoomIn({ step: MINIMUM_ZOOM_STEP * 4 }), |
| 106 | ); |
| 107 | zoomOutButtonRef.current?.addEventListener("click", () => |
| 108 | panzoom.zoomOut({ step: MINIMUM_ZOOM_STEP * 4 }), |
| 109 | ); |
| 110 | resetZoomButtonRef.current?.addEventListener("click", () => |
| 111 | panzoom.reset(), |
| 112 | ); |
| 113 | mermaidRenderContainerRef.current.parentElement?.addEventListener( |
| 114 | "wheel", |
| 115 | (event) => { |
| 116 | if (!event.shiftKey) return; |
| 117 | panzoom.zoomWithWheel(event); |
| 118 | }, |
| 119 | ); |
| 120 | } catch (err: any) { |
| 121 | if (err.message) { |
| 122 | setError(err.message); |
| 123 | } else { |
| 124 | setError( |
| 125 | "Unknown error when parsing or rendering the Mermaid diagram.", |
| 126 | ); |
| 127 | } |
| 128 | } |
| 129 | setIsLoading(false); |
| 130 | })(); |
| 131 | }, |
| 132 | 500, |
| 133 | [code], |
| 134 | ); |
| 135 | |
| 136 | return ( |
| 137 | <> |
| 138 | {isLoading && ( |
| 139 | <div className="text-vsc-foreground text-xs">Generating diagram...</div> |
| 140 | )} |
| 141 | {!!error ? ( |
| 142 | <div className="text-error whitespace-pre text-sm">{error}</div> |
| 143 | ) : ( |
| 144 | <div className="mermaid relative"> |
| 145 | <div className="absolute right-0 z-10 m-2 flex items-center gap-x-1"> |
| 146 | <ToolTip content={"Zoom In"}> |
| 147 | <MagnifyingGlassPlusIcon |
| 148 | ref={zoomInButtonRef} |
| 149 | className="h-4 w-4 cursor-pointer" |
| 150 | /> |
| 151 | </ToolTip> |
| 152 | <ToolTip content={"Zoom Out"}> |
| 153 | <MagnifyingGlassMinusIcon |
| 154 | ref={zoomOutButtonRef} |
| 155 | className="h-4 w-4 cursor-pointer" |
| 156 | /> |
| 157 | </ToolTip> |
| 158 | |
| 159 | <ToolTip content="Reset Zoom"> |
| 160 | <ArrowPathRoundedSquareIcon |
| 161 | ref={resetZoomButtonRef} |
| 162 | className="h-4 w-4 cursor-pointer" |
| 163 | /> |
| 164 | </ToolTip> |
| 165 | </div> |
| 166 | <div |
| 167 | className="flex min-h-5 justify-center" |
| 168 | ref={mermaidRenderContainerRef} |
| 169 | /> |
| 170 | </div> |
| 171 | )} |
| 172 | </> |
| 173 | ); |
| 174 | } |