Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mermaid render #6088

Merged
merged 7 commits into from
Jul 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: 修正mermaid
1. 修正svgtobase64
2. remote unused memo
  • Loading branch information
靖谦 committed Jul 5, 2024
commit c592df8983a28a34cca8de88f12976e712698fb1
37 changes: 18 additions & 19 deletions web/app/components/base/mermaid/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import mermaid from 'mermaid'
import CryptoJS from 'crypto-js'
import LoadingAnim from '@/app/components/base/chat/chat/loading-anim'
Expand All @@ -24,6 +24,17 @@ const style = {
overflow: 'auto',
}

const svgToBase64 = (svgGraph: string) => {
const svgBytes = new TextEncoder().encode(svgGraph)
const blob = new Blob([svgBytes], { type: 'image/svg+xml;charset=utf-8' })
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
})
}

const Flowchart = React.forwardRef((props: {
PrimitiveCode: string
}, ref) => {
Expand Down Expand Up @@ -55,32 +66,20 @@ const Flowchart = React.forwardRef((props: {
if (!dom.querySelector('g.main'))
throw new Error('empty svg')

// const base64Svg: any = await svgToBase64(svgGraph.svg)
setSvgCode(svgGraph.svg)
const base64Svg: any = await svgToBase64(svgGraph.svg)
setSvgCode(base64Svg)
setIsLoading(false)
if (chartId.current && svgGraph.svg)
localStorage.setItem(chartId.current, svgGraph.svg)
if (chartId.current && base64Svg)
localStorage.setItem(chartId.current, base64Svg)
}
}
catch (error) {
console.error('mermaid', (error as Error).message)
clearFlowchartCache()
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleReRender()
}
}

const svgToBase64 = (svgGraph: string) => {
const svgBytes = new TextEncoder().encode(svgGraph)
const blob = new Blob([svgBytes], { type: 'image/svg+xml;charset=utf-8' })
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
})
}

const handleReRender = () => {
setIsRender(false)
setSvgCode(null)
Expand All @@ -107,7 +106,7 @@ const Flowchart = React.forwardRef((props: {
<div ref={ref}>
{
isRender
&& <div id={chartId.current} className="mermaid" style={style}>
&& <div className="mermaid" style={style}>
{svgCode && <img src={svgCode} style={{ width: '100%', height: 'auto' }} alt="Mermaid chart" />}
</div>
}
Expand All @@ -120,4 +119,4 @@ const Flowchart = React.forwardRef((props: {
)
})

export default memo(Flowchart)
export default Flowchart