@leo-studio/leo-markup 0.2.0 → 0.4.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/README.md +20 -0
- package/dist/fence.d.ts +6 -1
- package/dist/fence.d.ts.map +1 -1
- package/dist/fence.js +27 -4
- package/dist/fence.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/infra/index.d.ts +2 -0
- package/dist/infra/index.d.ts.map +1 -0
- package/dist/infra/index.js +2 -0
- package/dist/infra/index.js.map +1 -0
- package/dist/infra/infra-diagram.d.ts +150 -0
- package/dist/infra/infra-diagram.d.ts.map +1 -0
- package/dist/infra/infra-diagram.js +1373 -0
- package/dist/infra/infra-diagram.js.map +1 -0
- package/dist/infra/raster.d.ts +13 -0
- package/dist/infra/raster.d.ts.map +1 -0
- package/dist/infra/raster.js +46 -0
- package/dist/infra/raster.js.map +1 -0
- package/dist/infra/standalone-style.d.ts +5 -0
- package/dist/infra/standalone-style.d.ts.map +1 -0
- package/dist/infra/standalone-style.js +101 -0
- package/dist/infra/standalone-style.js.map +1 -0
- package/dist/product-spec.d.ts.map +1 -1
- package/dist/product-spec.js +8 -0
- package/dist/product-spec.js.map +1 -1
- package/dist/references.d.ts +74 -0
- package/dist/references.d.ts.map +1 -0
- package/dist/references.js +132 -0
- package/dist/references.js.map +1 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +28 -3
- package/dist/registry.js.map +1 -1
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +4 -3
- package/dist/scanner.js.map +1 -1
- package/dist/schema/vocabulary.json +16 -5
- package/dist/vocabulary.d.ts +18 -4
- package/dist/vocabulary.d.ts.map +1 -1
- package/dist/vocabulary.js +78 -14
- package/dist/vocabulary.js.map +1 -1
- package/package.json +30 -5
|
@@ -0,0 +1,1373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Infrastructure diagrams from a Mermaid-style source.
|
|
3
|
+
*
|
|
4
|
+
* The author declares where things live (hosts, groups, and components on a
|
|
5
|
+
* column grid) and what talks to what (links). Placement follows the grid;
|
|
6
|
+
* width and gaps are minimums, expanded when routing or annotations need room.
|
|
7
|
+
* Links are routed orthogonally around components and titles, crossings get a
|
|
8
|
+
* halo, and every link gets a numbered badge. The output is a static SVG that
|
|
9
|
+
* the host-provided `.infra` styles theme.
|
|
10
|
+
*
|
|
11
|
+
* infra badges letters
|
|
12
|
+
* title "Inside one host"
|
|
13
|
+
* host app "App host" "always on" cols 2
|
|
14
|
+
* api[API | http · auth]:::wf
|
|
15
|
+
* db[Database | rows]:::cap
|
|
16
|
+
* end
|
|
17
|
+
* gh[GitHub | events]:::ext
|
|
18
|
+
* api -->|SQL| db
|
|
19
|
+
* api & db -->|HTTPS| gh :::ext
|
|
20
|
+
*
|
|
21
|
+
* Statements: `infra` header options (`width N`, `gap N`, `badges letters|numbers`),
|
|
22
|
+
* `title "…"`, `desc "…"`, containers `host id "Title" "sub"`, `group id "label"`,
|
|
23
|
+
* and frameless `grid`, each closed by `end`; components `id[Name | sub]:::plane`;
|
|
24
|
+
* `.` for an empty cell; links `a & b -->|label| c :::plane` (`<->` draws arrows at both ends,
|
|
25
|
+
* `---` draws an unlabelled bus line). Options: `cols N`, `span N`, `gap N`, `stack`, `bus`,
|
|
26
|
+
* `bus-right`, `h N`. `%%` starts a comment.
|
|
27
|
+
*/
|
|
28
|
+
import { standaloneStyle } from './standalone-style.js';
|
|
29
|
+
const planes = ['wf', 'cap', 'ses', 'pol', 'ext'];
|
|
30
|
+
const compHeight = 44;
|
|
31
|
+
const clearance = 24;
|
|
32
|
+
const canvasMargin = 8;
|
|
33
|
+
const bendCost = 80;
|
|
34
|
+
const crossCost = 160;
|
|
35
|
+
const frameCost = 160;
|
|
36
|
+
const alongBorderFactor = 2;
|
|
37
|
+
const portCostFactor = 0.2;
|
|
38
|
+
const badgeRadius = 9;
|
|
39
|
+
const laneSpacing = 24;
|
|
40
|
+
const terminalLength = 32;
|
|
41
|
+
const annotationPadding = 8;
|
|
42
|
+
class LayoutSpaceError extends Error {
|
|
43
|
+
}
|
|
44
|
+
const labelCharWidth = 5.9;
|
|
45
|
+
const labelSideOffset = 25;
|
|
46
|
+
const labelAbovePath = 35;
|
|
47
|
+
function container(kind, id) {
|
|
48
|
+
return {
|
|
49
|
+
kind,
|
|
50
|
+
id,
|
|
51
|
+
title: '',
|
|
52
|
+
sub: '',
|
|
53
|
+
plane: 'ses',
|
|
54
|
+
cols: 1,
|
|
55
|
+
span: 1,
|
|
56
|
+
gap: 16,
|
|
57
|
+
stack: false,
|
|
58
|
+
bus: '',
|
|
59
|
+
children: [],
|
|
60
|
+
rect: { x: 0, y: 0, w: 0, h: 0 },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Split on whitespace, keeping "quoted strings" whole (quotes removed). */
|
|
64
|
+
function tokenize(line) {
|
|
65
|
+
return [...line.matchAll(/"([^"]*)"|(\S+)/g)].map((m) => m[1] ?? m[2] ?? '');
|
|
66
|
+
}
|
|
67
|
+
function parsePlane(value, fail) {
|
|
68
|
+
if (!planes.includes(value)) {
|
|
69
|
+
fail(`unknown plane "${value}" (expected ${planes.join(', ')})`);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
function parseOptions(tokens, fail) {
|
|
74
|
+
const options = {};
|
|
75
|
+
const number = (i) => {
|
|
76
|
+
const value = Number(tokens[i + 1]);
|
|
77
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
78
|
+
fail(`"${tokens[i]}" needs a positive number`);
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
83
|
+
const token = tokens[i] ?? '';
|
|
84
|
+
if (token.startsWith(':::')) {
|
|
85
|
+
options.plane = parsePlane(token.slice(3), fail);
|
|
86
|
+
}
|
|
87
|
+
else if (token === 'stack') {
|
|
88
|
+
options.stack = true;
|
|
89
|
+
}
|
|
90
|
+
else if (token === 'bus') {
|
|
91
|
+
options.bus = 'left';
|
|
92
|
+
}
|
|
93
|
+
else if (token === 'bus-right') {
|
|
94
|
+
options.bus = 'right';
|
|
95
|
+
}
|
|
96
|
+
else if (token === 'badges') {
|
|
97
|
+
const value = tokens[++i];
|
|
98
|
+
if (value !== 'numbers' && value !== 'letters') {
|
|
99
|
+
fail('"badges" takes numbers or letters');
|
|
100
|
+
}
|
|
101
|
+
options.badges = value;
|
|
102
|
+
}
|
|
103
|
+
else if (token === 'cols' ||
|
|
104
|
+
token === 'span' ||
|
|
105
|
+
token === 'gap' ||
|
|
106
|
+
token === 'h' ||
|
|
107
|
+
token === 'width') {
|
|
108
|
+
options[token] = number(i);
|
|
109
|
+
i++;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
fail(`unknown option "${token}"`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return options;
|
|
116
|
+
}
|
|
117
|
+
function applyContainerOptions(node, options) {
|
|
118
|
+
node.cols = options.cols ?? node.cols;
|
|
119
|
+
node.span = options.span ?? node.span;
|
|
120
|
+
node.gap = options.gap ?? node.gap;
|
|
121
|
+
node.stack = options.stack ?? node.stack;
|
|
122
|
+
node.bus = options.bus ?? node.bus;
|
|
123
|
+
node.plane = options.plane ?? node.plane;
|
|
124
|
+
}
|
|
125
|
+
export function parseInfra(source) {
|
|
126
|
+
const root = container('grid', '');
|
|
127
|
+
root.gap = 40;
|
|
128
|
+
const diagram = {
|
|
129
|
+
title: '',
|
|
130
|
+
desc: '',
|
|
131
|
+
width: 1200,
|
|
132
|
+
badges: 'numbers',
|
|
133
|
+
root,
|
|
134
|
+
links: [],
|
|
135
|
+
};
|
|
136
|
+
const open = [root];
|
|
137
|
+
const openingLines = new Map();
|
|
138
|
+
const linkLines = [];
|
|
139
|
+
const ids = new Set();
|
|
140
|
+
let headerSeen = false;
|
|
141
|
+
source.split('\n').forEach((raw, index) => {
|
|
142
|
+
const line = raw.replace(/%%.*$/, '').trim();
|
|
143
|
+
if (!line) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const fail = (msg) => {
|
|
147
|
+
throw new Error(`infra line ${index + 1}: ${msg}: ${line}`);
|
|
148
|
+
};
|
|
149
|
+
const parent = open[open.length - 1];
|
|
150
|
+
const claim = (id) => {
|
|
151
|
+
if (ids.has(id)) {
|
|
152
|
+
fail(`duplicate id "${id}"`);
|
|
153
|
+
}
|
|
154
|
+
ids.add(id);
|
|
155
|
+
};
|
|
156
|
+
if (!headerSeen) {
|
|
157
|
+
const [head, ...rest] = tokenize(line);
|
|
158
|
+
if (head !== 'infra') {
|
|
159
|
+
fail('a diagram starts with "infra"');
|
|
160
|
+
}
|
|
161
|
+
headerSeen = true;
|
|
162
|
+
const options = parseOptions(rest, fail);
|
|
163
|
+
diagram.width = options.width ?? diagram.width;
|
|
164
|
+
diagram.badges = options.badges ?? diagram.badges;
|
|
165
|
+
applyContainerOptions(root, options);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (line === 'end') {
|
|
169
|
+
if (open.length === 1) {
|
|
170
|
+
fail('"end" without an open host, group, or grid');
|
|
171
|
+
}
|
|
172
|
+
open.pop();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const link = /^(.+?)\s*(<->|-->|---)\s*(?:\|([^|]*)\|)?\s*(.+?)(?:\s+:::(\w+))?$/.exec(line);
|
|
176
|
+
if (link && !/^[A-Za-z_][\w-]*\[/.test(line)) {
|
|
177
|
+
const split = (side) => (side ?? '').split('&').map((id) => id.trim());
|
|
178
|
+
diagram.links.push({
|
|
179
|
+
sources: split(link[1]),
|
|
180
|
+
targets: split(link[4]),
|
|
181
|
+
label: (link[3] ?? '').trim(),
|
|
182
|
+
plane: link[5] ? parsePlane(link[5], fail) : null,
|
|
183
|
+
arrow: link[2] !== '---',
|
|
184
|
+
...(link[2] === '<->' ? { bidirectional: true } : {}),
|
|
185
|
+
});
|
|
186
|
+
linkLines.push(index + 1);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const comp = /^([A-Za-z_][\w-]*)\[([^\]]*)\](.*)$/.exec(line);
|
|
190
|
+
if (comp) {
|
|
191
|
+
const [, id = '', text = '', rest = ''] = comp;
|
|
192
|
+
claim(id);
|
|
193
|
+
const [name = '', sub = ''] = text.split('|').map((part) => part.trim());
|
|
194
|
+
const options = parseOptions(tokenize(rest.trim()), fail);
|
|
195
|
+
parent.children.push({
|
|
196
|
+
kind: 'comp',
|
|
197
|
+
id,
|
|
198
|
+
name,
|
|
199
|
+
sub,
|
|
200
|
+
plane: options.plane ?? 'ses',
|
|
201
|
+
span: options.span ?? 1,
|
|
202
|
+
stack: options.stack ?? false,
|
|
203
|
+
h: options.h ?? compHeight,
|
|
204
|
+
rect: { x: 0, y: 0, w: 0, h: 0 },
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const [head = '', ...rest] = tokenize(line);
|
|
209
|
+
if (head === '.') {
|
|
210
|
+
parent.children.push({
|
|
211
|
+
kind: 'empty',
|
|
212
|
+
span: parseOptions(rest, fail).span ?? 1,
|
|
213
|
+
rect: { x: 0, y: 0, w: 0, h: 0 },
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
else if (head === 'title' || head === 'desc') {
|
|
217
|
+
diagram[head] = rest.join(' ');
|
|
218
|
+
}
|
|
219
|
+
else if (head === 'host' || head === 'group') {
|
|
220
|
+
const [id = '', title = '', ...more] = rest;
|
|
221
|
+
if (!/^[A-Za-z_][\w-]*$/.test(id)) {
|
|
222
|
+
fail(`${head} needs an id`);
|
|
223
|
+
}
|
|
224
|
+
claim(id);
|
|
225
|
+
const node = container(head, id);
|
|
226
|
+
node.title = title;
|
|
227
|
+
const sub = head === 'host' && more[0] !== undefined && !isOption(more[0]);
|
|
228
|
+
node.sub = sub ? (more.shift() ?? '') : '';
|
|
229
|
+
applyContainerOptions(node, parseOptions(more, fail));
|
|
230
|
+
parent.children.push(node);
|
|
231
|
+
open.push(node);
|
|
232
|
+
openingLines.set(node, index + 1);
|
|
233
|
+
}
|
|
234
|
+
else if (head === 'grid') {
|
|
235
|
+
const node = container('grid', '');
|
|
236
|
+
applyContainerOptions(node, parseOptions(rest, fail));
|
|
237
|
+
parent.children.push(node);
|
|
238
|
+
open.push(node);
|
|
239
|
+
openingLines.set(node, index + 1);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
fail('unrecognised statement');
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
if (!headerSeen) {
|
|
246
|
+
throw new Error('infra line 1: empty diagram');
|
|
247
|
+
}
|
|
248
|
+
if (open.length > 1) {
|
|
249
|
+
const node = open[open.length - 1];
|
|
250
|
+
throw new Error(`infra line ${openingLines.get(node)}: "${node.id || 'grid'}" is missing "end"`);
|
|
251
|
+
}
|
|
252
|
+
for (const [index, link] of diagram.links.entries()) {
|
|
253
|
+
for (const id of [...link.sources, ...link.targets]) {
|
|
254
|
+
if (!ids.has(id)) {
|
|
255
|
+
throw new Error(`infra line ${linkLines[index]}: link references unknown id "${id}"`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return diagram;
|
|
260
|
+
}
|
|
261
|
+
function isOption(token) {
|
|
262
|
+
return (token.startsWith(':::') ||
|
|
263
|
+
['cols', 'span', 'gap', 'h', 'stack', 'bus', 'bus-right'].includes(token));
|
|
264
|
+
}
|
|
265
|
+
// ── layout ────────────────────────────────────────────────────────────────
|
|
266
|
+
function padding(node, isRoot) {
|
|
267
|
+
if (isRoot) {
|
|
268
|
+
return { x: 40, top: 40, bottom: 40 };
|
|
269
|
+
}
|
|
270
|
+
if (node.kind === 'host') {
|
|
271
|
+
return { x: 40, top: 80, bottom: 40 };
|
|
272
|
+
}
|
|
273
|
+
if (node.kind === 'group') {
|
|
274
|
+
return { x: 32, top: 48, bottom: 32 };
|
|
275
|
+
}
|
|
276
|
+
return { x: 0, top: 0, bottom: 0 };
|
|
277
|
+
}
|
|
278
|
+
function half(value) {
|
|
279
|
+
return Math.round(value * 2) / 2;
|
|
280
|
+
}
|
|
281
|
+
function layout(node, x, y, w, isRoot = false) {
|
|
282
|
+
const pad = padding(node, isRoot);
|
|
283
|
+
const innerW = w - 2 * pad.x;
|
|
284
|
+
const colW = (innerW - node.gap * (node.cols - 1)) / node.cols;
|
|
285
|
+
const rows = [];
|
|
286
|
+
let used = node.cols;
|
|
287
|
+
for (const child of node.children) {
|
|
288
|
+
const span = Math.min(child.span, node.cols);
|
|
289
|
+
if (used + span > node.cols) {
|
|
290
|
+
rows.push([]);
|
|
291
|
+
used = 0;
|
|
292
|
+
}
|
|
293
|
+
rows[rows.length - 1]?.push(child);
|
|
294
|
+
used += span;
|
|
295
|
+
}
|
|
296
|
+
let rowY = y + pad.top;
|
|
297
|
+
for (const row of rows) {
|
|
298
|
+
let cx = x + pad.x;
|
|
299
|
+
let rowH = 0;
|
|
300
|
+
for (const child of row) {
|
|
301
|
+
const span = Math.min(child.span, node.cols);
|
|
302
|
+
const cw = colW * span + node.gap * (span - 1);
|
|
303
|
+
const h = child.kind === 'comp'
|
|
304
|
+
? child.h
|
|
305
|
+
: child.kind === 'empty'
|
|
306
|
+
? 0
|
|
307
|
+
: layout(child, cx, rowY, cw);
|
|
308
|
+
child.rect = { x: half(cx), y: half(rowY), w: half(cw), h };
|
|
309
|
+
rowH = Math.max(rowH, h);
|
|
310
|
+
cx += cw + node.gap;
|
|
311
|
+
}
|
|
312
|
+
for (const child of row) {
|
|
313
|
+
if (child.kind !== 'comp' && child.kind !== 'empty') {
|
|
314
|
+
child.rect.h = rowH;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
rowY += rowH + node.gap;
|
|
318
|
+
}
|
|
319
|
+
const height = (rows.length ? rowY - node.gap : rowY) - y + pad.bottom;
|
|
320
|
+
node.rect = { x, y, w, h: height };
|
|
321
|
+
return height;
|
|
322
|
+
}
|
|
323
|
+
function textWidth(text, size, factor) {
|
|
324
|
+
return text.length * size * factor;
|
|
325
|
+
}
|
|
326
|
+
function titleRect(node) {
|
|
327
|
+
if (node.kind === 'host') {
|
|
328
|
+
const w = Math.max(textWidth(node.title, 14, 0.62), textWidth(node.sub, 9.5, 0.68));
|
|
329
|
+
return { x: node.rect.x, y: node.rect.y, w: w + 28, h: 48 };
|
|
330
|
+
}
|
|
331
|
+
if (node.kind === 'group' && node.title) {
|
|
332
|
+
return { x: node.rect.x, y: node.rect.y, w: textWidth(node.title, 9.5, 0.68) + 24, h: 22 };
|
|
333
|
+
}
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
function buildScene(diagram) {
|
|
337
|
+
const height = layout(diagram.root, 0, 0, diagram.width, true);
|
|
338
|
+
const scene = {
|
|
339
|
+
diagram,
|
|
340
|
+
height,
|
|
341
|
+
comps: [],
|
|
342
|
+
containers: [],
|
|
343
|
+
titles: [],
|
|
344
|
+
byId: new Map(),
|
|
345
|
+
ancestors: new Map(),
|
|
346
|
+
};
|
|
347
|
+
const walk = (node, chain) => {
|
|
348
|
+
if (node.kind === 'empty') {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (node.kind === 'comp') {
|
|
352
|
+
const fit = node.rect.w - 16;
|
|
353
|
+
if (textWidth(node.name, 12.5, 0.52) > fit || textWidth(node.sub, 9.5, 0.6) > fit) {
|
|
354
|
+
throw new Error(`infra: "${node.name}" does not fit in ${Math.round(node.rect.w)}px`);
|
|
355
|
+
}
|
|
356
|
+
scene.comps.push(node);
|
|
357
|
+
scene.byId.set(node.id, node);
|
|
358
|
+
scene.ancestors.set(node.id, chain);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (node.kind !== 'grid') {
|
|
362
|
+
scene.containers.push(node);
|
|
363
|
+
scene.byId.set(node.id, node);
|
|
364
|
+
scene.ancestors.set(node.id, chain);
|
|
365
|
+
const title = titleRect(node);
|
|
366
|
+
if (title) {
|
|
367
|
+
scene.titles.push(title);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const next = node.kind === 'grid' ? chain : [...chain, node];
|
|
371
|
+
for (const child of node.children) {
|
|
372
|
+
walk(child, next);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
for (const child of diagram.root.children) {
|
|
376
|
+
walk(child, []);
|
|
377
|
+
}
|
|
378
|
+
return scene;
|
|
379
|
+
}
|
|
380
|
+
// ── routing ───────────────────────────────────────────────────────────────
|
|
381
|
+
const dirs = [
|
|
382
|
+
[1, 0],
|
|
383
|
+
[-1, 0],
|
|
384
|
+
[0, 1],
|
|
385
|
+
[0, -1],
|
|
386
|
+
];
|
|
387
|
+
function inflate(r, by) {
|
|
388
|
+
return { x: r.x - by, y: r.y - by, w: r.w + 2 * by, h: r.h + 2 * by };
|
|
389
|
+
}
|
|
390
|
+
function strictlyInside(r, x, y) {
|
|
391
|
+
return x > r.x + 0.01 && x < r.x + r.w - 0.01 && y > r.y + 0.01 && y < r.y + r.h - 0.01;
|
|
392
|
+
}
|
|
393
|
+
function inside(r, x, y) {
|
|
394
|
+
return x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h;
|
|
395
|
+
}
|
|
396
|
+
function uniqueSorted(values) {
|
|
397
|
+
const sorted = values.map((v) => Math.round(v * 2) / 2).sort((a, b) => a - b);
|
|
398
|
+
return sorted.filter((v, i) => i === 0 || v - sorted[i - 1] > 1.5);
|
|
399
|
+
}
|
|
400
|
+
/** Grid lines: every edge and centre, plus lanes through each gutter. */
|
|
401
|
+
function gridLines(scene, axis) {
|
|
402
|
+
const values = [];
|
|
403
|
+
const extent = axis === 'x' ? scene.diagram.width : scene.height;
|
|
404
|
+
const add = (r) => {
|
|
405
|
+
const start = axis === 'x' ? r.x : r.y;
|
|
406
|
+
const size = axis === 'x' ? r.w : r.h;
|
|
407
|
+
values.push(start, start + size, start + size / 2);
|
|
408
|
+
};
|
|
409
|
+
for (const comp of scene.comps) {
|
|
410
|
+
add(comp.rect);
|
|
411
|
+
add(inflate(comp.rect, clearance));
|
|
412
|
+
}
|
|
413
|
+
for (const node of scene.containers) {
|
|
414
|
+
add(node.rect);
|
|
415
|
+
}
|
|
416
|
+
for (const title of scene.titles) {
|
|
417
|
+
add(title);
|
|
418
|
+
}
|
|
419
|
+
values.push(canvasMargin, extent - canvasMargin);
|
|
420
|
+
const edges = uniqueSorted(values);
|
|
421
|
+
for (let i = 1; i < edges.length; i++) {
|
|
422
|
+
const a = edges[i - 1];
|
|
423
|
+
const b = edges[i];
|
|
424
|
+
// Every gutter gets lanes, so a later link is not pushed into a detour.
|
|
425
|
+
const lanes = Math.min(5, Math.max(1, Math.floor((b - a) / 18)));
|
|
426
|
+
if (b - a > 6) {
|
|
427
|
+
for (let lane = 1; lane <= lanes; lane++) {
|
|
428
|
+
values.push(a + ((b - a) * lane) / (lanes + 1));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return uniqueSorted(values).filter((v) => v >= canvasMargin && v <= extent - canvasMargin);
|
|
433
|
+
}
|
|
434
|
+
class Heap {
|
|
435
|
+
keys = [];
|
|
436
|
+
values = [];
|
|
437
|
+
get size() {
|
|
438
|
+
return this.keys.length;
|
|
439
|
+
}
|
|
440
|
+
push(key, value) {
|
|
441
|
+
const keys = this.keys;
|
|
442
|
+
const values = this.values;
|
|
443
|
+
let i = keys.length;
|
|
444
|
+
keys.push(key);
|
|
445
|
+
values.push(value);
|
|
446
|
+
while (i > 0) {
|
|
447
|
+
const parent = (i - 1) >> 1;
|
|
448
|
+
if (keys[parent] <= key) {
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
keys[i] = keys[parent];
|
|
452
|
+
values[i] = values[parent];
|
|
453
|
+
i = parent;
|
|
454
|
+
}
|
|
455
|
+
keys[i] = key;
|
|
456
|
+
values[i] = value;
|
|
457
|
+
}
|
|
458
|
+
pop() {
|
|
459
|
+
const keys = this.keys;
|
|
460
|
+
const values = this.values;
|
|
461
|
+
const top = values[0];
|
|
462
|
+
const lastKey = keys.pop();
|
|
463
|
+
const lastValue = values.pop();
|
|
464
|
+
if (keys.length) {
|
|
465
|
+
let i = 0;
|
|
466
|
+
for (;;) {
|
|
467
|
+
const left = 2 * i + 1;
|
|
468
|
+
if (left >= keys.length) {
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
const right = left + 1;
|
|
472
|
+
const child = right < keys.length && keys[right] < keys[left]
|
|
473
|
+
? right
|
|
474
|
+
: left;
|
|
475
|
+
if (keys[child] >= lastKey) {
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
keys[i] = keys[child];
|
|
479
|
+
values[i] = values[child];
|
|
480
|
+
i = child;
|
|
481
|
+
}
|
|
482
|
+
keys[i] = lastKey;
|
|
483
|
+
values[i] = lastValue;
|
|
484
|
+
}
|
|
485
|
+
return top;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
function onBorder(r, s) {
|
|
489
|
+
const mx = (s.x1 + s.x2) / 2;
|
|
490
|
+
const my = (s.y1 + s.y2) / 2;
|
|
491
|
+
if (s.x1 === s.x2) {
|
|
492
|
+
return ((Math.abs(mx - r.x) < 1 || Math.abs(mx - r.x - r.w) < 1) && my > r.y && my < r.y + r.h);
|
|
493
|
+
}
|
|
494
|
+
return (Math.abs(my - r.y) < 1 || Math.abs(my - r.y - r.h) < 1) && mx > r.x && mx < r.x + r.w;
|
|
495
|
+
}
|
|
496
|
+
/** Perpendicular segments that touch or cross, ignoring a shared endpoint at `skip`. */
|
|
497
|
+
function meets(a, b, skip = []) {
|
|
498
|
+
const aVertical = a.x1 === a.x2;
|
|
499
|
+
if (aVertical === (b.x1 === b.x2)) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
const v = aVertical ? a : b;
|
|
503
|
+
const h = aVertical ? b : a;
|
|
504
|
+
const x = v.x1;
|
|
505
|
+
const y = h.y1;
|
|
506
|
+
const hit = x >= Math.min(h.x1, h.x2) &&
|
|
507
|
+
x <= Math.max(h.x1, h.x2) &&
|
|
508
|
+
y >= Math.min(v.y1, v.y2) &&
|
|
509
|
+
y <= Math.max(v.y1, v.y2);
|
|
510
|
+
return hit && !skip.some(([sx, sy]) => sx === x && sy === y);
|
|
511
|
+
}
|
|
512
|
+
/** Routed segments indexed by the grid line they run on. */
|
|
513
|
+
class Occupancy {
|
|
514
|
+
all = [];
|
|
515
|
+
vertical = new Map();
|
|
516
|
+
horizontal = new Map();
|
|
517
|
+
add(segments) {
|
|
518
|
+
for (const s of segments) {
|
|
519
|
+
this.all.push(s);
|
|
520
|
+
const isVertical = s.x1 === s.x2;
|
|
521
|
+
const map = isVertical ? this.vertical : this.horizontal;
|
|
522
|
+
const key = Math.round(isVertical ? s.x1 : s.y1);
|
|
523
|
+
map.set(key, [...(map.get(key) ?? []), s]);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
/** True when a segment would run along (within one routing lane of) an existing one. */
|
|
527
|
+
overlaps(x1, y1, x2, y2, spacing = laneSpacing) {
|
|
528
|
+
const isVertical = x1 === x2;
|
|
529
|
+
const map = isVertical ? this.vertical : this.horizontal;
|
|
530
|
+
const at = isVertical ? x1 : y1;
|
|
531
|
+
const [lo, hi] = isVertical
|
|
532
|
+
? [Math.min(y1, y2), Math.max(y1, y2)]
|
|
533
|
+
: [Math.min(x1, x2), Math.max(x1, x2)];
|
|
534
|
+
for (let key = Math.floor(at - spacing); key <= Math.ceil(at + spacing); key++) {
|
|
535
|
+
for (const s of map.get(key) ?? []) {
|
|
536
|
+
const [slo, shi] = isVertical
|
|
537
|
+
? [Math.min(s.y1, s.y2), Math.max(s.y1, s.y2)]
|
|
538
|
+
: [Math.min(s.x1, s.x2), Math.max(s.x1, s.x2)];
|
|
539
|
+
if (Math.abs((isVertical ? s.x1 : s.y1) - at) < spacing &&
|
|
540
|
+
Math.min(hi, shi) - Math.max(lo, slo) > 1) {
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
/** Existing segments perpendicular to travel that pass through (x, y). */
|
|
548
|
+
through(x, y, travellingVertically) {
|
|
549
|
+
const map = travellingVertically ? this.horizontal : this.vertical;
|
|
550
|
+
const key = Math.round(travellingVertically ? y : x);
|
|
551
|
+
let count = 0;
|
|
552
|
+
for (const s of map.get(key) ?? []) {
|
|
553
|
+
if (travellingVertically) {
|
|
554
|
+
if (s.y1 === y && x >= Math.min(s.x1, s.x2) && x <= Math.max(s.x1, s.x2)) {
|
|
555
|
+
count++;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else if (s.x1 === x && y >= Math.min(s.y1, s.y2) && y <= Math.max(s.y1, s.y2)) {
|
|
559
|
+
count++;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return count;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
class Router {
|
|
566
|
+
xs;
|
|
567
|
+
ys;
|
|
568
|
+
hard;
|
|
569
|
+
scene;
|
|
570
|
+
blockedH;
|
|
571
|
+
blockedV;
|
|
572
|
+
occupancy = new Occupancy();
|
|
573
|
+
constructor(scene) {
|
|
574
|
+
this.scene = scene;
|
|
575
|
+
this.xs = gridLines(scene, 'x');
|
|
576
|
+
this.ys = gridLines(scene, 'y');
|
|
577
|
+
this.hard = [...scene.comps.map((c) => inflate(c.rect, clearance)), ...scene.titles];
|
|
578
|
+
const { xs, ys, hard } = this;
|
|
579
|
+
const nx = xs.length;
|
|
580
|
+
this.blockedH = new Uint8Array(nx * ys.length);
|
|
581
|
+
this.blockedV = new Uint8Array(nx * ys.length);
|
|
582
|
+
for (let iy = 0; iy < ys.length; iy++) {
|
|
583
|
+
for (let ix = 0; ix < nx; ix++) {
|
|
584
|
+
const x = xs[ix];
|
|
585
|
+
const y = ys[iy];
|
|
586
|
+
if (ix + 1 < nx) {
|
|
587
|
+
const mx = (x + xs[ix + 1]) / 2;
|
|
588
|
+
this.blockedH[iy * nx + ix] = hard.some((r) => strictlyInside(r, mx, y))
|
|
589
|
+
? 1
|
|
590
|
+
: 0;
|
|
591
|
+
}
|
|
592
|
+
if (iy + 1 < ys.length) {
|
|
593
|
+
const my = (y + ys[iy + 1]) / 2;
|
|
594
|
+
this.blockedV[iy * nx + ix] = hard.some((r) => strictlyInside(r, x, my))
|
|
595
|
+
? 1
|
|
596
|
+
: 0;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
straight(source, target, parallel) {
|
|
602
|
+
const s = source.rect, t = target.rect;
|
|
603
|
+
const ends = [source, target];
|
|
604
|
+
const direct = [];
|
|
605
|
+
const sharedY = [Math.max(s.y, t.y) + 12, Math.min(s.y + s.h, t.y + t.h) - 12];
|
|
606
|
+
const sharedX = [Math.max(s.x, t.x) + 12, Math.min(s.x + s.w, t.x + t.w) - 12];
|
|
607
|
+
if (sharedY[0] <= sharedY[1] &&
|
|
608
|
+
(s.x + s.w < t.x || t.x + t.w < s.x)) {
|
|
609
|
+
const y = half((sharedY[0] + sharedY[1]) / 2);
|
|
610
|
+
direct.push({
|
|
611
|
+
x1: s.x < t.x ? s.x + s.w : s.x,
|
|
612
|
+
y1: y,
|
|
613
|
+
x2: s.x < t.x ? t.x : t.x + t.w,
|
|
614
|
+
y2: y,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
if (sharedX[0] <= sharedX[1] &&
|
|
618
|
+
(s.y + s.h < t.y || t.y + t.h < s.y)) {
|
|
619
|
+
const x = half((sharedX[0] + sharedX[1]) / 2);
|
|
620
|
+
direct.push({
|
|
621
|
+
x1: x,
|
|
622
|
+
y1: s.y < t.y ? s.y + s.h : s.y,
|
|
623
|
+
x2: x,
|
|
624
|
+
y2: s.y < t.y ? t.y : t.y + t.h,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
const directObstacles = [
|
|
628
|
+
...this.scene.comps
|
|
629
|
+
.filter((c) => !ends.includes(c))
|
|
630
|
+
.map((c) => inflate(c.rect, clearance)),
|
|
631
|
+
...this.scene.titles,
|
|
632
|
+
];
|
|
633
|
+
return direct.find((segment) => !directObstacles.some((r) => segmentHitsRect(segment, r)) &&
|
|
634
|
+
!this.overlaps(segment.x1, segment.y1, segment.x2, segment.y2, parallel));
|
|
635
|
+
}
|
|
636
|
+
overlaps(x1, y1, x2, y2, parallel) {
|
|
637
|
+
return (this.occupancy.overlaps(x1, y1, x2, y2) ||
|
|
638
|
+
(parallel?.occupancy.overlaps(x1, y1, x2, y2, parallel.spacing) ?? false));
|
|
639
|
+
}
|
|
640
|
+
route(source, target, parallel) {
|
|
641
|
+
const { xs, ys, scene, occupancy } = this;
|
|
642
|
+
const nx = xs.length;
|
|
643
|
+
// Endpoint components keep only their own outline as an obstacle.
|
|
644
|
+
const ends = [source, target].filter((e) => e.kind === 'comp');
|
|
645
|
+
const endHalos = ends.map((e) => inflate(e.rect, clearance));
|
|
646
|
+
const linkHard = [
|
|
647
|
+
...scene.comps.map((c) => (ends.includes(c) ? c.rect : inflate(c.rect, clearance))),
|
|
648
|
+
...scene.titles,
|
|
649
|
+
];
|
|
650
|
+
const blocked = (x1, y1, x2, y2, base) => {
|
|
651
|
+
const mx = (x1 + x2) / 2;
|
|
652
|
+
const my = (y1 + y2) / 2;
|
|
653
|
+
if (endHalos.some((r) => strictlyInside(r, mx, my))) {
|
|
654
|
+
return linkHard.some((r) => strictlyInside(r, mx, my));
|
|
655
|
+
}
|
|
656
|
+
return base === 1;
|
|
657
|
+
};
|
|
658
|
+
const sourceChain = new Set(scene.ancestors.get(source.id));
|
|
659
|
+
const targetChain = new Set(scene.ancestors.get(target.id));
|
|
660
|
+
const borders = scene.containers.filter((c) => c !== source && c !== target && !sourceChain.has(c) && !targetChain.has(c));
|
|
661
|
+
const alongRects = [...scene.containers.map((c) => c.rect), source.rect, target.rect];
|
|
662
|
+
const s = source.rect;
|
|
663
|
+
const t = target.rect;
|
|
664
|
+
// Reserve direct corridors before routing detours. Frame crossings
|
|
665
|
+
// alone do not justify bends on an otherwise unobstructed connection.
|
|
666
|
+
const straight = this.straight(source, target, parallel);
|
|
667
|
+
if (straight) {
|
|
668
|
+
const points = [
|
|
669
|
+
[straight.x1, straight.y1],
|
|
670
|
+
[straight.x2, straight.y2],
|
|
671
|
+
];
|
|
672
|
+
const crossings = occupancy.all.filter((other) => meets(straight, other, points)).length;
|
|
673
|
+
occupancy.add([straight]);
|
|
674
|
+
parallel?.occupancy.add([straight]);
|
|
675
|
+
return { points, crossings };
|
|
676
|
+
}
|
|
677
|
+
const stateCount = nx * ys.length * 4;
|
|
678
|
+
const best = new Float64Array(stateCount).fill(Number.POSITIVE_INFINITY);
|
|
679
|
+
const parent = new Int32Array(stateCount).fill(-1);
|
|
680
|
+
const heap = new Heap();
|
|
681
|
+
const heuristic = (x, y) => Math.max(0, t.x - x, x - t.x - t.w) + Math.max(0, t.y - y, y - t.y - t.h);
|
|
682
|
+
const sides = [
|
|
683
|
+
{ fixed: 'y', at: s.y, from: s.x, to: s.x + s.w, dir: 3 },
|
|
684
|
+
{ fixed: 'y', at: s.y + s.h, from: s.x, to: s.x + s.w, dir: 2 },
|
|
685
|
+
{ fixed: 'x', at: s.x, from: s.y, to: s.y + s.h, dir: 1 },
|
|
686
|
+
{ fixed: 'x', at: s.x + s.w, from: s.y, to: s.y + s.h, dir: 0 },
|
|
687
|
+
];
|
|
688
|
+
for (const side of sides) {
|
|
689
|
+
const fixedLines = side.fixed === 'x' ? xs : ys;
|
|
690
|
+
const fixedIndex = fixedLines.findIndex((v) => Math.abs(v - side.at) < 0.6);
|
|
691
|
+
if (fixedIndex < 0) {
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
const lines = side.fixed === 'x' ? ys : xs;
|
|
695
|
+
// Components exit near the middle of a side; containers exit facing the target.
|
|
696
|
+
const towards = side.fixed === 'x' ? t.y + t.h / 2 : t.x + t.w / 2;
|
|
697
|
+
const centre = source.kind === 'comp'
|
|
698
|
+
? (side.from + side.to) / 2
|
|
699
|
+
: Math.min(side.to - 8, Math.max(side.from + 8, towards));
|
|
700
|
+
const factor = source.kind === 'comp' ? portCostFactor : portCostFactor / 4;
|
|
701
|
+
lines.forEach((value, i) => {
|
|
702
|
+
if (value < side.from + 8 || value > side.to - 8) {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const [ix, iy] = side.fixed === 'x' ? [fixedIndex, i] : [i, fixedIndex];
|
|
706
|
+
const state = (iy * nx + ix) * 4 + side.dir;
|
|
707
|
+
const g = Math.abs(value - centre) * factor;
|
|
708
|
+
if (g < best[state]) {
|
|
709
|
+
best[state] = g;
|
|
710
|
+
heap.push(g + heuristic(xs[ix], ys[iy]), state);
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
let goal = -1;
|
|
715
|
+
while (heap.size) {
|
|
716
|
+
const state = heap.pop();
|
|
717
|
+
const dir = state & 3;
|
|
718
|
+
const cell = state >> 2;
|
|
719
|
+
const ix = cell % nx;
|
|
720
|
+
const iy = (cell - ix) / nx;
|
|
721
|
+
const x = xs[ix];
|
|
722
|
+
const y = ys[iy];
|
|
723
|
+
const g = best[state];
|
|
724
|
+
if (parent[state] !== -1 && arrives(t, x, y, dir)) {
|
|
725
|
+
goal = state;
|
|
726
|
+
break;
|
|
727
|
+
}
|
|
728
|
+
for (let next = 0; next < 4; next++) {
|
|
729
|
+
if ((dir ^ next) === 1) {
|
|
730
|
+
continue; // no reversing
|
|
731
|
+
}
|
|
732
|
+
const [dx, dy] = dirs[next];
|
|
733
|
+
const jx = ix + dx;
|
|
734
|
+
const jy = iy + dy;
|
|
735
|
+
if (jx < 0 || jy < 0 || jx >= nx || jy >= ys.length) {
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
const x2 = xs[jx];
|
|
739
|
+
const y2 = ys[jy];
|
|
740
|
+
const base = dy === 0
|
|
741
|
+
? this.blockedH[iy * nx + Math.min(ix, jx)]
|
|
742
|
+
: this.blockedV[Math.min(iy, jy) * nx + ix];
|
|
743
|
+
if (blocked(x, y, x2, y2, base) || this.overlaps(x, y, x2, y2, parallel)) {
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
const length = Math.abs(x2 - x) + Math.abs(y2 - y);
|
|
747
|
+
let cost = length;
|
|
748
|
+
const seg = { x1: x, y1: y, x2, y2 };
|
|
749
|
+
if (alongRects.some((r) => onBorder(r, seg))) {
|
|
750
|
+
cost += length * alongBorderFactor;
|
|
751
|
+
}
|
|
752
|
+
if (next !== dir) {
|
|
753
|
+
// Leave a straight approach for the arrow and a clear departure.
|
|
754
|
+
const distance = (r) => Math.max(0, r.x - x, x - r.x - r.w) + Math.max(0, r.y - y, y - r.y - r.h);
|
|
755
|
+
if (distance(s) < terminalLength || distance(t) < terminalLength) {
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
cost += bendCost;
|
|
759
|
+
}
|
|
760
|
+
for (const border of borders) {
|
|
761
|
+
if (inside(border.rect, x, y) !== inside(border.rect, x2, y2)) {
|
|
762
|
+
cost += frameCost;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
if (inside(s, x2, y2) && !inside(s, x, y)) {
|
|
766
|
+
cost += frameCost;
|
|
767
|
+
}
|
|
768
|
+
cost += occupancy.through(x2, y2, dx === 0) * crossCost;
|
|
769
|
+
const nextState = (jy * nx + jx) * 4 + next;
|
|
770
|
+
const ng = g + cost;
|
|
771
|
+
if (ng < best[nextState]) {
|
|
772
|
+
best[nextState] = ng;
|
|
773
|
+
parent[nextState] = state;
|
|
774
|
+
heap.push(ng + heuristic(x2, y2), nextState);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (goal < 0) {
|
|
779
|
+
throw new LayoutSpaceError(`infra: no route from "${source.id}" to "${target.id}"`);
|
|
780
|
+
}
|
|
781
|
+
const points = [];
|
|
782
|
+
for (let state = goal; state !== -1; state = parent[state]) {
|
|
783
|
+
const cell = state >> 2;
|
|
784
|
+
const ix = cell % nx;
|
|
785
|
+
points.unshift([xs[ix], ys[(cell - ix) / nx]]);
|
|
786
|
+
}
|
|
787
|
+
const simple = points.filter((p, i) => {
|
|
788
|
+
const prev = points[i - 1];
|
|
789
|
+
const next = points[i + 1];
|
|
790
|
+
if (!prev || !next) {
|
|
791
|
+
return true;
|
|
792
|
+
}
|
|
793
|
+
return !((prev[0] === p[0] && p[0] === next[0]) ||
|
|
794
|
+
(prev[1] === p[1] && p[1] === next[1]));
|
|
795
|
+
});
|
|
796
|
+
const segments = toSegments(simple);
|
|
797
|
+
const ends2 = [simple[0], simple[simple.length - 1]];
|
|
798
|
+
let crossings = 0;
|
|
799
|
+
for (const seg of segments) {
|
|
800
|
+
crossings += occupancy.all.filter((r) => meets(seg, r, ends2)).length;
|
|
801
|
+
}
|
|
802
|
+
occupancy.add(segments);
|
|
803
|
+
parallel?.occupancy.add(segments);
|
|
804
|
+
return { points: simple, crossings };
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
function arrives(t, x, y, dir) {
|
|
808
|
+
const margin = 8;
|
|
809
|
+
const withinX = x >= t.x + margin && x <= t.x + t.w - margin;
|
|
810
|
+
const withinY = y >= t.y + margin && y <= t.y + t.h - margin;
|
|
811
|
+
return ((dir === 2 && Math.abs(y - t.y) < 0.6 && withinX) ||
|
|
812
|
+
(dir === 3 && Math.abs(y - t.y - t.h) < 0.6 && withinX) ||
|
|
813
|
+
(dir === 0 && Math.abs(x - t.x) < 0.6 && withinY) ||
|
|
814
|
+
(dir === 1 && Math.abs(x - t.x - t.w) < 0.6 && withinY));
|
|
815
|
+
}
|
|
816
|
+
function toSegments(points) {
|
|
817
|
+
const segments = [];
|
|
818
|
+
for (let i = 1; i < points.length; i++) {
|
|
819
|
+
const [x1, y1] = points[i - 1];
|
|
820
|
+
const [x2, y2] = points[i];
|
|
821
|
+
segments.push({ x1, y1, x2, y2 });
|
|
822
|
+
}
|
|
823
|
+
return segments;
|
|
824
|
+
}
|
|
825
|
+
function badgeText(index, style) {
|
|
826
|
+
if (style === 'numbers') {
|
|
827
|
+
return String(index + 1);
|
|
828
|
+
}
|
|
829
|
+
let text = '';
|
|
830
|
+
for (let n = index; n >= 0; n = Math.floor(n / 26) - 1) {
|
|
831
|
+
text = String.fromCharCode(97 + (n % 26)) + text;
|
|
832
|
+
}
|
|
833
|
+
return text;
|
|
834
|
+
}
|
|
835
|
+
function busSegments(scene) {
|
|
836
|
+
const segments = [];
|
|
837
|
+
for (const host of scene.containers) {
|
|
838
|
+
if (!host.bus) {
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
const comps = host.children.filter((c) => c.kind === 'comp');
|
|
842
|
+
if (comps.length < 2) {
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
const x = host.bus === 'left' ? host.rect.x + 12 : host.rect.x + host.rect.w - 12;
|
|
846
|
+
const ys = comps.map((c) => c.rect.y + c.rect.h / 2);
|
|
847
|
+
segments.push({ x1: x, y1: Math.min(...ys), x2: x, y2: Math.max(...ys) });
|
|
848
|
+
for (const [i, y] of ys.entries()) {
|
|
849
|
+
const comp = comps[i];
|
|
850
|
+
const edge = host.bus === 'left' ? comp.rect.x : comp.rect.x + comp.rect.w;
|
|
851
|
+
segments.push({ x1: x, y1: y, x2: edge, y2: y });
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return segments;
|
|
855
|
+
}
|
|
856
|
+
function routeAll(scene) {
|
|
857
|
+
const router = new Router(scene);
|
|
858
|
+
const buses = busSegments(scene);
|
|
859
|
+
router.occupancy.add(buses);
|
|
860
|
+
const pending = [];
|
|
861
|
+
const parallel = new Map();
|
|
862
|
+
// Opposite directions between the same endpoints use the same set of lanes.
|
|
863
|
+
const pairKey = (from, to) => (from < to ? `${from}:${to}` : `${to}:${from}`);
|
|
864
|
+
let numbered = 0;
|
|
865
|
+
for (const spec of scene.diagram.links) {
|
|
866
|
+
const badge = spec.arrow ? badgeText(numbered++, scene.diagram.badges) : '';
|
|
867
|
+
let first = true;
|
|
868
|
+
for (const sourceKey of spec.sources) {
|
|
869
|
+
for (const targetKey of spec.targets) {
|
|
870
|
+
const source = scene.byId.get(sourceKey);
|
|
871
|
+
const target = scene.byId.get(targetKey);
|
|
872
|
+
const plane = spec.plane ?? (source.kind === 'comp' ? source.plane : 'ext');
|
|
873
|
+
pending.push({
|
|
874
|
+
source,
|
|
875
|
+
target,
|
|
876
|
+
direct: !!router.straight(source, target),
|
|
877
|
+
link: {
|
|
878
|
+
from: sourceKey,
|
|
879
|
+
to: targetKey,
|
|
880
|
+
badge,
|
|
881
|
+
plane,
|
|
882
|
+
arrow: spec.arrow,
|
|
883
|
+
...(spec.bidirectional ? { bidirectional: true } : {}),
|
|
884
|
+
halo: false,
|
|
885
|
+
label: first ? spec.label : '',
|
|
886
|
+
points: [],
|
|
887
|
+
},
|
|
888
|
+
});
|
|
889
|
+
first = false;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
for (const { link } of pending) {
|
|
894
|
+
const key = pairKey(link.from, link.to);
|
|
895
|
+
const lanes = parallel.get(key) ?? {
|
|
896
|
+
occupancy: new Occupancy(),
|
|
897
|
+
spacing: laneSpacing,
|
|
898
|
+
count: 0,
|
|
899
|
+
};
|
|
900
|
+
lanes.count++;
|
|
901
|
+
// The label sits 25 points beside a vertical path, or up to 35 points
|
|
902
|
+
// above a horizontal path. Reserve space before routing sibling paths.
|
|
903
|
+
if (link.arrow) {
|
|
904
|
+
lanes.spacing = Math.max(lanes.spacing, labelAbovePath + 1, labelSideOffset + 1 + link.label.length * labelCharWidth);
|
|
905
|
+
}
|
|
906
|
+
parallel.set(key, lanes);
|
|
907
|
+
}
|
|
908
|
+
// Keep public link order and badge identities in declaration order, while
|
|
909
|
+
// reserving clear shared rows/columns before more complex paths use them.
|
|
910
|
+
for (const entry of [...pending].sort((a, b) => Number(b.direct) - Number(a.direct))) {
|
|
911
|
+
const lanes = parallel.get(pairKey(entry.link.from, entry.link.to));
|
|
912
|
+
const { points, crossings } = router.route(entry.source, entry.target, lanes && lanes.count > 1 ? lanes : undefined);
|
|
913
|
+
entry.link.points = points;
|
|
914
|
+
entry.link.halo = entry.link.arrow && crossings > 0;
|
|
915
|
+
}
|
|
916
|
+
const links = pending.map((entry) => entry.link);
|
|
917
|
+
for (const link of links) {
|
|
918
|
+
const endpoints = [link.points[0], link.points[link.points.length - 1]];
|
|
919
|
+
link.halo ||=
|
|
920
|
+
link.arrow &&
|
|
921
|
+
links.some((other) => other !== link &&
|
|
922
|
+
toSegments(link.points).some((a) => toSegments(other.points).some((b) => meets(a, b, endpoints))));
|
|
923
|
+
}
|
|
924
|
+
return { links, buses };
|
|
925
|
+
}
|
|
926
|
+
// ── badges and labels ─────────────────────────────────────────────────────
|
|
927
|
+
function rectsOverlap(a, b) {
|
|
928
|
+
return a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
|
|
929
|
+
}
|
|
930
|
+
function segmentHitsRect(s, r) {
|
|
931
|
+
const box = {
|
|
932
|
+
x: Math.min(s.x1, s.x2),
|
|
933
|
+
y: Math.min(s.y1, s.y2),
|
|
934
|
+
w: Math.abs(s.x2 - s.x1) || 0.5,
|
|
935
|
+
h: Math.abs(s.y2 - s.y1) || 0.5,
|
|
936
|
+
};
|
|
937
|
+
return rectsOverlap(box, r);
|
|
938
|
+
}
|
|
939
|
+
function borderSegments(r) {
|
|
940
|
+
return [
|
|
941
|
+
{ x1: r.x, y1: r.y, x2: r.x + r.w, y2: r.y },
|
|
942
|
+
{ x1: r.x, y1: r.y + r.h, x2: r.x + r.w, y2: r.y + r.h },
|
|
943
|
+
{ x1: r.x, y1: r.y, x2: r.x, y2: r.y + r.h },
|
|
944
|
+
{ x1: r.x + r.w, y1: r.y, x2: r.x + r.w, y2: r.y + r.h },
|
|
945
|
+
];
|
|
946
|
+
}
|
|
947
|
+
function place(scene, links, buses) {
|
|
948
|
+
const placed = { badges: [], labels: [] };
|
|
949
|
+
const blocked = [
|
|
950
|
+
...scene.comps.map((c) => inflate(c.rect, annotationPadding)),
|
|
951
|
+
...scene.titles,
|
|
952
|
+
];
|
|
953
|
+
const frames = scene.containers.flatMap((c) => borderSegments(c.rect));
|
|
954
|
+
const linkSegments = links.map((l) => toSegments(l.points));
|
|
955
|
+
const allSegments = [...buses, ...linkSegments.flat()];
|
|
956
|
+
const r = badgeRadius + annotationPadding;
|
|
957
|
+
const free = (box, own, avoidFrames) => box.x >= 0 &&
|
|
958
|
+
box.y >= 0 &&
|
|
959
|
+
box.x + box.w <= scene.diagram.width &&
|
|
960
|
+
box.y + box.h <= scene.height &&
|
|
961
|
+
!blocked.some((b) => rectsOverlap(b, box)) &&
|
|
962
|
+
!allSegments.some((s) => !own.includes(s) && segmentHitsRect(s, box)) &&
|
|
963
|
+
(!avoidFrames || !frames.some((s) => segmentHitsRect(s, box)));
|
|
964
|
+
const labelBoxes = (x, y, text, vertical) => {
|
|
965
|
+
const w = text.length * labelCharWidth;
|
|
966
|
+
const h = 11;
|
|
967
|
+
const side = [
|
|
968
|
+
{
|
|
969
|
+
x: x + labelSideOffset,
|
|
970
|
+
y: y + 3.5,
|
|
971
|
+
anchor: 'start',
|
|
972
|
+
box: { x: x + labelSideOffset, y: y - 6, w, h },
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
x: x - labelSideOffset,
|
|
976
|
+
y: y + 3.5,
|
|
977
|
+
anchor: 'end',
|
|
978
|
+
box: { x: x - labelSideOffset - w, y: y - 6, w, h },
|
|
979
|
+
},
|
|
980
|
+
];
|
|
981
|
+
const stacked = [
|
|
982
|
+
{ x, y: y - 26, anchor: 'middle', box: { x: x - w / 2, y: y - labelAbovePath, w, h } },
|
|
983
|
+
{ x, y: y + 34, anchor: 'middle', box: { x: x - w / 2, y: y + labelSideOffset, w, h } },
|
|
984
|
+
];
|
|
985
|
+
return vertical ? [...side, ...stacked] : [...stacked, ...side];
|
|
986
|
+
};
|
|
987
|
+
links.forEach((link, index) => {
|
|
988
|
+
if (!link.arrow) {
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
const own = linkSegments[index];
|
|
992
|
+
const ordered = own
|
|
993
|
+
.map((seg, i) => ({ seg, last: i === own.length - 1 }))
|
|
994
|
+
.sort((a, b) => segLength(b.seg) - segLength(a.seg));
|
|
995
|
+
let chosen = null;
|
|
996
|
+
// Strictness: avoid frames for badge and label, then for the badge only, then neither.
|
|
997
|
+
for (const [badgeAvoids, labelAvoids] of [
|
|
998
|
+
[true, true],
|
|
999
|
+
[true, false],
|
|
1000
|
+
[false, false],
|
|
1001
|
+
]) {
|
|
1002
|
+
for (const { seg, last } of ordered) {
|
|
1003
|
+
const vertical = seg.x1 === seg.x2;
|
|
1004
|
+
const length = segLength(seg);
|
|
1005
|
+
const endPad = last ? terminalLength + badgeRadius : 24;
|
|
1006
|
+
const startPad = link.bidirectional && seg === own[0] ? terminalLength + badgeRadius : 24;
|
|
1007
|
+
const sign = vertical ? Math.sign(seg.y2 - seg.y1) : Math.sign(seg.x2 - seg.x1);
|
|
1008
|
+
for (let k = 0; k <= length / 4 && !chosen; k++) {
|
|
1009
|
+
const offset = length / 2 + (k % 2 ? 1 : -1) * Math.ceil(k / 2) * 4;
|
|
1010
|
+
if (offset < startPad || offset > length - endPad) {
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
const x = vertical ? seg.x1 : seg.x1 + sign * offset;
|
|
1014
|
+
const y = vertical ? seg.y1 + sign * offset : seg.y1;
|
|
1015
|
+
const near = placed.badges.some((b) => Math.abs(b.x - x) < 2 * r && Math.abs(b.y - y) < 2 * r);
|
|
1016
|
+
if (near ||
|
|
1017
|
+
!free({ x: x - r, y: y - r, w: 2 * r, h: 2 * r }, own, badgeAvoids)) {
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
if (!link.label) {
|
|
1021
|
+
chosen = { x, y, label: null };
|
|
1022
|
+
break;
|
|
1023
|
+
}
|
|
1024
|
+
const badgeBox = { x: x - r, y: y - r, w: 2 * r, h: 2 * r };
|
|
1025
|
+
const label = labelBoxes(x, y, link.label, vertical).find((c) => !rectsOverlap(c.box, badgeBox) && free(c.box, [], labelAvoids));
|
|
1026
|
+
if (label) {
|
|
1027
|
+
chosen = { x, y, label };
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
if (chosen) {
|
|
1031
|
+
break;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (chosen) {
|
|
1035
|
+
break;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
if (!chosen) {
|
|
1039
|
+
throw new LayoutSpaceError(`infra: no clear annotation space for "${link.from}" to "${link.to}"`);
|
|
1040
|
+
}
|
|
1041
|
+
placed.badges.push({
|
|
1042
|
+
x: chosen.x,
|
|
1043
|
+
y: chosen.y,
|
|
1044
|
+
text: link.badge,
|
|
1045
|
+
plane: link.plane,
|
|
1046
|
+
link: index,
|
|
1047
|
+
});
|
|
1048
|
+
blocked.push({ x: chosen.x - r, y: chosen.y - r, w: 2 * r, h: 2 * r });
|
|
1049
|
+
if (chosen.label) {
|
|
1050
|
+
const { x, y, anchor, box } = chosen.label;
|
|
1051
|
+
placed.labels.push({ x, y, text: link.label, anchor, rect: box, link: index });
|
|
1052
|
+
blocked.push(box);
|
|
1053
|
+
}
|
|
1054
|
+
});
|
|
1055
|
+
return placed;
|
|
1056
|
+
}
|
|
1057
|
+
function segLength(s) {
|
|
1058
|
+
return Math.abs(s.x2 - s.x1) + Math.abs(s.y2 - s.y1);
|
|
1059
|
+
}
|
|
1060
|
+
// ── SVG ───────────────────────────────────────────────────────────────────
|
|
1061
|
+
function esc(text) {
|
|
1062
|
+
return text
|
|
1063
|
+
.replace(/&/g, '&')
|
|
1064
|
+
.replace(/</g, '<')
|
|
1065
|
+
.replace(/>/g, '>')
|
|
1066
|
+
.replace(/"/g, '"');
|
|
1067
|
+
}
|
|
1068
|
+
function n(value) {
|
|
1069
|
+
return String(Math.round(value * 10) / 10);
|
|
1070
|
+
}
|
|
1071
|
+
function pathData(points) {
|
|
1072
|
+
return points
|
|
1073
|
+
.map(([x, y], i) => {
|
|
1074
|
+
if (i === 0) {
|
|
1075
|
+
return `M${n(x)},${n(y)}`;
|
|
1076
|
+
}
|
|
1077
|
+
const [px] = points[i - 1];
|
|
1078
|
+
return px === x ? `V${n(y)}` : `H${n(x)}`;
|
|
1079
|
+
})
|
|
1080
|
+
.join(' ');
|
|
1081
|
+
}
|
|
1082
|
+
function describe(scene, links) {
|
|
1083
|
+
const name = (id) => {
|
|
1084
|
+
const node = scene.byId.get(id);
|
|
1085
|
+
return node?.kind === 'comp' ? node.name : (node?.title ?? id);
|
|
1086
|
+
};
|
|
1087
|
+
const parts = [];
|
|
1088
|
+
const visit = (node) => {
|
|
1089
|
+
if (node.kind === 'empty' || node.kind === 'comp') {
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
if (node.kind !== 'grid') {
|
|
1093
|
+
const names = node.children
|
|
1094
|
+
.filter((c) => c.kind === 'comp')
|
|
1095
|
+
.map((c) => c.name);
|
|
1096
|
+
if (names.length) {
|
|
1097
|
+
parts.push(`${node.title || node.id}: ${names.join(', ')}.`);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
node.children.forEach(visit);
|
|
1101
|
+
};
|
|
1102
|
+
visit(scene.diagram.root);
|
|
1103
|
+
const external = scene.diagram.root.children
|
|
1104
|
+
.filter((c) => c.kind === 'comp')
|
|
1105
|
+
.map((c) => c.name);
|
|
1106
|
+
if (external.length) {
|
|
1107
|
+
parts.push(`Also shown: ${external.join(', ')}.`);
|
|
1108
|
+
}
|
|
1109
|
+
let i = 0;
|
|
1110
|
+
for (const spec of scene.diagram.links) {
|
|
1111
|
+
if (!spec.arrow) {
|
|
1112
|
+
continue;
|
|
1113
|
+
}
|
|
1114
|
+
const badge = links.filter((l) => l.arrow)[i]?.badge ?? '';
|
|
1115
|
+
i += spec.sources.length * spec.targets.length;
|
|
1116
|
+
const via = spec.label ? ` (${spec.label})` : '';
|
|
1117
|
+
parts.push(`${badge}: ${spec.sources.map(name).join(' and ')} ${spec.bidirectional ? 'to and from' : 'to'} ${spec.targets.map(name).join(' and ')}${via}.`);
|
|
1118
|
+
}
|
|
1119
|
+
return parts.join(' ');
|
|
1120
|
+
}
|
|
1121
|
+
function ownMark(marks, key) {
|
|
1122
|
+
return Object.hasOwn(marks, key) ? marks[key] : undefined;
|
|
1123
|
+
}
|
|
1124
|
+
function markClass(mark) {
|
|
1125
|
+
if (!mark) {
|
|
1126
|
+
return '';
|
|
1127
|
+
}
|
|
1128
|
+
return mark === 'ghost' ? ' d d--removed d--ghost' : ` d d--${mark}`;
|
|
1129
|
+
}
|
|
1130
|
+
function renderNode(node, out, marks) {
|
|
1131
|
+
const { x, y, w, h } = node.rect;
|
|
1132
|
+
if (node.kind === 'empty') {
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
if (node.kind === 'comp') {
|
|
1136
|
+
if (node.stack) {
|
|
1137
|
+
out.push(`<rect class="comp-stack" x="${n(x + 6)}" y="${n(y + 6)}" width="${n(w)}" height="${n(h)}" rx="4"/>`, `<rect class="comp-stack" x="${n(x + 3)}" y="${n(y + 3)}" width="${n(w)}" height="${n(h)}" rx="4"/>`);
|
|
1138
|
+
}
|
|
1139
|
+
const mark = ownMark(marks, node.id);
|
|
1140
|
+
if (mark) {
|
|
1141
|
+
out.push(`<rect class="d-ring d--${mark}" x="${n(x - 4)}" y="${n(y - 4)}" width="${n(w + 8)}" height="${n(h + 8)}" rx="7"/>`);
|
|
1142
|
+
}
|
|
1143
|
+
const cls = (node.plane === 'ext' ? 'ext' : `comp comp--${node.plane}`) + markClass(mark);
|
|
1144
|
+
const nameY = node.plane === 'ext' ? 21 : 19;
|
|
1145
|
+
out.push(`<g class="${cls}"><rect x="${n(x)}" y="${n(y)}" width="${n(w)}" height="${n(h)}" rx="4"/>`, `<text class="t-name" x="${n(x + 12)}" y="${n(y + nameY)}">${esc(node.name)}</text>`, node.sub
|
|
1146
|
+
? `<text class="t-sub" x="${n(x + 12)}" y="${n(y + nameY + 15)}">${esc(node.sub)}</text></g>`
|
|
1147
|
+
: '</g>');
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (node.kind === 'host') {
|
|
1151
|
+
if (node.stack) {
|
|
1152
|
+
out.push(`<rect class="host-stack" x="${n(x + 8)}" y="${n(y + 8)}" width="${n(w)}" height="${n(h)}" rx="8"/>`);
|
|
1153
|
+
}
|
|
1154
|
+
out.push(`<rect class="host-frame${markClass(ownMark(marks, node.id))}" x="${n(x)}" y="${n(y)}" width="${n(w)}" height="${n(h)}" rx="8"/>`, `<text class="t-host" x="${n(x + 16)}" y="${n(y + 24)}">${esc(node.title)}</text>`);
|
|
1155
|
+
if (node.sub) {
|
|
1156
|
+
out.push(`<text class="t-hostsub" x="${n(x + 16)}" y="${n(y + 40)}">${esc(node.sub)}</text>`);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
else if (node.kind === 'group') {
|
|
1160
|
+
out.push(`<rect class="group group--${node.plane}${markClass(ownMark(marks, node.id))}" x="${n(x)}" y="${n(y)}" width="${n(w)}" height="${n(h)}" rx="6"/>`);
|
|
1161
|
+
if (node.title) {
|
|
1162
|
+
out.push(`<text class="t-group" x="${n(x + 12)}" y="${n(y + 16)}">${esc(node.title)}</text>`);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
for (const child of node.children) {
|
|
1166
|
+
renderNode(child, out, marks);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function plan(source) {
|
|
1170
|
+
// Gaps and width are minimums: preserve component widths while opening
|
|
1171
|
+
// routing corridors. Retry the layout, never overlap annotations as a fallback.
|
|
1172
|
+
let lastError;
|
|
1173
|
+
for (let pass = 0; pass < 6; pass++) {
|
|
1174
|
+
const diagram = parseInfra(source);
|
|
1175
|
+
layout(diagram.root, 0, 0, diagram.width, true);
|
|
1176
|
+
const minimumGap = 96 + pass * 32;
|
|
1177
|
+
const expand = (node, isRoot = false) => {
|
|
1178
|
+
const pad = padding(node, isRoot);
|
|
1179
|
+
const oldGap = node.gap;
|
|
1180
|
+
const oldColumn = (node.rect.w - 2 * pad.x - oldGap * (node.cols - 1)) / node.cols;
|
|
1181
|
+
node.gap = Math.max(node.gap, minimumGap);
|
|
1182
|
+
let column = oldColumn;
|
|
1183
|
+
for (const child of node.children) {
|
|
1184
|
+
const span = Math.min(child.span, node.cols);
|
|
1185
|
+
if (child.kind === 'empty') {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
const needed = child.kind === 'comp'
|
|
1189
|
+
? Math.max(child.rect.w, textWidth(child.name, 12.5, 0.52) + 32, textWidth(child.sub, 9.5, 0.6) + 32)
|
|
1190
|
+
: expand(child);
|
|
1191
|
+
column = Math.max(column, (needed - node.gap * (span - 1)) / span);
|
|
1192
|
+
}
|
|
1193
|
+
return column * node.cols + node.gap * (node.cols - 1) + 2 * pad.x;
|
|
1194
|
+
};
|
|
1195
|
+
diagram.width = Math.max(diagram.width, expand(diagram.root, true));
|
|
1196
|
+
try {
|
|
1197
|
+
const scene = buildScene(diagram);
|
|
1198
|
+
const { links, buses } = routeAll(scene);
|
|
1199
|
+
return { scene, links, buses, placed: place(scene, links, buses) };
|
|
1200
|
+
}
|
|
1201
|
+
catch (error) {
|
|
1202
|
+
if (!(error instanceof LayoutSpaceError)) {
|
|
1203
|
+
throw error;
|
|
1204
|
+
}
|
|
1205
|
+
lastError = error;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
throw lastError;
|
|
1209
|
+
}
|
|
1210
|
+
/** Geometry for checks: where every component, link, badge, and label landed. */
|
|
1211
|
+
export function layoutInfra(source) {
|
|
1212
|
+
const { scene, links, placed } = plan(source);
|
|
1213
|
+
return {
|
|
1214
|
+
width: scene.diagram.width,
|
|
1215
|
+
height: scene.height,
|
|
1216
|
+
comps: scene.comps.map((c) => ({ id: c.id, rect: c.rect })),
|
|
1217
|
+
titles: scene.titles,
|
|
1218
|
+
links,
|
|
1219
|
+
badges: placed.badges,
|
|
1220
|
+
labels: placed.labels,
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
/** Keep identifiers safe in XML attributes, fragment URLs and CSS selectors. */
|
|
1224
|
+
function normalizePrefix(prefix) {
|
|
1225
|
+
const safe = Array.from(prefix, (char) => /^[A-Za-z0-9-]$/.test(char) ? char : `_u${char.codePointAt(0)?.toString(16)}_`).join('');
|
|
1226
|
+
return /^[A-Za-z]/.test(safe) ? safe : `_${safe}`;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Render a diagram to SVG. `marks` highlights components, containers (by id),
|
|
1230
|
+
* and links (by "from->to") for architecture diffs; see `diffInfra`.
|
|
1231
|
+
*/
|
|
1232
|
+
export function renderInfra(source, idPrefix, marks = {}, options = {}) {
|
|
1233
|
+
idPrefix = normalizePrefix(idPrefix);
|
|
1234
|
+
const { scene, links, buses, placed } = plan(source);
|
|
1235
|
+
const { diagram } = scene;
|
|
1236
|
+
const out = [
|
|
1237
|
+
`<svg${options.standalone ? ` xmlns="http://www.w3.org/2000/svg" id="${idPrefix}-infra"` : ''} class="infra" viewBox="0 0 ${diagram.width} ${n(scene.height)}" role="img" aria-labelledby="${idPrefix}-title ${idPrefix}-desc">`,
|
|
1238
|
+
`<title id="${idPrefix}-title">${esc(diagram.title)}</title>`,
|
|
1239
|
+
`<desc id="${idPrefix}-desc">${esc(diagram.desc || describe(scene, links))}</desc>`,
|
|
1240
|
+
...(options.standalone
|
|
1241
|
+
? [standaloneStyle(`${idPrefix}-infra`, options.standalone.theme)]
|
|
1242
|
+
: []),
|
|
1243
|
+
...(options.standalone
|
|
1244
|
+
? ['<rect class="infra-background" width="100%" height="100%" aria-hidden="true"/>']
|
|
1245
|
+
: []),
|
|
1246
|
+
'<defs>',
|
|
1247
|
+
...planes.map((p) => `<marker id="${idPrefix}-${p}" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6.5" markerHeight="6.5" orient="auto-start-reverse"><path class="arrow arrow--${p}" d="M0,0 L10,5 L0,10 z"/></marker>`),
|
|
1248
|
+
'</defs>',
|
|
1249
|
+
];
|
|
1250
|
+
for (const child of diagram.root.children) {
|
|
1251
|
+
renderNode(child, out, marks);
|
|
1252
|
+
}
|
|
1253
|
+
const linkMark = (index) => {
|
|
1254
|
+
const link = links[index];
|
|
1255
|
+
return ownMark(marks, `${link.from}->${link.to}`);
|
|
1256
|
+
};
|
|
1257
|
+
for (const bus of buses) {
|
|
1258
|
+
out.push(`<path class="bus" d="M${n(bus.x1)},${n(bus.y1)} ${bus.x1 === bus.x2 ? `V${n(bus.y2)}` : `H${n(bus.x2)}`}"/>`);
|
|
1259
|
+
}
|
|
1260
|
+
links.forEach((link, index) => {
|
|
1261
|
+
const d = pathData(link.points);
|
|
1262
|
+
const mark = linkMark(index);
|
|
1263
|
+
if (mark) {
|
|
1264
|
+
out.push(`<path class="d-halo d--${mark}" d="${d}"/>`);
|
|
1265
|
+
}
|
|
1266
|
+
if (!link.arrow) {
|
|
1267
|
+
out.push(`<path class="bus${markClass(mark)}" d="${d}"/>`);
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1270
|
+
if (link.halo) {
|
|
1271
|
+
out.push(`<path class="link-halo" d="${d}"/>`);
|
|
1272
|
+
}
|
|
1273
|
+
const marker = `url(#${idPrefix}-${link.plane})`;
|
|
1274
|
+
const start = link.bidirectional ? ` marker-start="${marker}"` : '';
|
|
1275
|
+
out.push(`<path class="link link--${link.plane}${markClass(mark)}" d="${d}"${start} marker-end="${marker}"/>`);
|
|
1276
|
+
});
|
|
1277
|
+
for (const badge of placed.badges) {
|
|
1278
|
+
const mark = linkMark(badge.link);
|
|
1279
|
+
const text = mark === 'ghost' ? '\u2212' : badge.text;
|
|
1280
|
+
out.push(`<g class="badge badge--${badge.plane}${markClass(mark)}"><circle cx="${n(badge.x)}" cy="${n(badge.y)}" r="${badgeRadius}"/><text x="${n(badge.x)}" y="${n(badge.y + 3.5)}">${esc(text)}</text></g>`);
|
|
1281
|
+
}
|
|
1282
|
+
for (const label of placed.labels) {
|
|
1283
|
+
out.push(`<text class="t-label${markClass(linkMark(label.link))}" x="${n(label.x)}" y="${n(label.y)}" text-anchor="${label.anchor}">${esc(label.text)}</text>`);
|
|
1284
|
+
}
|
|
1285
|
+
out.push('</svg>');
|
|
1286
|
+
return out.join('');
|
|
1287
|
+
}
|
|
1288
|
+
function facts(diagram) {
|
|
1289
|
+
const out = new Map();
|
|
1290
|
+
const visit = (node, parent) => {
|
|
1291
|
+
if (node.kind === 'empty') {
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
if (node.kind === 'comp') {
|
|
1295
|
+
out.set(node.id, {
|
|
1296
|
+
kind: 'comp',
|
|
1297
|
+
name: node.name,
|
|
1298
|
+
fields: { name: node.name, sub: node.sub, plane: node.plane, in: parent },
|
|
1299
|
+
});
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
if (node.kind !== 'grid') {
|
|
1303
|
+
out.set(node.id, {
|
|
1304
|
+
kind: node.kind,
|
|
1305
|
+
name: node.title,
|
|
1306
|
+
fields: { title: node.title, sub: node.sub, in: parent },
|
|
1307
|
+
});
|
|
1308
|
+
}
|
|
1309
|
+
const inner = node.kind === 'grid' ? parent : node.id;
|
|
1310
|
+
for (const child of node.children) {
|
|
1311
|
+
visit(child, inner);
|
|
1312
|
+
}
|
|
1313
|
+
};
|
|
1314
|
+
visit(diagram.root, '');
|
|
1315
|
+
const name = (id) => out.get(id)?.name ?? id;
|
|
1316
|
+
for (const spec of diagram.links) {
|
|
1317
|
+
for (const from of spec.sources) {
|
|
1318
|
+
for (const to of spec.targets) {
|
|
1319
|
+
out.set(`${from}->${to}`, {
|
|
1320
|
+
kind: 'link',
|
|
1321
|
+
name: `${name(from)} ${spec.bidirectional ? '\u2194' : '\u2192'} ${name(to)}`,
|
|
1322
|
+
fields: {
|
|
1323
|
+
label: spec.label,
|
|
1324
|
+
plane: spec.plane ?? '',
|
|
1325
|
+
line: spec.bidirectional ? 'bidirectional' : spec.arrow ? 'arrow' : 'bus',
|
|
1326
|
+
},
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return out;
|
|
1332
|
+
}
|
|
1333
|
+
/** What changed between two versions of a diagram, matched by id. */
|
|
1334
|
+
export function diffInfra(before, after) {
|
|
1335
|
+
const a = facts(parseInfra(before));
|
|
1336
|
+
const b = facts(parseInfra(after));
|
|
1337
|
+
const changes = [];
|
|
1338
|
+
const show = (key, value) => key === 'in' ? value || 'top level' : value || '(none)';
|
|
1339
|
+
for (const [key, next] of b) {
|
|
1340
|
+
const prev = a.get(key);
|
|
1341
|
+
if (!prev) {
|
|
1342
|
+
const label = next.fields.label;
|
|
1343
|
+
changes.push({
|
|
1344
|
+
key,
|
|
1345
|
+
kind: next.kind,
|
|
1346
|
+
status: 'added',
|
|
1347
|
+
name: next.name,
|
|
1348
|
+
details: label ? [label] : [],
|
|
1349
|
+
});
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
const details = Object.keys(next.fields)
|
|
1353
|
+
.filter((f) => prev.fields[f] !== next.fields[f])
|
|
1354
|
+
.map((f) => `${f}: ${show(f, prev.fields[f] ?? '')} \u2192 ${show(f, next.fields[f] ?? '')}`);
|
|
1355
|
+
if (details.length) {
|
|
1356
|
+
changes.push({ key, kind: next.kind, status: 'changed', name: next.name, details });
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
for (const [key, prev] of a) {
|
|
1360
|
+
if (!b.has(key)) {
|
|
1361
|
+
const label = prev.fields.label;
|
|
1362
|
+
changes.push({
|
|
1363
|
+
key,
|
|
1364
|
+
kind: prev.kind,
|
|
1365
|
+
status: 'removed',
|
|
1366
|
+
name: prev.name,
|
|
1367
|
+
details: label ? [label] : [],
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return changes;
|
|
1372
|
+
}
|
|
1373
|
+
//# sourceMappingURL=infra-diagram.js.map
|