Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
98525319
Unverified
Commit
98525319
authored
Oct 26, 2023
by
Charlie.Wei
Committed by
GitHub
Oct 26, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mermaid front-end rendering initialization exception handling logic o… (#1407)
parent
48b42497
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
9 deletions
+20
-9
index.tsx
web/app/components/app/chat/mermaid/index.tsx
+20
-9
No files found.
web/app/components/app/chat/mermaid/index.tsx
View file @
98525319
import
React
,
{
useEffect
,
useRef
,
useState
}
from
'react'
import
mermaid
from
'mermaid'
import
{
t
}
from
'i18next'
import
CryptoJS
from
'crypto-js'
let
mermaidAPI
:
any
...
...
@@ -32,6 +31,14 @@ const Flowchart = React.forwardRef((props: {
const
chartId
=
useRef
(
`flowchart_
${
CryptoJS
.
MD5
(
props
.
PrimitiveCode
).
toString
()}
`
)
const
[
isRender
,
setIsRender
]
=
useState
(
true
)
const
clearFlowchartCache
=
()
=>
{
for
(
let
i
=
localStorage
.
length
-
1
;
i
>=
0
;
--
i
)
{
const
key
=
localStorage
.
key
(
i
)
if
(
key
&&
key
.
startsWith
(
'flowchart_'
))
localStorage
.
removeItem
(
key
)
}
}
const
renderFlowchart
=
async
(
PrimitiveCode
:
string
)
=>
{
try
{
const
cachedSvg
:
any
=
localStorage
.
getItem
(
chartId
.
current
)
...
...
@@ -44,15 +51,15 @@ const Flowchart = React.forwardRef((props: {
const
svgGraph
=
await
mermaidAPI
.
render
(
chartId
.
current
,
PrimitiveCode
)
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const
base64Svg
:
any
=
await
svgToBase64
(
svgGraph
.
svg
)
localStorage
.
setItem
(
chartId
.
current
,
base64Svg
)
setSvgCode
(
base64Svg
)
if
(
chartId
.
current
&&
base64Svg
)
localStorage
.
setItem
(
chartId
.
current
,
base64Svg
)
}
}
catch
(
error
)
{
localStorage
.
clear
()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
console
.
error
(
error
.
toString
())
clearFlowchartCache
()
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleReRender
()
}
}
...
...
@@ -70,7 +77,9 @@ const Flowchart = React.forwardRef((props: {
const
handleReRender
=
()
=>
{
setIsRender
(
false
)
setSvgCode
(
null
)
if
(
chartId
.
current
)
localStorage
.
removeItem
(
chartId
.
current
)
setTimeout
(()
=>
{
setIsRender
(
true
)
renderFlowchart
(
props
.
PrimitiveCode
)
...
...
@@ -90,9 +99,11 @@ const Flowchart = React.forwardRef((props: {
// @ts-expect-error
<
div
ref=
{
ref
}
>
{
isRender
&&
<
div
id=
{
chartId
.
current
}
className=
"mermaid"
style=
{
style
}
>
{
svgCode
&&
(<
img
src=
{
svgCode
}
style=
{
{
width
:
'100%'
,
height
:
'auto'
}
}
alt=
"Mermaid chart"
/>)
}
</
div
>
isRender
&&
<
div
id=
{
chartId
.
current
}
className=
"mermaid"
style=
{
style
}
>
{
svgCode
&&
<
img
src=
{
svgCode
}
style=
{
{
width
:
'100%'
,
height
:
'auto'
}
}
alt=
"Mermaid chart"
/>
}
</
div
>
}
<
button
onClick=
{
handleReRender
}
>
{
t
(
'appApi.merMaind.rerender'
)
}
</
button
>
</
div
>
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment