paseo-plugin-mermaid 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OVERVIEW.md +14 -2
- package/README.md +78 -30
- package/client/diagram.tsx +263 -2
- package/client/file-panel.tsx +2 -1
- package/index.client.tsx +17 -6
- package/package.json +1 -1
- package/shared/classic-shapes.ts +126 -0
- package/shared/drawable.ts +30 -10
- package/shared/fence.ts +10 -0
- package/shared/file-view.ts +6 -8
- package/shared/flowchart.ts +298 -41
- package/shared/layout.ts +47 -7
- package/shared/segment.ts +104 -42
- package/shared/transform.ts +55 -12
package/shared/flowchart.ts
CHANGED
|
@@ -9,7 +9,21 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
export type Direction = "TD" | "TB" | "BT" | "LR" | "RL";
|
|
12
|
-
export type NodeShape =
|
|
12
|
+
export type NodeShape =
|
|
13
|
+
| "rect"
|
|
14
|
+
| "round"
|
|
15
|
+
| "stadium"
|
|
16
|
+
| "subroutine"
|
|
17
|
+
| "diamond"
|
|
18
|
+
| "circle"
|
|
19
|
+
| "asymmetric"
|
|
20
|
+
| "cylinder"
|
|
21
|
+
| "hexagon"
|
|
22
|
+
| "lean-right"
|
|
23
|
+
| "lean-left"
|
|
24
|
+
| "trapezoid"
|
|
25
|
+
| "trapezoid-top"
|
|
26
|
+
| "double-circle";
|
|
13
27
|
export type EdgeStyle = "solid" | "dotted" | "thick";
|
|
14
28
|
|
|
15
29
|
export interface FlowNode {
|
|
@@ -32,6 +46,12 @@ export interface Flowchart {
|
|
|
32
46
|
edges: FlowEdge[];
|
|
33
47
|
/** Lines the parser chose not to interpret, so the UI can say so. */
|
|
34
48
|
skipped: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Every shape a mention gave, once each, in the order first given, including one a
|
|
51
|
+
* later mention replaced. What the source has asked for so far only ever grows as
|
|
52
|
+
* lines arrive, which a decision made while a diagram streams can rely on.
|
|
53
|
+
*/
|
|
54
|
+
shapes: NodeShape[];
|
|
35
55
|
}
|
|
36
56
|
|
|
37
57
|
const DIRECTIVE = /%%\{/;
|
|
@@ -47,60 +67,269 @@ const OPERATORS: { token: string; style: EdgeStyle; arrow: boolean }[] = [
|
|
|
47
67
|
{ token: "---", style: "solid", arrow: false },
|
|
48
68
|
];
|
|
49
69
|
|
|
50
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Tried in order, and the first whose opener sits after the id and whose closer ends
|
|
72
|
+
* the text wins, so a longer opener goes before any shorter one it starts with:
|
|
73
|
+
* `(((` before `((`, `[(`, `[/` and `[\` before `[`, `{{` before `{`. The slash
|
|
74
|
+
* shapes share an opener and are told apart by the closer: `[/ /]` leans right,
|
|
75
|
+
* `[\ \]` leans left, `[/ \]` is a trapezoid and `[\ /]` its upside-down twin.
|
|
76
|
+
*/
|
|
77
|
+
export const OPENERS: readonly { open: string; close: string; shape: NodeShape }[] = [
|
|
78
|
+
{ open: "(((", close: ")))", shape: "double-circle" },
|
|
51
79
|
{ open: "([", close: "])", shape: "stadium" },
|
|
52
80
|
{ open: "[[", close: "]]", shape: "subroutine" },
|
|
81
|
+
{ open: "[(", close: ")]", shape: "cylinder" },
|
|
53
82
|
{ open: "((", close: "))", shape: "circle" },
|
|
83
|
+
{ open: "{{", close: "}}", shape: "hexagon" },
|
|
84
|
+
{ open: "[/", close: "/]", shape: "lean-right" },
|
|
85
|
+
{ open: "[\\", close: "\\]", shape: "lean-left" },
|
|
86
|
+
{ open: "[/", close: "\\]", shape: "trapezoid" },
|
|
87
|
+
{ open: "[\\", close: "/]", shape: "trapezoid-top" },
|
|
54
88
|
{ open: "[", close: "]", shape: "rect" },
|
|
55
89
|
{ open: "(", close: ")", shape: "round" },
|
|
56
90
|
{ open: "{", close: "}", shape: "diamond" },
|
|
57
91
|
{ open: ">", close: "]", shape: "asymmetric" },
|
|
58
92
|
];
|
|
59
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Mermaid 11.16's short names and aliases for the shapes this plugin draws, for
|
|
96
|
+
* `id@{ shape: <name> }`. Every other name (`cloud`, `bolt`, `doc`, ...) is a shape it
|
|
97
|
+
* does not draw, and a Map keeps `constructor` from counting as a name.
|
|
98
|
+
*/
|
|
99
|
+
const SHAPE_NAMES: ReadonlyMap<string, NodeShape> = new Map(
|
|
100
|
+
(
|
|
101
|
+
[
|
|
102
|
+
["rect", ["rect", "proc", "process", "rectangle"]],
|
|
103
|
+
["round", ["rounded", "event"]],
|
|
104
|
+
["stadium", ["stadium", "terminal", "pill"]],
|
|
105
|
+
["subroutine", ["fr-rect", "subprocess", "subproc", "framed-rectangle", "subroutine"]],
|
|
106
|
+
["cylinder", ["cyl", "db", "database", "cylinder"]],
|
|
107
|
+
["circle", ["circle", "circ"]],
|
|
108
|
+
["diamond", ["diam", "decision", "diamond", "question"]],
|
|
109
|
+
["hexagon", ["hex", "hexagon", "prepare"]],
|
|
110
|
+
["lean-right", ["lean-r", "lean-right", "in-out"]],
|
|
111
|
+
["lean-left", ["lean-l", "lean-left", "out-in"]],
|
|
112
|
+
["trapezoid", ["trap-b", "priority", "trapezoid-bottom", "trapezoid"]],
|
|
113
|
+
["trapezoid-top", ["trap-t", "manual", "trapezoid-top", "inv-trapezoid"]],
|
|
114
|
+
["double-circle", ["dbl-circ", "double-circle"]],
|
|
115
|
+
] as [NodeShape, string[]][]
|
|
116
|
+
).flatMap(([shape, names]) => names.map((name): [string, NodeShape] => [name, shape])),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
/** The shape a Mermaid 11 `@{ shape: <name> }` name stands for, or null when this plugin does not draw it. Names are lower case, as in Mermaid. */
|
|
120
|
+
export function shapeFromName(name: string): NodeShape | null {
|
|
121
|
+
return SHAPE_NAMES.get(name) ?? null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ShapeField {
|
|
125
|
+
key: string;
|
|
126
|
+
value: string;
|
|
127
|
+
quoted: boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Reads the `key: value, key: value }` that follows an `@{`, from `from`, and says where
|
|
132
|
+
* the `}` ends. Keys are any word, so the span of an object this plugin will refuse
|
|
133
|
+
* (`img`, `icon`, `pos`, ...) is still found whole. Mermaid's lexer treats only `"` as
|
|
134
|
+
* opening a string: a double-quoted value may hold `,` `:` `{` and `}`, but in a
|
|
135
|
+
* single-quoted or an unquoted value a `}` ends the object and a `"` opens a string
|
|
136
|
+
* that never closes, so a single-quoted value holding either is null. An unquoted value
|
|
137
|
+
* runs to the next `,` or `}` and may hold none of the characters YAML or Mermaid would
|
|
138
|
+
* read as more structure. Anything else, from a missing space after the colon to a
|
|
139
|
+
* trailing comma, a backslash escape or an unclosed quote, is null: an object read
|
|
140
|
+
* halfway would draw the rest of it as stray text.
|
|
141
|
+
*/
|
|
142
|
+
export function scanShapeObject(text: string, from: number): { fields: ShapeField[]; end: number } | null {
|
|
143
|
+
const fields: ShapeField[] = [];
|
|
144
|
+
let index = from;
|
|
145
|
+
const skipSpace = () => {
|
|
146
|
+
while (index < text.length && /\s/.test(text[index])) index += 1;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
for (;;) {
|
|
150
|
+
skipSpace();
|
|
151
|
+
const key = /^[A-Za-z][\w-]*/.exec(text.slice(index));
|
|
152
|
+
if (!key) return null;
|
|
153
|
+
index += key[0].length;
|
|
154
|
+
if (text[index] !== ":" || !/\s/.test(text[index + 1] ?? "")) return null;
|
|
155
|
+
index += 1;
|
|
156
|
+
skipSpace();
|
|
157
|
+
|
|
158
|
+
const quote = text[index];
|
|
159
|
+
let value: string;
|
|
160
|
+
let quoted = false;
|
|
161
|
+
if (quote === '"' || quote === "'") {
|
|
162
|
+
const close = text.indexOf(quote, index + 1);
|
|
163
|
+
if (close < 0) return null;
|
|
164
|
+
value = text.slice(index + 1, close);
|
|
165
|
+
// `\"` and `\n` mean something else in a double-quoted YAML string than they do here.
|
|
166
|
+
if (quote === '"' && value.includes("\\")) return null;
|
|
167
|
+
// Mermaid ignores single quotes, so its object ends at a `}` here and a `"` starts a string.
|
|
168
|
+
if (quote === "'" && /[}"]/.test(value)) return null;
|
|
169
|
+
quoted = true;
|
|
170
|
+
index = close + 1;
|
|
171
|
+
} else {
|
|
172
|
+
const stop = text.slice(index).search(/[,}]/);
|
|
173
|
+
if (stop < 0) return null;
|
|
174
|
+
value = text.slice(index, index + stop).trim();
|
|
175
|
+
if (value.length === 0 || /["'`{[\]#]|:(?:\s|$)/.test(value)) return null;
|
|
176
|
+
index += stop;
|
|
177
|
+
}
|
|
178
|
+
fields.push({ key: key[0], value, quoted });
|
|
179
|
+
|
|
180
|
+
skipSpace();
|
|
181
|
+
if (text[index] === "}") return { fields, end: index + 1 };
|
|
182
|
+
if (text[index] !== ",") return null;
|
|
183
|
+
index += 1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Where each readable `@{ ... }` starts and ends in a line, so a `-->` or a `--` inside one is not mistaken for an edge. */
|
|
188
|
+
function shapeObjectSpans(line: string): [number, number][] {
|
|
189
|
+
const spans: [number, number][] = [];
|
|
190
|
+
let from = 0;
|
|
191
|
+
for (;;) {
|
|
192
|
+
const at = line.indexOf("@{", from);
|
|
193
|
+
if (at < 0) return spans;
|
|
194
|
+
const object = scanShapeObject(line, at + 2);
|
|
195
|
+
if (object) {
|
|
196
|
+
spans.push([at, object.end]);
|
|
197
|
+
from = object.end;
|
|
198
|
+
} else {
|
|
199
|
+
from = at + 2;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
60
204
|
/**
|
|
61
205
|
* Mermaid writes an edge label two ways. Rewriting `A -- yes --> B` into
|
|
62
206
|
* `A -->|yes| B` lets the splitter handle one form.
|
|
63
207
|
*/
|
|
64
208
|
export function normalizeEdgeLabels(line: string): string {
|
|
65
|
-
|
|
209
|
+
// A shape object's text is a label, not edge syntax, so the rewrite never reaches into one.
|
|
210
|
+
let result = "";
|
|
211
|
+
let from = 0;
|
|
212
|
+
for (const [start, end] of shapeObjectSpans(line)) {
|
|
213
|
+
result += normalizeOutsideShapeObjects(line.slice(from, start)) + line.slice(start, end);
|
|
214
|
+
from = end;
|
|
215
|
+
}
|
|
216
|
+
return result + normalizeOutsideShapeObjects(line.slice(from));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function normalizeOutsideShapeObjects(text: string): string {
|
|
220
|
+
return text
|
|
66
221
|
.replace(/--\s+([^->|]+?)\s+-->/g, "-->|$1|")
|
|
67
222
|
.replace(/-\.\s+([^.|>]+?)\s+\.->/g, "-.->|$1|")
|
|
68
223
|
.replace(/==\s+([^=|>]+?)\s+==>/g, "==>|$1|");
|
|
69
224
|
}
|
|
70
225
|
|
|
226
|
+
function breakLines(value: string): string {
|
|
227
|
+
return value.replace(/<br\s*\/?>/gi, "\n").trim();
|
|
228
|
+
}
|
|
229
|
+
|
|
71
230
|
function stripQuotes(value: string): string {
|
|
72
231
|
const trimmed = value.trim();
|
|
73
232
|
const quoted = /^"(.*)"$/s.exec(trimmed) ?? /^'(.*)'$/s.exec(trimmed);
|
|
74
|
-
return (quoted ? quoted[1] : trimmed)
|
|
233
|
+
return breakLines(quoted ? quoted[1] : trimmed);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** `"`text`"`: Mermaid's Markdown string, which this plugin does not format. */
|
|
237
|
+
function isMarkdownString(inner: string): boolean {
|
|
238
|
+
return /^`[\s\S]*`$/.test(inner.trim());
|
|
75
239
|
}
|
|
76
240
|
|
|
77
241
|
/**
|
|
78
242
|
* The label inside a shape's brackets, or null when the brackets hold syntax this
|
|
79
243
|
* plugin would draw wrong. Refusing it puts the line among the skipped ones, which
|
|
80
|
-
*
|
|
244
|
+
* sends the diagram to Mermaid (`drawable`).
|
|
81
245
|
*
|
|
82
246
|
* A quoted label may hold anything but its own quote and a Markdown string. Unquoted,
|
|
83
247
|
* a square bracket or the shape's own closing bracket means a second node run into
|
|
84
|
-
* this one (`A[x] & B[y]`, `A(x); B(y)`), a label wrapped whole in brackets is
|
|
85
|
-
* shape (`[(
|
|
86
|
-
* or a trapezoid
|
|
248
|
+
* this one (`A[x] & B[y]`, `A(x); B(y)`), a label wrapped whole in brackets is a second
|
|
249
|
+
* shape nested in the first (`A[([x])]`, `A(({x}))`), and slashes at both ends are a
|
|
250
|
+
* parallelogram or a trapezoid nested in it. The shapes that bracket syntax itself
|
|
251
|
+
* names (`[(db)]`, `{{hex}}`, `(((x)))`, `[/in/]`) are read as openers before a label
|
|
252
|
+
* is looked at. Other brackets inside the text, as in `A[Reload (beta)]`, stay part
|
|
87
253
|
* of the label.
|
|
88
254
|
*/
|
|
89
255
|
function labelOf(inner: string, close: string): string | null {
|
|
90
256
|
const trimmed = inner.trim();
|
|
91
257
|
const quoted = /^"([^"]*)"$/.exec(trimmed) ?? /^'([^']*)'$/.exec(trimmed);
|
|
92
|
-
if (quoted) return
|
|
258
|
+
if (quoted) return isMarkdownString(quoted[1]) ? null : stripQuotes(trimmed);
|
|
93
259
|
if (/[[\]]/.test(trimmed) || [...close].some((char) => trimmed.includes(char))) return null;
|
|
94
260
|
if (/^[([{][\s\S]*[)\]}]$/.test(trimmed)) return null;
|
|
95
261
|
if (/^[/\\][\s\S]*[/\\]$/.test(trimmed)) return null;
|
|
96
262
|
return stripQuotes(trimmed);
|
|
97
263
|
}
|
|
98
264
|
|
|
99
|
-
/**
|
|
265
|
+
/**
|
|
266
|
+
* A node as one statement wrote it. A node is mentioned many times in a diagram and
|
|
267
|
+
* Mermaid keeps the last shape and the last label any mention gave, so what the text
|
|
268
|
+
* actually said matters: a bare `A` gives neither, `A@{ shape: db }` gives only a shape.
|
|
269
|
+
*/
|
|
270
|
+
export interface NodeMention {
|
|
271
|
+
node: FlowNode;
|
|
272
|
+
/** The text carried a label; otherwise `node.label` is just the id. */
|
|
273
|
+
labelled: boolean;
|
|
274
|
+
/** The text carried a shape; otherwise `node.shape` is just the default `rect`. */
|
|
275
|
+
shaped: boolean;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The shape and the label field of a shape object this plugin reads: only the `shape`
|
|
280
|
+
* and `label` keys, each once, and a shape name it draws. Anything else (another key, an
|
|
281
|
+
* unknown shape, a Markdown string label) is null.
|
|
282
|
+
*/
|
|
283
|
+
export function readShapeFields(fields: ShapeField[]): { shape: NodeShape; label: ShapeField | undefined } | null {
|
|
284
|
+
const values = new Map<string, ShapeField>();
|
|
285
|
+
for (const field of fields) {
|
|
286
|
+
if (field.key !== "shape" && field.key !== "label") return null;
|
|
287
|
+
if (values.has(field.key)) return null;
|
|
288
|
+
values.set(field.key, field);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const name = values.get("shape");
|
|
292
|
+
const shape = name ? shapeFromName(name.value) : null;
|
|
293
|
+
if (shape === null) return null;
|
|
294
|
+
|
|
295
|
+
const label = values.get("label");
|
|
296
|
+
if (label?.quoted && isMarkdownString(label.value)) return null;
|
|
297
|
+
return { shape, label };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Reads `id@{ shape: cyl, label: "Orders" }`, where `at` is the index of the `@{`. The
|
|
302
|
+
* object has to be the whole of the text, and `readShapeFields` has to accept it. Anything
|
|
303
|
+
* else (another key, an unknown shape, a node run in after it with `&`) is null, so the
|
|
304
|
+
* line counts as skipped.
|
|
305
|
+
*/
|
|
306
|
+
function parseShapeObject(text: string, at: number, id: string): NodeMention | null {
|
|
307
|
+
const object = scanShapeObject(text, at + 2);
|
|
308
|
+
if (!object || text.slice(object.end).trim().length > 0) return null;
|
|
309
|
+
|
|
310
|
+
const read = readShapeFields(object.fields);
|
|
311
|
+
if (!read) return null;
|
|
312
|
+
|
|
313
|
+
const label = read.label ? breakLines(read.label.value) : "";
|
|
314
|
+
return { node: { id, label: label.length > 0 ? label : id, shape: read.shape }, labelled: label.length > 0, shaped: true };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Reads `id`, `id[Label]`, `id{Decision}`, `id((Circle))`, `id@{ shape: cyl }` and friends. */
|
|
100
318
|
export function parseNodeExpression(raw: string): FlowNode | null {
|
|
319
|
+
return readNodeMention(raw)?.node ?? null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function readNodeMention(raw: string): NodeMention | null {
|
|
101
323
|
const text = raw.trim();
|
|
102
324
|
if (text.length === 0) return null;
|
|
103
325
|
|
|
326
|
+
const at = text.indexOf("@{");
|
|
327
|
+
if (at > 0) {
|
|
328
|
+
const id = text.slice(0, at).trim();
|
|
329
|
+
// Not an id in front of it (`A["see @{ x }"]`): the bracket shapes below read it.
|
|
330
|
+
if (/^[\w.-]+$/.test(id)) return parseShapeObject(text, at, id);
|
|
331
|
+
}
|
|
332
|
+
|
|
104
333
|
for (const { open, close, shape } of OPENERS) {
|
|
105
334
|
const start = text.indexOf(open);
|
|
106
335
|
if (start <= 0) continue;
|
|
@@ -110,11 +339,11 @@ export function parseNodeExpression(raw: string): FlowNode | null {
|
|
|
110
339
|
// The longest opener that fits is the shape meant; a label it cannot read is final.
|
|
111
340
|
const label = labelOf(text.slice(start + open.length, text.length - close.length), close);
|
|
112
341
|
if (label === null) return null;
|
|
113
|
-
return { id, label: label.length > 0 ? label : id, shape };
|
|
342
|
+
return { node: { id, label: label.length > 0 ? label : id, shape }, labelled: label.length > 0, shaped: true };
|
|
114
343
|
}
|
|
115
344
|
|
|
116
345
|
if (!/^[\w.-]+$/.test(text)) return null;
|
|
117
|
-
return { id: text, label: text, shape: "rect" };
|
|
346
|
+
return { node: { id: text, label: text, shape: "rect" }, labelled: false, shaped: false };
|
|
118
347
|
}
|
|
119
348
|
|
|
120
349
|
interface Split {
|
|
@@ -126,12 +355,14 @@ interface Split {
|
|
|
126
355
|
export function splitChain(line: string): Split | null {
|
|
127
356
|
const parts: string[] = [];
|
|
128
357
|
const operators: Split["operators"] = [];
|
|
129
|
-
|
|
358
|
+
// Operators are looked for in a copy where each `@{ ... }` is blanked out, so one inside a quoted label is text.
|
|
359
|
+
const searchable = maskShapeObjects(line);
|
|
360
|
+
let cursor = 0;
|
|
130
361
|
|
|
131
362
|
for (;;) {
|
|
132
363
|
let best: { index: number; op: (typeof OPERATORS)[number] } | null = null;
|
|
133
364
|
for (const op of OPERATORS) {
|
|
134
|
-
const index =
|
|
365
|
+
const index = searchable.indexOf(op.token, cursor);
|
|
135
366
|
if (index < 0) continue;
|
|
136
367
|
if (!best || index < best.index || (index === best.index && op.token.length > best.op.token.length)) {
|
|
137
368
|
best = { index, op };
|
|
@@ -139,38 +370,71 @@ export function splitChain(line: string): Split | null {
|
|
|
139
370
|
}
|
|
140
371
|
if (!best) break;
|
|
141
372
|
|
|
142
|
-
parts.push(
|
|
143
|
-
let
|
|
373
|
+
parts.push(line.slice(cursor, best.index));
|
|
374
|
+
let next = best.index + best.op.token.length;
|
|
144
375
|
|
|
145
376
|
let label: string | null = null;
|
|
146
|
-
const labelled = /^\s*\|([^|]*)\|/.exec(
|
|
377
|
+
const labelled = /^\s*\|([^|]*)\|/.exec(line.slice(next));
|
|
147
378
|
if (labelled) {
|
|
148
379
|
label = stripQuotes(labelled[1]);
|
|
149
|
-
|
|
380
|
+
next += labelled[0].length;
|
|
150
381
|
}
|
|
151
382
|
|
|
152
383
|
operators.push({ style: best.op.style, arrow: best.op.arrow, label });
|
|
153
|
-
|
|
384
|
+
cursor = next;
|
|
154
385
|
}
|
|
155
386
|
|
|
156
387
|
if (operators.length === 0) return null;
|
|
157
|
-
parts.push(
|
|
388
|
+
parts.push(line.slice(cursor));
|
|
158
389
|
return { parts, operators };
|
|
159
390
|
}
|
|
160
391
|
|
|
392
|
+
/** `line` with each readable `@{ ... }` overwritten by `_`, same length, so indexes still match the original and no operator can be found inside one. */
|
|
393
|
+
function maskShapeObjects(line: string): string {
|
|
394
|
+
let masked = line;
|
|
395
|
+
for (const [start, end] of shapeObjectSpans(line)) {
|
|
396
|
+
masked = masked.slice(0, start) + "_".repeat(end - start) + masked.slice(end);
|
|
397
|
+
}
|
|
398
|
+
return masked;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* One statement as the parser reads it: its node mentions in the order written and the
|
|
403
|
+
* operators between them, or null when the parser skips the line. `line` is one line of
|
|
404
|
+
* a flowchart without its comment, and not a header, subgraph or styling line.
|
|
405
|
+
*/
|
|
406
|
+
export function readStatement(line: string): { mentions: NodeMention[]; operators: Split["operators"] } | null {
|
|
407
|
+
const chain = splitChain(normalizeEdgeLabels(line));
|
|
408
|
+
if (!chain) {
|
|
409
|
+
const single = readNodeMention(line.replace(/;$/, ""));
|
|
410
|
+
return single ? { mentions: [single], operators: [] } : null;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const mentions: NodeMention[] = [];
|
|
414
|
+
for (const part of chain.parts) {
|
|
415
|
+
const mention = readNodeMention(part.replace(/;$/, ""));
|
|
416
|
+
if (!mention) return null;
|
|
417
|
+
mentions.push(mention);
|
|
418
|
+
}
|
|
419
|
+
return { mentions, operators: chain.operators };
|
|
420
|
+
}
|
|
421
|
+
|
|
161
422
|
export function parseFlowchart(source: string): Flowchart | null {
|
|
162
423
|
const lines = source.split("\n");
|
|
163
424
|
let direction: Direction | null = null;
|
|
164
425
|
const nodes = new Map<string, FlowNode>();
|
|
165
426
|
const edges: FlowEdge[] = [];
|
|
166
427
|
const skipped: string[] = [];
|
|
428
|
+
const shapes = new Set<NodeShape>();
|
|
167
429
|
|
|
168
|
-
|
|
430
|
+
// Last wins, as in Mermaid: a mention that gives a label replaces the label, one that gives a shape replaces the shape, and a bare id changes neither.
|
|
431
|
+
const remember = ({ node, labelled, shaped }: NodeMention) => {
|
|
432
|
+
if (shaped) shapes.add(node.shape);
|
|
169
433
|
const existing = nodes.get(node.id);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
434
|
+
nodes.set(
|
|
435
|
+
node.id,
|
|
436
|
+
existing ? { id: node.id, label: labelled ? node.label : existing.label, shape: shaped ? node.shape : existing.shape } : node,
|
|
437
|
+
);
|
|
174
438
|
};
|
|
175
439
|
|
|
176
440
|
for (const original of lines) {
|
|
@@ -202,26 +466,19 @@ export function parseFlowchart(source: string): Flowchart | null {
|
|
|
202
466
|
continue;
|
|
203
467
|
}
|
|
204
468
|
|
|
205
|
-
const
|
|
206
|
-
if (!
|
|
207
|
-
const single = parseNodeExpression(line.replace(/;$/, ""));
|
|
208
|
-
if (single) remember(single);
|
|
209
|
-
else skipped.push(original.trim());
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const parsed = chain.parts.map((part) => parseNodeExpression(part.replace(/;$/, "")));
|
|
214
|
-
if (parsed.some((node) => node === null)) {
|
|
469
|
+
const statement = readStatement(line);
|
|
470
|
+
if (!statement) {
|
|
215
471
|
skipped.push(original.trim());
|
|
216
472
|
continue;
|
|
217
473
|
}
|
|
218
474
|
|
|
219
|
-
|
|
220
|
-
for (
|
|
221
|
-
|
|
475
|
+
const { mentions, operators } = statement;
|
|
476
|
+
for (const mention of mentions) remember(mention);
|
|
477
|
+
for (let index = 0; index < operators.length; index += 1) {
|
|
478
|
+
const operator = operators[index];
|
|
222
479
|
edges.push({
|
|
223
|
-
from:
|
|
224
|
-
to:
|
|
480
|
+
from: mentions[index].node.id,
|
|
481
|
+
to: mentions[index + 1].node.id,
|
|
225
482
|
label: operator.label,
|
|
226
483
|
style: operator.style,
|
|
227
484
|
arrow: operator.arrow,
|
|
@@ -230,5 +487,5 @@ export function parseFlowchart(source: string): Flowchart | null {
|
|
|
230
487
|
}
|
|
231
488
|
|
|
232
489
|
if (direction === null) return null;
|
|
233
|
-
return { direction, nodes: [...nodes.values()], edges, skipped };
|
|
490
|
+
return { direction, nodes: [...nodes.values()], edges, skipped, shapes: [...shapes] };
|
|
234
491
|
}
|
package/shared/layout.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Direction, Flowchart, FlowEdge, FlowNode } from "./flowchart";
|
|
1
|
+
import type { Direction, Flowchart, FlowEdge, FlowNode, NodeShape } from "./flowchart";
|
|
2
2
|
import type { SeqEvent } from "./sequence";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -48,6 +48,29 @@ const NODE_GAP = 22;
|
|
|
48
48
|
const LINE = 1.5;
|
|
49
49
|
/** Breadth reserved for each lane an edge that skips a layer runs through. */
|
|
50
50
|
const LANE_GAP = 26;
|
|
51
|
+
/** Height of one elliptical cap of a cylinder; the box reserves three of them (the lid's two arcs and the curved base). */
|
|
52
|
+
export const CYLINDER_CAP = 6;
|
|
53
|
+
/** Space between the two rings of a double circle. */
|
|
54
|
+
export const DOUBLE_RING_GAP = 4;
|
|
55
|
+
|
|
56
|
+
/** How far a parallelogram or trapezoid leans: two fifths of its height. */
|
|
57
|
+
export const slantFor = (height: number): number => Math.round(height * 0.4);
|
|
58
|
+
|
|
59
|
+
const LEANING: ReadonlySet<NodeShape> = new Set(["lean-right", "lean-left"]);
|
|
60
|
+
const TAPERED: ReadonlySet<NodeShape> = new Set(["trapezoid", "trapezoid-top"]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How far the sides of a node sit inside its box at mid-height, where a
|
|
64
|
+
* horizontal edge meets it. A parallelogram or trapezoid slants by `slantFor`,
|
|
65
|
+
* so at half the height the side has moved half that; every other shape either
|
|
66
|
+
* fills its box there or, like a hexagon, reaches it with a point.
|
|
67
|
+
*
|
|
68
|
+
* Relies on `measureNode` keeping these shapes at their base height: the slant
|
|
69
|
+
* is taken from the laid-out height, so a taller box would shift the contact.
|
|
70
|
+
*/
|
|
71
|
+
export function sideInset(node: LaidOutNode): number {
|
|
72
|
+
return LEANING.has(node.shape) || TAPERED.has(node.shape) ? slantFor(node.height) / 2 : 0;
|
|
73
|
+
}
|
|
51
74
|
|
|
52
75
|
export function measureNode(node: FlowNode): { width: number; height: number } {
|
|
53
76
|
const lines = node.label.split("\n");
|
|
@@ -65,6 +88,19 @@ export function measureNode(node: FlowNode): { width: number; height: number } {
|
|
|
65
88
|
const side = Math.max(width, height);
|
|
66
89
|
return { width: side, height: side };
|
|
67
90
|
}
|
|
91
|
+
// The outer ring sits DOUBLE_RING_GAP outside the inner one, so the box grows by
|
|
92
|
+
// a gap on every side of the circle that holds the label.
|
|
93
|
+
if (node.shape === "double-circle") {
|
|
94
|
+
const side = Math.max(width, height) + 2 * DOUBLE_RING_GAP;
|
|
95
|
+
return { width: side, height: side };
|
|
96
|
+
}
|
|
97
|
+
// Room for the lid's two arcs and the curved base, so the label keeps its own height.
|
|
98
|
+
if (node.shape === "cylinder") return { width, height: height + 3 * CYLINDER_CAP };
|
|
99
|
+
// A hexagon's points reach out by half the height on both sides.
|
|
100
|
+
if (node.shape === "hexagon") return { width: width + height, height };
|
|
101
|
+
// The slant eats into the label's room, so the box widens to give it back.
|
|
102
|
+
if (LEANING.has(node.shape)) return { width: width + slantFor(height), height };
|
|
103
|
+
if (TAPERED.has(node.shape)) return { width: width + 2 * slantFor(height), height };
|
|
68
104
|
return { width, height };
|
|
69
105
|
}
|
|
70
106
|
|
|
@@ -249,8 +285,10 @@ function route(
|
|
|
249
285
|
|
|
250
286
|
if (across) {
|
|
251
287
|
const forward = to.x >= from.x;
|
|
252
|
-
|
|
253
|
-
|
|
288
|
+
// A slanted side is inset at mid-height, where the edge runs; starting at the
|
|
289
|
+
// box edge would leave a gap between the line and the shape.
|
|
290
|
+
const startX = forward ? from.x + from.width - sideInset(from) : from.x + sideInset(from);
|
|
291
|
+
const endX = forward ? to.x + sideInset(to) : to.x + to.width - sideInset(to);
|
|
254
292
|
const startY = from.y + from.height / 2;
|
|
255
293
|
const endY = to.y + to.height / 2;
|
|
256
294
|
const midX = (startX + endX) / 2;
|
|
@@ -299,12 +337,14 @@ function routeThroughLane(
|
|
|
299
337
|
lane: number,
|
|
300
338
|
): LaidOutEdge {
|
|
301
339
|
if (across) {
|
|
302
|
-
|
|
303
|
-
|
|
340
|
+
// The lane's verticals stay clear of the boxes, measured from the box edges as
|
|
341
|
+
// before; only the stubs into and out of a node reach in to a slanted side.
|
|
342
|
+
const outX = from.x + from.width + LANE_GAP / 2;
|
|
343
|
+
const inX = to.x - LANE_GAP / 2;
|
|
344
|
+
const startX = from.x + from.width - sideInset(from);
|
|
345
|
+
const endX = to.x + sideInset(to);
|
|
304
346
|
const startY = from.y + from.height / 2;
|
|
305
347
|
const endY = to.y + to.height / 2;
|
|
306
|
-
const outX = startX + LANE_GAP / 2;
|
|
307
|
-
const inX = endX - LANE_GAP / 2;
|
|
308
348
|
|
|
309
349
|
return {
|
|
310
350
|
...edge,
|