@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.
Files changed (280) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -2
  3. package/dist/density.d.ts +4 -0
  4. package/dist/density.d.ts.map +1 -0
  5. package/dist/density.js +8 -0
  6. package/dist/density.js.map +1 -0
  7. package/dist/index.d.ts +400 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +388 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/node/canvas.d.ts +3 -0
  12. package/dist/node/canvas.d.ts.map +1 -0
  13. package/dist/node/canvas.js +13 -0
  14. package/dist/node/canvas.js.map +1 -0
  15. package/dist/node/codegen.d.ts +6 -0
  16. package/dist/node/codegen.d.ts.map +1 -0
  17. package/dist/node/codegen.js +70 -0
  18. package/dist/node/codegen.js.map +1 -0
  19. package/dist/node/css-escape.d.ts +8 -0
  20. package/dist/node/css-escape.d.ts.map +1 -0
  21. package/dist/node/css-escape.js +12 -0
  22. package/dist/node/css-escape.js.map +1 -0
  23. package/dist/node/fonts.d.ts +6 -0
  24. package/dist/node/fonts.d.ts.map +1 -0
  25. package/dist/node/fonts.js +219 -0
  26. package/dist/node/fonts.js.map +1 -0
  27. package/dist/node/lint.d.ts +3 -0
  28. package/dist/node/lint.d.ts.map +1 -0
  29. package/dist/node/lint.js +23 -0
  30. package/dist/node/lint.js.map +1 -0
  31. package/dist/node/mode.d.ts +9 -0
  32. package/dist/node/mode.d.ts.map +1 -0
  33. package/dist/node/mode.js +38 -0
  34. package/dist/node/mode.js.map +1 -0
  35. package/dist/node/theme.d.ts +13 -0
  36. package/dist/node/theme.d.ts.map +1 -0
  37. package/dist/node/theme.js +160 -0
  38. package/dist/node/theme.js.map +1 -0
  39. package/dist/types.d.ts +250 -0
  40. package/dist/types.d.ts.map +1 -0
  41. package/dist/types.js +80 -0
  42. package/dist/types.js.map +1 -0
  43. package/guide/acts.md +61 -0
  44. package/guide/audio.md +77 -0
  45. package/guide/blocks.md +82 -0
  46. package/guide/canvas-choosing.md +34 -0
  47. package/guide/canvas-editing.md +36 -0
  48. package/guide/canvas-layout.md +46 -0
  49. package/guide/canvas-opening.md +47 -0
  50. package/guide/canvas-overview.md +24 -0
  51. package/guide/canvas-states.md +37 -0
  52. package/guide/canvas-view.md +46 -0
  53. package/guide/canvas.md +94 -0
  54. package/guide/containers.md +60 -0
  55. package/guide/content.md +40 -0
  56. package/guide/controls.md +89 -0
  57. package/guide/density.md +48 -0
  58. package/guide/dock.md +42 -0
  59. package/guide/drag.md +38 -0
  60. package/guide/figures.md +25 -0
  61. package/guide/forms.md +82 -0
  62. package/guide/pane-bar.md +11 -0
  63. package/guide/pane.md +50 -0
  64. package/guide/reference.md +74 -0
  65. package/guide/rules.md +109 -0
  66. package/guide/section.md +29 -0
  67. package/guide/shell.md +26 -0
  68. package/guide/shots.md +37 -0
  69. package/guide/sidebar.md +30 -0
  70. package/guide/table.md +119 -0
  71. package/lint/no-arbitrary-value.grit +9 -0
  72. package/lint/no-class-on-component.grit +12 -0
  73. package/lint/no-density.grit +8 -0
  74. package/lint/no-host-look.grit +38 -0
  75. package/lint/no-img.grit +10 -0
  76. package/lint/no-navigate-act.grit +22 -0
  77. package/lint/no-raw-style.grit +8 -0
  78. package/package.json +150 -3
  79. package/src/density.ts +9 -0
  80. package/src/index.ts +503 -0
  81. package/src/node/canvas.ts +14 -0
  82. package/src/node/codegen.ts +100 -0
  83. package/src/node/css-escape.ts +27 -0
  84. package/src/node/fonts.ts +260 -0
  85. package/src/node/lint.ts +25 -0
  86. package/src/node/mode.ts +64 -0
  87. package/src/node/theme.ts +232 -0
  88. package/src/types.ts +120 -0
  89. package/src/ui/atoms/avatar/base.tsx +42 -0
  90. package/src/ui/atoms/button/glyph.tsx +244 -0
  91. package/src/ui/atoms/button/link.tsx +81 -0
  92. package/src/ui/atoms/button/look.ts +32 -0
  93. package/src/ui/atoms/button/reason.tsx +69 -0
  94. package/src/ui/atoms/chip/base.tsx +85 -0
  95. package/src/ui/atoms/count/base.tsx +27 -0
  96. package/src/ui/atoms/icon/index.tsx +51 -0
  97. package/src/ui/atoms/message/waiting.tsx +19 -0
  98. package/src/ui/atoms/prose/painted.tsx +35 -0
  99. package/src/ui/atoms/row/badges.tsx +226 -0
  100. package/src/ui/atoms/row/leading.tsx +166 -0
  101. package/src/ui/atoms/row/lock.tsx +17 -0
  102. package/src/ui/atoms/row/marks.tsx +79 -0
  103. package/src/ui/atoms/section/card.tsx +27 -0
  104. package/src/ui/atoms/spinner/index.tsx +42 -0
  105. package/src/ui/atoms/status/base.tsx +51 -0
  106. package/src/ui/atoms/status/change.tsx +29 -0
  107. package/src/ui/atoms/status/dot.tsx +45 -0
  108. package/src/ui/atoms/switch/checkbox.tsx +121 -0
  109. package/src/ui/atoms/turn.tsx +28 -0
  110. package/src/ui/components/action-bar/base.tsx +276 -0
  111. package/src/ui/components/action-bar/index.tsx +20 -0
  112. package/src/ui/components/avatar/index.tsx +21 -0
  113. package/src/ui/components/banner/index.tsx +207 -0
  114. package/src/ui/components/banner/pending.tsx +109 -0
  115. package/src/ui/components/bar-chart/index.tsx +701 -0
  116. package/src/ui/components/button/base.tsx +214 -0
  117. package/src/ui/components/button/face.tsx +84 -0
  118. package/src/ui/components/button/index.tsx +206 -0
  119. package/src/ui/components/canvas/connection.tsx +26 -0
  120. package/src/ui/components/canvas/edges.tsx +116 -0
  121. package/src/ui/components/canvas/elk.ts +260 -0
  122. package/src/ui/components/canvas/floor.ts +173 -0
  123. package/src/ui/components/canvas/geometry.ts +772 -0
  124. package/src/ui/components/canvas/ground.tsx +97 -0
  125. package/src/ui/components/canvas/group.tsx +156 -0
  126. package/src/ui/components/canvas/hit.ts +40 -0
  127. package/src/ui/components/canvas/index.tsx +497 -0
  128. package/src/ui/components/canvas/label.tsx +53 -0
  129. package/src/ui/components/canvas/layout.ts +543 -0
  130. package/src/ui/components/canvas/lift.ts +272 -0
  131. package/src/ui/components/canvas/look.ts +129 -0
  132. package/src/ui/components/canvas/node.tsx +689 -0
  133. package/src/ui/components/canvas/open.ts +243 -0
  134. package/src/ui/components/canvas/opening.ts +240 -0
  135. package/src/ui/components/canvas/view.ts +621 -0
  136. package/src/ui/components/canvas/viewport.ts +417 -0
  137. package/src/ui/components/canvas/zoom.tsx +113 -0
  138. package/src/ui/components/card/index.tsx +20 -0
  139. package/src/ui/components/chip/index.tsx +24 -0
  140. package/src/ui/components/code/frame.ts +2 -0
  141. package/src/ui/components/code/index.tsx +234 -0
  142. package/src/ui/components/columns/index.tsx +51 -0
  143. package/src/ui/components/count/index.tsx +17 -0
  144. package/src/ui/components/diff/index.tsx +249 -0
  145. package/src/ui/components/diff/word.tsx +104 -0
  146. package/src/ui/components/dock/index.tsx +262 -0
  147. package/src/ui/components/file-row/index.tsx +213 -0
  148. package/src/ui/components/form/base.tsx +280 -0
  149. package/src/ui/components/form/derive.tsx +269 -0
  150. package/src/ui/components/form/index.tsx +49 -0
  151. package/src/ui/components/form/saved.tsx +39 -0
  152. package/src/ui/components/form-field/index.tsx +658 -0
  153. package/src/ui/components/gallery/index.tsx +83 -0
  154. package/src/ui/components/gate/index.tsx +183 -0
  155. package/src/ui/components/image/base.tsx +245 -0
  156. package/src/ui/components/image/index.tsx +58 -0
  157. package/src/ui/components/input/base.tsx +246 -0
  158. package/src/ui/components/input/code.tsx +108 -0
  159. package/src/ui/components/input/drop.ts +37 -0
  160. package/src/ui/components/input/file.tsx +154 -0
  161. package/src/ui/components/input/index.tsx +132 -0
  162. package/src/ui/components/input/prose.tsx +107 -0
  163. package/src/ui/components/input/refusal.tsx +30 -0
  164. package/src/ui/components/item-header/base.tsx +410 -0
  165. package/src/ui/components/menu/base.tsx +319 -0
  166. package/src/ui/components/message/attachments.tsx +101 -0
  167. package/src/ui/components/message/base.tsx +266 -0
  168. package/src/ui/components/message-input/index.tsx +377 -0
  169. package/src/ui/components/missing/base.tsx +45 -0
  170. package/src/ui/components/pane/head.tsx +399 -0
  171. package/src/ui/components/pane/index.tsx +325 -0
  172. package/src/ui/components/picker/base.tsx +1551 -0
  173. package/src/ui/components/picker/index.tsx +224 -0
  174. package/src/ui/components/picker/list.tsx +423 -0
  175. package/src/ui/components/picker/segments.tsx +183 -0
  176. package/src/ui/components/prose/index.tsx +259 -0
  177. package/src/ui/components/qr-code/index.tsx +65 -0
  178. package/src/ui/components/row/acts.tsx +126 -0
  179. package/src/ui/components/row/fact.tsx +54 -0
  180. package/src/ui/components/row/index.tsx +1370 -0
  181. package/src/ui/components/row/progress.tsx +38 -0
  182. package/src/ui/components/row/status.tsx +54 -0
  183. package/src/ui/components/row/title.ts +7 -0
  184. package/src/ui/components/rules/index.tsx +290 -0
  185. package/src/ui/components/section/band.tsx +39 -0
  186. package/src/ui/components/section/head.tsx +267 -0
  187. package/src/ui/components/section/index.tsx +409 -0
  188. package/src/ui/components/section/rows.tsx +356 -0
  189. package/src/ui/components/sheet/base.tsx +272 -0
  190. package/src/ui/components/sheet/confirm.tsx +79 -0
  191. package/src/ui/components/sheet/docked.tsx +218 -0
  192. package/src/ui/components/sheet/form.tsx +76 -0
  193. package/src/ui/components/sheet/head.tsx +120 -0
  194. package/src/ui/components/sheet/index.tsx +45 -0
  195. package/src/ui/components/sheet/layer.tsx +168 -0
  196. package/src/ui/components/sheet/outlet.tsx +58 -0
  197. package/src/ui/components/sheet/task.tsx +41 -0
  198. package/src/ui/components/shell/cut.tsx +116 -0
  199. package/src/ui/components/shell/drag.tsx +45 -0
  200. package/src/ui/components/shell/host.tsx +71 -0
  201. package/src/ui/components/shell/index.tsx +737 -0
  202. package/src/ui/components/shell/panes.tsx +76 -0
  203. package/src/ui/components/shell/preview.ts +230 -0
  204. package/src/ui/components/shell/rail.tsx +163 -0
  205. package/src/ui/components/shell/row.ts +11 -0
  206. package/src/ui/components/shell/switcher.tsx +143 -0
  207. package/src/ui/components/shell/tree.tsx +252 -0
  208. package/src/ui/components/slider/base.tsx +139 -0
  209. package/src/ui/components/slider/index.tsx +57 -0
  210. package/src/ui/components/stages/index.tsx +198 -0
  211. package/src/ui/components/stats/count-links.tsx +47 -0
  212. package/src/ui/components/stats/index.tsx +203 -0
  213. package/src/ui/components/stats/meter.tsx +213 -0
  214. package/src/ui/components/stats/parts.ts +10 -0
  215. package/src/ui/components/step-count/index.tsx +45 -0
  216. package/src/ui/components/switch/base.tsx +86 -0
  217. package/src/ui/components/switch/index.tsx +38 -0
  218. package/src/ui/components/table/columns.tsx +131 -0
  219. package/src/ui/components/table/index.tsx +2215 -0
  220. package/src/ui/components/text/index.tsx +37 -0
  221. package/src/ui/components/thread/index.tsx +503 -0
  222. package/src/ui/components/thread/latest.tsx +59 -0
  223. package/src/ui/components/toast/index.tsx +113 -0
  224. package/src/ui/components/toast/layer.tsx +26 -0
  225. package/src/ui/components/toolbar/filters.tsx +161 -0
  226. package/src/ui/components/toolbar/index.tsx +125 -0
  227. package/src/ui/components/waveform/index.tsx +205 -0
  228. package/src/ui/globals.css +427 -0
  229. package/src/ui/lib/act.ts +60 -0
  230. package/src/ui/lib/age.ts +49 -0
  231. package/src/ui/lib/bound.ts +16 -0
  232. package/src/ui/lib/canvas-layout.ts +11 -0
  233. package/src/ui/lib/caret.ts +9 -0
  234. package/src/ui/lib/chip-lines.ts +3 -0
  235. package/src/ui/lib/closed.ts +8 -0
  236. package/src/ui/lib/code.tsx +184 -0
  237. package/src/ui/lib/columns.ts +39 -0
  238. package/src/ui/lib/commit.ts +24 -0
  239. package/src/ui/lib/container.ts +112 -0
  240. package/src/ui/lib/copy.ts +29 -0
  241. package/src/ui/lib/drag.ts +575 -0
  242. package/src/ui/lib/empty.tsx +85 -0
  243. package/src/ui/lib/field.ts +39 -0
  244. package/src/ui/lib/focus.ts +175 -0
  245. package/src/ui/lib/folds.ts +67 -0
  246. package/src/ui/lib/follow.ts +220 -0
  247. package/src/ui/lib/form.ts +19 -0
  248. package/src/ui/lib/guards.ts +38 -0
  249. package/src/ui/lib/heading.ts +14 -0
  250. package/src/ui/lib/ink.ts +26 -0
  251. package/src/ui/lib/lang.ts +7 -0
  252. package/src/ui/lib/leave.ts +14 -0
  253. package/src/ui/lib/lockup.tsx +145 -0
  254. package/src/ui/lib/measure.ts +22 -0
  255. package/src/ui/lib/media.ts +63 -0
  256. package/src/ui/lib/modality.ts +69 -0
  257. package/src/ui/lib/moment.ts +13 -0
  258. package/src/ui/lib/more.tsx +76 -0
  259. package/src/ui/lib/navigate.ts +210 -0
  260. package/src/ui/lib/noop.ts +3 -0
  261. package/src/ui/lib/not-found.tsx +40 -0
  262. package/src/ui/lib/paint.tsx +55 -0
  263. package/src/ui/lib/parts-line.tsx +54 -0
  264. package/src/ui/lib/picked.ts +18 -0
  265. package/src/ui/lib/player-store.ts +38 -0
  266. package/src/ui/lib/player.ts +139 -0
  267. package/src/ui/lib/portal.ts +12 -0
  268. package/src/ui/lib/read.tsx +269 -0
  269. package/src/ui/lib/region.tsx +49 -0
  270. package/src/ui/lib/reorder.ts +154 -0
  271. package/src/ui/lib/rest.ts +16 -0
  272. package/src/ui/lib/saved.ts +29 -0
  273. package/src/ui/lib/scramble.tsx +147 -0
  274. package/src/ui/lib/scrolls.ts +70 -0
  275. package/src/ui/lib/style.ts +6 -0
  276. package/src/ui/lib/swap.tsx +107 -0
  277. package/src/ui/lib/toast.ts +39 -0
  278. package/src/ui/lib/walk-classes.ts +16 -0
  279. package/src/ui/lib/window.tsx +376 -0
  280. package/src/ui/worker.d.ts +4 -0
@@ -0,0 +1,543 @@
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
+ type RefObject,
11
+ useCallback,
12
+ useEffect,
13
+ useLayoutEffect,
14
+ useMemo,
15
+ useRef,
16
+ useState,
17
+ } from "react";
18
+ import { spacing } from "../../lib/measure.ts";
19
+ import {
20
+ type ElkInput,
21
+ elkGraph,
22
+ fromElk,
23
+ type Gaps,
24
+ graphKey,
25
+ layerGap,
26
+ wrapParts,
27
+ } from "./elk.ts";
28
+ import { glyphTier, routeSide } from "./floor.ts";
29
+ import {
30
+ type Box,
31
+ hollowRow,
32
+ leftPads,
33
+ type Routes,
34
+ routeEdges,
35
+ type Size,
36
+ } from "./geometry.ts";
37
+ import type { Opening } from "./open.ts";
38
+ import { type Places, place } from "./view.ts";
39
+ import {
40
+ type About,
41
+ clearanceOf,
42
+ useViewportValue,
43
+ type Viewport,
44
+ } from "./viewport.ts";
45
+
46
+ // The sizes a layout is drawn by, from the spacing roles at the density in
47
+ // force: a layer gap holds the router's bend and a label chip with air round
48
+ // it.
49
+ export interface Space {
50
+ pad: number;
51
+ pair: number;
52
+ // A port ring's drawn size.
53
+ port: number;
54
+ gaps: Gaps;
55
+ }
56
+
57
+ export function spaces(chip: number): Space {
58
+ const pair = spacing("gap");
59
+ return {
60
+ pad: spacing("pad"),
61
+ pair,
62
+ // A port is two badge gaps square (`CANVAS_PORT`).
63
+ port: 2 * spacing("badge"),
64
+ gaps: {
65
+ node: spacing("pad"),
66
+ layer: layerGap(spacing("section"), chip, pair),
67
+ page: spacing("page"),
68
+ },
69
+ };
70
+ }
71
+
72
+ // What the hidden probe measures: the height of a group head, how far each
73
+ // group's head text reaches from its frame's left edge, and each chip's size,
74
+ // by edge id.
75
+ interface Measures {
76
+ head: number;
77
+ reach: ReadonlyMap<string, number>;
78
+ labels: ReadonlyMap<string, Size>;
79
+ }
80
+
81
+ const NONE: Measures = { head: 0, reach: new Map(), labels: new Map() };
82
+
83
+ const sameMap = <T>(
84
+ a: ReadonlyMap<string, T>,
85
+ b: ReadonlyMap<string, T>,
86
+ same: (one: T, two: T) => boolean,
87
+ ) =>
88
+ a.size === b.size &&
89
+ [...b].every(([id, value]) => {
90
+ const last = a.get(id);
91
+ return last !== undefined && same(last, value);
92
+ });
93
+
94
+ const sameMeasures = (a: Measures, b: Measures) =>
95
+ a.head === b.head &&
96
+ sameMap(a.reach, b.reach, (one, two) => one === two) &&
97
+ sameMap(
98
+ a.labels,
99
+ b.labels,
100
+ (one, two) => one.width === two.width && one.height === two.height,
101
+ );
102
+
103
+ // Reads the probe's parts (one `data-head` per group, one `data-label` per
104
+ // edge) and reads them again when any of them resizes. `null` until the first
105
+ // read.
106
+ function useMeasures(probe: HTMLElement | null): Measures | null {
107
+ const [measures, setMeasures] = useState<Measures | null>(null);
108
+ useLayoutEffect(() => {
109
+ if (!probe) return;
110
+ const read = () => {
111
+ const labels = new Map<string, Size>();
112
+ for (const part of probe.querySelectorAll<HTMLElement>("[data-label]")) {
113
+ const { width, height } = part.getBoundingClientRect();
114
+ labels.set(part.dataset.label ?? "", { width, height });
115
+ }
116
+ // A probe frame holds its head, whose text ends where its span does.
117
+ const reach = new Map<string, number>();
118
+ let head = 0;
119
+ for (const frame of probe.querySelectorAll<HTMLElement>("[data-head]")) {
120
+ const text = frame.querySelector("span")?.getBoundingClientRect();
121
+ const row = frame.firstElementChild?.getBoundingClientRect();
122
+ head = Math.max(head, Math.ceil(row?.height ?? 0));
123
+ reach.set(
124
+ frame.dataset.head ?? "",
125
+ (text?.right ?? 0) - frame.getBoundingClientRect().left,
126
+ );
127
+ }
128
+ const next = { head, reach, labels };
129
+ setMeasures((last) => (last && sameMeasures(last, next) ? last : next));
130
+ };
131
+ read();
132
+ const watch = new ResizeObserver(read);
133
+ for (const part of probe.children) watch.observe(part);
134
+ return () => watch.disconnect();
135
+ }, [probe]);
136
+ return measures;
137
+ }
138
+
139
+ // A node's width in px, read off the root as `spacing` reads a role: the
140
+ // `node` width follows the theme's size.
141
+ const nodeWidth = (): number =>
142
+ Number.parseFloat(
143
+ getComputedStyle(document.documentElement).getPropertyValue(
144
+ "--container-node",
145
+ ),
146
+ );
147
+
148
+ // A box of the flow for each node whose place and size are known.
149
+ function boxesOf(
150
+ nodes: readonly CanvasNode[],
151
+ places: Places,
152
+ sizes: ReadonlyMap<string, Size>,
153
+ ): Map<string, Box> {
154
+ const boxes = new Map<string, Box>();
155
+ for (const node of nodes) {
156
+ const size = sizes.get(node.id);
157
+ if (size) boxes.set(node.id, { ...place(node, places), ...size });
158
+ }
159
+ return boxes;
160
+ }
161
+
162
+ // One run of the layout. Each is its own object, so a rerun for the same
163
+ // structure (Arrange) is reported again.
164
+ export interface Laid {
165
+ positions: ReadonlyMap<string, CanvasPoint>;
166
+ // ELK's own output, whose top parts wrap in a narrow room (`wrapParts`).
167
+ output: ElkNode;
168
+ // It came from Arrange, which fits the view to it once it lands.
169
+ arranged: boolean;
170
+ // The nodes and empty groups it was run for (`setOf`).
171
+ set: string;
172
+ }
173
+
174
+ // The identity of a node set: the ids, in no order. A canvas whose set changes
175
+ // lays the graph out afresh and opens its view on the new one.
176
+ const setOf = (ids: readonly string[]): string => [...ids].sort().join("\0");
177
+
178
+ interface RunArgs {
179
+ nodes: readonly CanvasNode[];
180
+ edges: readonly CanvasEdge[];
181
+ groups: readonly CanvasGroup[];
182
+ // `pathOrder` of the nodes and the empty groups, which are leaves the layout
183
+ // places and no node: they take no position of the consumer's and no `onMove`.
184
+ order: readonly string[];
185
+ hollow: readonly string[];
186
+ sizes: ReadonlyMap<string, Size>;
187
+ region: RefObject<HTMLElement | null>;
188
+ viewport: Viewport;
189
+ // The hidden probe (a new element when its parts change, so the observer is
190
+ // given them again), and whether anything needs it (a group head or a chip).
191
+ probe: HTMLElement | null;
192
+ probed: boolean;
193
+ // The positions a drag holds and a landing chose, which `place` reads.
194
+ live: ReadonlyMap<string, CanvasPoint>;
195
+ landed: ReadonlyMap<string, CanvasPoint>;
196
+ onMove?: (id: string, position: CanvasPoint) => void;
197
+ // Each node draws an in port.
198
+ ports: boolean;
199
+ // A glyph's size on screen at the density: under the text floor the routes
200
+ // and the frames follow it.
201
+ glyph: number;
202
+ }
203
+
204
+ export interface GraphRun extends Pick<Opening, "tierAt" | "routeTier"> {
205
+ // The nodes' boxes, as placed.
206
+ boxes: ReadonlyMap<string, Box>;
207
+ // Where each node draws at the zoom in force: as placed above the text
208
+ // floor, spread under it by the group heads' growth (`glyphTier`).
209
+ drawn: ReadonlyMap<string, Box>;
210
+ // The box a node or a group's frame stands in at the zoom in force, by its id.
211
+ standing: (id: string | undefined) => Box | undefined;
212
+ // Where a node stands at a scale, which a zoom about it holds it by.
213
+ about: (id: string) => About;
214
+ routed: Routes;
215
+ space: Space;
216
+ // The last run, `null` until one lands.
217
+ laid: Laid | null;
218
+ // The node set the graph holds now (`setOf`), which `laid.set` is compared to.
219
+ set: string;
220
+ // No node has a position, so the layout places them all.
221
+ placing: boolean;
222
+ // Every node has its size.
223
+ measured: boolean;
224
+ // Every node is measured, the probe read and the graph placed: a view can be
225
+ // set on it.
226
+ placed: boolean;
227
+ // What wraps the parts: the room and the gaps the layout leaves between
228
+ // them. It moves when the parts wrap again for a resized room.
229
+ room: string;
230
+ // Runs the layout again, every position ignored.
231
+ arrange: () => void;
232
+ // How far the zoom stack's column reaches in from the pane's start, a
233
+ // `pair` past it: a group head's words start past it (`headShift`).
234
+ clearLeft: number;
235
+ // How far the stack reaches up the pane, from its foot.
236
+ clearStack: number;
237
+ }
238
+
239
+ // ELK runs for an input only when its key (`graphKey`) moves from the last run's:
240
+ // the key to run for, or `null` while the last run holds this input.
241
+ export function runKey(last: string | null, input: ElkInput): string | null {
242
+ const key = graphKey(input);
243
+ return key === last ? null : key;
244
+ }
245
+
246
+ // What a graph draws, routes to and frames at a routing side: the cards' boxes
247
+ // above the floor (side 0), the glyphs' squares under it, where a group's head
248
+ // draws at its own size on screen, so the band it covers and the padding round
249
+ // the glyphs grow by what the zoom takes (the side's step over the glyph) and the
250
+ // nodes spread by that growth (`glyphTier`). A glyph draws no port.
251
+ export function tiers(
252
+ graph: Omit<Parameters<typeof routeEdges>[0], "boxes"> & {
253
+ boxes: ReadonlyMap<string, Box>;
254
+ holes: ReadonlyMap<string, Box>;
255
+ glyph: number;
256
+ },
257
+ ): Pick<Opening, "tierAt" | "routeTier"> {
258
+ const { boxes, holes, groups, glyph, head, pad, left, ports } = graph;
259
+ return {
260
+ tierAt: (side) =>
261
+ glyphTier(boxes, holes, groups, { side, glyph, head, pad, left }),
262
+ routeTier: (side, tier) =>
263
+ routeEdges({
264
+ ...graph,
265
+ boxes: tier.boxes,
266
+ head: tier.head,
267
+ pad: tier.pad,
268
+ ports: ports && side === 0,
269
+ }),
270
+ };
271
+ }
272
+
273
+ // Measures, runs, reports and routes. ELK places nodes when no node has a
274
+ // position, once every node is measured and the probe read, and not again for
275
+ // the same input (`graphKey`); Arrange runs it again whatever the positions. Its
276
+ // worker is imported when first wanted: a graph the consumer placed never loads
277
+ // it.
278
+ export function useGraphRun({
279
+ nodes,
280
+ edges,
281
+ groups,
282
+ order,
283
+ hollow,
284
+ sizes,
285
+ region,
286
+ viewport,
287
+ probe,
288
+ probed,
289
+ live,
290
+ landed,
291
+ onMove,
292
+ ports,
293
+ glyph,
294
+ }: RunArgs): GraphRun {
295
+ const placing =
296
+ (nodes.length > 0 || hollow.length > 0) &&
297
+ nodes.every((node) => !node.position);
298
+ const measured = nodes.every((node) => sizes.has(node.id));
299
+ const width = nodeWidth();
300
+ const read = useMeasures(probe);
301
+ const measures = probed ? read : NONE;
302
+ const chip = Math.max(
303
+ 0,
304
+ ...[...(measures?.labels.values() ?? [])].map((s) => s.height),
305
+ );
306
+ const space = spaces(chip);
307
+ const { pad, pair, port } = space;
308
+ const { node: across, layer: down, page } = space.gaps;
309
+ const reach = measures?.reach;
310
+ const lefts = useMemo(
311
+ () => leftPads(groups, reach ?? NONE.reach, { pad, pair, width }),
312
+ [groups, reach, pad, pair, width],
313
+ );
314
+ // What ELK reads: sizes, never positions, so any run of the same input is the
315
+ // same layout. A node's height is its measured one, so a node that grows
316
+ // after a run (a gate whose answers' words gain a line) runs it again.
317
+ const input = useMemo(
318
+ (): ElkInput | null =>
319
+ measures
320
+ ? {
321
+ nodes,
322
+ edges,
323
+ groups,
324
+ order,
325
+ sizes,
326
+ width,
327
+ head: measures.head,
328
+ pad,
329
+ left: lefts,
330
+ gaps: { node: across, layer: down, page },
331
+ }
332
+ : null,
333
+ [
334
+ measures,
335
+ nodes,
336
+ edges,
337
+ groups,
338
+ order,
339
+ sizes,
340
+ width,
341
+ pad,
342
+ lefts,
343
+ across,
344
+ down,
345
+ page,
346
+ ],
347
+ );
348
+ const key = useMemo(() => (input ? graphKey(input) : null), [input]);
349
+ const [laid, setLaid] = useState<Laid | null>(null);
350
+ // A run that failed (its chunk did not load, or ELK threw) is thrown where the
351
+ // canvas renders, for the error boundary above it.
352
+ const [failure, setFailure] = useState<{ error: unknown } | null>(null);
353
+ const current = useRef(key);
354
+ const ran = useRef<string | null>(null);
355
+ // A run is in flight from its request to its result.
356
+ const flight = useRef(false);
357
+ const reported = useRef<Laid | null>(null);
358
+
359
+ useEffect(() => {
360
+ current.current = key;
361
+ }, [key]);
362
+
363
+ // A run's result lands only while its input is still the one in force.
364
+ const run = useCallback((given: ElkInput, at: string, arranged: boolean) => {
365
+ flight.current = true;
366
+ const graph = elkGraph(given);
367
+ const set = setOf(given.order);
368
+ import("@slotform/react-ui/lib/canvas-layout")
369
+ .then(({ layoutElk }) => layoutElk(graph))
370
+ .then(
371
+ (output) => {
372
+ flight.current = false;
373
+ if (current.current === at)
374
+ setLaid({ positions: fromElk(output), output, arranged, set });
375
+ },
376
+ (error: unknown) => {
377
+ flight.current = false;
378
+ setFailure({ error });
379
+ },
380
+ );
381
+ }, []);
382
+
383
+ useEffect(() => {
384
+ if (!placing || !measured || !input) return;
385
+ const due = runKey(ran.current, input);
386
+ if (due === null) return;
387
+ ran.current = due;
388
+ run(input, due, false);
389
+ }, [placing, measured, input, run]);
390
+
391
+ const arrange = () => {
392
+ if (flight.current || !measured || !input || !key) return;
393
+ run(input, key, true);
394
+ };
395
+
396
+ // The width the opening view has at the text floor: the pane less its inset
397
+ // and the zoom stack's clearance, as `fitTransform`'s room. The parts of a
398
+ // graph no edge links wrap to it (`wrapParts`), so it is read here and again
399
+ // as the pane resizes: CSS cannot place what ELK placed.
400
+ const [wrapSpace, setWrapSpace] = useState(Number.POSITIVE_INFINITY);
401
+ const [clearLeft, setClearLeft] = useState(0);
402
+ const [clearStack, setClearStack] = useState(0);
403
+ useLayoutEffect(() => {
404
+ const element = region.current;
405
+ if (!element) return;
406
+ const read = () => {
407
+ const inset = spacing("page");
408
+ const clear = clearanceOf(element);
409
+ setWrapSpace(viewport.size().width - inset - Math.max(inset, clear.left));
410
+ setClearLeft(clear.left);
411
+ setClearStack(clear.stack);
412
+ };
413
+ read();
414
+ const watch = new ResizeObserver(read);
415
+ watch.observe(element);
416
+ return () => watch.disconnect();
417
+ }, [region, viewport]);
418
+ const computed = useMemo(
419
+ () =>
420
+ laid
421
+ ? wrapParts(laid.output, laid.positions, edges, wrapSpace, {
422
+ across,
423
+ down,
424
+ })
425
+ : undefined,
426
+ [laid, edges, wrapSpace, across, down],
427
+ );
428
+
429
+ useEffect(() => {
430
+ if (!onMove || !laid || !computed || reported.current === laid) return;
431
+ reported.current = laid;
432
+ for (const id of order) {
433
+ const at = computed.get(id);
434
+ if (at && !hollow.includes(id)) onMove(id, at);
435
+ }
436
+ }, [laid, computed, onMove, order, hollow]);
437
+
438
+ const boxes = useMemo(
439
+ () => boxesOf(nodes, { live, landed, computed }, sizes),
440
+ [nodes, live, landed, computed, sizes],
441
+ );
442
+ const back = useMemo(() => backEdges(order, edges), [order, edges]);
443
+ const head = measures?.head ?? 0;
444
+ // An empty group stands where the layout put it, at the size of its head and
445
+ // padding. Where no layout ran (every node has a position) the ones left
446
+ // stand in one row below the nodes, in path order. Under the floor it keeps
447
+ // its frame, as every frame does.
448
+ const holes = useMemo(() => {
449
+ const size = { width, height: head + 2 * pad };
450
+ const out = new Map<string, Box>();
451
+ for (const id of hollow) {
452
+ const at = computed?.get(id);
453
+ if (at) out.set(id, { ...at, ...size });
454
+ }
455
+ const unplaced = order.filter((id) => hollow.includes(id) && !out.has(id));
456
+ const row = hollowRow(unplaced, boxes.values(), size, {
457
+ gap: across,
458
+ drop: down,
459
+ });
460
+ for (const [id, box] of row) out.set(id, box);
461
+ return out;
462
+ }, [hollow, order, computed, boxes, head, pad, across, down, width]);
463
+ // Under the floor a node is its glyph, so the routes and the frames follow
464
+ // the glyph's box, centred on the card's, and not the card's. The cards'
465
+ // sizes stay as they are, and their places only spread (`glyphTier`). The
466
+ // side moves in steps of a `pair`, which bounds how often the routes are
467
+ // drawn while the zoom moves.
468
+ const side = useViewportValue(viewport, (view) =>
469
+ routeSide(view.k, glyph, pair),
470
+ );
471
+ const labels = measures?.labels ?? NONE.labels;
472
+ const { tierAt, routeTier } = useMemo(
473
+ () =>
474
+ tiers({
475
+ boxes,
476
+ holes,
477
+ groups,
478
+ edges,
479
+ back,
480
+ labels,
481
+ glyph,
482
+ head,
483
+ pad,
484
+ left: lefts,
485
+ pair,
486
+ ports,
487
+ port,
488
+ }),
489
+ [
490
+ boxes,
491
+ holes,
492
+ groups,
493
+ edges,
494
+ back,
495
+ labels,
496
+ glyph,
497
+ head,
498
+ pad,
499
+ lefts,
500
+ pair,
501
+ ports,
502
+ port,
503
+ ],
504
+ );
505
+ const tier = useMemo(() => tierAt(side), [tierAt, side]);
506
+ const drawn = tier.cards;
507
+ const routed = useMemo(() => routeTier(side, tier), [routeTier, side, tier]);
508
+ const about = useCallback(
509
+ (id: string): About =>
510
+ (k: number) =>
511
+ tierAt(routeSide(k, glyph, pair)).cards.get(id),
512
+ [tierAt, glyph, pair],
513
+ );
514
+
515
+ // The box a node or a group's frame stands in, by its id.
516
+ const standing = useCallback(
517
+ (id: string | undefined) =>
518
+ id === undefined ? undefined : (drawn.get(id) ?? routed.frames.get(id)),
519
+ [drawn, routed],
520
+ );
521
+ const set = useMemo(() => setOf(order), [order]);
522
+
523
+ if (failure) throw failure.error;
524
+ return {
525
+ boxes,
526
+ drawn,
527
+ standing,
528
+ about,
529
+ routed,
530
+ tierAt,
531
+ routeTier,
532
+ space,
533
+ laid,
534
+ set,
535
+ placing,
536
+ measured,
537
+ placed: measured && measures !== null && (!placing || laid !== null),
538
+ room: `${wrapSpace}|${across}|${down}`,
539
+ arrange,
540
+ clearLeft,
541
+ clearStack,
542
+ };
543
+ }