From cbfc32b11fcabb950cf82f5a1a89ba9ab3ac6bb9 Mon Sep 17 00:00:00 2001 From: sayThQ199 <18852951350@163.com> Date: Sun, 25 May 2025 18:05:41 +0800 Subject: [PATCH] fix(echarts): Resolve chart flickering and animation loop in Markdown (#20161) --- web/app/components/base/markdown.tsx | 49 ++++++++++++++-------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/web/app/components/base/markdown.tsx b/web/app/components/base/markdown.tsx index b28bf2bc8f..a47d93268c 100644 --- a/web/app/components/base/markdown.tsx +++ b/web/app/components/base/markdown.tsx @@ -134,6 +134,24 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any const languageShowName = getCorrectCapitalizationLanguageName(language || '') const isDarkMode = theme === Theme.dark + const echartsStyle = useMemo(() => ({ + height: '350px', + width: '100%', + }), []) + + const echartsOpts = useMemo(() => ({ + renderer: 'canvas', + width: 'auto', + }) as any, []) + + const echartsOnEvents = useMemo(() => ({ + finished: () => { + const instance = echartsRef.current?.getEchartsInstance?.() + if (instance) + instance.resize() + }, + }), [echartsRef]) // echartsRef is stable, so this effectively runs once. + // Handle container resize for echarts useEffect(() => { if (language !== 'echarts' || !echartsRef.current) return @@ -329,24 +347,11 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any { - const instance = echartsRef.current?.getEchartsInstance?.() - if (instance) - instance.resize() - }, - }} + onEvents={echartsOnEvents} /> @@ -374,15 +379,9 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any @@ -423,7 +422,7 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any ) } - }, [children, language, isSVG, finalChartOption, props, theme, match]) + }, [children, language, isSVG, finalChartOption, props, theme, match, chartState, isDarkMode, echartsStyle, echartsOpts, echartsOnEvents]) if (inline || !match) return {children}