Repository navigation
Expand file tree
/
Copy pathlayout.ts
More file actions
359 lines (353 loc) · 11.7 KB
/
Copy pathlayout.ts
File metadata and controls
359 lines (353 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import ELK from "elkjs/lib/elk.bundled.js";
import type { ElkNode, ElkExtendedEdge, ELK as ElkEngine } from "elkjs/lib/elk-api.js";
import { widestLine, wrap } from "./measure.ts";
import { theme, type TextRole } from "./theme.ts";
import { validateDiagram, type Diagram } from "./diagram.ts";
import { textBlock, type Item, type Sketch } from "./scene.ts";
import { compileBoard } from "./board.ts";
const MARGIN = 24;
const ROOT = "$root";
function prepareGraph(diagram: Diagram, direction: "RIGHT" | "DOWN") {
const nodeLines = new Map<
string,
Pick<Extract<Item, { kind: "node" }>, "lines" | "titleLineCount">
>();
const groupLines = new Map<string, string[]>();
const elements = new Map<string, ElkNode>();
const root: ElkNode = {
id: ROOT,
children: [],
layoutOptions: {
"elk.algorithm": "layered",
"elk.direction": direction,
"elk.edgeRouting": "ORTHOGONAL",
"elk.hierarchyHandling": "INCLUDE_CHILDREN",
"elk.randomSeed": "1",
"elk.padding": "[top=16,left=16,bottom=16,right=16]",
"elk.spacing.nodeNode": "20",
"elk.layered.spacing.nodeNodeBetweenLayers": "28",
"elk.spacing.edgeLabel": "6",
"elk.layered.considerModelOrder.strategy": "NODES_AND_EDGES",
},
};
elements.set(ROOT, root);
for (const group of diagram.groups) {
const lines = wrap(
group.title,
theme.text.group.size,
"Bold",
direction === "DOWN" ? 200 : 320,
);
groupLines.set(group.id, lines);
const minWidth = Math.ceil(Math.max(56, widestLine(lines, theme.text.group.size, "Bold")) + 32);
elements.set(group.id, {
id: group.id,
children: [],
width: minWidth,
height: 32 + lines.length * 22,
layoutOptions: {
"elk.direction": direction,
"elk.padding": `[top=${16 + lines.length * 22},left=16,bottom=16,right=16]`,
"elk.nodeSize.constraints": "MINIMUM_SIZE",
"elk.nodeSize.minimum": `(${minWidth},${32 + lines.length * 22})`,
},
});
}
for (const group of diagram.groups) {
elements.get(group.parent ?? ROOT)!.children!.push(elements.get(group.id)!);
}
for (const node of diagram.nodes) {
const { titleSize, detailSize, padding, stride } = theme.node;
const title = wrap(node.title, titleSize, "Bold", 360);
const detail = node.detail ? wrap(node.detail, detailSize, "Italic", 210) : [];
const lines = [...title, ...detail];
nodeLines.set(node.id, {
lines,
...(title.length > 1 ? { titleLineCount: title.length } : {}),
});
const textWidth = Math.max(
56,
widestLine(title, titleSize, "Bold"),
widestLine(detail, detailSize, "Italic"),
);
const element: ElkNode = {
id: node.id,
width: Math.ceil(textWidth + 2 * padding),
height: 22 + lines.length * stride,
};
elements.get(node.group ?? ROOT)!.children!.push(element);
}
const edgeLines = diagram.edges.map((edge) =>
wrap(
`${edge.kind ? `${edge.kind === "setup" ? "Setup" : "Conditional"}: ` : ""}${edge.label}`,
theme.text.label.size,
"Regular",
160,
),
);
// Route both directions between a pair of regions through the same corridor.
// The first connection sets the layout direction; the scene restores each
// arrow to the source and target the author specified.
const parents = new Map(diagram.groups.map((group) => [group.id, group.parent]));
const region = new Map(
diagram.nodes.map((node) => {
let id = node.group ?? node.id;
while (parents.get(id)) {
id = parents.get(id)!;
}
return [node.id, id];
}),
);
const outbound = new Set<string>();
root.edges = diagram.edges.map((edge, index): ElkExtendedEdge => {
let from = region.get(edge.from)!;
let to = region.get(edge.to)!;
if (from === to) {
from = edge.from;
to = edge.to;
}
const reply = from !== to && outbound.has(`${to}>${from}`);
if (!reply) {
outbound.add(`${from}>${to}`);
}
const lines = edgeLines[index];
const bounds = textBlock({
kind: "text",
role: "label",
x: 0,
y: 0,
text: lines.join("\n"),
backdrop: true,
});
return {
id: `$edge-${index}`,
sources: [reply ? edge.to : edge.from],
targets: [reply ? edge.from : edge.to],
labels: [
{
text: lines.join("\n"),
width: Math.ceil(bounds.w),
height: Math.ceil(bounds.h),
...(direction === "DOWN"
? { layoutOptions: { "elk.edgeLabels.placement": "HEAD" } }
: {}),
},
],
};
});
return { root, nodeLines, groupLines, edgeLines };
}
function absolutePositions(graph: ElkNode, left: number, top: number) {
const positions = new Map<string, { x: number; y: number; node: ElkNode }>();
function collect(node: ElkNode, x: number, y: number): void {
const position = { x: x + (node.x ?? 0), y: y + (node.y ?? 0), node };
positions.set(node.id, position);
for (const child of node.children ?? []) {
collect(child, position.x, position.y);
}
}
collect(graph, left, top);
return positions;
}
function routedEdges(
diagram: Diagram,
positions: ReturnType<typeof absolutePositions>,
edgeLines: string[][],
) {
const lines: Item[] = [];
const labels: { lines: string[]; x: number; y: number }[] = [];
const segments: { left: number; right: number; top: number; bottom: number }[] = [];
for (const { node: owner } of positions.values()) {
for (const edge of owner.edges ?? []) {
const index = Number(edge.id.slice("$edge-".length));
const origin = positions.get(edge.container ?? owner.id)!;
if (!origin || !edge.sections?.length) {
throw new Error(`Layout did not route edge ${index}`);
}
for (const section of edge.sections) {
const points = [section.startPoint, ...(section.bendPoints ?? []), section.endPoint].map(
(point): [number, number] => [origin.x + point.x, origin.y + point.y],
);
for (let i = 1; i < points.length; i++) {
const [x1, y1] = points[i - 1];
const [x2, y2] = points[i];
segments.push({
left: Math.min(x1, x2),
right: Math.max(x1, x2),
top: Math.min(y1, y2),
bottom: Math.max(y1, y2),
});
}
const reply = edge.sources[0] !== diagram.edges[index].from;
lines.push({
kind: "line",
points: reply ? points.reverse() : points,
dashed: diagram.edges[index].kind !== undefined,
arrow: reply ? !section.incomingSections?.length : !section.outgoingSections?.length,
});
}
for (const label of edge.labels ?? []) {
labels.push({
lines: edgeLines[index],
x: origin.x + (label.x ?? 0) + theme.caption.paddingX,
y: origin.y + (label.y ?? 0) + theme.caption.paddingY + theme.text.label.size,
});
}
}
}
return { lines, labels, segments };
}
function compileScene(
diagram: Diagram,
graph: ElkNode,
{ nodeLines, groupLines, edgeLines }: ReturnType<typeof prepareGraph>,
): Sketch {
const groups = new Map(diagram.groups.map((group) => [group.id, group]));
const contentWidth = Math.max(520, graph.width ?? 0);
const headingWidth = Math.min(1000, contentWidth);
const titleLines = wrap(diagram.title, theme.text.title.size, "Bold", headingWidth);
const subtitleLines = diagram.subtitle
? wrap(diagram.subtitle, theme.text.subtitle.size, "Regular", headingWidth)
: [];
const graphTop = MARGIN + titleLines.length * 44 + subtitleLines.length * 26 + 20;
const items: Item[] = [];
function textLines(lines: string[], x: number, y: number, stride: number, role: TextRole): void {
lines.forEach((text, index) =>
items.push({
kind: "text",
x,
y: y + index * stride,
text,
size: theme.text[role].size,
role,
}),
);
}
textLines(titleLines, MARGIN, MARGIN + theme.text.title.size, 44, "title");
textLines(
subtitleLines,
MARGIN,
MARGIN + titleLines.length * 44 + theme.text.subtitle.size,
26,
"subtitle",
);
const positions = absolutePositions(graph, MARGIN, graphTop);
const headings: { lines: string[]; x: number; y: number; width: number }[] = [];
for (const { node, x, y } of positions.values()) {
const group = groups.get(node.id);
if (group) {
items.push({
kind: "box",
id: node.id,
x,
y,
w: node.width!,
h: node.height!,
role: group.role ?? "boundary",
});
headings.push({
lines: groupLines.get(node.id)!,
x: x + 16,
y: y + 23,
width: node.width! - 32,
});
}
}
for (const node of diagram.nodes) {
const { x, y, node: element } = positions.get(node.id)!;
items.push({
kind: "node",
id: node.id,
x,
y,
w: element.width!,
h: element.height!,
...nodeLines.get(node.id)!,
role: groups.get(node.group ?? "")?.role ?? "runtime",
});
}
const routes = routedEdges(diagram, positions, edgeLines);
items.push(...routes.lines);
// Boundary-crossing routes may enter the header from above. Place the
// heading in the first clear horizontal interval within its reserved band.
for (const heading of headings) {
const size = theme.text.group.size;
const occupied = routes.segments.filter(
(s) =>
s.top <= heading.y + (heading.lines.length - 1) * 22 + 4 && s.bottom >= heading.y - size,
);
const textWidth = widestLine(heading.lines, size, "Bold");
let x = heading.x;
for (const span of occupied.sort((a, b) => a.left - b.left)) {
if (span.left >= x + textWidth + 5) {
break;
}
if (span.right >= x - 5) {
x = span.right + 5;
}
}
textLines(
heading.lines,
x + textWidth <= heading.x + heading.width ? x : heading.x,
heading.y,
22,
"group",
);
}
for (const label of routes.labels) {
items.push({
kind: "text",
role: "label",
text: label.lines.join("\n"),
x: label.x,
y: label.y,
size: theme.text.label.size,
backdrop: true,
});
}
let bottom = graphTop + (graph.height ?? 0);
for (const note of diagram.notes ?? []) {
const lines = wrap(note, theme.text.note.size, "Italic", Math.min(1000, contentWidth));
bottom += 10;
textLines(lines, MARGIN, bottom + theme.text.note.size, 20, "note");
bottom += lines.length * 20;
}
return {
title: diagram.title,
width: Math.ceil(contentWidth + MARGIN * 2),
height: Math.ceil(bottom + MARGIN),
items,
};
}
/** Choose the orientation that fits most legibly into a landscape review window. */
export async function compileDiagram(diagram: Diagram): Promise<Sketch> {
validateDiagram(diagram);
if (diagram.layout?.board) {
return compileBoard(diagram);
}
// The bundled CommonJS export is the constructor, despite its ESM declaration.
const Elk = ELK as unknown as { new (): ElkEngine };
const elk = new Elk();
const directions = diagram.layout?.direction
? [diagram.layout.direction]
: (["RIGHT", "DOWN"] as const);
let best: Sketch | undefined;
let bestScore = Infinity;
for (const direction of directions) {
const prepared = prepareGraph(diagram, direction);
const graph = await elk.layout(prepared.root);
const scene = compileScene(diagram, graph, prepared);
if (scene.width > 8192 || scene.height > 8192 || scene.width * scene.height > 16_000_000) {
continue;
}
const score = Math.max(scene.width / 1.65, scene.height) + Math.max(0, scene.width - 1600);
if (score < bestScore) {
best = scene;
bestScore = score;
}
}
if (!best) {
throw new Error("Diagram exceeds scene size limits; split it into smaller diagrams");
}
return best;
}