Elffuss BraidHub

kikocis/continue public

Merge pull request #6455 from uinstinct/mermaid-diagrams-2

feat: mermaid diagrams
Changesetf96a5adcf583ca052628dc66b970cdbe3c4ae38615b7e8ea6a2e7c9ca1a787f5
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-07-09 10:10 -07:00
CommitterGitHub <noreply@github.com> · 2025-07-09 10:10 -07:00
Parentsc71f1eb00d9f, 5e92b78eb714

Browse the files at this changeset

6 files changed

modified gui/package-lock.json +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

modified gui/package.json +2 −0
@@ -19,6 +19,7 @@
1919 "@continuedev/config-yaml": "file:../packages/config-yaml",
2020 "@headlessui/react": "^2.2.0",
2121 "@heroicons/react": "^2.0.18",
22+ "@panzoom/panzoom": "^4.6.0",
2223 "@reduxjs/toolkit": "^2.3.0",
2324 "@tiptap/core": "^2.3.2",
2425 "@tiptap/extension-document": "^2.3.2",
@@ -43,6 +44,7 @@
4344 "handlebars": "^4.7.8",
4445 "lodash": "^4.17.21",
4546 "lowlight": "^3.3.0",
47+ "mermaid": "^11.6.0",
4648 "minisearch": "^7.0.2",
4749 "mustache": "^4.2.0",
4850 "posthog-js": "^1.130.1",
added gui/src/components/StyledMarkdownPreview/MermaidBlock.tsx +175 −0
@@ -0,0 +1,175 @@
1+import {
2+ ArrowPathRoundedSquareIcon,
3+ MagnifyingGlassMinusIcon,
4+ MagnifyingGlassPlusIcon,
5+} from "@heroicons/react/24/outline";
6+import Panzoom from "@panzoom/panzoom";
7+import mermaid from "mermaid";
8+import { useEffect, useId, useRef, useState } from "react";
9+import { useDebouncedEffect } from "../find/useDebounce";
10+import { ToolTip } from "../gui/Tooltip";
11+
12+const MINIMUM_ZOOM_STEP = 0.05;
13+
14+const MERMAID_THEME_COLORS = {
15+ background: "#1e1e1e",
16+ primaryColor: "#4d8bf0",
17+ primaryTextColor: "#ffffff",
18+ primaryBorderColor: "#4d8bf0",
19+ secondaryColor: "#3a6db3",
20+ secondaryTextColor: "#ffffff",
21+ secondaryBorderColor: "#3a6db3",
22+ tertiaryColor: "#59bc89",
23+ tertiaryTextColor: "#ffffff",
24+ tertiaryBorderColor: "#59bc89",
25+ noteBkgColor: "#2d2d2d",
26+ noteTextColor: "#e6e6e6",
27+ noteBorderColor: "#555555",
28+ lineColor: "#8c8c8c",
29+ textColor: "#e6e6e6",
30+ mainBkg: "#252525",
31+ errorBkgColor: "#f44336",
32+ errorTextColor: "#ffffff",
33+ nodeBorder: "#555555",
34+ clusterBkg: "#2a2a2a",
35+ clusterBorder: "#555555",
36+ defaultLinkColor: "#8c8c8c",
37+ titleColor: "#e6e6e6",
38+ edgeLabelBackground: "#252525",
39+ activeTaskBkgColor: "#4caf50",
40+ activeTaskBorderColor: "#388e3c",
41+ doneTaskBkgColor: "#388e3c",
42+ doneTaskBorderColor: "#2e7d32",
43+ critBkgColor: "#f44336",
44+ critBorderColor: "#d32f2f",
45+ taskTextColor: "#e6e6e6",
46+ taskTextOutsideColor: "#e6e6e6",
47+ taskTextLightColor: "#b3b3b3",
48+ sectionBkgColor: "#2a2a2a",
49+ altSectionBkgColor: "#303030",
50+ sectionBkgColor2: "#252525",
51+ excludeBkgColor: "#2d2d2d",
52+ fillType0: "#264f78",
53+ fillType1: "#3a6db3",
54+ fillType2: "#59bc89",
55+ fillType3: "#4d8bf0",
56+ fillType4: "#3a6db3",
57+ fillType5: "#264f78",
58+ fillType6: "#59bc89",
59+ fillType7: "#4d8bf0",
60+};
61+
62+mermaid.initialize({
63+ startOnLoad: false,
64+ securityLevel: "loose",
65+ theme: "dark",
66+ themeVariables: {
67+ ...MERMAID_THEME_COLORS,
68+ fontSize: "14px",
69+ fontFamily: "var(--vscode-font-family)",
70+ },
71+});
72+
73+export default function MermaidDiagram({ code }: { code: string }) {
74+ const mermaidRenderContainerRef = useRef<HTMLDivElement>(null);
75+ const zoomInButtonRef = useRef<SVGSVGElement>(null);
76+ const zoomOutButtonRef = useRef<SVGSVGElement>(null);
77+ const resetZoomButtonRef = useRef<SVGSVGElement>(null);
78+
79+ const zoomInButtonId = useId();
80+ const zoomOutButtonId = useId();
81+ const resetZoomButtonId = useId();
82+
83+ const [isLoading, setIsLoading] = useState(false);
84+ const [error, setError] = useState("");
85+
86+ useEffect(() => {
87+ setIsLoading(true);
88+ }, [code]);
89+
90+ useDebouncedEffect(
91+ () => {
92+ if (mermaidRenderContainerRef.current) {
93+ mermaidRenderContainerRef.current.innerHTML = "";
94+ }
95+ const diagramId = `mermaid-id-${Math.random().toString(36).substring(2)}`;
96+ void (async () => {
97+ if (!mermaidRenderContainerRef.current) return;
98+ try {
99+ await mermaid.parse(code);
100+ const renderedSVG = await mermaid.render(diagramId, code);
101+ mermaidRenderContainerRef.current.innerHTML = renderedSVG.svg;
102+ setError("");
103+ const panzoom = Panzoom(mermaidRenderContainerRef.current, {
104+ step: MINIMUM_ZOOM_STEP,
105+ });
106+ zoomInButtonRef.current?.addEventListener("click", () =>
107+ panzoom.zoomIn({ step: MINIMUM_ZOOM_STEP * 4 }),
108+ );
109+ zoomOutButtonRef.current?.addEventListener("click", () =>
110+ panzoom.zoomOut({ step: MINIMUM_ZOOM_STEP * 4 }),
111+ );
112+ resetZoomButtonRef.current?.addEventListener("click", () =>
113+ panzoom.reset(),
114+ );
115+ mermaidRenderContainerRef.current.parentElement?.addEventListener(
116+ "wheel",
117+ (event) => {
118+ if (!event.shiftKey) return;
119+ panzoom.zoomWithWheel(event);
120+ },
121+ );
122+ } catch (err: any) {
123+ if (err.message) {
124+ setError(err.message);
125+ } else {
126+ setError(
127+ "Unknown error when parsing or rendering the Mermaid diagram.",
128+ );
129+ }
130+ }
131+ setIsLoading(false);
132+ })();
133+ },
134+ 500,
135+ [code],
136+ );
137+
138+ return (
139+ <>
140+ {isLoading && (
141+ <div className="text-vsc-foreground text-xs">Generating diagram...</div>
142+ )}
143+ {!!error ? (
144+ <div className="text-error whitespace-pre text-sm">{error}</div>
145+ ) : (
146+ <div className="relative">
147+ <div className="absolute right-0 z-10 m-2 flex items-center gap-x-1">
148+ <MagnifyingGlassPlusIcon
149+ data-tooltip-id={zoomInButtonId}
150+ ref={zoomInButtonRef}
151+ className="h-4 w-4 cursor-pointer"
152+ />
153+ <ToolTip id={zoomInButtonId}>Zoom In</ToolTip>
154+ <MagnifyingGlassMinusIcon
155+ data-tooltip-id={zoomOutButtonId}
156+ ref={zoomOutButtonRef}
157+ className="h-4 w-4 cursor-pointer"
158+ />
159+ <ToolTip id={zoomOutButtonId}>Zoom Out</ToolTip>
160+ <ArrowPathRoundedSquareIcon
161+ data-tooltip-id={resetZoomButtonId}
162+ ref={resetZoomButtonRef}
163+ className="h-4 w-4 cursor-pointer"
164+ />
165+ <ToolTip id={resetZoomButtonId}>Reset Zoom</ToolTip>
166+ </div>
167+ <div
168+ className="flex min-h-5 justify-center"
169+ ref={mermaidRenderContainerRef}
170+ />
171+ </div>
172+ )}
173+ </>
174+ );
175+}
modified gui/src/components/StyledMarkdownPreview/index.tsx +5 −0
@@ -21,6 +21,7 @@
2121 import FilenameLink from "./FilenameLink";
2222 import "./katex.css";
2323 import "./markdown.css";
24+import MermaidBlock from "./MermaidBlock";
2425 import { rehypeHighlightPlugin } from "./rehypeHighlightPlugin";
2526 import { StepContainerPreToolbar } from "./StepContainerPreToolbar";
2627 import SymbolLink from "./SymbolLink";
@@ -333,6 +334,10 @@
333334 }
334335 }
335336 }
337+ if (codeProps.className?.includes("language-mermaid")) {
338+ const codeText = String(codeProps.children || "");
339+ return <MermaidBlock code={codeText} />;
340+ }
336341 return <code {...codeProps}>{codeProps.children}</code>;
337342 },
338343 },
modified gui/src/components/find/FindWidget.tsx +1 −1
@@ -18,7 +18,7 @@
1818 SearchMatch,
1919 searchWithinContainer,
2020 } from "./findWidgetSearch";
21-import useDebounceValue from "./useDebounce";
21+import { useDebounceValue } from "./useDebounce";
2222 import { useElementSize } from "./useElementSize";
2323
2424 interface HighlightOverlayProps {
modified gui/src/components/find/useDebounce.ts +31 −3
@@ -1,6 +1,6 @@
1-import { useEffect, useState } from "react";
1+import { useEffect, useRef, useState } from "react";
22
3-function useDebounceValue<T>(value: T, delay: number = 500): T {
3+export function useDebounceValue<T>(value: T, delay: number = 500): T {
44 const [debouncedValue, setDebouncedValue] = useState<T>(value);
55
66 useEffect(() => {
@@ -16,4 +16,32 @@
1616 return debouncedValue;
1717 }
1818
19-export default useDebounceValue;
19+export function useDebouncedEffect(
20+ effect: () => void,
21+ delay: number,
22+ deps: unknown[],
23+) {
24+ const timeoutRef = useRef<NodeJS.Timeout | null>(null);
25+ const callBackRef = useRef(() => {});
26+
27+ useEffect(() => {
28+ // do not want the timeout to reset on effect dep change
29+ callBackRef.current = effect;
30+ }, [effect]);
31+
32+ useEffect(() => {
33+ if (timeoutRef.current) {
34+ clearTimeout(timeoutRef.current);
35+ }
36+
37+ timeoutRef.current = setTimeout(() => {
38+ callBackRef.current();
39+ }, delay);
40+
41+ return () => {
42+ if (timeoutRef.current) {
43+ clearTimeout(timeoutRef.current);
44+ }
45+ };
46+ }, [delay, ...deps]);
47+}