@slotform/react-ui 0.0.0-stage → 0.1.1
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 +44 -2
- package/dist/density.d.ts +4 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +8 -0
- package/dist/density.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/node/canvas.d.ts +3 -0
- package/dist/node/canvas.d.ts.map +1 -0
- package/dist/node/canvas.js +13 -0
- package/dist/node/canvas.js.map +1 -0
- package/dist/node/codegen.d.ts +6 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +70 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/css-escape.d.ts +8 -0
- package/dist/node/css-escape.d.ts.map +1 -0
- package/dist/node/css-escape.js +12 -0
- package/dist/node/css-escape.js.map +1 -0
- package/dist/node/fonts.d.ts +6 -0
- package/dist/node/fonts.d.ts.map +1 -0
- package/dist/node/fonts.js +219 -0
- package/dist/node/fonts.js.map +1 -0
- package/dist/node/lint.d.ts +3 -0
- package/dist/node/lint.d.ts.map +1 -0
- package/dist/node/lint.js +23 -0
- package/dist/node/lint.js.map +1 -0
- package/dist/node/mode.d.ts +9 -0
- package/dist/node/mode.d.ts.map +1 -0
- package/dist/node/mode.js +38 -0
- package/dist/node/mode.js.map +1 -0
- package/dist/node/theme.d.ts +13 -0
- package/dist/node/theme.d.ts.map +1 -0
- package/dist/node/theme.js +160 -0
- package/dist/node/theme.js.map +1 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/guide/acts.md +61 -0
- package/guide/audio.md +77 -0
- package/guide/blocks.md +82 -0
- package/guide/canvas-choosing.md +34 -0
- package/guide/canvas-editing.md +36 -0
- package/guide/canvas-layout.md +46 -0
- package/guide/canvas-opening.md +47 -0
- package/guide/canvas-overview.md +24 -0
- package/guide/canvas-states.md +37 -0
- package/guide/canvas-view.md +46 -0
- package/guide/canvas.md +94 -0
- package/guide/containers.md +60 -0
- package/guide/content.md +40 -0
- package/guide/controls.md +89 -0
- package/guide/density.md +48 -0
- package/guide/dock.md +42 -0
- package/guide/drag.md +38 -0
- package/guide/figures.md +25 -0
- package/guide/forms.md +82 -0
- package/guide/pane-bar.md +11 -0
- package/guide/pane.md +50 -0
- package/guide/reference.md +74 -0
- package/guide/rules.md +109 -0
- package/guide/section.md +29 -0
- package/guide/shell.md +26 -0
- package/guide/shots.md +37 -0
- package/guide/sidebar.md +30 -0
- package/guide/table.md +119 -0
- package/lint/no-arbitrary-value.grit +9 -0
- package/lint/no-class-on-component.grit +12 -0
- package/lint/no-density.grit +8 -0
- package/lint/no-host-look.grit +38 -0
- package/lint/no-img.grit +10 -0
- package/lint/no-navigate-act.grit +22 -0
- package/lint/no-raw-style.grit +8 -0
- package/package.json +150 -3
- package/src/density.ts +9 -0
- package/src/index.ts +503 -0
- package/src/node/canvas.ts +14 -0
- package/src/node/codegen.ts +100 -0
- package/src/node/css-escape.ts +27 -0
- package/src/node/fonts.ts +260 -0
- package/src/node/lint.ts +25 -0
- package/src/node/mode.ts +64 -0
- package/src/node/theme.ts +232 -0
- package/src/types.ts +120 -0
- package/src/ui/atoms/avatar/base.tsx +42 -0
- package/src/ui/atoms/button/glyph.tsx +244 -0
- package/src/ui/atoms/button/link.tsx +81 -0
- package/src/ui/atoms/button/look.ts +32 -0
- package/src/ui/atoms/button/reason.tsx +69 -0
- package/src/ui/atoms/chip/base.tsx +85 -0
- package/src/ui/atoms/count/base.tsx +27 -0
- package/src/ui/atoms/icon/index.tsx +51 -0
- package/src/ui/atoms/message/waiting.tsx +19 -0
- package/src/ui/atoms/prose/painted.tsx +35 -0
- package/src/ui/atoms/row/badges.tsx +226 -0
- package/src/ui/atoms/row/leading.tsx +166 -0
- package/src/ui/atoms/row/lock.tsx +17 -0
- package/src/ui/atoms/row/marks.tsx +79 -0
- package/src/ui/atoms/section/card.tsx +27 -0
- package/src/ui/atoms/spinner/index.tsx +42 -0
- package/src/ui/atoms/status/base.tsx +51 -0
- package/src/ui/atoms/status/change.tsx +29 -0
- package/src/ui/atoms/status/dot.tsx +45 -0
- package/src/ui/atoms/switch/checkbox.tsx +121 -0
- package/src/ui/atoms/turn.tsx +28 -0
- package/src/ui/components/action-bar/base.tsx +276 -0
- package/src/ui/components/action-bar/index.tsx +20 -0
- package/src/ui/components/avatar/index.tsx +21 -0
- package/src/ui/components/banner/index.tsx +207 -0
- package/src/ui/components/banner/pending.tsx +109 -0
- package/src/ui/components/bar-chart/index.tsx +701 -0
- package/src/ui/components/button/base.tsx +214 -0
- package/src/ui/components/button/face.tsx +84 -0
- package/src/ui/components/button/index.tsx +206 -0
- package/src/ui/components/canvas/connection.tsx +26 -0
- package/src/ui/components/canvas/edges.tsx +116 -0
- package/src/ui/components/canvas/elk.ts +260 -0
- package/src/ui/components/canvas/floor.ts +173 -0
- package/src/ui/components/canvas/geometry.ts +772 -0
- package/src/ui/components/canvas/ground.tsx +97 -0
- package/src/ui/components/canvas/group.tsx +156 -0
- package/src/ui/components/canvas/hit.ts +40 -0
- package/src/ui/components/canvas/index.tsx +497 -0
- package/src/ui/components/canvas/label.tsx +53 -0
- package/src/ui/components/canvas/layout.ts +543 -0
- package/src/ui/components/canvas/lift.ts +272 -0
- package/src/ui/components/canvas/look.ts +129 -0
- package/src/ui/components/canvas/node.tsx +689 -0
- package/src/ui/components/canvas/open.ts +243 -0
- package/src/ui/components/canvas/opening.ts +240 -0
- package/src/ui/components/canvas/view.ts +621 -0
- package/src/ui/components/canvas/viewport.ts +417 -0
- package/src/ui/components/canvas/zoom.tsx +113 -0
- package/src/ui/components/card/index.tsx +20 -0
- package/src/ui/components/chip/index.tsx +24 -0
- package/src/ui/components/code/frame.ts +2 -0
- package/src/ui/components/code/index.tsx +234 -0
- package/src/ui/components/columns/index.tsx +51 -0
- package/src/ui/components/count/index.tsx +17 -0
- package/src/ui/components/diff/index.tsx +249 -0
- package/src/ui/components/diff/word.tsx +104 -0
- package/src/ui/components/dock/index.tsx +262 -0
- package/src/ui/components/file-row/index.tsx +213 -0
- package/src/ui/components/form/base.tsx +280 -0
- package/src/ui/components/form/derive.tsx +269 -0
- package/src/ui/components/form/index.tsx +49 -0
- package/src/ui/components/form/saved.tsx +39 -0
- package/src/ui/components/form-field/index.tsx +658 -0
- package/src/ui/components/gallery/index.tsx +83 -0
- package/src/ui/components/gate/index.tsx +183 -0
- package/src/ui/components/image/base.tsx +245 -0
- package/src/ui/components/image/index.tsx +58 -0
- package/src/ui/components/input/base.tsx +246 -0
- package/src/ui/components/input/code.tsx +108 -0
- package/src/ui/components/input/drop.ts +37 -0
- package/src/ui/components/input/file.tsx +154 -0
- package/src/ui/components/input/index.tsx +132 -0
- package/src/ui/components/input/prose.tsx +107 -0
- package/src/ui/components/input/refusal.tsx +30 -0
- package/src/ui/components/item-header/base.tsx +410 -0
- package/src/ui/components/menu/base.tsx +319 -0
- package/src/ui/components/message/attachments.tsx +101 -0
- package/src/ui/components/message/base.tsx +266 -0
- package/src/ui/components/message-input/index.tsx +377 -0
- package/src/ui/components/missing/base.tsx +45 -0
- package/src/ui/components/pane/head.tsx +399 -0
- package/src/ui/components/pane/index.tsx +325 -0
- package/src/ui/components/picker/base.tsx +1551 -0
- package/src/ui/components/picker/index.tsx +224 -0
- package/src/ui/components/picker/list.tsx +423 -0
- package/src/ui/components/picker/segments.tsx +183 -0
- package/src/ui/components/prose/index.tsx +259 -0
- package/src/ui/components/qr-code/index.tsx +65 -0
- package/src/ui/components/row/acts.tsx +126 -0
- package/src/ui/components/row/fact.tsx +54 -0
- package/src/ui/components/row/index.tsx +1370 -0
- package/src/ui/components/row/progress.tsx +38 -0
- package/src/ui/components/row/status.tsx +54 -0
- package/src/ui/components/row/title.ts +7 -0
- package/src/ui/components/rules/index.tsx +290 -0
- package/src/ui/components/section/band.tsx +39 -0
- package/src/ui/components/section/head.tsx +267 -0
- package/src/ui/components/section/index.tsx +409 -0
- package/src/ui/components/section/rows.tsx +356 -0
- package/src/ui/components/sheet/base.tsx +272 -0
- package/src/ui/components/sheet/confirm.tsx +79 -0
- package/src/ui/components/sheet/docked.tsx +218 -0
- package/src/ui/components/sheet/form.tsx +76 -0
- package/src/ui/components/sheet/head.tsx +120 -0
- package/src/ui/components/sheet/index.tsx +45 -0
- package/src/ui/components/sheet/layer.tsx +168 -0
- package/src/ui/components/sheet/outlet.tsx +58 -0
- package/src/ui/components/sheet/task.tsx +41 -0
- package/src/ui/components/shell/cut.tsx +116 -0
- package/src/ui/components/shell/drag.tsx +45 -0
- package/src/ui/components/shell/host.tsx +71 -0
- package/src/ui/components/shell/index.tsx +737 -0
- package/src/ui/components/shell/panes.tsx +76 -0
- package/src/ui/components/shell/preview.ts +230 -0
- package/src/ui/components/shell/rail.tsx +163 -0
- package/src/ui/components/shell/row.ts +11 -0
- package/src/ui/components/shell/switcher.tsx +143 -0
- package/src/ui/components/shell/tree.tsx +252 -0
- package/src/ui/components/slider/base.tsx +139 -0
- package/src/ui/components/slider/index.tsx +57 -0
- package/src/ui/components/stages/index.tsx +198 -0
- package/src/ui/components/stats/count-links.tsx +47 -0
- package/src/ui/components/stats/index.tsx +203 -0
- package/src/ui/components/stats/meter.tsx +213 -0
- package/src/ui/components/stats/parts.ts +10 -0
- package/src/ui/components/step-count/index.tsx +45 -0
- package/src/ui/components/switch/base.tsx +86 -0
- package/src/ui/components/switch/index.tsx +38 -0
- package/src/ui/components/table/columns.tsx +131 -0
- package/src/ui/components/table/index.tsx +2215 -0
- package/src/ui/components/text/index.tsx +37 -0
- package/src/ui/components/thread/index.tsx +503 -0
- package/src/ui/components/thread/latest.tsx +59 -0
- package/src/ui/components/toast/index.tsx +113 -0
- package/src/ui/components/toast/layer.tsx +26 -0
- package/src/ui/components/toolbar/filters.tsx +161 -0
- package/src/ui/components/toolbar/index.tsx +125 -0
- package/src/ui/components/waveform/index.tsx +205 -0
- package/src/ui/globals.css +427 -0
- package/src/ui/lib/act.ts +60 -0
- package/src/ui/lib/age.ts +49 -0
- package/src/ui/lib/bound.ts +16 -0
- package/src/ui/lib/canvas-layout.ts +11 -0
- package/src/ui/lib/caret.ts +9 -0
- package/src/ui/lib/chip-lines.ts +3 -0
- package/src/ui/lib/closed.ts +8 -0
- package/src/ui/lib/code.tsx +184 -0
- package/src/ui/lib/columns.ts +39 -0
- package/src/ui/lib/commit.ts +24 -0
- package/src/ui/lib/container.ts +112 -0
- package/src/ui/lib/copy.ts +29 -0
- package/src/ui/lib/drag.ts +575 -0
- package/src/ui/lib/empty.tsx +85 -0
- package/src/ui/lib/field.ts +39 -0
- package/src/ui/lib/focus.ts +175 -0
- package/src/ui/lib/folds.ts +67 -0
- package/src/ui/lib/follow.ts +220 -0
- package/src/ui/lib/form.ts +19 -0
- package/src/ui/lib/guards.ts +38 -0
- package/src/ui/lib/heading.ts +14 -0
- package/src/ui/lib/ink.ts +26 -0
- package/src/ui/lib/lang.ts +7 -0
- package/src/ui/lib/leave.ts +14 -0
- package/src/ui/lib/lockup.tsx +145 -0
- package/src/ui/lib/measure.ts +22 -0
- package/src/ui/lib/media.ts +63 -0
- package/src/ui/lib/modality.ts +69 -0
- package/src/ui/lib/moment.ts +13 -0
- package/src/ui/lib/more.tsx +76 -0
- package/src/ui/lib/navigate.ts +210 -0
- package/src/ui/lib/noop.ts +3 -0
- package/src/ui/lib/not-found.tsx +40 -0
- package/src/ui/lib/paint.tsx +55 -0
- package/src/ui/lib/parts-line.tsx +54 -0
- package/src/ui/lib/picked.ts +18 -0
- package/src/ui/lib/player-store.ts +38 -0
- package/src/ui/lib/player.ts +139 -0
- package/src/ui/lib/portal.ts +12 -0
- package/src/ui/lib/read.tsx +269 -0
- package/src/ui/lib/region.tsx +49 -0
- package/src/ui/lib/reorder.ts +154 -0
- package/src/ui/lib/rest.ts +16 -0
- package/src/ui/lib/saved.ts +29 -0
- package/src/ui/lib/scramble.tsx +147 -0
- package/src/ui/lib/scrolls.ts +70 -0
- package/src/ui/lib/style.ts +6 -0
- package/src/ui/lib/swap.tsx +107 -0
- package/src/ui/lib/toast.ts +39 -0
- package/src/ui/lib/walk-classes.ts +16 -0
- package/src/ui/lib/window.tsx +376 -0
- package/src/ui/worker.d.ts +4 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CanvasEdge,
|
|
3
|
+
CanvasGroup,
|
|
4
|
+
CanvasPoint,
|
|
5
|
+
} from "@slotform/ui-core/descriptors";
|
|
6
|
+
|
|
7
|
+
// Pure geometry of the canvas, in flow coordinates: no DOM, so the numbers it
|
|
8
|
+
// is given (the spacing roles, the measured sizes) are the caller's.
|
|
9
|
+
|
|
10
|
+
export interface Box {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Size {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface GroupTree {
|
|
23
|
+
// Each group's members, in `holds` order: the nodes and the groups it owns.
|
|
24
|
+
nodes: Map<string, string[]>;
|
|
25
|
+
groups: Map<string, string[]>;
|
|
26
|
+
// The group that owns a node or a group.
|
|
27
|
+
parent: Map<string, string>;
|
|
28
|
+
// The groups no group holds.
|
|
29
|
+
roots: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Who owns whom. A node held by two groups belongs to the first in `groups`
|
|
33
|
+
// order, as does a group; an id that is no node or group is ignored, and a
|
|
34
|
+
// group never takes one of its own ancestors, so the tree has no cycle.
|
|
35
|
+
export function groupTree(
|
|
36
|
+
groups: readonly CanvasGroup[],
|
|
37
|
+
nodeIds: ReadonlySet<string>,
|
|
38
|
+
): GroupTree {
|
|
39
|
+
const known = new Set(groups.map((group) => group.id));
|
|
40
|
+
const parent = new Map<string, string>();
|
|
41
|
+
const nodes = new Map<string, string[]>();
|
|
42
|
+
const held = new Map<string, string[]>();
|
|
43
|
+
const within = (id: string, ancestor: string): boolean => {
|
|
44
|
+
for (let at: string | undefined = id; at; at = parent.get(at)) {
|
|
45
|
+
if (at === ancestor) return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
};
|
|
49
|
+
for (const group of groups) {
|
|
50
|
+
nodes.set(group.id, []);
|
|
51
|
+
held.set(group.id, []);
|
|
52
|
+
}
|
|
53
|
+
for (const group of groups) {
|
|
54
|
+
for (const id of group.holds) {
|
|
55
|
+
if (parent.has(id)) continue;
|
|
56
|
+
if (nodeIds.has(id)) {
|
|
57
|
+
parent.set(id, group.id);
|
|
58
|
+
nodes.get(group.id)?.push(id);
|
|
59
|
+
} else if (known.has(id) && !within(group.id, id)) {
|
|
60
|
+
parent.set(id, group.id);
|
|
61
|
+
held.get(group.id)?.push(id);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
nodes,
|
|
67
|
+
groups: held,
|
|
68
|
+
parent,
|
|
69
|
+
roots: groups.map((group) => group.id).filter((id) => !parent.has(id)),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The groups that hold no present node and no group: leaves of the graph,
|
|
74
|
+
// which the layout places and an edge may name. A group holding only such a
|
|
75
|
+
// group is no leaf; it frames them.
|
|
76
|
+
export function emptyGroups(
|
|
77
|
+
groups: readonly CanvasGroup[],
|
|
78
|
+
nodeIds: ReadonlySet<string>,
|
|
79
|
+
): string[] {
|
|
80
|
+
const tree = groupTree(groups, nodeIds);
|
|
81
|
+
return groups
|
|
82
|
+
.map((group) => group.id)
|
|
83
|
+
.filter(
|
|
84
|
+
(id) =>
|
|
85
|
+
tree.nodes.get(id)?.length === 0 && tree.groups.get(id)?.length === 0,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Where the empty groups stand when no layout places them (every node carries
|
|
90
|
+
// a position): one row of `size` boxes `drop` below the nodes' bounds,
|
|
91
|
+
// left-aligned with them and `gap` apart, in the order of `ids`. No node box,
|
|
92
|
+
// no row.
|
|
93
|
+
export function hollowRow(
|
|
94
|
+
ids: readonly string[],
|
|
95
|
+
nodes: Iterable<Box>,
|
|
96
|
+
size: Size,
|
|
97
|
+
{ gap, drop }: { gap: number; drop: number },
|
|
98
|
+
): Map<string, Box> {
|
|
99
|
+
let left = Number.POSITIVE_INFINITY;
|
|
100
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
101
|
+
for (const box of nodes) {
|
|
102
|
+
left = Math.min(left, box.x);
|
|
103
|
+
bottom = Math.max(bottom, box.y + box.height);
|
|
104
|
+
}
|
|
105
|
+
const out = new Map<string, Box>();
|
|
106
|
+
if (left === Number.POSITIVE_INFINITY) return out;
|
|
107
|
+
ids.forEach((id, index) => {
|
|
108
|
+
out.set(id, {
|
|
109
|
+
x: left + index * (size.width + gap),
|
|
110
|
+
y: bottom + drop,
|
|
111
|
+
...size,
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// A group's frame: the rectangle of everything it holds, grown by `pad` on
|
|
118
|
+
// three sides and by `pad + head` on top, its left side by `left` where the
|
|
119
|
+
// group has an entry (`leftPads`). A group with a box of its own (an empty
|
|
120
|
+
// group, placed by the layout) is that box. A group holding no box has no
|
|
121
|
+
// frame.
|
|
122
|
+
// `reach` holds the right edge a group's content must reach (before its
|
|
123
|
+
// padding), where a back edge's corridor and label stand inside it.
|
|
124
|
+
function groupBoxes(
|
|
125
|
+
groups: readonly CanvasGroup[],
|
|
126
|
+
boxes: ReadonlyMap<string, Box>,
|
|
127
|
+
{
|
|
128
|
+
pad,
|
|
129
|
+
head,
|
|
130
|
+
left: lefts = new Map(),
|
|
131
|
+
}: { pad: number; head: number; left?: ReadonlyMap<string, number> },
|
|
132
|
+
reach: ReadonlyMap<string, number> = new Map(),
|
|
133
|
+
): Map<string, Box> {
|
|
134
|
+
const tree = groupTree(groups, new Set(boxes.keys()));
|
|
135
|
+
const out = new Map<string, Box>();
|
|
136
|
+
const frame = (id: string): Box | undefined => {
|
|
137
|
+
if (out.has(id)) return out.get(id);
|
|
138
|
+
const own = boxes.get(id);
|
|
139
|
+
if (own) {
|
|
140
|
+
out.set(id, own);
|
|
141
|
+
return own;
|
|
142
|
+
}
|
|
143
|
+
const parts = [
|
|
144
|
+
...(tree.nodes.get(id) ?? []).map((node) => boxes.get(node)),
|
|
145
|
+
...(tree.groups.get(id) ?? []).map(frame),
|
|
146
|
+
].filter((box): box is Box => box !== undefined);
|
|
147
|
+
if (parts.length === 0) return undefined;
|
|
148
|
+
const left = Math.min(...parts.map((box) => box.x));
|
|
149
|
+
const top = Math.min(...parts.map((box) => box.y));
|
|
150
|
+
const right = Math.max(
|
|
151
|
+
...parts.map((box) => box.x + box.width),
|
|
152
|
+
reach.get(id) ?? -Infinity,
|
|
153
|
+
);
|
|
154
|
+
const bottom = Math.max(...parts.map((box) => box.y + box.height));
|
|
155
|
+
const inset = Math.max(lefts.get(id) ?? pad, pad);
|
|
156
|
+
const box = {
|
|
157
|
+
x: left - inset,
|
|
158
|
+
y: top - pad - head,
|
|
159
|
+
width: right - left + inset + pad,
|
|
160
|
+
height: bottom - top + 2 * pad + head,
|
|
161
|
+
};
|
|
162
|
+
out.set(id, box);
|
|
163
|
+
return box;
|
|
164
|
+
};
|
|
165
|
+
for (const group of groups) frame(group.id);
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Where the boxes stand when every group's head and padding are `grow` times
|
|
170
|
+
// what they take at zoom 1 (under the text floor they draw at their own size on
|
|
171
|
+
// screen): a frame grows by `(head + pad) * (grow - 1)` over what it holds and
|
|
172
|
+
// by `pad * (grow - 1)` under it, and an empty group, its own frame, by both.
|
|
173
|
+
// Each box moves down by what every other frame grew above its centre: the top
|
|
174
|
+
// of one that starts above it, and the foot of one that ends above it, so a
|
|
175
|
+
// grown head stands in the room it grew by and never over a node the layout
|
|
176
|
+
// placed above its group. At `grow` 1 or under, the boxes as given.
|
|
177
|
+
export function spreadBoxes(
|
|
178
|
+
boxes: ReadonlyMap<string, Box>,
|
|
179
|
+
groups: readonly CanvasGroup[],
|
|
180
|
+
{
|
|
181
|
+
pad,
|
|
182
|
+
head,
|
|
183
|
+
left,
|
|
184
|
+
grow,
|
|
185
|
+
}: {
|
|
186
|
+
pad: number;
|
|
187
|
+
head: number;
|
|
188
|
+
left?: ReadonlyMap<string, number>;
|
|
189
|
+
grow: number;
|
|
190
|
+
},
|
|
191
|
+
): Map<string, Box> {
|
|
192
|
+
if (grow <= 1) return new Map(boxes);
|
|
193
|
+
const top = (head + pad) * (grow - 1);
|
|
194
|
+
const foot = pad * (grow - 1);
|
|
195
|
+
const frames = groupBoxes(groups, boxes, { pad, head, left });
|
|
196
|
+
const out = new Map<string, Box>();
|
|
197
|
+
for (const [id, box] of boxes) {
|
|
198
|
+
const centre = box.y + box.height / 2;
|
|
199
|
+
let by = 0;
|
|
200
|
+
for (const [group, frame] of frames) {
|
|
201
|
+
if (group === id) continue;
|
|
202
|
+
if (frame.y < centre) by += top;
|
|
203
|
+
if (frame.y + frame.height < centre) by += foot;
|
|
204
|
+
}
|
|
205
|
+
out.set(id, {
|
|
206
|
+
...box,
|
|
207
|
+
y: box.y + by,
|
|
208
|
+
height: box.height + (frames.has(id) ? top + foot : 0),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// A group's left padding, a whole number of pixels, where its head text needs
|
|
215
|
+
// more than `pad`: the text
|
|
216
|
+
// starts at the frame's left edge and ends `reach` from it, and an edge enters
|
|
217
|
+
// the group at a node's centre, at least half a node's `width` in from the
|
|
218
|
+
// content's left, so the padding grows until the text ends a `pair` before it.
|
|
219
|
+
export function leftPads(
|
|
220
|
+
groups: readonly CanvasGroup[],
|
|
221
|
+
reach: ReadonlyMap<string, number>,
|
|
222
|
+
{ pad, pair, width }: { pad: number; pair: number; width: number },
|
|
223
|
+
): Map<string, number> {
|
|
224
|
+
const out = new Map<string, number>();
|
|
225
|
+
for (const { id } of groups) {
|
|
226
|
+
const need = (reach.get(id) ?? 0) + pair - width / 2;
|
|
227
|
+
if (need > pad) out.set(id, Math.ceil(need));
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const headBand = (frame: Box, head: number): Box => ({
|
|
233
|
+
...frame,
|
|
234
|
+
height: head,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
export const overlaps = (a: Box, b: Box): boolean =>
|
|
238
|
+
a.x < b.x + b.width &&
|
|
239
|
+
b.x < a.x + a.width &&
|
|
240
|
+
a.y < b.y + b.height &&
|
|
241
|
+
b.y < a.y + a.height;
|
|
242
|
+
|
|
243
|
+
// The arrowhead's box, on its edge's end.
|
|
244
|
+
export const ARROW = 8;
|
|
245
|
+
|
|
246
|
+
// The marker's reference point along its length: the point that stands on the
|
|
247
|
+
// route's end. The tip is `ARROW - ARROW_REF` past it.
|
|
248
|
+
export const ARROW_REF = 7;
|
|
249
|
+
|
|
250
|
+
// Where a stroke stands within its pixel: `crisp` puts a point on a pixel's
|
|
251
|
+
// centre, half a pixel under a whole coordinate.
|
|
252
|
+
const PIXEL_CENTRE = 0.5;
|
|
253
|
+
|
|
254
|
+
interface Leg {
|
|
255
|
+
x: number;
|
|
256
|
+
top: number;
|
|
257
|
+
bottom: number;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// An edge's route: its points, the box its label chip stands in (when it
|
|
261
|
+
// carries one) and the box its arrowhead fills.
|
|
262
|
+
export interface Route {
|
|
263
|
+
points: CanvasPoint[];
|
|
264
|
+
label?: Box;
|
|
265
|
+
arrow: Box;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export interface Routes {
|
|
269
|
+
routes: Map<string, Route>;
|
|
270
|
+
// Each group's frame once the back edges inside it are placed.
|
|
271
|
+
frames: Map<string, Box>;
|
|
272
|
+
// Everything drawn: boxes, frames, route points and labels.
|
|
273
|
+
bounds: Box;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
interface RouteInput {
|
|
277
|
+
boxes: ReadonlyMap<string, Box>;
|
|
278
|
+
edges: readonly Pick<CanvasEdge, "id" | "from" | "to">[];
|
|
279
|
+
// The ids of the edges that go back (`backEdges`).
|
|
280
|
+
back: readonly string[];
|
|
281
|
+
groups: readonly CanvasGroup[];
|
|
282
|
+
// The chip an edge carries, by edge id.
|
|
283
|
+
labels: ReadonlyMap<string, Size>;
|
|
284
|
+
// A group head's height and a frame's padding, its left side's where the
|
|
285
|
+
// head text needs more.
|
|
286
|
+
head: number;
|
|
287
|
+
pad: number;
|
|
288
|
+
left?: ReadonlyMap<string, number>;
|
|
289
|
+
// The corner radius and the unit every distance off a line is a multiple of.
|
|
290
|
+
pair: number;
|
|
291
|
+
// Whether each node draws an in port, and the ring's drawn size. The ring is
|
|
292
|
+
// painted over the edges, centred on its node's top edge, so a forward
|
|
293
|
+
// route ends above that edge by the ring's radius, the tip's overhang past
|
|
294
|
+
// the route's end and a pixel centre: drawn, the arrowhead's tip stands on
|
|
295
|
+
// the ring's outer top edge, a half pixel clear.
|
|
296
|
+
ports: boolean;
|
|
297
|
+
port: number;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const same = (a: CanvasPoint, b: CanvasPoint) => a.x === b.x && a.y === b.y;
|
|
301
|
+
|
|
302
|
+
// The points with a repeat and a point on its neighbours' line taken out, until
|
|
303
|
+
// none is left: a route that doubles back on itself collapses to its ends.
|
|
304
|
+
export function cleanPoints(points: readonly CanvasPoint[]): CanvasPoint[] {
|
|
305
|
+
let out = [...points];
|
|
306
|
+
let changed = true;
|
|
307
|
+
while (changed) {
|
|
308
|
+
changed = false;
|
|
309
|
+
for (let i = 1; i < out.length; i++) {
|
|
310
|
+
const before = out[i - 1];
|
|
311
|
+
const at = out[i];
|
|
312
|
+
const after = out[i + 1];
|
|
313
|
+
if (!before || !at) continue;
|
|
314
|
+
const repeated = same(before, at);
|
|
315
|
+
const straight =
|
|
316
|
+
after !== undefined &&
|
|
317
|
+
(at.x - before.x) * (after.y - at.y) ===
|
|
318
|
+
(at.y - before.y) * (after.x - at.x);
|
|
319
|
+
if (repeated || straight) {
|
|
320
|
+
out = out.filter((_, index) => index !== i);
|
|
321
|
+
changed = true;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// A forward route between two points, with no collision check: straight when
|
|
330
|
+
// they share an x, bent in the middle of the span when `to` is not at least
|
|
331
|
+
// `2 * pair` below `from`, else bent at `bend` (a `pair` under `from` unless
|
|
332
|
+
// the router knows the gap).
|
|
333
|
+
export function bendRoute(
|
|
334
|
+
from: CanvasPoint,
|
|
335
|
+
to: CanvasPoint,
|
|
336
|
+
pair: number,
|
|
337
|
+
bend: number = from.y + pair,
|
|
338
|
+
): CanvasPoint[] {
|
|
339
|
+
if (from.x === to.x) return [from, to];
|
|
340
|
+
const y = to.y < from.y + 2 * pair ? (from.y + to.y) / 2 : bend;
|
|
341
|
+
return [from, { x: from.x, y }, { x: to.x, y }, to];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function verticalLegs(points: readonly CanvasPoint[]): Leg[] {
|
|
345
|
+
const legs: Leg[] = [];
|
|
346
|
+
points.forEach((point, index) => {
|
|
347
|
+
const next = points[index + 1];
|
|
348
|
+
if (next && next.x === point.x && next.y !== point.y)
|
|
349
|
+
legs.push({
|
|
350
|
+
x: point.x,
|
|
351
|
+
top: Math.min(point.y, next.y),
|
|
352
|
+
bottom: Math.max(point.y, next.y),
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
return legs;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Whether the open segment between two points passes through the inside of a
|
|
359
|
+
// box: touching its edge is no crossing.
|
|
360
|
+
export function crosses(a: CanvasPoint, b: CanvasPoint, box: Box): boolean {
|
|
361
|
+
return (
|
|
362
|
+
Math.max(a.x, b.x) > box.x &&
|
|
363
|
+
Math.min(a.x, b.x) < box.x + box.width &&
|
|
364
|
+
Math.max(a.y, b.y) > box.y &&
|
|
365
|
+
Math.min(a.y, b.y) < box.y + box.height
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// The stretches of a leg no other leg draws on.
|
|
370
|
+
function alone(leg: Leg, others: readonly Leg[]): Leg[] {
|
|
371
|
+
const taken = others
|
|
372
|
+
.filter((o) => o.x === leg.x && o.top < leg.bottom && o.bottom > leg.top)
|
|
373
|
+
.sort((a, b) => a.top - b.top);
|
|
374
|
+
const out: Leg[] = [];
|
|
375
|
+
let at = leg.top;
|
|
376
|
+
for (const o of taken) {
|
|
377
|
+
if (o.top > at) out.push({ x: leg.x, top: at, bottom: o.top });
|
|
378
|
+
at = Math.max(at, o.bottom);
|
|
379
|
+
}
|
|
380
|
+
if (at < leg.bottom) out.push({ x: leg.x, top: at, bottom: leg.bottom });
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const length = (leg: Leg) => leg.bottom - leg.top;
|
|
385
|
+
|
|
386
|
+
// Every route of the graph, from the final boxes. Back edges come first, so
|
|
387
|
+
// the frames they grow are the ones a forward edge is checked against.
|
|
388
|
+
//
|
|
389
|
+
// A forward edge leaves its source's bottom centre, bends `pair` below it and
|
|
390
|
+
// runs down its target's column into its target's top centre. When that last
|
|
391
|
+
// leg would cross a node or another group's head, it bends `pair` above the
|
|
392
|
+
// target instead. A back edge leaves its source's right side, runs up a
|
|
393
|
+
// corridor past everything it meets and enters its target's right side.
|
|
394
|
+
export function routeEdges(input: RouteInput): Routes {
|
|
395
|
+
const { boxes, edges, back, groups, labels, head, pad, left, pair } = input;
|
|
396
|
+
// A back edge, a self loop included, enters its target's right side, where
|
|
397
|
+
// no ring stands.
|
|
398
|
+
const ring = input.ports
|
|
399
|
+
? input.port / 2 + (ARROW - ARROW_REF) + PIXEL_CENTRE
|
|
400
|
+
: 0;
|
|
401
|
+
// An empty group takes no port, so its edges end on its frame.
|
|
402
|
+
const framed = new Set(groups.map((group) => group.id));
|
|
403
|
+
const tree = groupTree(groups, new Set(boxes.keys()));
|
|
404
|
+
const reach = new Map<string, number>();
|
|
405
|
+
let frames = groupBoxes(groups, boxes, { pad, head, left }, reach);
|
|
406
|
+
const routes = new Map<string, Route>();
|
|
407
|
+
|
|
408
|
+
// The groups that hold an id, nearest first.
|
|
409
|
+
const holders = (id: string): string[] => {
|
|
410
|
+
const out: string[] = [];
|
|
411
|
+
for (let at = tree.parent.get(id); at; at = tree.parent.get(at))
|
|
412
|
+
out.push(at);
|
|
413
|
+
return out;
|
|
414
|
+
};
|
|
415
|
+
const depth = (group: string | undefined) =>
|
|
416
|
+
group === undefined ? -1 : holders(group).length;
|
|
417
|
+
|
|
418
|
+
// Back edges: those inside one group first, the deepest group first, then
|
|
419
|
+
// the rest; each in `back` order.
|
|
420
|
+
const byId = new Map(edges.map((edge) => [edge.id, edge]));
|
|
421
|
+
const placed = back.flatMap((id) => {
|
|
422
|
+
const edge = byId.get(id);
|
|
423
|
+
if (!edge || !boxes.has(edge.from) || !boxes.has(edge.to)) return [];
|
|
424
|
+
const ends = holders(edge.to);
|
|
425
|
+
const scope = holders(edge.from).find((group) => ends.includes(group));
|
|
426
|
+
return [{ edge, scope }];
|
|
427
|
+
});
|
|
428
|
+
const order = [
|
|
429
|
+
...placed
|
|
430
|
+
.filter(({ scope }) => scope !== undefined)
|
|
431
|
+
.sort((a, b) => depth(b.scope) - depth(a.scope)),
|
|
432
|
+
...placed.filter(({ scope }) => scope === undefined),
|
|
433
|
+
];
|
|
434
|
+
|
|
435
|
+
const corridors: { x: number; top: number; bottom: number; width: number }[] =
|
|
436
|
+
[];
|
|
437
|
+
for (const { edge, scope } of order) {
|
|
438
|
+
const source = boxes.get(edge.from);
|
|
439
|
+
const target = boxes.get(edge.to);
|
|
440
|
+
if (!source || !target) continue;
|
|
441
|
+
const loop = edge.from === edge.to;
|
|
442
|
+
const fromY = source.y + source.height / (loop ? 4 : 2);
|
|
443
|
+
const toY = loop
|
|
444
|
+
? source.y + (source.height * 3) / 4
|
|
445
|
+
: target.y + target.height / 2;
|
|
446
|
+
const top = Math.min(fromY, toY);
|
|
447
|
+
const bottom = Math.max(fromY, toY);
|
|
448
|
+
const label = labels.get(edge.id);
|
|
449
|
+
const width = label?.width ?? 0;
|
|
450
|
+
const meets = (box: Box) => box.y <= bottom && box.y + box.height >= top;
|
|
451
|
+
|
|
452
|
+
// What the corridor must clear: inside a group, the nodes it holds and
|
|
453
|
+
// the groups nested in it; elsewhere every node and every frame.
|
|
454
|
+
const clears = ([id]: [string, Box]) =>
|
|
455
|
+
scope === undefined || holders(id).includes(scope);
|
|
456
|
+
let x =
|
|
457
|
+
Math.max(
|
|
458
|
+
source.x + source.width,
|
|
459
|
+
target.x + target.width,
|
|
460
|
+
...[...boxes, ...frames]
|
|
461
|
+
.filter(clears)
|
|
462
|
+
.map(([, box]) => box)
|
|
463
|
+
.filter(meets)
|
|
464
|
+
.map((box) => box.x + box.width),
|
|
465
|
+
) +
|
|
466
|
+
// A stub out of the source and into the target holds the arrowhead and a
|
|
467
|
+
// corner.
|
|
468
|
+
(ARROW + pair);
|
|
469
|
+
for (const c of corridors) {
|
|
470
|
+
if (c.top <= bottom && c.bottom >= top)
|
|
471
|
+
x = Math.max(x, c.x + Math.max(c.width, width) + 2 * pair);
|
|
472
|
+
}
|
|
473
|
+
corridors.push({ x, top, bottom, width });
|
|
474
|
+
if (scope !== undefined) {
|
|
475
|
+
reach.set(
|
|
476
|
+
scope,
|
|
477
|
+
Math.max(reach.get(scope) ?? -Infinity, Math.ceil(x + pair + width)),
|
|
478
|
+
);
|
|
479
|
+
frames = groupBoxes(groups, boxes, { pad, head, left }, reach);
|
|
480
|
+
}
|
|
481
|
+
const points = cleanPoints([
|
|
482
|
+
{ x: source.x + source.width, y: fromY },
|
|
483
|
+
{ x, y: fromY },
|
|
484
|
+
{ x, y: toY },
|
|
485
|
+
{ x: target.x + target.width, y: toY },
|
|
486
|
+
]);
|
|
487
|
+
routes.set(edge.id, {
|
|
488
|
+
points,
|
|
489
|
+
// Beside the corridor, `pair` clear of the corner at the source end.
|
|
490
|
+
label: label && {
|
|
491
|
+
x: x + pair,
|
|
492
|
+
y: toY < fromY ? fromY - pair - label.height : fromY + pair,
|
|
493
|
+
...label,
|
|
494
|
+
},
|
|
495
|
+
arrow: {
|
|
496
|
+
x: target.x + target.width,
|
|
497
|
+
y: toY - ARROW / 2,
|
|
498
|
+
width: ARROW,
|
|
499
|
+
height: ARROW,
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// Forward edges.
|
|
505
|
+
const bands = [...frames].map(([id, frame]) => ({
|
|
506
|
+
id,
|
|
507
|
+
band: headBand(frame, head),
|
|
508
|
+
}));
|
|
509
|
+
const solids = [...boxes.values(), ...frames.values()];
|
|
510
|
+
const over = (y: number) =>
|
|
511
|
+
solids.map((box) => box.y + box.height).filter((bottom) => bottom <= y);
|
|
512
|
+
const forward = edges.filter(
|
|
513
|
+
(edge) =>
|
|
514
|
+
!back.includes(edge.id) && boxes.has(edge.from) && boxes.has(edge.to),
|
|
515
|
+
);
|
|
516
|
+
for (const edge of forward) {
|
|
517
|
+
const source = boxes.get(edge.from);
|
|
518
|
+
const target = boxes.get(edge.to);
|
|
519
|
+
if (!source || !target) continue;
|
|
520
|
+
const from = {
|
|
521
|
+
x: source.x + source.width / 2,
|
|
522
|
+
y: source.y + source.height,
|
|
523
|
+
};
|
|
524
|
+
const to = { x: target.x + target.width / 2, y: target.y };
|
|
525
|
+
const end = { x: to.x, y: to.y - (framed.has(edge.to) ? 0 : ring) };
|
|
526
|
+
const ends = new Set([edge.from, edge.to]);
|
|
527
|
+
const heldByEnd = new Set([...ends].flatMap(holders));
|
|
528
|
+
// What a vertical stretch must not run through: any node but its ends and
|
|
529
|
+
// any group head but those holding them.
|
|
530
|
+
const blocks = (a: CanvasPoint, b: CanvasPoint): Box | undefined =>
|
|
531
|
+
[...boxes].find(
|
|
532
|
+
([id, box]) => !ends.has(id) && crosses(a, b, box),
|
|
533
|
+
)?.[1] ??
|
|
534
|
+
bands.find(({ id, band }) => !heldByEnd.has(id) && crosses(a, b, band))
|
|
535
|
+
?.band;
|
|
536
|
+
// The gap above the target: the middle of it, or higher where the chip
|
|
537
|
+
// needs the room under the bend, never within a `pair` of what stands
|
|
538
|
+
// over it.
|
|
539
|
+
const above = Math.max(from.y, ...over(to.y));
|
|
540
|
+
const chip = labels.get(edge.id);
|
|
541
|
+
const space = chip ? chip.height + 2 * pair + ARROW : 0;
|
|
542
|
+
const gap = Math.max(
|
|
543
|
+
above + pair,
|
|
544
|
+
Math.min((above + to.y) / 2, to.y - space),
|
|
545
|
+
);
|
|
546
|
+
let bend: number | undefined;
|
|
547
|
+
let points: CanvasPoint[] | undefined;
|
|
548
|
+
if (from.x !== to.x && end.y >= from.y + 2 * pair) {
|
|
549
|
+
// The bend stands a `pair` under the source, which leaves the leg under
|
|
550
|
+
// it the whole of a chip's room (`layerGap`); else in the gap above the
|
|
551
|
+
// target.
|
|
552
|
+
bend = from.y + pair;
|
|
553
|
+
if (blocks({ x: to.x, y: bend }, to)) bend = gap;
|
|
554
|
+
} else if (
|
|
555
|
+
from.x === to.x &&
|
|
556
|
+
to.y >= from.y + 2 * pair &&
|
|
557
|
+
blocks(from, to)
|
|
558
|
+
) {
|
|
559
|
+
// An edge that skips a layer down its source's own column would run
|
|
560
|
+
// behind what stands in it: it steps aside a `pair` under the source,
|
|
561
|
+
// runs down a column clear of what it passes, as ELK's dummy node
|
|
562
|
+
// does, and steps back in the gap above the target.
|
|
563
|
+
const top = from.y + pair;
|
|
564
|
+
let x = from.x;
|
|
565
|
+
for (
|
|
566
|
+
let met = blocks({ x, y: top }, { x, y: gap });
|
|
567
|
+
met;
|
|
568
|
+
met = blocks({ x, y: top }, { x, y: gap })
|
|
569
|
+
)
|
|
570
|
+
x = met.x + met.width + ARROW + pair;
|
|
571
|
+
points = [
|
|
572
|
+
from,
|
|
573
|
+
{ x: from.x, y: top },
|
|
574
|
+
{ x, y: top },
|
|
575
|
+
{ x, y: gap },
|
|
576
|
+
{
|
|
577
|
+
x: to.x,
|
|
578
|
+
y: gap,
|
|
579
|
+
},
|
|
580
|
+
end,
|
|
581
|
+
];
|
|
582
|
+
}
|
|
583
|
+
routes.set(edge.id, {
|
|
584
|
+
points: cleanPoints(points ?? bendRoute(from, end, pair, bend)),
|
|
585
|
+
arrow: {
|
|
586
|
+
x: end.x - ARROW / 2,
|
|
587
|
+
y: end.y - ARROW,
|
|
588
|
+
width: ARROW,
|
|
589
|
+
height: ARROW,
|
|
590
|
+
},
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// A chip stands beside the first stretch of a leg that is its edge's alone
|
|
595
|
+
// and long enough to hold it with a `pair` above and a `pair` and an
|
|
596
|
+
// arrowhead below, `pair` from the line and `pair` under the stretch's top,
|
|
597
|
+
// so it hangs near the source. A shared leg (a fan-out's stem) is skipped:
|
|
598
|
+
// the chip goes beside what follows it. A chip never prints over another,
|
|
599
|
+
// under a node or across a line: one that would meet a chip already placed,
|
|
600
|
+
// a node's box or a stretch of any route moves down its stretch, a `pair`
|
|
601
|
+
// under it, where the stretch still holds it, else to a later stretch, then
|
|
602
|
+
// to the left of each stretch in turn, and where none holds it, to the right
|
|
603
|
+
// of what it meets.
|
|
604
|
+
const chips = [...routes.values()].flatMap((route) =>
|
|
605
|
+
route.label ? [route.label] : [],
|
|
606
|
+
);
|
|
607
|
+
const nodes = [...boxes.values()];
|
|
608
|
+
const meetsChip = (box: Box) =>
|
|
609
|
+
[...chips, ...nodes].find(
|
|
610
|
+
(chip) =>
|
|
611
|
+
box.x < chip.x + chip.width + pair &&
|
|
612
|
+
chip.x < box.x + box.width + pair &&
|
|
613
|
+
box.y < chip.y + chip.height + pair &&
|
|
614
|
+
chip.y < box.y + box.height + pair,
|
|
615
|
+
);
|
|
616
|
+
// Every route's stretches, as the boxes they span.
|
|
617
|
+
const lines = [...routes.values()].flatMap(({ points }) =>
|
|
618
|
+
points.slice(1).map((to, index) => {
|
|
619
|
+
const from = points[index] ?? to;
|
|
620
|
+
return {
|
|
621
|
+
from,
|
|
622
|
+
to,
|
|
623
|
+
box: union([
|
|
624
|
+
{ ...from, width: 0, height: 0 },
|
|
625
|
+
{ ...to, width: 0, height: 0 },
|
|
626
|
+
]),
|
|
627
|
+
};
|
|
628
|
+
}),
|
|
629
|
+
);
|
|
630
|
+
// A frame's drawn lines: its border and its head's band, which a chip never
|
|
631
|
+
// prints over or straddles. A chip wholly inside or wholly outside a frame,
|
|
632
|
+
// a `pair` clear of its border, is fine; the one that meets a border is
|
|
633
|
+
// moved past it, and one that meets a side is moved past the whole frame.
|
|
634
|
+
const bordered = (box: Box): Box | undefined => {
|
|
635
|
+
const near = {
|
|
636
|
+
x: box.x - pair,
|
|
637
|
+
y: box.y - pair,
|
|
638
|
+
width: box.width + 2 * pair,
|
|
639
|
+
height: box.height + 2 * pair,
|
|
640
|
+
};
|
|
641
|
+
for (const frame of frames.values()) {
|
|
642
|
+
const band = headBand(frame, head);
|
|
643
|
+
if (overlaps(box, band)) return band;
|
|
644
|
+
const right = frame.x + frame.width;
|
|
645
|
+
const bottom = frame.y + frame.height;
|
|
646
|
+
const across = near.x < right && frame.x < near.x + near.width;
|
|
647
|
+
const down = near.y < bottom && frame.y < near.y + near.height;
|
|
648
|
+
if (!across || !down) continue;
|
|
649
|
+
const sides = [frame.y, bottom].filter(
|
|
650
|
+
(y) => near.y < y && y < near.y + near.height,
|
|
651
|
+
);
|
|
652
|
+
const top = sides[0];
|
|
653
|
+
if (top !== undefined)
|
|
654
|
+
return { x: frame.x, y: top, width: frame.width, height: 0 };
|
|
655
|
+
const edges = [frame.x, right].filter(
|
|
656
|
+
(x) => near.x < x && x < near.x + near.width,
|
|
657
|
+
);
|
|
658
|
+
if (edges.length > 0) return frame;
|
|
659
|
+
}
|
|
660
|
+
return undefined;
|
|
661
|
+
};
|
|
662
|
+
const meets = (box: Box) =>
|
|
663
|
+
meetsChip(box) ??
|
|
664
|
+
bordered(box) ??
|
|
665
|
+
lines.find(({ from, to }) => crosses(from, to, box))?.box;
|
|
666
|
+
// The chip beside the stretch, on its `side`, moved down past what it meets;
|
|
667
|
+
// none when the stretch cannot hold it clear of it.
|
|
668
|
+
const down = (
|
|
669
|
+
leg: Leg,
|
|
670
|
+
label: Size,
|
|
671
|
+
side: "right" | "left",
|
|
672
|
+
): Box | undefined => {
|
|
673
|
+
const x = side === "right" ? leg.x + pair : leg.x - pair - label.width;
|
|
674
|
+
const box = { x, y: leg.top + pair, ...label };
|
|
675
|
+
for (let met = meets(box); met; met = meets(box)) {
|
|
676
|
+
box.y = met.y + met.height + pair;
|
|
677
|
+
if (box.y + label.height + pair + ARROW > leg.bottom) return undefined;
|
|
678
|
+
}
|
|
679
|
+
return box;
|
|
680
|
+
};
|
|
681
|
+
for (const edge of forward) {
|
|
682
|
+
const route = routes.get(edge.id);
|
|
683
|
+
const label = labels.get(edge.id);
|
|
684
|
+
if (!route || !label) continue;
|
|
685
|
+
const others = [...routes]
|
|
686
|
+
.filter(([id]) => id !== edge.id)
|
|
687
|
+
.flatMap(([, other]) => verticalLegs(other.points));
|
|
688
|
+
const legs = verticalLegs(route.points);
|
|
689
|
+
const own = legs.flatMap((leg) => alone(leg, others));
|
|
690
|
+
const pool = own.length > 0 ? own : legs;
|
|
691
|
+
const space = label.height + 2 * pair + ARROW;
|
|
692
|
+
const fits = pool.filter((each) => length(each) >= space);
|
|
693
|
+
const leg =
|
|
694
|
+
fits[0] ??
|
|
695
|
+
pool.reduce<Leg | undefined>(
|
|
696
|
+
(best, each) => (!best || length(each) > length(best) ? each : best),
|
|
697
|
+
undefined,
|
|
698
|
+
);
|
|
699
|
+
if (!leg) continue;
|
|
700
|
+
let chip: Box | undefined;
|
|
701
|
+
for (const side of ["right", "left"] as const)
|
|
702
|
+
for (const each of fits) chip ??= down(each, label, side);
|
|
703
|
+
if (!chip) {
|
|
704
|
+
chip = { x: leg.x + pair, y: leg.top + pair, ...label };
|
|
705
|
+
for (let met = meetsChip(chip); met; met = meetsChip(chip))
|
|
706
|
+
chip.x = met.x + met.width + pair;
|
|
707
|
+
}
|
|
708
|
+
route.label = chip;
|
|
709
|
+
chips.push(chip);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const parts: Box[] = [...boxes.values(), ...frames.values()];
|
|
713
|
+
for (const route of routes.values()) {
|
|
714
|
+
for (const { x, y } of route.points)
|
|
715
|
+
parts.push({ x, y, width: 0, height: 0 });
|
|
716
|
+
if (route.label) parts.push(route.label);
|
|
717
|
+
}
|
|
718
|
+
return { routes, frames, bounds: union(parts) };
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
export function union(boxes: readonly Box[]): Box {
|
|
722
|
+
if (boxes.length === 0) return { x: 0, y: 0, width: 0, height: 0 };
|
|
723
|
+
const left = Math.min(...boxes.map((box) => box.x));
|
|
724
|
+
const top = Math.min(...boxes.map((box) => box.y));
|
|
725
|
+
const right = Math.max(...boxes.map((box) => box.x + box.width));
|
|
726
|
+
const bottom = Math.max(...boxes.map((box) => box.y + box.height));
|
|
727
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const fixed = (value: number): number => Math.round(value * 100) / 100;
|
|
731
|
+
|
|
732
|
+
// Points moved onto pixel centres: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
733
|
+
export function crisp(points: readonly CanvasPoint[]): CanvasPoint[] {
|
|
734
|
+
return points.map(({ x, y }) => ({
|
|
735
|
+
x: Math.floor(x) + PIXEL_CENTRE,
|
|
736
|
+
y: Math.floor(y) + PIXEL_CENTRE,
|
|
737
|
+
}));
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// The SVG `d` of a polyline whose corners are quadratic arcs of `radius`,
|
|
741
|
+
// clamped to half of each adjacent segment.
|
|
742
|
+
export function roundedPath(
|
|
743
|
+
points: readonly CanvasPoint[],
|
|
744
|
+
radius: number,
|
|
745
|
+
): string {
|
|
746
|
+
const [first, ...rest] = points;
|
|
747
|
+
if (!first) return "";
|
|
748
|
+
let d = `M${fixed(first.x)} ${fixed(first.y)}`;
|
|
749
|
+
rest.forEach((point, index) => {
|
|
750
|
+
const before = points[index];
|
|
751
|
+
const after = rest[index + 1];
|
|
752
|
+
const into = before
|
|
753
|
+
? Math.hypot(point.x - before.x, point.y - before.y)
|
|
754
|
+
: 0;
|
|
755
|
+
const out = after ? Math.hypot(after.x - point.x, after.y - point.y) : 0;
|
|
756
|
+
if (!before || !after || into === 0 || out === 0) {
|
|
757
|
+
d += `L${fixed(point.x)} ${fixed(point.y)}`;
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const r = Math.min(radius, into / 2, out / 2);
|
|
761
|
+
const near = {
|
|
762
|
+
x: point.x - ((point.x - before.x) / into) * r,
|
|
763
|
+
y: point.y - ((point.y - before.y) / into) * r,
|
|
764
|
+
};
|
|
765
|
+
const far = {
|
|
766
|
+
x: point.x + ((after.x - point.x) / out) * r,
|
|
767
|
+
y: point.y + ((after.y - point.y) / out) * r,
|
|
768
|
+
};
|
|
769
|
+
d += `L${fixed(near.x)} ${fixed(near.y)}Q${fixed(point.x)} ${fixed(point.y)} ${fixed(far.x)} ${fixed(far.y)}`;
|
|
770
|
+
});
|
|
771
|
+
return d;
|
|
772
|
+
}
|