-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathExcalidrawWrapper.tsx
More file actions
65 lines (56 loc) · 1.64 KB
/
ExcalidrawWrapper.tsx
File metadata and controls
65 lines (56 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { useEffect, useState } from "react";
import {
Excalidraw,
convertToExcalidrawElements,
} from "@excalidraw/excalidraw";
import { graphToExcalidraw } from "../src/graphToExcalidraw";
import { DEFAULT_FONT_SIZE } from "../src/constants";
import type { MermaidData } from "./";
import "@excalidraw/excalidraw/index.css";
import { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";
interface ExcalidrawWrapperProps {
mermaidDefinition: MermaidData["definition"];
mermaidOutput: MermaidData["output"];
}
const ExcalidrawWrapper = ({
mermaidDefinition,
mermaidOutput,
}: ExcalidrawWrapperProps) => {
const [excalidrawAPI, setExcalidrawAPI] =
useState<ExcalidrawImperativeAPI | null>(null);
useEffect(() => {
if (!excalidrawAPI) {
return;
}
if (mermaidDefinition === "" || mermaidOutput === null) {
excalidrawAPI.resetScene();
return;
}
const { elements, files } = graphToExcalidraw(mermaidOutput, {
fontSize: DEFAULT_FONT_SIZE,
});
excalidrawAPI.updateScene({
elements: convertToExcalidrawElements(elements),
});
excalidrawAPI.scrollToContent(excalidrawAPI.getSceneElements(), {
fitToContent: true,
});
if (files) {
excalidrawAPI.addFiles(Object.values(files));
}
}, [mermaidDefinition, mermaidOutput]);
return (
<div className="excalidraw-wrapper">
<Excalidraw
initialData={{
appState: {
viewBackgroundColor: "#fafafa",
currentItemFontFamily: 1,
},
}}
excalidrawAPI={(api) => setExcalidrawAPI(api)}
/>
</div>
);
};
export default ExcalidrawWrapper;