persist mermaid data to storage

This commit is contained in:
Aakansha Doshi 2023-08-24 12:29:26 +05:30
parent f85f890b25
commit 6518e05cab
3 changed files with 39 additions and 8 deletions

View File

@ -1178,10 +1178,12 @@ class App extends React.Component<AppProps, AppState> {
app={this} app={this}
> >
{this.props.children} {this.props.children}
<MermaidToExcalidraw {this.state.activeTool.type === "mermaid" && (
appState={this.state} <MermaidToExcalidraw
elements={this.scene.getNonDeletedElements()} appState={this.state}
/> elements={this.scene.getNonDeletedElements()}
/>
)}
</LayerUI> </LayerUI>
<div className="excalidraw-textEditorContainer" /> <div className="excalidraw-textEditorContainer" />

View File

@ -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 = () => {

View File

@ -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"],