yuhwa 1.2.0 → 1.3.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/dist/astro.d.ts +1 -1
- package/dist/browser.d.ts +3 -2
- package/dist/browser.js +3 -2
- package/dist/cli.js +1 -1
- package/dist/editor/assets/index-DAk6b1UB.js +689 -0
- package/dist/editor/assets/{lsp.worker-sZtzIA6-.js → lsp.worker-CMSpDTsX.js} +17 -17
- package/dist/editor/index.html +1 -1
- package/dist/{figure-CA2hy0xh.d.ts → figure-QAwSU1Ry.d.ts} +1 -1
- package/dist/index-6ZPJGThY.d.ts +216 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -1
- package/dist/ironmark.d.ts +1 -1
- package/dist/ironmark.js +1 -1
- package/dist/lsp-browser.js +1 -1
- package/dist/lsp-node.js +2 -2
- package/dist/lsp-server.js +1 -1
- package/dist/lsp.js +1 -1
- package/dist/markdown-it.d.ts +1 -1
- package/dist/markdown-it.js +1 -1
- package/dist/{node-Bzn2uKXZ.js → node-CLu2Pp_2.js} +2 -1
- package/dist/plugin-Bs4eiAuz.js +4166 -0
- package/dist/{index-CnnoZYl0.d.ts → plugin-NSkNyoUj.d.ts} +79 -208
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +1 -0
- package/dist/remark.d.ts +1 -1
- package/dist/remark.js +1 -1
- package/dist/{server-Dxo7OG8K.js → server-D6BSNC1P.js} +1 -1
- package/dist/{service-B1fpOg_L.js → service-D7DNWBAU.js} +3 -2
- package/dist/{shared-Ddw88O9D.js → shared-6aCU_HaV.js} +489 -401
- package/dist/{src-DkQj7aIV.js → src-BjOhvTAB.js} +440 -4512
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +3 -2
- package/dist/vuepress.d.ts +1 -1
- package/dist/yuhwa.iife.js +53 -53
- package/package.json +6 -1
- package/dist/editor/assets/index-BphHLTvA.js +0 -689
|
@@ -0,0 +1,4166 @@
|
|
|
1
|
+
// src/core/guard.ts
|
|
2
|
+
let sink = null;
|
|
3
|
+
/** run `fn` with plugin failures reported into `diags` */
|
|
4
|
+
function collecting(diags, fn) {
|
|
5
|
+
const prev = sink;
|
|
6
|
+
sink = diags;
|
|
7
|
+
try {
|
|
8
|
+
return fn();
|
|
9
|
+
} finally {
|
|
10
|
+
sink = prev;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** a thrown value as text */
|
|
14
|
+
const errorText = (e) => e instanceof Error ? e.message : String(e);
|
|
15
|
+
/** the default: an error on the diagram being processed, once per message */
|
|
16
|
+
const toSink = (message) => {
|
|
17
|
+
if (sink && !sink.some((d) => d.message === message)) sink.push({
|
|
18
|
+
line: 1,
|
|
19
|
+
message,
|
|
20
|
+
severity: "error"
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
/** `fn`, but a throw is reported and returns `fallback(...args)` */
|
|
24
|
+
function guard(plugin, what, fn, fallback, report = toSink) {
|
|
25
|
+
return function(...args) {
|
|
26
|
+
try {
|
|
27
|
+
return fn.apply(this, args);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
report(`plugin "${plugin}": ${what} failed: ${errorText(e)}`, args);
|
|
30
|
+
return fallback(...args);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** who registered each kind, by `"<what> <name>"` */
|
|
35
|
+
const owners = /* @__PURE__ */ new Map();
|
|
36
|
+
/** record that `plugin` registered `what` `name`; replacing another plugin's is allowed, but worth a word */
|
|
37
|
+
function claim(what, name, plugin) {
|
|
38
|
+
const key = `${what} ${name}`;
|
|
39
|
+
const prev = owners.get(key);
|
|
40
|
+
if (prev && prev !== plugin) console.warn(`yuhwa: plugin "${plugin}" replaces ${what} "${name}" of plugin "${prev}"`);
|
|
41
|
+
owners.set(key, plugin);
|
|
42
|
+
}
|
|
43
|
+
/** forget every owner, when `unuse` rebuilds the tables */
|
|
44
|
+
const clearOwners = () => owners.clear();
|
|
45
|
+
/** the plugin that registered `what` `name`, if one did */
|
|
46
|
+
const ownerOf = (what, name) => owners.get(`${what} ${name}`);
|
|
47
|
+
/**
|
|
48
|
+
* `o` without a prototype: a lookup by a name from the source (`theme:
|
|
49
|
+
* constructor`, `$toString`) then finds only the table's own keys.
|
|
50
|
+
*/
|
|
51
|
+
const table = (o) => Object.setPrototypeOf(o, null);
|
|
52
|
+
// src/core/theme.ts
|
|
53
|
+
/** Colour ramps: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950] */
|
|
54
|
+
const RAMPS = table({
|
|
55
|
+
slate: [
|
|
56
|
+
"#f8fafc",
|
|
57
|
+
"#f1f5f9",
|
|
58
|
+
"#e2e8f0",
|
|
59
|
+
"#cbd5e1",
|
|
60
|
+
"#94a3b8",
|
|
61
|
+
"#64748b",
|
|
62
|
+
"#475569",
|
|
63
|
+
"#334155",
|
|
64
|
+
"#1e293b",
|
|
65
|
+
"#0f172a",
|
|
66
|
+
"#020617"
|
|
67
|
+
],
|
|
68
|
+
gray: [
|
|
69
|
+
"#fafaf9",
|
|
70
|
+
"#f5f5f4",
|
|
71
|
+
"#e7e5e4",
|
|
72
|
+
"#d6d3d1",
|
|
73
|
+
"#a8a29e",
|
|
74
|
+
"#78716c",
|
|
75
|
+
"#57534e",
|
|
76
|
+
"#44403c",
|
|
77
|
+
"#292524",
|
|
78
|
+
"#1c1917",
|
|
79
|
+
"#0c0a09"
|
|
80
|
+
],
|
|
81
|
+
red: [
|
|
82
|
+
"#fff1f2",
|
|
83
|
+
"#ffe4e6",
|
|
84
|
+
"#fecdd3",
|
|
85
|
+
"#fda4af",
|
|
86
|
+
"#fb7185",
|
|
87
|
+
"#f43f5e",
|
|
88
|
+
"#e11d48",
|
|
89
|
+
"#be123c",
|
|
90
|
+
"#9f1239",
|
|
91
|
+
"#881337",
|
|
92
|
+
"#4c0519"
|
|
93
|
+
],
|
|
94
|
+
orange: [
|
|
95
|
+
"#fff7ed",
|
|
96
|
+
"#ffedd5",
|
|
97
|
+
"#fed7aa",
|
|
98
|
+
"#fdba74",
|
|
99
|
+
"#fb923c",
|
|
100
|
+
"#f97316",
|
|
101
|
+
"#ea580c",
|
|
102
|
+
"#c2410c",
|
|
103
|
+
"#9a3412",
|
|
104
|
+
"#7c2d12",
|
|
105
|
+
"#431407"
|
|
106
|
+
],
|
|
107
|
+
amber: [
|
|
108
|
+
"#fffbeb",
|
|
109
|
+
"#fef3c7",
|
|
110
|
+
"#fde68a",
|
|
111
|
+
"#fcd34d",
|
|
112
|
+
"#fbbf24",
|
|
113
|
+
"#f59e0b",
|
|
114
|
+
"#d97706",
|
|
115
|
+
"#b45309",
|
|
116
|
+
"#92400e",
|
|
117
|
+
"#78350f",
|
|
118
|
+
"#451a03"
|
|
119
|
+
],
|
|
120
|
+
lime: [
|
|
121
|
+
"#f7fee7",
|
|
122
|
+
"#ecfccb",
|
|
123
|
+
"#d9f99d",
|
|
124
|
+
"#bef264",
|
|
125
|
+
"#a3e635",
|
|
126
|
+
"#84cc16",
|
|
127
|
+
"#65a30d",
|
|
128
|
+
"#4d7c0f",
|
|
129
|
+
"#3f6212",
|
|
130
|
+
"#365314",
|
|
131
|
+
"#1a2e05"
|
|
132
|
+
],
|
|
133
|
+
green: [
|
|
134
|
+
"#ecfdf5",
|
|
135
|
+
"#d1fae5",
|
|
136
|
+
"#a7f3d0",
|
|
137
|
+
"#6ee7b7",
|
|
138
|
+
"#34d399",
|
|
139
|
+
"#10b981",
|
|
140
|
+
"#059669",
|
|
141
|
+
"#047857",
|
|
142
|
+
"#065f46",
|
|
143
|
+
"#064e3b",
|
|
144
|
+
"#022c22"
|
|
145
|
+
],
|
|
146
|
+
teal: [
|
|
147
|
+
"#f0fdfa",
|
|
148
|
+
"#ccfbf1",
|
|
149
|
+
"#99f6e4",
|
|
150
|
+
"#5eead4",
|
|
151
|
+
"#2dd4bf",
|
|
152
|
+
"#14b8a6",
|
|
153
|
+
"#0d9488",
|
|
154
|
+
"#0f766e",
|
|
155
|
+
"#115e59",
|
|
156
|
+
"#134e4a",
|
|
157
|
+
"#042f2e"
|
|
158
|
+
],
|
|
159
|
+
cyan: [
|
|
160
|
+
"#ecfeff",
|
|
161
|
+
"#cffafe",
|
|
162
|
+
"#a5f3fc",
|
|
163
|
+
"#67e8f9",
|
|
164
|
+
"#22d3ee",
|
|
165
|
+
"#06b6d4",
|
|
166
|
+
"#0891b2",
|
|
167
|
+
"#0e7490",
|
|
168
|
+
"#155e75",
|
|
169
|
+
"#164e63",
|
|
170
|
+
"#083344"
|
|
171
|
+
],
|
|
172
|
+
blue: [
|
|
173
|
+
"#eff6ff",
|
|
174
|
+
"#dbeafe",
|
|
175
|
+
"#bfdbfe",
|
|
176
|
+
"#93c5fd",
|
|
177
|
+
"#60a5fa",
|
|
178
|
+
"#3b82f6",
|
|
179
|
+
"#2563eb",
|
|
180
|
+
"#1d4ed8",
|
|
181
|
+
"#1e40af",
|
|
182
|
+
"#1e3a8a",
|
|
183
|
+
"#172554"
|
|
184
|
+
],
|
|
185
|
+
indigo: [
|
|
186
|
+
"#eef2ff",
|
|
187
|
+
"#e0e7ff",
|
|
188
|
+
"#c7d2fe",
|
|
189
|
+
"#a5b4fc",
|
|
190
|
+
"#818cf8",
|
|
191
|
+
"#6366f1",
|
|
192
|
+
"#4f46e5",
|
|
193
|
+
"#4338ca",
|
|
194
|
+
"#3730a3",
|
|
195
|
+
"#312e81",
|
|
196
|
+
"#1e1b4b"
|
|
197
|
+
],
|
|
198
|
+
purple: [
|
|
199
|
+
"#f5f3ff",
|
|
200
|
+
"#ede9fe",
|
|
201
|
+
"#ddd6fe",
|
|
202
|
+
"#c4b5fd",
|
|
203
|
+
"#a78bfa",
|
|
204
|
+
"#8b5cf6",
|
|
205
|
+
"#7c3aed",
|
|
206
|
+
"#6d28d9",
|
|
207
|
+
"#5b21b6",
|
|
208
|
+
"#4c1d95",
|
|
209
|
+
"#2e1065"
|
|
210
|
+
],
|
|
211
|
+
pink: [
|
|
212
|
+
"#fdf2f8",
|
|
213
|
+
"#fce7f3",
|
|
214
|
+
"#fbcfe8",
|
|
215
|
+
"#f9a8d4",
|
|
216
|
+
"#f472b6",
|
|
217
|
+
"#ec4899",
|
|
218
|
+
"#db2777",
|
|
219
|
+
"#be185d",
|
|
220
|
+
"#9d174d",
|
|
221
|
+
"#831843",
|
|
222
|
+
"#500724"
|
|
223
|
+
]
|
|
224
|
+
});
|
|
225
|
+
RAMPS.violet = RAMPS.purple;
|
|
226
|
+
RAMPS.rose = RAMPS.red;
|
|
227
|
+
RAMPS.emerald = RAMPS.green;
|
|
228
|
+
RAMPS.yellow = RAMPS.amber;
|
|
229
|
+
RAMPS.grey = RAMPS.gray;
|
|
230
|
+
RAMPS.sky = RAMPS.cyan;
|
|
231
|
+
/** true for a tone name such as `green` (aliases included) */
|
|
232
|
+
const isTone = (name) => Object.hasOwn(RAMPS, name);
|
|
233
|
+
const TONES = [
|
|
234
|
+
"slate",
|
|
235
|
+
"gray",
|
|
236
|
+
"red",
|
|
237
|
+
"orange",
|
|
238
|
+
"amber",
|
|
239
|
+
"lime",
|
|
240
|
+
"green",
|
|
241
|
+
"teal",
|
|
242
|
+
"cyan",
|
|
243
|
+
"blue",
|
|
244
|
+
"indigo",
|
|
245
|
+
"purple",
|
|
246
|
+
"pink"
|
|
247
|
+
];
|
|
248
|
+
const L = {
|
|
249
|
+
50: 0,
|
|
250
|
+
100: 1,
|
|
251
|
+
200: 2,
|
|
252
|
+
300: 3,
|
|
253
|
+
400: 4,
|
|
254
|
+
500: 5,
|
|
255
|
+
600: 6,
|
|
256
|
+
700: 7,
|
|
257
|
+
800: 8,
|
|
258
|
+
900: 9,
|
|
259
|
+
950: 10
|
|
260
|
+
};
|
|
261
|
+
function ramp(tone, level) {
|
|
262
|
+
return RAMPS[tone]?.[L[level]];
|
|
263
|
+
}
|
|
264
|
+
const SANS = "Inter, ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
|
|
265
|
+
const MONO = "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
|
|
266
|
+
const lightTone = (t) => RAMPS[t] && {
|
|
267
|
+
fill: ramp(t, 50),
|
|
268
|
+
stroke: ramp(t, 300),
|
|
269
|
+
text: ramp(t, 900)
|
|
270
|
+
};
|
|
271
|
+
const lightGroupTone = (t) => RAMPS[t] && {
|
|
272
|
+
fill: ramp(t, 50) + "b3",
|
|
273
|
+
stroke: ramp(t, 200),
|
|
274
|
+
text: ramp(t, 700)
|
|
275
|
+
};
|
|
276
|
+
const darkTone = (t) => RAMPS[t] && {
|
|
277
|
+
fill: ramp(t, 950),
|
|
278
|
+
stroke: ramp(t, 600),
|
|
279
|
+
text: ramp(t, 100)
|
|
280
|
+
};
|
|
281
|
+
const darkGroupTone = (t) => RAMPS[t] && {
|
|
282
|
+
fill: ramp(t, 950) + "80",
|
|
283
|
+
stroke: ramp(t, 800),
|
|
284
|
+
text: ramp(t, 300)
|
|
285
|
+
};
|
|
286
|
+
const THEMES = table({ paper: {
|
|
287
|
+
name: "paper",
|
|
288
|
+
dark: false,
|
|
289
|
+
background: "#fbfaf7",
|
|
290
|
+
font: SANS,
|
|
291
|
+
mono: false,
|
|
292
|
+
fontSize: 14,
|
|
293
|
+
node: {
|
|
294
|
+
fill: "#ffffff",
|
|
295
|
+
stroke: "#d9d4c7",
|
|
296
|
+
text: "#2a2925",
|
|
297
|
+
sub: "#7a766c"
|
|
298
|
+
},
|
|
299
|
+
group: {
|
|
300
|
+
fill: "#f3f1ea",
|
|
301
|
+
stroke: "#e4e0d4",
|
|
302
|
+
text: "#7a766c",
|
|
303
|
+
dash: null
|
|
304
|
+
},
|
|
305
|
+
edge: {
|
|
306
|
+
stroke: "#8f8a7e",
|
|
307
|
+
text: "#55524a",
|
|
308
|
+
labelBg: "#fbfaf7",
|
|
309
|
+
ink: "#55524a"
|
|
310
|
+
},
|
|
311
|
+
title: "#2a2925",
|
|
312
|
+
accent: "#2563eb",
|
|
313
|
+
strokeWidth: 1.25,
|
|
314
|
+
edgeWidth: 1.5,
|
|
315
|
+
radius: 10,
|
|
316
|
+
shadow: "rgba(60, 50, 30, 0.10)",
|
|
317
|
+
grid: null,
|
|
318
|
+
tone: lightTone,
|
|
319
|
+
groupTone: lightGroupTone,
|
|
320
|
+
edgeTone: (t) => ramp(t, 500)
|
|
321
|
+
} });
|
|
322
|
+
const THEME_NAMES = Object.keys(THEMES);
|
|
323
|
+
/** Add a theme (or replace one with the same name). Plugins call this through `use`. */
|
|
324
|
+
function registerTheme(input) {
|
|
325
|
+
const t = input;
|
|
326
|
+
t.edge.ink ??= t.edge.text;
|
|
327
|
+
THEMES[t.name] = t;
|
|
328
|
+
if (!THEME_NAMES.includes(t.name)) THEME_NAMES.push(t.name);
|
|
329
|
+
}
|
|
330
|
+
const HEX_RE = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
331
|
+
/** CSS named colours, as `name` + 6 hex digits, joined */
|
|
332
|
+
const NAMED = new Map("aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4 black 000000 blanchedalmond ffebcd blueviolet 8a2be2 brown a52a2a burlywood deb887 cadetblue 5f9ea0 chartreuse 7fff00 chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc crimson dc143c darkblue 00008b darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9 darkgreen 006400 darkkhaki bdb76b darkmagenta 8b008b darkolivegreen 556b2f darkorange ff8c00 darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f darkslateblue 483d8b darkslategray 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493 deepskyblue 00bfff dimgray 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0 forestgreen 228b22 fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 greenyellow adff2f honeydew f0fff0 hotpink ff69b4 indianred cd5c5c ivory fffff0 khaki f0e68c lavender e6e6fa lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd lightblue add8e6 lightcoral f08080 lightcyan e0ffff lightgoldenrodyellow fafad2 lightgray d3d3d3 lightgreen 90ee90 lightpink ffb6c1 lightsalmon ffa07a lightseagreen 20b2aa lightskyblue 87cefa lightslategray 778899 lightsteelblue b0c4de lightyellow ffffe0 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa mediumblue 0000cd mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee mediumspringgreen 00fa9a mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa mistyrose ffe4e1 moccasin ffe4b5 navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23 orangered ff4500 orchid da70d6 palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9 peru cd853f plum dda0dd powderblue b0e0e6 rebeccapurple 663399 rosybrown bc8f8f royalblue 4169e1 saddlebrown 8b4513 salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d silver c0c0c0 skyblue 87ceeb slateblue 6a5acd slategray 708090 snow fffafa springgreen 00ff7f steelblue 4682b4 tan d2b48c thistle d8bfd8 tomato ff6347 turquoise 40e0d0 wheat f5deb3 white ffffff whitesmoke f5f5f5 yellowgreen 9acd32".match(/\S+ \S+/g).map((x) => x.split(" ")));
|
|
333
|
+
const hex2 = (n) => Math.round(Math.min(255, Math.max(0, n))).toString(16).padStart(2, "0");
|
|
334
|
+
/** `rgb()` / `hsl()` channel: a number, a percentage of `max`, or an angle */
|
|
335
|
+
const channel = (v, max) => v.endsWith("%") ? parseFloat(v) / 100 * max : parseFloat(v);
|
|
336
|
+
/**
|
|
337
|
+
* Any CSS colour this library can mix, as `#rrggbb`: `#rgb`, `#rgba`,
|
|
338
|
+
* `#rrggbb(aa)`, `rgb()`/`rgba()`, `hsl()`/`hsla()` (comma or space syntax)
|
|
339
|
+
* and the CSS colour names. Palette names are not CSS colours: see `isColor`.
|
|
340
|
+
*/
|
|
341
|
+
function cssHex(v) {
|
|
342
|
+
const c = v.trim().toLowerCase();
|
|
343
|
+
const h = c.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/)?.[1];
|
|
344
|
+
if (h) return "#" + (h.length <= 4 ? h.slice(0, 3).split("").map((x) => x + x).join("") : h.slice(0, 6));
|
|
345
|
+
const fn = c.match(/^(rgba?|hsla?)\(\s*([^)]*)\)$/);
|
|
346
|
+
if (fn) {
|
|
347
|
+
const a = fn[2].split(/\s*[,/]\s*|\s+/).filter(Boolean);
|
|
348
|
+
if (a.length < 3 || a.slice(0, 3).some((x) => !Number.isFinite(parseFloat(x)))) return void 0;
|
|
349
|
+
if (fn[1].startsWith("rgb")) return "#" + a.slice(0, 3).map((x) => hex2(channel(x, 255))).join("");
|
|
350
|
+
const hue = (parseFloat(a[0]) % 360 + 360) % 360 / 360;
|
|
351
|
+
const sat = Math.min(1, channel(a[1], 1) / (a[1].endsWith("%") ? 1 : 100));
|
|
352
|
+
const lig = Math.min(1, channel(a[2], 1) / (a[2].endsWith("%") ? 1 : 100));
|
|
353
|
+
const q = lig < .5 ? lig * (1 + sat) : lig + sat - lig * sat;
|
|
354
|
+
const p = 2 * lig - q;
|
|
355
|
+
const k = (t) => {
|
|
356
|
+
t = (t + 1) % 1;
|
|
357
|
+
return t < 1 / 6 ? p + (q - p) * 6 * t : t < .5 ? q : t < 2 / 3 ? p + (q - p) * (2 / 3 - t) * 6 : p;
|
|
358
|
+
};
|
|
359
|
+
return "#" + [
|
|
360
|
+
hue + 1 / 3,
|
|
361
|
+
hue,
|
|
362
|
+
hue - 1 / 3
|
|
363
|
+
].map((t) => hex2(k(t) * 255)).join("");
|
|
364
|
+
}
|
|
365
|
+
const named = NAMED.get(c);
|
|
366
|
+
return named ? "#" + named : void 0;
|
|
367
|
+
}
|
|
368
|
+
const rgb = (hex) => {
|
|
369
|
+
const h = hex.length === 4 ? hex.slice(1).split("").map((c) => c + c).join("") : hex.slice(1);
|
|
370
|
+
return [
|
|
371
|
+
0,
|
|
372
|
+
2,
|
|
373
|
+
4
|
|
374
|
+
].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
375
|
+
};
|
|
376
|
+
/** `a` mixed into `b` by `t` (0..1), both hex */
|
|
377
|
+
const mix = (a, b, t) => "#" + rgb(a).map((v, i) => Math.round(v * t + rgb(b)[i] * (1 - t))).map((v) => v.toString(16).padStart(2, "0")).join("");
|
|
378
|
+
/** colours for any CSS colour (not a palette name): a tint of it on the theme background */
|
|
379
|
+
function hexTone(css, theme, group) {
|
|
380
|
+
const c = cssHex(css);
|
|
381
|
+
if (!c) return void 0;
|
|
382
|
+
const bg = HEX_RE.test(theme.background) ? theme.background : theme.dark ? "#000000" : "#ffffff";
|
|
383
|
+
const tint = theme.dark ? group ? .14 : .22 : group ? .07 : .12;
|
|
384
|
+
return {
|
|
385
|
+
fill: mix(c, bg, tint),
|
|
386
|
+
stroke: c,
|
|
387
|
+
text: theme.dark ? mix(c, "#ffffff", .45) : mix(c, "#000000", .55)
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
const withHex = /* @__PURE__ */ new WeakMap();
|
|
391
|
+
const customThemes = /* @__PURE__ */ new WeakMap();
|
|
392
|
+
/** a `true` / `false` prop value: `false`, `no`, `off`, `0` and `none` are false */
|
|
393
|
+
const bool = (v, d) => v === void 0 ? d : ![
|
|
394
|
+
"false",
|
|
395
|
+
"no",
|
|
396
|
+
"off",
|
|
397
|
+
"0",
|
|
398
|
+
"none"
|
|
399
|
+
].includes(v.toLowerCase());
|
|
400
|
+
const LINE_STYLES = [
|
|
401
|
+
"solid",
|
|
402
|
+
"dashed",
|
|
403
|
+
"dotted"
|
|
404
|
+
];
|
|
405
|
+
/** the group border dash for a line style; `fallback` when no style is set */
|
|
406
|
+
const groupDash = (style, fallback) => style === "dashed" ? "6 4" : style === "dotted" ? "1.5 4" : style === "solid" ? null : fallback;
|
|
407
|
+
/** a font setting (`mono`, `sans` or a family name) as a CSS font stack */
|
|
408
|
+
function fontStack(font) {
|
|
409
|
+
const mono = font === "mono" || /mono|code|courier/i.test(font);
|
|
410
|
+
if (font === "mono" || font === "sans") return {
|
|
411
|
+
font: mono ? MONO : SANS,
|
|
412
|
+
mono
|
|
413
|
+
};
|
|
414
|
+
return {
|
|
415
|
+
font: `${font}, ${mono ? "ui-monospace, monospace" : "ui-sans-serif, system-ui, sans-serif"}`,
|
|
416
|
+
mono
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* The keys a `theme name { … }` block (or `createTheme`) takes. Colours take
|
|
421
|
+
* palette names (`slate-800`) or any CSS colour.
|
|
422
|
+
*/
|
|
423
|
+
const THEME_KEYS = table({
|
|
424
|
+
base: {
|
|
425
|
+
info: "the theme to start from (default: paper)",
|
|
426
|
+
kind: "word"
|
|
427
|
+
},
|
|
428
|
+
dark: {
|
|
429
|
+
info: "true for a dark theme: palette colours pick dark shades",
|
|
430
|
+
kind: "word",
|
|
431
|
+
values: ["true", "false"]
|
|
432
|
+
},
|
|
433
|
+
background: {
|
|
434
|
+
info: "page colour, or none",
|
|
435
|
+
kind: "color",
|
|
436
|
+
values: ["none"]
|
|
437
|
+
},
|
|
438
|
+
font: {
|
|
439
|
+
info: "font family; a name with \"mono\" in it switches to monospace",
|
|
440
|
+
kind: "word",
|
|
441
|
+
values: ["sans", "mono"]
|
|
442
|
+
},
|
|
443
|
+
"font-size": {
|
|
444
|
+
info: "base text size in px",
|
|
445
|
+
kind: "number"
|
|
446
|
+
},
|
|
447
|
+
"node-fill": {
|
|
448
|
+
info: "box fill",
|
|
449
|
+
kind: "color"
|
|
450
|
+
},
|
|
451
|
+
"node-stroke": {
|
|
452
|
+
info: "box border",
|
|
453
|
+
kind: "color"
|
|
454
|
+
},
|
|
455
|
+
"node-text": {
|
|
456
|
+
info: "box label",
|
|
457
|
+
kind: "color"
|
|
458
|
+
},
|
|
459
|
+
"node-sub": {
|
|
460
|
+
info: "second line of a box (sub:)",
|
|
461
|
+
kind: "color"
|
|
462
|
+
},
|
|
463
|
+
"group-fill": {
|
|
464
|
+
info: "group background",
|
|
465
|
+
kind: "color",
|
|
466
|
+
values: ["none"]
|
|
467
|
+
},
|
|
468
|
+
"group-stroke": {
|
|
469
|
+
info: "group border",
|
|
470
|
+
kind: "color"
|
|
471
|
+
},
|
|
472
|
+
"group-text": {
|
|
473
|
+
info: "group label",
|
|
474
|
+
kind: "color"
|
|
475
|
+
},
|
|
476
|
+
"group-border": {
|
|
477
|
+
info: "group border style: solid, dashed or dotted",
|
|
478
|
+
kind: "word",
|
|
479
|
+
values: LINE_STYLES
|
|
480
|
+
},
|
|
481
|
+
"edge-color": {
|
|
482
|
+
info: "lines",
|
|
483
|
+
kind: "color"
|
|
484
|
+
},
|
|
485
|
+
"edge-text": {
|
|
486
|
+
info: "edge labels and panel ink",
|
|
487
|
+
kind: "color"
|
|
488
|
+
},
|
|
489
|
+
"label-bg": {
|
|
490
|
+
info: "background behind edge labels",
|
|
491
|
+
kind: "color"
|
|
492
|
+
},
|
|
493
|
+
"title-color": {
|
|
494
|
+
info: "title and panel headings",
|
|
495
|
+
kind: "color"
|
|
496
|
+
},
|
|
497
|
+
accent: {
|
|
498
|
+
info: "code spans, chips and highlights",
|
|
499
|
+
kind: "color"
|
|
500
|
+
},
|
|
501
|
+
radius: {
|
|
502
|
+
info: "corner radius in px",
|
|
503
|
+
kind: "number"
|
|
504
|
+
},
|
|
505
|
+
"stroke-width": {
|
|
506
|
+
info: "box border width",
|
|
507
|
+
kind: "number"
|
|
508
|
+
},
|
|
509
|
+
"edge-width": {
|
|
510
|
+
info: "line width",
|
|
511
|
+
kind: "number"
|
|
512
|
+
},
|
|
513
|
+
shadow: {
|
|
514
|
+
info: "box shadow colour, or none",
|
|
515
|
+
kind: "color",
|
|
516
|
+
values: ["none"]
|
|
517
|
+
},
|
|
518
|
+
grid: {
|
|
519
|
+
info: "background grid colour, or none",
|
|
520
|
+
kind: "color",
|
|
521
|
+
values: ["none"]
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
/**
|
|
525
|
+
* Build a theme from flat `key: value` props (see `THEME_KEYS`) on top of a
|
|
526
|
+
* base theme. Keys left out keep the base's value; palette colours in nodes
|
|
527
|
+
* and groups (`[blue]`) follow `dark`.
|
|
528
|
+
*
|
|
529
|
+
* ```ts
|
|
530
|
+
* use({ name: 'brand', themes: [createTheme('brand', { base: 'midnight', accent: '#ff5a1f' })] });
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
function createTheme(name, props, base) {
|
|
534
|
+
const b = base ?? THEMES[props.base] ?? THEMES.paper;
|
|
535
|
+
const dark = bool(props.dark, b.dark);
|
|
536
|
+
const col = (k, light, darkLevel) => resolveColor(props[k], b, dark ? darkLevel : light);
|
|
537
|
+
const n = (k, d) => {
|
|
538
|
+
const v = parseFloat(props[k]);
|
|
539
|
+
return Number.isFinite(v) && v >= 0 ? v : d;
|
|
540
|
+
};
|
|
541
|
+
const off = (k) => !bool(props[k], true);
|
|
542
|
+
const background = off("background") ? "none" : col("background", 50, 950) ?? b.background;
|
|
543
|
+
const edgeText = col("edge-text", 700, 300) ?? b.edge.text;
|
|
544
|
+
const flip = dark !== b.dark;
|
|
545
|
+
const font = props.font?.trim();
|
|
546
|
+
const stack = font ? fontStack(font) : {
|
|
547
|
+
font: b.font,
|
|
548
|
+
mono: b.mono
|
|
549
|
+
};
|
|
550
|
+
return {
|
|
551
|
+
name,
|
|
552
|
+
dark,
|
|
553
|
+
background,
|
|
554
|
+
font: stack.font,
|
|
555
|
+
mono: stack.mono,
|
|
556
|
+
fontSize: n("font-size", b.fontSize),
|
|
557
|
+
node: {
|
|
558
|
+
fill: col("node-fill", 50, 900) ?? b.node.fill,
|
|
559
|
+
stroke: col("node-stroke", 300, 600) ?? b.node.stroke,
|
|
560
|
+
text: col("node-text", 900, 100) ?? b.node.text,
|
|
561
|
+
sub: col("node-sub", 500, 400) ?? b.node.sub
|
|
562
|
+
},
|
|
563
|
+
group: {
|
|
564
|
+
fill: off("group-fill") ? "none" : col("group-fill", 50, 950) ?? b.group.fill,
|
|
565
|
+
stroke: col("group-stroke", 200, 800) ?? b.group.stroke,
|
|
566
|
+
text: col("group-text", 700, 300) ?? b.group.text,
|
|
567
|
+
dash: groupDash(props["group-border"], b.group.dash)
|
|
568
|
+
},
|
|
569
|
+
edge: {
|
|
570
|
+
stroke: col("edge-color", 500, 400) ?? b.edge.stroke,
|
|
571
|
+
text: edgeText,
|
|
572
|
+
labelBg: col("label-bg", 50, 950) ?? (b.edge.labelBg === b.background || props.background ? background : b.edge.labelBg),
|
|
573
|
+
labelStroke: b.edge.labelStroke,
|
|
574
|
+
ink: props["edge-text"] ? edgeText : col("edge-color", 600, 300) ?? b.edge.ink
|
|
575
|
+
},
|
|
576
|
+
title: col("title-color", 900, 100) ?? b.title,
|
|
577
|
+
accent: col("accent", 600, 400) ?? b.accent,
|
|
578
|
+
strokeWidth: n("stroke-width", b.strokeWidth),
|
|
579
|
+
edgeWidth: n("edge-width", b.edgeWidth),
|
|
580
|
+
radius: n("radius", b.radius),
|
|
581
|
+
shadow: off("shadow") ? null : col("shadow", 200, 950) ?? (flip ? dark ? "rgba(0, 0, 0, 0.45)" : "rgba(15, 23, 42, 0.10)" : b.shadow),
|
|
582
|
+
grid: off("grid") ? null : col("grid", 200, 800) ?? b.grid,
|
|
583
|
+
tone: flip ? dark ? darkTone : lightTone : b.tone,
|
|
584
|
+
groupTone: flip ? dark ? darkGroupTone : lightGroupTone : b.groupTone,
|
|
585
|
+
edgeTone: flip ? (t) => ramp(t, dark ? 400 : 500) : b.edgeTone
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* The themes a diagram defines (`theme name { … }`), built on their bases.
|
|
590
|
+
* A base may be another theme of the same diagram; cycles fall back to paper.
|
|
591
|
+
*/
|
|
592
|
+
function customTheme(name, custom, seen = []) {
|
|
593
|
+
const props = custom[name];
|
|
594
|
+
const baseName = props.base ?? (THEMES[name] ? name : void 0);
|
|
595
|
+
return createTheme(name, props, baseName && custom[baseName] && baseName !== name && !seen.includes(baseName) ? customTheme(baseName, custom, [...seen, name]) : THEMES[baseName ?? "paper"] ?? THEMES.paper);
|
|
596
|
+
}
|
|
597
|
+
/** the theme names a diagram can use: built-in and registered ones, plus its own */
|
|
598
|
+
const themeNames = (custom) => [.../* @__PURE__ */ new Set([...THEME_NAMES, ...Object.keys(custom ?? {})])];
|
|
599
|
+
/**
|
|
600
|
+
* A theme whose colour lookups also take any CSS colour (`#hex`, `rgb()`,
|
|
601
|
+
* `hsl()`, names). `custom` holds the diagram's own theme definitions
|
|
602
|
+
* (`Diagram.themes`), which win over registered themes of the same name.
|
|
603
|
+
*/
|
|
604
|
+
function getTheme(name, custom) {
|
|
605
|
+
if (name && custom?.[name]) {
|
|
606
|
+
let built = customThemes.get(custom);
|
|
607
|
+
if (!built) customThemes.set(custom, built = /* @__PURE__ */ new Map());
|
|
608
|
+
let t = built.get(name);
|
|
609
|
+
if (!t) built.set(name, t = withLookups(customTheme(name, custom)));
|
|
610
|
+
return t;
|
|
611
|
+
}
|
|
612
|
+
return withLookups(THEMES[name ?? "paper"] ?? THEMES.paper);
|
|
613
|
+
}
|
|
614
|
+
function withLookups(t) {
|
|
615
|
+
let w = withHex.get(t);
|
|
616
|
+
if (!w) {
|
|
617
|
+
w = {
|
|
618
|
+
...t,
|
|
619
|
+
tone: (x) => t.tone(x) ?? hexTone(x, w, false),
|
|
620
|
+
groupTone: (x) => t.groupTone(x) ?? hexTone(x, w, true),
|
|
621
|
+
edgeTone: (x) => t.edgeTone(x) ?? (cssHex(x) ? x : void 0)
|
|
622
|
+
};
|
|
623
|
+
withHex.set(t, w);
|
|
624
|
+
}
|
|
625
|
+
return w;
|
|
626
|
+
}
|
|
627
|
+
/** true for a palette name (`blue`, `blue-600`) or any CSS colour `cssHex` reads */
|
|
628
|
+
const isColor = (v) => {
|
|
629
|
+
const m = v.toLowerCase().match(/^([a-z]+)(?:-(\d{2,3}))?$/);
|
|
630
|
+
return !!m && isTone(m[1]) && (!m[2] || m[2] in L) || !!cssHex(v);
|
|
631
|
+
};
|
|
632
|
+
/** @deprecated use `isColor` */
|
|
633
|
+
const isToneOrHex = isColor;
|
|
634
|
+
/** regex source for one colour token inside `[...]`: `#hex`, `rgb(…)`, `hsl(…)` or a word */
|
|
635
|
+
const COLOR_TOKEN = String.raw`#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?)\([^)]*\)|[a-zA-Z][\w-]*`;
|
|
636
|
+
/** Resolve a colour value: tone names (`blue`, `blue-500`) or any CSS colour. */
|
|
637
|
+
function resolveColor(v, theme, fallbackLevel) {
|
|
638
|
+
if (!v) return void 0;
|
|
639
|
+
const m = v.match(/^([a-z]+)(?:-(\d{2,3}))?$/);
|
|
640
|
+
if (m && RAMPS[m[1]]) {
|
|
641
|
+
const lvl = m[2] ? Number(m[2]) : fallbackLevel;
|
|
642
|
+
return ramp(m[1], lvl in L ? lvl : fallbackLevel);
|
|
643
|
+
}
|
|
644
|
+
return isSafeCss(v) ? v : void 0;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* True when a value can sit inside an SVG attribute or a style rule as is:
|
|
648
|
+
* any CSS colour or font list passes, quotes, angle brackets, `;`, braces,
|
|
649
|
+
* `&` and backslashes do not. Colours outside the palette go through this.
|
|
650
|
+
*/
|
|
651
|
+
const isSafeCss = (v) => /^[\w\s#(),.%/+-]*$/.test(v);
|
|
652
|
+
/**
|
|
653
|
+
* True for a link a diagram may carry: http(s), mailto, an anchor or a
|
|
654
|
+
* relative path. Other schemes (`javascript:`, `data:`, …) run code when the
|
|
655
|
+
* SVG is inlined in a page. Browsers drop whitespace and control characters
|
|
656
|
+
* inside a scheme, so the check does too.
|
|
657
|
+
*/
|
|
658
|
+
const isSafeLink = (v) => {
|
|
659
|
+
const scheme = v.replace(/[\u0000-\u0020\u007f]/g, "").match(/^([a-z][a-z0-9+.-]*):/i);
|
|
660
|
+
return !scheme || [
|
|
661
|
+
"http",
|
|
662
|
+
"https",
|
|
663
|
+
"mailto"
|
|
664
|
+
].includes(scheme[1].toLowerCase());
|
|
665
|
+
};
|
|
666
|
+
// src/core/inline.ts
|
|
667
|
+
/** the theme's node ink, with an optional text and line colour */
|
|
668
|
+
const nodeInk = (theme, text = theme.node.text, stroke = theme.node.stroke) => ({
|
|
669
|
+
text,
|
|
670
|
+
sub: theme.node.sub,
|
|
671
|
+
stroke
|
|
672
|
+
});
|
|
673
|
+
/** the same ink drawn in its muted colour: subtitles, meta and caps lines */
|
|
674
|
+
const subInk = (col) => ({
|
|
675
|
+
...col,
|
|
676
|
+
text: col.sub
|
|
677
|
+
});
|
|
678
|
+
const COLOR_RE = /^(#[0-9a-fA-F]{3,8}|[a-z]+(?:-\d{2,3})?)$/;
|
|
679
|
+
const LEGACY_RE = /^\{(#[0-9a-fA-F]{3,8}|[a-z]+(?:-\d{2,3})?):[ ]?(.+)\}$/;
|
|
680
|
+
const INLINE_RE = /\*\*(.{1,1000}?)\*\*|~~(.{1,1000}?)~~|`([^`]+)`|\[\[(.{1,1000}?)\]\]|\{([^{}|\n]{1,1000}?)[ ]?\|[ ]?(.{1,1000}?)\}|\{(#[0-9a-fA-F]{3,8}|[a-z]+(?:-\d{2,3})?):[ ]?(.{1,1000}?)\}|\*(.{1,1000}?)\*/g;
|
|
681
|
+
/** the span keys: `parseSpanStyle` and the LSP completion and hover read this */
|
|
682
|
+
const SPAN_KEYS = table({
|
|
683
|
+
color: {
|
|
684
|
+
aliases: ["text"],
|
|
685
|
+
info: "text colour: a palette name or any CSS colour",
|
|
686
|
+
values: TONES
|
|
687
|
+
},
|
|
688
|
+
size: {
|
|
689
|
+
aliases: ["font-size"],
|
|
690
|
+
info: "font size, px",
|
|
691
|
+
values: []
|
|
692
|
+
},
|
|
693
|
+
weight: {
|
|
694
|
+
aliases: ["font-weight"],
|
|
695
|
+
info: "font weight: 400, 600, 700 ...",
|
|
696
|
+
values: [
|
|
697
|
+
"300",
|
|
698
|
+
"400",
|
|
699
|
+
"500",
|
|
700
|
+
"600",
|
|
701
|
+
"700",
|
|
702
|
+
"800",
|
|
703
|
+
"900"
|
|
704
|
+
]
|
|
705
|
+
},
|
|
706
|
+
italic: {
|
|
707
|
+
aliases: [],
|
|
708
|
+
info: "italic text",
|
|
709
|
+
values: ["true", "false"],
|
|
710
|
+
flag: true
|
|
711
|
+
},
|
|
712
|
+
bold: {
|
|
713
|
+
aliases: [],
|
|
714
|
+
info: "bold text, weight 700",
|
|
715
|
+
values: ["true", "false"],
|
|
716
|
+
flag: true
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
/** the span key a name or alias stands for */
|
|
720
|
+
const spanKey = (k) => k in SPAN_KEYS ? k : Object.keys(SPAN_KEYS).find((n) => SPAN_KEYS[n].aliases.includes(k));
|
|
721
|
+
/**
|
|
722
|
+
* `color: red, size: 18, weight: 700, italic` → a style; a bare colour sets the
|
|
723
|
+
* colour and a bare flag (`bold`, `italic`) means `true`. Undefined when any key is unknown.
|
|
724
|
+
*/
|
|
725
|
+
function parseSpanStyle(spec) {
|
|
726
|
+
const st = {};
|
|
727
|
+
for (const part of spec.split(",")) {
|
|
728
|
+
const [k0, ...rest] = part.split(":");
|
|
729
|
+
const name = k0.trim();
|
|
730
|
+
if (!rest.length && isColorName(name)) {
|
|
731
|
+
st.color = name;
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
const k = spanKey(name);
|
|
735
|
+
if (!k) return;
|
|
736
|
+
const flag = SPAN_KEYS[k].flag;
|
|
737
|
+
const v = rest.length ? rest.join(":").trim() : flag ? "true" : "";
|
|
738
|
+
if (flag) {
|
|
739
|
+
if (v !== "true" && v !== "false") return;
|
|
740
|
+
if (v === "true") k === "italic" ? st.italic = true : st.weight = 700;
|
|
741
|
+
} else if (k === "color") {
|
|
742
|
+
if (!isColor(v)) return;
|
|
743
|
+
st.color = v;
|
|
744
|
+
} else {
|
|
745
|
+
const n = parseFloat(v);
|
|
746
|
+
if (!(n > 0 && n <= 1e3)) return;
|
|
747
|
+
k === "size" ? st.size = n : st.weight = n;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return st;
|
|
751
|
+
}
|
|
752
|
+
/** true for `#hex` or a tone name (`green`, `green-600`) */
|
|
753
|
+
const isColorName = (c) => COLOR_RE.test(c) && (c.startsWith("#") || isTone(c.replace(/-\d+$/, "")));
|
|
754
|
+
/** Split one line of text into styled segments. */
|
|
755
|
+
function parseInline(s) {
|
|
756
|
+
const out = [];
|
|
757
|
+
let last = 0;
|
|
758
|
+
const push = (seg) => {
|
|
759
|
+
const prev = out[out.length - 1];
|
|
760
|
+
if (seg.kind === "text" && prev?.kind === "text") prev.text += seg.text;
|
|
761
|
+
else if (seg.text) out.push(seg);
|
|
762
|
+
};
|
|
763
|
+
for (const m of s.matchAll(INLINE_RE)) {
|
|
764
|
+
push({
|
|
765
|
+
kind: "text",
|
|
766
|
+
text: s.slice(last, m.index)
|
|
767
|
+
});
|
|
768
|
+
last = m.index + m[0].length;
|
|
769
|
+
if (m[1] !== void 0) push({
|
|
770
|
+
kind: "bold",
|
|
771
|
+
text: m[1]
|
|
772
|
+
});
|
|
773
|
+
else if (m[2] !== void 0) push({
|
|
774
|
+
kind: "strike",
|
|
775
|
+
text: m[2]
|
|
776
|
+
});
|
|
777
|
+
else if (m[3] !== void 0) push({
|
|
778
|
+
kind: "accent",
|
|
779
|
+
text: m[3]
|
|
780
|
+
});
|
|
781
|
+
else if (m[4] !== void 0) {
|
|
782
|
+
const c = m[4].match(/^([#\w-]+):\s*(.+)$/);
|
|
783
|
+
if (c && isColorName(c[1])) push({
|
|
784
|
+
kind: "chip",
|
|
785
|
+
text: c[2],
|
|
786
|
+
color: c[1]
|
|
787
|
+
});
|
|
788
|
+
else push({
|
|
789
|
+
kind: "chip",
|
|
790
|
+
text: m[4]
|
|
791
|
+
});
|
|
792
|
+
} else if (m[5] !== void 0) {
|
|
793
|
+
const st = parseSpanStyle(m[5]);
|
|
794
|
+
const legacy = st ? null : m[0].match(LEGACY_RE);
|
|
795
|
+
if (st) push({
|
|
796
|
+
kind: "color",
|
|
797
|
+
text: m[6],
|
|
798
|
+
...st
|
|
799
|
+
});
|
|
800
|
+
else if (legacy && isColorName(legacy[1])) push({
|
|
801
|
+
kind: "color",
|
|
802
|
+
text: legacy[2],
|
|
803
|
+
color: legacy[1]
|
|
804
|
+
});
|
|
805
|
+
else push({
|
|
806
|
+
kind: "text",
|
|
807
|
+
text: m[0]
|
|
808
|
+
});
|
|
809
|
+
} else if (m[7] !== void 0) {
|
|
810
|
+
if (isColorName(m[7])) push({
|
|
811
|
+
kind: "color",
|
|
812
|
+
text: m[8],
|
|
813
|
+
color: m[7]
|
|
814
|
+
});
|
|
815
|
+
else push({
|
|
816
|
+
kind: "text",
|
|
817
|
+
text: m[0]
|
|
818
|
+
});
|
|
819
|
+
} else push({
|
|
820
|
+
kind: "muted",
|
|
821
|
+
text: m[9]
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
push({
|
|
825
|
+
kind: "text",
|
|
826
|
+
text: s.slice(last)
|
|
827
|
+
});
|
|
828
|
+
return out;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Split a paragraph at spaces for wrapping, keeping each markup span whole so a
|
|
832
|
+
* `{red | two words}` never breaks across lines.
|
|
833
|
+
*/
|
|
834
|
+
function splitWords(para) {
|
|
835
|
+
const keep = [];
|
|
836
|
+
for (const m of para.matchAll(INLINE_RE)) keep.push([m.index, m.index + m[0].length]);
|
|
837
|
+
const out = [];
|
|
838
|
+
let cur = "";
|
|
839
|
+
let k = 0;
|
|
840
|
+
for (let i = 0; i < para.length; i++) {
|
|
841
|
+
while (k < keep.length && keep[k][1] <= i) k++;
|
|
842
|
+
const inside = k < keep.length && keep[k][0] <= i;
|
|
843
|
+
if (para[i] === " " && !inside) {
|
|
844
|
+
if (cur) out.push(cur);
|
|
845
|
+
cur = "";
|
|
846
|
+
} else cur += para[i];
|
|
847
|
+
}
|
|
848
|
+
if (cur) out.push(cur);
|
|
849
|
+
return out;
|
|
850
|
+
}
|
|
851
|
+
/** a line with no markup characters needs no parsing (the common plain label) */
|
|
852
|
+
const MARKUP_RE = /[*~`[{]/;
|
|
853
|
+
/** segments of a label line; `caps` upper-cases the text, not the markup */
|
|
854
|
+
function richSegs(line, caps = false) {
|
|
855
|
+
const segs = MARKUP_RE.test(line) ? parseInline(line) : line ? [{
|
|
856
|
+
kind: "text",
|
|
857
|
+
text: line
|
|
858
|
+
}] : [];
|
|
859
|
+
if (caps) for (const x of segs) x.text = x.text.toUpperCase();
|
|
860
|
+
return segs;
|
|
861
|
+
}
|
|
862
|
+
/** width of one label line with markup, plus `spacing` per character */
|
|
863
|
+
function richW(line, fs, weight, mono, m, caps = false, spacing = 0) {
|
|
864
|
+
const segs = richSegs(line, caps);
|
|
865
|
+
return segsW(segs, fs, mono, m, weight) + (spacing ? spacing * [...plain(segs)].length : 0);
|
|
866
|
+
}
|
|
867
|
+
/** largest font size over label lines: a `{size: …}` span can make a line taller */
|
|
868
|
+
const linesSize = (lines, fs) => lines.reduce((a, l) => MARKUP_RE.test(l) ? parseInline(l).reduce((b, x) => Math.max(b, x.size ?? 0), a) : a, fs);
|
|
869
|
+
/** a line's height factor over its font size */
|
|
870
|
+
const LINE_HEIGHT = 1.3;
|
|
871
|
+
/** the line height of a block of label lines: its largest font size times `k` */
|
|
872
|
+
const lineHeight = (lines, fs, k = LINE_HEIGHT) => linesSize(lines, fs) * k;
|
|
873
|
+
/** one `<tspan>` per line at `x`, line `i` at `y(i)` */
|
|
874
|
+
const textLines = (lines, x, y, body) => lines.map((l, i) => `<tspan x="${f$1(x)}" y="${f$1(y(i))}">${body(l)}</tspan>`).join("");
|
|
875
|
+
/**
|
|
876
|
+
* The inside of a `<text>`/`<tspan>` for a label line: escaped text when it
|
|
877
|
+
* has no markup (so plain labels render as before), styled tspans otherwise.
|
|
878
|
+
*/
|
|
879
|
+
function richText(line, col, theme, caps = false) {
|
|
880
|
+
const segs = richSegs(line, caps);
|
|
881
|
+
if (segs.every((x) => x.kind === "text")) return esc(plain(segs));
|
|
882
|
+
return tspans(segs, col, theme);
|
|
883
|
+
}
|
|
884
|
+
/** the text without markup */
|
|
885
|
+
const plain = (segs) => segs.map((x) => x.text).join("");
|
|
886
|
+
/** Remove the common leading whitespace of the non-empty lines. */
|
|
887
|
+
function dedent(lines) {
|
|
888
|
+
let ind = 1e400;
|
|
889
|
+
for (const l of lines) if (l.trim()) ind = Math.min(ind, l.match(/^[ \t]*/)[0].length);
|
|
890
|
+
const cut = Number.isFinite(ind) ? ind : 0;
|
|
891
|
+
while (lines.length && !lines[0].trim()) lines.shift();
|
|
892
|
+
while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
|
|
893
|
+
return lines.map((l) => l.slice(cut).replace(/\t/g, " "));
|
|
894
|
+
}
|
|
895
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
896
|
+
const f$1 = (n) => (Math.round(n * 100) / 100).toString();
|
|
897
|
+
/** a single-line `<text>` centred vertically on `y` */
|
|
898
|
+
const textEl = (x, y, fs, fill, body, extra = "") => `<text x="${f$1(x)}" y="${f$1(y)}" dominant-baseline="central" font-size="${f$1(fs)}" fill="${fill}"${extra}>${body}</text>`;
|
|
899
|
+
/** resolve a tone or CSS colour for marks drawn on top of an element */
|
|
900
|
+
const inkColor = (c, theme, fallback) => c && resolveColor(c, theme, theme.dark ? 400 : 600) || fallback;
|
|
901
|
+
const CHIP_PAD = .55;
|
|
902
|
+
const segWeight = (s, base) => s.weight ?? (s.kind === "bold" ? 700 : base);
|
|
903
|
+
/** Measure segments (stores each width on the segment) and return the total. */
|
|
904
|
+
function segsW(segs, fs, mono, m, weight = 400) {
|
|
905
|
+
let total = 0;
|
|
906
|
+
for (const s of segs) {
|
|
907
|
+
const size = s.size ?? fs;
|
|
908
|
+
const w = m(s.text, size, segWeight(s, weight), mono);
|
|
909
|
+
s.w = s.kind === "chip" ? w + size * CHIP_PAD * 2 : w;
|
|
910
|
+
total += s.w;
|
|
911
|
+
}
|
|
912
|
+
return total;
|
|
913
|
+
}
|
|
914
|
+
/** total of the widths stored by segsW */
|
|
915
|
+
const sumW = (segs) => segs.reduce((a, s) => a + (s.w ?? 0), 0);
|
|
916
|
+
function segColor(s, col, theme, muted) {
|
|
917
|
+
if (s.color) return inkColor(s.color, theme, col.text);
|
|
918
|
+
switch (s.kind) {
|
|
919
|
+
case "muted":
|
|
920
|
+
case "strike": return col.sub;
|
|
921
|
+
case "accent": return theme.accent;
|
|
922
|
+
default: return muted ? col.sub : col.text;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
/** Draw measured segments left to right from x, centred on cy. */
|
|
926
|
+
function renderSegs(segs, x, cy, fs, col, theme, muted = false, weight = 400) {
|
|
927
|
+
const out = [];
|
|
928
|
+
for (const s of segs) {
|
|
929
|
+
const w = s.w ?? 0;
|
|
930
|
+
const c = segColor(s, col, theme, muted);
|
|
931
|
+
if (s.kind === "chip") {
|
|
932
|
+
const ch = fs * 1.6;
|
|
933
|
+
const fill = s.color && c.startsWith("#") && c.length === 7 ? `${c}1f` : "none";
|
|
934
|
+
out.push(`<rect x="${f$1(x)}" y="${f$1(cy - ch / 2)}" width="${f$1(w)}" height="${f$1(ch)}" rx="4" fill="${fill}" stroke="${s.color ? c : col.stroke}" stroke-width="1"/>`);
|
|
935
|
+
out.push(textEl(x + fs * CHIP_PAD, cy, fs, s.color ? c : col.text, esc(s.text), " xml:space=\"preserve\""));
|
|
936
|
+
} else {
|
|
937
|
+
const wt = segWeight(s, weight);
|
|
938
|
+
const it = s.italic ? " font-style=\"italic\"" : "";
|
|
939
|
+
out.push(textEl(x, cy, s.size ?? fs, c, esc(s.text), ` xml:space="preserve"${wt !== 400 ? ` font-weight="${wt}"` : ""}${it}`));
|
|
940
|
+
if (s.kind === "strike") {
|
|
941
|
+
const lead = s.text.length - s.text.trimStart().length;
|
|
942
|
+
const x0 = x + w * lead / Math.max(1, s.text.length);
|
|
943
|
+
out.push(`<path d="M${f$1(x0)},${f$1(cy)}H${f$1(x + w)}" stroke="${col.sub}" stroke-width="1"/>`);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
x += w;
|
|
947
|
+
}
|
|
948
|
+
return out.join("");
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Segments as `<tspan>`s inside one `<text>`, so the browser flows them and no
|
|
952
|
+
* measuring is needed. Chips become coloured text here.
|
|
953
|
+
*/
|
|
954
|
+
function tspans(segs, col, theme, muted = false) {
|
|
955
|
+
return segs.map((s) => {
|
|
956
|
+
const a = [`fill="${segColor(s, col, theme, muted)}"`];
|
|
957
|
+
if (s.size) a.push(`font-size="${f$1(s.size)}"`);
|
|
958
|
+
if (s.weight) a.push(`font-weight="${s.weight}"`);
|
|
959
|
+
else if (s.kind === "bold") a.push("font-weight=\"750\"");
|
|
960
|
+
if (s.italic) a.push("font-style=\"italic\"");
|
|
961
|
+
if (s.kind === "strike") a.push("text-decoration=\"line-through\"");
|
|
962
|
+
if (s.kind === "chip") a.push("font-weight=\"600\"");
|
|
963
|
+
return `<tspan ${a.join(" ")}>${esc(s.text)}</tspan>`;
|
|
964
|
+
}).join("");
|
|
965
|
+
}
|
|
966
|
+
// src/core/style.ts
|
|
967
|
+
const num = (v, d) => {
|
|
968
|
+
const n = v === void 0 ? NaN : parseFloat(v);
|
|
969
|
+
return Number.isFinite(n) ? n : d;
|
|
970
|
+
};
|
|
971
|
+
/**
|
|
972
|
+
* The line style props ask for: `key` (`style` on edges, `border-style` on
|
|
973
|
+
* boxes) wins over the `dashed` / `dotted` shorthand flags. Undefined when
|
|
974
|
+
* nothing is set, so the caller's default applies.
|
|
975
|
+
*/
|
|
976
|
+
function lineStyle(p, key = "border-style") {
|
|
977
|
+
const v = p[key];
|
|
978
|
+
if (v && LINE_STYLES.includes(v)) return v;
|
|
979
|
+
return bool(p.dashed, false) ? "dashed" : bool(p.dotted, false) ? "dotted" : void 0;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* `edge-color` and `edge-width` as a diagram setting or on a panel: the ink
|
|
983
|
+
* every line takes unless it has its own colour. Returns `t` itself when
|
|
984
|
+
* neither is set.
|
|
985
|
+
*/
|
|
986
|
+
function withEdgeInk(t, p) {
|
|
987
|
+
const color = resolveColor(p["edge-color"], t, t.dark ? 400 : 500);
|
|
988
|
+
const width = edgeWidthOf(p);
|
|
989
|
+
if (!color && !width) return t;
|
|
990
|
+
const out = Object.create(t);
|
|
991
|
+
if (color) out.edge = {
|
|
992
|
+
...t.edge,
|
|
993
|
+
stroke: color,
|
|
994
|
+
ink: color
|
|
995
|
+
};
|
|
996
|
+
if (width) out.edgeWidth = width;
|
|
997
|
+
return out;
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* `edge-width` when written, else undefined: for a panel whose lines are
|
|
1001
|
+
* thinner than the theme's edges unless the author asks for more
|
|
1002
|
+
*/
|
|
1003
|
+
function edgeWidthOf(p) {
|
|
1004
|
+
return posNum(p["edge-width"]);
|
|
1005
|
+
}
|
|
1006
|
+
/** a size, gap or width: 0 or more, else the default (a negative one breaks the SVG) */
|
|
1007
|
+
const size = (v, d) => {
|
|
1008
|
+
const n = num(v, NaN);
|
|
1009
|
+
return n >= 0 ? n : d;
|
|
1010
|
+
};
|
|
1011
|
+
/** a positive number, else undefined */
|
|
1012
|
+
const posNum = (v) => {
|
|
1013
|
+
const n = num(v, NaN);
|
|
1014
|
+
return n > 0 ? n : void 0;
|
|
1015
|
+
};
|
|
1016
|
+
/** Merge class props then own props (own wins). */
|
|
1017
|
+
/** the shared text props (`font-size`, `font-weight`, `italic`) over an element's defaults */
|
|
1018
|
+
const textFont = (p, size, weight) => ({
|
|
1019
|
+
size: posNum(p["font-size"]) ?? size,
|
|
1020
|
+
weight: posNum(p["font-weight"]) ?? weight,
|
|
1021
|
+
italic: bool(p.italic, false)
|
|
1022
|
+
});
|
|
1023
|
+
/** an edge label's font */
|
|
1024
|
+
const edgeFont = (p, theme) => textFont(p, theme.fontSize * .86, 500);
|
|
1025
|
+
function effectiveProps(d, decl) {
|
|
1026
|
+
const out = {};
|
|
1027
|
+
for (const c of decl.classes ?? []) Object.assign(out, d.classes[c]);
|
|
1028
|
+
return Object.assign(out, decl.props);
|
|
1029
|
+
}
|
|
1030
|
+
/** the `stroke-dasharray` of a line style at a stroke width; null for solid */
|
|
1031
|
+
function dashFor(style, width) {
|
|
1032
|
+
switch (style) {
|
|
1033
|
+
case "dashed": return `${f$1(width * 4)} ${f$1(width * 3)}`;
|
|
1034
|
+
case "dotted": return `${f$1(width * .1)} ${f$1(width * 2.6)}`;
|
|
1035
|
+
default: return null;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
function resolveNodeStyle(d, n, p, theme) {
|
|
1039
|
+
const s = d.settings;
|
|
1040
|
+
const tone = p.color ? theme.tone(p.color) : void 0;
|
|
1041
|
+
const strokeWidth = size(p.border ?? p["stroke-width"], size(s["stroke-width"], theme.strokeWidth));
|
|
1042
|
+
const fill = resolveColor(p.fill, theme, theme.dark ? 900 : 100) ?? tone?.fill ?? theme.node.fill;
|
|
1043
|
+
const stroke = resolveColor(p.stroke, theme, 500) ?? tone?.stroke ?? theme.node.stroke;
|
|
1044
|
+
const text = resolveColor(p.text, theme, theme.dark ? 100 : 800) ?? tone?.text ?? theme.node.text;
|
|
1045
|
+
const font = textFont(p, posNum(s["font-size"]) ?? theme.fontSize, 500);
|
|
1046
|
+
const fontSize = font.size;
|
|
1047
|
+
return {
|
|
1048
|
+
fill,
|
|
1049
|
+
stroke,
|
|
1050
|
+
text,
|
|
1051
|
+
sub: resolveColor(p["sub-color"], theme, 500) ?? (tone ? text : theme.node.sub),
|
|
1052
|
+
strokeWidth,
|
|
1053
|
+
dash: dashFor(lineStyle(p), strokeWidth),
|
|
1054
|
+
radius: size(p.radius, size(s.radius, theme.radius)),
|
|
1055
|
+
fontSize,
|
|
1056
|
+
fontWeight: bool(p.bold, false) ? 650 : font.weight,
|
|
1057
|
+
body: resolveColor(p["body-color"], theme, theme.dark ? 100 : 800) ?? text,
|
|
1058
|
+
bodySize: posNum(p["body-size"]) ?? Math.round(fontSize * .86 * 10) / 10,
|
|
1059
|
+
bodyWeight: posNum(p["body-weight"]) ?? 400,
|
|
1060
|
+
lineHeight: posNum(p["line-height"]) ?? 1.75,
|
|
1061
|
+
italic: font.italic,
|
|
1062
|
+
shadow: bool(p.shadow, bool(s.shadow, theme.shadow !== null)),
|
|
1063
|
+
opacity: num(p.opacity, 1)
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
function resolveGroupStyle(g, theme) {
|
|
1067
|
+
const p = g.props;
|
|
1068
|
+
const tone = p.color ? theme.groupTone(p.color) : void 0;
|
|
1069
|
+
return {
|
|
1070
|
+
fill: resolveColor(p.fill, theme, theme.dark ? 950 : 50) ?? tone?.fill ?? theme.group.fill,
|
|
1071
|
+
stroke: resolveColor(p.stroke, theme, 300) ?? tone?.stroke ?? theme.group.stroke,
|
|
1072
|
+
text: resolveColor(p.text, theme, 700) ?? tone?.text ?? theme.group.text,
|
|
1073
|
+
dash: borderDash(p, theme.group.dash)
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
/** `border-style: dashed|dotted|solid` (or `dashed`/`dotted` flags) as a dash array */
|
|
1077
|
+
function borderDash(p, fallback) {
|
|
1078
|
+
return groupDash(lineStyle(p), fallback);
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* The theme a panel draws with: `theme: <name>` swaps colours for that
|
|
1082
|
+
* theme's, while font and size stay the diagram's; `edge-color` and
|
|
1083
|
+
* `edge-width` restyle its lines.
|
|
1084
|
+
*/
|
|
1085
|
+
function panelTheme(base, p, custom) {
|
|
1086
|
+
const name = p.theme;
|
|
1087
|
+
if (!name || !(THEMES[name] || custom?.[name]) || name === base.name) return withEdgeInk(base, p);
|
|
1088
|
+
const t = Object.create(getTheme(name, custom));
|
|
1089
|
+
t.font = base.font;
|
|
1090
|
+
t.mono = base.mono;
|
|
1091
|
+
t.fontSize = base.fontSize;
|
|
1092
|
+
return withEdgeInk(t, p);
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Frame of a panel. `background`/`fill` and `border`/`stroke` take any
|
|
1096
|
+
* colour; `border` also takes a width (`border: 2`) or `none`. A panel with
|
|
1097
|
+
* its own theme gets that theme's page colour as background.
|
|
1098
|
+
*/
|
|
1099
|
+
function resolvePanelStyle(p, T, base) {
|
|
1100
|
+
const tone = p.color ? T.groupTone(p.color) : void 0;
|
|
1101
|
+
const themed = T.name !== base.name;
|
|
1102
|
+
const border = p.border?.trim();
|
|
1103
|
+
const noBorder = border === "none" || border === "false" || p["border-width"] === "0";
|
|
1104
|
+
const borderColor = border && isColor(border) ? border : void 0;
|
|
1105
|
+
const bg = p.background ?? p.fill;
|
|
1106
|
+
const fill = bg === "none" || bg === "transparent" ? "none" : resolveColor(bg, T, T.dark ? 950 : 50) ?? tone?.fill ?? (themed && T.background !== "none" ? T.background : T.group.fill);
|
|
1107
|
+
return {
|
|
1108
|
+
framed: p.frame !== "false" && p.frame !== "none",
|
|
1109
|
+
fill,
|
|
1110
|
+
stroke: noBorder ? "none" : resolveColor(p.stroke ?? borderColor, T, 300) ?? tone?.stroke ?? T.group.stroke,
|
|
1111
|
+
text: resolveColor(p.text, T, T.dark ? 200 : 700) ?? tone?.text ?? T.title,
|
|
1112
|
+
width: size(p["border-width"], border && !borderColor && !noBorder ? size(border, 1) : 1),
|
|
1113
|
+
dash: borderDash(p, null),
|
|
1114
|
+
radius: size(p.radius, Math.min(14, T.radius + 4)),
|
|
1115
|
+
shadow: bool(p.shadow, false)
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
function resolveEdgeStyle(d, e, p, theme, defaultCurve) {
|
|
1119
|
+
const toneColor = p.color ? theme.edgeTone(p.color) ?? resolveColor(p.color, theme, 500) : void 0;
|
|
1120
|
+
const stroke = resolveColor(p.stroke, theme, theme.dark ? 400 : 500) ?? toneColor ?? theme.edge.stroke;
|
|
1121
|
+
const base = e.line === "thick" || bool(p.thick, false) ? theme.edgeWidth * 2 : theme.edgeWidth;
|
|
1122
|
+
const width = size(p.width, base);
|
|
1123
|
+
const line = lineStyle(p, "style") ?? (e.line === "dashed" || e.line === "dotted" ? e.line : void 0);
|
|
1124
|
+
const curve = [
|
|
1125
|
+
"smooth",
|
|
1126
|
+
"orthogonal",
|
|
1127
|
+
"straight"
|
|
1128
|
+
].includes(p.curve) ? p.curve : defaultCurve;
|
|
1129
|
+
const flow = bool(p.flow ?? p.animate, false);
|
|
1130
|
+
const font = edgeFont(p, theme);
|
|
1131
|
+
return {
|
|
1132
|
+
stroke,
|
|
1133
|
+
width,
|
|
1134
|
+
dash: dashFor(line, width) ?? (flow ? `${width * 4} ${width * 3}` : null),
|
|
1135
|
+
curve,
|
|
1136
|
+
text: resolveColor(p.text ?? p["label-color"], theme, 700) ?? (toneColor && !theme.dark ? resolveColor(p.color, theme, 700) : theme.edge.text),
|
|
1137
|
+
fontSize: font.size,
|
|
1138
|
+
fontWeight: font.weight,
|
|
1139
|
+
italic: font.italic,
|
|
1140
|
+
labelBg: resolveColor(p["label-bg"], theme, theme.dark ? 900 : 50) ?? theme.edge.labelBg,
|
|
1141
|
+
flow,
|
|
1142
|
+
opacity: num(p.opacity, 1)
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
// src/core/route.ts
|
|
1146
|
+
const SIDES$2 = [
|
|
1147
|
+
"top",
|
|
1148
|
+
"right",
|
|
1149
|
+
"bottom",
|
|
1150
|
+
"left"
|
|
1151
|
+
];
|
|
1152
|
+
const center = (b) => ({
|
|
1153
|
+
x: b.x + b.w / 2,
|
|
1154
|
+
y: b.y + b.h / 2
|
|
1155
|
+
});
|
|
1156
|
+
const add = (p, q, k = 1) => ({
|
|
1157
|
+
x: p.x + q.x * k,
|
|
1158
|
+
y: p.y + q.y * k
|
|
1159
|
+
});
|
|
1160
|
+
const sub = (p, q) => ({
|
|
1161
|
+
x: p.x - q.x,
|
|
1162
|
+
y: p.y - q.y
|
|
1163
|
+
});
|
|
1164
|
+
const len = (p) => Math.hypot(p.x, p.y);
|
|
1165
|
+
const norm = (p) => {
|
|
1166
|
+
const l = len(p) || 1;
|
|
1167
|
+
return {
|
|
1168
|
+
x: p.x / l,
|
|
1169
|
+
y: p.y / l
|
|
1170
|
+
};
|
|
1171
|
+
};
|
|
1172
|
+
const dist = (p, q) => len(sub(p, q));
|
|
1173
|
+
const r2 = (n) => Math.round(n * 100) / 100;
|
|
1174
|
+
/** Choose the side of `box` facing `target`. */
|
|
1175
|
+
function autoSide(box, target, other, dir) {
|
|
1176
|
+
const c = center(box);
|
|
1177
|
+
const dx = target.x - c.x;
|
|
1178
|
+
const dy = target.y - c.y;
|
|
1179
|
+
let hgap = Math.abs(dx) - (box.w + (other?.w ?? 0)) / 2;
|
|
1180
|
+
let vgap = Math.abs(dy) - (box.h + (other?.h ?? 0)) / 2;
|
|
1181
|
+
if (dir === "right" || dir === "left") hgap += 24;
|
|
1182
|
+
else vgap += 24;
|
|
1183
|
+
if (hgap >= vgap) return dx >= 0 ? "right" : "left";
|
|
1184
|
+
return dy >= 0 ? "bottom" : "top";
|
|
1185
|
+
}
|
|
1186
|
+
const OPPOSITE = {
|
|
1187
|
+
left: "right",
|
|
1188
|
+
right: "left",
|
|
1189
|
+
top: "bottom",
|
|
1190
|
+
bottom: "top"
|
|
1191
|
+
};
|
|
1192
|
+
/** Facing side on the other axis, e.g. right -> top/bottom towards the target. */
|
|
1193
|
+
function altSide(box, target, s) {
|
|
1194
|
+
const c = center(box);
|
|
1195
|
+
if (s === "left" || s === "right") return target.y >= c.y ? "bottom" : "top";
|
|
1196
|
+
return target.x >= c.x ? "right" : "left";
|
|
1197
|
+
}
|
|
1198
|
+
/** `s`, the facing side on the other axis, and the far side too: two top ports arc over what sits between */
|
|
1199
|
+
function turns(s, box, to) {
|
|
1200
|
+
const alt = altSide(box, to, s);
|
|
1201
|
+
return [
|
|
1202
|
+
s,
|
|
1203
|
+
alt,
|
|
1204
|
+
OPPOSITE[alt]
|
|
1205
|
+
];
|
|
1206
|
+
}
|
|
1207
|
+
/** Pick the side combination whose provisional curve crosses the fewest other nodes. */
|
|
1208
|
+
function avoidObstacles(a, b, s0, s1, free0, free1, obstacles) {
|
|
1209
|
+
const m = 80;
|
|
1210
|
+
const x = Math.min(a.x, b.x) - m;
|
|
1211
|
+
const y = Math.min(a.y, b.y) - m;
|
|
1212
|
+
const near = {
|
|
1213
|
+
x,
|
|
1214
|
+
y,
|
|
1215
|
+
w: Math.max(a.x + a.w, b.x + b.w) + m - x,
|
|
1216
|
+
h: Math.max(a.y + a.h, b.y + b.h) + m - y
|
|
1217
|
+
};
|
|
1218
|
+
const others = obstacles.filter((o) => o !== a && o !== b && hits(o, near));
|
|
1219
|
+
if (!others.length) return [s0, s1];
|
|
1220
|
+
const c0 = free0 ? turns(s0, a, center(b)) : [s0];
|
|
1221
|
+
const c1 = free1 ? turns(s1, b, center(a)) : [s1];
|
|
1222
|
+
const pairs = c0.flatMap((x) => c1.map((y) => [x, y]));
|
|
1223
|
+
const changes = ([x, y]) => +(x !== s0) + +(y !== s1);
|
|
1224
|
+
pairs.sort((p, q) => changes(p) - changes(q));
|
|
1225
|
+
let best = pairs[0];
|
|
1226
|
+
let bestHits = 1e400;
|
|
1227
|
+
for (const pair of pairs) {
|
|
1228
|
+
const [x, y] = pair;
|
|
1229
|
+
const S = portPoint(a.shape, a, x, 0);
|
|
1230
|
+
const E = portPoint(b.shape, b, y, 0);
|
|
1231
|
+
const pts = sample({
|
|
1232
|
+
kind: "cubic",
|
|
1233
|
+
segs: smoothRoute(S, SIDE_NORMAL[x], E, SIDE_NORMAL[y], [])
|
|
1234
|
+
});
|
|
1235
|
+
let hits = 0;
|
|
1236
|
+
for (const o of others) if (pts.some((p) => p.x > o.x + 2 && p.x < o.x + o.w - 2 && p.y > o.y + 2 && p.y < o.y + o.h - 2)) hits++;
|
|
1237
|
+
if (!hits) return pair;
|
|
1238
|
+
if (hits < bestHits) [best, bestHits] = [pair, hits];
|
|
1239
|
+
}
|
|
1240
|
+
return best;
|
|
1241
|
+
}
|
|
1242
|
+
const headKind = (v, dflt) => v && (v === "none" || MARKER_KINDS.has(v)) ? v : dflt;
|
|
1243
|
+
function routeEdges(d, nodes, ctx) {
|
|
1244
|
+
const items = [];
|
|
1245
|
+
const pairKey = (x, y) => x < y ? `${x}\u0000${y}` : `${y}\u0000${x}`;
|
|
1246
|
+
const bundles = /* @__PURE__ */ new Map();
|
|
1247
|
+
for (const e of d.edges) {
|
|
1248
|
+
if (e.from === e.to || d.layout.edges[e.key]?.via?.length || ctx.dummyVia.get(e.key)?.length) continue;
|
|
1249
|
+
const k = pairKey(e.from, e.to);
|
|
1250
|
+
if (!bundles.has(k)) bundles.set(k, []);
|
|
1251
|
+
bundles.get(k).push(e.key);
|
|
1252
|
+
}
|
|
1253
|
+
const fanGap = ctx.theme.fontSize * 2.1;
|
|
1254
|
+
const edgeByKey = new Map(d.edges.map((e) => [e.key, e]));
|
|
1255
|
+
const laneOffs = /* @__PURE__ */ new Map();
|
|
1256
|
+
for (const e of d.edges) {
|
|
1257
|
+
const a = nodes.get(e.from);
|
|
1258
|
+
const b = nodes.get(e.to);
|
|
1259
|
+
if (!a || !b) continue;
|
|
1260
|
+
const p = effectiveProps(d, e);
|
|
1261
|
+
const style = resolveEdgeStyle(d, e, p, ctx.theme, ctx.defaultCurve);
|
|
1262
|
+
const o = d.layout.edges[e.key];
|
|
1263
|
+
const manualVia = !!o?.via?.length;
|
|
1264
|
+
const via = (manualVia ? o.via : ctx.dummyVia.get(e.key) ?? []).map((q) => ({ ...q }));
|
|
1265
|
+
const bundle = a !== b && !via.length ? bundles.get(pairKey(e.from, e.to)) : void 0;
|
|
1266
|
+
if (bundle && bundle.length > 1) {
|
|
1267
|
+
const [lo, hi] = e.from < e.to ? [a, b] : [b, a];
|
|
1268
|
+
const dir = norm(sub(center(hi), center(lo)));
|
|
1269
|
+
let offs = laneOffs.get(bundle);
|
|
1270
|
+
if (!offs) {
|
|
1271
|
+
const lanes = bundle.map((k) => {
|
|
1272
|
+
const x = edgeByKey.get(k);
|
|
1273
|
+
const font = x?.label ? edgeFont(effectiveProps(d, x), ctx.theme) : void 0;
|
|
1274
|
+
const w = font ? Math.max(...x.label.split("\n").map((t) => richW(t, font.size, font.weight, ctx.theme.mono, ctx.measure))) + 18 : 0;
|
|
1275
|
+
return Math.max(fanGap, Math.abs(dir.y) * w + Math.abs(dir.x) * fanGap + 6);
|
|
1276
|
+
});
|
|
1277
|
+
const total = lanes.reduce((x, y) => x + y, 0);
|
|
1278
|
+
offs = /* @__PURE__ */ new Map();
|
|
1279
|
+
let run = 0;
|
|
1280
|
+
for (let i = 0; i < bundle.length; i++) {
|
|
1281
|
+
offs.set(bundle[i], run + lanes[i] / 2 - total / 2);
|
|
1282
|
+
run += lanes[i];
|
|
1283
|
+
}
|
|
1284
|
+
laneOffs.set(bundle, offs);
|
|
1285
|
+
}
|
|
1286
|
+
const off = offs.get(e.key);
|
|
1287
|
+
const ca = center(a);
|
|
1288
|
+
const cb = center(b);
|
|
1289
|
+
if (off) via.push(add({
|
|
1290
|
+
x: (ca.x + cb.x) / 2,
|
|
1291
|
+
y: (ca.y + cb.y) / 2
|
|
1292
|
+
}, {
|
|
1293
|
+
x: -dir.y,
|
|
1294
|
+
y: dir.x
|
|
1295
|
+
}, off));
|
|
1296
|
+
}
|
|
1297
|
+
const wantFrom = o?.ports?.[0] ?? p.exit ?? p["from-side"];
|
|
1298
|
+
const wantTo = o?.ports?.[1] ?? p.enter ?? p["to-side"];
|
|
1299
|
+
const fixed0 = SIDES$2.includes(wantFrom) ? wantFrom : null;
|
|
1300
|
+
const fixed1 = SIDES$2.includes(wantTo) ? wantTo : null;
|
|
1301
|
+
let s0 = fixed0 ?? autoSide(a, via[0] ?? center(b), via.length ? null : b, ctx.direction);
|
|
1302
|
+
let s1 = fixed1 ?? autoSide(b, via[via.length - 1] ?? center(a), via.length ? null : a, ctx.direction);
|
|
1303
|
+
if (!via.length && a !== b && (!fixed0 || !fixed1)) [s0, s1] = avoidObstacles(a, b, s0, s1, !fixed0, !fixed1, ctx.obstacles);
|
|
1304
|
+
items.push({
|
|
1305
|
+
e,
|
|
1306
|
+
a,
|
|
1307
|
+
b,
|
|
1308
|
+
via,
|
|
1309
|
+
manualVia,
|
|
1310
|
+
sides: a === b ? ["right", "top"] : [s0, s1],
|
|
1311
|
+
t: [0, 0],
|
|
1312
|
+
style,
|
|
1313
|
+
headKind: e.endHead || p.head ? headKind(p.head, "arrow") : "none",
|
|
1314
|
+
tailKind: e.startHead || p.tail ? headKind(p.tail, "arrow") : "none",
|
|
1315
|
+
labelOff: o?.label
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
const slots = /* @__PURE__ */ new Map();
|
|
1319
|
+
for (const it of items) {
|
|
1320
|
+
if (it.a === it.b) continue;
|
|
1321
|
+
for (const end of [0, 1]) {
|
|
1322
|
+
const node = end === 0 ? it.a : it.b;
|
|
1323
|
+
const other = end === 0 ? it.via[0] ?? center(it.b) : it.via[it.via.length - 1] ?? center(it.a);
|
|
1324
|
+
const k = `${node.id}\u0000${it.sides[end]}`;
|
|
1325
|
+
if (!slots.has(k)) slots.set(k, []);
|
|
1326
|
+
slots.get(k).push({
|
|
1327
|
+
it,
|
|
1328
|
+
end,
|
|
1329
|
+
other
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
for (const [k, list] of slots) {
|
|
1334
|
+
const side = k.split("\0")[1];
|
|
1335
|
+
const horizSide = side === "top" || side === "bottom";
|
|
1336
|
+
list.sort((p, q) => (horizSide ? p.other.x - q.other.x : p.other.y - q.other.y) || items.indexOf(p.it) - items.indexOf(q.it));
|
|
1337
|
+
const n = list.length;
|
|
1338
|
+
list.forEach((s, i) => s.it.t[s.end] = n === 1 ? 0 : (i + 1) / (n + 1) * 2 - 1);
|
|
1339
|
+
}
|
|
1340
|
+
return items.map((it) => buildEdge(it, ctx));
|
|
1341
|
+
}
|
|
1342
|
+
function buildEdge(it, ctx) {
|
|
1343
|
+
const { a, b, style } = it;
|
|
1344
|
+
let geo;
|
|
1345
|
+
let S;
|
|
1346
|
+
let E;
|
|
1347
|
+
if (a === b) {
|
|
1348
|
+
S = {
|
|
1349
|
+
x: a.x + a.w,
|
|
1350
|
+
y: a.y + a.h * .32
|
|
1351
|
+
};
|
|
1352
|
+
E = {
|
|
1353
|
+
x: a.x + a.w * .68,
|
|
1354
|
+
y: a.y
|
|
1355
|
+
};
|
|
1356
|
+
geo = {
|
|
1357
|
+
kind: "cubic",
|
|
1358
|
+
segs: [[
|
|
1359
|
+
S,
|
|
1360
|
+
add(S, {
|
|
1361
|
+
x: 52,
|
|
1362
|
+
y: 0
|
|
1363
|
+
}),
|
|
1364
|
+
add(E, {
|
|
1365
|
+
x: 0,
|
|
1366
|
+
y: -52
|
|
1367
|
+
}),
|
|
1368
|
+
E
|
|
1369
|
+
]]
|
|
1370
|
+
};
|
|
1371
|
+
} else {
|
|
1372
|
+
S = portPoint(a.shape, a, it.sides[0], it.t[0]);
|
|
1373
|
+
E = portPoint(b.shape, b, it.sides[1], it.t[1]);
|
|
1374
|
+
const n0 = SIDE_NORMAL[it.sides[0]];
|
|
1375
|
+
const n1 = SIDE_NORMAL[it.sides[1]];
|
|
1376
|
+
if (ctx.channels?.has(it.e.key) && it.via.length >= 2) {
|
|
1377
|
+
const via = it.via.map((q) => ({ ...q }));
|
|
1378
|
+
via[0].y = S.y;
|
|
1379
|
+
via[via.length - 1].y = E.y;
|
|
1380
|
+
geo = {
|
|
1381
|
+
kind: "poly",
|
|
1382
|
+
pts: orthoRoute(S, n0, E, n1, via, a, b, ctx.obstacles),
|
|
1383
|
+
radius: 10
|
|
1384
|
+
};
|
|
1385
|
+
} else if (style.curve === "orthogonal") geo = {
|
|
1386
|
+
kind: "poly",
|
|
1387
|
+
pts: orthoRoute(S, n0, E, n1, it.via, a, b, ctx.obstacles),
|
|
1388
|
+
radius: 10
|
|
1389
|
+
};
|
|
1390
|
+
else if (style.curve === "straight") geo = {
|
|
1391
|
+
kind: "poly",
|
|
1392
|
+
pts: [
|
|
1393
|
+
S,
|
|
1394
|
+
...it.via,
|
|
1395
|
+
E
|
|
1396
|
+
],
|
|
1397
|
+
radius: it.via.length ? 6 : 0
|
|
1398
|
+
};
|
|
1399
|
+
else geo = {
|
|
1400
|
+
kind: "cubic",
|
|
1401
|
+
segs: smoothRoute(S, n0, E, n1, it.via)
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
const samples = sample(geo);
|
|
1405
|
+
let label;
|
|
1406
|
+
const caption = it.e.props.caption;
|
|
1407
|
+
if (it.e.label || caption) {
|
|
1408
|
+
const fs = style.fontSize;
|
|
1409
|
+
const { mono } = ctx.theme;
|
|
1410
|
+
const lines = it.e.label ? it.e.label.split("\n") : [];
|
|
1411
|
+
const pw = lines.length ? Math.max(...lines.map((l) => richW(l, fs, style.fontWeight, mono, ctx.measure))) + (ctx.theme.edge.labelStroke ? 18 : 12) : 0;
|
|
1412
|
+
const lh = lineHeight(lines, fs);
|
|
1413
|
+
const ph = lines.length ? lines.length * lh + 6 : 0;
|
|
1414
|
+
const cs = fs * .88;
|
|
1415
|
+
const capLines = caption ? caption.split("\n") : [];
|
|
1416
|
+
const cw = capLines.length ? Math.max(...capLines.map((l) => richW(l, cs, 400, mono, ctx.measure))) + 8 : 0;
|
|
1417
|
+
const clh = lineHeight(capLines, cs);
|
|
1418
|
+
const chh = capLines.length * clh + (capLines.length ? 4 : 0);
|
|
1419
|
+
const dx = Math.max(pw, cw) / 2;
|
|
1420
|
+
const dy0 = ph ? -ph / 2 : 0;
|
|
1421
|
+
const dy1 = (ph ? ph / 2 : 0) + chh;
|
|
1422
|
+
const rotate = it.e.props.rotate === "true" || it.e.props.rotate === "";
|
|
1423
|
+
const boxAt = (p) => rotate ? {
|
|
1424
|
+
x: p.x + dy0,
|
|
1425
|
+
y: p.y - dx,
|
|
1426
|
+
w: dy1 - dy0,
|
|
1427
|
+
h: dx * 2
|
|
1428
|
+
} : {
|
|
1429
|
+
x: p.x - dx,
|
|
1430
|
+
y: p.y + dy0,
|
|
1431
|
+
w: dx * 2,
|
|
1432
|
+
h: dy1 - dy0
|
|
1433
|
+
};
|
|
1434
|
+
const anchor = it.labelOff ? add(pointAtHalf(samples), it.labelOff) : labelSpot(samples, boxAt, it, ctx);
|
|
1435
|
+
const box = boxAt(anchor);
|
|
1436
|
+
label = {
|
|
1437
|
+
text: it.e.label ?? "",
|
|
1438
|
+
caption,
|
|
1439
|
+
...box,
|
|
1440
|
+
anchor,
|
|
1441
|
+
pill: {
|
|
1442
|
+
w: pw,
|
|
1443
|
+
h: ph
|
|
1444
|
+
},
|
|
1445
|
+
lineHeight: lh,
|
|
1446
|
+
captionLineHeight: clh,
|
|
1447
|
+
rotate
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
const heads = [];
|
|
1451
|
+
const hl = headSize(style.width);
|
|
1452
|
+
const endDir = endDirection(geo, "end");
|
|
1453
|
+
const startDir = endDirection(geo, "start");
|
|
1454
|
+
if (it.headKind !== "none") {
|
|
1455
|
+
heads.push({
|
|
1456
|
+
at: E,
|
|
1457
|
+
angle: Math.atan2(endDir.y, endDir.x),
|
|
1458
|
+
kind: it.headKind
|
|
1459
|
+
});
|
|
1460
|
+
trim(geo, "end", trimFor(it.headKind, hl, style.width));
|
|
1461
|
+
}
|
|
1462
|
+
if (it.tailKind !== "none") {
|
|
1463
|
+
heads.push({
|
|
1464
|
+
at: S,
|
|
1465
|
+
angle: Math.atan2(startDir.y, startDir.x),
|
|
1466
|
+
kind: it.tailKind
|
|
1467
|
+
});
|
|
1468
|
+
trim(geo, "start", trimFor(it.tailKind, hl, style.width));
|
|
1469
|
+
}
|
|
1470
|
+
return {
|
|
1471
|
+
key: it.e.key,
|
|
1472
|
+
decl: it.e,
|
|
1473
|
+
d: pathData(geo),
|
|
1474
|
+
samples,
|
|
1475
|
+
start: S,
|
|
1476
|
+
end: E,
|
|
1477
|
+
sides: it.sides,
|
|
1478
|
+
via: it.via,
|
|
1479
|
+
manualVia: it.manualVia,
|
|
1480
|
+
label,
|
|
1481
|
+
heads,
|
|
1482
|
+
style
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
const LABEL_TRIES = [
|
|
1486
|
+
.5,
|
|
1487
|
+
.42,
|
|
1488
|
+
.58,
|
|
1489
|
+
.34,
|
|
1490
|
+
.66,
|
|
1491
|
+
.26,
|
|
1492
|
+
.74
|
|
1493
|
+
];
|
|
1494
|
+
const hits = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
|
|
1495
|
+
const inside = (a, b) => a.x >= b.x && a.y >= b.y && a.x + a.w <= b.x + b.w && a.y + a.h <= b.y + b.h;
|
|
1496
|
+
/**
|
|
1497
|
+
* Where along the edge the label goes: the middle, unless that lands on a
|
|
1498
|
+
* node, a group header or straddles a group border; then the nearest clear spot.
|
|
1499
|
+
*/
|
|
1500
|
+
function labelSpot(samples, boxAt, it, ctx) {
|
|
1501
|
+
let best = pointAt(samples, .5);
|
|
1502
|
+
let bestCost = 1e400;
|
|
1503
|
+
for (const t of LABEL_TRIES) {
|
|
1504
|
+
const p = pointAt(samples, t);
|
|
1505
|
+
const b = boxAt(p);
|
|
1506
|
+
let cost = Math.abs(t - .5);
|
|
1507
|
+
for (const o of ctx.obstacles) if (hits(b, o)) cost += 4;
|
|
1508
|
+
for (const g of ctx.groups ?? []) if (hits(b, g.header)) cost += 3;
|
|
1509
|
+
else if (hits(b, g.box) && !inside(b, g.box)) cost += 2;
|
|
1510
|
+
if (cost < bestCost) {
|
|
1511
|
+
bestCost = cost;
|
|
1512
|
+
best = p;
|
|
1513
|
+
}
|
|
1514
|
+
if (cost < 1) break;
|
|
1515
|
+
}
|
|
1516
|
+
return best;
|
|
1517
|
+
}
|
|
1518
|
+
const headSize = (w) => 7 + w * 2;
|
|
1519
|
+
function trimFor(kind, hl, w) {
|
|
1520
|
+
const m = MARKER_KINDS.get(kind);
|
|
1521
|
+
if (!m) return 0;
|
|
1522
|
+
return typeof m.inset === "function" ? m.inset(hl, w) : m.inset ?? 0;
|
|
1523
|
+
}
|
|
1524
|
+
/** Unit direction the path travels at its start or end (pointing outwards at the end, inwards at the start reversed). */
|
|
1525
|
+
function endDirection(g, which) {
|
|
1526
|
+
if (g.kind === "poly") {
|
|
1527
|
+
const p = g.pts;
|
|
1528
|
+
if (which === "end") {
|
|
1529
|
+
for (let i = p.length - 2; i >= 0; i--) if (dist(p[i], p[p.length - 1]) > .5) return norm(sub(p[p.length - 1], p[i]));
|
|
1530
|
+
} else for (let i = 1; i < p.length; i++) if (dist(p[i], p[0]) > .5) return norm(sub(p[0], p[i]));
|
|
1531
|
+
return {
|
|
1532
|
+
x: 1,
|
|
1533
|
+
y: 0
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1536
|
+
if (which === "end") {
|
|
1537
|
+
const [p0, , c2, p1] = g.segs[g.segs.length - 1];
|
|
1538
|
+
return norm(sub(p1, dist(c2, p1) > .5 ? c2 : p0));
|
|
1539
|
+
}
|
|
1540
|
+
const [p0, c1, , p1] = g.segs[0];
|
|
1541
|
+
return norm(sub(p0, dist(c1, p0) > .5 ? c1 : p1));
|
|
1542
|
+
}
|
|
1543
|
+
function trim(g, which, amount) {
|
|
1544
|
+
if (amount <= 0) return;
|
|
1545
|
+
if (g.kind === "poly") {
|
|
1546
|
+
const p = g.pts;
|
|
1547
|
+
const [i, j] = which === "end" ? [p.length - 1, p.length - 2] : [0, 1];
|
|
1548
|
+
const seg = dist(p[i], p[j]);
|
|
1549
|
+
if (seg < .5) return;
|
|
1550
|
+
p[i] = add(p[i], norm(sub(p[j], p[i])), Math.min(amount, seg * .9));
|
|
1551
|
+
return;
|
|
1552
|
+
}
|
|
1553
|
+
if (which === "end") {
|
|
1554
|
+
const s = g.segs[g.segs.length - 1];
|
|
1555
|
+
const dir = norm(sub(dist(s[2], s[3]) > .5 ? s[2] : s[0], s[3]));
|
|
1556
|
+
s[3] = add(s[3], dir, amount);
|
|
1557
|
+
if (dist(s[2], s[3]) < amount) s[2] = add(s[3], dir, amount);
|
|
1558
|
+
} else {
|
|
1559
|
+
const s = g.segs[0];
|
|
1560
|
+
const dir = norm(sub(dist(s[1], s[0]) > .5 ? s[1] : s[3], s[0]));
|
|
1561
|
+
s[0] = add(s[0], dir, amount);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
function smoothRoute(S, n0, E, n1, via) {
|
|
1565
|
+
if (!via.length) {
|
|
1566
|
+
const dd = dist(S, E);
|
|
1567
|
+
const facing = n0.x * (E.x - S.x) + n0.y * (E.y - S.y) > 0;
|
|
1568
|
+
const k = Math.max(28, Math.min(facing ? 160 : 220, dd * (facing ? .42 : .6)));
|
|
1569
|
+
return [[
|
|
1570
|
+
S,
|
|
1571
|
+
add(S, n0, k),
|
|
1572
|
+
add(E, n1, k),
|
|
1573
|
+
E
|
|
1574
|
+
]];
|
|
1575
|
+
}
|
|
1576
|
+
const P = [
|
|
1577
|
+
S,
|
|
1578
|
+
...via,
|
|
1579
|
+
E
|
|
1580
|
+
];
|
|
1581
|
+
const T = P.map((_, i) => {
|
|
1582
|
+
if (i === 0) return n0;
|
|
1583
|
+
if (i === P.length - 1) return {
|
|
1584
|
+
x: -n1.x,
|
|
1585
|
+
y: -n1.y
|
|
1586
|
+
};
|
|
1587
|
+
return norm(sub(P[i + 1], P[i - 1]));
|
|
1588
|
+
});
|
|
1589
|
+
const segs = [];
|
|
1590
|
+
for (let i = 0; i < P.length - 1; i++) {
|
|
1591
|
+
const l = dist(P[i], P[i + 1]);
|
|
1592
|
+
const k0 = i === 0 ? l * .45 : l / 3;
|
|
1593
|
+
const k1 = i === P.length - 2 ? l * .45 : l / 3;
|
|
1594
|
+
segs.push([
|
|
1595
|
+
P[i],
|
|
1596
|
+
add(P[i], T[i], k0),
|
|
1597
|
+
add(P[i + 1], T[i + 1], -k1),
|
|
1598
|
+
P[i + 1]
|
|
1599
|
+
]);
|
|
1600
|
+
}
|
|
1601
|
+
return segs;
|
|
1602
|
+
}
|
|
1603
|
+
const STUB = 18;
|
|
1604
|
+
function orthoRoute(S, n0, E, n1, via, a, b, obstacles) {
|
|
1605
|
+
const A = add(S, n0, STUB);
|
|
1606
|
+
const B = add(E, n1, STUB);
|
|
1607
|
+
const pts = [S, A];
|
|
1608
|
+
let cur = A;
|
|
1609
|
+
let heading = n0.x !== 0 ? "h" : "v";
|
|
1610
|
+
for (const T of via) {
|
|
1611
|
+
if (Math.abs(T.x - cur.x) < .5 || Math.abs(T.y - cur.y) < .5) pts.push(T);
|
|
1612
|
+
else if (heading === "h") {
|
|
1613
|
+
pts.push({
|
|
1614
|
+
x: T.x,
|
|
1615
|
+
y: cur.y
|
|
1616
|
+
}, T);
|
|
1617
|
+
heading = "v";
|
|
1618
|
+
} else {
|
|
1619
|
+
pts.push({
|
|
1620
|
+
x: cur.x,
|
|
1621
|
+
y: T.y
|
|
1622
|
+
}, T);
|
|
1623
|
+
heading = "h";
|
|
1624
|
+
}
|
|
1625
|
+
cur = T;
|
|
1626
|
+
}
|
|
1627
|
+
const xs = [
|
|
1628
|
+
(cur.x + B.x) / 2,
|
|
1629
|
+
Math.min(a.x, b.x) - 24,
|
|
1630
|
+
Math.max(a.x + a.w, b.x + b.w) + 24
|
|
1631
|
+
];
|
|
1632
|
+
const ys = [
|
|
1633
|
+
(cur.y + B.y) / 2,
|
|
1634
|
+
Math.min(a.y, b.y) - 24,
|
|
1635
|
+
Math.max(a.y + a.h, b.y + b.h) + 24
|
|
1636
|
+
];
|
|
1637
|
+
const candidates = [[{
|
|
1638
|
+
x: B.x,
|
|
1639
|
+
y: cur.y
|
|
1640
|
+
}], [{
|
|
1641
|
+
x: cur.x,
|
|
1642
|
+
y: B.y
|
|
1643
|
+
}]];
|
|
1644
|
+
for (const mx of xs) candidates.push([{
|
|
1645
|
+
x: mx,
|
|
1646
|
+
y: cur.y
|
|
1647
|
+
}, {
|
|
1648
|
+
x: mx,
|
|
1649
|
+
y: B.y
|
|
1650
|
+
}]);
|
|
1651
|
+
for (const my of ys) candidates.push([{
|
|
1652
|
+
x: cur.x,
|
|
1653
|
+
y: my
|
|
1654
|
+
}, {
|
|
1655
|
+
x: B.x,
|
|
1656
|
+
y: my
|
|
1657
|
+
}]);
|
|
1658
|
+
let x0 = 1e400, y0 = 1e400, x1 = -1e400, y1 = -1e400;
|
|
1659
|
+
for (const q of [
|
|
1660
|
+
...pts,
|
|
1661
|
+
...candidates.flat(),
|
|
1662
|
+
B,
|
|
1663
|
+
E
|
|
1664
|
+
]) {
|
|
1665
|
+
x0 = Math.min(x0, q.x);
|
|
1666
|
+
y0 = Math.min(y0, q.y);
|
|
1667
|
+
x1 = Math.max(x1, q.x);
|
|
1668
|
+
y1 = Math.max(y1, q.y);
|
|
1669
|
+
}
|
|
1670
|
+
const near = obstacles.filter((o) => o.x < x1 && o.x + o.w > x0 && o.y < y1 && o.y + o.h > y0);
|
|
1671
|
+
let best = null;
|
|
1672
|
+
let bestScore = 1e400;
|
|
1673
|
+
for (const c of candidates) {
|
|
1674
|
+
const full = simplify([
|
|
1675
|
+
...pts,
|
|
1676
|
+
...c,
|
|
1677
|
+
B,
|
|
1678
|
+
E
|
|
1679
|
+
]);
|
|
1680
|
+
const s = scoreOrtho(full, near);
|
|
1681
|
+
if (s < bestScore) {
|
|
1682
|
+
bestScore = s;
|
|
1683
|
+
best = full;
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
return best;
|
|
1687
|
+
}
|
|
1688
|
+
function dedupe(p) {
|
|
1689
|
+
const out = [];
|
|
1690
|
+
for (const q of p) if (!out.length || dist(out[out.length - 1], q) > .5) out.push(q);
|
|
1691
|
+
return out;
|
|
1692
|
+
}
|
|
1693
|
+
function simplify(p) {
|
|
1694
|
+
const d = dedupe(p);
|
|
1695
|
+
const out = [];
|
|
1696
|
+
for (let i = 0; i < d.length; i++) {
|
|
1697
|
+
if (out.length >= 1 && i < d.length - 1) {
|
|
1698
|
+
const prev = out[out.length - 1];
|
|
1699
|
+
const next = d[i + 1];
|
|
1700
|
+
const v1 = norm(sub(d[i], prev));
|
|
1701
|
+
const v2 = norm(sub(next, d[i]));
|
|
1702
|
+
if (Math.abs(v1.x * v2.y - v1.y * v2.x) < 1e-6 && v1.x * v2.x + v1.y * v2.y > 0) continue;
|
|
1703
|
+
}
|
|
1704
|
+
out.push(d[i]);
|
|
1705
|
+
}
|
|
1706
|
+
return out;
|
|
1707
|
+
}
|
|
1708
|
+
function segHitsBox(p, q, b) {
|
|
1709
|
+
const m = 2;
|
|
1710
|
+
const x0 = b.x + m;
|
|
1711
|
+
const x1 = b.x + b.w - m;
|
|
1712
|
+
const y0 = b.y + m;
|
|
1713
|
+
const y1 = b.y + b.h - m;
|
|
1714
|
+
if (Math.abs(p.y - q.y) < .5) {
|
|
1715
|
+
if (p.y <= y0 || p.y >= y1) return false;
|
|
1716
|
+
return Math.max(p.x, q.x) > x0 && Math.min(p.x, q.x) < x1;
|
|
1717
|
+
}
|
|
1718
|
+
if (Math.abs(p.x - q.x) < .5) {
|
|
1719
|
+
if (p.x <= x0 || p.x >= x1) return false;
|
|
1720
|
+
return Math.max(p.y, q.y) > y0 && Math.min(p.y, q.y) < y1;
|
|
1721
|
+
}
|
|
1722
|
+
return false;
|
|
1723
|
+
}
|
|
1724
|
+
function scoreOrtho(p, obstacles) {
|
|
1725
|
+
let length = 0;
|
|
1726
|
+
let reversals = 0;
|
|
1727
|
+
let hits = 0;
|
|
1728
|
+
for (let i = 1; i < p.length; i++) {
|
|
1729
|
+
length += dist(p[i - 1], p[i]);
|
|
1730
|
+
if (i >= 2) {
|
|
1731
|
+
const v1 = norm(sub(p[i - 1], p[i - 2]));
|
|
1732
|
+
const v2 = norm(sub(p[i], p[i - 1]));
|
|
1733
|
+
if (v1.x * v2.x + v1.y * v2.y < -.5) reversals++;
|
|
1734
|
+
}
|
|
1735
|
+
for (const o of obstacles) if (segHitsBox(p[i - 1], p[i], o)) hits++;
|
|
1736
|
+
}
|
|
1737
|
+
return length + (p.length - 2) * 18 + reversals * 600 + hits * 400;
|
|
1738
|
+
}
|
|
1739
|
+
function sample(g) {
|
|
1740
|
+
if (g.kind === "poly") return g.pts.map((p) => ({ ...p }));
|
|
1741
|
+
const out = [];
|
|
1742
|
+
for (const [p0, c1, c2, p1] of g.segs) for (let i = out.length ? 1 : 0; i <= 16; i++) out.push(bezier(p0, c1, c2, p1, i / 16));
|
|
1743
|
+
return out;
|
|
1744
|
+
}
|
|
1745
|
+
function bezier(p0, c1, c2, p1, t) {
|
|
1746
|
+
const u = 1 - t;
|
|
1747
|
+
return {
|
|
1748
|
+
x: u * u * u * p0.x + 3 * u * u * t * c1.x + 3 * u * t * t * c2.x + t * t * t * p1.x,
|
|
1749
|
+
y: u * u * u * p0.y + 3 * u * u * t * c1.y + 3 * u * t * t * c2.y + t * t * t * p1.y
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
function pointAtHalf(pts) {
|
|
1753
|
+
return pointAt(pts, .5);
|
|
1754
|
+
}
|
|
1755
|
+
/** the point at fraction `t` of the polyline's length */
|
|
1756
|
+
function pointAt(pts, t) {
|
|
1757
|
+
let total = 0;
|
|
1758
|
+
for (let i = 1; i < pts.length; i++) total += dist(pts[i - 1], pts[i]);
|
|
1759
|
+
let half = total * t;
|
|
1760
|
+
for (let i = 1; i < pts.length; i++) {
|
|
1761
|
+
const l = dist(pts[i - 1], pts[i]);
|
|
1762
|
+
if (half <= l && l > 0) {
|
|
1763
|
+
const t = half / l;
|
|
1764
|
+
return {
|
|
1765
|
+
x: pts[i - 1].x + (pts[i].x - pts[i - 1].x) * t,
|
|
1766
|
+
y: pts[i - 1].y + (pts[i].y - pts[i - 1].y) * t
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
half -= l;
|
|
1770
|
+
}
|
|
1771
|
+
return pts[0] ?? {
|
|
1772
|
+
x: 0,
|
|
1773
|
+
y: 0
|
|
1774
|
+
};
|
|
1775
|
+
}
|
|
1776
|
+
function pathData(g) {
|
|
1777
|
+
if (g.kind === "cubic") {
|
|
1778
|
+
let s = `M${r2(g.segs[0][0].x)},${r2(g.segs[0][0].y)}`;
|
|
1779
|
+
for (const [, c1, c2, p1] of g.segs) s += `C${r2(c1.x)},${r2(c1.y)} ${r2(c2.x)},${r2(c2.y)} ${r2(p1.x)},${r2(p1.y)}`;
|
|
1780
|
+
return s;
|
|
1781
|
+
}
|
|
1782
|
+
const p = dedupe(g.pts);
|
|
1783
|
+
let s = `M${r2(p[0].x)},${r2(p[0].y)}`;
|
|
1784
|
+
for (let i = 1; i < p.length - 1; i++) {
|
|
1785
|
+
const prev = p[i - 1];
|
|
1786
|
+
const c = p[i];
|
|
1787
|
+
const next = p[i + 1];
|
|
1788
|
+
const r = Math.min(g.radius, dist(prev, c) / 2, dist(c, next) / 2);
|
|
1789
|
+
if (r < .5) {
|
|
1790
|
+
s += `L${r2(c.x)},${r2(c.y)}`;
|
|
1791
|
+
continue;
|
|
1792
|
+
}
|
|
1793
|
+
const a = add(c, norm(sub(prev, c)), r);
|
|
1794
|
+
const b = add(c, norm(sub(next, c)), r);
|
|
1795
|
+
s += `L${r2(a.x)},${r2(a.y)}Q${r2(c.x)},${r2(c.y)} ${r2(b.x)},${r2(b.y)}`;
|
|
1796
|
+
}
|
|
1797
|
+
const last = p[p.length - 1];
|
|
1798
|
+
return s + `L${r2(last.x)},${r2(last.y)}`;
|
|
1799
|
+
}
|
|
1800
|
+
// src/core/extend.ts
|
|
1801
|
+
const SHAPE_KINDS = /* @__PURE__ */ new Map();
|
|
1802
|
+
const MARKER_KINDS = /* @__PURE__ */ new Map();
|
|
1803
|
+
const AFTER_LAYOUT = [];
|
|
1804
|
+
const POST_RENDER = [];
|
|
1805
|
+
/**
|
|
1806
|
+
* An edge end marker with its tip at `at`, pointing along `angle`: any
|
|
1807
|
+
* registered marker (the built-in ones come from the `markers` plugin). `L` is its length; graph
|
|
1808
|
+
* edges size it from the stroke width, panels pass their own.
|
|
1809
|
+
*/
|
|
1810
|
+
function renderHead(at, angle, kind, color, w, L = headSize(w)) {
|
|
1811
|
+
const m = MARKER_KINDS.get(kind);
|
|
1812
|
+
return m ? m.render(at, angle, {
|
|
1813
|
+
color,
|
|
1814
|
+
width: w,
|
|
1815
|
+
size: L
|
|
1816
|
+
}) : "";
|
|
1817
|
+
}
|
|
1818
|
+
// src/core/shapes.ts
|
|
1819
|
+
/**
|
|
1820
|
+
* The registered node shapes, in registration order. The built-in ones come
|
|
1821
|
+
* from the `shapes` plugin; `use()` adds every plugin's shapes here.
|
|
1822
|
+
*/
|
|
1823
|
+
const SHAPES = [];
|
|
1824
|
+
/** other names for a shape (`db` → `cylinder`), filled by `use()` from `ShapeKind.aliases` */
|
|
1825
|
+
const SHAPE_ALIASES = table({});
|
|
1826
|
+
function normalizeShape(s) {
|
|
1827
|
+
if (!s) return "rounded";
|
|
1828
|
+
const k = s.toLowerCase();
|
|
1829
|
+
if (SHAPE_KINDS.has(k)) return k;
|
|
1830
|
+
return SHAPE_ALIASES[k] ?? "rounded";
|
|
1831
|
+
}
|
|
1832
|
+
/** the outer size of a rounded box around content of `cw` × `ch`: the default for shapes without `size` */
|
|
1833
|
+
function boxSize(cw, ch) {
|
|
1834
|
+
return {
|
|
1835
|
+
w: Math.max(cw + 44, 96),
|
|
1836
|
+
h: Math.max(ch + 28, 48)
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
1839
|
+
/** Grow a content box (text size) into the outer size for a shape. */
|
|
1840
|
+
function sizeForShape(shape, cw, ch) {
|
|
1841
|
+
return SHAPE_KINDS.get(shape)?.size?.(cw, ch) ?? boxSize(cw, ch);
|
|
1842
|
+
}
|
|
1843
|
+
/** Vertical offset of the text centre inside a shape (e.g. below a cylinder's lid). */
|
|
1844
|
+
function textOffset(shape) {
|
|
1845
|
+
return SHAPE_KINDS.get(shape)?.textOffset ?? 0;
|
|
1846
|
+
}
|
|
1847
|
+
const f = (n) => (Math.round(n * 100) / 100).toString();
|
|
1848
|
+
/** SVG path data for the outline of a shape; a rounded box when no plugin registered it. */
|
|
1849
|
+
function shapePath(shape, b, radius) {
|
|
1850
|
+
const custom = SHAPE_KINDS.get(shape);
|
|
1851
|
+
if (custom) return custom.path(b, radius);
|
|
1852
|
+
return rectPath(b.x, b.y, b.w, b.h, Math.min(radius, b.w / 2, b.h / 2));
|
|
1853
|
+
}
|
|
1854
|
+
/** Extra decoration strokes (cylinder lid line, note fold) drawn over the fill. */
|
|
1855
|
+
function shapeDetail(shape, b) {
|
|
1856
|
+
return SHAPE_KINDS.get(shape)?.detail?.(b) ?? null;
|
|
1857
|
+
}
|
|
1858
|
+
function rectPath(x, y, w, h, r) {
|
|
1859
|
+
if (r <= 0) return `M${f(x)},${f(y)}h${f(w)}v${f(h)}h${f(-w)}Z`;
|
|
1860
|
+
return `M${f(x + r)},${f(y)}H${f(x + w - r)}A${f(r)},${f(r)} 0 0 1 ${f(x + w)},${f(y + r)}V${f(y + h - r)}A${f(r)},${f(r)} 0 0 1 ${f(x + w - r)},${f(y + h)}H${f(x + r)}A${f(r)},${f(r)} 0 0 1 ${f(x)},${f(y + h - r)}V${f(y + r)}A${f(r)},${f(r)} 0 0 1 ${f(x + r)},${f(y)}Z`;
|
|
1861
|
+
}
|
|
1862
|
+
const SIDE_NORMAL = {
|
|
1863
|
+
top: {
|
|
1864
|
+
x: 0,
|
|
1865
|
+
y: -1
|
|
1866
|
+
},
|
|
1867
|
+
right: {
|
|
1868
|
+
x: 1,
|
|
1869
|
+
y: 0
|
|
1870
|
+
},
|
|
1871
|
+
bottom: {
|
|
1872
|
+
x: 0,
|
|
1873
|
+
y: 1
|
|
1874
|
+
},
|
|
1875
|
+
left: {
|
|
1876
|
+
x: -1,
|
|
1877
|
+
y: 0
|
|
1878
|
+
}
|
|
1879
|
+
};
|
|
1880
|
+
/**
|
|
1881
|
+
* Point on the outline of a box for a port on `side`. `t` in [-1, 1] slides
|
|
1882
|
+
* the port along that side (0 = centre); `spread` is how much of the side can
|
|
1883
|
+
* host ports (fraction of its half-length). `depth(horiz, rel, u)` gives the
|
|
1884
|
+
* distance from the centre to the outline, for outlines that are not a box:
|
|
1885
|
+
* `horiz` is true on the top and bottom sides, `rel` the scaled position along
|
|
1886
|
+
* the side and `u` the offset in px.
|
|
1887
|
+
*/
|
|
1888
|
+
function portOn(b, side, t, spread = .75, depth) {
|
|
1889
|
+
const cx = b.x + b.w / 2;
|
|
1890
|
+
const cy = b.y + b.h / 2;
|
|
1891
|
+
const s = Math.max(-1, Math.min(1, t)) * spread;
|
|
1892
|
+
const horiz = side === "top" || side === "bottom";
|
|
1893
|
+
const sign = side === "bottom" || side === "right" ? 1 : -1;
|
|
1894
|
+
const u = s * (horiz ? b.w / 2 : b.h / 2);
|
|
1895
|
+
const v = depth ? depth(horiz, s, u) : horiz ? b.h / 2 : b.w / 2;
|
|
1896
|
+
if (horiz) return {
|
|
1897
|
+
x: cx + u,
|
|
1898
|
+
y: cy + sign * v
|
|
1899
|
+
};
|
|
1900
|
+
return {
|
|
1901
|
+
x: cx + sign * v,
|
|
1902
|
+
y: cy + u
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
/** Point on the outline of a shape for a port on `side`, see `portOn`. */
|
|
1906
|
+
function portPoint(shape, b, side, t) {
|
|
1907
|
+
const custom = SHAPE_KINDS.get(shape);
|
|
1908
|
+
return custom?.port ? custom.port(b, side, t) : portOn(b, side, t);
|
|
1909
|
+
}
|
|
1910
|
+
// src/core/lint.ts
|
|
1911
|
+
const CURVES$1 = [
|
|
1912
|
+
"smooth",
|
|
1913
|
+
"orthogonal",
|
|
1914
|
+
"straight"
|
|
1915
|
+
];
|
|
1916
|
+
/** `none` is core; `use()` adds every plugin's markers (the built-in ones too) in front of it */
|
|
1917
|
+
const HEADS = ["none"];
|
|
1918
|
+
const SIDES$1 = [
|
|
1919
|
+
"top",
|
|
1920
|
+
"right",
|
|
1921
|
+
"bottom",
|
|
1922
|
+
"left",
|
|
1923
|
+
"auto"
|
|
1924
|
+
];
|
|
1925
|
+
/**
|
|
1926
|
+
* Node props that place a node next to another one: the value is that node's
|
|
1927
|
+
* id. `below`/`above`/`right-of`/`left-of` are screen directions, whatever the
|
|
1928
|
+
* diagram's `direction`; `rank` puts the node in the other node's rank.
|
|
1929
|
+
*/
|
|
1930
|
+
const PLACEMENT_PROPS = [
|
|
1931
|
+
"below",
|
|
1932
|
+
"above",
|
|
1933
|
+
"right-of",
|
|
1934
|
+
"left-of",
|
|
1935
|
+
"rank"
|
|
1936
|
+
];
|
|
1937
|
+
/**
|
|
1938
|
+
* Text props every element takes: nodes (label), edges (label), groups and
|
|
1939
|
+
* panels (title). Inline `{color: …, size: …, weight: …, italic | text}` spans
|
|
1940
|
+
* style part of a text.
|
|
1941
|
+
*/
|
|
1942
|
+
const TEXT_PROPS = [
|
|
1943
|
+
"text",
|
|
1944
|
+
"font-size",
|
|
1945
|
+
"font-weight",
|
|
1946
|
+
"italic"
|
|
1947
|
+
];
|
|
1948
|
+
const NODE_PROPS = [
|
|
1949
|
+
"shape",
|
|
1950
|
+
"color",
|
|
1951
|
+
"fill",
|
|
1952
|
+
"stroke",
|
|
1953
|
+
...TEXT_PROPS,
|
|
1954
|
+
"sub",
|
|
1955
|
+
"sub-color",
|
|
1956
|
+
"icon",
|
|
1957
|
+
"border",
|
|
1958
|
+
"stroke-width",
|
|
1959
|
+
"border-style",
|
|
1960
|
+
"dashed",
|
|
1961
|
+
"dotted",
|
|
1962
|
+
"radius",
|
|
1963
|
+
"bold",
|
|
1964
|
+
"shadow",
|
|
1965
|
+
"opacity",
|
|
1966
|
+
"width",
|
|
1967
|
+
"height",
|
|
1968
|
+
"link",
|
|
1969
|
+
"tooltip",
|
|
1970
|
+
"meta",
|
|
1971
|
+
"caps",
|
|
1972
|
+
"mono",
|
|
1973
|
+
"body-size",
|
|
1974
|
+
"body-color",
|
|
1975
|
+
"body-weight",
|
|
1976
|
+
"line-height",
|
|
1977
|
+
"max-width",
|
|
1978
|
+
...PLACEMENT_PROPS
|
|
1979
|
+
];
|
|
1980
|
+
const EDGE_PROPS = [
|
|
1981
|
+
"style",
|
|
1982
|
+
"width",
|
|
1983
|
+
"color",
|
|
1984
|
+
"stroke",
|
|
1985
|
+
"curve",
|
|
1986
|
+
"head",
|
|
1987
|
+
"tail",
|
|
1988
|
+
"flow",
|
|
1989
|
+
"animate",
|
|
1990
|
+
"opacity",
|
|
1991
|
+
...TEXT_PROPS,
|
|
1992
|
+
"label-color",
|
|
1993
|
+
"label-bg",
|
|
1994
|
+
"dashed",
|
|
1995
|
+
"dotted",
|
|
1996
|
+
"thick",
|
|
1997
|
+
"exit",
|
|
1998
|
+
"from-side",
|
|
1999
|
+
"enter",
|
|
2000
|
+
"to-side",
|
|
2001
|
+
"constraint",
|
|
2002
|
+
"caption",
|
|
2003
|
+
"rotate",
|
|
2004
|
+
"length"
|
|
2005
|
+
];
|
|
2006
|
+
const GROUP_PROPS = [
|
|
2007
|
+
"color",
|
|
2008
|
+
"fill",
|
|
2009
|
+
"stroke",
|
|
2010
|
+
...TEXT_PROPS,
|
|
2011
|
+
"border-style",
|
|
2012
|
+
"dashed",
|
|
2013
|
+
"dotted",
|
|
2014
|
+
"meta",
|
|
2015
|
+
"caps"
|
|
2016
|
+
];
|
|
2017
|
+
/** frame props every panel takes, next to the plugin's own */
|
|
2018
|
+
const PANEL_PROPS = [
|
|
2019
|
+
"color",
|
|
2020
|
+
"theme",
|
|
2021
|
+
"background",
|
|
2022
|
+
"fill",
|
|
2023
|
+
"border",
|
|
2024
|
+
"stroke",
|
|
2025
|
+
...TEXT_PROPS,
|
|
2026
|
+
"border-width",
|
|
2027
|
+
"border-style",
|
|
2028
|
+
"radius",
|
|
2029
|
+
"shadow",
|
|
2030
|
+
"frame",
|
|
2031
|
+
"edge-color",
|
|
2032
|
+
"edge-width"
|
|
2033
|
+
];
|
|
2034
|
+
/** allowed values for enum-like props */
|
|
2035
|
+
const ENUMS = table({
|
|
2036
|
+
shape: [...SHAPES, ...Object.keys(SHAPE_ALIASES)],
|
|
2037
|
+
"border-style": LINE_STYLES,
|
|
2038
|
+
style: LINE_STYLES,
|
|
2039
|
+
curve: CURVES$1,
|
|
2040
|
+
head: HEADS,
|
|
2041
|
+
tail: HEADS,
|
|
2042
|
+
exit: SIDES$1,
|
|
2043
|
+
"from-side": SIDES$1,
|
|
2044
|
+
enter: SIDES$1,
|
|
2045
|
+
"to-side": SIDES$1
|
|
2046
|
+
});
|
|
2047
|
+
/** props whose value must be a colour (or `none`/`transparent`), on nodes, edges, groups and panels */
|
|
2048
|
+
const COLORS = [
|
|
2049
|
+
"color",
|
|
2050
|
+
"fill",
|
|
2051
|
+
"stroke",
|
|
2052
|
+
"text",
|
|
2053
|
+
"sub-color",
|
|
2054
|
+
"body-color",
|
|
2055
|
+
"label-color",
|
|
2056
|
+
"label-bg",
|
|
2057
|
+
"background",
|
|
2058
|
+
"edge-color"
|
|
2059
|
+
];
|
|
2060
|
+
const badColor = (v) => ![
|
|
2061
|
+
"none",
|
|
2062
|
+
"transparent",
|
|
2063
|
+
"currentcolor",
|
|
2064
|
+
"inherit"
|
|
2065
|
+
].includes(v.toLowerCase()) && !/^var\(--[\w-]+\)$/.test(v) && !isColor(v);
|
|
2066
|
+
const colorHint = (v) => didYouMean(v, TONES) || `: use a palette name (${TONES.join(", ")}), #hex, rgb(), hsl() or a CSS colour name`;
|
|
2067
|
+
const BOOLS = [
|
|
2068
|
+
"dashed",
|
|
2069
|
+
"dotted",
|
|
2070
|
+
"thick",
|
|
2071
|
+
"bold",
|
|
2072
|
+
"italic",
|
|
2073
|
+
"shadow",
|
|
2074
|
+
"flow",
|
|
2075
|
+
"animate",
|
|
2076
|
+
"constraint",
|
|
2077
|
+
"caps",
|
|
2078
|
+
"mono",
|
|
2079
|
+
"rotate"
|
|
2080
|
+
];
|
|
2081
|
+
const NUMBERS = [
|
|
2082
|
+
"border",
|
|
2083
|
+
"stroke-width",
|
|
2084
|
+
"radius",
|
|
2085
|
+
"font-size",
|
|
2086
|
+
"font-weight",
|
|
2087
|
+
"opacity",
|
|
2088
|
+
"width",
|
|
2089
|
+
"height",
|
|
2090
|
+
"max-width",
|
|
2091
|
+
"length",
|
|
2092
|
+
"body-size",
|
|
2093
|
+
"body-weight",
|
|
2094
|
+
"line-height"
|
|
2095
|
+
];
|
|
2096
|
+
function distance(a, b) {
|
|
2097
|
+
const dp = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
2098
|
+
for (let i = 1; i <= a.length; i++) {
|
|
2099
|
+
let prev = dp[0];
|
|
2100
|
+
dp[0] = i;
|
|
2101
|
+
for (let j = 1; j <= b.length; j++) {
|
|
2102
|
+
const tmp = dp[j];
|
|
2103
|
+
dp[j] = Math.min(dp[j] + 1, dp[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
2104
|
+
prev = tmp;
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
return dp[b.length];
|
|
2108
|
+
}
|
|
2109
|
+
/** the option nearest to `v` and its edit distance, ignoring case */
|
|
2110
|
+
function closest(v, options) {
|
|
2111
|
+
const lv = v.toLowerCase();
|
|
2112
|
+
let best = "";
|
|
2113
|
+
let bestD = 1e400;
|
|
2114
|
+
for (const o of options) {
|
|
2115
|
+
const dd = distance(lv, o.toLowerCase());
|
|
2116
|
+
if (dd < bestD) [best, bestD] = [o, dd];
|
|
2117
|
+
}
|
|
2118
|
+
return [best, bestD];
|
|
2119
|
+
}
|
|
2120
|
+
/** " (did you mean "x"?)" for a close match, else "" */
|
|
2121
|
+
function didYouMean(v, options) {
|
|
2122
|
+
const [best, bestD] = closest(v, options);
|
|
2123
|
+
return bestD <= (v.length <= 3 ? 1 : Math.max(2, Math.floor(v.length / 3))) ? ` (did you mean "${best}"?)` : "";
|
|
2124
|
+
}
|
|
2125
|
+
function checkProps(props, allowed, what, line0, warn, propLines) {
|
|
2126
|
+
for (const [k, v] of Object.entries(props)) {
|
|
2127
|
+
const line = propLines?.[k] ?? line0;
|
|
2128
|
+
if (!allowed.includes(k)) {
|
|
2129
|
+
if (what === "node" && EDGE_PROPS.includes(k)) warn(line, `"${k}" is an edge prop, but here it is on a node. On an edge line, edge props go after a colon: a -> b: [${k}]`);
|
|
2130
|
+
else warn(line, `Unknown ${what} prop "${k}"${didYouMean(k, allowed) || ` (${what} props: ${allowed.join(", ")})`}`);
|
|
2131
|
+
continue;
|
|
2132
|
+
}
|
|
2133
|
+
const opts = ENUMS[k];
|
|
2134
|
+
const word = k === "frame" ? v.trim().split(/\s+/)[0].toLowerCase() : v.toLowerCase();
|
|
2135
|
+
if (COLORS.includes(k) && badColor(v)) warn(line, `Unknown ${k} "${v}"${colorHint(v)}`);
|
|
2136
|
+
else if (opts && !opts.includes(word)) {
|
|
2137
|
+
const shown = k === "shape" ? SHAPES : opts;
|
|
2138
|
+
warn(line, `Unknown ${k} "${v}"${didYouMean(v, opts) || ` (use one of: ${shown.join(", ")})`}`);
|
|
2139
|
+
} else if (BOOLS.includes(k) && !["true", "false"].includes(v)) warn(line, `${k} expects true or false, got "${v}"`);
|
|
2140
|
+
else if (NUMBERS.includes(k) && !(parseFloat(v) >= 0 && Number.isFinite(parseFloat(v)))) warn(line, `${k} expects a number of 0 or more, got "${v}"`);
|
|
2141
|
+
else if (k === "link" && !isSafeLink(v)) warn(line, `link "${v}" is not rendered: use http(s), mailto, an #anchor or a relative path`);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
/** a panel's own header props against the types its `options` declare */
|
|
2145
|
+
function checkPanelOptions(props, options, line, warn) {
|
|
2146
|
+
if (!options) return;
|
|
2147
|
+
for (const [k, v] of Object.entries(props)) {
|
|
2148
|
+
const o = options[k];
|
|
2149
|
+
if (o === void 0 || o === "text") continue;
|
|
2150
|
+
if (o === "color") {
|
|
2151
|
+
if (!isColor(v)) warn(line, `Unknown ${k} "${v}"${didYouMean(v, TONES) || ": use a palette name, #hex, rgb(), hsl() or a CSS colour name"}`);
|
|
2152
|
+
} else if (o === "number") {
|
|
2153
|
+
if (!Number.isFinite(parseFloat(v))) warn(line, `${k} expects a number, got "${v}"`);
|
|
2154
|
+
} else if (o === "flag") {
|
|
2155
|
+
if (!["true", "false"].includes(v)) warn(line, `${k} expects true or false, got "${v}"`);
|
|
2156
|
+
} else if (!o.includes(v.toLowerCase())) warn(line, `Unknown ${k} "${v}"${didYouMean(v, [...o]) || ` (use one of: ${o.join(", ")})`}`);
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* The placement prop a node uses (the first one written) and its anchor id.
|
|
2161
|
+
* Layout reads the same choice, so the warnings below describe what it does.
|
|
2162
|
+
*/
|
|
2163
|
+
/** the 0-based line where a node's prop `key` was written: its own line below, its `[...]`, or the declaration */
|
|
2164
|
+
const propLine = (n, key) => n.ref?.propLines?.[key] ?? n.propAt?.[key] ?? n.ref?.line;
|
|
2165
|
+
function placementOf(props) {
|
|
2166
|
+
for (const [k, v] of Object.entries(props)) if (PLACEMENT_PROPS.includes(k) && v) return [k, v.trim()];
|
|
2167
|
+
}
|
|
2168
|
+
/** placement props: the anchor must be another node, one prop per node, no loops */
|
|
2169
|
+
function checkPlacement(d, warn) {
|
|
2170
|
+
const anchor = /* @__PURE__ */ new Map();
|
|
2171
|
+
for (const n of d.nodes.values()) {
|
|
2172
|
+
const p = effectiveProps(d, n);
|
|
2173
|
+
const used = placementOf(p);
|
|
2174
|
+
if (!used) continue;
|
|
2175
|
+
const [key, id] = used;
|
|
2176
|
+
const line = propLine(n, key);
|
|
2177
|
+
for (const k of Object.keys(p)) if (k !== key && PLACEMENT_PROPS.includes(k) && n.ref) warn(propLine(n, k), `${n.id} already has ${key}: ${id}, so ${k} is ignored. A node takes one placement prop`);
|
|
2178
|
+
if (id === n.id) {
|
|
2179
|
+
if (n.ref) warn(line, `${key}: ${id} places ${n.id} next to itself; use another node's id`);
|
|
2180
|
+
continue;
|
|
2181
|
+
}
|
|
2182
|
+
if (!d.nodes.has(id)) {
|
|
2183
|
+
if (!n.ref) continue;
|
|
2184
|
+
if (d.groups.has(id)) warn(line, `${key}: ${id} names a group; placement takes a node id`);
|
|
2185
|
+
else warn(line, `${key}: unknown node "${id}"${didYouMean(id, [...d.nodes.keys()])}`);
|
|
2186
|
+
continue;
|
|
2187
|
+
}
|
|
2188
|
+
anchor.set(n.id, id);
|
|
2189
|
+
}
|
|
2190
|
+
const reported = /* @__PURE__ */ new Set();
|
|
2191
|
+
for (const start of anchor.keys()) {
|
|
2192
|
+
const chain = [];
|
|
2193
|
+
let cur = start;
|
|
2194
|
+
while (cur !== void 0 && !chain.includes(cur)) {
|
|
2195
|
+
chain.push(cur);
|
|
2196
|
+
cur = anchor.get(cur);
|
|
2197
|
+
}
|
|
2198
|
+
if (cur !== start || reported.has(start)) continue;
|
|
2199
|
+
chain.forEach((id) => reported.add(id));
|
|
2200
|
+
const n = d.nodes.get(start);
|
|
2201
|
+
const key = placementOf(effectiveProps(d, n))[0];
|
|
2202
|
+
if (n.ref) warn(propLine(n, key), `placement loop: ${[...chain, start].join(" → ")}. These nodes cannot all be placed; remove one placement prop`);
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
/** ids that differ only in their numbers (`s1`, `s2`) are a series, not a typo */
|
|
2206
|
+
const series = (id) => id.toLowerCase().replace(/\d+/g, "#");
|
|
2207
|
+
/**
|
|
2208
|
+
* A node that only an edge mentions, while a declared node has an id one or
|
|
2209
|
+
* two edits away, is almost always a typo: `api -> dbb` next to `db "Postgres"`
|
|
2210
|
+
* draws a second, unlabelled box. Short ids are skipped (`a`, `b` are a series).
|
|
2211
|
+
*/
|
|
2212
|
+
function checkNodeTypos(d, warn) {
|
|
2213
|
+
const nodes = [...d.nodes.values()];
|
|
2214
|
+
const declared = nodes.filter((n) => !n.implicit).map((n) => ({
|
|
2215
|
+
id: n.id,
|
|
2216
|
+
series: series(n.id)
|
|
2217
|
+
}));
|
|
2218
|
+
if (!declared.length || nodes.every((n) => !n.implicit || n.id.length < 3)) return;
|
|
2219
|
+
const edgeLine = /* @__PURE__ */ new Map();
|
|
2220
|
+
for (const e of d.edges) if (e.ref) {
|
|
2221
|
+
for (const id of [e.from, e.to]) if (!edgeLine.has(id)) edgeLine.set(id, e.ref.line);
|
|
2222
|
+
}
|
|
2223
|
+
for (const n of nodes) {
|
|
2224
|
+
const line = edgeLine.get(n.id);
|
|
2225
|
+
if (!n.implicit || n.id.length < 3 || line === void 0) continue;
|
|
2226
|
+
const s = series(n.id);
|
|
2227
|
+
const max = n.id.length < 7 ? 1 : 2;
|
|
2228
|
+
const options = declared.filter((o) => o.series !== s && Math.abs(o.id.length - n.id.length) <= max);
|
|
2229
|
+
const [best, bestD] = closest(n.id, options.map((o) => o.id));
|
|
2230
|
+
if (bestD <= max) warn(line, `Node "${n.id}" is only used in edges (did you mean "${best}"?). If it is a separate node, declare it: ${n.id} "Label"`);
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
function lint(d, panelOptions) {
|
|
2234
|
+
const warn = (line, message) => d.diagnostics.push({
|
|
2235
|
+
line: line + 1,
|
|
2236
|
+
message,
|
|
2237
|
+
severity: "warning"
|
|
2238
|
+
});
|
|
2239
|
+
for (const n of d.nodes.values()) if (n.ref) checkProps(n.props, NODE_PROPS, "node", n.ref.line, warn, {
|
|
2240
|
+
...n.propAt,
|
|
2241
|
+
...n.ref.propLines
|
|
2242
|
+
});
|
|
2243
|
+
checkPlacement(d, warn);
|
|
2244
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2245
|
+
for (const e of d.edges) {
|
|
2246
|
+
if (!e.ref) continue;
|
|
2247
|
+
const line = e.ref.line;
|
|
2248
|
+
if (seen.has(line)) continue;
|
|
2249
|
+
seen.add(line);
|
|
2250
|
+
checkProps(e.props, EDGE_PROPS, "edge", line, warn, e.ref.propLines);
|
|
2251
|
+
}
|
|
2252
|
+
for (const g of d.groups.values()) if (g.ref) checkProps(g.props, GROUP_PROPS, "group", g.ref.line, warn, g.ref.propLines);
|
|
2253
|
+
checkNodeTypos(d, warn);
|
|
2254
|
+
const names = themeNames(d.themes);
|
|
2255
|
+
for (const p of d.panels) {
|
|
2256
|
+
if (!p.ref) continue;
|
|
2257
|
+
const line = p.lines?.[0] ?? p.ref.line;
|
|
2258
|
+
const pp = p.props;
|
|
2259
|
+
if (pp.theme && !names.includes(pp.theme)) warn(line, `Unknown theme "${pp.theme}"${didYouMean(pp.theme, names) || ` (themes: ${names.join(", ")})`}`);
|
|
2260
|
+
for (const k of COLORS) if (pp[k] && badColor(pp[k])) warn(line, `Unknown ${k} "${pp[k]}"${colorHint(pp[k])}`);
|
|
2261
|
+
const b = pp.border;
|
|
2262
|
+
if (b && !["none", "false"].includes(b) && !isColor(b) && !Number.isFinite(parseFloat(b))) warn(line, `border takes a colour, a width or none, got "${b}"`);
|
|
2263
|
+
if (pp["border-style"] && !LINE_STYLES.includes(pp["border-style"])) warn(line, `Unknown border-style "${pp["border-style"]}" (use one of: ${LINE_STYLES.join(", ")})`);
|
|
2264
|
+
checkPanelOptions(pp, panelOptions?.(p.kind), line, warn);
|
|
2265
|
+
}
|
|
2266
|
+
const any = [.../* @__PURE__ */ new Set([
|
|
2267
|
+
...NODE_PROPS,
|
|
2268
|
+
...EDGE_PROPS,
|
|
2269
|
+
...GROUP_PROPS
|
|
2270
|
+
])];
|
|
2271
|
+
for (const [c, props] of Object.entries(d.classes)) {
|
|
2272
|
+
const line = d.classLines?.[c];
|
|
2273
|
+
if (line !== void 0) checkProps(props, any, `class .${c}`, line, warn);
|
|
2274
|
+
}
|
|
2275
|
+
d.diagnostics.sort((a, b) => a.line - b.line);
|
|
2276
|
+
}
|
|
2277
|
+
// src/core/parse.ts
|
|
2278
|
+
const THEME_KEY_NAMES = Object.keys(THEME_KEYS);
|
|
2279
|
+
const KNOWN_SETTINGS = /* @__PURE__ */ new Set([
|
|
2280
|
+
"title",
|
|
2281
|
+
"subtitle",
|
|
2282
|
+
"theme",
|
|
2283
|
+
"accent",
|
|
2284
|
+
"direction",
|
|
2285
|
+
"curve",
|
|
2286
|
+
"gap",
|
|
2287
|
+
"rank-gap",
|
|
2288
|
+
"max-width",
|
|
2289
|
+
"font",
|
|
2290
|
+
"font-size",
|
|
2291
|
+
"background",
|
|
2292
|
+
"padding",
|
|
2293
|
+
"shadow",
|
|
2294
|
+
"radius",
|
|
2295
|
+
"stroke-width",
|
|
2296
|
+
"shape",
|
|
2297
|
+
"grid",
|
|
2298
|
+
"panels",
|
|
2299
|
+
"panel-gap",
|
|
2300
|
+
"wrap",
|
|
2301
|
+
"edge-color",
|
|
2302
|
+
"edge-width"
|
|
2303
|
+
]);
|
|
2304
|
+
/** other tools' diagram names → the yuhwa panel header to write instead (`ID` is the panel id) */
|
|
2305
|
+
const PANEL_ALIASES = table({
|
|
2306
|
+
pie: "chart ID [type: pie]",
|
|
2307
|
+
donut: "chart ID [type: donut]",
|
|
2308
|
+
bar: "chart ID",
|
|
2309
|
+
line: "chart ID [type: line]",
|
|
2310
|
+
xychart: "chart ID",
|
|
2311
|
+
sequenceDiagram: "sequence ID",
|
|
2312
|
+
gitGraph: "git ID",
|
|
2313
|
+
quadrantChart: "quadrant ID",
|
|
2314
|
+
mindMap: "mindmap ID",
|
|
2315
|
+
roadmap: "gantt ID",
|
|
2316
|
+
board: "kanban ID",
|
|
2317
|
+
tree: "treeview ID",
|
|
2318
|
+
filetree: "treeview ID"
|
|
2319
|
+
});
|
|
2320
|
+
/** panel kinds by keyword, filled by `use` (see plugin.ts) */
|
|
2321
|
+
const PANELS = /* @__PURE__ */ new Map();
|
|
2322
|
+
/** plugin statements by keyword, filled by `use` */
|
|
2323
|
+
const STATEMENTS = /* @__PURE__ */ new Map();
|
|
2324
|
+
/** diagram transforms, run in order at the end of `parse` */
|
|
2325
|
+
const TRANSFORMS = [];
|
|
2326
|
+
/** Mermaid-style direction names are accepted too */
|
|
2327
|
+
/** settings with a fixed set of values; `theme` is checked against the registered themes */
|
|
2328
|
+
const SETTING_VALUES = table({
|
|
2329
|
+
direction: [
|
|
2330
|
+
"right",
|
|
2331
|
+
"down",
|
|
2332
|
+
"left",
|
|
2333
|
+
"up"
|
|
2334
|
+
],
|
|
2335
|
+
curve: [
|
|
2336
|
+
"smooth",
|
|
2337
|
+
"orthogonal",
|
|
2338
|
+
"straight"
|
|
2339
|
+
],
|
|
2340
|
+
panels: [
|
|
2341
|
+
"row",
|
|
2342
|
+
"column",
|
|
2343
|
+
"grid",
|
|
2344
|
+
"wrap"
|
|
2345
|
+
]
|
|
2346
|
+
});
|
|
2347
|
+
const DIRECTION_ALIASES = table({
|
|
2348
|
+
lr: "right",
|
|
2349
|
+
rl: "left",
|
|
2350
|
+
tb: "down",
|
|
2351
|
+
td: "down",
|
|
2352
|
+
bt: "up"
|
|
2353
|
+
});
|
|
2354
|
+
const TONE_SET = /* @__PURE__ */ new Set([...TONES, "violet"]);
|
|
2355
|
+
const CURVES = /* @__PURE__ */ new Set([
|
|
2356
|
+
"smooth",
|
|
2357
|
+
"orthogonal",
|
|
2358
|
+
"straight"
|
|
2359
|
+
]);
|
|
2360
|
+
const isShape = (k) => SHAPE_KINDS.has(k) || k in SHAPE_ALIASES;
|
|
2361
|
+
/**
|
|
2362
|
+
* Imports only read `.yu` (or the older `.yuhwa`) files: a diagram must not pull other files
|
|
2363
|
+
* (keys, configs) into its output or errors, whatever the resolver.
|
|
2364
|
+
*/
|
|
2365
|
+
const isYuhwaPath = (path) => /\.(yu|yuhwa)$/i.test(path);
|
|
2366
|
+
/** Join an import path to the importing file's folder and normalise `.`/`..`. */
|
|
2367
|
+
function joinPath(from, rel) {
|
|
2368
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(rel)) return rel;
|
|
2369
|
+
const base = rel.startsWith("/") || !from ? "" : from.replace(/[^/]*$/, "");
|
|
2370
|
+
const abs = (base + rel).startsWith("/");
|
|
2371
|
+
const out = [];
|
|
2372
|
+
for (const part of (base + rel).split("/")) {
|
|
2373
|
+
if (!part || part === ".") continue;
|
|
2374
|
+
if (part === ".." && out.length && out[out.length - 1] !== "..") out.pop();
|
|
2375
|
+
else if (part === ".." && abs) continue;
|
|
2376
|
+
else out.push(part);
|
|
2377
|
+
}
|
|
2378
|
+
return (abs ? "/" : "") + out.join("/");
|
|
2379
|
+
}
|
|
2380
|
+
const SIDES = /* @__PURE__ */ new Set([
|
|
2381
|
+
"top",
|
|
2382
|
+
"right",
|
|
2383
|
+
"bottom",
|
|
2384
|
+
"left",
|
|
2385
|
+
"auto"
|
|
2386
|
+
]);
|
|
2387
|
+
const IDENT_RE = /^[\p{L}_][\p{L}\p{N}_]*(?:-[\p{L}\p{N}_]+)*/u;
|
|
2388
|
+
const ARROW_RE = /^(<)?(--|-|\.\.|==|=)(>)?/;
|
|
2389
|
+
const CLASS_RE = /^\.[\p{L}_][\p{L}\p{N}_-]*/u;
|
|
2390
|
+
const NUMBER_RE = /^-?\d+(?:\.\d+)?/;
|
|
2391
|
+
function unescape(s) {
|
|
2392
|
+
return s.replace(/\\(n|t|"|'|\\)/g, (_, c) => c === "n" ? "\n" : c === "t" ? " " : c);
|
|
2393
|
+
}
|
|
2394
|
+
/** Split one source line into tokens (strings, `[...]` props, ids, classes, arrows, …). */
|
|
2395
|
+
function lex(line) {
|
|
2396
|
+
const tokens = [];
|
|
2397
|
+
let i = 0;
|
|
2398
|
+
const n = line.length;
|
|
2399
|
+
while (i < n) {
|
|
2400
|
+
const c = line[i];
|
|
2401
|
+
if (/\s/.test(c)) {
|
|
2402
|
+
i++;
|
|
2403
|
+
continue;
|
|
2404
|
+
}
|
|
2405
|
+
if (c === "/" && line[i + 1] === "/" && (i === 0 || /\s/.test(line[i - 1]))) return {
|
|
2406
|
+
tokens,
|
|
2407
|
+
codeEnd: i
|
|
2408
|
+
};
|
|
2409
|
+
const rest = line.slice(i);
|
|
2410
|
+
if (c === "\"" || c === "'") {
|
|
2411
|
+
let j = i + 1;
|
|
2412
|
+
while (j < n && line[j] !== c) j += line[j] === "\\" ? 2 : 1;
|
|
2413
|
+
tokens.push({
|
|
2414
|
+
kind: "string",
|
|
2415
|
+
value: unescape(line.slice(i + 1, j)),
|
|
2416
|
+
start: i,
|
|
2417
|
+
end: Math.min(j + 1, n)
|
|
2418
|
+
});
|
|
2419
|
+
i = j + 1;
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
if (c === "[") {
|
|
2423
|
+
let j = i + 1;
|
|
2424
|
+
let q = null;
|
|
2425
|
+
while (j < n) {
|
|
2426
|
+
const d = line[j];
|
|
2427
|
+
if (q) {
|
|
2428
|
+
if (d === "\\") j++;
|
|
2429
|
+
else if (d === q) q = null;
|
|
2430
|
+
} else if (d === "\"" || d === "'") q = d;
|
|
2431
|
+
else if (d === "]") break;
|
|
2432
|
+
j++;
|
|
2433
|
+
}
|
|
2434
|
+
tokens.push({
|
|
2435
|
+
kind: "props",
|
|
2436
|
+
value: line.slice(i + 1, j),
|
|
2437
|
+
start: i,
|
|
2438
|
+
end: Math.min(j + 1, n)
|
|
2439
|
+
});
|
|
2440
|
+
i = j + 1;
|
|
2441
|
+
continue;
|
|
2442
|
+
}
|
|
2443
|
+
let m;
|
|
2444
|
+
if (c === "$" && (m = rest.match(/^\$[A-Za-z_]\w*/))) {
|
|
2445
|
+
tokens.push({
|
|
2446
|
+
kind: "ident",
|
|
2447
|
+
value: m[0],
|
|
2448
|
+
start: i,
|
|
2449
|
+
end: i + m[0].length
|
|
2450
|
+
});
|
|
2451
|
+
i += m[0].length;
|
|
2452
|
+
continue;
|
|
2453
|
+
}
|
|
2454
|
+
if (m = rest.match(CLASS_RE)) {
|
|
2455
|
+
tokens.push({
|
|
2456
|
+
kind: "class",
|
|
2457
|
+
value: m[0].slice(1),
|
|
2458
|
+
start: i,
|
|
2459
|
+
end: i + m[0].length
|
|
2460
|
+
});
|
|
2461
|
+
i += m[0].length;
|
|
2462
|
+
continue;
|
|
2463
|
+
}
|
|
2464
|
+
if (m = rest.match(IDENT_RE)) {
|
|
2465
|
+
tokens.push({
|
|
2466
|
+
kind: "ident",
|
|
2467
|
+
value: m[0],
|
|
2468
|
+
start: i,
|
|
2469
|
+
end: i + m[0].length
|
|
2470
|
+
});
|
|
2471
|
+
i += m[0].length;
|
|
2472
|
+
continue;
|
|
2473
|
+
}
|
|
2474
|
+
if (m = rest.match(NUMBER_RE)) {
|
|
2475
|
+
tokens.push({
|
|
2476
|
+
kind: "number",
|
|
2477
|
+
value: m[0],
|
|
2478
|
+
start: i,
|
|
2479
|
+
end: i + m[0].length
|
|
2480
|
+
});
|
|
2481
|
+
i += m[0].length;
|
|
2482
|
+
continue;
|
|
2483
|
+
}
|
|
2484
|
+
if (m = rest.match(ARROW_RE)) {
|
|
2485
|
+
tokens.push({
|
|
2486
|
+
kind: "arrow",
|
|
2487
|
+
value: m[0],
|
|
2488
|
+
start: i,
|
|
2489
|
+
end: i + m[0].length
|
|
2490
|
+
});
|
|
2491
|
+
i += m[0].length;
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
if ("{},:".includes(c)) {
|
|
2495
|
+
tokens.push({
|
|
2496
|
+
kind: "punct",
|
|
2497
|
+
value: c,
|
|
2498
|
+
start: i,
|
|
2499
|
+
end: i + 1
|
|
2500
|
+
});
|
|
2501
|
+
i++;
|
|
2502
|
+
continue;
|
|
2503
|
+
}
|
|
2504
|
+
const w = rest.match(/^\S+/);
|
|
2505
|
+
tokens.push({
|
|
2506
|
+
kind: "word",
|
|
2507
|
+
value: w[0],
|
|
2508
|
+
start: i,
|
|
2509
|
+
end: i + w[0].length
|
|
2510
|
+
});
|
|
2511
|
+
i += w[0].length;
|
|
2512
|
+
}
|
|
2513
|
+
return {
|
|
2514
|
+
tokens,
|
|
2515
|
+
codeEnd: n
|
|
2516
|
+
};
|
|
2517
|
+
}
|
|
2518
|
+
/**
|
|
2519
|
+
* The `{` and `}` of a lexed line that open or close a block. Braces after the
|
|
2520
|
+
* first `:` belong to a label or a value (`footer: {teal: v1.0}`), not to the
|
|
2521
|
+
* structure.
|
|
2522
|
+
*/
|
|
2523
|
+
function blockBraces(tokens) {
|
|
2524
|
+
const out = [];
|
|
2525
|
+
for (const t of tokens) {
|
|
2526
|
+
if (t.kind !== "punct") continue;
|
|
2527
|
+
if (t.value === ":") break;
|
|
2528
|
+
if (t.value === "{" || t.value === "}") out.push(t.value);
|
|
2529
|
+
}
|
|
2530
|
+
return out;
|
|
2531
|
+
}
|
|
2532
|
+
/** Merge `[...]` props into a node, noting the line each one came from. */
|
|
2533
|
+
function addNodeProps(n, props, li) {
|
|
2534
|
+
Object.assign(n.props, props);
|
|
2535
|
+
for (const k in props) (n.propAt ??= {})[k] = li;
|
|
2536
|
+
}
|
|
2537
|
+
/** Parse the inside of a `[...]` block: `key: value, key: value, flag` */
|
|
2538
|
+
/** `[...]` after a node id. `A[Web Server]` is Mermaid for a label, not a prop list. */
|
|
2539
|
+
function nodeProps(tok, id, li, warn) {
|
|
2540
|
+
const v = tok.value.trim();
|
|
2541
|
+
if (/\s/.test(v) && !/[:,;]/.test(v)) {
|
|
2542
|
+
warn(li, `[${v}] looks like a Mermaid label. Write: ${id} "${v}" (props are separated by commas)`);
|
|
2543
|
+
return {};
|
|
2544
|
+
}
|
|
2545
|
+
return parseProps(tok.value);
|
|
2546
|
+
}
|
|
2547
|
+
/** a prop name as written → its canonical key: `tone` is the old name of `color` */
|
|
2548
|
+
const propKey = (k) => {
|
|
2549
|
+
const key = k.trim().toLowerCase();
|
|
2550
|
+
return key === "tone" ? "color" : key;
|
|
2551
|
+
};
|
|
2552
|
+
function parseProps(raw = "") {
|
|
2553
|
+
const out = {};
|
|
2554
|
+
const parts = [];
|
|
2555
|
+
let cur = "";
|
|
2556
|
+
let q = null;
|
|
2557
|
+
let depth = 0;
|
|
2558
|
+
for (let i = 0; i < raw.length; i++) {
|
|
2559
|
+
const c = raw[i];
|
|
2560
|
+
if (q) {
|
|
2561
|
+
cur += c;
|
|
2562
|
+
if (c === "\\") cur += raw[++i] ?? "";
|
|
2563
|
+
else if (c === q) q = null;
|
|
2564
|
+
} else if (c === "\"" || c === "'") {
|
|
2565
|
+
q = c;
|
|
2566
|
+
cur += c;
|
|
2567
|
+
} else if (c === "(") {
|
|
2568
|
+
depth++;
|
|
2569
|
+
cur += c;
|
|
2570
|
+
} else if (c === ")") {
|
|
2571
|
+
depth = Math.max(0, depth - 1);
|
|
2572
|
+
cur += c;
|
|
2573
|
+
} else if ((c === "," || c === ";") && !depth) {
|
|
2574
|
+
parts.push(cur);
|
|
2575
|
+
cur = "";
|
|
2576
|
+
} else cur += c;
|
|
2577
|
+
}
|
|
2578
|
+
parts.push(cur);
|
|
2579
|
+
for (const part of parts) {
|
|
2580
|
+
const p = part.trim();
|
|
2581
|
+
if (!p) continue;
|
|
2582
|
+
const colon = p.indexOf(":");
|
|
2583
|
+
if (colon < 0) {
|
|
2584
|
+
const k = p.toLowerCase();
|
|
2585
|
+
if (TONE_SET.has(k)) out.color = k;
|
|
2586
|
+
else if (isShape(k)) out.shape = k;
|
|
2587
|
+
else if (CURVES.has(k)) out.curve = k;
|
|
2588
|
+
else if (isColor(p)) out.color = p;
|
|
2589
|
+
else out[k] = "true";
|
|
2590
|
+
continue;
|
|
2591
|
+
}
|
|
2592
|
+
const key = propKey(p.slice(0, colon));
|
|
2593
|
+
let value = p.slice(colon + 1).trim();
|
|
2594
|
+
if (/^(["']).*\1$/s.test(value)) value = unescape(value.slice(1, -1));
|
|
2595
|
+
out[key] = value;
|
|
2596
|
+
}
|
|
2597
|
+
return out;
|
|
2598
|
+
}
|
|
2599
|
+
/** `text // comment` → `text`; a search for one fixed-width mark, so a long whitespace run stays linear */
|
|
2600
|
+
const stripComment = (s) => {
|
|
2601
|
+
const i = s.search(/\s\/\//);
|
|
2602
|
+
return i < 0 ? s : s.slice(0, i).trimEnd();
|
|
2603
|
+
};
|
|
2604
|
+
/** a `[props]` group ending a line; `[[x]]` (an inline chip) is not one */
|
|
2605
|
+
const TRAILING_PROPS = /(?<!\[)\[([^[\]]*)\]\s*$/;
|
|
2606
|
+
/**
|
|
2607
|
+
* `text [props]` → the text before the bracket and its props. `accept` may
|
|
2608
|
+
* refuse a bracket (it then stays part of the text), e.g. one whose keys
|
|
2609
|
+
* the statement does not know.
|
|
2610
|
+
*/
|
|
2611
|
+
function splitTrailingProps(text, accept) {
|
|
2612
|
+
const m = text.match(TRAILING_PROPS);
|
|
2613
|
+
const p = m ? parseProps(m[1]) : void 0;
|
|
2614
|
+
return p && (!accept || accept(p)) ? [text.slice(0, m.index).trimEnd(), p] : [text, {}];
|
|
2615
|
+
}
|
|
2616
|
+
/**
|
|
2617
|
+
* Set or remove one prop in the `[...]` at the end of a line, adding or
|
|
2618
|
+
* dropping the brackets as needed: `commit "x" [tag: v1]` → `commit "x" [tag: v1, red]`.
|
|
2619
|
+
*/
|
|
2620
|
+
function setTrailingProp(line, name, value) {
|
|
2621
|
+
const key = propKey(name);
|
|
2622
|
+
const [rest, props] = splitTrailingProps(line);
|
|
2623
|
+
if (value === null) delete props[key];
|
|
2624
|
+
else props[key] = value;
|
|
2625
|
+
const head = rest.replace(/\s+$/, "");
|
|
2626
|
+
const s = serializeProps(props);
|
|
2627
|
+
return s ? `${head} [${s}]` : head;
|
|
2628
|
+
}
|
|
2629
|
+
/** accepts a `[props]` group that only sets a colour, so `Launch [beta]` stays text */
|
|
2630
|
+
const onlyColor = (p) => {
|
|
2631
|
+
const keys = Object.keys(p);
|
|
2632
|
+
return keys.length === 1 && keys[0] === "color" && isColor(p.color);
|
|
2633
|
+
};
|
|
2634
|
+
/**
|
|
2635
|
+
* Set or remove one prop on a panel line that takes its `[props]` before the
|
|
2636
|
+
* colon or at the end: `EU [red]: 46` and `EU: 46 [red]` both work. The end
|
|
2637
|
+
* bracket wins when the line has one; otherwise the prop goes before the
|
|
2638
|
+
* colon, first in the list. `head` splits the line into
|
|
2639
|
+
* `(before)(?:\s*\[(props)\])?(colon and the rest)`; `accept` is the
|
|
2640
|
+
* one the line's reader passes to `splitTrailingProps`.
|
|
2641
|
+
*/
|
|
2642
|
+
function setLineProp(line, head, name, value, accept) {
|
|
2643
|
+
const key = propKey(name);
|
|
2644
|
+
const [rest, tail] = splitTrailingProps(line, accept);
|
|
2645
|
+
const atEnd = Object.keys(tail).length > 0 && head.test(rest);
|
|
2646
|
+
const out = (atEnd ? rest.trimEnd() : line).replace(head, (_, a, p, b) => {
|
|
2647
|
+
const { [key]: _old, ...pr } = parseProps(p);
|
|
2648
|
+
const s = serializeProps(atEnd || value === null ? pr : {
|
|
2649
|
+
[key]: value,
|
|
2650
|
+
...pr
|
|
2651
|
+
});
|
|
2652
|
+
return `${a}${s ? ` [${s}]` : ""}${b}`;
|
|
2653
|
+
});
|
|
2654
|
+
if (!atEnd) return out;
|
|
2655
|
+
if (value === null) delete tail[key];
|
|
2656
|
+
else tail[key] = value;
|
|
2657
|
+
const s = serializeProps(tail);
|
|
2658
|
+
return s ? `${out} [${s}]` : out;
|
|
2659
|
+
}
|
|
2660
|
+
function serializeProps(props) {
|
|
2661
|
+
return Object.entries(props).map(([k, v]) => {
|
|
2662
|
+
if (v === "true" && !TONE_SET.has(k) && !isShape(k) && !CURVES.has(k) && !isColor(k)) return k;
|
|
2663
|
+
if (k === "color" && isColor(v) && !isShape(v) && !CURVES.has(v.toLowerCase()) && !/[;[\]]/.test(v)) return v;
|
|
2664
|
+
return `${k}: ${/[,;[\]"'()\n\r]|^\s|\s$|^$/.test(v) ? quote(v) : v}`;
|
|
2665
|
+
}).join(", ");
|
|
2666
|
+
}
|
|
2667
|
+
function arrowInfo(a) {
|
|
2668
|
+
const m = a.match(ARROW_RE);
|
|
2669
|
+
const start = !!m[1];
|
|
2670
|
+
const end = !!m[3];
|
|
2671
|
+
const body = m[2];
|
|
2672
|
+
let line;
|
|
2673
|
+
if (body.startsWith("=")) line = "thick";
|
|
2674
|
+
else if (body === "..") line = "dotted";
|
|
2675
|
+
else if (body === "--" && (start || end)) line = "dashed";
|
|
2676
|
+
else line = "solid";
|
|
2677
|
+
return {
|
|
2678
|
+
start,
|
|
2679
|
+
end,
|
|
2680
|
+
line
|
|
2681
|
+
};
|
|
2682
|
+
}
|
|
2683
|
+
/** caps that keep a hostile source from hanging `render()` */
|
|
2684
|
+
const MAX_LINE = 1e5;
|
|
2685
|
+
const MAX_EXPANSION = 5e4;
|
|
2686
|
+
const VAR_RE = /\$\$|\$\{([A-Za-z_]\w*)\}|\$([A-Za-z_]\w*)/g;
|
|
2687
|
+
const VAR_DEF_RE = /^\s*\$([A-Za-z_]\w*)\s*[:=]\s*(.*)$/;
|
|
2688
|
+
const DEF_RE = /^\s*def\s+([\p{L}_][\p{L}\p{N}_-]*)\s*\(([^)]*)\)\s*\{\s*$/u;
|
|
2689
|
+
const CALL_RE = /^\s*([\p{L}_][\p{L}\p{N}_-]*)\s*\((.*)\)\s*$/u;
|
|
2690
|
+
/** the components a component sees: those of the file it was defined in */
|
|
2691
|
+
const SCOPE = /* @__PURE__ */ new WeakMap();
|
|
2692
|
+
/** `a, "b, c", [x: 1]` → three parts; commas inside quotes and brackets stay */
|
|
2693
|
+
function splitList(s) {
|
|
2694
|
+
const out = [];
|
|
2695
|
+
let cur = "";
|
|
2696
|
+
let q = null;
|
|
2697
|
+
let nest = 0;
|
|
2698
|
+
for (let i = 0; i < s.length; i++) {
|
|
2699
|
+
const c = s[i];
|
|
2700
|
+
if (q) {
|
|
2701
|
+
if (c === "\\") cur += c + (s[++i] ?? "");
|
|
2702
|
+
else cur += c, c === q && (q = null);
|
|
2703
|
+
continue;
|
|
2704
|
+
}
|
|
2705
|
+
if (c === "\"" || c === "'") q = c;
|
|
2706
|
+
else if ("[({".includes(c)) nest++;
|
|
2707
|
+
else if ("])}".includes(c)) nest--;
|
|
2708
|
+
else if (c === "," && !nest) {
|
|
2709
|
+
out.push(cur.trim());
|
|
2710
|
+
cur = "";
|
|
2711
|
+
continue;
|
|
2712
|
+
}
|
|
2713
|
+
cur += c;
|
|
2714
|
+
}
|
|
2715
|
+
if (cur.trim() || out.length) out.push(cur.trim());
|
|
2716
|
+
return out;
|
|
2717
|
+
}
|
|
2718
|
+
/** `s` as a double-quoted string the lexer reads back unchanged (the inverse of `unescape`) */
|
|
2719
|
+
const quote = (s) => `"${s.replace(/[\\"]/g, "\\$&").replace(/\r\n?|\n/g, "\\n").replace(/\t/g, "\\t")}"`;
|
|
2720
|
+
const unquote = (s) => /^(["']).*\1$/s.test(s.trim()) ? unescape(s.trim().slice(1, -1)) : s.trim();
|
|
2721
|
+
function parse(source, opts = {}) {
|
|
2722
|
+
const d = {
|
|
2723
|
+
settings: {},
|
|
2724
|
+
nodes: /* @__PURE__ */ new Map(),
|
|
2725
|
+
edges: [],
|
|
2726
|
+
groups: /* @__PURE__ */ new Map(),
|
|
2727
|
+
classes: table({}),
|
|
2728
|
+
layout: {
|
|
2729
|
+
nodes: {},
|
|
2730
|
+
edges: {}
|
|
2731
|
+
},
|
|
2732
|
+
panels: [],
|
|
2733
|
+
diagnostics: []
|
|
2734
|
+
};
|
|
2735
|
+
const lines = source.split(/\r?\n/);
|
|
2736
|
+
const merged = opts.seen ??= /* @__PURE__ */ new Set();
|
|
2737
|
+
if (opts.file) merged.add(opts.file);
|
|
2738
|
+
/** names marked with `export` (filtered to what exists at the end) */
|
|
2739
|
+
/** exported name to the line that exports it */
|
|
2740
|
+
const exported = /* @__PURE__ */ new Map();
|
|
2741
|
+
/** every `.class` written on a node, edge or group, with its line: checked once all classes are known */
|
|
2742
|
+
const classUses = [];
|
|
2743
|
+
const useClass = (c, li) => classUses.push([c, li]);
|
|
2744
|
+
let hasExport = false;
|
|
2745
|
+
const stack = [];
|
|
2746
|
+
const edgeCount = /* @__PURE__ */ new Map();
|
|
2747
|
+
const warn = (line, message, severity = "warning") => d.diagnostics.push({
|
|
2748
|
+
line: line + 1,
|
|
2749
|
+
message,
|
|
2750
|
+
severity
|
|
2751
|
+
});
|
|
2752
|
+
/** the line of the `theme:` setting, checked once every theme is known */
|
|
2753
|
+
let themeLine;
|
|
2754
|
+
const themeProp = (props, key, value, li) => {
|
|
2755
|
+
if (!(key in THEME_KEYS)) return warn(li, `Unknown theme key "${key}"${didYouMean(key, THEME_KEY_NAMES) || ` (keys: ${THEME_KEY_NAMES.join(", ")})`}`);
|
|
2756
|
+
if (THEME_KEYS[key].kind === "color" && !["none", "transparent"].includes(value) && !value.includes("$") && !isColor(value)) warn(li, `Unknown color "${value}" for ${key}${didYouMean(value, TONES)}`);
|
|
2757
|
+
props[key] = value;
|
|
2758
|
+
};
|
|
2759
|
+
/** theme name → the line of its definition in this file */
|
|
2760
|
+
const themeLines = /* @__PURE__ */ new Map();
|
|
2761
|
+
const defineTheme = (name, props, li) => {
|
|
2762
|
+
if (themeLines.has(name)) warn(li, `Theme "${name}" is defined twice`);
|
|
2763
|
+
(d.themes ??= table({}))[name] = props;
|
|
2764
|
+
themeLines.set(name, li);
|
|
2765
|
+
};
|
|
2766
|
+
const currentGroup = () => {
|
|
2767
|
+
for (let i = stack.length - 1; i >= 0; i--) if (stack[i].kind === "group") return stack[i].id;
|
|
2768
|
+
};
|
|
2769
|
+
const touchNode = (id) => {
|
|
2770
|
+
let n = d.nodes.get(id);
|
|
2771
|
+
if (!n) {
|
|
2772
|
+
n = {
|
|
2773
|
+
id,
|
|
2774
|
+
label: id,
|
|
2775
|
+
props: {},
|
|
2776
|
+
classes: [],
|
|
2777
|
+
implicit: true,
|
|
2778
|
+
group: currentGroup()
|
|
2779
|
+
};
|
|
2780
|
+
d.nodes.set(id, n);
|
|
2781
|
+
}
|
|
2782
|
+
return n;
|
|
2783
|
+
};
|
|
2784
|
+
/** a second, different label in this file is almost always a misplaced edge label */
|
|
2785
|
+
const labelled = /* @__PURE__ */ new Map();
|
|
2786
|
+
const relabel = (n, tok, lineNo, onEdge) => {
|
|
2787
|
+
const label = tok.value;
|
|
2788
|
+
n.labelRef = {
|
|
2789
|
+
line: lineNo,
|
|
2790
|
+
at: tok.start,
|
|
2791
|
+
range: [tok.start, tok.end],
|
|
2792
|
+
generated: gen || void 0
|
|
2793
|
+
};
|
|
2794
|
+
const before = labelled.get(n.id);
|
|
2795
|
+
if (before !== void 0 && n.label !== label) warn(lineNo, `"${n.id}" already has the label "${n.label}" (line ${before + 1}); this renames it to "${label}"` + (onEdge ? `. For an edge label write: ... -> ${n.id}: ${label}` : ""));
|
|
2796
|
+
n.label = label;
|
|
2797
|
+
labelled.set(n.id, lineNo);
|
|
2798
|
+
};
|
|
2799
|
+
const layoutLines = /* @__PURE__ */ new Map();
|
|
2800
|
+
/**
|
|
2801
|
+
* Parse an imported file and merge it in; this file's own statements win.
|
|
2802
|
+
* `names` ([name, alias] pairs) narrows a selective import.
|
|
2803
|
+
*/
|
|
2804
|
+
const importFile = (rel, li, names) => {
|
|
2805
|
+
if (!opts.resolve) return warn(li, `Cannot import "${rel}": no resolver (pass { resolve } to render, or use the CLI)`, "error");
|
|
2806
|
+
const path = joinPath(opts.file, rel);
|
|
2807
|
+
if (!isYuhwaPath(path)) return warn(li, `Cannot import "${rel}": imports must be .yu files`, "error");
|
|
2808
|
+
const chain = [...opts.chain ?? (opts.file ? [opts.file] : [])];
|
|
2809
|
+
if (chain.includes(path)) return warn(li, `Import cycle: ${[...chain, path].join(" -> ")}`, "error");
|
|
2810
|
+
const key = names ? `${path}{${names.map((n) => n.join(" as ")).join(",")}}` : path;
|
|
2811
|
+
if (merged.has(key)) return;
|
|
2812
|
+
let src;
|
|
2813
|
+
try {
|
|
2814
|
+
src = opts.resolve(path);
|
|
2815
|
+
} catch {
|
|
2816
|
+
src = void 0;
|
|
2817
|
+
}
|
|
2818
|
+
if (src === void 0) return warn(li, `Cannot import "${rel}": file not found (${path})`, "error");
|
|
2819
|
+
const seen = new Set(merged);
|
|
2820
|
+
const sub = parse(src, {
|
|
2821
|
+
resolve: opts.resolve,
|
|
2822
|
+
file: path,
|
|
2823
|
+
chain: [...chain, path],
|
|
2824
|
+
seen
|
|
2825
|
+
});
|
|
2826
|
+
for (const x of sub.diagnostics) warn(li, `${rel}:${x.line}: ${x.message}`, x.severity);
|
|
2827
|
+
const narrowed = narrow(sub, names, (msg) => warn(li, `${rel}: ${msg}`, "error"));
|
|
2828
|
+
if (narrowed === sub) for (const k of seen) merged.add(k);
|
|
2829
|
+
merged.add(key);
|
|
2830
|
+
mergeDiagram(d, narrowed, edgeCount);
|
|
2831
|
+
(d.imports ??= []).push(path, ...sub.imports ?? []);
|
|
2832
|
+
};
|
|
2833
|
+
const vars = d.vars = table({});
|
|
2834
|
+
const defs = d.defs = table({});
|
|
2835
|
+
const used = d.used = /* @__PURE__ */ new Set();
|
|
2836
|
+
SCOPE.set(defs, defs);
|
|
2837
|
+
/** set while a line comes from a variable or a component: refs then carry `generated` */
|
|
2838
|
+
let gen = false;
|
|
2839
|
+
let depth = 0;
|
|
2840
|
+
/** lines stamped out by component calls so far: recursion that fans out stops at MAX_EXPANSION */
|
|
2841
|
+
let expanded = 0;
|
|
2842
|
+
const tooDeep = /* @__PURE__ */ new Set();
|
|
2843
|
+
let lastDecl = null;
|
|
2844
|
+
const indentOf = (line) => line.match(/^\s*/)[0].length;
|
|
2845
|
+
/** `$name` → its value; params of a component call shadow the file's variables */
|
|
2846
|
+
const substitute = (line, li, local, quiet = false) => {
|
|
2847
|
+
const v = line.match(VAR_DEF_RE);
|
|
2848
|
+
if (v) return line.slice(0, line.length - v[2].length) + substitute(v[2], li, local, quiet);
|
|
2849
|
+
let size = line.length;
|
|
2850
|
+
const out = line.replace(VAR_RE, (all, braced, bare) => {
|
|
2851
|
+
const name = braced ?? bare;
|
|
2852
|
+
if (!name) return "$";
|
|
2853
|
+
const value = local?.[name] ?? vars[name];
|
|
2854
|
+
if (local?.[name] === void 0 && value !== void 0) used.add("$" + name);
|
|
2855
|
+
if (value !== void 0) return (size += value.length) > MAX_LINE ? all : value;
|
|
2856
|
+
if (!quiet) warn(li, `Unknown variable "$${name}"${didYouMean(name, [...Object.keys(local ?? {}), ...Object.keys(vars)])}`);
|
|
2857
|
+
return all;
|
|
2858
|
+
});
|
|
2859
|
+
if (size > MAX_LINE) warn(li, `Variables make this line longer than ${MAX_LINE} characters`, "error");
|
|
2860
|
+
return out;
|
|
2861
|
+
};
|
|
2862
|
+
const expand = (def, argText, li) => {
|
|
2863
|
+
if (depth >= 16) {
|
|
2864
|
+
if (!tooDeep.has(def.name)) warn(li, `${def.name}(): calls nest too deep`, "error");
|
|
2865
|
+
tooDeep.add(def.name);
|
|
2866
|
+
return;
|
|
2867
|
+
}
|
|
2868
|
+
if ((expanded += def.body.length) > MAX_EXPANSION) {
|
|
2869
|
+
if (expanded - def.body.length <= MAX_EXPANSION) warn(li, `${def.name}(): components expand to more than ${MAX_EXPANSION} lines`, "error");
|
|
2870
|
+
return;
|
|
2871
|
+
}
|
|
2872
|
+
const args = splitList(argText);
|
|
2873
|
+
const local = table({});
|
|
2874
|
+
let pos = 0;
|
|
2875
|
+
for (const a of args) {
|
|
2876
|
+
const named = a.match(/^([A-Za-z_]\w*)\s*[:=]\s*([\s\S]*)$/);
|
|
2877
|
+
if (named && def.params.some((p) => p.name === named[1])) local[named[1]] = unquote(named[2]);
|
|
2878
|
+
else if (pos < def.params.length) local[def.params[pos++].name] = unquote(a);
|
|
2879
|
+
else return void warn(li, `${def.name}() takes ${def.params.length} argument(s), got ${args.length}`, "error");
|
|
2880
|
+
}
|
|
2881
|
+
for (const p of def.params) {
|
|
2882
|
+
if (p.name in local) continue;
|
|
2883
|
+
if (p.default === void 0) return void warn(li, `${def.name}(): missing argument "${p.name}" (${def.params.map((x) => x.name).join(", ")})`, "error");
|
|
2884
|
+
local[p.name] = p.default;
|
|
2885
|
+
}
|
|
2886
|
+
const open = stack.length;
|
|
2887
|
+
const was = gen;
|
|
2888
|
+
gen = true;
|
|
2889
|
+
depth++;
|
|
2890
|
+
const lines = def.body.map((l) => substitute(l, li, local));
|
|
2891
|
+
const scope = SCOPE.get(def) ?? defs;
|
|
2892
|
+
for (const l of lines) step(l, li, true, scope);
|
|
2893
|
+
depth--;
|
|
2894
|
+
gen = was;
|
|
2895
|
+
if (stack.length > open) {
|
|
2896
|
+
warn(li, `${def.name}(): a block inside the component is not closed`, "error");
|
|
2897
|
+
stack.length = open;
|
|
2898
|
+
}
|
|
2899
|
+
};
|
|
2900
|
+
/** one source line: variables, components, then the statement itself */
|
|
2901
|
+
const step = (raw0, li, done = false, scope = defs) => {
|
|
2902
|
+
if (/^\s*#(\s|$)/.test(raw0)) return void warn(li, "Comments start with \"//\", not \"#\"", "error");
|
|
2903
|
+
const top = stack[stack.length - 1];
|
|
2904
|
+
if (top?.kind === "def") {
|
|
2905
|
+
for (const b of blockBraces(lex(raw0).tokens)) top.depth += b === "{" ? 1 : -1;
|
|
2906
|
+
if (top.depth > 0) return void top.lines.push(raw0);
|
|
2907
|
+
stack.pop();
|
|
2908
|
+
if (raw0.trim() !== "}") warn(li, `Expected "}" alone on the line that closes ${top.id}()`, "error");
|
|
2909
|
+
const params = top.params;
|
|
2910
|
+
const own = table(Object.fromEntries(params.map((p) => [p.name, `\${${p.name}}`])));
|
|
2911
|
+
const def = {
|
|
2912
|
+
name: top.id,
|
|
2913
|
+
params,
|
|
2914
|
+
body: dedent(top.lines).map((l) => substitute(l, li, own, true))
|
|
2915
|
+
};
|
|
2916
|
+
SCOPE.set(def, scope);
|
|
2917
|
+
if (defs[def.name]) warn(top.start, `Component "${def.name}" is defined twice`);
|
|
2918
|
+
defs[def.name] = def;
|
|
2919
|
+
(d.defLines ??= {})[def.name] = top.start;
|
|
2920
|
+
return;
|
|
2921
|
+
}
|
|
2922
|
+
const verbatim = top?.kind === "body" || top?.kind === "panel";
|
|
2923
|
+
if (!verbatim && top?.kind !== "layout") {
|
|
2924
|
+
const v = raw0.match(VAR_DEF_RE);
|
|
2925
|
+
if (v) {
|
|
2926
|
+
vars[v[1]] = unquote(stripComment(done ? v[2] : substitute(v[2], li)).trimEnd());
|
|
2927
|
+
(d.varLines ??= {})[v[1]] = li;
|
|
2928
|
+
return;
|
|
2929
|
+
}
|
|
2930
|
+
const h = raw0.match(DEF_RE);
|
|
2931
|
+
if (h) {
|
|
2932
|
+
if (stack.length) warn(li, "def must be at the top level", "error");
|
|
2933
|
+
const params = splitList(h[2]).map((p) => {
|
|
2934
|
+
const m = p.match(/^([A-Za-z_]\w*)\s*(?:[:=]\s*([\s\S]*))?$/);
|
|
2935
|
+
if (!m) warn(li, `Bad parameter "${p}": def name(a, b = "default") {`, "error");
|
|
2936
|
+
return {
|
|
2937
|
+
name: m?.[1] ?? p,
|
|
2938
|
+
default: m?.[2] !== void 0 ? unquote(m[2]) : void 0
|
|
2939
|
+
};
|
|
2940
|
+
});
|
|
2941
|
+
stack.push({
|
|
2942
|
+
kind: "def",
|
|
2943
|
+
id: h[1],
|
|
2944
|
+
start: li,
|
|
2945
|
+
lines: [],
|
|
2946
|
+
depth: 1,
|
|
2947
|
+
params
|
|
2948
|
+
});
|
|
2949
|
+
return;
|
|
2950
|
+
}
|
|
2951
|
+
const c = stripComment(raw0).match(CALL_RE);
|
|
2952
|
+
if (c) {
|
|
2953
|
+
const def = scope[c[1]] ?? defs[c[1]];
|
|
2954
|
+
if (def) {
|
|
2955
|
+
used.add(c[1]);
|
|
2956
|
+
expand(def, done ? c[2] : substitute(c[2], li), li);
|
|
2957
|
+
return;
|
|
2958
|
+
}
|
|
2959
|
+
if (!lex(raw0).tokens.some((x) => x.kind === "arrow")) return void warn(li, `Unknown component "${c[1]}"${didYouMean(c[1], Object.keys(defs))}`, "error");
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
const raw = done || top?.kind === "layout" || !verbatim && /^\s*(import|export)\b/.test(raw0) ? raw0 : substitute(raw0, li, void 0, verbatim);
|
|
2963
|
+
const was = gen;
|
|
2964
|
+
gen ||= raw !== raw0;
|
|
2965
|
+
statement(raw, li);
|
|
2966
|
+
gen = was;
|
|
2967
|
+
};
|
|
2968
|
+
/** node body: verbatim lines until a line that is only "}" */
|
|
2969
|
+
const bodyLine = (top, raw, li) => {
|
|
2970
|
+
if (raw.trim() === "}") {
|
|
2971
|
+
stack.pop();
|
|
2972
|
+
const n = d.nodes.get(top.id);
|
|
2973
|
+
n.body = dedent(top.lines);
|
|
2974
|
+
n.bodyLines = [top.start, li];
|
|
2975
|
+
} else top.lines.push(raw);
|
|
2976
|
+
};
|
|
2977
|
+
/** panel body: verbatim too, handed to the panel plugin on close */
|
|
2978
|
+
const panelLine = (top, raw, li) => {
|
|
2979
|
+
if (raw.trim() === "}") {
|
|
2980
|
+
stack.pop();
|
|
2981
|
+
closePanel(top, li);
|
|
2982
|
+
} else top.lines.push(raw.slice(0, lex(raw).codeEnd).trimEnd());
|
|
2983
|
+
};
|
|
2984
|
+
/** hand a panel's body to its plugin; `end` is the line of its "}" (or the last line when it is missing) */
|
|
2985
|
+
const closePanel = (top, end) => {
|
|
2986
|
+
const p = top.panel;
|
|
2987
|
+
const lead = top.lines.findIndex((l) => l.trim());
|
|
2988
|
+
p.body = dedent(top.lines);
|
|
2989
|
+
p.lines = [top.start, end];
|
|
2990
|
+
try {
|
|
2991
|
+
p.data = PANELS.get(p.kind).parse(p.body, p.props, (i, msg) => warn(i < 0 ? top.start : top.start + 1 + Math.max(0, lead) + i, msg));
|
|
2992
|
+
} catch (e) {
|
|
2993
|
+
const who = ownerOf("panel", p.kind);
|
|
2994
|
+
warn(top.start, `${who ? `plugin "${who}": ` : ""}${p.kind} panel failed to parse: ${errorText(e)}`, "error");
|
|
2995
|
+
p.failed = true;
|
|
2996
|
+
}
|
|
2997
|
+
};
|
|
2998
|
+
/** theme body: `key: value` lines */
|
|
2999
|
+
const themeBodyLine = (top, raw, li) => {
|
|
3000
|
+
const code = raw.slice(0, lex(raw).codeEnd).trim();
|
|
3001
|
+
if (!code) return;
|
|
3002
|
+
if (code === "}") {
|
|
3003
|
+
stack.pop();
|
|
3004
|
+
defineTheme(top.id, top.props, top.start);
|
|
3005
|
+
return;
|
|
3006
|
+
}
|
|
3007
|
+
const m = code.match(/^([A-Za-z][\w-]*)\s*:\s*(.*)$/);
|
|
3008
|
+
if (!m) return void warn(li, `Expected "key: value" in theme ${top.id} (keys: ${THEME_KEY_NAMES.join(", ")})`, "error");
|
|
3009
|
+
themeProp(top.props, m[1].toLowerCase(), unquote(m[2].trim()), li);
|
|
3010
|
+
};
|
|
3011
|
+
const layoutLine = (raw, li) => {
|
|
3012
|
+
const code = raw.slice(0, lex(raw).codeEnd).trim();
|
|
3013
|
+
if (!code) return;
|
|
3014
|
+
if (code.startsWith("}")) {
|
|
3015
|
+
stack.pop();
|
|
3016
|
+
d.layoutBlock[1] = li;
|
|
3017
|
+
return;
|
|
3018
|
+
}
|
|
3019
|
+
layoutLines.set(code.split(/\s+/)[0], li);
|
|
3020
|
+
parseLayoutLine(code, li, d, warn);
|
|
3021
|
+
};
|
|
3022
|
+
const closeBlock = (li) => {
|
|
3023
|
+
if (!stack.length) warn(li, "Unmatched \"}\"", "error");
|
|
3024
|
+
else stack.pop();
|
|
3025
|
+
lastDecl = null;
|
|
3026
|
+
};
|
|
3027
|
+
/** import "./shared.yu" | import { api, db as store, .svc } from "./shared.yu" */
|
|
3028
|
+
const importStmt = (t, li, selective) => {
|
|
3029
|
+
if (stack.length) warn(li, "import must be at the top level", "error");
|
|
3030
|
+
else if (!t[1]) warn(li, "import needs a path: import \"./shared.yu\"", "error");
|
|
3031
|
+
else if (!selective) {
|
|
3032
|
+
if (t.length > 2) warn(li, `Unexpected "${t[2].value}" after import path`, "error");
|
|
3033
|
+
importFile(t[1].value, li);
|
|
3034
|
+
} else {
|
|
3035
|
+
const names = readImportList(t, li, warn);
|
|
3036
|
+
if (names) importFile(names.path, li, names.names);
|
|
3037
|
+
}
|
|
3038
|
+
};
|
|
3039
|
+
/** export api, .svc | export api "API" [blue] | export group g { ... } */
|
|
3040
|
+
const exportStmt = (t, raw, li) => {
|
|
3041
|
+
if (stack.length) warn(li, "export must be at the top level", "error");
|
|
3042
|
+
const setting = t[1].kind === "ident" && t[2]?.value === ":" && KNOWN_SETTINGS.has(t[1].value.toLowerCase());
|
|
3043
|
+
if (setting || t[1].value === "theme" && t[2]?.kind === "ident") {
|
|
3044
|
+
const key = t[1].value.toLowerCase();
|
|
3045
|
+
if (setting && !d.sharedSettings?.includes(key)) (d.sharedSettings ??= []).push(key);
|
|
3046
|
+
return step(raw.slice(0, t[0].start) + " ".repeat(6) + raw.slice(t[0].end), li);
|
|
3047
|
+
}
|
|
3048
|
+
hasExport = true;
|
|
3049
|
+
const list = t.slice(1).every((x) => x.kind === "ident" || x.kind === "class" || x.value === ",");
|
|
3050
|
+
const exp = (name) => exported.has(name) || exported.set(name, li);
|
|
3051
|
+
if (t[1].value === "def" && t[2]) exp(t[2].value);
|
|
3052
|
+
else if (t[1].value.startsWith("$") && (t[2]?.value === ":" || t[2]?.value === "=")) exp(t[1].value);
|
|
3053
|
+
else for (const x of t.slice(1)) if (x.kind === "ident" || x.kind === "class") exp(x.kind === "class" ? `.${x.value}` : x.value);
|
|
3054
|
+
if (list && !t.slice(1).some((x) => x.kind === "ident" && PANELS.has(x.value))) return;
|
|
3055
|
+
return step(raw.slice(0, t[0].start) + " ".repeat(6) + raw.slice(t[0].end), li);
|
|
3056
|
+
};
|
|
3057
|
+
/** theme brand [base: midnight] { | theme brand [base: midnight, accent: red] */
|
|
3058
|
+
const themeStmt = (t, li) => {
|
|
3059
|
+
if (stack.length) warn(li, "theme must be at the top level", "error");
|
|
3060
|
+
const props = {};
|
|
3061
|
+
let k = 2;
|
|
3062
|
+
if (t[k]?.kind === "props") {
|
|
3063
|
+
for (const [key, v] of Object.entries(parseProps(t[k].value))) themeProp(props, key, v, li);
|
|
3064
|
+
k++;
|
|
3065
|
+
}
|
|
3066
|
+
if (t[k]?.value === "{") {
|
|
3067
|
+
k++;
|
|
3068
|
+
if (t[k]?.value === "}") k++;
|
|
3069
|
+
else if (k === t.length) return void stack.push({
|
|
3070
|
+
kind: "theme",
|
|
3071
|
+
id: t[1].value,
|
|
3072
|
+
start: li,
|
|
3073
|
+
props
|
|
3074
|
+
});
|
|
3075
|
+
}
|
|
3076
|
+
if (k < t.length) warn(li, `Unexpected "${t[k].value}" in theme ${t[1].value}`, "error");
|
|
3077
|
+
defineTheme(t[1].value, props, li);
|
|
3078
|
+
};
|
|
3079
|
+
/** `gantt g {`: looks like a panel of a kind nobody registered */
|
|
3080
|
+
const unknownPanelHint = (t, li) => {
|
|
3081
|
+
warn(li, `"${t[0].value} ${t[1].value} {" reads as two nodes; "${t[0].value}" is not a panel kind (${[...PANELS.keys()].join(", ")})` + (PANEL_ALIASES[t[0].value] && PANELS.has(PANEL_ALIASES[t[0].value].split(" ")[0]) ? `: write "${PANEL_ALIASES[t[0].value].replace("ID", t[1].value)} {"` : ""));
|
|
3082
|
+
};
|
|
3083
|
+
/** sequence id "Title" [props] { — a panel drawn by a plugin */
|
|
3084
|
+
const panelStmt = (t, li) => {
|
|
3085
|
+
const kind = t[0].value;
|
|
3086
|
+
let k = 1;
|
|
3087
|
+
const id = t[k]?.kind === "ident" ? t[k++].value : `${kind}${d.panels.length + 1}`;
|
|
3088
|
+
const labelRef = gen ? void 0 : t[k]?.kind === "string" ? {
|
|
3089
|
+
line: li,
|
|
3090
|
+
at: t[k].start,
|
|
3091
|
+
range: [t[k].start, t[k].end]
|
|
3092
|
+
} : {
|
|
3093
|
+
line: li,
|
|
3094
|
+
at: t[k - 1].end
|
|
3095
|
+
};
|
|
3096
|
+
const label = t[k]?.kind === "string" ? t[k++].value : void 0;
|
|
3097
|
+
const ref = {
|
|
3098
|
+
line: li,
|
|
3099
|
+
insertAt: t[k - 1].end
|
|
3100
|
+
};
|
|
3101
|
+
let props = {};
|
|
3102
|
+
if (t[k]?.kind === "props") {
|
|
3103
|
+
props = parseProps(t[k].value);
|
|
3104
|
+
ref.props = [t[k].start, t[k].end];
|
|
3105
|
+
k++;
|
|
3106
|
+
}
|
|
3107
|
+
if (props.vertical !== void 0 && props.direction === void 0 && props.vertical !== "false") props.direction = "down";
|
|
3108
|
+
delete props.vertical;
|
|
3109
|
+
if (k !== t.length - 1) warn(li, `Unexpected "${t[k].value}" in ${kind} header`, "error");
|
|
3110
|
+
if (stack.length) warn(li, `${kind} must be at the top level`, "error");
|
|
3111
|
+
const panel = {
|
|
3112
|
+
kind,
|
|
3113
|
+
id,
|
|
3114
|
+
label,
|
|
3115
|
+
props,
|
|
3116
|
+
body: [],
|
|
3117
|
+
data: void 0,
|
|
3118
|
+
ref: gen ? void 0 : ref,
|
|
3119
|
+
labelRef
|
|
3120
|
+
};
|
|
3121
|
+
d.panels.push(panel);
|
|
3122
|
+
stack.push({
|
|
3123
|
+
kind: "panel",
|
|
3124
|
+
id,
|
|
3125
|
+
start: li,
|
|
3126
|
+
lines: [],
|
|
3127
|
+
panel
|
|
3128
|
+
});
|
|
3129
|
+
};
|
|
3130
|
+
/** layout { ... } */
|
|
3131
|
+
const layoutStmt = (t, li) => {
|
|
3132
|
+
if (stack.length) warn(li, "layout block must be at top level", "error");
|
|
3133
|
+
d.layoutBlock = [li, li];
|
|
3134
|
+
if (t[2]?.value === "}") return;
|
|
3135
|
+
stack.push({ kind: "layout" });
|
|
3136
|
+
};
|
|
3137
|
+
/** group id "Label" [.class ...] [props] { ... } | group "Label" { ... } */
|
|
3138
|
+
const groupStmt = (t, raw, li) => {
|
|
3139
|
+
let k = 2;
|
|
3140
|
+
let id = t[1].value;
|
|
3141
|
+
if (t[1].kind === "string") {
|
|
3142
|
+
id = t[1].value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "group";
|
|
3143
|
+
k = 1;
|
|
3144
|
+
}
|
|
3145
|
+
const g = d.groups.get(id) ?? {
|
|
3146
|
+
id,
|
|
3147
|
+
label: id,
|
|
3148
|
+
props: {},
|
|
3149
|
+
parent: currentGroup()
|
|
3150
|
+
};
|
|
3151
|
+
if (t[k]?.kind === "string") {
|
|
3152
|
+
const s = t[k++];
|
|
3153
|
+
g.label = s.value;
|
|
3154
|
+
g.labelRef = {
|
|
3155
|
+
line: li,
|
|
3156
|
+
at: s.start,
|
|
3157
|
+
range: [s.start, s.end],
|
|
3158
|
+
id: k === 2 ? id : void 0
|
|
3159
|
+
};
|
|
3160
|
+
} else g.labelRef ??= {
|
|
3161
|
+
line: li,
|
|
3162
|
+
at: t[1].end
|
|
3163
|
+
};
|
|
3164
|
+
if (gen) g.labelRef.generated = true;
|
|
3165
|
+
while (t[k]?.kind === "class") {
|
|
3166
|
+
useClass(t[k].value, li);
|
|
3167
|
+
(g.classes ??= []).push(t[k++].value);
|
|
3168
|
+
}
|
|
3169
|
+
const ref = {
|
|
3170
|
+
line: li,
|
|
3171
|
+
insertAt: t[k - 1].end,
|
|
3172
|
+
generated: gen || void 0
|
|
3173
|
+
};
|
|
3174
|
+
if (t[k]?.kind === "props") {
|
|
3175
|
+
Object.assign(g.props, parseProps(t[k].value));
|
|
3176
|
+
ref.props = [t[k].start, t[k].end];
|
|
3177
|
+
k++;
|
|
3178
|
+
}
|
|
3179
|
+
g.ref = ref;
|
|
3180
|
+
lastDecl = {
|
|
3181
|
+
indent: indentOf(raw),
|
|
3182
|
+
targets: [g]
|
|
3183
|
+
};
|
|
3184
|
+
d.groups.set(id, g);
|
|
3185
|
+
if (t[k]?.value !== "{") {
|
|
3186
|
+
warn(li, `Expected "{" after group ${id}`, "error");
|
|
3187
|
+
return;
|
|
3188
|
+
}
|
|
3189
|
+
k++;
|
|
3190
|
+
const close = t.findIndex((x, idx) => idx >= k && x.value === "}");
|
|
3191
|
+
if (close >= 0) {
|
|
3192
|
+
for (let j = k; j < close; j++) if (t[j].kind === "ident") {
|
|
3193
|
+
const n = touchNode(t[j].value);
|
|
3194
|
+
n.group = id;
|
|
3195
|
+
}
|
|
3196
|
+
return;
|
|
3197
|
+
}
|
|
3198
|
+
stack.push({
|
|
3199
|
+
kind: "group",
|
|
3200
|
+
id
|
|
3201
|
+
});
|
|
3202
|
+
};
|
|
3203
|
+
/** .class [props] | .primary .svc [bold]: a class built on other classes | .class alone, props on lines below */
|
|
3204
|
+
const classStmt = (t, raw, li) => {
|
|
3205
|
+
const last = t[t.length - 1];
|
|
3206
|
+
const base = {};
|
|
3207
|
+
for (const x of t.slice(1)) {
|
|
3208
|
+
if (x.kind !== "class") continue;
|
|
3209
|
+
if (!d.classes[x.value]) warn(li, `Unknown class ".${x.value}"${didYouMean(x.value, Object.keys(d.classes))}`);
|
|
3210
|
+
else Object.assign(base, d.classes[x.value]);
|
|
3211
|
+
}
|
|
3212
|
+
d.classes[t[0].value] = {
|
|
3213
|
+
...d.classes[t[0].value],
|
|
3214
|
+
...base,
|
|
3215
|
+
...last.kind === "props" ? parseProps(last.value) : {}
|
|
3216
|
+
};
|
|
3217
|
+
(d.classLines ??= {})[t[0].value] = li;
|
|
3218
|
+
lastDecl = {
|
|
3219
|
+
indent: indentOf(raw),
|
|
3220
|
+
targets: [{ props: d.classes[t[0].value] }]
|
|
3221
|
+
};
|
|
3222
|
+
};
|
|
3223
|
+
/** key: value — a setting, or a prop of the declaration above or the enclosing group */
|
|
3224
|
+
const settingStmt = (t, raw, li, codeEnd, top) => {
|
|
3225
|
+
const key = propKey(t[0].value);
|
|
3226
|
+
let value = raw.slice(t[1].end, codeEnd).trim();
|
|
3227
|
+
if (/^(["']).*\1$/.test(value)) value = unescape(value.slice(1, -1));
|
|
3228
|
+
const deeper = lastDecl && indentOf(raw) > lastDecl.indent ? lastDecl.targets : null;
|
|
3229
|
+
const grp = top?.kind === "group" ? d.groups.get(top.id) : void 0;
|
|
3230
|
+
const targets = deeper ?? (grp ? [grp] : null);
|
|
3231
|
+
if (targets) {
|
|
3232
|
+
for (const x of targets) {
|
|
3233
|
+
x.props[key] = value || "true";
|
|
3234
|
+
if (x.ref && !gen) (x.ref.propLines ??= {})[key] = li;
|
|
3235
|
+
}
|
|
3236
|
+
return;
|
|
3237
|
+
}
|
|
3238
|
+
if (stack.length) warn(li, `Setting "${key}" should be at the top level`);
|
|
3239
|
+
if (!KNOWN_SETTINGS.has(key)) {
|
|
3240
|
+
const id = t[0].value;
|
|
3241
|
+
const node = value.startsWith("{") ? `${id} [${value.slice(1, -1).trim()}]` : `${id} "${value}"`;
|
|
3242
|
+
warn(li, `Unknown setting "${key}"${didYouMean(key, [...KNOWN_SETTINGS]) || `. For a node write ${node}`}`);
|
|
3243
|
+
return;
|
|
3244
|
+
}
|
|
3245
|
+
if (key === "direction") value = DIRECTION_ALIASES[value.toLowerCase()] ?? value;
|
|
3246
|
+
if (key === "theme") themeLine = li;
|
|
3247
|
+
const allowed = key === "theme" ? void 0 : SETTING_VALUES[key];
|
|
3248
|
+
if (allowed && value && !value.includes("$") && !allowed.includes(value)) warn(li, `Unknown ${key} "${value}"${didYouMean(value, allowed) || ` (use one of: ${allowed.join(", ")})`}`);
|
|
3249
|
+
d.settings[key] = value;
|
|
3250
|
+
};
|
|
3251
|
+
const edgeStmt = (t, raw, li, codeEnd) => {
|
|
3252
|
+
const ne = d.edges.length;
|
|
3253
|
+
parseEdgeStatement(t, li, raw.slice(0, codeEnd), d, touchNode, edgeCount, warn, relabel, useClass);
|
|
3254
|
+
lastDecl = {
|
|
3255
|
+
indent: indentOf(raw),
|
|
3256
|
+
targets: d.edges.slice(ne)
|
|
3257
|
+
};
|
|
3258
|
+
if (!gen) return;
|
|
3259
|
+
for (const e of d.edges.slice(ne)) {
|
|
3260
|
+
if (e.ref) e.ref.generated = true;
|
|
3261
|
+
if (e.labelRef) e.labelRef.generated = true;
|
|
3262
|
+
}
|
|
3263
|
+
for (const x of t) if (x.kind === "ident") {
|
|
3264
|
+
const n = d.nodes.get(x.value);
|
|
3265
|
+
if (n?.ref?.line === li) n.ref.generated = true;
|
|
3266
|
+
if (n?.labelRef?.line === li) n.labelRef.generated = true;
|
|
3267
|
+
}
|
|
3268
|
+
};
|
|
3269
|
+
/** node declarations: id "Label" .cls [props] (several per line allowed) */
|
|
3270
|
+
const nodeStmt = (t, raw, li) => {
|
|
3271
|
+
let k = 0;
|
|
3272
|
+
let sep = true;
|
|
3273
|
+
while (k < t.length) {
|
|
3274
|
+
const tk = t[k];
|
|
3275
|
+
if (tk.kind === "punct" && tk.value === ",") {
|
|
3276
|
+
k++;
|
|
3277
|
+
sep = true;
|
|
3278
|
+
continue;
|
|
3279
|
+
}
|
|
3280
|
+
if (!sep && tk.kind === "ident") {
|
|
3281
|
+
const words = [t[k - 1].value];
|
|
3282
|
+
while (t[k]?.kind === "ident") words.push(t[k++].value);
|
|
3283
|
+
const lbl = words.join(" ");
|
|
3284
|
+
const kw = words[0].toLowerCase();
|
|
3285
|
+
warn(li, kw === "subgraph" || kw === "end" ? `"${words[0]}" is Mermaid syntax: write group id "Label" { ... }` : `"${lbl}" looks like a label: write ${words.join("_").toLowerCase()} "${lbl}" (or separate nodes with commas)`, "error");
|
|
3286
|
+
break;
|
|
3287
|
+
}
|
|
3288
|
+
if (tk.kind === "punct" && tk.value === "{" && k === t.length - 1 && k > 0 && t[k - 1].kind !== "punct") {
|
|
3289
|
+
const prev = t.slice(0, k).reverse().find((x) => x.kind === "ident");
|
|
3290
|
+
stack.push({
|
|
3291
|
+
kind: "body",
|
|
3292
|
+
id: prev.value,
|
|
3293
|
+
start: li,
|
|
3294
|
+
lines: []
|
|
3295
|
+
});
|
|
3296
|
+
break;
|
|
3297
|
+
}
|
|
3298
|
+
if (tk.kind !== "ident") {
|
|
3299
|
+
warn(li, `Unexpected "${tk.value}"`, "error");
|
|
3300
|
+
break;
|
|
3301
|
+
}
|
|
3302
|
+
sep = false;
|
|
3303
|
+
const n = touchNode(tk.value);
|
|
3304
|
+
n.implicit = false;
|
|
3305
|
+
const grp = currentGroup();
|
|
3306
|
+
if (grp && n.group && n.group !== grp) warn(li, `"${tk.value}" is already in group "${n.group}": a node lives in one group, so it moves to "${grp}"`);
|
|
3307
|
+
if (grp) n.group = grp;
|
|
3308
|
+
k++;
|
|
3309
|
+
if (t[k]?.kind === "string") relabel(n, t[k++], li, false);
|
|
3310
|
+
else n.labelRef ??= {
|
|
3311
|
+
line: li,
|
|
3312
|
+
at: tk.end,
|
|
3313
|
+
generated: gen || void 0
|
|
3314
|
+
};
|
|
3315
|
+
while (t[k]?.kind === "class") {
|
|
3316
|
+
const c = t[k++].value;
|
|
3317
|
+
useClass(c, li);
|
|
3318
|
+
if (!n.classes.includes(c)) n.classes.push(c);
|
|
3319
|
+
}
|
|
3320
|
+
const ref = {
|
|
3321
|
+
line: li,
|
|
3322
|
+
insertAt: t[k - 1].end,
|
|
3323
|
+
generated: gen || void 0
|
|
3324
|
+
};
|
|
3325
|
+
if (t[k]?.kind === "props") {
|
|
3326
|
+
addNodeProps(n, nodeProps(t[k], n.id, li, warn), li);
|
|
3327
|
+
ref.props = [t[k].start, t[k].end];
|
|
3328
|
+
k++;
|
|
3329
|
+
}
|
|
3330
|
+
n.ref = ref;
|
|
3331
|
+
lastDecl = {
|
|
3332
|
+
indent: indentOf(raw),
|
|
3333
|
+
targets: [n]
|
|
3334
|
+
};
|
|
3335
|
+
}
|
|
3336
|
+
};
|
|
3337
|
+
/** one line after substitution: picks the handler; the order of the checks matters */
|
|
3338
|
+
const statement = (raw, li) => {
|
|
3339
|
+
const top = stack[stack.length - 1];
|
|
3340
|
+
if (top?.kind === "body") return bodyLine(top, raw, li);
|
|
3341
|
+
if (top?.kind === "panel") return panelLine(top, raw, li);
|
|
3342
|
+
if (top?.kind === "theme") return themeBodyLine(top, raw, li);
|
|
3343
|
+
if (top?.kind === "layout") return layoutLine(raw, li);
|
|
3344
|
+
const { tokens: t, codeEnd } = lex(raw);
|
|
3345
|
+
if (!t.length) return;
|
|
3346
|
+
const foreign = /^\s*%%/.test(raw) ? "Comments start with # or //" : /(--|==|-\.|->|=>)\|[^|]*\|/.test(raw) ? "Edge labels go after a colon: a -> b: label" : void 0;
|
|
3347
|
+
if (foreign) return void warn(li, foreign, "error");
|
|
3348
|
+
if (t[0].kind === "punct" && t[0].value === "}") return closeBlock(li);
|
|
3349
|
+
const head = t[0].kind === "ident" ? t[0].value : void 0;
|
|
3350
|
+
const selective = t[1]?.value === "{" && t.some((x) => x.kind === "ident" && x.value === "from");
|
|
3351
|
+
if (head === "import" && (t[1]?.kind === "string" || t.length === 1 || selective)) return importStmt(t, li, selective);
|
|
3352
|
+
if (head === "export" && t.length > 1 && t[1].kind !== "arrow" && t[1].value !== ":") return exportStmt(t, raw, li);
|
|
3353
|
+
const stmt = head !== void 0 ? STATEMENTS.get(head) : void 0;
|
|
3354
|
+
if (stmt && t[1]?.kind !== "arrow" && t[1]?.value !== ":" && t[1]?.value !== ",") return void stmt.parse(raw.slice(t[0].end, codeEnd).trim(), d, {
|
|
3355
|
+
line: li,
|
|
3356
|
+
warn: (msg, sev) => warn(li, msg, sev)
|
|
3357
|
+
});
|
|
3358
|
+
if (head === "theme" && t[1]?.kind === "ident" && (t[2]?.kind === "props" || t[2]?.value === "{")) return themeStmt(t, li);
|
|
3359
|
+
const opensBlock = t[t.length - 1].value === "{";
|
|
3360
|
+
if (head !== void 0 && t.length > 2 && !PANELS.has(head) && t[1].kind === "ident" && opensBlock && head !== "group" && head !== "layout") unknownPanelHint(t, li);
|
|
3361
|
+
if (head !== void 0 && PANELS.has(head) && opensBlock && (t[1]?.kind === "ident" || t[1]?.kind === "string" || t[1]?.kind === "props" || t[1]?.value === "{")) return panelStmt(t, li);
|
|
3362
|
+
if (head === "layout" && t[1]?.value === "{") return layoutStmt(t, li);
|
|
3363
|
+
if (head === "group" && (t[1]?.kind === "ident" || t[1]?.kind === "string" && t.some((x) => x.value === "{"))) return groupStmt(t, raw, li);
|
|
3364
|
+
const last = t[t.length - 1];
|
|
3365
|
+
if (t[0].kind === "class" && t.slice(1, -1).every((x) => x.kind === "class") && (last.kind === "props" || last.kind === "class")) return classStmt(t, raw, li);
|
|
3366
|
+
if (head !== void 0 && t[1]?.kind === "punct" && t[1].value === ":") return settingStmt(t, raw, li, codeEnd, top);
|
|
3367
|
+
if (t.some((x) => x.kind === "arrow")) return edgeStmt(t, raw, li, codeEnd);
|
|
3368
|
+
nodeStmt(t, raw, li);
|
|
3369
|
+
};
|
|
3370
|
+
for (let li = 0; li < lines.length; li++) step(lines[li], li);
|
|
3371
|
+
for (const ctx of stack) if (ctx.kind === "group") warn(lines.length - 1, `Group "${ctx.id}" is missing "}"`, "error");
|
|
3372
|
+
else if (ctx.kind === "body") warn(ctx.start, `Body of "${ctx.id}" is missing a closing "}" line`, "error");
|
|
3373
|
+
else if (ctx.kind === "panel") {
|
|
3374
|
+
warn(ctx.start, `${ctx.panel.kind} "${ctx.id}" is missing a closing "}" line`, "error");
|
|
3375
|
+
closePanel(ctx, lines.length - 1);
|
|
3376
|
+
} else if (ctx.kind === "def") warn(ctx.start, `def ${ctx.id}() is missing "}"`, "error");
|
|
3377
|
+
else if (ctx.kind === "theme") warn(ctx.start, `theme ${ctx.id} is missing "}"`, "error");
|
|
3378
|
+
else warn(lines.length - 1, "layout block is missing \"}\"", "error");
|
|
3379
|
+
const names = themeNames(d.themes);
|
|
3380
|
+
for (const [name, li] of themeLines) {
|
|
3381
|
+
const base = d.themes[name].base;
|
|
3382
|
+
if (base && base !== name && !names.includes(base)) warn(li, `Unknown base theme "${base}"${didYouMean(base, names)}`);
|
|
3383
|
+
}
|
|
3384
|
+
const theme = d.settings.theme;
|
|
3385
|
+
if (themeLine !== void 0 && theme && !theme.includes("$")) {
|
|
3386
|
+
if (!names.includes(theme)) warn(themeLine, `Unknown theme "${theme}"${didYouMean(theme, names) || ` (use one of: ${names.join(", ")})`}`);
|
|
3387
|
+
}
|
|
3388
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3389
|
+
for (const [c, line] of classUses) {
|
|
3390
|
+
if (d.classes[c] || seen.has(`${line}.${c}`)) continue;
|
|
3391
|
+
seen.add(`${line}.${c}`);
|
|
3392
|
+
warn(line, `Unknown class ".${c}"`);
|
|
3393
|
+
}
|
|
3394
|
+
if (hasExport) {
|
|
3395
|
+
const panelIds = new Set(d.panels.map((p) => p.id));
|
|
3396
|
+
d.exports = [...exported.keys()].filter((x) => x.startsWith(".") ? x.slice(1) in d.classes : x.startsWith("$") ? x.slice(1) in vars : d.nodes.has(x) || d.groups.has(x) || panelIds.has(x) || x in defs || x in (d.themes ?? {}));
|
|
3397
|
+
for (const [x, line] of exported) if (!d.exports.includes(x) && !PANELS.has(x) && x !== "group" && x !== "def") warn(line, `export: "${x}" is not declared in this file`);
|
|
3398
|
+
}
|
|
3399
|
+
for (const p of d.panels) {
|
|
3400
|
+
if (d.nodes.get(p.id)?.implicit) d.nodes.delete(p.id);
|
|
3401
|
+
const o = d.layout.nodes[p.id];
|
|
3402
|
+
if (!o) continue;
|
|
3403
|
+
if (o.x !== void 0 && o.y !== void 0) panelOverride(d, p.id).at = {
|
|
3404
|
+
x: o.x,
|
|
3405
|
+
y: o.y
|
|
3406
|
+
};
|
|
3407
|
+
delete d.layout.nodes[p.id];
|
|
3408
|
+
}
|
|
3409
|
+
const panelAt = /* @__PURE__ */ new Map();
|
|
3410
|
+
for (const p of d.panels) {
|
|
3411
|
+
const first = panelAt.get(p.id);
|
|
3412
|
+
if (!first) panelAt.set(p.id, p);
|
|
3413
|
+
else if (p.lines) warn(p.lines[0], `panel id "${p.id}" is already used${first.lines ? ` on line ${first.lines[0] + 1}` : " by an import"}: layout, edges and edits can't tell them apart, rename one`);
|
|
3414
|
+
}
|
|
3415
|
+
for (const [id, g] of d.groups) if (d.nodes.has(id)) warn(d.edges.find((e) => e.ref && (e.from === id || e.to === id))?.ref?.line ?? g.ref?.line ?? 0, `"${id}" is both a node and a group. Edges connect nodes, not groups: use a node inside "${id}", or rename one of them`);
|
|
3416
|
+
lint(d, (kind) => PANELS.get(kind)?.options);
|
|
3417
|
+
if (!opts.chain) collecting(d.diagnostics, () => TRANSFORMS.forEach((t) => t(d)));
|
|
3418
|
+
for (const id of Object.keys(d.layout.nodes)) {
|
|
3419
|
+
const at = layoutLines.get(id);
|
|
3420
|
+
if (at !== void 0 && !d.nodes.has(id) && !d.groups.has(id)) warn(at, `layout: there is no node "${id}" (renamed or deleted?); remove this line${didYouMean(id, [...d.nodes.keys()])}`);
|
|
3421
|
+
}
|
|
3422
|
+
return d;
|
|
3423
|
+
}
|
|
3424
|
+
/** `import { a, b as c, .svc } from "path"` → names and path, or null after an error */
|
|
3425
|
+
function readImportList(t, li, warn) {
|
|
3426
|
+
const close = t.findIndex((x) => x.value === "}");
|
|
3427
|
+
const fail = (msg) => (warn(li, `${msg}: import { a, b as c, .style } from "./file.yu"`, "error"), null);
|
|
3428
|
+
if (close < 0) return fail("import list is missing \"}\"");
|
|
3429
|
+
const names = [];
|
|
3430
|
+
let k = 2;
|
|
3431
|
+
while (k < close) {
|
|
3432
|
+
const x = t[k];
|
|
3433
|
+
if (x.value === ",") {
|
|
3434
|
+
k++;
|
|
3435
|
+
continue;
|
|
3436
|
+
}
|
|
3437
|
+
if (x.kind !== "ident" && x.kind !== "class") return fail(`Unexpected "${x.value}" in import list`);
|
|
3438
|
+
const name = x.kind === "class" ? `.${x.value}` : x.value;
|
|
3439
|
+
let alias = name;
|
|
3440
|
+
k++;
|
|
3441
|
+
if (t[k]?.kind === "ident" && t[k].value === "as") {
|
|
3442
|
+
const a = t[k + 1];
|
|
3443
|
+
if (!a || a.kind !== "ident" && a.kind !== "class" || a.kind === "class" !== name.startsWith(".") || a.value.startsWith("$") !== name.startsWith("$")) return fail(`"as" needs a new ${name.startsWith(".") ? "class" : name.startsWith("$") ? "variable" : "name"} after "${name}"`);
|
|
3444
|
+
alias = a.kind === "class" ? `.${a.value}` : a.value;
|
|
3445
|
+
k += 2;
|
|
3446
|
+
}
|
|
3447
|
+
names.push([name, alias]);
|
|
3448
|
+
}
|
|
3449
|
+
if (t[close + 1]?.value !== "from" || t[close + 2]?.kind !== "string") return fail("Expected from \"path\" after the import list");
|
|
3450
|
+
if (t.length > close + 3) return fail(`Unexpected "${t[close + 3].value}" after import path`);
|
|
3451
|
+
if (!names.length) return fail("Empty import list");
|
|
3452
|
+
return {
|
|
3453
|
+
path: t[close + 2].value,
|
|
3454
|
+
names
|
|
3455
|
+
};
|
|
3456
|
+
}
|
|
3457
|
+
/**
|
|
3458
|
+
* What an importer gets from `sub`: everything, or only exported names, or the
|
|
3459
|
+
* names a selective import asks for (renamed by `as`). Groups bring their
|
|
3460
|
+
* members, edges come along when both ends do, classes when something uses them.
|
|
3461
|
+
*/
|
|
3462
|
+
function narrow(sub, wanted, fail) {
|
|
3463
|
+
const exp = sub.exports;
|
|
3464
|
+
if (!wanted && !exp) return sub;
|
|
3465
|
+
const isOpen = (name) => !exp || exp.includes(name);
|
|
3466
|
+
const rename = /* @__PURE__ */ new Map();
|
|
3467
|
+
const names = /* @__PURE__ */ new Set();
|
|
3468
|
+
const panelIds = new Set(sub.panels.map((p) => p.id));
|
|
3469
|
+
if (wanted) for (const [name, alias] of wanted) {
|
|
3470
|
+
const id = name.replace(/^\./, "");
|
|
3471
|
+
if (sub.sharedSettings?.includes(name) && !sub.nodes.has(name) && !sub.groups.has(name)) {
|
|
3472
|
+
if (alias !== name) fail(`a setting keeps its name: import { ${name} }`);
|
|
3473
|
+
else names.add(name);
|
|
3474
|
+
continue;
|
|
3475
|
+
}
|
|
3476
|
+
const exists = name.startsWith(".") ? id in sub.classes : name.startsWith("$") ? name.slice(1) in (sub.vars ?? {}) : sub.nodes.has(id) || sub.groups.has(id) || panelIds.has(id) || id in (sub.defs ?? {});
|
|
3477
|
+
if (!exists && name in (sub.themes ?? {})) continue;
|
|
3478
|
+
if (!exists) fail(`no "${name}" to import${exp?.length ? ` (exports: ${exp.join(", ")})` : ""}`);
|
|
3479
|
+
else if (!isOpen(name)) fail(`"${name}" is not exported (exports: ${exp.join(", ") || "none"})`);
|
|
3480
|
+
else {
|
|
3481
|
+
names.add(name);
|
|
3482
|
+
if (alias !== name) rename.set(name, alias);
|
|
3483
|
+
}
|
|
3484
|
+
}
|
|
3485
|
+
else for (const x of exp) names.add(x);
|
|
3486
|
+
const inPicked = (group) => {
|
|
3487
|
+
for (let g = group; g; g = sub.groups.get(g)?.parent) if (names.has(g)) return true;
|
|
3488
|
+
return false;
|
|
3489
|
+
};
|
|
3490
|
+
const out = {
|
|
3491
|
+
settings: {},
|
|
3492
|
+
nodes: /* @__PURE__ */ new Map(),
|
|
3493
|
+
edges: [],
|
|
3494
|
+
groups: /* @__PURE__ */ new Map(),
|
|
3495
|
+
classes: table({}),
|
|
3496
|
+
layout: {
|
|
3497
|
+
nodes: {},
|
|
3498
|
+
edges: {}
|
|
3499
|
+
},
|
|
3500
|
+
panels: [],
|
|
3501
|
+
diagnostics: []
|
|
3502
|
+
};
|
|
3503
|
+
const id = (x) => rename.get(x)?.replace(/^\./, "") ?? x;
|
|
3504
|
+
const cls = (c) => rename.get(`.${c}`)?.slice(1) ?? c;
|
|
3505
|
+
const used = /* @__PURE__ */ new Set();
|
|
3506
|
+
for (const g of sub.groups.values()) if (names.has(g.id) || inPicked(g.parent)) {
|
|
3507
|
+
out.groups.set(id(g.id), {
|
|
3508
|
+
...g,
|
|
3509
|
+
id: id(g.id),
|
|
3510
|
+
parent: g.parent && out.groups.has(id(g.parent)) ? id(g.parent) : void 0,
|
|
3511
|
+
classes: g.classes?.map(cls)
|
|
3512
|
+
});
|
|
3513
|
+
g.classes?.forEach((c) => used.add(c));
|
|
3514
|
+
}
|
|
3515
|
+
const picked = (node) => names.has(node) || inPicked(sub.nodes.get(node)?.group);
|
|
3516
|
+
for (const n of sub.nodes.values()) if (picked(n.id)) {
|
|
3517
|
+
const group = n.group && out.groups.has(id(n.group)) ? id(n.group) : void 0;
|
|
3518
|
+
out.nodes.set(id(n.id), {
|
|
3519
|
+
...n,
|
|
3520
|
+
id: id(n.id),
|
|
3521
|
+
group,
|
|
3522
|
+
classes: n.classes.map(cls)
|
|
3523
|
+
});
|
|
3524
|
+
n.classes.forEach((c) => used.add(c));
|
|
3525
|
+
}
|
|
3526
|
+
for (const e of sub.edges) if (picked(e.from) && picked(e.to)) {
|
|
3527
|
+
const key = id(e.from) + "->" + id(e.to) + (e.key.match(/#\d+$/)?.[0] ?? "");
|
|
3528
|
+
out.edges.push({
|
|
3529
|
+
...e,
|
|
3530
|
+
key,
|
|
3531
|
+
from: id(e.from),
|
|
3532
|
+
to: id(e.to),
|
|
3533
|
+
classes: e.classes?.map(cls)
|
|
3534
|
+
});
|
|
3535
|
+
e.classes?.forEach((c) => used.add(c));
|
|
3536
|
+
}
|
|
3537
|
+
for (const p of sub.panels) if (names.has(p.id)) out.panels.push({
|
|
3538
|
+
...p,
|
|
3539
|
+
id: id(p.id)
|
|
3540
|
+
});
|
|
3541
|
+
for (const [k, def] of Object.entries(sub.defs ?? {})) if (names.has(k)) {
|
|
3542
|
+
for (const l of def.body) for (const x of lex(l).tokens) if (x.kind === "class") used.add(x.value);
|
|
3543
|
+
}
|
|
3544
|
+
out.sharedSettings = (sub.sharedSettings ?? []).filter((k) => !wanted || names.has(k));
|
|
3545
|
+
for (const k of out.sharedSettings) out.settings[k] = sub.settings[k];
|
|
3546
|
+
for (const [c, props] of Object.entries(sub.classes)) if (names.has(`.${c}`) || used.has(c)) out.classes[cls(c)] = props;
|
|
3547
|
+
out.vars = {};
|
|
3548
|
+
for (const [k, v] of Object.entries(sub.vars ?? {})) if (names.has(`$${k}`)) out.vars[(rename.get(`$${k}`) ?? `$${k}`).slice(1)] = v;
|
|
3549
|
+
out.defs = {};
|
|
3550
|
+
for (const [k, v] of Object.entries(sub.defs ?? {})) if (names.has(k)) out.defs[rename.get(k) ?? k] = v;
|
|
3551
|
+
out.themes = sub.themes;
|
|
3552
|
+
return out;
|
|
3553
|
+
}
|
|
3554
|
+
/**
|
|
3555
|
+
* Merge an imported diagram into `d`. Things `d` already has keep their
|
|
3556
|
+
* values; imported declarations carry no source refs, so editors write
|
|
3557
|
+
* overrides into the importing file instead of the imported one.
|
|
3558
|
+
*/
|
|
3559
|
+
function mergeDiagram(d, sub, edgeCount) {
|
|
3560
|
+
for (const k of sub.sharedSettings ?? []) if (!(k in d.settings) && k in sub.settings) d.settings[k] = sub.settings[k];
|
|
3561
|
+
for (const [c, props] of Object.entries(sub.classes)) d.classes[c] = {
|
|
3562
|
+
...props,
|
|
3563
|
+
...d.classes[c]
|
|
3564
|
+
};
|
|
3565
|
+
for (const g of sub.groups.values()) if (!d.groups.has(g.id)) d.groups.set(g.id, {
|
|
3566
|
+
...g,
|
|
3567
|
+
props: { ...g.props },
|
|
3568
|
+
ref: void 0,
|
|
3569
|
+
labelRef: void 0
|
|
3570
|
+
});
|
|
3571
|
+
for (const n of sub.nodes.values()) {
|
|
3572
|
+
const own = d.nodes.get(n.id);
|
|
3573
|
+
if (!own) {
|
|
3574
|
+
d.nodes.set(n.id, {
|
|
3575
|
+
...n,
|
|
3576
|
+
props: { ...n.props },
|
|
3577
|
+
classes: [...n.classes],
|
|
3578
|
+
ref: void 0,
|
|
3579
|
+
labelRef: void 0,
|
|
3580
|
+
bodyLines: void 0
|
|
3581
|
+
});
|
|
3582
|
+
continue;
|
|
3583
|
+
}
|
|
3584
|
+
if (own.implicit && !n.implicit) {
|
|
3585
|
+
own.label = n.label;
|
|
3586
|
+
own.implicit = false;
|
|
3587
|
+
}
|
|
3588
|
+
own.props = {
|
|
3589
|
+
...n.props,
|
|
3590
|
+
...own.props
|
|
3591
|
+
};
|
|
3592
|
+
for (const c of n.classes) if (!own.classes.includes(c)) own.classes.push(c);
|
|
3593
|
+
own.group ??= n.group;
|
|
3594
|
+
if (!own.body && n.body) own.body = n.body;
|
|
3595
|
+
}
|
|
3596
|
+
for (const e of sub.edges) {
|
|
3597
|
+
const base = `${e.from}->${e.to}`;
|
|
3598
|
+
const n = (edgeCount.get(base) ?? 0) + 1;
|
|
3599
|
+
edgeCount.set(base, n);
|
|
3600
|
+
d.edges.push({
|
|
3601
|
+
...e,
|
|
3602
|
+
key: n === 1 ? base : `${base}#${n}`,
|
|
3603
|
+
ref: void 0,
|
|
3604
|
+
labelRef: void 0
|
|
3605
|
+
});
|
|
3606
|
+
}
|
|
3607
|
+
for (const p of sub.panels) d.panels.push({
|
|
3608
|
+
...p,
|
|
3609
|
+
lines: void 0,
|
|
3610
|
+
ref: void 0,
|
|
3611
|
+
labelRef: void 0
|
|
3612
|
+
});
|
|
3613
|
+
for (const [k, v] of Object.entries(sub.data ?? {})) if (!(k in (d.data ??= {}))) d.data[k] = v;
|
|
3614
|
+
if (sub.layout.ranks?.length) (d.layout.ranks ??= []).push(...sub.layout.ranks);
|
|
3615
|
+
for (const [k, v] of Object.entries(sub.vars ?? {})) (d.vars ??= {})[k] ??= v;
|
|
3616
|
+
for (const [k, v] of Object.entries(sub.defs ?? {})) (d.defs ??= {})[k] ??= v;
|
|
3617
|
+
for (const [k, v] of Object.entries(sub.themes ?? {})) (d.themes ??= table({}))[k] ??= v;
|
|
3618
|
+
}
|
|
3619
|
+
function parseEdgeStatement(t, li, raw, d, touchNode, edgeCount, warn, relabel, useClass) {
|
|
3620
|
+
let k = 0;
|
|
3621
|
+
let inlineLabel = false;
|
|
3622
|
+
/** `a`, `a "Label" .cls [props]`, `a, b, c` — inline declarations bind to their node */
|
|
3623
|
+
const readNodes = (target = false) => {
|
|
3624
|
+
const ids = [];
|
|
3625
|
+
let sep = true;
|
|
3626
|
+
while (k < t.length) {
|
|
3627
|
+
if (t[k].kind === "punct" && t[k].value === ",") {
|
|
3628
|
+
k++;
|
|
3629
|
+
sep = true;
|
|
3630
|
+
continue;
|
|
3631
|
+
}
|
|
3632
|
+
if (t[k].kind !== "ident") break;
|
|
3633
|
+
if (!sep) {
|
|
3634
|
+
const prev = ids.pop();
|
|
3635
|
+
const words = [prev];
|
|
3636
|
+
while (t[k]?.kind === "ident") words.push(t[k++].value);
|
|
3637
|
+
const lbl = words.join(" ");
|
|
3638
|
+
warn(li, `"${lbl}" looks like a label: write ${words.join("_").toLowerCase()} "${lbl}" (or separate nodes with commas)`, "error");
|
|
3639
|
+
ids.push(prev);
|
|
3640
|
+
continue;
|
|
3641
|
+
}
|
|
3642
|
+
sep = false;
|
|
3643
|
+
const id = t[k++].value;
|
|
3644
|
+
ids.push(id);
|
|
3645
|
+
const startK = k;
|
|
3646
|
+
const fresh = !d.nodes.has(id);
|
|
3647
|
+
const n = touchNode(id);
|
|
3648
|
+
if (t[k]?.kind === "string") {
|
|
3649
|
+
const tok = t[k++];
|
|
3650
|
+
const lbl = tok.value;
|
|
3651
|
+
if (target && fresh && k === t.length && !inlineLabel && /^[a-z][a-z ]*$/.test(lbl)) warn(li, `"${lbl}" becomes the label of node "${id}". For an edge label write: ... -> ${id}: ${lbl}`);
|
|
3652
|
+
relabel(n, tok, li, true);
|
|
3653
|
+
inlineLabel = true;
|
|
3654
|
+
}
|
|
3655
|
+
while (t[k]?.kind === "class") {
|
|
3656
|
+
const c = t[k++].value;
|
|
3657
|
+
useClass(c, li);
|
|
3658
|
+
if (!n.classes.includes(c)) n.classes.push(c);
|
|
3659
|
+
}
|
|
3660
|
+
const own = !n.ref || n.implicit;
|
|
3661
|
+
if (t[k]?.kind === "props") {
|
|
3662
|
+
addNodeProps(n, nodeProps(t[k], id, li, warn), li);
|
|
3663
|
+
if (own) n.ref = {
|
|
3664
|
+
line: li,
|
|
3665
|
+
props: [t[k].start, t[k].end],
|
|
3666
|
+
insertAt: t[k].start
|
|
3667
|
+
};
|
|
3668
|
+
k++;
|
|
3669
|
+
} else if (k > startK && own) n.ref = {
|
|
3670
|
+
line: li,
|
|
3671
|
+
insertAt: t[k - 1].end
|
|
3672
|
+
};
|
|
3673
|
+
if (k > startK) n.implicit = false;
|
|
3674
|
+
if (k > startK && own && !n.labelRef) n.labelRef = {
|
|
3675
|
+
line: li,
|
|
3676
|
+
at: t[startK - 1].end
|
|
3677
|
+
};
|
|
3678
|
+
}
|
|
3679
|
+
return ids;
|
|
3680
|
+
};
|
|
3681
|
+
let sources = readNodes();
|
|
3682
|
+
if (!sources.length) {
|
|
3683
|
+
warn(li, "Edge needs a source node", "error");
|
|
3684
|
+
return;
|
|
3685
|
+
}
|
|
3686
|
+
const segs = [];
|
|
3687
|
+
while (k < t.length && t[k].kind === "arrow") {
|
|
3688
|
+
const arrow = t[k++].value;
|
|
3689
|
+
const targets = readNodes(true);
|
|
3690
|
+
if (!targets.length) {
|
|
3691
|
+
warn(li, `Edge "${arrow}" needs a target node`, "error");
|
|
3692
|
+
return;
|
|
3693
|
+
}
|
|
3694
|
+
segs.push({
|
|
3695
|
+
from: sources,
|
|
3696
|
+
to: targets,
|
|
3697
|
+
arrow
|
|
3698
|
+
});
|
|
3699
|
+
sources = targets;
|
|
3700
|
+
}
|
|
3701
|
+
let label;
|
|
3702
|
+
const classes = [];
|
|
3703
|
+
let props = {};
|
|
3704
|
+
const ref = {
|
|
3705
|
+
line: li,
|
|
3706
|
+
insertAt: t[k - 1].end,
|
|
3707
|
+
prefix: ":"
|
|
3708
|
+
};
|
|
3709
|
+
const labelRef = {
|
|
3710
|
+
line: li,
|
|
3711
|
+
at: t[k - 1].end,
|
|
3712
|
+
colon: false
|
|
3713
|
+
};
|
|
3714
|
+
if (t[k]?.kind === "punct" && t[k].value === ":") {
|
|
3715
|
+
ref.insertAt = labelRef.at = t[k].end;
|
|
3716
|
+
labelRef.colon = void 0;
|
|
3717
|
+
ref.prefix = void 0;
|
|
3718
|
+
k++;
|
|
3719
|
+
const lblStart = k;
|
|
3720
|
+
while (k < t.length && t[k].kind !== "class" && t[k].kind !== "props") k++;
|
|
3721
|
+
if (k > lblStart) {
|
|
3722
|
+
label = k - lblStart === 1 && t[lblStart].kind === "string" ? t[lblStart].value : raw.slice(t[lblStart].start, t[k - 1].end).trim();
|
|
3723
|
+
ref.insertAt = t[k - 1].end;
|
|
3724
|
+
labelRef.range = [t[lblStart].start, t[k - 1].end];
|
|
3725
|
+
}
|
|
3726
|
+
while (t[k]?.kind === "class") {
|
|
3727
|
+
useClass(t[k].value, li);
|
|
3728
|
+
classes.push(t[k].value);
|
|
3729
|
+
ref.insertAt = t[k++].end;
|
|
3730
|
+
}
|
|
3731
|
+
if (t[k]?.kind === "props") {
|
|
3732
|
+
props = parseProps(t[k].value);
|
|
3733
|
+
ref.props = [t[k].start, t[k].end];
|
|
3734
|
+
k++;
|
|
3735
|
+
}
|
|
3736
|
+
}
|
|
3737
|
+
if (k < t.length) {
|
|
3738
|
+
const hint = t[k].kind === "string" ? " (edge labels go after a colon: a -> b: \"label\")" : "";
|
|
3739
|
+
warn(li, `Unexpected "${t[k].value}" in edge${hint}`, "error");
|
|
3740
|
+
}
|
|
3741
|
+
for (const seg of segs) {
|
|
3742
|
+
const info = arrowInfo(seg.arrow);
|
|
3743
|
+
for (const a of seg.from) for (const b of seg.to) {
|
|
3744
|
+
let from = a;
|
|
3745
|
+
let to = b;
|
|
3746
|
+
let startHead = info.start;
|
|
3747
|
+
let endHead = info.end;
|
|
3748
|
+
if (startHead && !endHead) {
|
|
3749
|
+
[from, to] = [to, from];
|
|
3750
|
+
[startHead, endHead] = [false, true];
|
|
3751
|
+
}
|
|
3752
|
+
const base = `${from}->${to}`;
|
|
3753
|
+
const n = (edgeCount.get(base) ?? 0) + 1;
|
|
3754
|
+
edgeCount.set(base, n);
|
|
3755
|
+
const e = {
|
|
3756
|
+
key: n === 1 ? base : `${base}#${n}`,
|
|
3757
|
+
from,
|
|
3758
|
+
to,
|
|
3759
|
+
label,
|
|
3760
|
+
startHead,
|
|
3761
|
+
endHead,
|
|
3762
|
+
line: info.line,
|
|
3763
|
+
props: { ...props },
|
|
3764
|
+
classes: [...classes],
|
|
3765
|
+
ref,
|
|
3766
|
+
labelRef
|
|
3767
|
+
};
|
|
3768
|
+
d.edges.push(e);
|
|
3769
|
+
}
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
const panelOverride = (d, id) => (d.layout.panels ??= {})[id] ??= {};
|
|
3773
|
+
function parseLayoutLine(code, li, d, warn) {
|
|
3774
|
+
const words = code.replace(/\s*->\s*/g, "->").split(/\s+/);
|
|
3775
|
+
const head = words[0];
|
|
3776
|
+
const nums = (from, count) => {
|
|
3777
|
+
const out = [];
|
|
3778
|
+
for (let i = 0; i < count; i++) {
|
|
3779
|
+
const v = Number(words[from + i]);
|
|
3780
|
+
if (!Number.isFinite(v)) return null;
|
|
3781
|
+
out.push(v);
|
|
3782
|
+
}
|
|
3783
|
+
return out;
|
|
3784
|
+
};
|
|
3785
|
+
let i = 1;
|
|
3786
|
+
if (head === "rank" && words[1] !== "at" && words[1] !== "size") {
|
|
3787
|
+
const ids = words.slice(1).filter(Boolean);
|
|
3788
|
+
if (ids.length < 2) return warn(li, "rank needs two or more node ids: rank a b c");
|
|
3789
|
+
(d.layout.ranks ??= []).push(ids);
|
|
3790
|
+
return;
|
|
3791
|
+
}
|
|
3792
|
+
if (head.includes("->")) {
|
|
3793
|
+
const m = head.match(/^(.+?)->(.+?)(?:#(\d+))?$/);
|
|
3794
|
+
if (!m) return warn(li, `Bad edge key "${head}"`);
|
|
3795
|
+
const key = `${m[1]}->${m[2]}${m[3] && m[3] !== "1" ? "#" + m[3] : ""}`;
|
|
3796
|
+
const o = d.layout.edges[key] ??= {};
|
|
3797
|
+
while (i < words.length) {
|
|
3798
|
+
const kw = words[i++];
|
|
3799
|
+
if (kw === "via") {
|
|
3800
|
+
o.via = [];
|
|
3801
|
+
while (i + 1 < words.length && Number.isFinite(Number(words[i])) && Number.isFinite(Number(words[i + 1]))) {
|
|
3802
|
+
o.via.push({
|
|
3803
|
+
x: Number(words[i]),
|
|
3804
|
+
y: Number(words[i + 1])
|
|
3805
|
+
});
|
|
3806
|
+
i += 2;
|
|
3807
|
+
}
|
|
3808
|
+
} else if (kw === "label") {
|
|
3809
|
+
const v = nums(i, 2);
|
|
3810
|
+
if (!v) return warn(li, "label expects two numbers: dx dy");
|
|
3811
|
+
o.label = {
|
|
3812
|
+
x: v[0],
|
|
3813
|
+
y: v[1]
|
|
3814
|
+
};
|
|
3815
|
+
i += 2;
|
|
3816
|
+
} else if (kw === "ports") {
|
|
3817
|
+
const a = words[i];
|
|
3818
|
+
const b = words[i + 1];
|
|
3819
|
+
if (!SIDES.has(a) || !SIDES.has(b)) return warn(li, "ports expects two sides: top|right|bottom|left|auto");
|
|
3820
|
+
o.ports = [a, b];
|
|
3821
|
+
i += 2;
|
|
3822
|
+
} else return warn(li, `Unknown layout keyword "${kw}"`);
|
|
3823
|
+
}
|
|
3824
|
+
return;
|
|
3825
|
+
}
|
|
3826
|
+
const colon = head.indexOf(":");
|
|
3827
|
+
if (colon > 0) {
|
|
3828
|
+
const panel = head.slice(0, colon);
|
|
3829
|
+
const part = head.slice(colon + 1);
|
|
3830
|
+
const from = words[1] === "offset" ? 2 : 1;
|
|
3831
|
+
const count = words.length - from;
|
|
3832
|
+
const v = part && (count === 1 || count === 2) ? nums(from, count) : null;
|
|
3833
|
+
if (!v) return warn(li, `Bad panel part "${code.trim()}" (expected panel:part d, or panel:part dx dy)`);
|
|
3834
|
+
(panelOverride(d, panel).parts ??= {})[part] = {
|
|
3835
|
+
x: v[0],
|
|
3836
|
+
y: v[1] ?? 0
|
|
3837
|
+
};
|
|
3838
|
+
return;
|
|
3839
|
+
}
|
|
3840
|
+
if (d.panels.some((p) => p.id === head)) {
|
|
3841
|
+
const v = words[1] === "at" && words.length === 4 ? nums(2, 2) : null;
|
|
3842
|
+
if (!v) return warn(li, `panel "${head}" takes only "at x y" in layout (its content sets the size)`);
|
|
3843
|
+
panelOverride(d, head).at = {
|
|
3844
|
+
x: v[0],
|
|
3845
|
+
y: v[1]
|
|
3846
|
+
};
|
|
3847
|
+
return;
|
|
3848
|
+
}
|
|
3849
|
+
const o = d.layout.nodes[head] ??= {};
|
|
3850
|
+
while (i < words.length) {
|
|
3851
|
+
const kw = words[i++];
|
|
3852
|
+
if (kw === "at") {
|
|
3853
|
+
const v = nums(i, 2);
|
|
3854
|
+
if (!v) return warn(li, "at expects two numbers: x y");
|
|
3855
|
+
o.x = v[0];
|
|
3856
|
+
o.y = v[1];
|
|
3857
|
+
i += 2;
|
|
3858
|
+
} else if (kw === "size") {
|
|
3859
|
+
const v = nums(i, 2);
|
|
3860
|
+
if (!v) return warn(li, "size expects two numbers: width height");
|
|
3861
|
+
o.w = v[0];
|
|
3862
|
+
o.h = v[1];
|
|
3863
|
+
i += 2;
|
|
3864
|
+
} else return warn(li, `Unknown layout keyword "${kw}"`);
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3867
|
+
// src/core/plugin.ts
|
|
3868
|
+
/** what `options[key]` asks for: a select of values, or one kind of field */
|
|
3869
|
+
function optionKind(o) {
|
|
3870
|
+
return typeof o === "string" ? { kind: o } : {
|
|
3871
|
+
kind: "select",
|
|
3872
|
+
values: o
|
|
3873
|
+
};
|
|
3874
|
+
}
|
|
3875
|
+
/**
|
|
3876
|
+
* `fn` computed once per panel per layout pass: `measure`, `render` and
|
|
3877
|
+
* `handles` share one ctx, so a plugin's geometry wrapped in this is worked
|
|
3878
|
+
* out (and its text measured) once instead of three times.
|
|
3879
|
+
*/
|
|
3880
|
+
function perPass(fn) {
|
|
3881
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
3882
|
+
return (data, ctx) => {
|
|
3883
|
+
const hit = cache.get(ctx);
|
|
3884
|
+
if (hit && hit.data === data) return hit.g;
|
|
3885
|
+
const g = fn(data, ctx);
|
|
3886
|
+
cache.set(ctx, {
|
|
3887
|
+
data,
|
|
3888
|
+
g
|
|
3889
|
+
});
|
|
3890
|
+
return g;
|
|
3891
|
+
};
|
|
3892
|
+
}
|
|
3893
|
+
/**
|
|
3894
|
+
* an `editPart` for kinds whose parts only take a colour: `fn` gets the part
|
|
3895
|
+
* id and the tone (a tone name or `#hex`; null removes it)
|
|
3896
|
+
*/
|
|
3897
|
+
function recolor(fn) {
|
|
3898
|
+
return (body, edit, data) => {
|
|
3899
|
+
if (edit.op !== "set" || edit.key !== "color") return null;
|
|
3900
|
+
const out = fn(body, edit.id, edit.value || null, data);
|
|
3901
|
+
return out && {
|
|
3902
|
+
body: out,
|
|
3903
|
+
id: edit.id
|
|
3904
|
+
};
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
/** everything registered through `use` */
|
|
3908
|
+
const registry = {
|
|
3909
|
+
plugins: [],
|
|
3910
|
+
rows: [],
|
|
3911
|
+
frames: /* @__PURE__ */ new Map(),
|
|
3912
|
+
decorations: [],
|
|
3913
|
+
panels: PANELS
|
|
3914
|
+
};
|
|
3915
|
+
const addAll = (list, items) => {
|
|
3916
|
+
for (const i of items ?? []) if (!list.includes(i)) list.push(i);
|
|
3917
|
+
};
|
|
3918
|
+
/** marker names go in front of `none`, so the choices list the drawn ones first */
|
|
3919
|
+
const addMarkers = (list, markers) => {
|
|
3920
|
+
for (const m of markers ?? []) {
|
|
3921
|
+
if (list.includes(m.name)) continue;
|
|
3922
|
+
const none = list.indexOf("none");
|
|
3923
|
+
list.splice(none < 0 ? list.length : none, 0, m.name);
|
|
3924
|
+
}
|
|
3925
|
+
};
|
|
3926
|
+
const none = () => {};
|
|
3927
|
+
const empty = () => "";
|
|
3928
|
+
const zero = () => ({
|
|
3929
|
+
w: 0,
|
|
3930
|
+
h: 0
|
|
3931
|
+
});
|
|
3932
|
+
/** what each kind's functions return instead when they throw */
|
|
3933
|
+
const FALLBACK = {
|
|
3934
|
+
row: {
|
|
3935
|
+
parse: () => null,
|
|
3936
|
+
finish: none,
|
|
3937
|
+
measure: zero,
|
|
3938
|
+
render: empty
|
|
3939
|
+
},
|
|
3940
|
+
frame: {
|
|
3941
|
+
inset: () => ({ top: 0 }),
|
|
3942
|
+
render: empty
|
|
3943
|
+
},
|
|
3944
|
+
decoration: {
|
|
3945
|
+
measure: () => null,
|
|
3946
|
+
render: empty
|
|
3947
|
+
},
|
|
3948
|
+
shape: {
|
|
3949
|
+
path: (b) => rectPath(b.x, b.y, b.w, b.h, 0),
|
|
3950
|
+
size: (w, h) => ({
|
|
3951
|
+
w,
|
|
3952
|
+
h
|
|
3953
|
+
}),
|
|
3954
|
+
detail: () => null,
|
|
3955
|
+
port: (b) => ({
|
|
3956
|
+
x: b.x + b.w / 2,
|
|
3957
|
+
y: b.y + b.h / 2
|
|
3958
|
+
})
|
|
3959
|
+
},
|
|
3960
|
+
marker: {
|
|
3961
|
+
render: empty,
|
|
3962
|
+
inset: () => 0
|
|
3963
|
+
},
|
|
3964
|
+
panel: {
|
|
3965
|
+
measure: zero,
|
|
3966
|
+
render: empty,
|
|
3967
|
+
handles: () => [],
|
|
3968
|
+
parts: () => [],
|
|
3969
|
+
editPart: () => null
|
|
3970
|
+
},
|
|
3971
|
+
hook: {
|
|
3972
|
+
transform: none,
|
|
3973
|
+
afterLayout: none,
|
|
3974
|
+
postRender: (svg) => svg
|
|
3975
|
+
}
|
|
3976
|
+
};
|
|
3977
|
+
/** `k` with each function named in `fb` wrapped by `guard` */
|
|
3978
|
+
function wrap(plugin, what, k, fb) {
|
|
3979
|
+
const out = { ...k };
|
|
3980
|
+
for (const [m, f] of Object.entries(fb)) {
|
|
3981
|
+
const fn = out[m];
|
|
3982
|
+
if (typeof fn === "function") out[m] = guard(plugin, what(m), fn.bind(k), f);
|
|
3983
|
+
}
|
|
3984
|
+
return out;
|
|
3985
|
+
}
|
|
3986
|
+
/** copies of a plugin's parts whose every function reports a throw instead of passing it on */
|
|
3987
|
+
function guarded(p) {
|
|
3988
|
+
const n = p.name;
|
|
3989
|
+
const kinds = (list, what, fb) => (list ?? []).map((k) => wrap(n, (m) => what(k, m), k, fb));
|
|
3990
|
+
return {
|
|
3991
|
+
rows: kinds(p.rows, (r) => `row "${r.kind}"`, FALLBACK.row),
|
|
3992
|
+
frames: kinds(p.frames, (f) => `frame "${f.name}"`, FALLBACK.frame),
|
|
3993
|
+
decorations: kinds(p.decorations, (d) => `decoration "${d.name}"`, FALLBACK.decoration),
|
|
3994
|
+
shapes: kinds(p.shapes, (s) => `shape "${s.name}"`, FALLBACK.shape),
|
|
3995
|
+
markers: kinds(p.markers, (k) => `marker "${k.name}"`, FALLBACK.marker),
|
|
3996
|
+
panels: kinds(p.panels, (pk, m) => `${pk.name} panel ${m === "editPart" ? "edit" : m}`, FALLBACK.panel),
|
|
3997
|
+
statements: (p.statements ?? []).map((st) => ({
|
|
3998
|
+
...st,
|
|
3999
|
+
parse: guard(n, st.keyword, st.parse.bind(st), none, (msg, [, , ctx]) => ctx.warn(msg, "error"))
|
|
4000
|
+
})),
|
|
4001
|
+
hooks: wrap(n, (m) => m, {
|
|
4002
|
+
transform: p.transform,
|
|
4003
|
+
afterLayout: p.afterLayout,
|
|
4004
|
+
postRender: p.postRender
|
|
4005
|
+
}, FALLBACK.hook)
|
|
4006
|
+
};
|
|
4007
|
+
}
|
|
4008
|
+
/** the tables as they were before the first plugin, so `unuse` can rebuild them */
|
|
4009
|
+
let baseline;
|
|
4010
|
+
function snapshot() {
|
|
4011
|
+
const sets = [KNOWN_SETTINGS].map((s) => [s, [...s]]);
|
|
4012
|
+
const maps = [
|
|
4013
|
+
PANELS,
|
|
4014
|
+
STATEMENTS,
|
|
4015
|
+
SHAPE_KINDS,
|
|
4016
|
+
MARKER_KINDS,
|
|
4017
|
+
registry.frames
|
|
4018
|
+
].map((m) => [m, [...m]]);
|
|
4019
|
+
const lists = [
|
|
4020
|
+
TRANSFORMS,
|
|
4021
|
+
AFTER_LAYOUT,
|
|
4022
|
+
POST_RENDER,
|
|
4023
|
+
NODE_PROPS,
|
|
4024
|
+
EDGE_PROPS,
|
|
4025
|
+
GROUP_PROPS,
|
|
4026
|
+
BOOLS,
|
|
4027
|
+
NUMBERS,
|
|
4028
|
+
THEME_NAMES,
|
|
4029
|
+
SHAPES,
|
|
4030
|
+
registry.rows,
|
|
4031
|
+
registry.decorations
|
|
4032
|
+
].map((l) => [l, [...l]]);
|
|
4033
|
+
const enums = Object.entries(ENUMS).map(([k, v]) => [
|
|
4034
|
+
k,
|
|
4035
|
+
v,
|
|
4036
|
+
[...v]
|
|
4037
|
+
]);
|
|
4038
|
+
const themes = { ...THEMES };
|
|
4039
|
+
const aliases = { ...SHAPE_ALIASES };
|
|
4040
|
+
return () => {
|
|
4041
|
+
for (const [s, v] of sets) {
|
|
4042
|
+
s.clear();
|
|
4043
|
+
for (const x of v) s.add(x);
|
|
4044
|
+
}
|
|
4045
|
+
for (const [m, v] of maps) {
|
|
4046
|
+
m.clear();
|
|
4047
|
+
for (const [k, x] of v) m.set(k, x);
|
|
4048
|
+
}
|
|
4049
|
+
for (const [l, v] of lists) l.splice(0, l.length, ...v);
|
|
4050
|
+
for (const k of Object.keys(ENUMS)) delete ENUMS[k];
|
|
4051
|
+
for (const [k, arr, v] of enums) {
|
|
4052
|
+
arr.splice(0, arr.length, ...v);
|
|
4053
|
+
ENUMS[k] = arr;
|
|
4054
|
+
}
|
|
4055
|
+
for (const k of Object.keys(THEMES)) delete THEMES[k];
|
|
4056
|
+
Object.assign(THEMES, themes);
|
|
4057
|
+
for (const k of Object.keys(SHAPE_ALIASES)) delete SHAPE_ALIASES[k];
|
|
4058
|
+
Object.assign(SHAPE_ALIASES, aliases);
|
|
4059
|
+
clearOwners();
|
|
4060
|
+
};
|
|
4061
|
+
}
|
|
4062
|
+
/** `item` replaces the entry with the same name where it was (order matters for rows), or goes last */
|
|
4063
|
+
function place(list, item, same) {
|
|
4064
|
+
const at = list.findIndex(same);
|
|
4065
|
+
if (at >= 0) list[at] = item;
|
|
4066
|
+
else list.push(item);
|
|
4067
|
+
}
|
|
4068
|
+
/** Register plugins. Registering the same plugin name twice is a no-op. */
|
|
4069
|
+
function use(...plugins) {
|
|
4070
|
+
baseline ??= snapshot();
|
|
4071
|
+
for (const p of plugins) {
|
|
4072
|
+
if (registry.plugins.some((q) => q.name === p.name)) continue;
|
|
4073
|
+
registry.plugins.push(p);
|
|
4074
|
+
const w = guarded(p);
|
|
4075
|
+
/** each item, under its name, into `into`: a map, or a function for more work */
|
|
4076
|
+
const own = (what, items, key, into) => {
|
|
4077
|
+
for (const x of items) {
|
|
4078
|
+
const k = key(x);
|
|
4079
|
+
claim(what, k, p.name);
|
|
4080
|
+
if (typeof into === "function") into(x, k);
|
|
4081
|
+
else into.set(k, x);
|
|
4082
|
+
}
|
|
4083
|
+
};
|
|
4084
|
+
for (const t of p.themes ?? []) registerTheme(t);
|
|
4085
|
+
own("row", w.rows, (r) => r.kind, (r, k) => place(registry.rows, r, (x) => x.kind === k));
|
|
4086
|
+
own("decoration", w.decorations, (dec) => dec.name, (dec, k) => {
|
|
4087
|
+
place(registry.decorations, dec, (x) => x.name === k);
|
|
4088
|
+
KNOWN_SETTINGS.add(dec.setting);
|
|
4089
|
+
});
|
|
4090
|
+
own("frame", w.frames, (f) => f.name, registry.frames);
|
|
4091
|
+
own("panel", w.panels, (pk) => pk.name, PANELS);
|
|
4092
|
+
own("statement", w.statements, (st) => st.keyword, STATEMENTS);
|
|
4093
|
+
own("marker", w.markers, (mk) => mk.name, MARKER_KINDS);
|
|
4094
|
+
own("shape", w.shapes, (sh) => sh.name, (sh, k) => {
|
|
4095
|
+
SHAPE_KINDS.set(k, sh);
|
|
4096
|
+
if (!SHAPES.includes(k)) SHAPES.push(k);
|
|
4097
|
+
for (const a of sh.aliases ?? []) SHAPE_ALIASES[a] = k;
|
|
4098
|
+
});
|
|
4099
|
+
addAll(ENUMS.shape ??= [], [...p.shapes?.map((x) => x.name) ?? [], ...p.shapes?.flatMap((x) => x.aliases ?? []) ?? []]);
|
|
4100
|
+
addMarkers(ENUMS.head ??= [], p.markers);
|
|
4101
|
+
if (ENUMS.tail !== ENUMS.head) addMarkers(ENUMS.tail ??= [], p.markers);
|
|
4102
|
+
const { transform, afterLayout, postRender } = w.hooks;
|
|
4103
|
+
if (transform) TRANSFORMS.push(transform);
|
|
4104
|
+
if (afterLayout) AFTER_LAYOUT.push(afterLayout);
|
|
4105
|
+
if (postRender) POST_RENDER.push(postRender);
|
|
4106
|
+
for (const s of p.settings ?? []) KNOWN_SETTINGS.add(s);
|
|
4107
|
+
addAll(NODE_PROPS, p.props?.node);
|
|
4108
|
+
addAll(EDGE_PROPS, p.props?.edge);
|
|
4109
|
+
addAll(GROUP_PROPS, p.props?.group);
|
|
4110
|
+
for (const [k, v] of Object.entries(p.enums ?? {})) addAll(ENUMS[k] ??= [], v);
|
|
4111
|
+
addAll(BOOLS, p.bools);
|
|
4112
|
+
addAll(NUMBERS, p.numbers);
|
|
4113
|
+
}
|
|
4114
|
+
if (registry.frames.size) addAll(ENUMS.frame ??= [], [...registry.frames.keys()]);
|
|
4115
|
+
}
|
|
4116
|
+
/**
|
|
4117
|
+
* Unregister a plugin by name: every table is rebuilt from the state before
|
|
4118
|
+
* the first `use` and the other plugins are registered again, in order.
|
|
4119
|
+
* Returns false when no plugin has that name.
|
|
4120
|
+
*/
|
|
4121
|
+
function unuse(name) {
|
|
4122
|
+
if (!baseline || !registry.plugins.some((p) => p.name === name)) return false;
|
|
4123
|
+
const keep = registry.plugins.filter((p) => p.name !== name);
|
|
4124
|
+
registry.plugins.length = 0;
|
|
4125
|
+
baseline();
|
|
4126
|
+
use(...keep);
|
|
4127
|
+
return true;
|
|
4128
|
+
}
|
|
4129
|
+
// src/core/measure.ts
|
|
4130
|
+
const NARROW = new Set("iljtfr.,;:'!|()[]{} \"`".split(""));
|
|
4131
|
+
const WIDE = new Set("mwMW@%".split(""));
|
|
4132
|
+
/** Font-independent width estimate, tuned for Inter / system UI fonts. */
|
|
4133
|
+
const approxMeasure = (text, fontSize, weight, mono) => {
|
|
4134
|
+
if (mono) return [...text].length * fontSize * .6;
|
|
4135
|
+
let w = 0;
|
|
4136
|
+
for (const ch of text) {
|
|
4137
|
+
const cp = ch.codePointAt(0);
|
|
4138
|
+
if (cp > 8192) w += cp > 126976 ? 1.2 : 1;
|
|
4139
|
+
else if (NARROW.has(ch)) w += .3;
|
|
4140
|
+
else if (WIDE.has(ch)) w += .86;
|
|
4141
|
+
else if (ch >= "A" && ch <= "Z") w += .66;
|
|
4142
|
+
else if (ch >= "0" && ch <= "9") w += .58;
|
|
4143
|
+
else w += .54;
|
|
4144
|
+
}
|
|
4145
|
+
return w * fontSize * (weight >= 600 ? 1.05 : 1);
|
|
4146
|
+
};
|
|
4147
|
+
const MONO_STACK = "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
|
|
4148
|
+
let canvasCtx;
|
|
4149
|
+
/** Accurate measurement using a canvas when running in a browser. */
|
|
4150
|
+
function canvasMeasure(fontFamily) {
|
|
4151
|
+
return (text, fontSize, weight, mono) => {
|
|
4152
|
+
if (canvasCtx === void 0) try {
|
|
4153
|
+
canvasCtx = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : null;
|
|
4154
|
+
} catch {
|
|
4155
|
+
canvasCtx = null;
|
|
4156
|
+
}
|
|
4157
|
+
if (!canvasCtx) return approxMeasure(text, fontSize, weight, mono);
|
|
4158
|
+
const family = mono && !/mono|courier|consolas|menlo/i.test(fontFamily) ? MONO_STACK : fontFamily;
|
|
4159
|
+
canvasCtx.font = `${weight} ${fontSize}px ${family}`;
|
|
4160
|
+
return canvasCtx.measureText(text).width;
|
|
4161
|
+
};
|
|
4162
|
+
}
|
|
4163
|
+
// src/plugin.ts
|
|
4164
|
+
/** `p` as it is, typed: no annotation needed for the kinds inside */
|
|
4165
|
+
const definePlugin = (p) => p;
|
|
4166
|
+
export { edgeWidthOf as $, table as $t, ENUMS as A, tspans as At, normalizeShape as B, createTheme as Bt, setLineProp as C, segsW as Ct, BOOLS as D, sumW as Dt, unquote as E, subInk as Et, PLACEMENT_PROPS as F, THEMES as Ft, sizeForShape as G, isSafeCss as Gt, rectPath as H, fontStack as Ht, lint as I, THEME_KEYS as It, POST_RENDER as J, ramp as Jt, textOffset as K, isSafeLink as Kt, placementOf as L, THEME_NAMES as Lt, NODE_PROPS as M, MONO as Mt, NUMBERS as N, RAMPS as Nt, COLORS as O, textEl as Ot, PANEL_PROPS as P, SANS as Pt, edgeFont as Q, collecting as Qt, propLine as R, TONES as Rt, serializeProps as S, richW as St, splitTrailingProps as T, splitWords as Tt, shapeDetail as U, getTheme as Ut, portOn as V, cssHex as Vt, shapePath as W, isColor as Wt, routeEdges as X, resolveColor as Xt, renderHead as Y, registerTheme as Yt, dashFor as Z, themeNames as Zt, onlyColor as _, parseInline as _t, perPass as a, resolveGroupStyle as at, propKey as b, renderSegs as bt, unuse as c, size as ct, PANELS as d, SPAN_KEYS as dt, effectiveProps as et, SETTING_VALUES as f, esc as ft, lex as g, nodeInk as gt, joinPath as h, lineHeight as ht, optionKind as i, posNum as it, GROUP_PROPS as j, COLOR_TOKEN as jt, EDGE_PROPS as k, textLines as kt, use as l, textFont as lt, isYuhwaPath as m, inkColor as mt, approxMeasure as n, num as nt, recolor as o, resolveNodeStyle as ot, blockBraces as p, f$1 as pt, AFTER_LAYOUT as q, isToneOrHex as qt, canvasMeasure as r, panelTheme as rt, registry as s, resolvePanelStyle as st, definePlugin as t, lineStyle as tt, KNOWN_SETTINGS as u, withEdgeInk as ut, parse as v, parseSpanStyle as vt, setTrailingProp as w, spanKey as wt, quote as x, richText as xt, parseProps as y, plain as yt, SHAPES as z, bool as zt };
|