mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-20 21:23:22 +03:00
feat(editor): bump mermaid-to-excalidraw to 3.6.0 (elbow flowcharts, Gantt, quadrant charts, timelines) (#12535)
* feat(editor): bump mermaid-to-excalidraw to 3.3.0 (Pele 1.13, stepped edges) - mermaid-to-excalidraw 3.1.0 -> 3.3.0 (Pele 1.8.0 -> 1.13.0): flowchart edges and state transitions are elbow arrows when the definition asks for a stepped curve (`curve: "step"`, `linkStyle … interpolate step`); class namespaces are drawn as boxes like subgraphs (3.2.0) - Pele 1.13 reports syntax errors on the source line (counting front matter, directives, comments and blank lines) and errors past the end on the last line, so the TTD dialog no longer converts them: the conversion would shift them twice Claude Opus 5.5 (low) * docs: link #12535 in the changelog Claude Opus 5.5 (low) * feat(editor): draw Mermaid flowchart and state edges as elbow arrows Convert Mermaid diagrams (TTD dialog and paste) with `flowchart.curve: "step"` (`MERMAID_TO_EXCALIDRAW_CONFIG`), which m2e 3.3 turns into elbow arrows for flowchart edges and state transitions. A curve the definition sets itself (front matter, `%%{init}%%`, `linkStyle … interpolate`) still wins; class and ER diagrams are unaffected. Claude Opus 5.5 (low) * feat(editor): elbow arrows for Mermaid flowcharts only (mermaid-to-excalidraw 3.4.0) mermaid-to-excalidraw 3.4.0 replaces `flowchart.curve` with `elbow`, per diagram type. Use `elbow: { flowchart: true }`: state diagrams' elbow transitions need more work in m2e, so they stay as before. Claude Opus 5.5 (low) * feat(editor): bump mermaid-to-excalidraw to 3.5.0 (Gantt, quadrant charts, timelines) mermaid-to-excalidraw 3.5.0 converts Gantt charts, quadrant charts and timelines to Excalidraw elements instead of an image. The Mermaid dialog's description lists them (and State diagrams, missing before), its syntax links built by one helper. Claude Opus 5.5 (low) * fix(editor): detect pasted Mermaid after comments and front matter Pasted text was only taken for Mermaid when its diagram type came first, or after a single one-line `%%{...}%%` directive. Skip everything Mermaid allows before it: blank lines, `%%` comments, directives (also over several lines) and front matter. A directive doesn't run past the next `%%{`, so text with many unclosed directives is checked in linear time (searching each to the end of the text would be quadratic: ReDoS). Claude Opus 5.5 (low) * feat(editor): bump mermaid-to-excalidraw to 3.6.0 (diagram types loaded on first use) mermaid-to-excalidraw 3.6.0 loads each diagram type's parser and converter on first use: its main chunk is 20.3 KiB (brotli) instead of 35.8, plus 1.5–5.1 KiB for the type converted (a first flowchart: 28.5 KiB). Claude Opus 5.5 (low)
This commit is contained in:
1 parent
dd2f8fc32b
commit
7ded689cad
16 files changed
+155
-222
No files matched your search
@@ -209,7 +209,7 @@ The toolbar UI was rewritten from hidden radio/checkbox inputs to real `<button>
|
||||
|
||||
### Other Features
|
||||
|
||||
- Mermaid diagrams (in the TTD dialog and on paste) are converted with `@excalidraw/mermaid-to-excalidraw` v3, which lays them out with [Pele](https://pele.run) instead of Mermaid, no longer bundled. Layouts differ from before, and `swimlane-beta` diagrams are supported. Each diagram type's code is loaded the first time one is converted. [#12480](https://github.com/excalidraw/excalidraw/pull/12480), [#12481](https://github.com/excalidraw/excalidraw/pull/12481)
|
||||
- Mermaid diagrams (in the TTD dialog and on paste) are converted with `@excalidraw/mermaid-to-excalidraw` v3, which lays them out with [Pele](https://pele.run) instead of Mermaid, no longer bundled. Layouts differ from before, `swimlane-beta` diagrams are supported, and Gantt charts, quadrant charts and timelines are converted to elements instead of an image. Each diagram type's code is loaded the first time one is converted. Flowchart edges are elbow arrows, unless the definition sets its own curve (in its front matter or `%%{init}%%`), and class diagram namespaces are drawn as boxes like flowchart subgraphs. [#12480](https://github.com/excalidraw/excalidraw/pull/12480), [#12481](https://github.com/excalidraw/excalidraw/pull/12481), [#12535](https://github.com/excalidraw/excalidraw/pull/12535)
|
||||
|
||||
- `label.labelPosition` (0–1) of skeleton arrows places their label along the arrow, instead of halfway. [#12425](https://github.com/excalidraw/excalidraw/pull/12425)
|
||||
|
||||
|
||||
@@ -39,7 +39,10 @@ import { tryParseSpreadsheet } from "../charts";
|
||||
import { parseClipboard, parseDataTransferEvent } from "../clipboard";
|
||||
import { ImageURLToFile, SVGStringToFile } from "../data/blob";
|
||||
import { t } from "../i18n";
|
||||
import { isMaybeMermaidDefinition } from "../mermaid";
|
||||
import {
|
||||
isMaybeMermaidDefinition,
|
||||
MERMAID_TO_EXCALIDRAW_CONFIG,
|
||||
} from "../mermaid";
|
||||
import { withBatchedUpdates } from "../reactUtils";
|
||||
import { getShortcutKey } from "../shortcut";
|
||||
import { scrollBoundsIntoView } from "../viewport";
|
||||
@@ -210,7 +213,10 @@ export class AppClipboard {
|
||||
// if this fails to load (e.g. offline), fall through and paste as text
|
||||
const api = await import("@excalidraw/mermaid-to-excalidraw");
|
||||
const { elements: skeletonElements, files = {} } =
|
||||
await api.parseMermaidToExcalidraw(data.text);
|
||||
await api.parseMermaidToExcalidraw(
|
||||
data.text,
|
||||
MERMAID_TO_EXCALIDRAW_CONFIG,
|
||||
);
|
||||
|
||||
const elements = convertToExcalidrawElements(skeletonElements, {
|
||||
regenerateIds: true,
|
||||
|
||||
@@ -66,6 +66,18 @@ const getErrorMessage = (error: unknown): string => {
|
||||
return "";
|
||||
};
|
||||
|
||||
/** a link to the Mermaid syntax docs of a diagram type */
|
||||
const mermaidSyntaxLink = (page: string) => (el: React.ReactNode) =>
|
||||
(
|
||||
<a
|
||||
href={`https://mermaid.js.org/syntax/${page}.html`}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{el}
|
||||
</a>
|
||||
);
|
||||
|
||||
const MermaidToExcalidraw = ({
|
||||
mermaidToExcalidrawLib,
|
||||
onRetryLoad,
|
||||
@@ -253,42 +265,14 @@ const MermaidToExcalidraw = ({
|
||||
<div className="ttd-dialog-desc">
|
||||
<Trans
|
||||
i18nKey="mermaid.description"
|
||||
flowchartLink={(el) => (
|
||||
<a
|
||||
href="https://mermaid.js.org/syntax/flowchart.html"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{el}
|
||||
</a>
|
||||
)}
|
||||
sequenceLink={(el) => (
|
||||
<a
|
||||
href="https://mermaid.js.org/syntax/sequenceDiagram.html"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{el}
|
||||
</a>
|
||||
)}
|
||||
classLink={(el) => (
|
||||
<a
|
||||
href="https://mermaid.js.org/syntax/classDiagram.html"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{el}
|
||||
</a>
|
||||
)}
|
||||
erdLink={(el) => (
|
||||
<a
|
||||
href="https://mermaid.js.org/syntax/entityRelationshipDiagram.html"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{el}
|
||||
</a>
|
||||
)}
|
||||
flowchartLink={mermaidSyntaxLink("flowchart")}
|
||||
sequenceLink={mermaidSyntaxLink("sequenceDiagram")}
|
||||
classLink={mermaidSyntaxLink("classDiagram")}
|
||||
stateLink={mermaidSyntaxLink("stateDiagram")}
|
||||
erdLink={mermaidSyntaxLink("entityRelationshipDiagram")}
|
||||
ganttLink={mermaidSyntaxLink("gantt")}
|
||||
quadrantLink={mermaidSyntaxLink("quadrantChart")}
|
||||
timelineLink={mermaidSyntaxLink("timeline")}
|
||||
/>
|
||||
</div>
|
||||
<TTDDialogPanels>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { addEventListener } from "@excalidraw/common";
|
||||
|
||||
import { useUIAppState } from "../../context/ui-appState";
|
||||
import { t } from "../../i18n";
|
||||
import { MERMAID_TO_EXCALIDRAW_CONFIG } from "../../mermaid";
|
||||
import { useApp } from "../App";
|
||||
import { Dialog } from "../Dialog";
|
||||
import { withInternalFallback } from "../hoc/withInternalFallback";
|
||||
@@ -55,7 +56,10 @@ const loadMermaidToExcalidrawLib = (
|
||||
).then((lib) => ({
|
||||
// Pele lazy-loads a chunk per diagram type, which can fail to load
|
||||
// (e.g. offline) even though the lib itself loaded fine
|
||||
parseMermaidToExcalidraw: async (definition, config) => {
|
||||
parseMermaidToExcalidraw: async (
|
||||
definition,
|
||||
config = MERMAID_TO_EXCALIDRAW_CONFIG,
|
||||
) => {
|
||||
try {
|
||||
return await lib.parseMermaidToExcalidraw(definition, config);
|
||||
} catch (error) {
|
||||
|
||||
@@ -47,7 +47,7 @@ export const TTDDialogOutput = ({
|
||||
const errorMessage = parseError
|
||||
? hideErrorDetails
|
||||
? t("chat.errors.mermaidParseError")
|
||||
: formatMermaidParseErrorMessage(parseError.message, sourceText)
|
||||
: formatMermaidParseErrorMessage(parseError.message)
|
||||
: null;
|
||||
const syntaxGuidance =
|
||||
parseError && !hideErrorDetails
|
||||
|
||||
@@ -24,7 +24,6 @@ import { TTDChatPanel } from "./Chat/TTDChatPanel";
|
||||
import { TTDPreviewPanel } from "./TTDPreviewPanel";
|
||||
|
||||
import { getLastAssistantMessage } from "./utils/chat";
|
||||
import { withMermaidSourceLineNumber } from "./utils/mermaidError";
|
||||
|
||||
import type { BinaryFiles } from "../../types";
|
||||
import type {
|
||||
@@ -152,10 +151,7 @@ const TextToDiagramContent = ({
|
||||
|
||||
const handleAiRepairClick = async (message: TChat.ChatMessage) => {
|
||||
const mermaidContent = message.content || "";
|
||||
const errorMessage = withMermaidSourceLineNumber(
|
||||
message.error || "",
|
||||
mermaidContent,
|
||||
);
|
||||
const errorMessage = message.error || "";
|
||||
|
||||
if (!mermaidContent) {
|
||||
return;
|
||||
|
||||
@@ -31,20 +31,6 @@ Expecting 'SEMI', 'NEWLINE', 'SPACE', got 'NODE_STRING'`;
|
||||
expect(formatMermaidParseErrorMessage(message)).toBe(message);
|
||||
});
|
||||
|
||||
it("reports the error on its source line, given the source", () => {
|
||||
expect(
|
||||
formatMermaidParseErrorMessage(
|
||||
`Parse error on line 3:
|
||||
C --> D]
|
||||
---------^
|
||||
Expecting 'SEMI', got 'SQE'`,
|
||||
"%% a\n%% b\nflowchart TD\n A --> B\n C --> D]",
|
||||
),
|
||||
).toBe(`Parse error on line 5:
|
||||
C --> D]
|
||||
---------^`);
|
||||
});
|
||||
|
||||
it("does not modify non-Mermaid parse messages", () => {
|
||||
const message =
|
||||
"Unexpected token while parsing JSON. Expecting value at position 10.";
|
||||
@@ -120,35 +106,13 @@ describe("getMermaidInactiveParticipant", () => {
|
||||
});
|
||||
|
||||
describe("getMermaidErrorLineNumber", () => {
|
||||
it("reports errors after the last line on the last line", () => {
|
||||
it("keeps the line as reported, since Pele numbers source lines", () => {
|
||||
expect(
|
||||
getMermaidErrorLineNumber(
|
||||
"Parse error on line 4: ...",
|
||||
"graph TD\nsubgraph A\n A1[Start]\n",
|
||||
"Parse error on line 5: ...",
|
||||
"%% a\n%% b\nflowchart TD\n A --> B\n C --> D]",
|
||||
),
|
||||
).toBe(3);
|
||||
});
|
||||
|
||||
it("maps lines as Mermaid and Pele number them to the source's", () => {
|
||||
// [source, the line they report the error on line `actual` on]
|
||||
const cases: Array<[string, number, number]> = [
|
||||
["%% a\n%% b\nflowchart TD\n A --> B\n C --> D]", 3, 5],
|
||||
["flowchart TD\n %% c\n A --> B\n C --> D]", 3, 4],
|
||||
["\n\nflowchart TD\n A --> B\n C --> D]", 3, 5],
|
||||
["flowchart TD\n\n A --> B\n\n C --> D]", 5, 5],
|
||||
["---\ntitle: x\n---\nflowchart TD\n A --> B\n C --> D]", 3, 6],
|
||||
["%%{init: {'theme':'dark'}}%%\nflowchart TD\n C --> D]", 2, 3],
|
||||
["flowchart TD\n %%\n A --> B\n C --> D]", 4, 4],
|
||||
// a flowchart line ending with a comment runs on into the next one
|
||||
["flowchart TD\n A --> B %% note\n C --> D]", 2, 2],
|
||||
["sequenceDiagram\n Alice->>Bob: hi %% note\n Alice-B: x", 3, 3],
|
||||
["classDiagram\n %% c\n A <|-- B\n A <|-|> C", 3, 4],
|
||||
];
|
||||
for (const [source, reported, actual] of cases) {
|
||||
expect(
|
||||
getMermaidErrorLineNumber(`Parse error on line ${reported}:`, source),
|
||||
).toBe(actual);
|
||||
}
|
||||
).toBe(5);
|
||||
});
|
||||
|
||||
it("reports unknown diagram types on the line declaring it", () => {
|
||||
|
||||
@@ -136,73 +136,9 @@ export const getMermaidErrorLineNumber = (
|
||||
}
|
||||
return getInactiveParticipantLineNumber(message, sourceText);
|
||||
}
|
||||
const line = Number.parseInt(match[1], 10);
|
||||
return sourceText ? getMermaidSourceLineNumber(line, sourceText) : line;
|
||||
};
|
||||
|
||||
/**
|
||||
* The (0-based) indexes of the source lines the lines Mermaid (and Pele)
|
||||
* number errors by start at. They parse a definition without its front
|
||||
* matter, directives, comment lines and leading blank lines, and in
|
||||
* flowcharts and ER diagrams, a line ending with a comment runs on into the
|
||||
* next one.
|
||||
*/
|
||||
const getParsedLineStarts = (sourceText: string) => {
|
||||
const lines = sourceText.split(/\r?\n/);
|
||||
const type = getMermaidDiagramType(sourceText);
|
||||
const runsOnAfterComments = type === "flowchart" || type === "er";
|
||||
const starts: number[] = [];
|
||||
let index = 0;
|
||||
if (lines[0]?.trim() === "---") {
|
||||
const end = lines.findIndex((line, i) => i > 0 && line.trim() === "---");
|
||||
if (end !== -1) {
|
||||
index = end + 1;
|
||||
}
|
||||
}
|
||||
let runsOn = false;
|
||||
for (; index < lines.length; index++) {
|
||||
const line = lines[index];
|
||||
// a directive, possibly over several lines
|
||||
if (/^\s*%%\{/.test(line)) {
|
||||
while (index < lines.length - 1 && !/\}%%/.test(lines[index])) {
|
||||
index++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (/^\s*%%(?!\{)./.test(line) || (!starts.length && !line.trim())) {
|
||||
continue;
|
||||
}
|
||||
if (!runsOn) {
|
||||
starts.push(index);
|
||||
}
|
||||
runsOn = runsOnAfterComments && /\S.*%%/.test(line);
|
||||
}
|
||||
return starts;
|
||||
};
|
||||
|
||||
/**
|
||||
* The line of the source an error Mermaid (or Pele) reports on `line` is on:
|
||||
* errors past its end (e.g. a missing `end`) are on its last non-empty line.
|
||||
*/
|
||||
export const getMermaidSourceLineNumber = (
|
||||
line: number,
|
||||
sourceText: string,
|
||||
) => {
|
||||
const starts = getParsedLineStarts(sourceText);
|
||||
if (line >= 1 && line <= starts.length) {
|
||||
let index = starts[line - 1];
|
||||
// e.g. the empty line Mermaid reports an error at the end on
|
||||
while (index > 0 && !sourceText.split(/\r?\n/)[index].trim()) {
|
||||
index--;
|
||||
}
|
||||
return index + 1;
|
||||
}
|
||||
const lines = sourceText.split(/\r?\n/);
|
||||
let lastLine = lines.length;
|
||||
while (lastLine > 1 && !lines[lastLine - 1].trim()) {
|
||||
lastLine--;
|
||||
}
|
||||
return lastLine;
|
||||
// Pele reports errors on the source line (counting front matter, comments
|
||||
// and blank lines), and errors past the end on the last line
|
||||
return Number.parseInt(match[1], 10);
|
||||
};
|
||||
|
||||
const countMatches = (text: string, re: RegExp) =>
|
||||
@@ -377,32 +313,11 @@ export const getMermaidSyntaxErrorGuidance = (
|
||||
};
|
||||
};
|
||||
|
||||
/** The error, reported on its line of `sourceText` (see `getMermaidSourceLineNumber`) */
|
||||
export const withMermaidSourceLineNumber = (
|
||||
message: string,
|
||||
sourceText: string,
|
||||
) =>
|
||||
message.replace(MERMAID_SYNTAX_ERROR_LINE, (match, line) =>
|
||||
match.replace(
|
||||
line,
|
||||
String(getMermaidSourceLineNumber(Number(line), sourceText)),
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* The parse error, without the tokens it expected, and if given the source,
|
||||
* on its line of it.
|
||||
*/
|
||||
export const formatMermaidParseErrorMessage = (
|
||||
message: string,
|
||||
sourceText?: string,
|
||||
) => {
|
||||
/** The parse error, without the tokens it expected */
|
||||
export const formatMermaidParseErrorMessage = (message: string) => {
|
||||
if (!isMermaidParseSyntaxError(message)) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const formatted = message.replace(/\n\s*Expecting[\s\S]*$/, "").trimEnd();
|
||||
return sourceText
|
||||
? withMermaidSourceLineNumber(formatted, sourceText)
|
||||
: formatted;
|
||||
return message.replace(/\n\s*Expecting[\s\S]*$/, "").trimEnd();
|
||||
};
|
||||
@@ -677,7 +677,7 @@
|
||||
"mermaid": {
|
||||
"title": "Mermaid to Excalidraw",
|
||||
"button": "Insert",
|
||||
"description": "Currently only <flowchartLink>Flowchart</flowchartLink>, <sequenceLink>Sequence</sequenceLink>, <classLink>Class</classLink>, and <erdLink>Entity Relationship</erdLink> Diagrams are supported. The other types will be rendered as image in Excalidraw.",
|
||||
"description": "<flowchartLink>Flowcharts</flowchartLink>, <sequenceLink>Sequence</sequenceLink>, <classLink>Class</classLink>, <stateLink>State</stateLink> and <erdLink>Entity Relationship</erdLink> diagrams, <ganttLink>Gantt charts</ganttLink>, <quadrantLink>Quadrant charts</quadrantLink> and <timelineLink>Timelines</timelineLink> are converted to Excalidraw elements. Other types are inserted as an image.",
|
||||
"syntax": "Mermaid Syntax",
|
||||
"preview": "Preview",
|
||||
"label": "Mermaid",
|
||||
|
||||
@@ -10,7 +10,43 @@ describe("isMaybeMermaidDefinition", () => {
|
||||
expect(
|
||||
isMaybeMermaidDefinition("swimlane-beta LR\n subgraph Customer"),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("should skip comments, directives and front matter before the diagram type", () => {
|
||||
expect(
|
||||
isMaybeMermaidDefinition(
|
||||
"%% model: x\n%% prompt: 54. A hackathon\ngantt\n title A",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isMaybeMermaidDefinition(
|
||||
"\n%%{init: {\n 'theme': 'dark'\n}}%%\n%% a\n\nsequenceDiagram",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isMaybeMermaidDefinition(
|
||||
"---\ntitle: A\nconfig:\n theme: dark\n---\nflowchart TD\n A --> B",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(isMaybeMermaidDefinition("---\r\ntitle: A\r\n---\r\npie")).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
expect(isMaybeMermaidDefinition("%% flowchart")).toBe(false);
|
||||
expect(isMaybeMermaidDefinition("%% a\nnot a diagram")).toBe(false);
|
||||
expect(isMaybeMermaidDefinition("---\nflowchart")).toBe(false);
|
||||
});
|
||||
|
||||
it("should check text with many unclosed directives in linear time", () => {
|
||||
// took seconds (quadratic) when each directive was searched for its end
|
||||
// to the end of the text
|
||||
const text = `${"%%{".padEnd(50, "x")}\n`.repeat(20000);
|
||||
const start = performance.now();
|
||||
expect(isMaybeMermaidDefinition(text)).toBe(false);
|
||||
expect(performance.now() - start).toBeLessThan(500);
|
||||
});
|
||||
|
||||
it("should return false for text that isn't a mermaid definition", () => {
|
||||
expect(isMaybeMermaidDefinition("graphs")).toBe(false);
|
||||
expect(isMaybeMermaidDefinition("this flowchart")).toBe(false);
|
||||
expect(isMaybeMermaidDefinition("this\nflowchart")).toBe(false);
|
||||
|
||||
@@ -1,35 +1,51 @@
|
||||
/** heuristically checks whether the text may be a mermaid diagram definition */
|
||||
export const isMaybeMermaidDefinition = (text: string) => {
|
||||
const chartTypes = [
|
||||
"flowchart",
|
||||
"graph",
|
||||
"sequenceDiagram",
|
||||
"classDiagram",
|
||||
"stateDiagram",
|
||||
"stateDiagram-v2",
|
||||
"erDiagram",
|
||||
"journey",
|
||||
"gantt",
|
||||
"pie",
|
||||
"quadrantChart",
|
||||
"requirementDiagram",
|
||||
"gitGraph",
|
||||
"C4Context",
|
||||
"mindmap",
|
||||
"timeline",
|
||||
"zenuml",
|
||||
"sankey",
|
||||
"xychart",
|
||||
"block",
|
||||
// `swimlane-beta`, which Pele (unlike Mermaid) renders, as a flowchart
|
||||
"swimlane",
|
||||
];
|
||||
import type { MermaidConfig } from "@excalidraw/mermaid-to-excalidraw";
|
||||
|
||||
const re = new RegExp(
|
||||
`^(?:%%{.*?}%%[\\s\\n]*)?\\b(?:${chartTypes
|
||||
.map((x) => `\\s*${x}(-beta)?`)
|
||||
.join("|")})\\b`,
|
||||
);
|
||||
|
||||
return re.test(text.trim());
|
||||
/**
|
||||
* How Mermaid diagrams are converted: flowchart edges as elbow arrows, which
|
||||
* are easier to edit (unless the definition sets its own curve in its front
|
||||
* matter or `%%{init}%%`).
|
||||
*/
|
||||
export const MERMAID_TO_EXCALIDRAW_CONFIG: MermaidConfig = {
|
||||
elbow: { flowchart: true },
|
||||
};
|
||||
|
||||
// What may precede a definition's diagram type: whitespace, front matter,
|
||||
// directives (`%%{...}%%`, possibly over several lines) and comment lines.
|
||||
// A directive doesn't run past the next `%%{`, so an unclosed one is searched
|
||||
// for its end only up to there, not to the end of the text, every time (which
|
||||
// is quadratic in the number of unclosed directives).
|
||||
const MERMAID_PREAMBLE =
|
||||
/^(?:\s|---[ \t]*\r?\n[\s\S]*?\r?\n[ \t]*---[ \t]*(?=\r?\n|$)|%%\{(?:(?!%%\{)[\s\S])*?\}%%|%%[^\n]*)*/;
|
||||
|
||||
const MERMAID_DIAGRAM_TYPES = [
|
||||
"flowchart",
|
||||
"graph",
|
||||
"sequenceDiagram",
|
||||
"classDiagram",
|
||||
"stateDiagram",
|
||||
"stateDiagram-v2",
|
||||
"erDiagram",
|
||||
"journey",
|
||||
"gantt",
|
||||
"pie",
|
||||
"quadrantChart",
|
||||
"requirementDiagram",
|
||||
"gitGraph",
|
||||
"C4Context",
|
||||
"mindmap",
|
||||
"timeline",
|
||||
"zenuml",
|
||||
"sankey",
|
||||
"xychart",
|
||||
"block",
|
||||
// `swimlane-beta`, which Pele (unlike Mermaid) renders, as a flowchart
|
||||
"swimlane",
|
||||
];
|
||||
|
||||
const MERMAID_DIAGRAM_TYPE = new RegExp(
|
||||
`^(?:${MERMAID_DIAGRAM_TYPES.join("|")})(-beta)?\\b`,
|
||||
);
|
||||
|
||||
/** heuristically checks whether the text may be a mermaid diagram definition */
|
||||
export const isMaybeMermaidDefinition = (text: string) =>
|
||||
MERMAID_DIAGRAM_TYPE.test(text.replace(MERMAID_PREAMBLE, ""));
|
||||
@@ -88,7 +88,7 @@
|
||||
"@excalidraw/element": "0.18.0",
|
||||
"@excalidraw/laser-pointer": "1.3.1",
|
||||
"@excalidraw/math": "0.18.0",
|
||||
"@excalidraw/mermaid-to-excalidraw": "3.1.0",
|
||||
"@excalidraw/mermaid-to-excalidraw": "3.6.0",
|
||||
"@excalidraw/random-username": "1.1.0",
|
||||
"browser-fs-access": "0.38.0",
|
||||
"canvas-roundrect-polyfill": "0.0.1",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { expect, vi } from "vitest";
|
||||
|
||||
import * as MermaidToExcalidrawLib from "@excalidraw/mermaid-to-excalidraw";
|
||||
import { pointFrom } from "@excalidraw/math";
|
||||
|
||||
import type { LocalPoint } from "@excalidraw/math";
|
||||
@@ -130,6 +131,12 @@ describe("Test <MermaidToExcalidraw/>", () => {
|
||||
const dialog = document.querySelector(".ttd-dialog")!;
|
||||
await waitFor(() => expect(dialog.querySelector("canvas")).not.toBeNull());
|
||||
expect(normalizeDialogSnapshot(dialog)).toMatchSnapshot();
|
||||
// flowchart edges as elbow arrows
|
||||
expect(
|
||||
MermaidToExcalidrawLib.parseMermaidToExcalidraw,
|
||||
).toHaveBeenCalledWith(expect.any(String), {
|
||||
elbow: { flowchart: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should show error in preview when mermaid library throws error", async () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`Test <MermaidToExcalidraw/> > should open mermaid popup when active tool is mermaid 1`] = `"<div class="Modal Dialog ttd-dialog" role="dialog" aria-modal="true" aria-labelledby="dialog-title"><div class="Modal__background"></div><div class="Modal__content" style="--max-width: 1520px;" tabindex="0"><div class="Island"><div class="Dialog__content"><div dir="ltr" data-orientation="horizontal" class="ttd-dialog-tabs-root"><p class="dialog-mermaid-title">Mermaid to Excalidraw</p><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:r4:-trigger-mermaid" id="radix-:r4:-content-mermaid" tabindex="0" class="ttd-dialog-content"><div class="ttd-dialog-desc">Currently only <a href="https://mermaid.js.org/syntax/flowchart.html" target="_blank" rel="noreferrer">Flowchart</a>, <a href="https://mermaid.js.org/syntax/sequenceDiagram.html" target="_blank" rel="noreferrer">Sequence</a>, <a href="https://mermaid.js.org/syntax/classDiagram.html" target="_blank" rel="noreferrer">Class</a>, and <a href="https://mermaid.js.org/syntax/entityRelationshipDiagram.html" target="_blank" rel="noreferrer">Entity Relationship</a> Diagrams are supported. The other types will be rendered as image in Excalidraw.</div><div class="ttd-dialog-panels"><div class="ttd-dialog-panel"><div class="ttd-dialog-panel-button-container invisible" style="justify-content: flex-start;"></div></div><div class="ttd-dialog-panel"><div class="ttd-dialog-output-wrapper "><div class="ttd-dialog-output-canvas-container"><div class="ttd-dialog-output-canvas-content"><canvas width="90" height="158" dir="ltr"></canvas></div></div></div><div class="ttd-dialog-panel-button-container" style="justify-content: flex-start;"><button type="button" class="excalidraw-button ttd-dialog-panel-button"><div class="">Insert<span><svg aria-hidden="true" focusable="false" role="img" viewBox="0 0 20 20" class="" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><g stroke-width="1.25"><path d="M4.16602 10H15.8327"></path><path d="M12.5 13.3333L15.8333 10"></path><path d="M12.5 6.66666L15.8333 9.99999"></path></g></svg></span></div></button><div class="ttd-dialog-submit-shortcut"><div class="ttd-dialog-submit-shortcut__key">Ctrl</div><div class="ttd-dialog-submit-shortcut__key">Enter</div></div></div></div></div></div></div></div></div></div></div>"`;
|
||||
exports[`Test <MermaidToExcalidraw/> > should open mermaid popup when active tool is mermaid 1`] = `"<div class="Modal Dialog ttd-dialog" role="dialog" aria-modal="true" aria-labelledby="dialog-title"><div class="Modal__background"></div><div class="Modal__content" style="--max-width: 1520px;" tabindex="0"><div class="Island"><div class="Dialog__content"><div dir="ltr" data-orientation="horizontal" class="ttd-dialog-tabs-root"><p class="dialog-mermaid-title">Mermaid to Excalidraw</p><div data-state="active" data-orientation="horizontal" role="tabpanel" aria-labelledby="radix-:r4:-trigger-mermaid" id="radix-:r4:-content-mermaid" tabindex="0" class="ttd-dialog-content"><div class="ttd-dialog-desc"><a href="https://mermaid.js.org/syntax/flowchart.html" target="_blank" rel="noreferrer">Flowcharts</a>, <a href="https://mermaid.js.org/syntax/sequenceDiagram.html" target="_blank" rel="noreferrer">Sequence</a>, <a href="https://mermaid.js.org/syntax/classDiagram.html" target="_blank" rel="noreferrer">Class</a>, <a href="https://mermaid.js.org/syntax/stateDiagram.html" target="_blank" rel="noreferrer">State</a> and <a href="https://mermaid.js.org/syntax/entityRelationshipDiagram.html" target="_blank" rel="noreferrer">Entity Relationship</a> diagrams, <a href="https://mermaid.js.org/syntax/gantt.html" target="_blank" rel="noreferrer">Gantt charts</a>, <a href="https://mermaid.js.org/syntax/quadrantChart.html" target="_blank" rel="noreferrer">Quadrant charts</a> and <a href="https://mermaid.js.org/syntax/timeline.html" target="_blank" rel="noreferrer">Timelines</a> are converted to Excalidraw elements. Other types are inserted as an image.</div><div class="ttd-dialog-panels"><div class="ttd-dialog-panel"><div class="ttd-dialog-panel-button-container invisible" style="justify-content: flex-start;"></div></div><div class="ttd-dialog-panel"><div class="ttd-dialog-output-wrapper "><div class="ttd-dialog-output-canvas-container"><div class="ttd-dialog-output-canvas-content"><canvas width="90" height="158" dir="ltr"></canvas></div></div></div><div class="ttd-dialog-panel-button-container" style="justify-content: flex-start;"><button type="button" class="excalidraw-button ttd-dialog-panel-button"><div class="">Insert<span><svg aria-hidden="true" focusable="false" role="img" viewBox="0 0 20 20" class="" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"><g stroke-width="1.25"><path d="M4.16602 10H15.8327"></path><path d="M12.5 13.3333L15.8333 10"></path><path d="M12.5 6.66666L15.8333 9.99999"></path></g></svg></span></div></button><div class="ttd-dialog-submit-shortcut"><div class="ttd-dialog-submit-shortcut__key">Ctrl</div><div class="ttd-dialog-submit-shortcut__key">Enter</div></div></div></div></div></div></div></div></div></div></div>"`;
|
||||
|
||||
exports[`Test <MermaidToExcalidraw/> > should show error in preview when mermaid library throws error 1`] = `
|
||||
"flowchart TD
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import React from "react";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import * as MermaidToExcalidrawLib from "@excalidraw/mermaid-to-excalidraw";
|
||||
|
||||
import { getLineHeightInPx } from "@excalidraw/element";
|
||||
|
||||
import {
|
||||
@@ -801,6 +803,9 @@ describe("clipboard - pasting mermaid definition", () => {
|
||||
]),
|
||||
);
|
||||
});
|
||||
expect(
|
||||
MermaidToExcalidrawLib.parseMermaidToExcalidraw,
|
||||
).toHaveBeenCalledWith(text, { elbow: { flowchart: true } });
|
||||
});
|
||||
|
||||
it("should support directives", async () => {
|
||||
|
||||
@@ -1481,14 +1481,14 @@
|
||||
resolved "https://registry.yarnpkg.com/@excalidraw/markdown-to-text/-/markdown-to-text-0.1.2.tgz#1703705e7da608cf478f17bfe96fb295f55a23eb"
|
||||
integrity sha512-1nDXBNAojfi3oSFwJswKREkFm5wrSjqay81QlyRv2pkITG/XYB5v+oChENVBQLcxQwX4IUATWvXM5BcaNhPiIg==
|
||||
|
||||
"@excalidraw/mermaid-to-excalidraw@3.1.0":
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@excalidraw/mermaid-to-excalidraw/-/mermaid-to-excalidraw-3.1.0.tgz#352c9eaf7a56fa0e55015df70cc96c97fe7e8d20"
|
||||
integrity sha512-B3H6aXm4f4O4+f7k1z/Eu1xNj3ZurnCX9RQNzjx00RCfnanLY4z8Pj/1r76FP8yZ1Ue84z50gJoRFCUIZy5xDg==
|
||||
"@excalidraw/mermaid-to-excalidraw@3.6.0":
|
||||
version "3.6.0"
|
||||
resolved "https://registry.yarnpkg.com/@excalidraw/mermaid-to-excalidraw/-/mermaid-to-excalidraw-3.6.0.tgz#fbf422ef01932d953f82a443b06f72ea51646fa0"
|
||||
integrity sha512-h21Mr9FxmfLDds78iw0iTRDzes+/CDRFJ5kndFAmH+EVQLG6PHoMyn4Lms1dHiYgDTal+MkBJnqKJC9Xj080uA==
|
||||
dependencies:
|
||||
"@excalidraw/markdown-to-text" "0.1.2"
|
||||
nanoid "4.0.2"
|
||||
pele "1.8.0"
|
||||
pele "1.13.0"
|
||||
|
||||
"@excalidraw/prettier-config@1.0.2":
|
||||
version "1.0.2"
|
||||
@@ -7625,10 +7625,10 @@ pathval@^2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/pathval/-/pathval-2.0.0.tgz#7e2550b422601d4f6b8e26f1301bc8f15a741a25"
|
||||
integrity sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==
|
||||
|
||||
pele@1.8.0:
|
||||
version "1.8.0"
|
||||
resolved "https://registry.yarnpkg.com/pele/-/pele-1.8.0.tgz#85853ea6207dd619a2cc24592483a5360c1474b9"
|
||||
integrity sha512-AYmHttE70cwNJVXdfPMCANGivFlmc3bGGpMG0GqH5S5vE3RgtEuLlkqbhRfe94Csv/ZEd1TUbv2dMUHpw4p1Bg==
|
||||
pele@1.13.0:
|
||||
version "1.13.0"
|
||||
resolved "https://registry.yarnpkg.com/pele/-/pele-1.13.0.tgz#5a14f20b809a5ae61b04cd8825a4940550675d11"
|
||||
integrity sha512-Ie8UKY9x00MsuR3jO6AzUwzK2Z6MIV3Tf4EDdITiuk8Uaz8K7g0R082WqsqNaSU8DdgkrRvZThzhit83mi/uiw==
|
||||
|
||||
pend@~1.2.0:
|
||||
version "1.2.0"
|
||||
|
||||
Reference in new issue
Block a user