Browse Source

store mermaid text in customData of image element

zsviczian 1 year ago
parent
commit
7da500fe0a
3 changed files with 32 additions and 9 deletions
  1. 0 1
      src/components/Actions.tsx
  2. 5 1
      src/components/App.tsx
  3. 27 7
      src/components/MermaidToExcalidraw.tsx

+ 0 - 1
src/components/Actions.tsx

@@ -341,7 +341,6 @@ export const ShapesSwitcher = ({
               setAppState({
                 activeTool: nextActiveTool,
                 multiElement: null,
-                selectedElementIds: {},
               });
             }}
             icon={mermaidLogoIcon}

+ 5 - 1
src/components/App.tsx

@@ -1192,7 +1192,11 @@ class App extends React.Component<AppProps, AppState> {
                         >
                           {this.props.children}
                           {this.state.activeTool.type === "mermaid" && (
-                            <MermaidToExcalidraw />
+                            <MermaidToExcalidraw
+                              selectedElements={this.scene.getSelectedElements(
+                                this.state,
+                              )}
+                            />
                           )}
                         </LayerUI>
 

+ 27 - 7
src/components/MermaidToExcalidraw.tsx

@@ -1,5 +1,5 @@
 import { useState, useRef, useEffect, useDeferredValue } from "react";
-import { BinaryFiles } from "../types";
+import { AppState, BinaryFiles } from "../types";
 import { useApp } from "./App";
 import { Button } from "./Button";
 import { Dialog } from "./Dialog";
@@ -8,7 +8,10 @@ import {
   convertToExcalidrawElements,
   exportToCanvas,
 } from "../packages/excalidraw/index";
-import { NonDeletedExcalidrawElement } from "../element/types";
+import {
+  ExcalidrawElement,
+  NonDeletedExcalidrawElement,
+} from "../element/types";
 import { canvasToBlob } from "../data/blob";
 import { ArrowRightIcon } from "./icons";
 import Spinner from "./Spinner";
@@ -65,7 +68,11 @@ const ErrorComp = ({ error }: { error: string }) => {
   );
 };
 
-const MermaidToExcalidraw = () => {
+const MermaidToExcalidraw = ({
+  selectedElements,
+}: {
+  selectedElements: readonly NonDeletedExcalidrawElement[];
+}) => {
   const mermaidToExcalidrawLib = useRef<{
     parseMermaidToExcalidraw: (
       defination: string,
@@ -111,7 +118,12 @@ const MermaidToExcalidraw = () => {
 
   useEffect(() => {
     if (!loading) {
-      const data = importMermaidDataFromStorage() || MERMAID_EXAMPLE;
+      const selectedMermaidImage = selectedElements.filter(
+        (el) => el.type === "image" && el.customData?.mermaidText,
+      )[0];
+      const data = selectedMermaidImage
+        ? selectedMermaidImage.customData?.mermaidText
+        : importMermaidDataFromStorage() || MERMAID_EXAMPLE;
 
       setText(data);
     }
@@ -134,9 +146,17 @@ const MermaidToExcalidraw = () => {
         setError(null);
 
         data.current = {
-          elements: convertToExcalidrawElements(elements, {
-            regenerateIds: true,
-          }),
+          elements: convertToExcalidrawElements(
+            elements.map((el) => {
+              if (el.type === "image") {
+                el.customData = { mermaidText: text };
+              }
+              return el;
+            }),
+            {
+              regenerateIds: true,
+            },
+          ),
           files,
         };
         const parent = canvasNode.parentElement!;