@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,621 @@
1
+ import type { CanvasNode, CanvasPoint } from "@slotform/ui-core/descriptors";
2
+ import { edgeDepth } from "@slotform/ui-core/drag";
3
+ import type { Box, Size } from "./geometry.ts";
4
+
5
+ const ORIGIN: CanvasPoint = { x: 0, y: 0 };
6
+
7
+ // The places a node's position can come from besides its own: a drag's live
8
+ // one, a landing's, and the layout's.
9
+ export interface Places {
10
+ live: ReadonlyMap<string, CanvasPoint>;
11
+ landed: ReadonlyMap<string, CanvasPoint>;
12
+ computed?: ReadonlyMap<string, CanvasPoint>;
13
+ }
14
+
15
+ // Where a node stands: what the pointer holds beats what the consumer gave,
16
+ // which beats a landing, which beats the layout, which beats the origin.
17
+ export function place(
18
+ node: Pick<CanvasNode, "id" | "position">,
19
+ { live, landed, computed }: Places,
20
+ ): CanvasPoint {
21
+ return (
22
+ live.get(node.id) ??
23
+ node.position ??
24
+ landed.get(node.id) ??
25
+ computed?.get(node.id) ??
26
+ ORIGIN
27
+ );
28
+ }
29
+
30
+ export function landAt(centre: CanvasPoint, size: Size): CanvasPoint {
31
+ return { x: centre.x - size.width / 2, y: centre.y - size.height / 2 };
32
+ }
33
+
34
+ // Pure maths of the view: where the layer stands for a graph and a pane. A
35
+ // transform maps a flow point `p` to the pane point `p * k + (x, y)`.
36
+ export interface Transform {
37
+ x: number;
38
+ y: number;
39
+ k: number;
40
+ }
41
+
42
+ // Whether a box of the flow stands whole inside the pane under the transform,
43
+ // inset by `inset` on every side.
44
+ export function inside(
45
+ box: Box,
46
+ transform: Transform,
47
+ pane: Size,
48
+ inset: number,
49
+ ): boolean {
50
+ const left = box.x * transform.k + transform.x;
51
+ const top = box.y * transform.k + transform.y;
52
+ return (
53
+ left >= inset &&
54
+ top >= inset &&
55
+ left + box.width * transform.k <= pane.width - inset &&
56
+ top + box.height * transform.k <= pane.height - inset
57
+ );
58
+ }
59
+
60
+ // A transform, or a node's dropped position, at whole pixels:
61
+ // docs/architecture/plugins/react-ui.md, "Canvas".
62
+ export const whole = <T extends CanvasPoint>(point: T): T => ({
63
+ ...point,
64
+ x: Math.round(point.x),
65
+ y: Math.round(point.y),
66
+ });
67
+
68
+ // The dot grid's pitch, in flow pixels.
69
+ export const PITCH = 16;
70
+
71
+ const ARROWS: Record<string, CanvasPoint> = {
72
+ ArrowLeft: { x: -1, y: 0 },
73
+ ArrowRight: { x: 1, y: 0 },
74
+ ArrowUp: { x: 0, y: -1 },
75
+ ArrowDown: { x: 0, y: 1 },
76
+ };
77
+
78
+ // What an arrow key moves a focused node by, in flow pixels: a quarter of the
79
+ // grid's pitch, and with Shift four times that, one pitch. Undefined for any
80
+ // other key.
81
+ export function keyStep(key: string, shift: boolean): CanvasPoint | undefined {
82
+ const arrow = ARROWS[key];
83
+ if (!arrow) return undefined;
84
+ const step = shift ? PITCH : PITCH / 4;
85
+ return { x: arrow.x * step, y: arrow.y * step };
86
+ }
87
+
88
+ // A plain wheel's pan, in the wheel's own units: with Shift and no sideways
89
+ // delta (a mouse wheel off a Mac, where the system does not turn it) the
90
+ // vertical delta pans sideways.
91
+ export function wheelPan(wheel: {
92
+ deltaX: number;
93
+ deltaY: number;
94
+ shiftKey: boolean;
95
+ }): CanvasPoint {
96
+ return wheel.shiftKey && wheel.deltaX === 0
97
+ ? { x: wheel.deltaY, y: 0 }
98
+ : { x: wheel.deltaX, y: wheel.deltaY };
99
+ }
100
+
101
+ // How deep a drag at `point` in the pane (pane pixels from its top left)
102
+ // stands in the pane's edge band on each axis, as the translate it asks of
103
+ // the view: toward 1 at the start edge, so the view moves right or down and
104
+ // what lies past that edge comes in, toward -1 at the end edge, 0 in the
105
+ // middle. A scroller's band and speed (`edgeDepth`, `edgeSpeed`), with the
106
+ // sign turned, since a pan moves the content where a scroll moves the view.
107
+ export function autoPan(point: CanvasPoint, pane: Size): CanvasPoint {
108
+ return {
109
+ x: 0 - edgeDepth(point.x, 0, pane.width),
110
+ y: 0 - edgeDepth(point.y, 0, pane.height),
111
+ };
112
+ }
113
+
114
+ // The scale a pan or zoom stays within; the lower end rises with the graph
115
+ // (`minZoomFor`).
116
+ export const EXTENT: readonly [number, number] = [0.1, 2];
117
+
118
+ // A node at its own size: the one zoom its tokens are drawn for, and the text
119
+ // floor's threshold.
120
+ export const ZOOM_TO_NODE = 1;
121
+
122
+ export const paneOf = (
123
+ point: CanvasPoint,
124
+ { x, y, k }: Transform,
125
+ ): CanvasPoint => ({
126
+ x: point.x * k + x,
127
+ y: point.y * k + y,
128
+ });
129
+
130
+ export const centreOf = (box: Box): CanvasPoint => ({
131
+ x: box.x + box.width / 2,
132
+ y: box.y + box.height / 2,
133
+ });
134
+
135
+ // The transform at scale `k` that keeps the flow point under `anchor` (a pane
136
+ // point) where it stands: the one zoom a tap, a button and a pinch share.
137
+ export function zoomAbout(
138
+ transform: Transform,
139
+ anchor: CanvasPoint,
140
+ k: number,
141
+ ): Transform {
142
+ const ratio = k / transform.k;
143
+ return {
144
+ k,
145
+ x: anchor.x - (anchor.x - transform.x) * ratio,
146
+ y: anchor.y - (anchor.y - transform.y) * ratio,
147
+ };
148
+ }
149
+
150
+ // Where a span of `size` starting at `start` stands within a pane's `extent`,
151
+ // inset on both ends: moved just enough to stand whole, or to the near end when
152
+ // it is longer than the room.
153
+ function clamp(start: number, size: number, extent: number, inset: number) {
154
+ if (start < inset || extent - 2 * inset < size) return inset;
155
+ return Math.min(start, extent - inset - size);
156
+ }
157
+
158
+ // The transform shifted just enough that the box (a box of the flow) stands whole
159
+ // in the pane, inset on every side; unchanged when it already does. A box larger
160
+ // than the pane keeps its top left in view.
161
+ export function shiftInto(
162
+ box: Box,
163
+ transform: Transform,
164
+ pane: Size,
165
+ inset: number,
166
+ ): Transform {
167
+ const { k } = transform;
168
+ const left = box.x * k + transform.x;
169
+ const top = box.y * k + transform.y;
170
+ return {
171
+ k,
172
+ x: transform.x + clamp(left, box.width * k, pane.width, inset) - left,
173
+ y: transform.y + clamp(top, box.height * k, pane.height, inset) - top,
174
+ };
175
+ }
176
+
177
+ // The transform that puts the box's centre on the pane's centre at scale `k`.
178
+ function centredTransform(box: Box, k: number, pane: Size): Transform {
179
+ const centre = centreOf(box);
180
+ return {
181
+ k,
182
+ x: pane.width / 2 - centre.x * k,
183
+ y: pane.height / 2 - centre.y * k,
184
+ };
185
+ }
186
+
187
+ // How far the canvas's own chrome reaches in from the pane's left and bottom
188
+ // edges (the zoom stack and the act): a fitted graph stays outside it.
189
+ export interface Clearance {
190
+ left: number;
191
+ bottom: number;
192
+ // How far the part against the left edge reaches up from the pane's bottom.
193
+ stack: number;
194
+ }
195
+
196
+ export const NO_CLEARANCE: Clearance = { left: 0, bottom: 0, stack: 0 };
197
+
198
+ // Whether a span down the pane, `top` to `bottom` in pane pixels, stands in the
199
+ // rows the zoom stack reaches up the pane (`stack`, from its foot).
200
+ export const inStackRows = (
201
+ top: number,
202
+ bottom: number,
203
+ pane: Size,
204
+ stack: number,
205
+ ): boolean => bottom > pane.height - stack && top < pane.height;
206
+
207
+ // The transform moved, at its scale, so the box (a box of the flow) stands
208
+ // clear of the zoom stack's corner (the columns `clear.left` reaches across,
209
+ // the rows `clear.stack` reaches up): past the stack where the pane holds the
210
+ // box there, inset from its end, else over it. Unchanged where the box already
211
+ // stands clear, or where neither move holds it whole.
212
+ export function clearOfStack(
213
+ box: Box,
214
+ transform: Transform,
215
+ pane: Size,
216
+ inset: number,
217
+ clear: Clearance,
218
+ ): Transform {
219
+ const { k } = transform;
220
+ const left = box.x * k + transform.x;
221
+ const bottom = (box.y + box.height) * k + transform.y;
222
+ const top = pane.height - clear.stack;
223
+ if (left >= clear.left || bottom <= top) return transform;
224
+ if (clear.left + box.width * k <= pane.width - inset)
225
+ return { ...transform, x: transform.x + clear.left - left };
226
+ if (box.height * k <= top - inset)
227
+ return { ...transform, y: transform.y + top - bottom };
228
+ return transform;
229
+ }
230
+
231
+ // Whether the box stands whole in the pane, inset, and clear of the zoom
232
+ // stack's corner at the transform (`clearOfStack` has nothing to move). It
233
+ // relies on `clearOfStack` returning the very object it was given when nothing
234
+ // moves, so the identity check below is the "nothing to move" test.
235
+ export function standsIn(
236
+ box: Box,
237
+ transform: Transform,
238
+ pane: Size,
239
+ inset: number,
240
+ clear: Clearance,
241
+ ): boolean {
242
+ return (
243
+ inside(box, transform, pane, inset) &&
244
+ clearOfStack(box, transform, pane, inset, clear) === transform
245
+ );
246
+ }
247
+
248
+ // The transform at scale `k` that stands the box where the canvas's own chrome
249
+ // leaves room: its centre on the middle of the room a fit fills (`room`), then
250
+ // clear of the zoom stack's corner, so a node brought into view is never one the
251
+ // controls cover.
252
+ export function standIn(
253
+ box: Box,
254
+ k: number,
255
+ pane: Size,
256
+ inset: number,
257
+ clear: Clearance,
258
+ ): Transform {
259
+ const area = space(pane, inset, clear);
260
+ const centre = centredTransform(box, k, area);
261
+ return clearOfStack(
262
+ box,
263
+ { k, x: centre.x + area.x, y: centre.y + area.y },
264
+ pane,
265
+ inset,
266
+ clear,
267
+ );
268
+ }
269
+
270
+ // The part of the pane a fit may fill: inset on every side, and past the
271
+ // chrome on the left and the bottom.
272
+ export function space(pane: Size, inset: number, clear: Clearance): Box {
273
+ const x = Math.max(inset, clear.left);
274
+ const bottom = Math.max(inset, clear.bottom);
275
+ return {
276
+ x,
277
+ y: inset,
278
+ width: pane.width - inset - x,
279
+ height: pane.height - inset - bottom,
280
+ };
281
+ }
282
+
283
+ // The scale a fit never goes under: the one at which a node draws its title
284
+ // and its line, its own size (`ZOOM_TO_NODE`, the text floor). A graph that
285
+ // holds only under it is fitted at it and cut at the far edge, so the words stay
286
+ // readable and the reader pans for the rest; a zoom by hand still goes under.
287
+ export const FIT_FLOOR = ZOOM_TO_NODE;
288
+
289
+ // The highest scale the opening view takes while a graph is drawn as its
290
+ // overview: just under the text floor, where a node is still a glyph.
291
+ export const OPEN_UNDER = 0.99;
292
+
293
+ // The scale an overview opens at: the highest one, no higher than `from`, at
294
+ // which what the graph draws takes no more than `room` px across (or down) at
295
+ // that scale, and no lower than `lowest`. `extent(k)` is what the graph takes on
296
+ // screen at scale `k`: the node centres stand `spread` flow units apart and
297
+ // everything else (a glyph, a frame's padding, a head, the last name) keeps its
298
+ // size on screen, so the extent is a constant plus `spread * k` and each step
299
+ // solves that line, measured again at the scale it gave (the frames grow in
300
+ // steps as the zoom moves). A graph that does not hold in the room at `lowest`
301
+ // opens at `lowest` anyway.
302
+ export function solveScale(
303
+ extent: (k: number) => number,
304
+ spread: number,
305
+ space: number,
306
+ lowest: number,
307
+ from: number = OPEN_UNDER,
308
+ ): number {
309
+ let k = Math.max(lowest, from);
310
+ for (let step = 0; step < 6; step++) {
311
+ const taken = extent(k);
312
+ if (taken <= space + 1e-6) return k;
313
+ if (k <= lowest || spread <= 0) break;
314
+ const next = (space - (taken - spread * k)) / spread;
315
+ k = Math.max(lowest, Math.min(next, k - 1e-3));
316
+ }
317
+ return k;
318
+ }
319
+
320
+ // The offset that centres a span in the room on its axis, at scale `k`.
321
+ const centred = (
322
+ span: { start: number; size: number },
323
+ space: { origin: number; extent: number },
324
+ k: number,
325
+ ): number => space.origin + (space.extent - span.size * k) / 2 - span.start * k;
326
+
327
+ // Where a span of `size` stands on one axis of a room of `extent` from `origin`,
328
+ // as the offset the transform adds: centred when it fits, else its start
329
+ // (`start`, the span's own offset) at the room's start; and, with a `centre`
330
+ // (the graph's start node's, as an offset in the span), that centre on the
331
+ // room's, held so the span still covers the room's ends.
332
+ function framed(
333
+ span: { start: number; size: number },
334
+ space: { origin: number; extent: number },
335
+ k: number,
336
+ centre?: number,
337
+ ): number {
338
+ if (span.size * k <= space.extent) return centred(span, space, k);
339
+ const first = space.origin - span.start * k;
340
+ if (centre === undefined) return first;
341
+ const wanted = space.origin + space.extent / 2 - centre * k;
342
+ // The span's start may not pass the room's, nor its end stop short of it.
343
+ return Math.max(
344
+ space.origin + space.extent - (span.start + span.size) * k,
345
+ Math.min(first, wanted),
346
+ );
347
+ }
348
+
349
+ // What a graph holds that a framed view keeps clear: its nodes' boxes (kept
350
+ // clear of the zoom stack's corner) and where a node's icon stands across it,
351
+ // from and to its left edge: the part of a node the stack must leave clear where
352
+ // no offset clears whole nodes. A group head needs no room of its own: its
353
+ // words stand past the stack's column while its frame runs under or off it
354
+ // (`headShift`),
355
+ // so a framed view stays centred on its start node.
356
+ export interface Parts {
357
+ nodes: readonly Box[];
358
+ icon?: { start: number; end: number };
359
+ }
360
+
361
+ // The offset `x` of a framed graph (scale `k`, offset `y` down) moved where the
362
+ // parts ask. The start node stands whole in the pane, inset (or against its
363
+ // start where it is wider than that), at every offset this gives: the window
364
+ // it allows bounds every move below. The offset is moved clear of the chrome's
365
+ // corner, no node's box meeting the zoom stack's,
366
+ // to whichever of the offsets the graph may stand at (`lo` to `hi`: its far end
367
+ // at the room's, its start at the room's, within the start's window) is
368
+ // nearest, and where none clears whole nodes, to the nearest that leaves every
369
+ // node's icon clear. Where neither is, the offset stays.
370
+ function kept(
371
+ x: number,
372
+ view: { k: number; y: number; lo: number; hi: number },
373
+ space: { inset: number; pane: Size; clear: Clearance },
374
+ parts: Parts,
375
+ start?: Box,
376
+ ): number {
377
+ const { k, y } = view;
378
+ const { inset, pane, clear } = space;
379
+ let { lo, hi } = view;
380
+ let at = x;
381
+ if (start) {
382
+ // The start node stands at the pane's top, over the zoom stack's rows, so
383
+ // its window is the pane's, inset, and not the room past the stack.
384
+ const left = inset - start.x * k;
385
+ const right =
386
+ start.width * k > pane.width - 2 * inset
387
+ ? left
388
+ : pane.width - inset - (start.x + start.width) * k;
389
+ // The start's window within the graph's, unless the two do not meet.
390
+ if (Math.max(lo, left) <= Math.min(hi, right)) {
391
+ lo = Math.max(lo, left);
392
+ hi = Math.min(hi, right);
393
+ }
394
+ at = Math.min(hi, Math.max(lo, x));
395
+ }
396
+ // The nodes in the rows the stack reaches up the pane.
397
+ const rows = parts.nodes.filter((node) =>
398
+ inStackRows(
399
+ node.y * k + y,
400
+ (node.y + node.height) * k + y,
401
+ pane,
402
+ clear.stack,
403
+ ),
404
+ );
405
+ // The offsets that put a node's part (its whole box, or the span `part` from
406
+ // its left edge) in the stack's columns.
407
+ const clearOf = (part?: { start: number; end: number }) => {
408
+ const meets = rows.map((node) => {
409
+ const from = node.x + (part?.start ?? 0);
410
+ const to = node.x + (part?.end ?? node.width);
411
+ return [-to * k, clear.left - from * k] as const;
412
+ });
413
+ const free = (offset: number) =>
414
+ meets.every(([from, to]) => from >= offset || offset >= to);
415
+ if (free(at)) return at;
416
+ const options = [lo, hi, ...meets.flat()].filter(
417
+ (offset) => Math.min(hi, Math.max(lo, offset)) === offset && free(offset),
418
+ );
419
+ return options.length === 0
420
+ ? undefined
421
+ : options.reduce((best, one) =>
422
+ Math.abs(best - at) > Math.abs(one - at) ? one : best,
423
+ );
424
+ };
425
+ return (
426
+ clearOf() ??
427
+ (parts.icon === undefined ? undefined : clearOf(parts.icon)) ??
428
+ at
429
+ );
430
+ }
431
+
432
+ // The offset `y` of a framed graph moved up just far enough that the anchor (the
433
+ // graph's first node, or the running or failed one) stands whole in the room,
434
+ // never so far that its top leaves the room's.
435
+ function raised(
436
+ y: number,
437
+ anchor: Box | undefined,
438
+ k: number,
439
+ area: Box,
440
+ ): number {
441
+ if (!anchor) return y;
442
+ const over = (anchor.y + anchor.height) * k + y - (area.y + area.height);
443
+ if (over <= 0) return y;
444
+ return Math.max(y - over, area.y - anchor.y * k);
445
+ }
446
+
447
+ // The transform that stands the bounds whole in the room the pane leaves,
448
+ // centred there, at the scale that fits and never above 1: a small graph keeps
449
+ // its own size. The scale never falls under `floor`: the opening view's is
450
+ // `FIT_FLOOR`, the text floor, and the Fit act's is the lowest scale the pan and
451
+ // zoom allow, so Fit shows the whole graph wherever that scale holds it. A graph
452
+ // that holds only under the floor is framed at its start instead, its top at
453
+ // the room's top and `start` (the box of the graph's first node), when it is
454
+ // wider than the room, at the room's middle, so a graph narrower than the room
455
+ // stays centred across. A framed view also keeps the
456
+ // start node whole and no node under the zoom stack's corner (`kept`).
457
+ export function fitTransform(
458
+ bounds: Box,
459
+ pane: Size,
460
+ inset: number,
461
+ clear: Clearance,
462
+ start: Box | undefined,
463
+ parts: Parts,
464
+ floor: number = FIT_FLOOR,
465
+ ): Transform {
466
+ const area = space(pane, inset, clear);
467
+ const fits = Math.min(
468
+ 1,
469
+ area.width / bounds.width,
470
+ area.height / bounds.height,
471
+ );
472
+ const k = Math.max(floor, fits);
473
+ if (k === fits) {
474
+ const centre = centredTransform(bounds, k, area);
475
+ const y = centre.y + area.y;
476
+ // The room's start is the chrome's column, which only a graph in the
477
+ // stack's rows needs left clear: one that ends above them is centred
478
+ // across the pane, a margin the same on both sides, never pushed against
479
+ // the far one by a stack it does not stand over.
480
+ const above =
481
+ (bounds.y + bounds.height) * k + y <= pane.height - clear.stack;
482
+ const x = above
483
+ ? (pane.width - bounds.width * k) / 2 - bounds.x * k
484
+ : centre.x + area.x;
485
+ return { k, x, y };
486
+ }
487
+ const y = raised(
488
+ framed(
489
+ { start: bounds.y, size: bounds.height },
490
+ { origin: area.y, extent: area.height },
491
+ k,
492
+ ),
493
+ start,
494
+ k,
495
+ area,
496
+ );
497
+ const x = framed(
498
+ { start: bounds.x, size: bounds.width },
499
+ { origin: area.x, extent: area.width },
500
+ k,
501
+ start && start.x + start.width / 2,
502
+ );
503
+ return {
504
+ k,
505
+ x: kept(
506
+ x,
507
+ {
508
+ k,
509
+ y,
510
+ lo: area.x + area.width - (bounds.x + bounds.width) * k,
511
+ hi: area.x - bounds.x * k,
512
+ },
513
+ { inset, pane, clear },
514
+ parts,
515
+ start,
516
+ ),
517
+ y,
518
+ };
519
+ }
520
+
521
+ // The box with room for the name that stands beside its last glyph: `extra`
522
+ // px on screen at scale `k`, in the flow's units.
523
+ export function withNames(box: Box, extra: number, k: number): Box {
524
+ return { ...box, width: box.width + extra / k };
525
+ }
526
+
527
+ // Where a span of `size` starting at `start` stands along one axis of the room
528
+ // from `origin` for `extent`, as the offset the transform adds, at scale `k`:
529
+ // centred where it fits; else its start at the room's start, so the node nearest
530
+ // the start is never the one cut, and moved back only as far as the anchor needs
531
+ // to stand whole between `lo` and `hi` (its start winning where it is longer than
532
+ // that), never so far that the span's end leaves the room short.
533
+ function opened(
534
+ span: { start: number; size: number },
535
+ space: { origin: number; extent: number },
536
+ k: number,
537
+ anchor: { start: number; size: number } | undefined,
538
+ window: { lo: number; hi: number },
539
+ ): number {
540
+ if (span.size * k <= space.extent) return centred(span, space, k);
541
+ let offset = space.origin - span.start * k;
542
+ if (anchor) {
543
+ const end = (anchor.start + anchor.size) * k + offset;
544
+ if (end > window.hi) offset -= end - window.hi;
545
+ if (anchor.start * k + offset < window.lo)
546
+ offset = window.lo - anchor.start * k;
547
+ }
548
+ return Math.max(
549
+ space.origin + space.extent - (span.start + span.size) * k,
550
+ offset,
551
+ );
552
+ }
553
+
554
+ // The opening view of a graph drawn as its overview at scale `k`: `bounds` is
555
+ // what it draws (the glyphs, the frames and the last name, in flow units at that
556
+ // scale). Where that holds in the room past the zoom stack it is centred there,
557
+ // so nothing stands under the stack; where it does not (the scale is at its
558
+ // floor) the graph stands against the room's start, its top and its first column
559
+ // whole, and the viewer pans for the rest. The `anchor` (the first node, or the
560
+ // running or failed one) stands whole in the pane and clear of the stack.
561
+ export function overviewTransform(
562
+ bounds: Box,
563
+ k: number,
564
+ pane: Size,
565
+ inset: number,
566
+ clear: Clearance,
567
+ anchor: Box | undefined,
568
+ nodes: readonly Box[],
569
+ ): Transform {
570
+ const area = space(pane, inset, clear);
571
+ const view: Transform = {
572
+ k,
573
+ x: opened(
574
+ { start: bounds.x, size: bounds.width },
575
+ { origin: area.x, extent: area.width },
576
+ k,
577
+ anchor && { start: anchor.x, size: anchor.width },
578
+ { lo: inset, hi: pane.width - inset },
579
+ ),
580
+ y: opened(
581
+ { start: bounds.y, size: bounds.height },
582
+ { origin: area.y, extent: area.height },
583
+ k,
584
+ anchor && { start: anchor.y, size: anchor.height },
585
+ { lo: inset, hi: area.y + area.height },
586
+ ),
587
+ };
588
+ const placed = anchor ? clearOfStack(anchor, view, pane, inset, clear) : view;
589
+ return pastStack(nodes, placed, pane, inset, clear, anchor);
590
+ }
591
+
592
+ // The transform moved right, at its scale, just far enough that no node's box
593
+ // (`nodes`, the boxes the canvas draws: a glyph's box is its card's, wider than
594
+ // the glyph) stands in the zoom stack's columns where it reaches up the pane,
595
+ // as far as the anchor still stands whole in the pane, inset. The overview
596
+ // places its glyphs at the room's start, and the boxes round them reach back
597
+ // past it. Unchanged where every box already stands clear.
598
+ function pastStack(
599
+ nodes: readonly Box[],
600
+ view: Transform,
601
+ pane: Size,
602
+ inset: number,
603
+ clear: Clearance,
604
+ anchor?: Box,
605
+ ): Transform {
606
+ const { k } = view;
607
+ let by = 0;
608
+ for (const box of nodes) {
609
+ const left = box.x * k + view.x;
610
+ const top = box.y * k + view.y;
611
+ const bottom = (box.y + box.height) * k + view.y;
612
+ if (left < clear.left && inStackRows(top, bottom, pane, clear.stack))
613
+ by = Math.max(by, clear.left - left);
614
+ }
615
+ if (anchor)
616
+ by = Math.min(
617
+ by,
618
+ Math.max(0, pane.width - inset - (anchor.x + anchor.width) * k - view.x),
619
+ );
620
+ return by > 0 ? { ...view, x: view.x + by } : view;
621
+ }