@slotform/react-ui 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,417 @@
1
+ import type { CanvasPoint } from "@slotform/ui-core/descriptors";
2
+ import { type Band, bandAt, edgeSpeed } from "@slotform/ui-core/drag";
3
+ import { createStore } from "@slotform/ui-core/store";
4
+ import { select } from "d3-selection";
5
+ import { type D3ZoomEvent, zoom, zoomIdentity } from "d3-zoom";
6
+ import {
7
+ type RefObject,
8
+ useEffect,
9
+ useState,
10
+ useSyncExternalStore,
11
+ } from "react";
12
+ import { travel } from "../../lib/follow.ts";
13
+ import { spacing } from "../../lib/measure.ts";
14
+ import type { Box } from "./geometry.ts";
15
+ import {
16
+ autoPan,
17
+ type Clearance,
18
+ centreOf,
19
+ clearOfStack,
20
+ EXTENT,
21
+ NO_CLEARANCE,
22
+ PITCH,
23
+ paneOf,
24
+ shiftInto,
25
+ standIn,
26
+ standsIn,
27
+ type Transform,
28
+ wheelPan,
29
+ whole,
30
+ ZOOM_TO_NODE,
31
+ zoomAbout,
32
+ } from "./view.ts";
33
+
34
+ // A drag that pans the view near the pane's edge (`Viewport.edge`): the
35
+ // pointer's client position, and what the drag does again at it once the
36
+ // view has moved under it.
37
+ export interface EdgeDrag {
38
+ point: CanvasPoint;
39
+ follow: (point: CanvasPoint) => void;
40
+ }
41
+
42
+ // What a zoom stands about: a node's box, or the box it takes at a scale. Under
43
+ // the text floor the boxes spread as the scale moves (`glyphTier`), so a zoom
44
+ // about a node holds it where it stands at the scale it leaves, at the place it
45
+ // takes at the scale it reaches.
46
+ export type About = (k: number) => Box | undefined;
47
+
48
+ // The canvas's viewport: pan, wheel, pinch and the zoom stack over one layer.
49
+ // The rest of the canvas reaches the pan and zoom only through this object.
50
+ export interface Viewport {
51
+ get(): Transform;
52
+ // Every transform change, for a part that reads the zoom.
53
+ subscribe(listener: () => void): () => void;
54
+ // A step of the scale about a node's box, whose centre stays where it stands
55
+ // in the pane until the node would pass the pane's edge, when the view shifts
56
+ // just enough to hold it whole and clear of the zoom stack; the pane's
57
+ // centre without one. It travels.
58
+ zoomIn(about?: About): void;
59
+ zoomOut(about?: About): void;
60
+ // A node's own size (`ZOOM_TO_NODE`) about its box, as a step does.
61
+ zoomTo(about: About): void;
62
+ // Instant, within the scale extent.
63
+ set(transform: Transform): void;
64
+ // The view travels to the transform (within the scale extent) on `base`
65
+ // `in-out`, by the script's frames; a pan, a wheel or a pinch taking over
66
+ // ends it where it got to.
67
+ go(transform: Transform): void;
68
+ // What the canvas's own chrome takes from the pane, which a fit and the opening view stay clear of.
69
+ clearance(): Clearance;
70
+ // Pans only, at the zoom it has, and nothing when the box already stands in
71
+ // view and clear of the zoom stack. The box is centred in the room the chrome
72
+ // leaves (`standIn`). It travels.
73
+ centreOn(box: Box): void;
74
+ // Raises the lowest scale, and a scale already under it to it.
75
+ limit(lowest: number): void;
76
+ // Whether the viewer has panned or zoomed since the canvas last set the view
77
+ // itself (`settle`): a drag, a wheel, a pinch, a zoom step or a glyph's press.
78
+ // The canvas's own moves (the opening view, Fit, a reveal, the lowest-scale
79
+ // clamp) do not count.
80
+ moved(): boolean;
81
+ // The view is the canvas's own again: the viewer has not moved it since.
82
+ settle(): void;
83
+ // The pane's size, read off the region.
84
+ size(): { width: number; height: number };
85
+ // A pointer's client position as a flow point.
86
+ screenToFlow(point: CanvasPoint): CanvasPoint;
87
+ // A drag of a node or a link heard at each pointer move: while the pointer
88
+ // stands near the pane's edge the view pans toward it a step a frame
89
+ // (`autoPan`, `edgeSpeed`), the viewer's move, and the drag follows its
90
+ // pointer again after each step. `null` ends it.
91
+ edge(drag: EdgeDrag | null): void;
92
+ }
93
+
94
+ const STEP = 1.2;
95
+ // One wheel event moves the scale by at most the zoom stack's step, in d3's
96
+ // log2 measure; a trackpad pinch's small deltas stay proportional below it.
97
+ const MOST = Math.log2(STEP);
98
+ // The DOM's own approximation of a line in a wheel's `deltaMode`: Chrome and
99
+ // Firefox report pixels for a trackpad, so a line count comes from a mouse.
100
+ const LINE = 16;
101
+ // The dot grid's dots, in the pattern page's range: 1 px across, a `PITCH` apart.
102
+ export const DOT = 0.5;
103
+
104
+ // d3-zoom's own scale of a wheel's delta by its mode; a Ctrl or Cmd wheel (a
105
+ // trackpad pinch is a Ctrl wheel) zooms ten times faster.
106
+ const scaleOf = (event: WheelEvent): number => {
107
+ if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return 0.05;
108
+ return event.deltaMode === WheelEvent.DOM_DELTA_PAGE ? 1 : 0.002;
109
+ };
110
+
111
+ const pixels = (event: WheelEvent, page: number): number => {
112
+ if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return LINE;
113
+ return event.deltaMode === WheelEvent.DOM_DELTA_PAGE ? page : 1;
114
+ };
115
+
116
+ // A pan starts anywhere but on a part that carries its own drag (`data-no-pan`),
117
+ // and a wheel zooms with Ctrl or Cmd, which a trackpad pinch sends as Ctrl.
118
+ const pans = (event: MouseEvent | TouchEvent | WheelEvent): boolean => {
119
+ if (event instanceof WheelEvent) return event.ctrlKey || event.metaKey;
120
+ if (event.ctrlKey) return false;
121
+ if (event instanceof MouseEvent && event.button) return false;
122
+ return !(
123
+ event.target instanceof Element && event.target.closest("[data-no-pan]")
124
+ );
125
+ };
126
+
127
+ // What the canvas's chrome takes from the pane: docs/architecture/plugins/react-ui.md, "Canvas".
128
+ export function clearanceOf(element: HTMLElement | null): Clearance {
129
+ if (!element) return NO_CLEARANCE;
130
+ const pane = element.getBoundingClientRect();
131
+ const pair = spacing("gap");
132
+ const out = { left: 0, bottom: 0, stack: 0 };
133
+ for (const part of element.querySelectorAll<HTMLElement>("[data-clear]")) {
134
+ const box = part.getBoundingClientRect();
135
+ if (part.dataset.clear === "left") {
136
+ out.left = Math.max(out.left, box.right - pane.left + pair);
137
+ out.stack = Math.max(out.stack, pane.bottom - box.top + pair);
138
+ } else out.bottom = Math.max(out.bottom, pane.bottom - box.top + pair);
139
+ }
140
+ return out;
141
+ }
142
+
143
+ function make(region: RefObject<HTMLElement | null>) {
144
+ const held = createStore<Transform>({ x: 0, y: 0, k: 1 });
145
+ let layer: HTMLElement | null = null;
146
+ let pattern: SVGPatternElement | null = null;
147
+ // Whether the viewer moved the view since the canvas last set it.
148
+ let moved = false;
149
+
150
+ // The layer and the grid move by script, so a pan renders no React tree.
151
+ const apply = (to: Transform) => {
152
+ const next = { x: to.x, y: to.y, k: to.k };
153
+ const { x, y, k } = next;
154
+ if (layer) layer.style.transform = `translate(${x}px, ${y}px) scale(${k})`;
155
+ if (pattern) {
156
+ const pitch = PITCH * k;
157
+ pattern.setAttribute("x", String(x));
158
+ pattern.setAttribute("y", String(y));
159
+ pattern.setAttribute("width", String(pitch));
160
+ pattern.setAttribute("height", String(pitch));
161
+ // A dot's centre stands on a pixel's centre where the layer sits at whole
162
+ // pixels (scale 1), or it smears over four.
163
+ const dot = pattern.firstElementChild;
164
+ dot?.setAttribute("cx", String(Math.floor(x + pitch / 2) + 0.5 - x));
165
+ dot?.setAttribute("cy", String(Math.floor(y + pitch / 2) + 0.5 - y));
166
+ dot?.setAttribute("r", String(DOT * k));
167
+ }
168
+ held.set(next);
169
+ };
170
+
171
+ const behaviour = zoom<HTMLElement, unknown>()
172
+ .scaleExtent([...EXTENT])
173
+ .filter(pans)
174
+ .wheelDelta((event: WheelEvent) => {
175
+ const raw =
176
+ -event.deltaY *
177
+ scaleOf(event) *
178
+ (event.ctrlKey || event.metaKey ? 10 : 1);
179
+ return Math.max(-MOST, Math.min(MOST, raw));
180
+ })
181
+ .on("zoom", (event: D3ZoomEvent<HTMLElement, unknown>) => {
182
+ // A drag, a pinch and a Ctrl wheel carry their event; the canvas's own
183
+ // transforms (`land`) and the plain wheel's pan (below) do not.
184
+ if (event.sourceEvent) moved = true;
185
+ apply(event.transform);
186
+ });
187
+
188
+ const on = () => {
189
+ const element = region.current;
190
+ return element ? select(element) : null;
191
+ };
192
+ const pane = () => ({
193
+ width: region.current?.clientWidth ?? 0,
194
+ height: region.current?.clientHeight ?? 0,
195
+ });
196
+ const middle = (): CanvasPoint => {
197
+ const size = pane();
198
+ return { x: size.width / 2, y: size.height / 2 };
199
+ };
200
+
201
+ // The view that travels, if one does, and where it is headed; whatever sets
202
+ // the view or takes the pointer ends it. A step asked for while one travels
203
+ // is a step from where it is headed, so quick presses add up.
204
+ let travelling = () => {};
205
+ let heading: Transform | null = null;
206
+ const stop = () => {
207
+ travelling();
208
+ travelling = () => {};
209
+ heading = null;
210
+ };
211
+ const base = (): Transform => heading ?? held.get();
212
+
213
+ // The scale moves to `target` about the centre of the box `about`, else the
214
+ // pane's centre, and that point stays where it stands; a box is then kept
215
+ // whole in the pane and clear of the zoom stack (`clearOfStack`). A step or a
216
+ // glyph's press is the viewer's move.
217
+ const zoomTo = (target: number, about?: About) => {
218
+ const [low, high] = behaviour.scaleExtent();
219
+ const k = Math.min(high, Math.max(low, target));
220
+ moved = true;
221
+ const from = about?.(base().k);
222
+ const to = about?.(k);
223
+ if (!from || !to) {
224
+ viewport.go(zoomAbout(base(), middle(), k));
225
+ return;
226
+ }
227
+ // The box's centre stays where it stands in the pane, at its place at `k`.
228
+ const at = paneOf(centreOf(from), base());
229
+ const centre = centreOf(to);
230
+ const zoomed = { k, x: at.x - centre.x * k, y: at.y - centre.y * k };
231
+ const inset = spacing("page");
232
+ const size = pane();
233
+ const into = shiftInto(to, zoomed, size, inset);
234
+ viewport.go(
235
+ clearOfStack(to, into, size, inset, clearanceOf(region.current)),
236
+ );
237
+ };
238
+
239
+ // The transform on the layer: d3's own state moves with it, so the next
240
+ // pan or wheel starts from where the view stands.
241
+ const land = (transform: Transform) => {
242
+ const [low, high] = behaviour.scaleExtent();
243
+ const { x, y, k } = whole(transform);
244
+ const selection = on();
245
+ if (selection)
246
+ behaviour.transform(
247
+ selection,
248
+ zoomIdentity.translate(x, y).scale(Math.min(high, Math.max(low, k))),
249
+ );
250
+ };
251
+
252
+ // The drag near the edge; the frame its next step waits on (0 when none
253
+ // does); and the edge band the pointer stands in, which the speed ramps and
254
+ // steps over (`bandAt`, `edgeSpeed`). A step at zero depth ends the frames
255
+ // until the pointer moves.
256
+ let dragged: EdgeDrag | null = null;
257
+ let frame = 0;
258
+ let band: Band | undefined;
259
+ const step = (now: number) => {
260
+ frame = 0;
261
+ const element = region.current;
262
+ const rect = element?.getBoundingClientRect();
263
+ const depth =
264
+ dragged &&
265
+ rect &&
266
+ autoPan(
267
+ { x: dragged.point.x - rect.left, y: dragged.point.y - rect.top },
268
+ rect,
269
+ );
270
+ if (!dragged || !element || !depth || (depth.x === 0 && depth.y === 0)) {
271
+ band = undefined;
272
+ return;
273
+ }
274
+ // One number per edge and corner the pane pans toward.
275
+ const side = Math.sign(depth.x) * 3 + Math.sign(depth.y);
276
+ const { band: next, elapsed, since } = bandAt(band, element, side, now);
277
+ band = next;
278
+ stop();
279
+ moved = true;
280
+ const { k } = held.get();
281
+ behaviour.translateBy(
282
+ select(element),
283
+ edgeSpeed(depth.x, elapsed, since) / k,
284
+ edgeSpeed(depth.y, elapsed, since) / k,
285
+ );
286
+ frame = requestAnimationFrame(step);
287
+ dragged.follow(dragged.point);
288
+ };
289
+
290
+ const viewport: Viewport = {
291
+ get: held.get,
292
+ subscribe: held.subscribe,
293
+ zoomIn: (about) => zoomTo(base().k * STEP, about),
294
+ zoomOut: (about) => zoomTo(base().k / STEP, about),
295
+ zoomTo: (about) => zoomTo(ZOOM_TO_NODE, about),
296
+ // Every transform the canvas sets itself comes through here, at whole
297
+ // pixels; a wheel or a pinch stays as d3 gives it.
298
+ set(transform) {
299
+ stop();
300
+ land(transform);
301
+ },
302
+ go(transform) {
303
+ stop();
304
+ const [low, high] = behaviour.scaleExtent();
305
+ const { x, y, k } = whole(transform);
306
+ const to = { x, y, k: Math.min(high, Math.max(low, k)) };
307
+ heading = to;
308
+ travelling = travel(held.get(), to, (view) => {
309
+ land(view);
310
+ if (view === to) heading = null;
311
+ });
312
+ },
313
+ clearance: () => clearanceOf(region.current),
314
+ moved: () => moved,
315
+ settle() {
316
+ moved = false;
317
+ },
318
+ size: pane,
319
+ centreOn(box) {
320
+ const size = pane();
321
+ const inset = spacing("page");
322
+ const clear = clearanceOf(region.current);
323
+ if (standsIn(box, base(), size, inset, clear)) return;
324
+ viewport.go(standIn(box, base().k, size, inset, clear));
325
+ },
326
+ limit(lowest) {
327
+ behaviour.scaleExtent([lowest, EXTENT[1]]);
328
+ const { k } = held.get();
329
+ // A clamp of the scale is no move: it lands at once, about the pane's
330
+ // centre.
331
+ if (k < lowest) {
332
+ viewport.set(zoomAbout(base(), middle(), Math.min(EXTENT[1], lowest)));
333
+ }
334
+ },
335
+ screenToFlow(point) {
336
+ const rect = region.current?.getBoundingClientRect();
337
+ const { x, y, k } = held.get();
338
+ return {
339
+ x: (point.x - (rect?.left ?? 0) - x) / k,
340
+ y: (point.y - (rect?.top ?? 0) - y) / k,
341
+ };
342
+ },
343
+ edge(drag) {
344
+ // A drag ended or begun enters the band afresh, whether or not a frame
345
+ // ran between.
346
+ if (!drag || !dragged) band = undefined;
347
+ dragged = drag;
348
+ if (!frame) frame = requestAnimationFrame(step);
349
+ },
350
+ };
351
+
352
+ // A plain wheel pans through `translateBy`, which takes world units; the
353
+ // handler is not passive so the page never scrolls under the canvas.
354
+ const mount = () => {
355
+ const element = region.current;
356
+ if (!element) return;
357
+ layer = element.querySelector<HTMLElement>("[data-layer]");
358
+ pattern = element.querySelector<SVGPatternElement>("[data-grid]");
359
+ if (layer) layer.style.transformOrigin = "0 0";
360
+ const selection = select(element);
361
+ selection.call(behaviour).on("dblclick.zoom", null);
362
+ const wheel = (event: WheelEvent) => {
363
+ if (event.ctrlKey || event.metaKey) return;
364
+ event.preventDefault();
365
+ moved = true;
366
+ const unit = pixels(event, element.clientHeight);
367
+ const by = wheelPan(event);
368
+ behaviour.translateBy(
369
+ selection,
370
+ (-by.x * unit) / held.get().k,
371
+ (-by.y * unit) / held.get().k,
372
+ );
373
+ };
374
+ element.addEventListener("wheel", wheel, { passive: false });
375
+ // A pointer, a finger or a wheel taking the view over ends a travel where
376
+ // it got to, before d3-zoom reads the view it starts from. A part with its
377
+ // own drag (the zoom stack, a node being moved) leaves the view alone.
378
+ const take = (event: Event) => {
379
+ if (
380
+ event.type !== "wheel" &&
381
+ event.target instanceof Element &&
382
+ event.target.closest("[data-no-pan]")
383
+ )
384
+ return;
385
+ stop();
386
+ };
387
+ const TAKEN = ["pointerdown", "touchstart", "wheel"] as const;
388
+ for (const type of TAKEN)
389
+ element.addEventListener(type, take, { capture: true, passive: true });
390
+ apply(held.get());
391
+ return () => {
392
+ stop();
393
+ selection.on(".zoom", null);
394
+ element.removeEventListener("wheel", wheel);
395
+ for (const type of TAKEN)
396
+ element.removeEventListener(type, take, { capture: true });
397
+ };
398
+ };
399
+
400
+ return { viewport, mount };
401
+ }
402
+
403
+ // The viewport: d3-zoom on the region: docs/architecture/plugins/react-ui.md, "Canvas".
404
+ export function useViewport(region: RefObject<HTMLElement | null>): Viewport {
405
+ const [{ viewport, mount }] = useState(() => make(region));
406
+ useEffect(mount, [mount]);
407
+ return viewport;
408
+ }
409
+
410
+ // A value read off the transform, rendered again only when it changes: a pan
411
+ // or a zoom within one value of `select` renders nothing.
412
+ export function useViewportValue<T>(
413
+ viewport: Viewport,
414
+ select: (transform: Transform) => T,
415
+ ): T {
416
+ return useSyncExternalStore(viewport.subscribe, () => select(viewport.get()));
417
+ }
@@ -0,0 +1,113 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import type { LinkAct } from "@slotform/ui-core/descriptors";
3
+ import { useWords } from "@slotform/ui-core/react/words";
4
+ import { CANVAS_ZOOM } from "@slotform/ui-core/variants";
5
+ import { IconButtonBase } from "../../atoms/button/glyph.tsx";
6
+ import type { Act } from "../../lib/act.ts";
7
+ import { ActFace } from "../button/face.tsx";
8
+ import { EXTENT } from "./view.ts";
9
+ import { type About, useViewportValue, type Viewport } from "./viewport.ts";
10
+
11
+ const STACK = "absolute bottom-page left-page flex flex-col";
12
+ const FOOT =
13
+ "absolute bottom-page inset-x-0 flex justify-center pointer-events-none";
14
+ const ACT = "pointer-events-auto";
15
+
16
+ // The zoom stack: docs/architecture/plugins/react-ui.md, "Canvas".
17
+ function ZoomButtons({
18
+ atMax = false,
19
+ atMin = false,
20
+ arrange,
21
+ onZoomIn,
22
+ onZoomOut,
23
+ onFit,
24
+ onArrange,
25
+ }: {
26
+ atMax?: boolean;
27
+ atMin?: boolean;
28
+ arrange: boolean;
29
+ onZoomIn?: () => void;
30
+ onZoomOut?: () => void;
31
+ onFit?: () => void;
32
+ onArrange?: () => void;
33
+ }) {
34
+ const words = useWords();
35
+ return (
36
+ <div data-no-pan data-clear="left" className={cn(CANVAS_ZOOM, STACK)}>
37
+ <IconButtonBase
38
+ icon="ZoomIn"
39
+ label={words.zoomIn}
40
+ disabled={atMax}
41
+ onClick={onZoomIn}
42
+ />
43
+ <IconButtonBase
44
+ icon="ZoomOut"
45
+ label={words.zoomOut}
46
+ disabled={atMin}
47
+ onClick={onZoomOut}
48
+ />
49
+ <IconButtonBase icon="Maximize" label={words.fit} onClick={onFit} />
50
+ {arrange ? (
51
+ <IconButtonBase
52
+ icon="Network"
53
+ label={words.arrange}
54
+ onClick={onArrange}
55
+ />
56
+ ) : null}
57
+ </div>
58
+ );
59
+ }
60
+
61
+ // The stack of a loaded canvas, its ends of the scale read from the viewport.
62
+ export function ZoomStack({
63
+ viewport,
64
+ onFit,
65
+ minZoom,
66
+ onArrange,
67
+ anchor,
68
+ }: {
69
+ viewport: Viewport;
70
+ onFit: () => void;
71
+ minZoom: number;
72
+ onArrange?: () => void;
73
+ // What the buttons zoom about: the focused node, else nothing, which is the
74
+ // pane's centre.
75
+ anchor: () => About | undefined;
76
+ }) {
77
+ const atMax = useViewportValue(viewport, (view) => view.k >= EXTENT[1]);
78
+ const atMin = useViewportValue(viewport, (view) => view.k <= minZoom);
79
+ return (
80
+ <ZoomButtons
81
+ atMax={atMax}
82
+ atMin={atMin}
83
+ arrange={onArrange !== undefined}
84
+ onZoomIn={() => viewport.zoomIn(anchor())}
85
+ onZoomOut={() => viewport.zoomOut(anchor())}
86
+ onFit={onFit}
87
+ onArrange={onArrange}
88
+ />
89
+ );
90
+ }
91
+
92
+ // The stack a waiting canvas draws: the loaded stack's buttons, with `arrange`
93
+ // where the canvas can move nodes, so nothing changes when the graph arrives.
94
+ export function ZoomPainted({ arrange }: { arrange: boolean }) {
95
+ return <ZoomButtons arrange={arrange} />;
96
+ }
97
+
98
+ // The canvas's act, at the foot's centre. It adds, never removes, so its
99
+ // `destructive` and `quiet` flags are not read. A fit stays clear of it
100
+ // (`data-clear`).
101
+ export function ActFoot({ act }: { act: Act | LinkAct }) {
102
+ return (
103
+ <div data-no-pan className={FOOT}>
104
+ <div data-clear="bottom" className={cn(CANVAS_ZOOM, ACT)}>
105
+ <ActFace
106
+ act={act}
107
+ kind="quiet"
108
+ blocked={"blocked" in act ? act.blocked : undefined}
109
+ />
110
+ </div>
111
+ </div>
112
+ );
113
+ }
@@ -0,0 +1,20 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import { CARD_BODY, CARD_PART, ROWS } from "@slotform/ui-core/variants";
3
+ import type { ReactNode } from "react";
4
+ import type { Closed } from "../../lib/closed.ts";
5
+
6
+ const BOX = "min-w-0";
7
+
8
+ /** A card for a part of a Section's body that draws none of its own. */
9
+ export interface CardProps extends Closed {
10
+ /** The part (a Prose, a Slider, a field, a Stages, an Image), at the card's inset. */
11
+ children: ReactNode;
12
+ }
13
+
14
+ /** The surface inside a hairline at the card radius, its content at the card's inset: what a Section's part stands on when it is no sheet, Form, Table, Stats or BarChart, which are cards of their own and never stand in one. */
15
+ export function Card(props: CardProps) {
16
+ // A run of rows: docs/architecture/ui-core/layout.md, "Rows grid".
17
+ return (
18
+ <div className={cn(CARD_PART, CARD_BODY, ROWS, BOX)}>{props.children}</div>
19
+ );
20
+ }
@@ -0,0 +1,24 @@
1
+ import { useSelections } from "@slotform/ui-core/react/read";
2
+ import { type Datum, paints } from "@slotform/ui-core/read";
3
+ import type { ChipFamily } from "@slotform/ui-core/tokens";
4
+ import { ChipBase } from "../../atoms/chip/base.tsx";
5
+ import type { Closed } from "../../lib/closed.ts";
6
+
7
+ interface ChipProps extends Closed {
8
+ /** The value (a word; truncates past the short measure, 20 characters). A selection paints the pill, and the Chip is inert, until its read lands. */
9
+ label: Datum<string>;
10
+ /** The family of values it belongs to, one hue per family across screens. A selection paints the pill like a `label` does. */
11
+ family: Datum<ChipFamily>;
12
+ /** Adds the remove act, a round disc the chip's height closing its right end, its hit the target. */
13
+ onRemove?: () => void;
14
+ }
15
+
16
+ /** An outlined pill, a dot in the family's hue before the label in the body ink (the neutral family, which has no hue, the label alone on its soft ground, no edge); its one act removes the value. */
17
+ export function Chip({ label, family, onRemove }: ChipProps) {
18
+ const [word, hue] = useSelections([label, family]);
19
+ if (paints(word) || paints(hue))
20
+ return <ChipBase label="" family="neutral" painted />;
21
+ // An ended read draws no pill; the frame that holds it says why.
22
+ if (word.painted || hue.painted) return null;
23
+ return <ChipBase label={word.value} family={hue.value} onRemove={onRemove} />;
24
+ }
@@ -0,0 +1,2 @@
1
+ // The frame Code and Diff share: a card's column that clips what it holds.
2
+ export const CODE_FRAME = "flex flex-col shrink-0 min-w-0 overflow-hidden";