store mermaid text in customData of image element

This commit is contained in:
zsviczian 2023-09-24 19:14:49 +02:00 committed by GitHub
parent b1d923db3a
commit 7da500fe0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 9 deletions

View File

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

View File

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

View File

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