@tinyactors/scxmljs 0.1.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/LICENSE +21 -0
- package/README.md +174 -0
- package/THIRD_PARTY_NOTICES +63 -0
- package/dist/bridge.d.ts +44 -0
- package/dist/bridge.js +254 -0
- package/dist/bridge.js.map +1 -0
- package/dist/clock.d.ts +113 -0
- package/dist/clock.js +310 -0
- package/dist/clock.js.map +1 -0
- package/dist/common.d.ts +15 -0
- package/dist/common.js +14 -0
- package/dist/common.js.map +1 -0
- package/dist/datamodel-base.d.ts +122 -0
- package/dist/datamodel-base.js +268 -0
- package/dist/datamodel-base.js.map +1 -0
- package/dist/datamodel-quickjs.d.ts +21 -0
- package/dist/datamodel-quickjs.js +286 -0
- package/dist/datamodel-quickjs.js.map +1 -0
- package/dist/datamodel-trusted.d.ts +1 -0
- package/dist/datamodel-trusted.js +149 -0
- package/dist/datamodel-trusted.js.map +1 -0
- package/dist/diagnostics.d.ts +39 -0
- package/dist/diagnostics.js +154 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/element-events.d.ts +85 -0
- package/dist/element-events.js +129 -0
- package/dist/element-events.js.map +1 -0
- package/dist/events.d.ts +31 -0
- package/dist/events.js +33 -0
- package/dist/events.js.map +1 -0
- package/dist/explorer/element.d.ts +150 -0
- package/dist/explorer/element.js +1791 -0
- package/dist/explorer/element.js.map +1 -0
- package/dist/explorer/layout.d.ts +43 -0
- package/dist/explorer/layout.js +50 -0
- package/dist/explorer/layout.js.map +1 -0
- package/dist/explorer/strings.d.ts +275 -0
- package/dist/explorer/strings.js +135 -0
- package/dist/explorer/strings.js.map +1 -0
- package/dist/explorer/styles.d.ts +10 -0
- package/dist/explorer/styles.js +265 -0
- package/dist/explorer/styles.js.map +1 -0
- package/dist/explorer/viewmodel.d.ts +284 -0
- package/dist/explorer/viewmodel.js +481 -0
- package/dist/explorer/viewmodel.js.map +1 -0
- package/dist/explorer.d.ts +21 -0
- package/dist/explorer.js +22 -0
- package/dist/explorer.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +157 -0
- package/dist/model.js +485 -0
- package/dist/model.js.map +1 -0
- package/dist/session-events.d.ts +136 -0
- package/dist/session-events.js +114 -0
- package/dist/session-events.js.map +1 -0
- package/dist/session.d.ts +412 -0
- package/dist/session.js +1399 -0
- package/dist/session.js.map +1 -0
- package/dist/themes/tinyactors.css +44 -0
- package/dist/trusted.d.ts +15 -0
- package/dist/trusted.js +30 -0
- package/dist/trusted.js.map +1 -0
- package/dist/ui/announcer.d.ts +22 -0
- package/dist/ui/announcer.js +31 -0
- package/dist/ui/announcer.js.map +1 -0
- package/dist/ui/svg.d.ts +8 -0
- package/dist/ui/svg.js +29 -0
- package/dist/ui/svg.js.map +1 -0
- package/dist/ui/theme.d.ts +34 -0
- package/dist/ui/theme.js +151 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/view/element.d.ts +172 -0
- package/dist/view/element.js +1045 -0
- package/dist/view/element.js.map +1 -0
- package/dist/view/layout.d.ts +154 -0
- package/dist/view/layout.js +1122 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/strings.d.ts +72 -0
- package/dist/view/strings.js +48 -0
- package/dist/view/strings.js.map +1 -0
- package/dist/view/styles.d.ts +1 -0
- package/dist/view/styles.js +115 -0
- package/dist/view/styles.js.map +1 -0
- package/dist/view.d.ts +20 -0
- package/dist/view.js +21 -0
- package/dist/view.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,1122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hierarchical layout for `<scxml-view>`: the whole chart at once, as nested
|
|
3
|
+
* boxes with orthogonal transition routes. Pure and deterministic — no DOM, no
|
|
4
|
+
* randomness — so it is unit-testable and the same chart always looks the same.
|
|
5
|
+
*
|
|
6
|
+
* Per container (the root, compound states), children are arranged with a
|
|
7
|
+
* layered (Sugiyama-style) layout of the transitions between them:
|
|
8
|
+
* 1. transitions between descendants of two different children are lifted to
|
|
9
|
+
* an edge between those children;
|
|
10
|
+
* 2. cycles are broken by a depth-first search from the initial child
|
|
11
|
+
* (document order breaks ties); the back edges are routed separately;
|
|
12
|
+
* 3. layers by longest path; edges spanning several layers get dummy nodes
|
|
13
|
+
* that reserve a corridor through the layers in between;
|
|
14
|
+
* 4. crossings are reduced with barycentre sweeps (the best order wins);
|
|
15
|
+
* 5. positions: layers side by side (gaps sized for labels and bend channels),
|
|
16
|
+
* nodes aligned towards the centre of their neighbours.
|
|
17
|
+
* Parallel states arrange their regions in a grid. Containers size themselves
|
|
18
|
+
* around their content, bottom-up.
|
|
19
|
+
*
|
|
20
|
+
* Routing: forward edges leave the source box on its far side, carry their
|
|
21
|
+
* label in the gap after the source's layer, bend in a channel of their own and
|
|
22
|
+
* enter the target box on its near side; back edges leave downwards, run along
|
|
23
|
+
* a lane below the container's content and come back up. Everything is computed
|
|
24
|
+
* in a canonical orientation (layers along +x) and transposed for `direction:
|
|
25
|
+
* "down"`.
|
|
26
|
+
*/
|
|
27
|
+
import { isCompound } from "../model.js";
|
|
28
|
+
const DEFAULT_SPACING = { node: 18, layer: 44, padding: 14, edge: 10, label: 6 };
|
|
29
|
+
const INITIAL_DOT = 10;
|
|
30
|
+
// ───────────────────────────────── helpers ─────────────────────────────────
|
|
31
|
+
const within = (s, ancestor) => {
|
|
32
|
+
for (let n = s; n; n = n.parent)
|
|
33
|
+
if (n === ancestor)
|
|
34
|
+
return true;
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
/** The child of `c` on the way down to `s` (s itself when s is a child), or undefined. */
|
|
38
|
+
const childOf = (c, s) => {
|
|
39
|
+
let n = s;
|
|
40
|
+
while (n && n.parent !== c)
|
|
41
|
+
n = n.parent;
|
|
42
|
+
return n ?? undefined;
|
|
43
|
+
};
|
|
44
|
+
const isContainerNode = (s) => s.kind === "scxml" || s.kind === "parallel" || isCompound(s);
|
|
45
|
+
// canonical ⇄ real: canonical x runs along the layers
|
|
46
|
+
const tp = (p, down) => (down ? { x: p.y, y: p.x } : p);
|
|
47
|
+
const tr = (r, down) => (down ? { x: r.y, y: r.x, w: r.h, h: r.w } : r);
|
|
48
|
+
// ─────────────────────────────── the layout ───────────────────────────────
|
|
49
|
+
/**
|
|
50
|
+
* Lay out a whole chart (usually `model.root`) as nested boxes with
|
|
51
|
+
* orthogonal edges. Pure and deterministic: no DOM, no randomness. This is
|
|
52
|
+
* what `<scxml-view>` draws; hosts can use it to draw charts themselves.
|
|
53
|
+
*/
|
|
54
|
+
export function layoutChart(root, opts) {
|
|
55
|
+
const direction = opts.direction ?? "right";
|
|
56
|
+
const down = direction === "down";
|
|
57
|
+
const sp = { ...DEFAULT_SPACING, ...opts.spacing };
|
|
58
|
+
const collapsed = opts.collapsed ?? new Set();
|
|
59
|
+
// hidden: inside a collapsed container
|
|
60
|
+
const drawn = (s) => {
|
|
61
|
+
let top = s;
|
|
62
|
+
for (let n = s.parent; n; n = n.parent)
|
|
63
|
+
if (collapsed.has(n))
|
|
64
|
+
top = n;
|
|
65
|
+
return top;
|
|
66
|
+
};
|
|
67
|
+
const isLeafBox = (s) => !isContainerNode(s) || collapsed.has(s);
|
|
68
|
+
/** Visible children of an expanded container: states, then history pseudo-states. */
|
|
69
|
+
const kidsOf = (c) => (c.kind === "parallel" ? c.children : [...c.children, ...c.history]);
|
|
70
|
+
// every transition, grouped by the container that routes it (filled while lifting)
|
|
71
|
+
const transitions = [];
|
|
72
|
+
const all = [];
|
|
73
|
+
const walk = (s) => {
|
|
74
|
+
all.push(s);
|
|
75
|
+
for (const t of s.transitions)
|
|
76
|
+
transitions.push(t);
|
|
77
|
+
for (const k of [...s.children, ...s.history])
|
|
78
|
+
walk(k);
|
|
79
|
+
};
|
|
80
|
+
walk(root);
|
|
81
|
+
const infos = new Map();
|
|
82
|
+
const labelAlong = (t) => {
|
|
83
|
+
const l = opts.labelSize(t);
|
|
84
|
+
return (down ? l.h : l.w) + 2 * sp.label;
|
|
85
|
+
};
|
|
86
|
+
/** Lay out a container's children; returns its real size. Leaves just measure. */
|
|
87
|
+
const layout = (c) => {
|
|
88
|
+
if (isLeafBox(c))
|
|
89
|
+
return opts.measure(c, collapsed.has(c));
|
|
90
|
+
const head = opts.measure(c, false);
|
|
91
|
+
const kids = kidsOf(c);
|
|
92
|
+
const sizes = new Map(kids.map((k) => [k, layout(k)]));
|
|
93
|
+
const info = {
|
|
94
|
+
state: c,
|
|
95
|
+
kind: c.kind === "scxml" ? "root" : c.kind === "parallel" ? "parallel" : "compound",
|
|
96
|
+
size: { w: 0, h: 0 },
|
|
97
|
+
header: c.kind === "scxml" ? 0 : head.h,
|
|
98
|
+
content: { x: sp.padding, y: (c.kind === "scxml" ? 0 : head.h) + sp.padding },
|
|
99
|
+
nodes: new Map(),
|
|
100
|
+
initialPad: 0,
|
|
101
|
+
layers: [],
|
|
102
|
+
layerX: [],
|
|
103
|
+
layerW: [],
|
|
104
|
+
canonW: 0,
|
|
105
|
+
canonH: 0,
|
|
106
|
+
chains: new Map(),
|
|
107
|
+
reversed: new Set(),
|
|
108
|
+
lanes: 0,
|
|
109
|
+
laneTop: 0,
|
|
110
|
+
gapLabel: [],
|
|
111
|
+
gapChannels: [],
|
|
112
|
+
leftMargin: 0,
|
|
113
|
+
rightMargin: 0,
|
|
114
|
+
};
|
|
115
|
+
infos.set(c, info);
|
|
116
|
+
let contentReal;
|
|
117
|
+
if (c.kind === "parallel")
|
|
118
|
+
contentReal = arrangeRegions(info, kids, sizes);
|
|
119
|
+
else
|
|
120
|
+
contentReal = arrangeLayers(info, kids, sizes);
|
|
121
|
+
const minW = c.kind === "scxml" ? 0 : head.w;
|
|
122
|
+
info.size = {
|
|
123
|
+
w: Math.max(minW, contentReal.w) + 2 * sp.padding,
|
|
124
|
+
h: info.header + contentReal.h + 2 * sp.padding,
|
|
125
|
+
};
|
|
126
|
+
return info.size;
|
|
127
|
+
};
|
|
128
|
+
/** Parallel regions side by side in a grid (real orientation). */
|
|
129
|
+
const arrangeRegions = (info, kids, sizes) => {
|
|
130
|
+
// regions stack across the layer direction (each region is itself a chain along it);
|
|
131
|
+
// many regions wrap into a grid
|
|
132
|
+
const n = kids.length;
|
|
133
|
+
const stack = n <= 4 ? n : Math.ceil(Math.sqrt(n));
|
|
134
|
+
const cols = down ? stack : Math.ceil(n / stack);
|
|
135
|
+
const rows = Math.ceil(n / cols);
|
|
136
|
+
const colW = Array.from({ length: cols }, (_, ci) => Math.max(0, ...kids.filter((_, i) => i % cols === ci).map((k) => sizes.get(k).w)));
|
|
137
|
+
const rowH = Array.from({ length: rows }, (_, ri) => Math.max(0, ...kids.filter((_, i) => Math.floor(i / cols) === ri).map((k) => sizes.get(k).h)));
|
|
138
|
+
kids.forEach((k, i) => {
|
|
139
|
+
const ci = i % cols;
|
|
140
|
+
const ri = Math.floor(i / cols);
|
|
141
|
+
const x = colW.slice(0, ci).reduce((a, b) => a + b + sp.node, 0);
|
|
142
|
+
const y = rowH.slice(0, ri).reduce((a, b) => a + b + sp.node, 0);
|
|
143
|
+
// regions fill their grid cell, so the dashed separators line up
|
|
144
|
+
const s = sizes.get(k);
|
|
145
|
+
local.set(k, { x, y, w: Math.max(s.w, colW[ci]), h: Math.max(s.h, rowH[ri]) });
|
|
146
|
+
});
|
|
147
|
+
// transitions between regions run along lanes below the grid
|
|
148
|
+
let lanes = 0;
|
|
149
|
+
for (const t of transitions) {
|
|
150
|
+
if (!within(t.source, info.state))
|
|
151
|
+
continue;
|
|
152
|
+
const a = childOf(info.state, drawn(t.source));
|
|
153
|
+
for (const target of t.targets) {
|
|
154
|
+
const b = within(target, info.state) ? childOf(info.state, drawn(target)) : undefined;
|
|
155
|
+
if (a && b && a !== b)
|
|
156
|
+
lanes++;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
info.lanes = lanes;
|
|
160
|
+
return {
|
|
161
|
+
w: colW.reduce((a, b) => a + b, 0) + sp.node * (cols - 1),
|
|
162
|
+
h: rowH.reduce((a, b) => a + b, 0) + sp.node * (rows - 1) + (lanes ? (lanes + 1) * sp.edge : 0),
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
/** Real position (relative to the parent's content origin) and final size of every non-root box. */
|
|
166
|
+
const local = new Map();
|
|
167
|
+
const arrangeLayers = (info, kids, sizes) => {
|
|
168
|
+
const c = info.state;
|
|
169
|
+
let nextId = 0;
|
|
170
|
+
const mk = (kind, w, h, state) => ({
|
|
171
|
+
id: nextId++,
|
|
172
|
+
state,
|
|
173
|
+
kind,
|
|
174
|
+
w,
|
|
175
|
+
h,
|
|
176
|
+
layer: 0,
|
|
177
|
+
order: 0,
|
|
178
|
+
x: 0,
|
|
179
|
+
y: 0,
|
|
180
|
+
});
|
|
181
|
+
for (const k of kids) {
|
|
182
|
+
const s = sizes.get(k);
|
|
183
|
+
const canon = down ? { w: s.h, h: s.w } : s;
|
|
184
|
+
info.nodes.set(k, mk("state", canon.w, canon.h, k));
|
|
185
|
+
}
|
|
186
|
+
const initialTargets = c.initial?.targets.map((t) => childOf(c, t)).filter((t) => !!t && info.nodes.has(t)) ?? [];
|
|
187
|
+
// 1. lift transitions to edges between children
|
|
188
|
+
const weights = new Map();
|
|
189
|
+
const addEdge = (a, b) => {
|
|
190
|
+
const key = `${a.id}→${b.id}`;
|
|
191
|
+
const e = weights.get(key);
|
|
192
|
+
if (e)
|
|
193
|
+
e.weight++;
|
|
194
|
+
else
|
|
195
|
+
weights.set(key, { from: a, to: b, weight: 1 });
|
|
196
|
+
};
|
|
197
|
+
for (const t of transitions) {
|
|
198
|
+
if (!within(t.source, c) || t.source === c)
|
|
199
|
+
continue;
|
|
200
|
+
const a = childOf(c, drawn(t.source));
|
|
201
|
+
for (const target of t.targets) {
|
|
202
|
+
if (!within(target, c) || target === c)
|
|
203
|
+
continue;
|
|
204
|
+
const b = childOf(c, drawn(target));
|
|
205
|
+
if (a && b && a !== b)
|
|
206
|
+
addEdge(info.nodes.get(a), info.nodes.get(b));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const edges = [...weights.values()];
|
|
210
|
+
// 2. break cycles: a greedy feedback arc set (Eades, Lin & Smyth) on edge weights —
|
|
211
|
+
// sinks go last, sources first, otherwise the node with the most (out − in) weight;
|
|
212
|
+
// the initial child gets a slight head start, document order breaks ties
|
|
213
|
+
const stateNodes = [...info.nodes.values()];
|
|
214
|
+
const order = stateNodes;
|
|
215
|
+
const initialSet = new Set(initialTargets.map((t) => info.nodes.get(t)));
|
|
216
|
+
const rank = feedbackArcOrder(stateNodes, edges, initialSet);
|
|
217
|
+
const reversedEdges = new Set(edges.filter((e) => rank.get(e.from) > rank.get(e.to)));
|
|
218
|
+
for (const e of reversedEdges)
|
|
219
|
+
info.reversed.add(`${e.from.state?.id}→${e.to.state?.id}`);
|
|
220
|
+
const dag = edges.filter((e) => !reversedEdges.has(e));
|
|
221
|
+
// 3. layers: longest path
|
|
222
|
+
const indeg = new Map(order.map((n) => [n, 0]));
|
|
223
|
+
for (const e of dag)
|
|
224
|
+
indeg.set(e.to, indeg.get(e.to) + 1);
|
|
225
|
+
const queue = order.filter((n) => indeg.get(n) === 0);
|
|
226
|
+
for (let i = 0; i < queue.length; i++) {
|
|
227
|
+
const n = queue[i];
|
|
228
|
+
for (const e of dag) {
|
|
229
|
+
if (e.from !== n)
|
|
230
|
+
continue;
|
|
231
|
+
e.to.layer = Math.max(e.to.layer, n.layer + 1);
|
|
232
|
+
indeg.set(e.to, indeg.get(e.to) - 1);
|
|
233
|
+
if (indeg.get(e.to) === 0)
|
|
234
|
+
queue.push(e.to);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// a lone initial dot shouldn't cost a whole layer column of its own: keep it, it's small
|
|
238
|
+
const layerCount = Math.max(0, ...order.map((n) => n.layer)) + 1;
|
|
239
|
+
const layers = Array.from({ length: layerCount }, () => []);
|
|
240
|
+
for (const n of order)
|
|
241
|
+
layers[n.layer].push(n);
|
|
242
|
+
// long edges → dummy chains (one per lifted pair, in layer order; reversed edges run backwards
|
|
243
|
+
// through the same kind of corridor), same-layer edges need none
|
|
244
|
+
const segments = [];
|
|
245
|
+
for (const raw of edges) {
|
|
246
|
+
if (raw.from.layer === raw.to.layer)
|
|
247
|
+
continue;
|
|
248
|
+
const e = raw.from.layer < raw.to.layer ? raw : { from: raw.to, to: raw.from, weight: raw.weight };
|
|
249
|
+
if (e.to.layer - e.from.layer <= 1) {
|
|
250
|
+
segments.push(e);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const key = `${e.from.state?.id}→${e.to.state?.id}`;
|
|
254
|
+
if (info.chains.has(key)) {
|
|
255
|
+
const chain = info.chains.get(key);
|
|
256
|
+
for (const d of chain)
|
|
257
|
+
d.h += sp.edge;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const chain = [];
|
|
261
|
+
let prev = e.from;
|
|
262
|
+
for (let l = e.from.layer + 1; l < e.to.layer; l++) {
|
|
263
|
+
const d = mk("dummy", 0, sp.edge * Math.min(3, e.weight));
|
|
264
|
+
d.layer = l;
|
|
265
|
+
layers[l].push(d);
|
|
266
|
+
chain.push(d);
|
|
267
|
+
segments.push({ from: prev, to: d, weight: e.weight });
|
|
268
|
+
prev = d;
|
|
269
|
+
}
|
|
270
|
+
segments.push({ from: prev, to: e.to, weight: e.weight });
|
|
271
|
+
info.chains.set(key, chain);
|
|
272
|
+
}
|
|
273
|
+
// 4. crossing reduction: barycentre sweeps, keep the best
|
|
274
|
+
layers.forEach((layer) => {
|
|
275
|
+
layer.forEach((n, i) => {
|
|
276
|
+
n.order = i;
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
const up = new Map();
|
|
280
|
+
const dn = new Map();
|
|
281
|
+
for (const s of segments) {
|
|
282
|
+
(dn.get(s.from) ?? dn.set(s.from, []).get(s.from)).push(s);
|
|
283
|
+
(up.get(s.to) ?? up.set(s.to, []).get(s.to)).push(s);
|
|
284
|
+
}
|
|
285
|
+
const countCrossings = () => {
|
|
286
|
+
let n = 0;
|
|
287
|
+
for (let l = 0; l + 1 < layers.length; l++) {
|
|
288
|
+
const segs = segments.filter((s) => s.from.layer === l);
|
|
289
|
+
for (let i = 0; i < segs.length; i++)
|
|
290
|
+
for (let j = i + 1; j < segs.length; j++) {
|
|
291
|
+
const a = segs[i];
|
|
292
|
+
const b = segs[j];
|
|
293
|
+
if ((a.from.order - b.from.order) * (a.to.order - b.to.order) < 0)
|
|
294
|
+
n += a.weight * b.weight;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return n;
|
|
298
|
+
};
|
|
299
|
+
const snapshot = () => layers.map((layer) => [...layer]);
|
|
300
|
+
let best = snapshot();
|
|
301
|
+
let bestCrossings = countCrossings();
|
|
302
|
+
for (let sweep = 0; sweep < 8 && bestCrossings > 0; sweep++) {
|
|
303
|
+
const forward = sweep % 2 === 0;
|
|
304
|
+
const range = forward
|
|
305
|
+
? layers.map((_, i) => i).slice(1)
|
|
306
|
+
: layers
|
|
307
|
+
.map((_, i) => i)
|
|
308
|
+
.slice(0, -1)
|
|
309
|
+
.reverse();
|
|
310
|
+
for (const l of range) {
|
|
311
|
+
const layer = layers[l];
|
|
312
|
+
const bary = new Map();
|
|
313
|
+
for (const n of layer) {
|
|
314
|
+
const ns = (forward ? up.get(n) : dn.get(n)) ?? [];
|
|
315
|
+
let sum = 0;
|
|
316
|
+
let w = 0;
|
|
317
|
+
for (const s of ns) {
|
|
318
|
+
sum += (forward ? s.from.order : s.to.order) * s.weight;
|
|
319
|
+
w += s.weight;
|
|
320
|
+
}
|
|
321
|
+
bary.set(n, w ? sum / w : n.order);
|
|
322
|
+
}
|
|
323
|
+
layer.sort((p, q) => bary.get(p) - bary.get(q) || p.order - q.order);
|
|
324
|
+
layer.forEach((n, i) => {
|
|
325
|
+
n.order = i;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
const c2 = countCrossings();
|
|
329
|
+
if (c2 < bestCrossings) {
|
|
330
|
+
bestCrossings = c2;
|
|
331
|
+
best = snapshot();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
layers.splice(0, layers.length, ...best);
|
|
335
|
+
layers.forEach((layer) => {
|
|
336
|
+
layer.forEach((n, i) => {
|
|
337
|
+
n.order = i;
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
info.layers = layers;
|
|
341
|
+
// boxes sending several labelled edges forward grow so every label gets its own port
|
|
342
|
+
const outgoing = new Map();
|
|
343
|
+
for (const t of transitions) {
|
|
344
|
+
if (!within(t.source, c) || t.source === c)
|
|
345
|
+
continue;
|
|
346
|
+
const from = drawn(t.source);
|
|
347
|
+
if (from.parent !== c)
|
|
348
|
+
continue; // only edges leaving the child box itself
|
|
349
|
+
const a = info.nodes.get(from);
|
|
350
|
+
const l = opts.labelSize(t);
|
|
351
|
+
for (const target of t.targets) {
|
|
352
|
+
if (!within(target, c) || target === c)
|
|
353
|
+
continue;
|
|
354
|
+
const b = childOf(c, drawn(target));
|
|
355
|
+
if (b && b !== from && info.nodes.get(b).layer > a.layer)
|
|
356
|
+
outgoing.set(a, (outgoing.get(a) ?? 0) + (down ? l.w : l.h) + sp.label);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
for (const [n, need] of outgoing)
|
|
360
|
+
if (need + sp.label > n.h)
|
|
361
|
+
n.h = need + sp.label;
|
|
362
|
+
// 5a. gaps between layers: room for labels (at the source side) and bend channels
|
|
363
|
+
const gapLabel = Array.from({ length: Math.max(0, layers.length - 1) }, () => 0);
|
|
364
|
+
const gapChannels = Array.from({ length: Math.max(0, layers.length - 1) }, () => 0);
|
|
365
|
+
for (const t of transitions) {
|
|
366
|
+
if (!within(t.source, c) || t.source === c)
|
|
367
|
+
continue;
|
|
368
|
+
const a = childOf(c, drawn(t.source));
|
|
369
|
+
for (const target of t.targets) {
|
|
370
|
+
if (!within(target, c) || target === c)
|
|
371
|
+
continue;
|
|
372
|
+
const b = childOf(c, drawn(target));
|
|
373
|
+
if (!a || !b || a === b)
|
|
374
|
+
continue;
|
|
375
|
+
const na = info.nodes.get(a);
|
|
376
|
+
const nb = info.nodes.get(b);
|
|
377
|
+
if (nb.layer !== na.layer) {
|
|
378
|
+
// forward: label in the gap after the source; reversed: in the gap before it (it leaves backwards)
|
|
379
|
+
const lo = Math.min(na.layer, nb.layer);
|
|
380
|
+
const hi = Math.max(na.layer, nb.layer);
|
|
381
|
+
const lg = nb.layer > na.layer ? na.layer : na.layer - 1;
|
|
382
|
+
gapLabel[lg] = Math.max(gapLabel[lg], labelAlong(t));
|
|
383
|
+
for (let l = lo; l < hi; l++)
|
|
384
|
+
gapChannels[l]++;
|
|
385
|
+
}
|
|
386
|
+
else if (na.layer < layers.length - 1) {
|
|
387
|
+
// same layer: a short loop through the gap after it (or the right margin)
|
|
388
|
+
gapChannels[na.layer]++;
|
|
389
|
+
gapLabel[na.layer] = Math.max(gapLabel[na.layer], labelAlong(t));
|
|
390
|
+
}
|
|
391
|
+
else
|
|
392
|
+
info.rightMargin++;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
info.gapLabel = gapLabel;
|
|
396
|
+
info.gapChannels = gapChannels;
|
|
397
|
+
// the initial dot sits just before its target; targets in the first layer need room for it
|
|
398
|
+
const initialPad = [...initialSet].some((n) => n.layer === 0) ? INITIAL_DOT + sp.edge * 1.6 : 0;
|
|
399
|
+
info.initialPad = initialPad;
|
|
400
|
+
let x = initialPad + (info.leftMargin ? (info.leftMargin + 1) * sp.edge : 0);
|
|
401
|
+
for (let l = 0; l < layers.length; l++) {
|
|
402
|
+
const w = Math.max(0, ...layers[l].map((n) => n.w));
|
|
403
|
+
info.layerX.push(x);
|
|
404
|
+
info.layerW.push(w);
|
|
405
|
+
if (l < layers.length - 1)
|
|
406
|
+
x += w + Math.max(sp.layer, gapLabel[l] + (gapChannels[l] + 1) * sp.edge);
|
|
407
|
+
else
|
|
408
|
+
x += w;
|
|
409
|
+
}
|
|
410
|
+
info.canonW = x + (info.rightMargin ? (info.rightMargin + 1) * sp.edge : 0);
|
|
411
|
+
// 5b. positions across the layers: stack, then pull towards neighbours (order preserved)
|
|
412
|
+
for (const layer of layers) {
|
|
413
|
+
let y = 0;
|
|
414
|
+
for (const n of layer) {
|
|
415
|
+
n.y = y;
|
|
416
|
+
y += n.h + (n.kind === "dummy" ? sp.edge : sp.node);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const gapAfter = (n) => (n.kind === "dummy" ? sp.edge : sp.node);
|
|
420
|
+
/** Move nodes to their desired spots where possible, keeping their order and never overlapping. */
|
|
421
|
+
const settle = (layer, desired) => {
|
|
422
|
+
let prevEnd = Number.NEGATIVE_INFINITY;
|
|
423
|
+
for (const n of layer) {
|
|
424
|
+
n.y = Math.max(desired.get(n) ?? n.y, prevEnd);
|
|
425
|
+
prevEnd = n.y + n.h + gapAfter(n);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
const center = (n) => n.y + n.h / 2;
|
|
429
|
+
for (let pass = 0; pass < 4; pass++) {
|
|
430
|
+
const forward = pass % 2 === 0;
|
|
431
|
+
const range = forward
|
|
432
|
+
? layers.map((_, i) => i).slice(1)
|
|
433
|
+
: layers
|
|
434
|
+
.map((_, i) => i)
|
|
435
|
+
.slice(0, -1)
|
|
436
|
+
.reverse();
|
|
437
|
+
for (const l of range) {
|
|
438
|
+
const desired = new Map();
|
|
439
|
+
for (const n of layers[l]) {
|
|
440
|
+
const ns = (forward ? up.get(n) : dn.get(n)) ?? [];
|
|
441
|
+
if (!ns.length)
|
|
442
|
+
continue;
|
|
443
|
+
let sum = 0;
|
|
444
|
+
let w = 0;
|
|
445
|
+
for (const s of ns) {
|
|
446
|
+
sum += center(forward ? s.from : s.to) * s.weight;
|
|
447
|
+
w += s.weight;
|
|
448
|
+
}
|
|
449
|
+
desired.set(n, sum / w - n.h / 2);
|
|
450
|
+
}
|
|
451
|
+
settle(layers[l], desired);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
// long-edge corridors: dummies lie on the straight line between their chain's ends
|
|
455
|
+
for (const [key, chain] of info.chains) {
|
|
456
|
+
const [fromId, toId] = key.split("→");
|
|
457
|
+
const from = stateNodes.find((n) => n.state?.id === fromId);
|
|
458
|
+
const to = stateNodes.find((n) => n.state?.id === toId);
|
|
459
|
+
if (!from || !to)
|
|
460
|
+
continue;
|
|
461
|
+
const y0 = center(from);
|
|
462
|
+
const y1 = center(to);
|
|
463
|
+
chain.forEach((d, i) => {
|
|
464
|
+
const f = (i + 1) / (chain.length + 1);
|
|
465
|
+
d.y = y0 + (y1 - y0) * f - d.h / 2;
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
for (const layer of layers) {
|
|
469
|
+
if (!layer.some((n) => n.kind === "dummy"))
|
|
470
|
+
continue;
|
|
471
|
+
const desired = new Map(layer.map((n) => [n, n.y]));
|
|
472
|
+
// keep the layer's order consistent with the new positions, then resolve overlaps
|
|
473
|
+
layer.sort((p, q) => p.y + p.h / 2 - (q.y + q.h / 2) || p.order - q.order);
|
|
474
|
+
layer.forEach((n, i) => {
|
|
475
|
+
n.order = i;
|
|
476
|
+
});
|
|
477
|
+
settle(layer, desired);
|
|
478
|
+
}
|
|
479
|
+
const minY = Math.min(...layers.flat().map((n) => n.y));
|
|
480
|
+
for (const n of layers.flat())
|
|
481
|
+
n.y -= minY;
|
|
482
|
+
for (const [l, layer] of layers.entries())
|
|
483
|
+
for (const n of layer)
|
|
484
|
+
n.x = info.layerX[l] + (info.layerW[l] - n.w) / 2;
|
|
485
|
+
info.canonH = Math.max(0, ...layers.flat().map((n) => n.y + n.h));
|
|
486
|
+
const canonTotalH = info.canonH;
|
|
487
|
+
for (const [k, n] of info.nodes) {
|
|
488
|
+
const r = tr({ x: n.x, y: n.y, w: n.w, h: n.h }, down);
|
|
489
|
+
local.set(k, r);
|
|
490
|
+
}
|
|
491
|
+
return down ? { w: canonTotalH, h: info.canonW } : { w: info.canonW, h: canonTotalH };
|
|
492
|
+
};
|
|
493
|
+
layout(root);
|
|
494
|
+
// ── absolute rectangles (real) ──
|
|
495
|
+
const boxes = [];
|
|
496
|
+
const abs = new Map();
|
|
497
|
+
const rootInfo = infos.get(root);
|
|
498
|
+
const rootSize = rootInfo?.size ?? opts.measure(root, false);
|
|
499
|
+
abs.set(root, { x: 0, y: 0, w: rootSize.w, h: rootSize.h });
|
|
500
|
+
const place = (c, depth) => {
|
|
501
|
+
const info = infos.get(c);
|
|
502
|
+
const r = abs.get(c);
|
|
503
|
+
const kind = c.kind === "scxml"
|
|
504
|
+
? "root"
|
|
505
|
+
: collapsed.has(c)
|
|
506
|
+
? "collapsed"
|
|
507
|
+
: c.kind === "parallel"
|
|
508
|
+
? "parallel"
|
|
509
|
+
: c.kind === "final"
|
|
510
|
+
? "final"
|
|
511
|
+
: c.kind === "history"
|
|
512
|
+
? "history"
|
|
513
|
+
: c.parent?.kind === "parallel"
|
|
514
|
+
? "region"
|
|
515
|
+
: isCompound(c)
|
|
516
|
+
? "compound"
|
|
517
|
+
: "atomic";
|
|
518
|
+
boxes.push({ node: c, kind, rect: r, header: info?.header ?? 0, depth });
|
|
519
|
+
if (!info)
|
|
520
|
+
return;
|
|
521
|
+
for (const k of kidsOf(c)) {
|
|
522
|
+
const l = local.get(k);
|
|
523
|
+
const size = infos.get(k)?.size;
|
|
524
|
+
// leaves keep their measured size; containers theirs; parallel regions fill their cell
|
|
525
|
+
const w = c.kind === "parallel" ? l.w : (size?.w ?? l.w);
|
|
526
|
+
const h = c.kind === "parallel" ? l.h : (size?.h ?? l.h);
|
|
527
|
+
abs.set(k, { x: r.x + info.content.x + l.x, y: r.y + info.content.y + l.y, w, h });
|
|
528
|
+
place(k, depth + 1);
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
place(root, 0);
|
|
532
|
+
const routed = routeEdges({ root, down, sp, opts, infos, abs, drawn, transitions, collapsed });
|
|
533
|
+
return {
|
|
534
|
+
width: rootSize.w,
|
|
535
|
+
height: rootSize.h,
|
|
536
|
+
direction,
|
|
537
|
+
boxes,
|
|
538
|
+
edges: routed.edges,
|
|
539
|
+
initials: routed.initials,
|
|
540
|
+
inside: routed.inside,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function routeEdges(ctx) {
|
|
544
|
+
const { down, sp, infos, abs, drawn } = ctx;
|
|
545
|
+
const R = (s) => tr(abs.get(s), down); // canonical rectangle
|
|
546
|
+
const inside = new Map();
|
|
547
|
+
const addInside = (s, t) => {
|
|
548
|
+
const list = inside.get(s) ?? inside.set(s, []).get(s);
|
|
549
|
+
if (!list.includes(t))
|
|
550
|
+
list.push(t);
|
|
551
|
+
};
|
|
552
|
+
/** canonical origin of a container's content area */
|
|
553
|
+
const contentOrigin = (c) => {
|
|
554
|
+
const info = infos.get(c);
|
|
555
|
+
const r = abs.get(c);
|
|
556
|
+
return tp({ x: r.x + info.content.x, y: r.y + info.content.y }, down);
|
|
557
|
+
};
|
|
558
|
+
const layerOf = (c, child) => infos.get(c).nodes.get(child).layer;
|
|
559
|
+
const labelCanon = (t) => {
|
|
560
|
+
const l = ctx.opts.labelSize(t);
|
|
561
|
+
return down ? { w: l.h, h: l.w } : l;
|
|
562
|
+
};
|
|
563
|
+
// ── 1. classify every (transition, target) pair ──
|
|
564
|
+
const pending = [];
|
|
565
|
+
for (const t of ctx.transitions) {
|
|
566
|
+
const from = drawn(t.source);
|
|
567
|
+
if (!t.targets.length) {
|
|
568
|
+
addInside(from, t);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
for (const target of t.targets) {
|
|
572
|
+
const to = drawn(target);
|
|
573
|
+
const base = { transition: t, target, from, to, channels: new Map() };
|
|
574
|
+
if (from === to) {
|
|
575
|
+
if (t.source === target && !ctx.collapsed.has(from))
|
|
576
|
+
pending.push({ ...base, route: "self", container: from });
|
|
577
|
+
else
|
|
578
|
+
addInside(from, t);
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
if (within(from, to)) {
|
|
582
|
+
pending.push({ ...base, route: "to-ancestor", container: to });
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
if (within(to, from)) {
|
|
586
|
+
pending.push({ ...base, route: "to-descendant", container: from });
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
let c = from.parent;
|
|
590
|
+
while (!within(to, c))
|
|
591
|
+
c = c.parent;
|
|
592
|
+
if (infos.get(c).kind === "parallel") {
|
|
593
|
+
pending.push({ ...base, route: "regions", container: c });
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
const a = layerOf(c, childOf(c, from));
|
|
597
|
+
const b = layerOf(c, childOf(c, to));
|
|
598
|
+
pending.push({ ...base, route: b > a ? "forward" : b < a ? "reversed" : "same", container: c });
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
// ── 2. ports: attachments per box side, spread evenly, ordered by where the other end is ──
|
|
602
|
+
const attachments = new Map();
|
|
603
|
+
const attach = (box, side, key) => {
|
|
604
|
+
const a = { side, key, at: 0 };
|
|
605
|
+
const k = `${box.id}|${side}`;
|
|
606
|
+
(attachments.get(k) ?? attachments.set(k, []).get(k)).push(a);
|
|
607
|
+
return a;
|
|
608
|
+
};
|
|
609
|
+
const boxOf = new Map();
|
|
610
|
+
const at = (box, side, key) => {
|
|
611
|
+
const a = attach(box, side, key);
|
|
612
|
+
boxOf.set(a, box);
|
|
613
|
+
return a;
|
|
614
|
+
};
|
|
615
|
+
const cy = (s) => R(s).y + R(s).h / 2;
|
|
616
|
+
const cx = (s) => R(s).x + R(s).w / 2;
|
|
617
|
+
for (const p of pending) {
|
|
618
|
+
switch (p.route) {
|
|
619
|
+
case "forward":
|
|
620
|
+
p.start = at(p.from, "far", cy(p.to));
|
|
621
|
+
p.end = at(p.to, "near", cy(p.from));
|
|
622
|
+
break;
|
|
623
|
+
case "reversed":
|
|
624
|
+
p.start = at(p.from, "near", cy(p.to));
|
|
625
|
+
p.end = at(p.to, "far", cy(p.from));
|
|
626
|
+
break;
|
|
627
|
+
case "same":
|
|
628
|
+
p.start = at(p.from, "far", cy(p.to));
|
|
629
|
+
p.end = at(p.to, "far", cy(p.from));
|
|
630
|
+
break;
|
|
631
|
+
case "regions":
|
|
632
|
+
p.start = at(p.from, "bottom", cx(p.to));
|
|
633
|
+
p.end = at(p.to, "bottom", cx(p.from));
|
|
634
|
+
break;
|
|
635
|
+
case "to-ancestor":
|
|
636
|
+
p.start = at(p.from, "far", cy(p.from));
|
|
637
|
+
break;
|
|
638
|
+
case "to-descendant":
|
|
639
|
+
p.end = at(p.to, "near", Number.NEGATIVE_INFINITY);
|
|
640
|
+
break;
|
|
641
|
+
case "self":
|
|
642
|
+
break;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
for (const list of attachments.values()) {
|
|
646
|
+
list.sort((a, b) => a.key - b.key);
|
|
647
|
+
const box = boxOf.get(list[0]);
|
|
648
|
+
const r = R(box);
|
|
649
|
+
const info = infos.get(box);
|
|
650
|
+
// on containers, keep ports clear of the header band
|
|
651
|
+
const headerAcross = info && !down ? info.header : 0;
|
|
652
|
+
const headerAlong = info && down ? info.header : 0;
|
|
653
|
+
list.forEach((a, i) => {
|
|
654
|
+
const f = (i + 1) / (list.length + 1);
|
|
655
|
+
if (a.side === "bottom")
|
|
656
|
+
a.at = r.x + headerAlong + (r.w - headerAlong) * f;
|
|
657
|
+
else
|
|
658
|
+
a.at = r.y + headerAcross + (r.h - headerAcross) * f;
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
// ── 3. bend channels per gap (in order of the source port) and lanes per parallel state ──
|
|
662
|
+
const channelUse = new Map();
|
|
663
|
+
const useChannel = (p, key) => (channelUse.get(key) ?? channelUse.set(key, []).get(key)).push(p);
|
|
664
|
+
for (const p of pending) {
|
|
665
|
+
const c = p.container;
|
|
666
|
+
if (p.route === "forward" || p.route === "reversed") {
|
|
667
|
+
const a = layerOf(c, childOf(c, p.from));
|
|
668
|
+
const b = layerOf(c, childOf(c, p.to));
|
|
669
|
+
for (let g = Math.min(a, b); g < Math.max(a, b); g++)
|
|
670
|
+
useChannel(p, `${c.id}|${g}`);
|
|
671
|
+
}
|
|
672
|
+
else if (p.route === "same") {
|
|
673
|
+
const a = layerOf(c, childOf(c, p.from));
|
|
674
|
+
useChannel(p, a < infos.get(c).layers.length - 1 ? `${c.id}|${a}` : `${c.id}|R`);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
// order bends in a gap so parallel runs don't cross: an edge going down bends further out the
|
|
678
|
+
// higher it starts; an edge going up, the lower it starts (canonical: "out" = towards the next layer)
|
|
679
|
+
const ends = (p) => {
|
|
680
|
+
const s0 = p.route === "reversed" ? p.end.at : p.start.at;
|
|
681
|
+
const s1 = p.route === "reversed" ? p.start.at : p.end.at;
|
|
682
|
+
return { s0, s1 };
|
|
683
|
+
};
|
|
684
|
+
for (const [key, list] of channelUse) {
|
|
685
|
+
list.sort((p, q) => {
|
|
686
|
+
const a = ends(p);
|
|
687
|
+
const b = ends(q);
|
|
688
|
+
const downA = a.s1 >= a.s0;
|
|
689
|
+
const downB = b.s1 >= b.s0;
|
|
690
|
+
if (downA !== downB)
|
|
691
|
+
return downA ? -1 : 1;
|
|
692
|
+
return downA ? b.s0 - a.s0 || b.s1 - a.s1 : a.s0 - b.s0 || a.s1 - b.s1;
|
|
693
|
+
});
|
|
694
|
+
list.forEach((p, i) => {
|
|
695
|
+
p.channels.set(key, i);
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
const laneUse = new Map();
|
|
699
|
+
for (const p of pending)
|
|
700
|
+
if (p.route === "regions")
|
|
701
|
+
(laneUse.get(p.container) ?? laneUse.set(p.container, []).get(p.container)).push(p);
|
|
702
|
+
for (const list of laneUse.values()) {
|
|
703
|
+
list.sort((p, q) => Math.abs(cx(p.from) - cx(p.to)) - Math.abs(cx(q.from) - cx(q.to)) || p.transition.order - q.transition.order);
|
|
704
|
+
list.forEach((p, i) => {
|
|
705
|
+
p.lane = i;
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
const gapStart = (c, g) => {
|
|
709
|
+
const info = infos.get(c);
|
|
710
|
+
return contentOrigin(c).x + info.layerX[g] + info.layerW[g];
|
|
711
|
+
};
|
|
712
|
+
const channelX = (p, c, g) => {
|
|
713
|
+
const info = infos.get(c);
|
|
714
|
+
const last = info.layers.length - 1;
|
|
715
|
+
if (g >= last) {
|
|
716
|
+
const i = p.channels.get(`${c.id}|R`) ?? 0;
|
|
717
|
+
return contentOrigin(c).x + info.layerX[last] + info.layerW[last] + (i + 1) * sp.edge;
|
|
718
|
+
}
|
|
719
|
+
const i = p.channels.get(`${c.id}|${g}`) ?? 0;
|
|
720
|
+
return gapStart(c, g) + info.gapLabel[g] + (i + 1) * sp.edge;
|
|
721
|
+
};
|
|
722
|
+
// hierarchy-aware exits: leave nested boxes on one side without crossing their siblings
|
|
723
|
+
const visibleKids = (P) => (P.kind === "parallel" ? P.children : [...P.children, ...P.history]);
|
|
724
|
+
const escapes = new Map();
|
|
725
|
+
const gutter = (P, y) => {
|
|
726
|
+
const i = escapes.get(P) ?? 0;
|
|
727
|
+
escapes.set(P, i + 1);
|
|
728
|
+
const PR = R(P);
|
|
729
|
+
const top = contentOrigin(P).y - sp.padding / 2;
|
|
730
|
+
const bottom = PR.y + PR.h - sp.padding / 2;
|
|
731
|
+
return { gy: Math.abs(y - top) <= Math.abs(y - bottom) ? top : bottom, off: sp.edge * (0.6 + (i % 3) * 0.4) };
|
|
732
|
+
};
|
|
733
|
+
/** From `start` on a side of `from` (dir +1 = far, −1 = near) out through the same side of its ancestor `upTo`. */
|
|
734
|
+
const exit = (from, upTo, start, dir) => {
|
|
735
|
+
const pts = [start];
|
|
736
|
+
let cur = from;
|
|
737
|
+
let p = start;
|
|
738
|
+
while (cur !== upTo) {
|
|
739
|
+
const P = cur.parent;
|
|
740
|
+
const PR = R(P);
|
|
741
|
+
const cr = R(cur);
|
|
742
|
+
const border = dir > 0 ? PR.x + PR.w : PR.x;
|
|
743
|
+
const blocked = visibleKids(P).some((k) => {
|
|
744
|
+
if (k === cur)
|
|
745
|
+
return false;
|
|
746
|
+
const r = R(k);
|
|
747
|
+
const beyond = dir > 0 ? r.x >= cr.x + cr.w - 0.5 : r.x + r.w <= cr.x + 0.5;
|
|
748
|
+
return beyond && r.y - 2 < p.y && p.y < r.y + r.h + 2;
|
|
749
|
+
});
|
|
750
|
+
if (blocked) {
|
|
751
|
+
const { gy, off } = gutter(P, p.y);
|
|
752
|
+
const xe = dir > 0 ? cr.x + cr.w + off : cr.x - off;
|
|
753
|
+
pts.push({ x: xe, y: p.y }, { x: xe, y: gy });
|
|
754
|
+
p = { x: border, y: gy };
|
|
755
|
+
}
|
|
756
|
+
else
|
|
757
|
+
p = { x: border, y: p.y };
|
|
758
|
+
pts.push(p);
|
|
759
|
+
cur = P;
|
|
760
|
+
}
|
|
761
|
+
return pts;
|
|
762
|
+
};
|
|
763
|
+
/** Through `upTo`'s side into `end` on the same side of `to` (the reverse of an exit). */
|
|
764
|
+
const enter = (to, upTo, end, dir) => exit(to, upTo, end, dir).reverse();
|
|
765
|
+
/** Horizontal run through layers lo…hi (canonical), bending in each gap's channel and passing dummies. */
|
|
766
|
+
const corridor = (p, c, lo, hi, yStart, yEnd) => {
|
|
767
|
+
const info = infos.get(c);
|
|
768
|
+
const A = childOf(c, p.route === "reversed" ? p.to : p.from);
|
|
769
|
+
const B = childOf(c, p.route === "reversed" ? p.from : p.to);
|
|
770
|
+
const chain = info.chains.get(`${A.id}→${B.id}`) ?? [];
|
|
771
|
+
const pts = [];
|
|
772
|
+
let y = yStart;
|
|
773
|
+
for (let g = lo; g < hi; g++) {
|
|
774
|
+
const x = channelX(p, c, g);
|
|
775
|
+
pts.push({ x, y });
|
|
776
|
+
const next = g + 1 < hi ? chain[g - lo] : undefined;
|
|
777
|
+
y = next ? contentOrigin(c).y + next.y + next.h / 2 : yEnd;
|
|
778
|
+
pts.push({ x, y });
|
|
779
|
+
if (next)
|
|
780
|
+
pts.push({ x: contentOrigin(c).x + info.layerX[g + 1] + info.layerW[g + 1], y });
|
|
781
|
+
}
|
|
782
|
+
return pts;
|
|
783
|
+
};
|
|
784
|
+
const laneY = (c, lane) => {
|
|
785
|
+
const r = R(c);
|
|
786
|
+
return r.y + r.h - sp.padding / 2 - (infos.get(c).lanes - lane) * sp.edge;
|
|
787
|
+
};
|
|
788
|
+
const routed = [];
|
|
789
|
+
for (const p of pending) {
|
|
790
|
+
const S = R(p.from);
|
|
791
|
+
const T = R(p.to);
|
|
792
|
+
let pts = [];
|
|
793
|
+
let prefer = 0; // index of the segment the label would like to sit on
|
|
794
|
+
const c = p.container;
|
|
795
|
+
switch (p.route) {
|
|
796
|
+
case "forward": {
|
|
797
|
+
const A = childOf(c, p.from);
|
|
798
|
+
const B = childOf(c, p.to);
|
|
799
|
+
const out = exit(p.from, A, { x: S.x + S.w, y: p.start.at }, 1);
|
|
800
|
+
const inn = enter(p.to, B, { x: T.x, y: p.end.at }, -1);
|
|
801
|
+
pts = [...out, ...corridor(p, c, layerOf(c, A), layerOf(c, B), out[out.length - 1].y, inn[0].y), ...inn];
|
|
802
|
+
prefer = out.length - 1; // the segment leaving the source's layer
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
case "reversed": {
|
|
806
|
+
// laid out like a forward edge from the target to the source, then reversed
|
|
807
|
+
const A = childOf(c, p.from);
|
|
808
|
+
const B = childOf(c, p.to);
|
|
809
|
+
const out = exit(p.to, B, { x: T.x + T.w, y: p.end.at }, 1);
|
|
810
|
+
const inn = enter(p.from, A, { x: S.x, y: p.start.at }, -1);
|
|
811
|
+
pts = [...out, ...corridor(p, c, layerOf(c, B), layerOf(c, A), out[out.length - 1].y, inn[0].y), ...inn].reverse();
|
|
812
|
+
prefer = inn.length - 1; // the segment leaving the source backwards
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
case "same": {
|
|
816
|
+
const A = childOf(c, p.from);
|
|
817
|
+
const B = childOf(c, p.to);
|
|
818
|
+
const out = exit(p.from, A, { x: S.x + S.w, y: p.start.at }, 1);
|
|
819
|
+
const inn = enter(p.to, B, { x: T.x + T.w, y: p.end.at }, 1);
|
|
820
|
+
const x = channelX(p, c, layerOf(c, A));
|
|
821
|
+
pts = [...out, { x, y: out[out.length - 1].y }, { x, y: inn[0].y }, ...inn];
|
|
822
|
+
prefer = out.length - 1;
|
|
823
|
+
break;
|
|
824
|
+
}
|
|
825
|
+
case "regions": {
|
|
826
|
+
const x0 = p.start.at;
|
|
827
|
+
const x1 = p.end.at;
|
|
828
|
+
const ly = laneY(c, p.lane);
|
|
829
|
+
pts = [
|
|
830
|
+
{ x: x0, y: S.y + S.h },
|
|
831
|
+
{ x: x0, y: ly },
|
|
832
|
+
{ x: x1, y: ly },
|
|
833
|
+
{ x: x1, y: T.y + T.h },
|
|
834
|
+
];
|
|
835
|
+
prefer = 1;
|
|
836
|
+
break;
|
|
837
|
+
}
|
|
838
|
+
case "to-ancestor": {
|
|
839
|
+
const out = exit(p.from, childOf(p.to, p.from), { x: S.x + S.w, y: p.start.at }, 1);
|
|
840
|
+
const last = out[out.length - 1];
|
|
841
|
+
pts = [...out, { x: T.x + T.w - 2, y: last.y }];
|
|
842
|
+
prefer = out.length - 1;
|
|
843
|
+
break;
|
|
844
|
+
}
|
|
845
|
+
case "to-descendant": {
|
|
846
|
+
// from just inside the container, along its top gutter, then down just before the target
|
|
847
|
+
const B = childOf(p.from, p.to);
|
|
848
|
+
const inn = enter(p.to, B, { x: T.x, y: p.end.at }, -1);
|
|
849
|
+
const first = inn[0];
|
|
850
|
+
const gy = contentOrigin(p.from).y - sp.padding / 2;
|
|
851
|
+
const fi = infos.get(p.from);
|
|
852
|
+
const xg = fi.kind === "parallel" ? first.x - sp.edge : contentOrigin(p.from).x + fi.layerX[layerOf(p.from, B)] - sp.edge;
|
|
853
|
+
pts = [{ x: S.x + sp.padding / 3, y: gy }, { x: xg, y: gy }, { x: xg, y: first.y }, ...inn];
|
|
854
|
+
prefer = 0;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
case "self": {
|
|
858
|
+
const out = sp.edge * 1.6;
|
|
859
|
+
const yA = S.y + S.h * 0.3;
|
|
860
|
+
const yB = S.y + S.h * 0.7;
|
|
861
|
+
pts = [
|
|
862
|
+
{ x: S.x + S.w, y: yA },
|
|
863
|
+
{ x: S.x + S.w + out, y: yA },
|
|
864
|
+
{ x: S.x + S.w + out, y: yB },
|
|
865
|
+
{ x: S.x + S.w, y: yB },
|
|
866
|
+
];
|
|
867
|
+
prefer = 1;
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
const kind = p.route === "forward" ? "forward" : p.route === "self" || p.route === "to-ancestor" || p.route === "to-descendant" ? p.route : "back";
|
|
872
|
+
const simple = simplify(pts);
|
|
873
|
+
// map the preferred segment through simplification: nearest segment to the original midpoint
|
|
874
|
+
const a = pts[Math.min(prefer, pts.length - 2)];
|
|
875
|
+
const b = pts[Math.min(prefer + 1, pts.length - 1)];
|
|
876
|
+
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
877
|
+
let best = 0;
|
|
878
|
+
let bestD = Number.POSITIVE_INFINITY;
|
|
879
|
+
for (let i = 0; i + 1 < simple.length; i++) {
|
|
880
|
+
const d = distToSegment(mid, simple[i], simple[i + 1]);
|
|
881
|
+
if (d < bestD) {
|
|
882
|
+
bestD = d;
|
|
883
|
+
best = i;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
const edge = {
|
|
887
|
+
transition: p.transition,
|
|
888
|
+
target: p.target,
|
|
889
|
+
from: p.from,
|
|
890
|
+
to: p.to,
|
|
891
|
+
kind,
|
|
892
|
+
points: simple.map((q) => tp(q, down)),
|
|
893
|
+
};
|
|
894
|
+
routed.push({ edge, points: simple, prefer: best });
|
|
895
|
+
}
|
|
896
|
+
// ── 5. labels (canonical): on their own route — the preferred segment first, then the others,
|
|
897
|
+
// sliding along each; only then stepping aside ──
|
|
898
|
+
const obstacles = [];
|
|
899
|
+
for (const [s, r] of abs) {
|
|
900
|
+
const info = infos.get(s);
|
|
901
|
+
if (s === ctx.root)
|
|
902
|
+
continue;
|
|
903
|
+
if (!info)
|
|
904
|
+
obstacles.push(tr(r, down));
|
|
905
|
+
else if (info.header > 0)
|
|
906
|
+
obstacles.push(tr({ x: r.x, y: r.y, w: r.w, h: info.header }, down));
|
|
907
|
+
}
|
|
908
|
+
const placed = [];
|
|
909
|
+
const free = (r) => !placed.some((o) => overlaps(o, r)) && !obstacles.some((o) => overlaps(o, r));
|
|
910
|
+
const labelled = new Set(); // a transition with several targets is labelled once
|
|
911
|
+
for (const { edge, points, prefer } of routed) {
|
|
912
|
+
const real = ctx.opts.labelSize(edge.transition);
|
|
913
|
+
if (!(real.w > 0 && real.h > 0) || labelled.has(edge.transition))
|
|
914
|
+
continue;
|
|
915
|
+
labelled.add(edge.transition);
|
|
916
|
+
const size = labelCanon(edge.transition);
|
|
917
|
+
const segs = points.slice(1).map((b, i) => [points[i], b]);
|
|
918
|
+
const order = [
|
|
919
|
+
prefer,
|
|
920
|
+
...segs
|
|
921
|
+
.map((_, i) => i)
|
|
922
|
+
.filter((i) => i !== prefer)
|
|
923
|
+
.sort((i, j) => segLen(segs[j]) - segLen(segs[i])),
|
|
924
|
+
];
|
|
925
|
+
let chosen;
|
|
926
|
+
const rectAt = (x, y) => ({ x: x - size.w / 2, y: y - size.h / 2, w: size.w, h: size.h });
|
|
927
|
+
for (const i of order) {
|
|
928
|
+
const [a, b] = segs[i];
|
|
929
|
+
const horizontal = Math.abs(a.y - b.y) < 0.01;
|
|
930
|
+
const room = horizontal ? Math.abs(b.x - a.x) - size.w : Math.abs(b.y - a.y) - size.h;
|
|
931
|
+
if (room < 4)
|
|
932
|
+
continue;
|
|
933
|
+
for (const t of [0.5, 0.3, 0.7, 0.15, 0.85]) {
|
|
934
|
+
const r = rectAt(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
|
|
935
|
+
// keep the label inside the segment's extent
|
|
936
|
+
if (horizontal
|
|
937
|
+
? r.x < Math.min(a.x, b.x) || r.x + r.w > Math.max(a.x, b.x)
|
|
938
|
+
: r.y < Math.min(a.y, b.y) || r.y + r.h > Math.max(a.y, b.y))
|
|
939
|
+
continue;
|
|
940
|
+
if (free(inflate(r, 2))) {
|
|
941
|
+
chosen = r;
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (chosen)
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
if (!chosen) {
|
|
949
|
+
// step aside from the preferred segment, closest first
|
|
950
|
+
const [a, b] = segs[prefer];
|
|
951
|
+
const horizontal = Math.abs(a.y - b.y) < 0.01;
|
|
952
|
+
const mx = (a.x + b.x) / 2;
|
|
953
|
+
const my = (a.y + b.y) / 2;
|
|
954
|
+
const step = (horizontal ? size.h : size.w) + 4;
|
|
955
|
+
for (const k of [1, -1, 2, -2, 3, -3]) {
|
|
956
|
+
const r = horizontal ? rectAt(mx, my + k * step * 0.6) : rectAt(mx + k * step * 0.6, my);
|
|
957
|
+
if (free(inflate(r, 2))) {
|
|
958
|
+
chosen = r;
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
chosen ??= rectAt(mx, my);
|
|
963
|
+
}
|
|
964
|
+
placed.push(chosen);
|
|
965
|
+
edge.label = tr(chosen, down);
|
|
966
|
+
}
|
|
967
|
+
// ── 6. initial markers: a dot just before each (drawn) initial target ──
|
|
968
|
+
const initials = [];
|
|
969
|
+
for (const [c, info] of infos) {
|
|
970
|
+
if (info.kind === "parallel" || !c.initial)
|
|
971
|
+
continue;
|
|
972
|
+
for (const target of c.initial.targets) {
|
|
973
|
+
const to = drawn(target);
|
|
974
|
+
if (!within(to, c) || to === c)
|
|
975
|
+
continue;
|
|
976
|
+
const T = R(to);
|
|
977
|
+
const ty = T.y + Math.min(T.h / 2, 16);
|
|
978
|
+
const dot = { x: T.x - sp.edge * 1.6 - INITIAL_DOT / 2, y: ty };
|
|
979
|
+
initials.push({
|
|
980
|
+
container: c,
|
|
981
|
+
at: tp(dot, down),
|
|
982
|
+
points: [
|
|
983
|
+
{ x: dot.x + INITIAL_DOT / 2, y: ty },
|
|
984
|
+
{ x: T.x, y: ty },
|
|
985
|
+
].map((q) => tp(q, down)),
|
|
986
|
+
target: to,
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
return { edges: routed.map((r) => r.edge), initials, inside };
|
|
991
|
+
}
|
|
992
|
+
const segLen = ([a, b]) => Math.abs(b.x - a.x) + Math.abs(b.y - a.y);
|
|
993
|
+
function distToSegment(p, a, b) {
|
|
994
|
+
const dx = b.x - a.x;
|
|
995
|
+
const dy = b.y - a.y;
|
|
996
|
+
const len = dx * dx + dy * dy;
|
|
997
|
+
const t = len ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len)) : 0;
|
|
998
|
+
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
|
|
999
|
+
}
|
|
1000
|
+
const overlaps = (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;
|
|
1001
|
+
const inflate = (r, d) => ({ x: r.x - d, y: r.y - d, w: r.w + 2 * d, h: r.h + 2 * d });
|
|
1002
|
+
/** Drop repeated points and the middle of collinear runs. */
|
|
1003
|
+
function simplify(points) {
|
|
1004
|
+
const out = [];
|
|
1005
|
+
for (const p of points) {
|
|
1006
|
+
const last = out[out.length - 1];
|
|
1007
|
+
if (last && Math.abs(last.x - p.x) < 0.01 && Math.abs(last.y - p.y) < 0.01)
|
|
1008
|
+
continue;
|
|
1009
|
+
out.push(p);
|
|
1010
|
+
while (out.length >= 3) {
|
|
1011
|
+
const [a, b, c] = out.slice(-3);
|
|
1012
|
+
const collinear = (Math.abs(a.x - b.x) < 0.01 && Math.abs(b.x - c.x) < 0.01) || (Math.abs(a.y - b.y) < 0.01 && Math.abs(b.y - c.y) < 0.01);
|
|
1013
|
+
if (!collinear)
|
|
1014
|
+
break;
|
|
1015
|
+
out.splice(out.length - 2, 1);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
return out;
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Greedy feedback arc set ordering (Eades, Lin & Smyth 1993), weighted: returns
|
|
1022
|
+
* each node's position in an order that keeps most edge weight pointing
|
|
1023
|
+
* forward. Deterministic: ties go to `preferred` nodes, then to input order.
|
|
1024
|
+
*/
|
|
1025
|
+
function feedbackArcOrder(nodes, edges, preferred) {
|
|
1026
|
+
const left = [];
|
|
1027
|
+
const right = [];
|
|
1028
|
+
const remaining = new Set(nodes);
|
|
1029
|
+
const inW = (n) => edges.reduce((s, e) => s + (e.to === n && e.from !== n && remaining.has(e.from) ? e.weight : 0), 0);
|
|
1030
|
+
const outW = (n) => edges.reduce((s, e) => s + (e.from === n && e.to !== n && remaining.has(e.to) ? e.weight : 0), 0);
|
|
1031
|
+
const pick = (pred) => {
|
|
1032
|
+
const cands = nodes.filter((n) => remaining.has(n) && pred(n));
|
|
1033
|
+
return cands.find((n) => preferred.has(n)) ?? cands[0];
|
|
1034
|
+
};
|
|
1035
|
+
while (remaining.size) {
|
|
1036
|
+
let changed = true;
|
|
1037
|
+
while (changed) {
|
|
1038
|
+
changed = false;
|
|
1039
|
+
// sinks (nothing going out) go to the end — but never the preferred start while it has alternatives
|
|
1040
|
+
const sink = nodes.filter((n) => remaining.has(n) && outW(n) === 0 && !(preferred.has(n) && remaining.size > 1)).pop();
|
|
1041
|
+
if (sink !== undefined) {
|
|
1042
|
+
remaining.delete(sink);
|
|
1043
|
+
right.unshift(sink);
|
|
1044
|
+
changed = true;
|
|
1045
|
+
continue;
|
|
1046
|
+
}
|
|
1047
|
+
const source = pick((n) => inW(n) === 0);
|
|
1048
|
+
if (source !== undefined) {
|
|
1049
|
+
remaining.delete(source);
|
|
1050
|
+
left.push(source);
|
|
1051
|
+
changed = true;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
if (!remaining.size)
|
|
1055
|
+
break;
|
|
1056
|
+
let best;
|
|
1057
|
+
let bestDelta = Number.NEGATIVE_INFINITY;
|
|
1058
|
+
for (const n of nodes) {
|
|
1059
|
+
if (!remaining.has(n))
|
|
1060
|
+
continue;
|
|
1061
|
+
const delta = outW(n) - inW(n) + (preferred.has(n) ? 0.5 : 0);
|
|
1062
|
+
if (delta > bestDelta) {
|
|
1063
|
+
bestDelta = delta;
|
|
1064
|
+
best = n;
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
remaining.delete(best);
|
|
1068
|
+
left.push(best);
|
|
1069
|
+
}
|
|
1070
|
+
return new Map([...left, ...right].map((n, i) => [n, i]));
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Which containers to fold so that at most `maxBoxes` boxes are drawn: the
|
|
1074
|
+
* largest containers first (each folds into one box), never the root, and
|
|
1075
|
+
* never a container that holds one of the `keep` states (e.g. the active ones)
|
|
1076
|
+
* while there is anything else to fold. Deterministic.
|
|
1077
|
+
*/
|
|
1078
|
+
export function autoCollapse(root, maxBoxes, keep = new Set()) {
|
|
1079
|
+
const size = new Map();
|
|
1080
|
+
const count = (s) => {
|
|
1081
|
+
let n = 0;
|
|
1082
|
+
for (const k of [...s.children, ...s.history])
|
|
1083
|
+
n += 1 + count(k);
|
|
1084
|
+
size.set(s, n);
|
|
1085
|
+
return n;
|
|
1086
|
+
};
|
|
1087
|
+
let visible = count(root);
|
|
1088
|
+
const collapsed = new Set();
|
|
1089
|
+
const holdsKept = (s) => [...keep].some((k) => k !== s && within(k, s));
|
|
1090
|
+
const candidates = () => {
|
|
1091
|
+
const out = [];
|
|
1092
|
+
const walk = (s) => {
|
|
1093
|
+
for (const k of s.children) {
|
|
1094
|
+
if (!(k.children.length || k.history.length))
|
|
1095
|
+
continue;
|
|
1096
|
+
out.push(k);
|
|
1097
|
+
walk(k);
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
walk(root);
|
|
1101
|
+
return out.filter((c) => ![...collapsed].some((x) => within(c, x)));
|
|
1102
|
+
};
|
|
1103
|
+
while (visible > maxBoxes) {
|
|
1104
|
+
const list = candidates();
|
|
1105
|
+
if (!list.length)
|
|
1106
|
+
break;
|
|
1107
|
+
// prefer containers without kept states; among those the largest (then document order)
|
|
1108
|
+
list.sort((a, b) => Number(holdsKept(a)) - Number(holdsKept(b)) || size.get(b) - size.get(a) || a.order - b.order);
|
|
1109
|
+
const c = list[0];
|
|
1110
|
+
// folding c hides its descendants, minus the ones already hidden by folded containers inside it
|
|
1111
|
+
let hidden = size.get(c);
|
|
1112
|
+
for (const x of collapsed)
|
|
1113
|
+
if (within(x, c)) {
|
|
1114
|
+
hidden -= size.get(x);
|
|
1115
|
+
collapsed.delete(x);
|
|
1116
|
+
}
|
|
1117
|
+
collapsed.add(c);
|
|
1118
|
+
visible -= hidden;
|
|
1119
|
+
}
|
|
1120
|
+
return collapsed;
|
|
1121
|
+
}
|
|
1122
|
+
//# sourceMappingURL=layout.js.map
|