@tinyactors/scxmljs 0.2.0-dev.0 → 0.2.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 +9 -9
- package/dist/explorer/element.d.ts +5 -3
- package/dist/explorer/element.js +523 -262
- package/dist/explorer/element.js.map +1 -1
- package/dist/explorer/strings.d.ts +41 -23
- package/dist/explorer/strings.js +24 -15
- package/dist/explorer/strings.js.map +1 -1
- package/dist/explorer/styles.d.ts +9 -1
- package/dist/explorer/styles.js +291 -150
- package/dist/explorer/styles.js.map +1 -1
- package/dist/explorer.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/model.d.ts +4 -1
- package/dist/model.js +7 -1
- package/dist/model.js.map +1 -1
- package/dist/session.d.ts +22 -9
- package/dist/session.js +53 -13
- package/dist/session.js.map +1 -1
- package/dist/themes/tinyactors.css +1 -0
- package/dist/trusted.d.ts +1 -0
- package/dist/ui/icons.d.ts +31 -0
- package/dist/ui/icons.js +186 -0
- package/dist/ui/icons.js.map +1 -0
- package/dist/ui/notation.d.ts +53 -0
- package/dist/ui/notation.js +117 -0
- package/dist/ui/notation.js.map +1 -0
- package/dist/ui/svg.d.ts +4 -0
- package/dist/ui/svg.js +26 -0
- package/dist/ui/svg.js.map +1 -1
- package/dist/ui/theme.d.ts +1 -1
- package/dist/ui/theme.js +6 -0
- package/dist/ui/theme.js.map +1 -1
- package/dist/view/describe.d.ts +17 -0
- package/dist/view/describe.js +49 -0
- package/dist/view/describe.js.map +1 -0
- package/dist/view/element.d.ts +39 -9
- package/dist/view/element.js +723 -86
- package/dist/view/element.js.map +1 -1
- package/dist/view/layout.d.ts +20 -0
- package/dist/view/layout.js +205 -29
- package/dist/view/layout.js.map +1 -1
- package/dist/view/strings.d.ts +34 -8
- package/dist/view/strings.js +19 -6
- package/dist/view/strings.js.map +1 -1
- package/dist/view/styles.d.ts +1 -1
- package/dist/view/styles.js +107 -21
- package/dist/view/styles.js.map +1 -1
- package/dist/view.d.ts +8 -4
- package/dist/view.js +6 -3
- package/dist/view.js.map +1 -1
- package/package.json +2 -2
package/dist/view/layout.d.ts
CHANGED
|
@@ -71,8 +71,18 @@ export interface LayoutOptions {
|
|
|
71
71
|
labelSize: (transition: TransitionNode) => Size;
|
|
72
72
|
/** Containers drawn as a single box. */
|
|
73
73
|
collapsed?: ReadonlySet<StateNode>;
|
|
74
|
+
/**
|
|
75
|
+
* Transitions not to route: the element lists them in their source's box instead
|
|
76
|
+
* (see `portalTransitions`). They take no part in the layout.
|
|
77
|
+
*/
|
|
78
|
+
portals?: ReadonlySet<TransitionNode>;
|
|
74
79
|
/** Override some of the default gaps. */
|
|
75
80
|
spacing?: Partial<Spacing>;
|
|
81
|
+
/**
|
|
82
|
+
* Width to stay within where the layout has a choice (px): with `direction: "down"`,
|
|
83
|
+
* parallel regions that would sit side by side wrap into fewer columns, down to one.
|
|
84
|
+
*/
|
|
85
|
+
maxWidth?: number;
|
|
76
86
|
}
|
|
77
87
|
/** How a box is drawn: `region` is a child of a `<parallel>`, `collapsed` a folded container. */
|
|
78
88
|
export type BoxKind = "root" | "atomic" | "compound" | "parallel" | "region" | "final" | "history" | "collapsed";
|
|
@@ -145,6 +155,16 @@ export interface ChartLayout {
|
|
|
145
155
|
* what `<scxml-view>` draws; hosts can use it to draw charts themselves.
|
|
146
156
|
*/
|
|
147
157
|
export declare function layoutChart(root: StateNode, opts: LayoutOptions): ChartLayout;
|
|
158
|
+
/** A transition crossing at least this many container borders is listed, not routed. */
|
|
159
|
+
export declare const PORTAL_BORDERS = 3;
|
|
160
|
+
/**
|
|
161
|
+
* Transitions better listed in their source's box ("event → target") than drawn as edges:
|
|
162
|
+
* a container's transitions into its own descendants ("from anywhere in here"), and
|
|
163
|
+
* transitions that cross `PORTAL_BORDERS` or more container borders on their way. Both
|
|
164
|
+
* would otherwise run long lines across the chart. Pass the result to `layoutChart` as
|
|
165
|
+
* `portals`. Deterministic; depends on which containers are `collapsed`.
|
|
166
|
+
*/
|
|
167
|
+
export declare function portalTransitions(root: StateNode, collapsed?: ReadonlySet<StateNode>): Set<TransitionNode>;
|
|
148
168
|
/**
|
|
149
169
|
* Which containers to fold so that at most `maxBoxes` boxes are drawn: the
|
|
150
170
|
* largest containers first (each folds into one box), never the root, and
|
package/dist/view/layout.js
CHANGED
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
import { isCompound } from "../model.js";
|
|
28
28
|
const DEFAULT_SPACING = { node: 18, layer: 44, padding: 14, edge: 10, label: 6 };
|
|
29
29
|
const INITIAL_DOT = 10;
|
|
30
|
+
/** down: how wide a box may grow to put the labels of its outgoing edges side by side */
|
|
31
|
+
const LABEL_ROW = 320;
|
|
30
32
|
// ───────────────────────────────── helpers ─────────────────────────────────
|
|
31
33
|
const within = (s, ancestor) => {
|
|
32
34
|
for (let n = s; n; n = n.parent)
|
|
@@ -70,10 +72,12 @@ export function layoutChart(root, opts) {
|
|
|
70
72
|
// every transition, grouped by the container that routes it (filled while lifting)
|
|
71
73
|
const transitions = [];
|
|
72
74
|
const all = [];
|
|
75
|
+
const portals = opts.portals ?? new Set();
|
|
73
76
|
const walk = (s) => {
|
|
74
77
|
all.push(s);
|
|
75
78
|
for (const t of s.transitions)
|
|
76
|
-
|
|
79
|
+
if (!portals.has(t))
|
|
80
|
+
transitions.push(t);
|
|
77
81
|
for (const k of [...s.children, ...s.history])
|
|
78
82
|
walk(k);
|
|
79
83
|
};
|
|
@@ -131,7 +135,13 @@ export function layoutChart(root, opts) {
|
|
|
131
135
|
// many regions wrap into a grid
|
|
132
136
|
const n = kids.length;
|
|
133
137
|
const stack = n <= 4 ? n : Math.ceil(Math.sqrt(n));
|
|
134
|
-
|
|
138
|
+
let cols = down ? stack : Math.ceil(n / stack);
|
|
139
|
+
// down, in a narrow space: fewer columns (the regions then stack, as rows)
|
|
140
|
+
const gridW = (c) => Array.from({ length: c }, (_, ci) => Math.max(0, ...kids.filter((_, i) => i % c === ci).map((k) => sizes.get(k).w))).reduce((a, b) => a + b, 0) +
|
|
141
|
+
sp.node * (c - 1);
|
|
142
|
+
if (down && opts.maxWidth)
|
|
143
|
+
while (cols > 1 && gridW(cols) + 2 * sp.padding > opts.maxWidth)
|
|
144
|
+
cols--;
|
|
135
145
|
const rows = Math.ceil(n / cols);
|
|
136
146
|
const colW = Array.from({ length: cols }, (_, ci) => Math.max(0, ...kids.filter((_, i) => i % cols === ci).map((k) => sizes.get(k).w)));
|
|
137
147
|
const rowH = Array.from({ length: rows }, (_, ri) => Math.max(0, ...kids.filter((_, i) => Math.floor(i / cols) === ri).map((k) => sizes.get(k).h)));
|
|
@@ -338,7 +348,10 @@ export function layoutChart(root, opts) {
|
|
|
338
348
|
});
|
|
339
349
|
});
|
|
340
350
|
info.layers = layers;
|
|
341
|
-
// boxes sending several labelled edges forward grow so every label gets its own port
|
|
351
|
+
// boxes sending several labelled edges forward grow so every label gets its own port. Across
|
|
352
|
+
// the layers that costs a label's height when they run right, but its whole width when they run
|
|
353
|
+
// down: then a box grows only up to LABEL_ROW (or its own width), and the labels that don't fit
|
|
354
|
+
// side by side stack in the gap below it instead (5a)
|
|
342
355
|
const outgoing = new Map();
|
|
343
356
|
for (const t of transitions) {
|
|
344
357
|
if (!within(t.source, c) || t.source === c)
|
|
@@ -356,12 +369,21 @@ export function layoutChart(root, opts) {
|
|
|
356
369
|
outgoing.set(a, (outgoing.get(a) ?? 0) + (down ? l.w : l.h) + sp.label);
|
|
357
370
|
}
|
|
358
371
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
372
|
+
/** down: how many rows the labels leaving each box need */
|
|
373
|
+
const labelRows = new Map();
|
|
374
|
+
for (const [n, need] of outgoing) {
|
|
375
|
+
const room = down ? Math.max(n.h, LABEL_ROW) : Number.POSITIVE_INFINITY;
|
|
376
|
+
if (down && need + sp.label > room)
|
|
377
|
+
labelRows.set(n, Math.ceil((need + sp.label) / room));
|
|
378
|
+
const want = Math.min(need + sp.label, room);
|
|
379
|
+
if (want > n.h)
|
|
380
|
+
n.h = want;
|
|
381
|
+
}
|
|
362
382
|
// 5a. gaps between layers: room for labels (at the source side) and bend channels
|
|
363
383
|
const gapLabel = Array.from({ length: Math.max(0, layers.length - 1) }, () => 0);
|
|
364
384
|
const gapChannels = Array.from({ length: Math.max(0, layers.length - 1) }, () => 0);
|
|
385
|
+
/** down: labels leaving a box that couldn't put them side by side stack, in rows */
|
|
386
|
+
const stack = (_g, from, t) => labelAlong(t) * (labelRows.get(info.nodes.get(from)) ?? 1);
|
|
365
387
|
for (const t of transitions) {
|
|
366
388
|
if (!within(t.source, c) || t.source === c)
|
|
367
389
|
continue;
|
|
@@ -379,14 +401,15 @@ export function layoutChart(root, opts) {
|
|
|
379
401
|
const lo = Math.min(na.layer, nb.layer);
|
|
380
402
|
const hi = Math.max(na.layer, nb.layer);
|
|
381
403
|
const lg = nb.layer > na.layer ? na.layer : na.layer - 1;
|
|
382
|
-
|
|
404
|
+
if (lg >= 0)
|
|
405
|
+
gapLabel[lg] = Math.max(gapLabel[lg], stack(lg, a, t));
|
|
383
406
|
for (let l = lo; l < hi; l++)
|
|
384
407
|
gapChannels[l]++;
|
|
385
408
|
}
|
|
386
409
|
else if (na.layer < layers.length - 1) {
|
|
387
410
|
// same layer: a short loop through the gap after it (or the right margin)
|
|
388
411
|
gapChannels[na.layer]++;
|
|
389
|
-
gapLabel[na.layer] = Math.max(gapLabel[na.layer],
|
|
412
|
+
gapLabel[na.layer] = Math.max(gapLabel[na.layer], stack(na.layer, a, t));
|
|
390
413
|
}
|
|
391
414
|
else
|
|
392
415
|
info.rightMargin++;
|
|
@@ -479,9 +502,11 @@ export function layoutChart(root, opts) {
|
|
|
479
502
|
const minY = Math.min(...layers.flat().map((n) => n.y));
|
|
480
503
|
for (const n of layers.flat())
|
|
481
504
|
n.y -= minY;
|
|
505
|
+
// boxes start at the beginning of their layer (a short box beside a tall one doesn't float in
|
|
506
|
+
// the middle of it); dummies keep to the middle, where the corridor runs
|
|
482
507
|
for (const [l, layer] of layers.entries())
|
|
483
508
|
for (const n of layer)
|
|
484
|
-
n.x = info.layerX[l] + (info.layerW[l] - n.w) / 2;
|
|
509
|
+
n.x = info.layerX[l] + (n.kind === "dummy" ? (info.layerW[l] - n.w) / 2 : 0);
|
|
485
510
|
info.canonH = Math.max(0, ...layers.flat().map((n) => n.y + n.h));
|
|
486
511
|
const canonTotalH = info.canonH;
|
|
487
512
|
for (const [k, n] of info.nodes) {
|
|
@@ -530,7 +555,7 @@ export function layoutChart(root, opts) {
|
|
|
530
555
|
};
|
|
531
556
|
place(root, 0);
|
|
532
557
|
const routed = routeEdges({ root, down, sp, opts, infos, abs, drawn, transitions, collapsed });
|
|
533
|
-
return {
|
|
558
|
+
return contain({
|
|
534
559
|
width: rootSize.w,
|
|
535
560
|
height: rootSize.h,
|
|
536
561
|
direction,
|
|
@@ -538,7 +563,58 @@ export function layoutChart(root, opts) {
|
|
|
538
563
|
edges: routed.edges,
|
|
539
564
|
initials: routed.initials,
|
|
540
565
|
inside: routed.inside,
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Edges routed around a container's outside, and their labels, can reach past the root's box:
|
|
570
|
+
* move everything so nothing has a negative coordinate, and grow the size to hold it all.
|
|
571
|
+
*/
|
|
572
|
+
function contain(layout) {
|
|
573
|
+
const xs = [0];
|
|
574
|
+
const ys = [0];
|
|
575
|
+
const xe = [layout.width];
|
|
576
|
+
const ye = [layout.height];
|
|
577
|
+
const rect = (r) => {
|
|
578
|
+
xs.push(r.x);
|
|
579
|
+
ys.push(r.y);
|
|
580
|
+
xe.push(r.x + r.w);
|
|
581
|
+
ye.push(r.y + r.h);
|
|
541
582
|
};
|
|
583
|
+
const point = (p, r = 0) => rect({ x: p.x - r, y: p.y - r, w: 2 * r, h: 2 * r });
|
|
584
|
+
for (const e of layout.edges) {
|
|
585
|
+
for (const p of e.points)
|
|
586
|
+
point(p);
|
|
587
|
+
if (e.label)
|
|
588
|
+
rect(e.label);
|
|
589
|
+
}
|
|
590
|
+
for (const m of layout.initials)
|
|
591
|
+
point(m.at, INITIAL_DOT / 2);
|
|
592
|
+
const dx = -Math.min(...xs);
|
|
593
|
+
const dy = -Math.min(...ys);
|
|
594
|
+
if (dx === 0 && dy === 0 && Math.max(...xe) <= layout.width && Math.max(...ye) <= layout.height)
|
|
595
|
+
return layout;
|
|
596
|
+
const move = (p) => {
|
|
597
|
+
p.x += dx;
|
|
598
|
+
p.y += dy;
|
|
599
|
+
};
|
|
600
|
+
for (const b of layout.boxes)
|
|
601
|
+
if (b.kind !== "root")
|
|
602
|
+
move(b.rect);
|
|
603
|
+
for (const e of layout.edges) {
|
|
604
|
+
e.points.forEach(move);
|
|
605
|
+
if (e.label)
|
|
606
|
+
move(e.label);
|
|
607
|
+
}
|
|
608
|
+
for (const m of layout.initials) {
|
|
609
|
+
move(m.at);
|
|
610
|
+
m.points.forEach(move);
|
|
611
|
+
}
|
|
612
|
+
layout.width = Math.max(...xe) + dx;
|
|
613
|
+
layout.height = Math.max(...ye) + dy;
|
|
614
|
+
const root = layout.boxes.find((b) => b.kind === "root");
|
|
615
|
+
if (root)
|
|
616
|
+
root.rect = { x: 0, y: 0, w: layout.width, h: layout.height };
|
|
617
|
+
return layout;
|
|
542
618
|
}
|
|
543
619
|
function routeEdges(ctx) {
|
|
544
620
|
const { down, sp, infos, abs, drawn } = ctx;
|
|
@@ -600,15 +676,15 @@ function routeEdges(ctx) {
|
|
|
600
676
|
}
|
|
601
677
|
// ── 2. ports: attachments per box side, spread evenly, ordered by where the other end is ──
|
|
602
678
|
const attachments = new Map();
|
|
603
|
-
const attach = (box, side, key) => {
|
|
604
|
-
const a = { side, key, at: 0 };
|
|
679
|
+
const attach = (box, side, key, room = 0) => {
|
|
680
|
+
const a = { side, key, at: 0, room };
|
|
605
681
|
const k = `${box.id}|${side}`;
|
|
606
682
|
(attachments.get(k) ?? attachments.set(k, []).get(k)).push(a);
|
|
607
683
|
return a;
|
|
608
684
|
};
|
|
609
685
|
const boxOf = new Map();
|
|
610
|
-
const at = (box, side, key) => {
|
|
611
|
-
const a = attach(box, side, key);
|
|
686
|
+
const at = (box, side, key, room = 0) => {
|
|
687
|
+
const a = attach(box, side, key, room);
|
|
612
688
|
boxOf.set(a, box);
|
|
613
689
|
return a;
|
|
614
690
|
};
|
|
@@ -617,7 +693,8 @@ function routeEdges(ctx) {
|
|
|
617
693
|
for (const p of pending) {
|
|
618
694
|
switch (p.route) {
|
|
619
695
|
case "forward":
|
|
620
|
-
|
|
696
|
+
// down, a label sits across its edge just below the port: its width is the room it needs
|
|
697
|
+
p.start = at(p.from, "far", cy(p.to), down ? labelCanon(p.transition).h + sp.label : 0);
|
|
621
698
|
p.end = at(p.to, "near", cy(p.from));
|
|
622
699
|
break;
|
|
623
700
|
case "reversed":
|
|
@@ -650,13 +727,22 @@ function routeEdges(ctx) {
|
|
|
650
727
|
// on containers, keep ports clear of the header band
|
|
651
728
|
const headerAcross = info && !down ? info.header : 0;
|
|
652
729
|
const headerAlong = info && down ? info.header : 0;
|
|
653
|
-
list.
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
730
|
+
const span = a0(list[0].side === "bottom" ? r.w - headerAlong : r.h - headerAcross);
|
|
731
|
+
const start = list[0].side === "bottom" ? r.x + headerAlong : r.y + headerAcross;
|
|
732
|
+
const rooms = list.map((a) => Math.max(a.room, sp.edge));
|
|
733
|
+
const total = rooms.reduce((x, y) => x + y, 0);
|
|
734
|
+
if (list.some((a) => a.room > 0) && total <= span) {
|
|
735
|
+
// ports in slots as wide as their labels, the row of slots centred on the side
|
|
736
|
+
let x = start + (span - total) / 2;
|
|
737
|
+
list.forEach((a, i) => {
|
|
738
|
+
a.at = x + rooms[i] / 2;
|
|
739
|
+
x += rooms[i];
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
else
|
|
743
|
+
list.forEach((a, i) => {
|
|
744
|
+
a.at = start + span * ((i + 1) / (list.length + 1));
|
|
745
|
+
});
|
|
660
746
|
}
|
|
661
747
|
// ── 3. bend channels per gap (in order of the source port) and lanes per parallel state ──
|
|
662
748
|
const channelUse = new Map();
|
|
@@ -799,7 +885,7 @@ function routeEdges(ctx) {
|
|
|
799
885
|
const out = exit(p.from, A, { x: S.x + S.w, y: p.start.at }, 1);
|
|
800
886
|
const inn = enter(p.to, B, { x: T.x, y: p.end.at }, -1);
|
|
801
887
|
pts = [...out, ...corridor(p, c, layerOf(c, A), layerOf(c, B), out[out.length - 1].y, inn[0].y), ...inn];
|
|
802
|
-
prefer =
|
|
888
|
+
prefer = 0; // at the source: a label further along reads as belonging to something else
|
|
803
889
|
break;
|
|
804
890
|
}
|
|
805
891
|
case "reversed": {
|
|
@@ -809,7 +895,7 @@ function routeEdges(ctx) {
|
|
|
809
895
|
const out = exit(p.to, B, { x: T.x + T.w, y: p.end.at }, 1);
|
|
810
896
|
const inn = enter(p.from, A, { x: S.x, y: p.start.at }, -1);
|
|
811
897
|
pts = [...out, ...corridor(p, c, layerOf(c, B), layerOf(c, A), out[out.length - 1].y, inn[0].y), ...inn].reverse();
|
|
812
|
-
prefer =
|
|
898
|
+
prefer = 0; // (reversed) at the source too
|
|
813
899
|
break;
|
|
814
900
|
}
|
|
815
901
|
case "same": {
|
|
@@ -819,7 +905,7 @@ function routeEdges(ctx) {
|
|
|
819
905
|
const inn = enter(p.to, B, { x: T.x + T.w, y: p.end.at }, 1);
|
|
820
906
|
const x = channelX(p, c, layerOf(c, A));
|
|
821
907
|
pts = [...out, { x, y: out[out.length - 1].y }, { x, y: inn[0].y }, ...inn];
|
|
822
|
-
prefer =
|
|
908
|
+
prefer = 0;
|
|
823
909
|
break;
|
|
824
910
|
}
|
|
825
911
|
case "regions": {
|
|
@@ -839,7 +925,7 @@ function routeEdges(ctx) {
|
|
|
839
925
|
const out = exit(p.from, childOf(p.to, p.from), { x: S.x + S.w, y: p.start.at }, 1);
|
|
840
926
|
const last = out[out.length - 1];
|
|
841
927
|
pts = [...out, { x: T.x + T.w - 2, y: last.y }];
|
|
842
|
-
prefer =
|
|
928
|
+
prefer = 0;
|
|
843
929
|
break;
|
|
844
930
|
}
|
|
845
931
|
case "to-descendant": {
|
|
@@ -915,12 +1001,14 @@ function routeEdges(ctx) {
|
|
|
915
1001
|
labelled.add(edge.transition);
|
|
916
1002
|
const size = labelCanon(edge.transition);
|
|
917
1003
|
const segs = points.slice(1).map((b, i) => [points[i], b]);
|
|
1004
|
+
// the preferred segment, then the ones next to it along the route (a label far from its source
|
|
1005
|
+
// reads as belonging to something else), longer ones first among equals
|
|
918
1006
|
const order = [
|
|
919
1007
|
prefer,
|
|
920
1008
|
...segs
|
|
921
1009
|
.map((_, i) => i)
|
|
922
1010
|
.filter((i) => i !== prefer)
|
|
923
|
-
.sort((i, j) => segLen(segs[j]) - segLen(segs[i])),
|
|
1011
|
+
.sort((i, j) => Math.abs(i - prefer) - Math.abs(j - prefer) || segLen(segs[j]) - segLen(segs[i])),
|
|
924
1012
|
];
|
|
925
1013
|
let chosen;
|
|
926
1014
|
const rectAt = (x, y) => ({ x: x - size.w / 2, y: y - size.h / 2, w: size.w, h: size.h });
|
|
@@ -945,6 +1033,30 @@ function routeEdges(ctx) {
|
|
|
945
1033
|
if (chosen)
|
|
946
1034
|
break;
|
|
947
1035
|
}
|
|
1036
|
+
// beside a segment, just clear of the line: either side, preferred segment first
|
|
1037
|
+
for (const i of chosen ? [] : order) {
|
|
1038
|
+
const [a, b] = segs[i];
|
|
1039
|
+
const horizontal = Math.abs(a.y - b.y) < 0.01;
|
|
1040
|
+
const room = horizontal ? Math.abs(b.x - a.x) - size.w : Math.abs(b.y - a.y) - size.h;
|
|
1041
|
+
if (room < 0)
|
|
1042
|
+
continue;
|
|
1043
|
+
const off = (horizontal ? size.h : size.w) / 2 + 4;
|
|
1044
|
+
for (const t of [0.5, 0.3, 0.7]) {
|
|
1045
|
+
const mx = a.x + (b.x - a.x) * t;
|
|
1046
|
+
const my = a.y + (b.y - a.y) * t;
|
|
1047
|
+
for (const side of [1, -1]) {
|
|
1048
|
+
const r = horizontal ? rectAt(mx, my + side * off) : rectAt(mx + side * off, my);
|
|
1049
|
+
if (free(inflate(r, 2))) {
|
|
1050
|
+
chosen = r;
|
|
1051
|
+
break;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
if (chosen)
|
|
1055
|
+
break;
|
|
1056
|
+
}
|
|
1057
|
+
if (chosen)
|
|
1058
|
+
break;
|
|
1059
|
+
}
|
|
948
1060
|
if (!chosen) {
|
|
949
1061
|
// step aside from the preferred segment, closest first
|
|
950
1062
|
const [a, b] = segs[prefer];
|
|
@@ -989,6 +1101,7 @@ function routeEdges(ctx) {
|
|
|
989
1101
|
}
|
|
990
1102
|
return { edges: routed.map((r) => r.edge), initials, inside };
|
|
991
1103
|
}
|
|
1104
|
+
const a0 = (n) => Math.max(0, n);
|
|
992
1105
|
const segLen = ([a, b]) => Math.abs(b.x - a.x) + Math.abs(b.y - a.y);
|
|
993
1106
|
function distToSegment(p, a, b) {
|
|
994
1107
|
const dx = b.x - a.x;
|
|
@@ -999,8 +1112,20 @@ function distToSegment(p, a, b) {
|
|
|
999
1112
|
}
|
|
1000
1113
|
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
1114
|
const inflate = (r, d) => ({ x: r.x - d, y: r.y - d, w: r.w + 2 * d, h: r.h + 2 * d });
|
|
1002
|
-
/**
|
|
1003
|
-
function simplify(
|
|
1115
|
+
/** Straighten sub-pixel jogs, then drop repeated points and the middle of collinear runs. */
|
|
1116
|
+
function simplify(input) {
|
|
1117
|
+
const points = input.map((p) => ({ ...p }));
|
|
1118
|
+
// a jog of less than a pixel (ports that differ by rounding) only splits a straight line in two:
|
|
1119
|
+
// move the run of points sharing the jogged coordinate onto the previous point's
|
|
1120
|
+
for (let i = 1; i < points.length; i++)
|
|
1121
|
+
for (const axis of ["x", "y"]) {
|
|
1122
|
+
const d = Math.abs(points[i][axis] - points[i - 1][axis]);
|
|
1123
|
+
if (!(d > 0.01 && d < 1))
|
|
1124
|
+
continue;
|
|
1125
|
+
const old = points[i][axis];
|
|
1126
|
+
for (let j = i; j < points.length && Math.abs(points[j][axis] - old) < 0.01; j++)
|
|
1127
|
+
points[j][axis] = points[i - 1][axis];
|
|
1128
|
+
}
|
|
1004
1129
|
const out = [];
|
|
1005
1130
|
for (const p of points) {
|
|
1006
1131
|
const last = out[out.length - 1];
|
|
@@ -1069,6 +1194,57 @@ function feedbackArcOrder(nodes, edges, preferred) {
|
|
|
1069
1194
|
}
|
|
1070
1195
|
return new Map([...left, ...right].map((n, i) => [n, i]));
|
|
1071
1196
|
}
|
|
1197
|
+
/** How many container borders a transition crosses from `from` to `to` (their boxes, as drawn). */
|
|
1198
|
+
function borders(from, to) {
|
|
1199
|
+
let lca = from.parent;
|
|
1200
|
+
while (lca && !within(to, lca))
|
|
1201
|
+
lca = lca.parent;
|
|
1202
|
+
if (!lca)
|
|
1203
|
+
return 0;
|
|
1204
|
+
return from.depth - lca.depth - 1 + (to.depth - lca.depth - 1);
|
|
1205
|
+
}
|
|
1206
|
+
/** A transition crossing at least this many container borders is listed, not routed. */
|
|
1207
|
+
export const PORTAL_BORDERS = 3;
|
|
1208
|
+
/**
|
|
1209
|
+
* Transitions better listed in their source's box ("event → target") than drawn as edges:
|
|
1210
|
+
* a container's transitions into its own descendants ("from anywhere in here"), and
|
|
1211
|
+
* transitions that cross `PORTAL_BORDERS` or more container borders on their way. Both
|
|
1212
|
+
* would otherwise run long lines across the chart. Pass the result to `layoutChart` as
|
|
1213
|
+
* `portals`. Deterministic; depends on which containers are `collapsed`.
|
|
1214
|
+
*/
|
|
1215
|
+
export function portalTransitions(root, collapsed = new Set()) {
|
|
1216
|
+
const drawn = (s) => {
|
|
1217
|
+
let top = s;
|
|
1218
|
+
for (let n = s.parent; n; n = n.parent)
|
|
1219
|
+
if (collapsed.has(n))
|
|
1220
|
+
top = n;
|
|
1221
|
+
return top;
|
|
1222
|
+
};
|
|
1223
|
+
const out = new Set();
|
|
1224
|
+
const walk = (s) => {
|
|
1225
|
+
for (const t of s.transitions) {
|
|
1226
|
+
if (!t.targets.length)
|
|
1227
|
+
continue;
|
|
1228
|
+
const from = drawn(t.source);
|
|
1229
|
+
const far = t.targets.every((target) => {
|
|
1230
|
+
const to = drawn(target);
|
|
1231
|
+
if (to === from)
|
|
1232
|
+
return false;
|
|
1233
|
+
if (within(to, from))
|
|
1234
|
+
return true; // from anywhere inside `from`
|
|
1235
|
+
if (within(from, to))
|
|
1236
|
+
return false; // out to an ancestor: a short hop through its border
|
|
1237
|
+
return borders(from, to) >= PORTAL_BORDERS;
|
|
1238
|
+
});
|
|
1239
|
+
if (far)
|
|
1240
|
+
out.add(t);
|
|
1241
|
+
}
|
|
1242
|
+
for (const k of [...s.children, ...s.history])
|
|
1243
|
+
walk(k);
|
|
1244
|
+
};
|
|
1245
|
+
walk(root);
|
|
1246
|
+
return out;
|
|
1247
|
+
}
|
|
1072
1248
|
/**
|
|
1073
1249
|
* Which containers to fold so that at most `maxBoxes` boxes are drawn: the
|
|
1074
1250
|
* largest containers first (each folds into one box), never the root, and
|