Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3fc9ca

Browse files
authored
Merge pull request lowcoder-org#287 from mingfang/develop
add Mermaid component
2 parents df68423 + 0765d7b commit e3fc9ca

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

client/packages/openblocks-comps/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@types/react": "17",
88
"@types/react-dom": "17",
99
"big.js": "^6.2.1",
10+
"mermaid": "^10.0.0",
1011
"openblocks-cli": "workspace:^",
1112
"openblocks-sdk": "workspace:^",
1213
"react": "17",
@@ -31,6 +32,14 @@
3132
"w": 15,
3233
"h": 60
3334
}
35+
},
36+
"mermaid": {
37+
"name": "Mermaid",
38+
"icon": "./icons/icon-chart.svg",
39+
"layoutInfo": {
40+
"w": 15,
41+
"h": 60
42+
}
3443
}
3544
}
3645
},
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
UICompBuilder,
3+
Section,
4+
withExposingConfigs,
5+
stringExposingStateControl,
6+
NameConfig,
7+
eventHandlerControl,
8+
withMethodExposing,
9+
} from "openblocks-sdk";
10+
11+
import Mermaid from "./mermaid"
12+
13+
const childrenMap = {
14+
code: stringExposingStateControl("code",
15+
`graph LR
16+
Start --> Stop`
17+
),
18+
onEvent: eventHandlerControl([
19+
{
20+
label: "onChange",
21+
value: "change",
22+
description: "",
23+
},
24+
]),
25+
};
26+
27+
const CompBase = new UICompBuilder(childrenMap, (props: any) => {
28+
const code = props.code.value;
29+
return (
30+
<Mermaid code={code}/>
31+
);
32+
}).setPropertyViewFn((children: any) => {
33+
return (
34+
<>
35+
<Section name="Basic">
36+
{children.code.propertyView({ label: "code" })}
37+
</Section>
38+
<Section name="Interaction">{children.onEvent.propertyView()}</Section>
39+
</>
40+
);
41+
}).build();
42+
43+
const AppViewCompTemp = withMethodExposing(CompBase, []);
44+
45+
export const MermaidComp = withExposingConfigs(AppViewCompTemp, [
46+
new NameConfig("code", ""),
47+
]);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { useEffect, useState } from "react"
2+
import mermaid from "mermaid"
3+
4+
function escape(str: string): string {
5+
const entries: { [index: string]: any } = { lt: "<", gt: ">", nbsp: " ", amp: "&", quot: "\"" }
6+
return str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function (_, t) {
7+
return entries[t]
8+
}).trim()
9+
}
10+
11+
export default ({ id = "graphDiv", code = "" }) => {
12+
const [svg, setSvg] = useState('')
13+
14+
useEffect(() => {
15+
mermaid.initialize({ startOnLoad: false })
16+
}, [])
17+
18+
useEffect(() => {
19+
if (!code) return
20+
21+
mermaid.mermaidAPI.render(id, escape(code)).then(res => {
22+
setSvg(res.svg)
23+
})
24+
}, [code, setSvg])
25+
26+
return (
27+
<pre className="mermaid" dangerouslySetInnerHTML={{ __html: svg }}></pre>
28+
)
29+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ChartCompWithDefault } from "./comps/chartComp/chartComp";
22
import { ImageEditorComp } from "./comps/imageEditorComp/index";
3+
import {MermaidComp} from "./comps/mermaidComp";
34

45
export default {
56
chart: ChartCompWithDefault,
67
imageEditor: ImageEditorComp,
8+
mermaid: MermaidComp,
79
};

0 commit comments

Comments
 (0)