@@ -7,82 +7,7 @@ import { useAppTranslation } from "@src/i18n/TranslationContext"
77import { useCopyToClipboard } from "@src/utils/clipboard"
88import CodeBlock from "./CodeBlock"
99import { MermaidButton } from "@/components/common/MermaidButton"
10-
11- // Removed previous attempts at static imports for individual diagram types
12- // as the paths were incorrect for Mermaid v11.4.1 and caused errors.
13- // The primary strategy will now rely on Vite's bundling configuration.
14-
15- const MERMAID_THEME = {
16- background : "#1e1e1e" , // VS Code dark theme background
17- textColor : "#ffffff" , // Main text color
18- mainBkg : "#2d2d2d" , // Background for nodes
19- nodeBorder : "#888888" , // Border color for nodes
20- lineColor : "#cccccc" , // Lines connecting nodes
21- primaryColor : "#3c3c3c" , // Primary color for highlights
22- primaryTextColor : "#ffffff" , // Text in primary colored elements
23- primaryBorderColor : "#888888" ,
24- secondaryColor : "#2d2d2d" , // Secondary color for alternate elements
25- tertiaryColor : "#454545" , // Third color for special elements
26-
27- // Class diagram specific
28- classText : "#ffffff" ,
29-
30- // State diagram specific
31- labelColor : "#ffffff" ,
32-
33- // Sequence diagram specific
34- actorLineColor : "#cccccc" ,
35- actorBkg : "#2d2d2d" ,
36- actorBorder : "#888888" ,
37- actorTextColor : "#ffffff" ,
38-
39- // Flow diagram specific
40- fillType0 : "#2d2d2d" ,
41- fillType1 : "#3c3c3c" ,
42- fillType2 : "#454545" ,
43- }
44-
45- mermaid . initialize ( {
46- startOnLoad : false ,
47- // "strict" is required: mermaid renders LLM-generated source, and looser modes allow HTML injection through diagram labels.
48- securityLevel : "strict" ,
49- theme : "dark" ,
50- suppressErrorRendering : true ,
51- themeVariables : {
52- ...MERMAID_THEME ,
53- fontSize : "16px" ,
54- fontFamily : "var(--vscode-font-family, 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif)" ,
55-
56- // Additional styling
57- noteTextColor : "#ffffff" ,
58- noteBkgColor : "#454545" ,
59- noteBorderColor : "#888888" ,
60-
61- // Improve contrast for special elements
62- critBorderColor : "#ff9580" ,
63- critBkgColor : "#803d36" ,
64-
65- // Task diagram specific
66- taskTextColor : "#ffffff" ,
67- taskTextOutsideColor : "#ffffff" ,
68- taskTextLightColor : "#ffffff" ,
69-
70- // Numbers/sections
71- sectionBkgColor : "#2d2d2d" ,
72- sectionBkgColor2 : "#3c3c3c" ,
73-
74- // Alt sections in sequence diagrams
75- altBackground : "#2d2d2d" ,
76-
77- // Links
78- linkColor : "#6cb6ff" ,
79-
80- // Borders and lines
81- compositeBackground : "#2d2d2d" ,
82- compositeBorder : "#888888" ,
83- titleColor : "#ffffff" ,
84- } ,
85- } )
10+ import { getMermaidBackgroundColor , getMermaidConfig , useMermaidTheme } from "./mermaidTheme"
8611
8712interface MermaidBlockProps {
8813 code : string
@@ -93,43 +18,51 @@ export default function MermaidBlock({ code }: MermaidBlockProps) {
9318 const [ isLoading , setIsLoading ] = useState ( false )
9419 const [ error , setError ] = useState < string | null > ( null )
9520 const [ isErrorExpanded , setIsErrorExpanded ] = useState ( false )
21+ const renderVersionRef = useRef ( 0 )
22+ const theme = useMermaidTheme ( )
9623 const { showCopyFeedback, copyWithFeedback } = useCopyToClipboard ( )
9724 const { t } = useAppTranslation ( )
9825
99- // 1) Whenever `code` changes, mark that we need to re-render a new chart
26+ // Whenever the source or host theme changes, invalidate in-flight rendering.
10027 useEffect ( ( ) => {
28+ renderVersionRef . current += 1
10129 setIsLoading ( true )
10230 setError ( null )
103- } , [ code ] )
31+ } , [ code , theme . signature ] )
10432
105- // 2) Debounce the actual parse/render
10633 useDebounceEffect (
10734 ( ) => {
35+ const renderVersion = renderVersionRef . current
10836 if ( containerRef . current ) {
10937 containerRef . current . innerHTML = ""
11038 }
11139
40+ mermaid . initialize ( getMermaidConfig ( theme . kind ) )
11241 mermaid
11342 . parse ( code )
11443 . then ( ( ) => {
11544 const id = `mermaid-${ Math . random ( ) . toString ( 36 ) . substring ( 2 ) } `
11645 return mermaid . render ( id , code )
11746 } )
11847 . then ( ( { svg } ) => {
119- if ( containerRef . current ) {
48+ if ( containerRef . current && renderVersion === renderVersionRef . current ) {
12049 containerRef . current . innerHTML = svg
12150 }
12251 } )
12352 . catch ( ( err ) => {
124- console . warn ( "Mermaid parse/render failed:" , err )
125- setError ( err . message || "Failed to render Mermaid diagram" )
53+ if ( renderVersion === renderVersionRef . current ) {
54+ console . warn ( "Mermaid parse/render failed:" , err )
55+ setError ( err . message || "Failed to render Mermaid diagram" )
56+ }
12657 } )
12758 . finally ( ( ) => {
128- setIsLoading ( false )
59+ if ( renderVersion === renderVersionRef . current ) {
60+ setIsLoading ( false )
61+ }
12962 } )
13063 } ,
131- 500 , // Delay 500ms
132- [ code ] , // Dependencies for scheduling
64+ 500 ,
65+ [ code , theme . signature ] ,
13366 )
13467
13568 /**
@@ -225,6 +158,7 @@ export default function MermaidBlock({ code }: MermaidBlockProps) {
225158}
226159
227160async function svgToPng ( svgEl : SVGElement ) : Promise < string > {
161+ const backgroundColor = getMermaidBackgroundColor ( )
228162 // Clone the SVG to avoid modifying the original
229163 const svgClone = svgEl . cloneNode ( true ) as SVGElement
230164
@@ -266,8 +200,7 @@ async function svgToPng(svgEl: SVGElement): Promise<string> {
266200 const ctx = canvas . getContext ( "2d" )
267201 if ( ! ctx ) return reject ( "Canvas context not available" )
268202
269- // Fill background with Mermaid's dark theme background color
270- ctx . fillStyle = MERMAID_THEME . background
203+ ctx . fillStyle = backgroundColor
271204 ctx . fillRect ( 0 , 0 , canvas . width , canvas . height )
272205
273206 ctx . imageSmoothingEnabled = true
0 commit comments