allow mermaid syntax and export in preview

This commit is contained in:
Aakansha Doshi 2023-08-21 20:52:01 +05:30
parent c5514974b7
commit fec984895e
5 changed files with 116 additions and 26 deletions

View File

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

View File

@ -1,34 +1,118 @@
import { AppState } from "../types"; import { useState, useRef, useEffect } from "react";
import { AppState, BinaryFiles } from "../types";
import { updateActiveTool } from "../utils"; import { updateActiveTool } from "../utils";
import { 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";
const MermaidToExcalidraw = ({ appState }: { appState: AppState }) => { import { DEFAULT_EXPORT_PADDING, DEFAULT_FONT_SIZE } from "../constants";
import {
convertToExcalidrawElements,
exportToCanvas,
} from "../packages/excalidraw/index";
import { NonDeletedExcalidrawElement } from "../element/types";
import { canvasToBlob } from "../data/blob";
const MermaidToExcalidraw = ({
appState,
elements,
}: {
appState: AppState;
elements: readonly NonDeletedExcalidrawElement[];
}) => {
const [text, setText] = useState("");
const [canvasData, setCanvasData] = useState<{
//@ts-ignore
elements: readonly NonDeletedExcalidrawElement[];
files: BinaryFiles | null;
}>({ elements: [], files: null });
const canvasRef = useRef<HTMLDivElement>(null);
const app = useApp();
useEffect(() => {
const canvasNode = canvasRef.current;
if (!canvasNode) {
return;
}
const maxWidth = canvasNode.offsetWidth;
const maxHeight = canvasNode.offsetHeight;
let dimension = Math.max(maxWidth, maxHeight);
if (dimension > canvasNode.offsetWidth) {
dimension = canvasNode.offsetWidth - 10;
}
if (dimension > canvasNode.offsetHeight) {
dimension = canvasNode.offsetHeight;
}
exportToCanvas({
elements: canvasData.elements,
files: canvasData.files,
exportPadding: DEFAULT_EXPORT_PADDING,
maxWidthOrHeight: dimension,
}).then((canvas) => {
// if converting to blob fails, there's some problem that will
// likely prevent preview and export (e.g. canvas too big)
return canvasToBlob(canvas).then(() => {
canvasNode.replaceChildren(canvas);
});
});
}, [canvasData, canvasRef]);
const setAppState = useExcalidrawSetAppState(); const setAppState = useExcalidrawSetAppState();
if (appState?.activeTool?.type !== "mermaid") { if (appState?.activeTool?.type !== "mermaid") {
return null; return null;
} }
const onChange = async (event: any) => {
setText(event.target.value);
let mermaidGraphData;
try {
mermaidGraphData = await parseMermaid(event.target.value, {
fontSize: DEFAULT_FONT_SIZE,
});
} catch (e) {
// Parse error, displaying error message to users
}
if (mermaidGraphData) {
const { elements, files } = graphToExcalidraw(mermaidGraphData);
setCanvasData({ elements: convertToExcalidrawElements(elements), files });
}
};
const onClose = () => {
const activeTool = updateActiveTool(appState, { type: "selection" });
setAppState({ activeTool });
};
const onSelect = () => {
app.scene.replaceAllElements([...elements, ...canvasData.elements]);
app.addFiles(Object.values(canvasData.files || []));
app.scrollToContent(canvasData.elements);
onClose();
};
return ( return (
<Dialog <Dialog onCloseRequest={onClose} title="Mermaid to Excalidraw">
onCloseRequest={() => {
const activeTool = updateActiveTool(appState, { type: "selection" });
setAppState({ activeTool });
}}
title="Mermaid to Excalidraw"
>
<div className="mermaid-to-excalidraw-wrapper"> <div className="mermaid-to-excalidraw-wrapper">
<div className="mermaid-to-excalidraw-wrapper-text"> <div className="mermaid-to-excalidraw-wrapper-text">
<label>Describe</label> <label>Describe</label>
<textarea /> <textarea onChange={onChange} value={text} />
</div> </div>
<div className="mermaid-to-excalidraw-wrapper-preview"> <div className="mermaid-to-excalidraw-wrapper-preview">
<label>Preview</label> <label>Preview</label>
<div className="mermaid-to-excalidraw-wrapper-preview-canvas"></div> <div
className="mermaid-to-excalidraw-wrapper-preview-canvas"
ref={canvasRef}
></div>
<Button <Button
className="mermaid-to-excalidraw-wrapper-preview-insert" className="mermaid-to-excalidraw-wrapper-preview-insert"
onSelect={() => console.log("hey")} onSelect={onSelect}
> >
Insert Insert
</Button> </Button>

View File

@ -159,7 +159,7 @@ export type ExcalidrawElementSkeleton =
} & Partial<ExcalidrawImageElement>); } & Partial<ExcalidrawImageElement>);
const DEFAULT_LINEAR_ELEMENT_PROPS = { const DEFAULT_LINEAR_ELEMENT_PROPS = {
width: 300, width: 100,
height: 0, height: 0,
}; };

View File

@ -7,7 +7,11 @@ import { AppState, PointerDownState } from "../types";
import { getBoundTextElement } from "./textElement"; import { getBoundTextElement } from "./textElement";
import { isSelectedViaGroup } from "../groups"; import { isSelectedViaGroup } from "../groups";
import Scene from "../scene/Scene"; import Scene from "../scene/Scene";
import { isFrameElement } from "./typeChecks"; import {
isArrowElement,
isBoundToContainer,
isFrameElement,
} from "./typeChecks";
export const dragSelectedElements = ( export const dragSelectedElements = (
pointerDownState: PointerDownState, pointerDownState: PointerDownState,
@ -36,6 +40,7 @@ export const dragSelectedElements = (
if (frames.length > 0) { if (frames.length > 0) {
const elementsInFrames = scene const elementsInFrames = scene
.getNonDeletedElements() .getNonDeletedElements()
.filter((e) => !isBoundToContainer(e))
.filter((e) => e.frameId !== null) .filter((e) => e.frameId !== null)
.filter((e) => frames.includes(e.frameId!)); .filter((e) => frames.includes(e.frameId!));
@ -54,20 +59,16 @@ export const dragSelectedElements = (
// update coords of bound text only if we're dragging the container directly // update coords of bound text only if we're dragging the container directly
// (we don't drag the group that it's part of) // (we don't drag the group that it's part of)
if ( if (
// Don't update coords of arrow label since we calculate its position during render
!isArrowElement(element) &&
// container isn't part of any group // container isn't part of any group
// (perf optim so we don't check `isSelectedViaGroup()` in every case) // (perf optim so we don't check `isSelectedViaGroup()` in every case)
!element.groupIds.length || (!element.groupIds.length ||
// container is part of a group, but we're dragging the container directly // container is part of a group, but we're dragging the container directly
(appState.editingGroupId && !isSelectedViaGroup(appState, element)) (appState.editingGroupId && !isSelectedViaGroup(appState, element)))
) { ) {
const textElement = getBoundTextElement(element); const textElement = getBoundTextElement(element);
if ( if (textElement) {
textElement &&
// when container is added to a frame, so will its bound text
// so the text is already in `elementsToUpdate` and we should avoid
// updating its coords again
(!textElement.frameId || !frames.includes(textElement.frameId))
) {
updateElementCoords( updateElementCoords(
lockDirection, lockDirection,
distanceX, distanceX,

View File

@ -525,6 +525,8 @@ export type AppClassProperties = {
onInsertElements: App["onInsertElements"]; onInsertElements: App["onInsertElements"];
onExportImage: App["onExportImage"]; onExportImage: App["onExportImage"];
lastViewportPosition: App["lastViewportPosition"]; lastViewportPosition: App["lastViewportPosition"];
scrollToContent: App["scrollToContent"];
addFiles: App["addFiles"];
}; };
export type PointerDownState = Readonly<{ export type PointerDownState = Readonly<{