rendering mermaid diagrams
basic mermaid js implementation add panzoom and change color theme variables
This commit is contained in:
parent
0938308c2d
commit
b090f04b70
5 changed files with 217 additions and 4 deletions
|
|
@ -19,6 +19,7 @@
|
|||
"@continuedev/config-yaml": "file:../packages/config-yaml",
|
||||
"@headlessui/react": "^2.2.0",
|
||||
"@heroicons/react": "^2.0.18",
|
||||
"@panzoom/panzoom": "^4.6.0",
|
||||
"@reduxjs/toolkit": "^2.3.0",
|
||||
"@tiptap/core": "^2.3.2",
|
||||
"@tiptap/extension-document": "^2.3.2",
|
||||
|
|
@ -43,6 +44,7 @@
|
|||
"handlebars": "^4.7.8",
|
||||
"lodash": "^4.17.21",
|
||||
"lowlight": "^3.3.0",
|
||||
"mermaid": "^11.6.0",
|
||||
"minisearch": "^7.0.2",
|
||||
"mustache": "^4.2.0",
|
||||
"posthog-js": "^1.130.1",
|
||||
|
|
|
|||
178
gui/src/components/StyledMarkdownPreview/MermaidBlock.tsx
Normal file
178
gui/src/components/StyledMarkdownPreview/MermaidBlock.tsx
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
import {
|
||||
ArrowPathRoundedSquareIcon,
|
||||
MagnifyingGlassMinusIcon,
|
||||
MagnifyingGlassPlusIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import Panzoom from "@panzoom/panzoom";
|
||||
import mermaid from "mermaid";
|
||||
import { useEffect, useId, useRef, useState } from "react";
|
||||
import { useDebouncedEffect } from "../find/useDebounce";
|
||||
import { ToolTip } from "../gui/Tooltip";
|
||||
|
||||
const MINIMUM_ZOOM_STEP = 0.05;
|
||||
|
||||
const MERMAID_THEME_COLORS = {
|
||||
background: "#1e1e1e",
|
||||
primaryColor: "#4d8bf0",
|
||||
primaryTextColor: "#ffffff",
|
||||
primaryBorderColor: "#4d8bf0",
|
||||
secondaryColor: "#3a6db3",
|
||||
secondaryTextColor: "#ffffff",
|
||||
secondaryBorderColor: "#3a6db3",
|
||||
tertiaryColor: "#59bc89",
|
||||
tertiaryTextColor: "#ffffff",
|
||||
tertiaryBorderColor: "#59bc89",
|
||||
noteBkgColor: "#2d2d2d",
|
||||
noteTextColor: "#e6e6e6",
|
||||
noteBorderColor: "#555555",
|
||||
lineColor: "#8c8c8c",
|
||||
textColor: "#e6e6e6",
|
||||
mainBkg: "#252525",
|
||||
errorBkgColor: "#f44336",
|
||||
errorTextColor: "#ffffff",
|
||||
nodeBorder: "#555555",
|
||||
clusterBkg: "#2a2a2a",
|
||||
clusterBorder: "#555555",
|
||||
defaultLinkColor: "#8c8c8c",
|
||||
titleColor: "#e6e6e6",
|
||||
edgeLabelBackground: "#252525",
|
||||
activeTaskBkgColor: "#4caf50",
|
||||
activeTaskBorderColor: "#388e3c",
|
||||
doneTaskBkgColor: "#388e3c",
|
||||
doneTaskBorderColor: "#2e7d32",
|
||||
critBkgColor: "#f44336",
|
||||
critBorderColor: "#d32f2f",
|
||||
taskTextColor: "#e6e6e6",
|
||||
taskTextOutsideColor: "#e6e6e6",
|
||||
taskTextLightColor: "#b3b3b3",
|
||||
sectionBkgColor: "#2a2a2a",
|
||||
altSectionBkgColor: "#303030",
|
||||
sectionBkgColor2: "#252525",
|
||||
excludeBkgColor: "#2d2d2d",
|
||||
fillType0: "#264f78",
|
||||
fillType1: "#3a6db3",
|
||||
fillType2: "#59bc89",
|
||||
fillType3: "#4d8bf0",
|
||||
fillType4: "#3a6db3",
|
||||
fillType5: "#264f78",
|
||||
fillType6: "#59bc89",
|
||||
fillType7: "#4d8bf0",
|
||||
};
|
||||
|
||||
mermaid.initialize({
|
||||
startOnLoad: false,
|
||||
securityLevel: "loose",
|
||||
theme: "dark",
|
||||
themeVariables: {
|
||||
...MERMAID_THEME_COLORS,
|
||||
fontSize: "14px",
|
||||
fontFamily: "var(--vscode-font-family)",
|
||||
},
|
||||
});
|
||||
|
||||
export default function MermaidDiagram({ code }: { code: string }) {
|
||||
const mermaidRenderContainerRef = useRef<HTMLDivElement>(null);
|
||||
const zoomInButtonRef = useRef<SVGSVGElement>(null);
|
||||
const zoomOutButtonRef = useRef<SVGSVGElement>(null);
|
||||
const resetZoomButtonRef = useRef<SVGSVGElement>(null);
|
||||
|
||||
const zoomInButtonId = useId();
|
||||
const zoomOutButtonId = useId();
|
||||
const resetZoomButtonId = useId();
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
setIsLoading(true);
|
||||
}, [code]);
|
||||
|
||||
useDebouncedEffect(
|
||||
() => {
|
||||
if (mermaidRenderContainerRef.current) {
|
||||
mermaidRenderContainerRef.current.innerHTML = "";
|
||||
}
|
||||
const diagramId = `mermaid-id-${Math.random().toString(36).substring(2)}`;
|
||||
mermaid
|
||||
.parse(code)
|
||||
.then(() => mermaid.render(diagramId, code))
|
||||
.then(({ svg }) => {
|
||||
if (mermaidRenderContainerRef.current) {
|
||||
mermaidRenderContainerRef.current.innerHTML = svg;
|
||||
setError("");
|
||||
const panzoom = Panzoom(mermaidRenderContainerRef.current, {
|
||||
step: MINIMUM_ZOOM_STEP,
|
||||
});
|
||||
zoomInButtonRef.current?.addEventListener("click", () =>
|
||||
panzoom.zoomIn({ step: MINIMUM_ZOOM_STEP * 4 }),
|
||||
);
|
||||
zoomOutButtonRef.current?.addEventListener("click", () =>
|
||||
panzoom.zoomOut({ step: MINIMUM_ZOOM_STEP * 4 }),
|
||||
);
|
||||
resetZoomButtonRef.current?.addEventListener("click", () =>
|
||||
panzoom.reset(),
|
||||
);
|
||||
mermaidRenderContainerRef.current.parentElement?.addEventListener(
|
||||
"wheel",
|
||||
(event) => {
|
||||
if (!event.shiftKey) return;
|
||||
panzoom.zoomWithWheel(event);
|
||||
},
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err.message) {
|
||||
setError(err.message);
|
||||
} else {
|
||||
setError(
|
||||
"Unknown error when parsing or rendering the Mermaid diagram.",
|
||||
);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
setIsLoading(false);
|
||||
});
|
||||
},
|
||||
500,
|
||||
[code],
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
{isLoading && (
|
||||
<div className="text-vsc-foreground text-sm">Generating diagram...</div>
|
||||
)}
|
||||
{!!error ? (
|
||||
<div className="text-error whitespace-pre text-sm">{error}</div>
|
||||
) : (
|
||||
<div className="relative">
|
||||
<div className="absolute right-0 z-10 m-2 flex items-center gap-x-1">
|
||||
<MagnifyingGlassPlusIcon
|
||||
data-tooltip-id={zoomInButtonId}
|
||||
ref={zoomInButtonRef}
|
||||
className="h-4 w-4 cursor-pointer"
|
||||
/>
|
||||
<ToolTip id={zoomInButtonId}>Zoom In</ToolTip>
|
||||
<MagnifyingGlassMinusIcon
|
||||
data-tooltip-id={zoomOutButtonId}
|
||||
ref={zoomOutButtonRef}
|
||||
className="h-4 w-4 cursor-pointer"
|
||||
/>
|
||||
<ToolTip id={zoomOutButtonId}>Zoom Out</ToolTip>
|
||||
<ArrowPathRoundedSquareIcon
|
||||
data-tooltip-id={resetZoomButtonId}
|
||||
ref={resetZoomButtonRef}
|
||||
className="h-4 w-4 cursor-pointer"
|
||||
/>
|
||||
<ToolTip id={resetZoomButtonId}>Reset Zoom</ToolTip>
|
||||
</div>
|
||||
<div
|
||||
className="flex min-h-5 justify-center"
|
||||
ref={mermaidRenderContainerRef}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ import { ToolTip } from "../gui/Tooltip";
|
|||
import FilenameLink from "./FilenameLink";
|
||||
import "./katex.css";
|
||||
import "./markdown.css";
|
||||
import MermaidBlock from "./MermaidBlock";
|
||||
import { rehypeHighlightPlugin } from "./rehypeHighlightPlugin";
|
||||
import { StepContainerPreToolbar } from "./StepContainerPreToolbar";
|
||||
import SymbolLink from "./SymbolLink";
|
||||
|
|
@ -331,6 +332,10 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
}
|
||||
}
|
||||
}
|
||||
if (codeProps.className?.includes("language-mermaid")) {
|
||||
const codeText = String(codeProps.children || "");
|
||||
return <MermaidBlock code={codeText} />;
|
||||
}
|
||||
return <code {...codeProps}>{codeProps.children}</code>;
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import {
|
|||
SearchMatch,
|
||||
searchWithinContainer,
|
||||
} from "./findWidgetSearch";
|
||||
import useDebounceValue from "./useDebounce";
|
||||
import { useDebounceValue } from "./useDebounce";
|
||||
import { useElementSize } from "./useElementSize";
|
||||
|
||||
interface HighlightOverlayProps {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
function useDebounceValue<T>(value: T, delay: number = 500): T {
|
||||
export function useDebounceValue<T>(value: T, delay: number = 500): T {
|
||||
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -16,4 +16,32 @@ function useDebounceValue<T>(value: T, delay: number = 500): T {
|
|||
return debouncedValue;
|
||||
}
|
||||
|
||||
export default useDebounceValue;
|
||||
export function useDebouncedEffect(
|
||||
effect: () => void,
|
||||
delay: number,
|
||||
deps: unknown[],
|
||||
) {
|
||||
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
const callBackRef = useRef(() => {});
|
||||
|
||||
useEffect(() => {
|
||||
// do not want the timeout to reset on effect dep change
|
||||
callBackRef.current = effect;
|
||||
}, [effect]);
|
||||
|
||||
useEffect(() => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
callBackRef.current();
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
};
|
||||
}, [delay, ...deps]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue