Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 5.3 KiBRawPermalink
1import {
2 ArrowPathRoundedSquareIcon,
3 MagnifyingGlassMinusIcon,
4 MagnifyingGlassPlusIcon,
5} from "@heroicons/react/24/outline";
6// @ts-ignore
7import Panzoom from "@panzoom/panzoom";
8// @ts-ignore
9import mermaid from "mermaid";
10import { useEffect, useId, useRef, useState } from "react";
11import { useDebouncedEffect } from "../find/useDebounce";
12import { ToolTip } from "../gui/Tooltip";
13
14const MINIMUM_ZOOM_STEP = 0.05;
15
16const 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
64mermaid.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
75export 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 zoomInButtonId = useId();
82 const zoomOutButtonId = useId();
83 const resetZoomButtonId = useId();
84
85 const [isLoading, setIsLoading] = useState(false);
86 const [error, setError] = useState("");
87
88 useEffect(() => {
89 setIsLoading(true);
90 }, [code]);
91
92 useDebouncedEffect(
93 () => {
94 if (mermaidRenderContainerRef.current) {
95 mermaidRenderContainerRef.current.innerHTML = "";
96 }
97 const diagramId = `mermaid-id-${Math.random().toString(36).substring(2)}`;
98 void (async () => {
99 if (!mermaidRenderContainerRef.current) return;
100 try {
101 await mermaid.parse(code);
102 const renderedSVG = await mermaid.render(diagramId, code);
103 mermaidRenderContainerRef.current.innerHTML = renderedSVG.svg;
104 setError("");
105 const panzoom = Panzoom(mermaidRenderContainerRef.current, {
106 step: MINIMUM_ZOOM_STEP,
107 });
108 zoomInButtonRef.current?.addEventListener("click", () =>
109 panzoom.zoomIn({ step: MINIMUM_ZOOM_STEP * 4 }),
110 );
111 zoomOutButtonRef.current?.addEventListener("click", () =>
112 panzoom.zoomOut({ step: MINIMUM_ZOOM_STEP * 4 }),
113 );
114 resetZoomButtonRef.current?.addEventListener("click", () =>
115 panzoom.reset(),
116 );
117 mermaidRenderContainerRef.current.parentElement?.addEventListener(
118 "wheel",
119 (event) => {
120 if (!event.shiftKey) return;
121 panzoom.zoomWithWheel(event);
122 },
123 );
124 } catch (err: any) {
125 if (err.message) {
126 setError(err.message);
127 } else {
128 setError(
129 "Unknown error when parsing or rendering the Mermaid diagram.",
130 );
131 }
132 }
133 setIsLoading(false);
134 })();
135 },
136 500,
137 [code],
138 );
139
140 return (
141 <>
142 {isLoading && (
143 <div className="text-vsc-foreground text-xs">Generating diagram...</div>
144 )}
145 {!!error ? (
146 <div className="text-error whitespace-pre text-sm">{error}</div>
147 ) : (
148 <div className="relative">
149 <div className="absolute right-0 z-10 m-2 flex items-center gap-x-1">
150 <MagnifyingGlassPlusIcon
151 data-tooltip-id={zoomInButtonId}
152 ref={zoomInButtonRef}
153 className="h-4 w-4 cursor-pointer"
154 />
155 <ToolTip id={zoomInButtonId}>Zoom In</ToolTip>
156 <MagnifyingGlassMinusIcon
157 data-tooltip-id={zoomOutButtonId}
158 ref={zoomOutButtonRef}
159 className="h-4 w-4 cursor-pointer"
160 />
161 <ToolTip id={zoomOutButtonId}>Zoom Out</ToolTip>
162 <ArrowPathRoundedSquareIcon
163 data-tooltip-id={resetZoomButtonId}
164 ref={resetZoomButtonRef}
165 className="h-4 w-4 cursor-pointer"
166 />
167 <ToolTip id={resetZoomButtonId}>Reset Zoom</ToolTip>
168 </div>
169 <div
170 className="flex min-h-5 justify-center"
171 ref={mermaidRenderContainerRef}
172 />
173 </div>
174 )}
175 </>
176 );
177}