You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.0 KiB
TypeScript
163 lines
4.0 KiB
TypeScript
1 year ago
|
import { MermaidOptions } from "@excalidraw/mermaid-to-excalidraw";
|
||
|
import { MermaidToExcalidrawResult } from "@excalidraw/mermaid-to-excalidraw/dist/interfaces";
|
||
1 year ago
|
import {
|
||
|
DEFAULT_EXPORT_PADDING,
|
||
|
DEFAULT_FONT_SIZE,
|
||
|
EDITOR_LS_KEYS,
|
||
|
} from "../../constants";
|
||
1 year ago
|
import { convertToExcalidrawElements, exportToCanvas } from "../../index";
|
||
1 year ago
|
import { NonDeletedExcalidrawElement } from "../../element/types";
|
||
|
import { AppClassProperties, BinaryFiles } from "../../types";
|
||
|
import { canvasToBlob } from "../../data/blob";
|
||
1 year ago
|
import { EditorLocalStorage } from "../../data/EditorLocalStorage";
|
||
1 year ago
|
|
||
|
const resetPreview = ({
|
||
|
canvasRef,
|
||
|
setError,
|
||
|
}: {
|
||
|
canvasRef: React.RefObject<HTMLDivElement>;
|
||
|
setError: (error: Error | null) => void;
|
||
|
}) => {
|
||
|
const canvasNode = canvasRef.current;
|
||
|
|
||
|
if (!canvasNode) {
|
||
|
return;
|
||
|
}
|
||
|
const parent = canvasNode.parentElement;
|
||
|
if (!parent) {
|
||
|
return;
|
||
|
}
|
||
|
parent.style.background = "";
|
||
|
setError(null);
|
||
|
canvasNode.replaceChildren();
|
||
|
};
|
||
|
|
||
|
export interface MermaidToExcalidrawLibProps {
|
||
|
loaded: boolean;
|
||
|
api: Promise<{
|
||
|
parseMermaidToExcalidraw: (
|
||
|
definition: string,
|
||
|
options: MermaidOptions,
|
||
|
) => Promise<MermaidToExcalidrawResult>;
|
||
|
}>;
|
||
|
}
|
||
|
|
||
|
interface ConvertMermaidToExcalidrawFormatProps {
|
||
|
canvasRef: React.RefObject<HTMLDivElement>;
|
||
|
mermaidToExcalidrawLib: MermaidToExcalidrawLibProps;
|
||
1 year ago
|
mermaidDefinition: string;
|
||
1 year ago
|
setError: (error: Error | null) => void;
|
||
|
data: React.MutableRefObject<{
|
||
|
elements: readonly NonDeletedExcalidrawElement[];
|
||
|
files: BinaryFiles | null;
|
||
|
}>;
|
||
|
}
|
||
|
|
||
|
export const convertMermaidToExcalidraw = async ({
|
||
|
canvasRef,
|
||
|
mermaidToExcalidrawLib,
|
||
1 year ago
|
mermaidDefinition,
|
||
1 year ago
|
setError,
|
||
|
data,
|
||
|
}: ConvertMermaidToExcalidrawFormatProps) => {
|
||
|
const canvasNode = canvasRef.current;
|
||
|
const parent = canvasNode?.parentElement;
|
||
|
|
||
|
if (!canvasNode || !parent) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
1 year ago
|
if (!mermaidDefinition) {
|
||
1 year ago
|
resetPreview({ canvasRef, setError });
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
const api = await mermaidToExcalidrawLib.api;
|
||
|
|
||
1 year ago
|
let ret;
|
||
|
try {
|
||
|
ret = await api.parseMermaidToExcalidraw(mermaidDefinition, {
|
||
|
fontSize: DEFAULT_FONT_SIZE,
|
||
|
});
|
||
|
} catch (err: any) {
|
||
|
ret = await api.parseMermaidToExcalidraw(
|
||
|
mermaidDefinition.replace(/"/g, "'"),
|
||
|
{
|
||
|
fontSize: DEFAULT_FONT_SIZE,
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
const { elements, files } = ret;
|
||
1 year ago
|
setError(null);
|
||
|
|
||
|
data.current = {
|
||
|
elements: convertToExcalidrawElements(elements, {
|
||
|
regenerateIds: true,
|
||
|
}),
|
||
|
files,
|
||
|
};
|
||
|
|
||
|
const canvas = await exportToCanvas({
|
||
|
elements: data.current.elements,
|
||
|
files: data.current.files,
|
||
|
exportPadding: DEFAULT_EXPORT_PADDING,
|
||
|
maxWidthOrHeight:
|
||
|
Math.max(parent.offsetWidth, parent.offsetHeight) *
|
||
|
window.devicePixelRatio,
|
||
|
});
|
||
|
// if converting to blob fails, there's some problem that will
|
||
|
// likely prevent preview and export (e.g. canvas too big)
|
||
|
await canvasToBlob(canvas);
|
||
|
parent.style.background = "var(--default-bg-color)";
|
||
|
canvasNode.replaceChildren(canvas);
|
||
|
} catch (err: any) {
|
||
|
parent.style.background = "var(--default-bg-color)";
|
||
1 year ago
|
if (mermaidDefinition) {
|
||
1 year ago
|
setError(err);
|
||
|
}
|
||
|
|
||
|
throw err;
|
||
|
}
|
||
|
};
|
||
|
|
||
1 year ago
|
export const saveMermaidDataToStorage = (mermaidDefinition: string) => {
|
||
|
EditorLocalStorage.set(
|
||
|
EDITOR_LS_KEYS.MERMAID_TO_EXCALIDRAW,
|
||
|
mermaidDefinition,
|
||
|
);
|
||
1 year ago
|
};
|
||
|
|
||
|
export const insertToEditor = ({
|
||
|
app,
|
||
|
data,
|
||
|
text,
|
||
|
shouldSaveMermaidDataToStorage,
|
||
|
}: {
|
||
|
app: AppClassProperties;
|
||
|
data: React.MutableRefObject<{
|
||
|
elements: readonly NonDeletedExcalidrawElement[];
|
||
|
files: BinaryFiles | null;
|
||
|
}>;
|
||
|
text?: string;
|
||
|
shouldSaveMermaidDataToStorage?: boolean;
|
||
|
}) => {
|
||
|
const { elements: newElements, files } = data.current;
|
||
|
|
||
|
if (!newElements.length) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
app.addElementsFromPasteOrLibrary({
|
||
|
elements: newElements,
|
||
|
files,
|
||
|
position: "center",
|
||
|
fitToContent: true,
|
||
|
});
|
||
|
app.setOpenDialog(null);
|
||
|
|
||
|
if (shouldSaveMermaidDataToStorage && text) {
|
||
|
saveMermaidDataToStorage(text);
|
||
|
}
|
||
|
};
|