@zsherman/canvas 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 (196) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +373 -0
  3. package/README.md +15 -2
  4. package/THIRD_PARTY_NOTICES.md +117 -0
  5. package/dist/dom/canvas/assets.js +89 -0
  6. package/dist/dom/canvas/board-store.js +56 -0
  7. package/dist/dom/canvas/canvas-actions.js +78 -0
  8. package/dist/dom/canvas/canvas-board.js +557 -0
  9. package/dist/dom/canvas/canvas-board.styles.js +110 -0
  10. package/dist/dom/canvas/canvas-chrome.js +1167 -0
  11. package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
  12. package/dist/dom/canvas/canvas-comments.js +950 -0
  13. package/dist/dom/canvas/canvas-comments.styles.js +231 -0
  14. package/dist/dom/canvas/canvas-context.js +196 -0
  15. package/dist/dom/canvas/canvas-controller.js +697 -0
  16. package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
  17. package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
  18. package/dist/dom/canvas/canvas-menus.js +276 -0
  19. package/dist/dom/canvas/canvas-menus.styles.js +7 -0
  20. package/dist/dom/canvas/canvas-style-bar.js +930 -0
  21. package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
  22. package/dist/dom/canvas/canvas-surface.js +1353 -0
  23. package/dist/dom/canvas/canvas-surface.styles.js +345 -0
  24. package/dist/dom/canvas/canvas-tool-rail.js +406 -0
  25. package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
  26. package/dist/dom/canvas/comments.js +58 -0
  27. package/dist/dom/canvas/equality.js +13 -0
  28. package/dist/dom/canvas/export-png.js +496 -0
  29. package/dist/dom/canvas/gesture-machine.js +91 -0
  30. package/dist/dom/canvas/handlers.js +18 -0
  31. package/dist/dom/canvas/index.js +9 -0
  32. package/dist/dom/canvas/input.js +85 -0
  33. package/dist/dom/canvas/instant.js +28 -0
  34. package/dist/dom/canvas/kernel.js +39 -0
  35. package/dist/dom/canvas/motion.js +686 -0
  36. package/dist/dom/canvas/palette.js +165 -0
  37. package/dist/dom/canvas/random-id.js +15 -0
  38. package/dist/dom/canvas/scene-renderer.js +559 -0
  39. package/dist/dom/canvas/scene.styles.js +7 -0
  40. package/dist/dom/canvas/surface-model.js +324 -0
  41. package/dist/dom/canvas/uploads.js +294 -0
  42. package/dist/dom/index.js +9 -0
  43. package/dist/dom/kernel/actions.js +455 -0
  44. package/dist/dom/kernel/board.js +856 -0
  45. package/dist/dom/kernel/claims.js +40 -0
  46. package/dist/dom/kernel/clipboard.js +30 -0
  47. package/dist/dom/kernel/constants.js +210 -0
  48. package/dist/dom/kernel/contract.js +0 -0
  49. package/dist/dom/kernel/decode.js +85 -0
  50. package/dist/dom/kernel/editor.js +2077 -0
  51. package/dist/dom/kernel/errors.js +82 -0
  52. package/dist/dom/kernel/fonts/inter.js +7924 -0
  53. package/dist/dom/kernel/fractional-index.js +54 -0
  54. package/dist/dom/kernel/geometry.js +123 -0
  55. package/dist/dom/kernel/hit-test.js +156 -0
  56. package/dist/dom/kernel/index.js +22 -0
  57. package/dist/dom/kernel/inspect.js +116 -0
  58. package/dist/dom/kernel/kernel.js +251 -0
  59. package/dist/dom/kernel/outline.js +64 -0
  60. package/dist/dom/kernel/plan.js +1370 -0
  61. package/dist/dom/kernel/rev.js +173 -0
  62. package/dist/dom/kernel/scene.js +580 -0
  63. package/dist/dom/kernel/schema.js +1109 -0
  64. package/dist/dom/kernel/snap.js +258 -0
  65. package/dist/dom/kernel/svg.js +148 -0
  66. package/dist/dom/kernel/text.js +142 -0
  67. package/dist/solid/canvas/assets.jsx +89 -0
  68. package/dist/solid/canvas/board-store.jsx +56 -0
  69. package/dist/solid/canvas/canvas-actions.jsx +78 -0
  70. package/dist/solid/canvas/canvas-board.jsx +326 -0
  71. package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
  72. package/dist/solid/canvas/canvas-chrome.jsx +612 -0
  73. package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
  74. package/dist/solid/canvas/canvas-comments.jsx +435 -0
  75. package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
  76. package/dist/solid/canvas/canvas-context.jsx +174 -0
  77. package/dist/solid/canvas/canvas-controller.jsx +697 -0
  78. package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
  79. package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
  80. package/dist/solid/canvas/canvas-menus.jsx +223 -0
  81. package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
  82. package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
  83. package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
  84. package/dist/solid/canvas/canvas-surface.jsx +681 -0
  85. package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
  86. package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
  87. package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
  88. package/dist/solid/canvas/comments.jsx +58 -0
  89. package/dist/solid/canvas/equality.jsx +13 -0
  90. package/dist/solid/canvas/export-png.jsx +496 -0
  91. package/dist/solid/canvas/gesture-machine.jsx +91 -0
  92. package/dist/solid/canvas/handlers.jsx +18 -0
  93. package/dist/solid/canvas/index.jsx +9 -0
  94. package/dist/solid/canvas/input.jsx +85 -0
  95. package/dist/solid/canvas/instant.jsx +28 -0
  96. package/dist/solid/canvas/kernel.jsx +39 -0
  97. package/dist/solid/canvas/motion.jsx +686 -0
  98. package/dist/solid/canvas/palette.jsx +165 -0
  99. package/dist/solid/canvas/random-id.jsx +15 -0
  100. package/dist/solid/canvas/scene-renderer.jsx +559 -0
  101. package/dist/solid/canvas/scene.styles.jsx +7 -0
  102. package/dist/solid/canvas/surface-model.jsx +324 -0
  103. package/dist/solid/canvas/uploads.jsx +294 -0
  104. package/dist/solid/index.jsx +9 -0
  105. package/dist/solid/kernel/actions.jsx +455 -0
  106. package/dist/solid/kernel/board.jsx +856 -0
  107. package/dist/solid/kernel/claims.jsx +40 -0
  108. package/dist/solid/kernel/clipboard.jsx +30 -0
  109. package/dist/solid/kernel/constants.jsx +210 -0
  110. package/dist/solid/kernel/contract.jsx +0 -0
  111. package/dist/solid/kernel/decode.jsx +85 -0
  112. package/dist/solid/kernel/editor.jsx +2077 -0
  113. package/dist/solid/kernel/errors.jsx +82 -0
  114. package/dist/solid/kernel/fonts/inter.jsx +7924 -0
  115. package/dist/solid/kernel/fractional-index.jsx +54 -0
  116. package/dist/solid/kernel/geometry.jsx +123 -0
  117. package/dist/solid/kernel/hit-test.jsx +156 -0
  118. package/dist/solid/kernel/index.jsx +22 -0
  119. package/dist/solid/kernel/inspect.jsx +116 -0
  120. package/dist/solid/kernel/kernel.jsx +251 -0
  121. package/dist/solid/kernel/outline.jsx +64 -0
  122. package/dist/solid/kernel/plan.jsx +1370 -0
  123. package/dist/solid/kernel/rev.jsx +173 -0
  124. package/dist/solid/kernel/scene.jsx +580 -0
  125. package/dist/solid/kernel/schema.jsx +1109 -0
  126. package/dist/solid/kernel/snap.jsx +258 -0
  127. package/dist/solid/kernel/svg.jsx +148 -0
  128. package/dist/solid/kernel/text.jsx +142 -0
  129. package/dist/styles.css +1 -0
  130. package/dist/types/canvas/assets.d.ts +111 -0
  131. package/dist/types/canvas/board-store.d.ts +86 -0
  132. package/dist/types/canvas/canvas-actions.d.ts +21 -0
  133. package/dist/types/canvas/canvas-board.d.ts +53 -0
  134. package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
  135. package/dist/types/canvas/canvas-chrome.d.ts +109 -0
  136. package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
  137. package/dist/types/canvas/canvas-comments.d.ts +20 -0
  138. package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
  139. package/dist/types/canvas/canvas-context.d.ts +104 -0
  140. package/dist/types/canvas/canvas-controller.d.ts +159 -0
  141. package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
  142. package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
  143. package/dist/types/canvas/canvas-menus.d.ts +21 -0
  144. package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
  145. package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
  146. package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
  147. package/dist/types/canvas/canvas-surface.d.ts +15 -0
  148. package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
  149. package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
  150. package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
  151. package/dist/types/canvas/comments.d.ts +106 -0
  152. package/dist/types/canvas/equality.d.ts +4 -0
  153. package/dist/types/canvas/export-png.d.ts +77 -0
  154. package/dist/types/canvas/gesture-machine.d.ts +46 -0
  155. package/dist/types/canvas/handlers.d.ts +7 -0
  156. package/dist/types/canvas/index.d.ts +18 -0
  157. package/dist/types/canvas/input.d.ts +63 -0
  158. package/dist/types/canvas/instant.d.ts +13 -0
  159. package/dist/types/canvas/kernel.d.ts +27 -0
  160. package/dist/types/canvas/motion.d.ts +72 -0
  161. package/dist/types/canvas/palette.d.ts +46 -0
  162. package/dist/types/canvas/random-id.d.ts +6 -0
  163. package/dist/types/canvas/scene-renderer.d.ts +27 -0
  164. package/dist/types/canvas/scene.styles.d.ts +4 -0
  165. package/dist/types/canvas/surface-model.d.ts +113 -0
  166. package/dist/types/canvas/uploads.d.ts +58 -0
  167. package/dist/types/index.d.ts +1 -0
  168. package/dist/types/kernel/actions.d.ts +19 -0
  169. package/dist/types/kernel/board.d.ts +137 -0
  170. package/dist/types/kernel/claims.d.ts +29 -0
  171. package/dist/types/kernel/clipboard.d.ts +10 -0
  172. package/dist/types/kernel/constants.d.ts +195 -0
  173. package/dist/types/kernel/contract.d.ts +1533 -0
  174. package/dist/types/kernel/decode.d.ts +60 -0
  175. package/dist/types/kernel/editor.d.ts +12 -0
  176. package/dist/types/kernel/errors.d.ts +11 -0
  177. package/dist/types/kernel/fractional-index.d.ts +7 -0
  178. package/dist/types/kernel/geometry.d.ts +42 -0
  179. package/dist/types/kernel/hit-test.d.ts +16 -0
  180. package/dist/types/kernel/index.d.ts +8 -0
  181. package/dist/types/kernel/inspect.d.ts +6 -0
  182. package/dist/types/kernel/kernel.d.ts +6 -0
  183. package/dist/types/kernel/outline.d.ts +4 -0
  184. package/dist/types/kernel/plan.d.ts +20 -0
  185. package/dist/types/kernel/rev.d.ts +21 -0
  186. package/dist/types/kernel/scene.d.ts +16 -0
  187. package/dist/types/kernel/schema.d.ts +3436 -0
  188. package/dist/types/kernel/snap.d.ts +41 -0
  189. package/dist/types/kernel/svg.d.ts +3 -0
  190. package/dist/types/kernel/text.d.ts +21 -0
  191. package/docs/api.json +207 -0
  192. package/docs/canvas.md +444 -0
  193. package/docs/kernel.md +108 -0
  194. package/docs/llms.txt +41 -0
  195. package/docs/search-index.json +52 -0
  196. package/package.json +55 -4
@@ -0,0 +1,686 @@
1
+ // src/canvas/motion.ts
2
+ /**
3
+ * Smooth motion for what arrives in steps: other people's presence, at the
4
+ * rate their host sends it, and the viewer's own camera, which the kernel
5
+ * moves in jumps. Pure and clock-free: every call takes the time, so tests
6
+ * drive it with a fake clock.
7
+ */
8
+ /** The longest presence is held back to smooth it, in milliseconds. */
9
+ const MAX_DELAY = 300;
10
+ /** Extra delay over one interval, so a late sample is not waited on. */
11
+ const MIN_MARGIN = 12;
12
+ /** A gap after which the next sample starts a new motion, in milliseconds. */
13
+ const STALE = 1e3;
14
+ /** The interval assumed for a motion that starts after a pause. */
15
+ const START_INTERVAL = 50;
16
+ /** How fast the interval and jitter estimates follow new gaps. */
17
+ const SMOOTHING = .25;
18
+ /** Samples kept per connection; four are enough for a spline segment. */
19
+ const KEEP = 8;
20
+ /** How long a drop eases the rest of the way to where it was committed, at least. */
21
+ const SETTLE_MS = 80;
22
+ /** How long a remote change with no preview glides to its new place. */
23
+ const GLIDE_CHANGE_MS = 160;
24
+ /** How long a dragged shape waits at its last preview for the drop to arrive. */
25
+ const DROP_GRACE = 1e3;
26
+ /** A zoom step's glide, in milliseconds. */
27
+ const GLIDE_MS = 220;
28
+ /** How quickly the camera catches up while following: 1/e of the gap per this many ms. */
29
+ const FOLLOW_TAU = 90;
30
+ /** The camera stops easing within this many screen pixels of its target. */
31
+ const LOCK_PX = 2;
32
+ const centreOf = (entry) => entry.cursor?.point ?? {
33
+ x: entry.viewport.x + entry.viewport.w / 2,
34
+ y: entry.viewport.y + entry.viewport.h / 2
35
+ };
36
+ /**
37
+ * Catmull-Rom between `p1` and `p2`, kept inside them: a spline through the
38
+ * samples that never swings past the next one.
39
+ */
40
+ function spline(p0, p1, p2, p3, u) {
41
+ const m1 = (p2 - p0) / 2;
42
+ const m2 = (p3 - p1) / 2;
43
+ const u2 = u * u;
44
+ const u3 = u2 * u;
45
+ const value = (2 * u3 - 3 * u2 + 1) * p1 + (u3 - 2 * u2 + u) * m1 + (-2 * u3 + 3 * u2) * p2 + (u3 - u2) * m2;
46
+ return Math.min(Math.max(value, Math.min(p1, p2)), Math.max(p1, p2));
47
+ }
48
+ /** `curve`'s value between samples `a` and `b`; `a` whole when they don't blend. */
49
+ function between(curve, p0, a, b, p3, u) {
50
+ if (!curve.fits(a, b)) return a;
51
+ return curve.mix(curve.fits(p0, a) ? p0 : a, a, b, curve.fits(b, p3) ? p3 : b, u);
52
+ }
53
+ const POINT = {
54
+ fits: () => true,
55
+ mix: (p0, p1, p2, p3, u) => ({
56
+ x: spline(p0.x, p1.x, p2.x, p3.x, u),
57
+ y: spline(p0.y, p1.y, p2.y, p3.y, u)
58
+ })
59
+ };
60
+ const BOX = {
61
+ fits: () => true,
62
+ mix: (p0, p1, p2, p3, u) => ({
63
+ h: spline(p0.h, p1.h, p2.h, p3.h, u),
64
+ w: spline(p0.w, p1.w, p2.w, p3.w, u),
65
+ x: spline(p0.x, p1.x, p2.x, p3.x, u),
66
+ y: spline(p0.y, p1.y, p2.y, p3.y, u)
67
+ })
68
+ };
69
+ /** A cursor glides between points; a timed glide's own time never blends. */
70
+ const CURSOR = {
71
+ fits: (a, b) => a !== null && b !== null,
72
+ mix: (p0, p1, p2, p3, u) => p1 === null || p2 === null ? p1 : {
73
+ arriveInMs: p1.arriveInMs,
74
+ point: POINT.mix(p0?.point ?? p1.point, p1.point, p2.point, p3?.point ?? p2.point, u)
75
+ }
76
+ };
77
+ /** A stroke's points blend one to one while their count holds. */
78
+ const POINTS = {
79
+ fits: (a, b) => a.length === b.length,
80
+ mix: (p0, p1, p2, p3, u) => p1.map((entry, i) => POINT.mix(p0[i] ?? entry, entry, p2[i] ?? entry, p3[i] ?? entry, u))
81
+ };
82
+ /** Two ends of a connector line up when they are the same kind of end on the same shape. */
83
+ function sameAnchor(a, b) {
84
+ if (a._tag === "Point" || b._tag === "Point") return a._tag === b._tag;
85
+ return a.id === b.id && a.at === null === (b.at === null);
86
+ }
87
+ const ANCHOR = {
88
+ fits: sameAnchor,
89
+ mix: (p0, p1, p2, p3, u) => {
90
+ if (p1._tag === "Point") {
91
+ const point = (anchor) => anchor._tag === "Point" ? anchor : p1;
92
+ return {
93
+ _tag: "Point",
94
+ ...POINT.mix(point(p0), p1, point(p2), point(p3), u)
95
+ };
96
+ }
97
+ const at = (anchor) => anchor._tag === "Shape" ? anchor.at : null;
98
+ const a = p1.at;
99
+ const b = at(p2);
100
+ if (a === null || b === null) return p1;
101
+ return {
102
+ ...p1,
103
+ at: POINT.mix(at(p0) ?? a, a, b, at(p3) ?? b, u)
104
+ };
105
+ }
106
+ };
107
+ /**
108
+ * What someone is doing blends only within one gesture: the same kind,
109
+ * on the same things, with the same style; then only its positions move.
110
+ * Drags and resizes are left to the hands, which place what they move.
111
+ * A stroke's points blend only while their count holds, so a growing
112
+ * stroke switches whole at each sample.
113
+ */
114
+ const ACTIVITY = {
115
+ fits: (a, b) => {
116
+ switch (a._tag) {
117
+ case "Selecting": return b._tag === "Selecting";
118
+ case "Creating": return b._tag === "Creating" && a.draft === b.draft && a.id === b.id;
119
+ case "Connecting": return b._tag === "Connecting" && a.arrow === b.arrow && sameAnchor(a.from, b.from) && sameAnchor(a.to, b.to);
120
+ case "Inking": return b._tag === "Inking" && a.id === b.id && a.brush === b.brush && a.color === b.color && a.size === b.size && POINTS.fits(a.points, b.points);
121
+ default: return false;
122
+ }
123
+ },
124
+ mix: (p0, p1, p2, p3, u) => {
125
+ const boxOf = (activity, fallback) => "box" in activity ? activity.box : fallback;
126
+ switch (p1._tag) {
127
+ case "Selecting":
128
+ case "Creating": return {
129
+ ...p1,
130
+ box: BOX.mix(boxOf(p0, p1.box), p1.box, boxOf(p2, p1.box), boxOf(p3, p1.box), u)
131
+ };
132
+ case "Connecting": {
133
+ const ends = (activity) => activity._tag === "Connecting" ? activity : p1;
134
+ return {
135
+ ...p1,
136
+ from: ANCHOR.mix(ends(p0).from, p1.from, ends(p2).from, ends(p3).from, u),
137
+ to: ANCHOR.mix(ends(p0).to, p1.to, ends(p2).to, ends(p3).to, u)
138
+ };
139
+ }
140
+ case "Inking": {
141
+ const of = (activity) => activity._tag === "Inking" ? activity.points : p1.points;
142
+ return {
143
+ ...p1,
144
+ points: POINTS.mix(of(p0), p1.points, of(p2), of(p3), u)
145
+ };
146
+ }
147
+ default: return p1;
148
+ }
149
+ }
150
+ };
151
+ const easeOutCubic = (t) => easeOut(t, 3);
152
+ const NOWHERE = {
153
+ h: 0,
154
+ w: 0,
155
+ x: 0,
156
+ y: 0
157
+ };
158
+ const corner = (place) => ({
159
+ ...place,
160
+ h: 0,
161
+ w: 0
162
+ });
163
+ const samePlace = (a, b) => a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
164
+ function movesOf(activity) {
165
+ if (activity._tag === "Gesture") return activity.shapes.map((target) => ({
166
+ ids: [target.id],
167
+ key: `move\n${target.id}`,
168
+ place: {
169
+ h: 0,
170
+ w: 0,
171
+ x: target.x,
172
+ y: target.y
173
+ },
174
+ resize: false,
175
+ transaction: activity.transaction
176
+ }));
177
+ if (activity._tag === "Resizing") return [{
178
+ ids: activity.ids,
179
+ key: `resize\n${activity.ids.join("\n")}`,
180
+ place: activity.box,
181
+ resize: true,
182
+ transaction: activity.transaction
183
+ }];
184
+ return [];
185
+ }
186
+ /** Where `stops` put a hand at `time`: a spline through them, never past the next. */
187
+ function placeAt(stops, time) {
188
+ const first = stops[0];
189
+ const last = stops.at(-1);
190
+ if (first === void 0 || last === void 0) return NOWHERE;
191
+ if (time <= first.at) return first.place;
192
+ if (time >= last.at) return last.place;
193
+ const i = stops.findLastIndex((stop) => stop.at <= time);
194
+ const a = stops[i] ?? first;
195
+ const b = stops[i + 1] ?? last;
196
+ const p0 = (stops[i - 1] ?? a).place;
197
+ const p3 = (stops[i + 2] ?? b).place;
198
+ return BOX.mix(p0, a.place, b.place, p3, (time - a.at) / (b.at - a.at));
199
+ }
200
+ /**
201
+ * The shapes others are dragging or resizing, and the changes that move
202
+ * shapes with no preview, eased. `lag` says how late a connection is shown.
203
+ */
204
+ function createHands(lag, reduced) {
205
+ const hands = /* @__PURE__ */ new Map();
206
+ /** Each connection's previews in its last presence, by hand key. */
207
+ const live = /* @__PURE__ */ new Map();
208
+ /** When an actor's change moved a shape before any preview of it, by actor and shape. */
209
+ const ahead = /* @__PURE__ */ new Map();
210
+ let places = /* @__PURE__ */ new Map();
211
+ let head = 0;
212
+ function targetOf(hand) {
213
+ const boxes = hand.ids.flatMap((id) => places.get(id) ?? []);
214
+ if (boxes.length < hand.ids.length) return null;
215
+ const x = Math.min(...boxes.map((box) => box.x));
216
+ const y = Math.min(...boxes.map((box) => box.y));
217
+ const box = {
218
+ h: Math.max(...boxes.map((each) => each.y + each.h)) - y,
219
+ w: Math.max(...boxes.map((each) => each.x + each.w)) - x,
220
+ x,
221
+ y
222
+ };
223
+ return hand.resize ? box : corner(box);
224
+ }
225
+ /**
226
+ * Sets the drop as the hand's last stop, eased to from where it is. Under
227
+ * reduced motion every hand shows its last stop, so it snaps.
228
+ */
229
+ function close(hand, now) {
230
+ const target = targetOf(hand);
231
+ if (target === null) {
232
+ hands.delete(hand.key);
233
+ return;
234
+ }
235
+ hand.stops.push(...hand.stops.slice(-1));
236
+ const at = Math.max(now, (hand.stops.at(-1)?.at ?? now) + 80);
237
+ hand.stops.push({
238
+ at,
239
+ place: target
240
+ });
241
+ hand.closed = true;
242
+ hand.ended = null;
243
+ }
244
+ function add(hand) {
245
+ hands.set(hand.key, {
246
+ ...hand,
247
+ played: Number.NEGATIVE_INFINITY,
248
+ shown: hand.stops[0]?.place ?? NOWHERE
249
+ });
250
+ }
251
+ function start(owner, actor, move, now) {
252
+ for (const id of move.ids) {
253
+ const early = ahead.get(`${actor}\n${id}`);
254
+ if (early !== void 0 && now - early < 1e3) {
255
+ ahead.delete(`${actor}\n${id}`);
256
+ return;
257
+ }
258
+ }
259
+ const before = hands.get(move.key);
260
+ for (const [key, hand] of hands) if (hand.ids.some((id) => move.ids.includes(id))) hands.delete(key);
261
+ const over = before?.owner === owner && !before.closed;
262
+ add({
263
+ actor,
264
+ closed: false,
265
+ ended: null,
266
+ ids: move.ids,
267
+ key: move.key,
268
+ owes: over ? before.owes + 1 : 0,
269
+ owner,
270
+ resize: move.resize,
271
+ transaction: move.transaction,
272
+ stops: [{
273
+ at: now - Math.max(1, lag(owner)),
274
+ place: before?.shown ?? targetOf(move) ?? move.place
275
+ }, {
276
+ at: now,
277
+ place: move.place
278
+ }]
279
+ });
280
+ }
281
+ return {
282
+ /** True while any hand is still easing, so another frame is needed. */
283
+ active: () => hands.size > 0,
284
+ /** A board arrived; when `remote`, drops in it close the drags they end. */
285
+ board(board, now, remote) {
286
+ const before = places;
287
+ places = new Map(board.shapes.map((row) => [row.id, row]));
288
+ const authors = /* @__PURE__ */ new Map();
289
+ for (const record of board.transactions) {
290
+ if (record.seq <= head) continue;
291
+ for (const id of record.shapes) authors.set(id, [...authors.get(id) ?? [], record.author.principal]);
292
+ }
293
+ head = board.head;
294
+ for (const [key, hand] of hands) if (hand.ids.some((id) => !places.has(id))) hands.delete(key);
295
+ if (!remote) return;
296
+ const closed = /* @__PURE__ */ new Set();
297
+ for (const [id, place] of places) {
298
+ const was = before.get(id);
299
+ if (was === void 0 || samePlace(was, place)) continue;
300
+ const by = authors.get(id) ?? [];
301
+ const author = by.at(-1) ?? null;
302
+ const on = [...hands.values()].filter((hand) => hand.ids.includes(id));
303
+ if (on.length === 0) {
304
+ if (author !== null) ahead.set(`${author}\n${id}`, now);
305
+ if (was.w !== place.w || was.h !== place.h) continue;
306
+ add({
307
+ actor: author,
308
+ closed: true,
309
+ ended: null,
310
+ ids: [id],
311
+ key: `move\n${id}`,
312
+ owes: -1,
313
+ owner: null,
314
+ resize: false,
315
+ transaction: GLIDE,
316
+ stops: [{
317
+ at: now,
318
+ place: corner(was)
319
+ }, {
320
+ at: now + GLIDE_CHANGE_MS,
321
+ place: corner(place)
322
+ }]
323
+ });
324
+ continue;
325
+ }
326
+ for (const hand of on) {
327
+ if (closed.has(hand)) continue;
328
+ closed.add(hand);
329
+ const drops = by.filter((each) => each === hand.actor).length;
330
+ hand.owes -= drops;
331
+ if (drops > 0 && hand.owes >= 0) continue;
332
+ if (hand.owes < -1) ahead.set(`${hand.actor}\n${id}`, now);
333
+ close(hand, now);
334
+ }
335
+ }
336
+ },
337
+ /** A connection's presence arrived, or it left (`activity` null). */
338
+ preview(owner, actor, activity, now) {
339
+ const moves = new Map((activity === null ? [] : movesOf(activity)).map((move) => [move.key, move]));
340
+ const was = live.get(owner) ?? /* @__PURE__ */ new Map();
341
+ for (const key of was.keys()) {
342
+ if (moves.has(key)) continue;
343
+ const hand = hands.get(key);
344
+ if (hand?.owner === owner && !hand.closed) hand.ended = now;
345
+ }
346
+ for (const [key, move] of moves) {
347
+ const previous = was.get(key);
348
+ const hand = hands.get(key);
349
+ if (previous === void 0 || previous.transaction !== move.transaction) start(owner, actor, move, now);
350
+ else if (!samePlace(previous.place, move.place) && hand?.owner === owner && !hand.closed) hand.stops.push({
351
+ at: now,
352
+ place: move.place
353
+ });
354
+ }
355
+ if (moves.size === 0) live.delete(owner);
356
+ else live.set(owner, moves);
357
+ },
358
+ /** Moves every hand to `now`; drops the ones that arrived. */
359
+ step(now) {
360
+ for (const [key, hand] of hands) {
361
+ if (!hand.closed && hand.ended !== null && now - hand.ended >= 1e3) {
362
+ close(hand, now);
363
+ if (!hands.has(key)) continue;
364
+ }
365
+ hand.played = reduced() ? Number.POSITIVE_INFINITY : Math.max(hand.played, now - (hand.owner === null ? 0 : lag(hand.owner)));
366
+ const last = hand.stops.at(-1);
367
+ if (hand.closed && (last === void 0 || hand.played >= last.at)) {
368
+ hands.delete(key);
369
+ continue;
370
+ }
371
+ hand.shown = placeAt(hand.stops, hand.played);
372
+ while (hand.stops.length > 4 && (hand.stops[2]?.at ?? 0) <= hand.played) hand.stops.shift();
373
+ }
374
+ },
375
+ /**
376
+ * `entries` with every shape a hand holds placed by its hand, in its
377
+ * connection's entry (or its actor's, for a change with no preview),
378
+ * since that entry holds the claim; previews no hand owns are dropped.
379
+ */
380
+ place(entries) {
381
+ const held = /* @__PURE__ */ new Map();
382
+ const stray = [];
383
+ for (const hand of hands.values()) {
384
+ const entry = entries.find((each) => trackKey(each) === hand.owner) ?? entries.find((each) => each.actor === hand.actor);
385
+ if (entry === void 0) stray.push(hand);
386
+ else held.set(entry, [...held.get(entry) ?? [], hand]);
387
+ }
388
+ const loose = [...groupsOf(stray)];
389
+ const placed = entries.map((entry) => {
390
+ const [first, ...rest] = groupsOf(held.get(entry) ?? []);
391
+ loose.push(...rest);
392
+ if (first !== void 0) return {
393
+ ...entry,
394
+ activity: activityOf(first)
395
+ };
396
+ return movesOf(entry.activity).length > 0 ? {
397
+ ...entry,
398
+ activity: { _tag: "Idle" }
399
+ } : entry;
400
+ });
401
+ const extra = loose.map((group, index) => ({
402
+ activity: activityOf(group),
403
+ actor: group[0]?.actor ?? GLIDE,
404
+ claims: [],
405
+ chat: null,
406
+ connection: `${GLIDE}-${index}`,
407
+ cursor: null,
408
+ following: null,
409
+ kind: "person",
410
+ name: "",
411
+ selection: [],
412
+ uploads: [],
413
+ viewport: NOWHERE
414
+ }));
415
+ return [...placed, ...extra];
416
+ }
417
+ };
418
+ }
419
+ /** Names the entries `place` makes for hands no one's presence holds. */
420
+ const GLIDE = "glide";
421
+ /** The hands one activity can show: all the moves, or one resize. */
422
+ function groupsOf(hands) {
423
+ const moves = hands.filter((hand) => !hand.resize);
424
+ return [...moves.length > 0 ? [moves] : [], ...hands.filter((hand) => hand.resize).map((hand) => [hand])];
425
+ }
426
+ function activityOf(group) {
427
+ const [first] = group;
428
+ const transaction = first?.transaction ?? GLIDE;
429
+ return first?.resize ? {
430
+ _tag: "Resizing",
431
+ box: first.shown,
432
+ ids: first.ids,
433
+ transaction
434
+ } : {
435
+ _tag: "Gesture",
436
+ ["shapes"]: group.flatMap((hand) => hand.ids.map((id) => ({
437
+ id,
438
+ x: hand.shown.x,
439
+ y: hand.shown.y
440
+ }))),
441
+ transaction
442
+ };
443
+ }
444
+ /** One track per open editor: an actor can have several tabs open. */
445
+ function trackKey(entry) {
446
+ return `${entry.actor}\n${entry.connection}`;
447
+ }
448
+ /**
449
+ * Other people's presence, smoothed. Each connection's samples are kept with the
450
+ * time they arrived and shown a little late, about one sending interval plus
451
+ * a margin for jitter, so the view always has the next sample to move
452
+ * toward. Positions follow a spline through the samples; everything else
453
+ * (selection, claims, the kind of activity, a stroke's points) switches
454
+ * whole, at the sample it came with, so a frame never mixes two samples.
455
+ */
456
+ function createPresenceMotion(options = {}) {
457
+ const tracks = /* @__PURE__ */ new Map();
458
+ const hands = createHands((owner) => {
459
+ const track = tracks.get(owner);
460
+ return track === void 0 ? 0 : delayOf(track);
461
+ }, options.reduced ?? (() => false));
462
+ let order = [];
463
+ let latest = [];
464
+ let shown = {
465
+ moving: false,
466
+ presence: []
467
+ };
468
+ let shownAt = Number.NEGATIVE_INFINITY;
469
+ let dirty = true;
470
+ /** How late a connection is shown, in milliseconds. */
471
+ function delayOf(track) {
472
+ if (track.interval === null) return 0;
473
+ return Math.min(300, track.interval + Math.max(MIN_MARGIN, 2 * track.jitter));
474
+ }
475
+ function entryAt(track, now) {
476
+ const { samples } = track;
477
+ const last = samples.at(-1);
478
+ if (last === void 0) throw new Error("A track has no samples.");
479
+ const time = now - delayOf(track);
480
+ let entry = last.entry;
481
+ if (samples.length > 1 && time < last.at) {
482
+ let i = samples.findLastIndex((sample) => sample.at <= time);
483
+ if (i < 0) i = 0;
484
+ const a = samples[i];
485
+ const b = samples[i + 1];
486
+ if (a !== void 0 && b !== void 0) {
487
+ const u = Math.min(Math.max((time - a.at) / (b.at - a.at), 0), 1);
488
+ const p0 = (samples[i - 1] ?? a).entry;
489
+ const p3 = (samples[i + 2] ?? b).entry;
490
+ const at = (key, curve) => between(curve, p0[key], a.entry[key], b.entry[key], p3[key], u);
491
+ entry = {
492
+ ...a.entry,
493
+ activity: at("activity", ACTIVITY),
494
+ cursor: at("cursor", CURSOR),
495
+ viewport: at("viewport", BOX)
496
+ };
497
+ } else if (a !== void 0) entry = a.entry;
498
+ }
499
+ const glide = last.entry.cursor;
500
+ if (glide !== null && glide.arriveInMs > 0 && track.glideFrom !== null) {
501
+ const t = Math.min(Math.max((now - last.at) / glide.arriveInMs, 0), 1);
502
+ const k = easeOutCubic(t);
503
+ const from = track.glideFrom;
504
+ entry = {
505
+ ...entry,
506
+ cursor: {
507
+ ...glide,
508
+ point: {
509
+ x: from.x + (glide.point.x - from.x) * k,
510
+ y: from.y + (glide.point.y - from.y) * k
511
+ }
512
+ }
513
+ };
514
+ }
515
+ return entry;
516
+ }
517
+ function movingAt(track, now) {
518
+ const last = track.samples.at(-1);
519
+ if (last === void 0) return false;
520
+ if (track.samples.length > 1 && now - delayOf(track) < last.at) return true;
521
+ const glide = last.entry.cursor;
522
+ return glide !== null && glide.arriveInMs > 0 && track.glideFrom !== null && now - last.at < glide.arriveInMs;
523
+ }
524
+ return {
525
+ /** Records presence as it arrives at `now`. */
526
+ push(presence, now) {
527
+ if (presence === latest) return;
528
+ latest = presence;
529
+ dirty = true;
530
+ const byKey = /* @__PURE__ */ new Map();
531
+ for (const entry of presence) byKey.set(trackKey(entry), entry);
532
+ order = [...byKey.keys()];
533
+ for (const key of tracks.keys()) if (!byKey.has(key)) {
534
+ tracks.delete(key);
535
+ hands.preview(key, "", null, now);
536
+ }
537
+ for (const [key, entry] of byKey) {
538
+ const track = tracks.get(key);
539
+ if (track === void 0) {
540
+ tracks.set(key, {
541
+ glideFrom: null,
542
+ interval: null,
543
+ jitter: 0,
544
+ samples: [{
545
+ at: now,
546
+ entry
547
+ }]
548
+ });
549
+ continue;
550
+ }
551
+ const last = track.samples.at(-1);
552
+ if (last === void 0 || last.entry === entry) continue;
553
+ if (JSON.stringify(last.entry) === JSON.stringify(entry)) continue;
554
+ const from = entryAt(track, now).cursor?.point ?? null;
555
+ const gap = now - last.at;
556
+ const reach = Math.max(last.entry.viewport.w, last.entry.viewport.h, 400);
557
+ const a = centreOf(last.entry);
558
+ const b = centreOf(entry);
559
+ if (Math.hypot(b.x - a.x, b.y - a.y) > reach) track.samples = [{
560
+ at: now,
561
+ entry
562
+ }];
563
+ else if (gap > 1e3) track.samples = [{
564
+ at: now - (track.interval ?? START_INTERVAL),
565
+ entry: last.entry
566
+ }, {
567
+ at: now,
568
+ entry
569
+ }];
570
+ else {
571
+ if (track.interval === null) track.interval = gap;
572
+ else {
573
+ track.jitter += (Math.abs(gap - track.interval) - track.jitter) * SMOOTHING;
574
+ track.interval += (gap - track.interval) * SMOOTHING;
575
+ }
576
+ track.samples.push({
577
+ at: now,
578
+ entry
579
+ });
580
+ if (track.samples.length > KEEP) track.samples.shift();
581
+ }
582
+ track.glideFrom = entry.cursor !== null && entry.cursor.arriveInMs > 0 ? from : null;
583
+ }
584
+ for (const [key, entry] of byKey) hands.preview(key, entry.actor, entry.activity, now);
585
+ },
586
+ /**
587
+ * Records the board as it arrives at `now`: `remote` when it came from
588
+ * the store, so a drop it holds ends the drag that previewed it.
589
+ */
590
+ board(board, now, remote) {
591
+ hands.board(board, now, remote);
592
+ dirty = true;
593
+ },
594
+ /** Presence to draw at `now`. The same object until something moves. */
595
+ sample(now) {
596
+ if (now === shownAt && !dirty) return shown;
597
+ dirty = false;
598
+ shownAt = now;
599
+ hands.step(now);
600
+ const tracking = order.some((key) => {
601
+ const track = tracks.get(key);
602
+ return track !== void 0 && movingAt(track, now);
603
+ });
604
+ const base = order.flatMap((key) => {
605
+ const track = tracks.get(key);
606
+ if (track === void 0) return [];
607
+ return tracking ? [entryAt(track, now)] : track.samples.slice(-1).map((sample) => sample.entry);
608
+ });
609
+ const presence = hands.place(base);
610
+ const moving = tracking || hands.active();
611
+ shown = {
612
+ moving,
613
+ presence: !moving && sameEntries(shown.presence, presence) ? shown.presence : presence
614
+ };
615
+ return shown;
616
+ }
617
+ };
618
+ }
619
+ function sameEntries(a, b) {
620
+ return a.length === b.length && a.every((entry, i) => entry === b[i]);
621
+ }
622
+ const lerp = (a, b, t) => a + (b - a) * t;
623
+ /** Fast, then slowing into the end; a higher `power` slows sooner. */
624
+ function easeOut(t, power) {
625
+ return 1 - (1 - t) ** power;
626
+ }
627
+ /** `ease-out-quint`, the docs site's motion token. */
628
+ const easeOutQuint = (t) => easeOut(t, 5);
629
+ /** `target`'s screen, centred on `cx, cy` at `zoom`. */
630
+ function framed(target, cx, cy, zoom) {
631
+ const w = target.w * target.zoom / zoom;
632
+ const h = target.h * target.zoom / zoom;
633
+ return {
634
+ ...target,
635
+ h,
636
+ w,
637
+ x: cx - w / 2,
638
+ y: cy - h / 2,
639
+ zoom
640
+ };
641
+ }
642
+ /** True when `a` is within the lock distance of `b` everywhere on screen. */
643
+ function close(a, b) {
644
+ const ax = a.x + a.w / 2;
645
+ const ay = a.y + a.h / 2;
646
+ const bx = b.x + b.w / 2;
647
+ const by = b.y + b.h / 2;
648
+ return Math.hypot(ax - bx, ay - by) * b.zoom + Math.max(b.w, b.h) * b.zoom / 2 * Math.abs(1 - a.zoom / b.zoom) < LOCK_PX;
649
+ }
650
+ /**
651
+ * One frame of the camera: a zoom step glides with `ease-out-quint`; while
652
+ * following, the camera eases toward the leader's view each frame and
653
+ * locks on within two pixels. Returns the motion still running, or null.
654
+ */
655
+ function stepCamera(shown, target, motion, now) {
656
+ if (motion._tag === "Glide") {
657
+ const t = Math.min(Math.max((now - motion.began) / 220, 0), 1);
658
+ if (t >= 1) return {
659
+ motion: null,
660
+ view: target
661
+ };
662
+ const k = easeOutQuint(t);
663
+ const { from } = motion;
664
+ const zoom = Math.exp(lerp(Math.log(from.zoom), Math.log(target.zoom), k));
665
+ return {
666
+ motion,
667
+ view: framed(target, lerp(from.x + from.w / 2, target.x + target.w / 2, k), lerp(from.y + from.h / 2, target.y + target.h / 2, k), zoom)
668
+ };
669
+ }
670
+ const dt = Math.min(Math.max(now - motion.last, 0), 64);
671
+ const k = 1 - Math.exp(-dt / FOLLOW_TAU);
672
+ const zoom = Math.exp(lerp(Math.log(shown.zoom), Math.log(target.zoom), k));
673
+ const view = framed(target, lerp(shown.x + shown.w / 2, target.x + target.w / 2, k), lerp(shown.y + shown.h / 2, target.y + target.h / 2, k), zoom);
674
+ if (close(view, target)) return {
675
+ motion: null,
676
+ view: target
677
+ };
678
+ return {
679
+ motion: {
680
+ _tag: "Follow",
681
+ last: now
682
+ },
683
+ view
684
+ };
685
+ }
686
+ export { DROP_GRACE, GLIDE_MS, MAX_DELAY, SETTLE_MS, STALE, createPresenceMotion, stepCamera };