load mermaid lazily
This commit is contained in:
parent
3346c72356
commit
41a0258fe1
@ -4,10 +4,6 @@ import { updateActiveTool } from "../utils";
|
|||||||
import { useApp, useExcalidrawSetAppState } from "./App";
|
import { useApp, useExcalidrawSetAppState } from "./App";
|
||||||
import { Button } from "./Button";
|
import { Button } from "./Button";
|
||||||
import { Dialog } from "./Dialog";
|
import { Dialog } from "./Dialog";
|
||||||
import {
|
|
||||||
parseMermaid,
|
|
||||||
graphToExcalidraw,
|
|
||||||
} from "@excalidraw/mermaid-to-excalidraw";
|
|
||||||
|
|
||||||
import "./MermaidToExcalidraw.scss";
|
import "./MermaidToExcalidraw.scss";
|
||||||
import { DEFAULT_EXPORT_PADDING, DEFAULT_FONT_SIZE } from "../constants";
|
import { DEFAULT_EXPORT_PADDING, DEFAULT_FONT_SIZE } from "../constants";
|
||||||
@ -50,35 +46,53 @@ const MermaidToExcalidraw = ({
|
|||||||
appState: AppState;
|
appState: AppState;
|
||||||
elements: readonly NonDeletedExcalidrawElement[];
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
}) => {
|
}) => {
|
||||||
|
const mermaidToExcalidrawLib = useRef<any>(null);
|
||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
const canvasRef = useRef<HTMLDivElement>(null);
|
const canvasRef = useRef<HTMLDivElement>(null);
|
||||||
const data = useRef<{
|
const data = useRef<{
|
||||||
elements: readonly NonDeletedExcalidrawElement[];
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
files: BinaryFiles | null;
|
files: BinaryFiles | null;
|
||||||
}>({ elements: [], files: null });
|
}>({ elements: [], files: null });
|
||||||
|
|
||||||
const app = useApp();
|
const app = useApp();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const data = importMermaidDataFromStorage();
|
const loadMermaidToExcalidrawLib = async () => {
|
||||||
if (data) {
|
mermaidToExcalidrawLib.current = await import(
|
||||||
setText(data);
|
/* webpackChunkName:"mermaid-to-excalidraw" */ "@excalidraw/mermaid-to-excalidraw"
|
||||||
}
|
);
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
loadMermaidToExcalidrawLib();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!loading) {
|
||||||
|
const data = importMermaidDataFromStorage();
|
||||||
|
if (data) {
|
||||||
|
setText(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [loading]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const convertMermaidToExcal = async () => {
|
const convertMermaidToExcal = async () => {
|
||||||
let mermaidGraphData;
|
let mermaidGraphData;
|
||||||
try {
|
try {
|
||||||
mermaidGraphData = await parseMermaid(text, {
|
mermaidGraphData = await mermaidToExcalidrawLib.current.parseMermaid(
|
||||||
fontSize: DEFAULT_FONT_SIZE,
|
text,
|
||||||
});
|
{
|
||||||
|
fontSize: DEFAULT_FONT_SIZE,
|
||||||
|
},
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Parse error, displaying error message to users
|
// Parse error, displaying error message to users
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mermaidGraphData) {
|
if (mermaidGraphData) {
|
||||||
const { elements, files } = graphToExcalidraw(mermaidGraphData);
|
const { elements, files } =
|
||||||
|
mermaidToExcalidrawLib.current.graphToExcalidraw(mermaidGraphData);
|
||||||
|
|
||||||
data.current = {
|
data.current = {
|
||||||
elements: convertToExcalidrawElements(elements),
|
elements: convertToExcalidrawElements(elements),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user