@slotform/react-ui 0.0.0-stage → 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 +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,260 @@
|
|
|
1
|
+
import { backEdges } from "@slotform/ui-core/canvas";
|
|
2
|
+
import type {
|
|
3
|
+
CanvasEdge,
|
|
4
|
+
CanvasGroup,
|
|
5
|
+
CanvasNode,
|
|
6
|
+
CanvasPoint,
|
|
7
|
+
} from "@slotform/ui-core/descriptors";
|
|
8
|
+
import type { ElkNode } from "elkjs/lib/elk-api.js";
|
|
9
|
+
import {
|
|
10
|
+
ARROW,
|
|
11
|
+
type Box,
|
|
12
|
+
emptyGroups,
|
|
13
|
+
groupTree,
|
|
14
|
+
type Size,
|
|
15
|
+
} from "./geometry.ts";
|
|
16
|
+
|
|
17
|
+
// The input to ELK and the read of its output: pure, so node tests run them.
|
|
18
|
+
// Ids carry their kind, so a node, a group and an edge sharing a string never
|
|
19
|
+
// collide in ELK's one id space.
|
|
20
|
+
|
|
21
|
+
export interface Gaps {
|
|
22
|
+
// Between nodes of a layer, and between layers.
|
|
23
|
+
node: number;
|
|
24
|
+
layer: number;
|
|
25
|
+
// The graph's own margin.
|
|
26
|
+
page: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ElkInput {
|
|
30
|
+
nodes: readonly Pick<CanvasNode, "id">[];
|
|
31
|
+
edges: readonly Pick<CanvasEdge, "id" | "from" | "to">[];
|
|
32
|
+
groups: readonly CanvasGroup[];
|
|
33
|
+
// `pathOrder` of the nodes, the empty groups and the edges.
|
|
34
|
+
order: readonly string[];
|
|
35
|
+
sizes: ReadonlyMap<string, Size>;
|
|
36
|
+
// Every node's width: the `node` width at the rendered theme's size.
|
|
37
|
+
width: number;
|
|
38
|
+
// A group's head height and the padding around what it holds.
|
|
39
|
+
head: number;
|
|
40
|
+
pad: number;
|
|
41
|
+
// A group's left padding where its head text needs more (`leftPads`).
|
|
42
|
+
left?: ReadonlyMap<string, number>;
|
|
43
|
+
gaps: Gaps;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// A layer gap holds the router's own room: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
47
|
+
export function layerGap(floor: number, chip: number, pair: number): number {
|
|
48
|
+
return Math.max(floor, pair + chip + 2 * pair + ARROW);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sides = (top: number, left: number, rest: number) =>
|
|
52
|
+
`[top=${top},left=${left},bottom=${rest},right=${rest}]`;
|
|
53
|
+
|
|
54
|
+
export function elkGraph(input: ElkInput): ElkNode {
|
|
55
|
+
const { nodes, edges, groups, order, sizes, width, head, pad, left, gaps } =
|
|
56
|
+
input;
|
|
57
|
+
// An empty group is a leaf of its head and padding, which an edge may name.
|
|
58
|
+
const empty = emptyGroups(groups, new Set(nodes.map((node) => node.id)));
|
|
59
|
+
const ids = new Set([...nodes.map((node) => node.id), ...empty]);
|
|
60
|
+
const rank = new Map(order.map((id, index) => [id, index]));
|
|
61
|
+
const tree = groupTree(groups, ids);
|
|
62
|
+
|
|
63
|
+
// Where a group stands among its siblings: its first member's place.
|
|
64
|
+
const firsts = new Map<string, number>();
|
|
65
|
+
const first = (id: string): number => {
|
|
66
|
+
const known = firsts.get(id);
|
|
67
|
+
if (known !== undefined) return known;
|
|
68
|
+
const at = Math.min(
|
|
69
|
+
...(tree.nodes.get(id) ?? []).map((node) => rank.get(node) ?? Infinity),
|
|
70
|
+
...(tree.groups.get(id) ?? []).map(first),
|
|
71
|
+
);
|
|
72
|
+
firsts.set(id, at);
|
|
73
|
+
return at;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const leaf = (id: string): ElkNode => ({
|
|
77
|
+
id: `n:${id}`,
|
|
78
|
+
width,
|
|
79
|
+
height: empty.includes(id) ? head + 2 * pad : (sizes.get(id)?.height ?? 0),
|
|
80
|
+
});
|
|
81
|
+
const frame = (id: string): ElkNode => {
|
|
82
|
+
const members = [
|
|
83
|
+
...(tree.nodes.get(id) ?? []).map((node) => ({
|
|
84
|
+
at: rank.get(node) ?? Infinity,
|
|
85
|
+
elk: leaf(node),
|
|
86
|
+
})),
|
|
87
|
+
...(tree.groups.get(id) ?? []).map((group) => ({
|
|
88
|
+
at: first(group),
|
|
89
|
+
elk: frame(group),
|
|
90
|
+
})),
|
|
91
|
+
].filter((member) => member.at !== Infinity);
|
|
92
|
+
return {
|
|
93
|
+
id: `g:${id}`,
|
|
94
|
+
layoutOptions: {
|
|
95
|
+
"elk.padding": sides(head + pad, left?.get(id) ?? pad, pad),
|
|
96
|
+
"elk.spacing.nodeNode": String(gaps.node),
|
|
97
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": String(gaps.layer),
|
|
98
|
+
},
|
|
99
|
+
children: members.sort((a, b) => a.at - b.at).map(({ elk }) => elk),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const held = new Set([...tree.nodes.values()].flat());
|
|
104
|
+
const top = [
|
|
105
|
+
...order
|
|
106
|
+
.filter((id) => ids.has(id) && !held.has(id))
|
|
107
|
+
.map((id) => ({ at: rank.get(id) ?? Infinity, elk: leaf(id) })),
|
|
108
|
+
...tree.roots
|
|
109
|
+
.filter((id) => first(id) !== Infinity)
|
|
110
|
+
.map((id) => ({ at: first(id), elk: frame(id) })),
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
const back = new Set(backEdges(order, edges));
|
|
114
|
+
return {
|
|
115
|
+
id: "root",
|
|
116
|
+
layoutOptions: {
|
|
117
|
+
"elk.algorithm": "layered",
|
|
118
|
+
"elk.direction": "DOWN",
|
|
119
|
+
"elk.hierarchyHandling": "INCLUDE_CHILDREN",
|
|
120
|
+
"elk.layered.considerModelOrder.strategy": "NODES_AND_EDGES",
|
|
121
|
+
// A parent stands over the middle of its children.
|
|
122
|
+
"elk.layered.nodePlacement.strategy": "BRANDES_KOEPF",
|
|
123
|
+
"elk.layered.nodePlacement.bk.fixedAlignment": "BALANCED",
|
|
124
|
+
"elk.json.shapeCoords": "ROOT",
|
|
125
|
+
"elk.spacing.nodeNode": String(gaps.node),
|
|
126
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": String(gaps.layer),
|
|
127
|
+
"elk.padding": sides(gaps.page, gaps.page, gaps.page),
|
|
128
|
+
},
|
|
129
|
+
children: top.sort((a, b) => a.at - b.at).map(({ elk }) => elk),
|
|
130
|
+
edges: edges
|
|
131
|
+
.filter(
|
|
132
|
+
(edge) => !back.has(edge.id) && ids.has(edge.from) && ids.has(edge.to),
|
|
133
|
+
)
|
|
134
|
+
.map((edge) => ({
|
|
135
|
+
id: `e:${edge.id}`,
|
|
136
|
+
sources: [`n:${edge.from}`],
|
|
137
|
+
targets: [`n:${edge.to}`],
|
|
138
|
+
})),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Every node's absolute position, from ELK's output: `shapeCoords: ROOT` makes
|
|
143
|
+
// a node's `x` and `y` absolute, so the walk is flat and a group's own
|
|
144
|
+
// rectangle is ignored. A position is whole pixels where it enters the canvas,
|
|
145
|
+
// so node boxes, frames and edges drawn at pixel centres agree.
|
|
146
|
+
export function fromElk(output: ElkNode): Map<string, CanvasPoint> {
|
|
147
|
+
const out = new Map<string, CanvasPoint>();
|
|
148
|
+
const walk = (node: ElkNode) => {
|
|
149
|
+
if (node.id.startsWith("n:"))
|
|
150
|
+
out.set(node.id.slice(2), {
|
|
151
|
+
x: Math.round(node.x ?? 0),
|
|
152
|
+
y: Math.round(node.y ?? 0),
|
|
153
|
+
});
|
|
154
|
+
for (const child of node.children ?? []) walk(child);
|
|
155
|
+
};
|
|
156
|
+
walk(output);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// The graph's parts that no edge links (a group or a loose node at the top, with
|
|
161
|
+
// every part an edge joins to it), which ELK stands side by side in one row of
|
|
162
|
+
// layers. Where that row is wider than `room` (the width the opening view has
|
|
163
|
+
// at the text floor) the parts wrap: each takes the row's next place while the
|
|
164
|
+
// row holds it, else opens a row under the last, `across` and `down` apart, in
|
|
165
|
+
// the order ELK stood them, so a narrow room reads them in a column and none
|
|
166
|
+
// stands wholly out of sight across. A row that holds, or one part, keeps ELK's
|
|
167
|
+
// own places. Positions are whole pixels, as `fromElk`'s are.
|
|
168
|
+
export function wrapParts(
|
|
169
|
+
output: ElkNode,
|
|
170
|
+
positions: ReadonlyMap<string, CanvasPoint>,
|
|
171
|
+
edges: readonly Pick<CanvasEdge, "from" | "to">[],
|
|
172
|
+
space: number,
|
|
173
|
+
gaps: { across: number; down: number },
|
|
174
|
+
): ReadonlyMap<string, CanvasPoint> {
|
|
175
|
+
const tops = output.children ?? [];
|
|
176
|
+
if (tops.length < 2) return positions;
|
|
177
|
+
// Each top part's leaves, and which part a leaf stands in.
|
|
178
|
+
const partOf = new Map<string, number>();
|
|
179
|
+
tops.forEach((top, index) => {
|
|
180
|
+
const walk = (node: ElkNode) => {
|
|
181
|
+
if (node.id.startsWith("n:")) partOf.set(node.id.slice(2), index);
|
|
182
|
+
for (const child of node.children ?? []) walk(child);
|
|
183
|
+
};
|
|
184
|
+
walk(top);
|
|
185
|
+
});
|
|
186
|
+
// The parts an edge joins are one.
|
|
187
|
+
const root = tops.map((_, index) => index);
|
|
188
|
+
const find = (index: number): number => {
|
|
189
|
+
let at = index;
|
|
190
|
+
while (root[at] !== at) at = root[at] ?? at;
|
|
191
|
+
return at;
|
|
192
|
+
};
|
|
193
|
+
for (const edge of edges) {
|
|
194
|
+
const from = partOf.get(edge.from);
|
|
195
|
+
const to = partOf.get(edge.to);
|
|
196
|
+
if (from !== undefined && to !== undefined) root[find(from)] = find(to);
|
|
197
|
+
}
|
|
198
|
+
const joined = new Map<number, Box>();
|
|
199
|
+
tops.forEach((top, index) => {
|
|
200
|
+
const box = {
|
|
201
|
+
x: top.x ?? 0,
|
|
202
|
+
y: top.y ?? 0,
|
|
203
|
+
width: top.width ?? 0,
|
|
204
|
+
height: top.height ?? 0,
|
|
205
|
+
};
|
|
206
|
+
const key = find(index);
|
|
207
|
+
const last = joined.get(key);
|
|
208
|
+
if (!last) {
|
|
209
|
+
joined.set(key, box);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const x = Math.min(last.x, box.x);
|
|
213
|
+
const y = Math.min(last.y, box.y);
|
|
214
|
+
joined.set(key, {
|
|
215
|
+
x,
|
|
216
|
+
y,
|
|
217
|
+
width: Math.max(last.x + last.width, box.x + box.width) - x,
|
|
218
|
+
height: Math.max(last.y + last.height, box.y + box.height) - y,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
if (joined.size < 2) return positions;
|
|
222
|
+
const parts = [...joined].sort(([, a], [, b]) => a.x - b.x || a.y - b.y);
|
|
223
|
+
const left = Math.min(...parts.map(([, box]) => box.x));
|
|
224
|
+
const right = Math.max(...parts.map(([, box]) => box.x + box.width));
|
|
225
|
+
if (right - left <= space) return positions;
|
|
226
|
+
const top = Math.min(...parts.map(([, box]) => box.y));
|
|
227
|
+
const shift = new Map<number, CanvasPoint>();
|
|
228
|
+
let x = left;
|
|
229
|
+
let y = top;
|
|
230
|
+
let tall = 0;
|
|
231
|
+
for (const [key, box] of parts) {
|
|
232
|
+
if (x > left && x + box.width > left + space) {
|
|
233
|
+
x = left;
|
|
234
|
+
y += tall + gaps.down;
|
|
235
|
+
tall = 0;
|
|
236
|
+
}
|
|
237
|
+
shift.set(key, { x: x - box.x, y: y - box.y });
|
|
238
|
+
x += box.width + gaps.across;
|
|
239
|
+
tall = Math.max(tall, box.height);
|
|
240
|
+
}
|
|
241
|
+
const out = new Map<string, CanvasPoint>();
|
|
242
|
+
for (const [id, at] of positions) {
|
|
243
|
+
const part = partOf.get(id);
|
|
244
|
+
const by = part === undefined ? undefined : shift.get(find(part));
|
|
245
|
+
out.set(
|
|
246
|
+
id,
|
|
247
|
+
by ? { x: Math.round(at.x + by.x), y: Math.round(at.y + by.y) } : at,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// What a layout is keyed by: everything ELK reads (the structure, the path
|
|
254
|
+
// order, each node's height, a group head's height and padding, the gaps a
|
|
255
|
+
// chip's room sets), so a layout runs again when any of it changes, a node that
|
|
256
|
+
// grows once it is measured again included, and not for a status, a label's
|
|
257
|
+
// words or a path that leaves them.
|
|
258
|
+
export function graphKey(input: ElkInput): string {
|
|
259
|
+
return JSON.stringify(elkGraph(input));
|
|
260
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { CanvasGroup } from "@slotform/ui-core/descriptors";
|
|
2
|
+
import {
|
|
3
|
+
BODY_SIZE,
|
|
4
|
+
MEASURE_CHARACTERS,
|
|
5
|
+
SANS_ADVANCE,
|
|
6
|
+
TYPE_SCALE,
|
|
7
|
+
} from "@slotform/ui-core/tokens";
|
|
8
|
+
import { type Box, spreadBoxes } from "./geometry.ts";
|
|
9
|
+
import { EXTENT } from "./view.ts";
|
|
10
|
+
|
|
11
|
+
// A node's text floor: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
12
|
+
export const TEXT_FLOOR = Math.round(
|
|
13
|
+
BODY_SIZE.desktop * TYPE_SCALE.caption.size,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
// Between the full node (zoom 1 and up) and the glyph alone, a node under zoom 1
|
|
17
|
+
// is its overview: the glyph with its name beside it, at the floor size. This is
|
|
18
|
+
// the lowest zoom the overview stands at before a graph raises it; under it a
|
|
19
|
+
// node is its glyph alone. Raised, never lowered, by `overviewFloorFor`.
|
|
20
|
+
export const OVERVIEW_FLOOR = 0.5;
|
|
21
|
+
|
|
22
|
+
// The variable that holds a part at its own size on screen: docs/architecture/plugins/react-ui.md, "Canvas".
|
|
23
|
+
export const UNZOOM_VAR = "--canvas-unzoom";
|
|
24
|
+
export const UNZOOM = { scale: `var(${UNZOOM_VAR})` };
|
|
25
|
+
|
|
26
|
+
export function belowFloor(zoom: number, text: number, floor: number): boolean {
|
|
27
|
+
return zoom * text < floor;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The lowest zoom at which no two glyphs of `glyph` px stand closer than `2 * pair`
|
|
31
|
+
// px, with every route between two of them as long as that gap: the glyph and the
|
|
32
|
+
// gap over the smallest distance between two node centres, measured as the
|
|
33
|
+
// larger of the two axes' gaps (two squares touch when both gaps are under their
|
|
34
|
+
// size). A route runs between the nodes' routing boxes (`routeSide`), each up to
|
|
35
|
+
// `pair` flow units wider than its glyph, so the distance counts less that
|
|
36
|
+
// rounding and the edges between glyphs keep a stretch to draw.
|
|
37
|
+
export function minZoomFor(
|
|
38
|
+
boxes: readonly Box[],
|
|
39
|
+
glyph: number,
|
|
40
|
+
pair: number,
|
|
41
|
+
): number {
|
|
42
|
+
const [lowest] = EXTENT;
|
|
43
|
+
let nearest = Number.POSITIVE_INFINITY;
|
|
44
|
+
boxes.forEach((one, index) => {
|
|
45
|
+
for (const two of boxes.slice(index + 1))
|
|
46
|
+
nearest = Math.min(
|
|
47
|
+
nearest,
|
|
48
|
+
Math.max(
|
|
49
|
+
Math.abs(one.x + one.width / 2 - (two.x + two.width / 2)),
|
|
50
|
+
Math.abs(one.y + one.height / 2 - (two.y + two.height / 2)),
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
const space = nearest - pair;
|
|
55
|
+
if (space <= 0) return 1;
|
|
56
|
+
return Math.min(1, Math.max(lowest, (glyph + 2 * pair) / space));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The widest a node's name stands on screen at the `meta` text size (px): the
|
|
60
|
+
// short measure in characters of the name's own role, at the sans advance. It is
|
|
61
|
+
// the cap, not the name's own width, so the floor a graph needs does not move
|
|
62
|
+
// with its words.
|
|
63
|
+
export function nameCap(meta: number): number {
|
|
64
|
+
return Math.ceil(MEASURE_CHARACTERS["measure-short"] * SANS_ADVANCE * meta);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The zoom under which a graph's nodes are their glyphs alone: the overview's
|
|
68
|
+
// floor, at least `OVERVIEW_FLOOR` and the lowest zoom at which no two overview
|
|
69
|
+
// forms stand closer than `air`. A form is its glyph, the `gap` after it and
|
|
70
|
+
// its name at the cap, `width` px wide in all, `glyph` px high; on screen two
|
|
71
|
+
// forms clear each other when their centres stand `width + air` apart across or
|
|
72
|
+
// `glyph + air` apart down, and each pair asks the nearer of the two zooms
|
|
73
|
+
// that reach one of them. A pair whose centres coincide, or a floor of 1 or more,
|
|
74
|
+
// leaves the graph no overview: the floor is then 1, the full node's own.
|
|
75
|
+
export function overviewFloorFor(
|
|
76
|
+
boxes: readonly Box[],
|
|
77
|
+
glyph: number,
|
|
78
|
+
width: number,
|
|
79
|
+
air: number,
|
|
80
|
+
): number {
|
|
81
|
+
let need = 0;
|
|
82
|
+
boxes.forEach((one, index) => {
|
|
83
|
+
for (const two of boxes.slice(index + 1)) {
|
|
84
|
+
const across = Math.abs(one.x + one.width / 2 - (two.x + two.width / 2));
|
|
85
|
+
const down = Math.abs(one.y + one.height / 2 - (two.y + two.height / 2));
|
|
86
|
+
need = Math.max(
|
|
87
|
+
need,
|
|
88
|
+
Math.min(
|
|
89
|
+
across === 0 ? Number.POSITIVE_INFINITY : (width + air) / across,
|
|
90
|
+
down === 0 ? Number.POSITIVE_INFINITY : (glyph + air) / down,
|
|
91
|
+
),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return Math.min(1, Math.max(OVERVIEW_FLOOR, need));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The side, in flow units, of the box a node's edges and frames route to under
|
|
99
|
+
// the floor: its glyph's size on screen over the zoom, rounded up to a multiple
|
|
100
|
+
// of `step` so a wheel tick between two multiples routes nothing again. Above
|
|
101
|
+
// the floor it is 0 and a node routes to its card.
|
|
102
|
+
export function routeSide(zoom: number, glyph: number, step: number): number {
|
|
103
|
+
if (!belowFloor(zoom, TEXT_FLOOR, TEXT_FLOOR)) return 0;
|
|
104
|
+
return Math.ceil(glyph / zoom / step) * step;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function glyphBoxes(
|
|
108
|
+
boxes: ReadonlyMap<string, Box>,
|
|
109
|
+
side: number,
|
|
110
|
+
): Map<string, Box> {
|
|
111
|
+
return new Map(
|
|
112
|
+
[...boxes].map(([id, box]) => [
|
|
113
|
+
id,
|
|
114
|
+
{
|
|
115
|
+
x: box.x + box.width / 2 - side / 2,
|
|
116
|
+
y: box.y + box.height / 2 - side / 2,
|
|
117
|
+
width: side,
|
|
118
|
+
height: side,
|
|
119
|
+
},
|
|
120
|
+
]),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// What a graph draws, routes to and frames at a routing side (`routeSide`).
|
|
125
|
+
// Above the floor (side 0): the cards and the empty groups as they stand, the
|
|
126
|
+
// head and the padding as measured. Under it a group's head and padding draw at
|
|
127
|
+
// their own size on screen, so each grows by the side's step over the glyph,
|
|
128
|
+
// and the boxes spread by that growth (`spreadBoxes`) so no grown head stands
|
|
129
|
+
// over a node outside its group: `cards` is where each node then draws, and
|
|
130
|
+
// `boxes` what the router meets, each node's glyph square on its card's centre
|
|
131
|
+
// and each empty group's spread box.
|
|
132
|
+
export function glyphTier(
|
|
133
|
+
cards: ReadonlyMap<string, Box>,
|
|
134
|
+
holes: ReadonlyMap<string, Box>,
|
|
135
|
+
groups: readonly CanvasGroup[],
|
|
136
|
+
{
|
|
137
|
+
side,
|
|
138
|
+
glyph,
|
|
139
|
+
head,
|
|
140
|
+
pad,
|
|
141
|
+
left,
|
|
142
|
+
}: {
|
|
143
|
+
side: number;
|
|
144
|
+
glyph: number;
|
|
145
|
+
head: number;
|
|
146
|
+
pad: number;
|
|
147
|
+
left?: ReadonlyMap<string, number>;
|
|
148
|
+
},
|
|
149
|
+
): {
|
|
150
|
+
cards: Map<string, Box>;
|
|
151
|
+
boxes: Map<string, Box>;
|
|
152
|
+
head: number;
|
|
153
|
+
pad: number;
|
|
154
|
+
} {
|
|
155
|
+
const grow = side > 0 ? side / glyph : 1;
|
|
156
|
+
const spread = spreadBoxes(new Map([...cards, ...holes]), groups, {
|
|
157
|
+
head,
|
|
158
|
+
pad,
|
|
159
|
+
left,
|
|
160
|
+
grow,
|
|
161
|
+
});
|
|
162
|
+
const drawn = new Map([...spread].filter(([id]) => cards.has(id)));
|
|
163
|
+
const hollow = [...spread].filter(([id]) => !cards.has(id));
|
|
164
|
+
return {
|
|
165
|
+
cards: drawn,
|
|
166
|
+
boxes: new Map([
|
|
167
|
+
...(side > 0 ? glyphBoxes(drawn, side) : drawn),
|
|
168
|
+
...hollow,
|
|
169
|
+
]),
|
|
170
|
+
head: head * grow,
|
|
171
|
+
pad: pad * grow,
|
|
172
|
+
};
|
|
173
|
+
}
|