fix(mermaid): Mermaid theme not change after theme toggling

This commit is contained in:
PilgrimLyieu 2025-03-18 13:48:32 +08:00 committed by 亢奋猫
parent 4dacea04a6
commit f21bf3d860
2 changed files with 19 additions and 6 deletions

View File

@ -18,7 +18,6 @@ export const useMermaid = () => {
startOnLoad: true, startOnLoad: true,
theme: theme === ThemeMode.dark ? 'dark' : 'default' theme: theme === ThemeMode.dark ? 'dark' : 'default'
}) })
window.mermaid.contentLoaded()
}) })
}, [theme]) }, [theme])

View File

@ -1,5 +1,6 @@
import React, { useEffect } from 'react' import React, { useEffect, useRef } from 'react'
import { useTheme } from '@renderer/context/ThemeProvider'
import { ThemeMode } from '@renderer/types'
import MermaidPopup from './MermaidPopup' import MermaidPopup from './MermaidPopup'
interface Props { interface Props {
@ -7,16 +8,29 @@ interface Props {
} }
const Mermaid: React.FC<Props> = ({ chart }) => { const Mermaid: React.FC<Props> = ({ chart }) => {
const { theme } = useTheme()
const mermaidRef = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
window?.mermaid?.contentLoaded() if (mermaidRef.current && window.mermaid) {
}, []) mermaidRef.current.innerHTML = chart
mermaidRef.current.removeAttribute('data-processed')
if (window.mermaid.initialize) {
window.mermaid.initialize({
startOnLoad: true,
theme: theme === ThemeMode.dark ? 'dark' : 'default'
})
}
window.mermaid.contentLoaded()
}
}, [chart, theme])
const onPreview = () => { const onPreview = () => {
MermaidPopup.show({ chart }) MermaidPopup.show({ chart })
} }
return ( return (
<div className="mermaid" onClick={onPreview} style={{ cursor: 'pointer' }}> <div ref={mermaidRef} className="mermaid" onClick={onPreview} style={{ cursor: 'pointer' }}>
{chart} {chart}
</div> </div>
) )