persist mermaid data to storage
This commit is contained in:
parent
f85f890b25
commit
6518e05cab
@ -1178,10 +1178,12 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
app={this}
|
app={this}
|
||||||
>
|
>
|
||||||
{this.props.children}
|
{this.props.children}
|
||||||
|
{this.state.activeTool.type === "mermaid" && (
|
||||||
<MermaidToExcalidraw
|
<MermaidToExcalidraw
|
||||||
appState={this.state}
|
appState={this.state}
|
||||||
elements={this.scene.getNonDeletedElements()}
|
elements={this.scene.getNonDeletedElements()}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</LayerUI>
|
</LayerUI>
|
||||||
|
|
||||||
<div className="excalidraw-textEditorContainer" />
|
<div className="excalidraw-textEditorContainer" />
|
||||||
|
@ -18,6 +18,31 @@ import {
|
|||||||
import { NonDeletedExcalidrawElement } from "../element/types";
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
||||||
import { canvasToBlob } from "../data/blob";
|
import { canvasToBlob } from "../data/blob";
|
||||||
|
|
||||||
|
const LOCAL_STORAGE_KEY_MERMAID_TO_EXCALIDRAW = "mermaid-to-excalidraw";
|
||||||
|
|
||||||
|
const saveMermaidDataToStorage = (data: string) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(LOCAL_STORAGE_KEY_MERMAID_TO_EXCALIDRAW, data);
|
||||||
|
} catch (error: any) {
|
||||||
|
// Unable to access window.localStorage
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const importMermaidDataFromStorage = () => {
|
||||||
|
try {
|
||||||
|
const data = localStorage.getItem(LOCAL_STORAGE_KEY_MERMAID_TO_EXCALIDRAW);
|
||||||
|
if (data) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
// Unable to access localStorage
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
const MermaidToExcalidraw = ({
|
const MermaidToExcalidraw = ({
|
||||||
appState,
|
appState,
|
||||||
elements,
|
elements,
|
||||||
@ -34,6 +59,13 @@ const MermaidToExcalidraw = ({
|
|||||||
const canvasRef = useRef<HTMLDivElement>(null);
|
const canvasRef = useRef<HTMLDivElement>(null);
|
||||||
const app = useApp();
|
const app = useApp();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const data = importMermaidDataFromStorage();
|
||||||
|
if (data) {
|
||||||
|
setText(data);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const canvasNode = canvasRef.current;
|
const canvasNode = canvasRef.current;
|
||||||
if (!canvasNode) {
|
if (!canvasNode) {
|
||||||
@ -86,13 +118,11 @@ const MermaidToExcalidraw = ({
|
|||||||
}, [text]);
|
}, [text]);
|
||||||
|
|
||||||
const setAppState = useExcalidrawSetAppState();
|
const setAppState = useExcalidrawSetAppState();
|
||||||
if (appState?.activeTool?.type !== "mermaid") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const onClose = () => {
|
const onClose = () => {
|
||||||
const activeTool = updateActiveTool(appState, { type: "selection" });
|
const activeTool = updateActiveTool(appState, { type: "selection" });
|
||||||
setAppState({ activeTool });
|
setAppState({ activeTool });
|
||||||
|
saveMermaidDataToStorage(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSelect = () => {
|
const onSelect = () => {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { defineConfig } from "vitest/config";
|
import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
test: {
|
test: {
|
||||||
setupFiles: ["./src/setupTests.ts"],
|
setupFiles: ["./src/setupTests.ts"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user