@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,580 @@
1
+ import { INTERACTION, RADIUS, STROKE_WIDTH } from "./constants.jsx";
2
+ import { applyProposal, bounds, boundsOf, fitText, hasText, history, inkPoints, inkStroke, layoutText, outlinePath, pendingProposals, polylinePath, route, routeEnd, routeStart, strokePath } from "./board.jsx";
3
+ import { boxFromPoints, inflate, intersects, round, roundBox } from "./geometry.jsx";
4
+ import { RESIZE_HANDLES, handlePoint, resizable } from "./hit-test.jsx";
5
+ import { copyShape, scale, translate } from "./plan.jsx";
6
+ import { claimants, claimsOf, connections, sameConnection } from "./claims.jsx";
7
+ import { finish, revOf, shapeHash } from "./rev.jsx";
8
+ // src/kernel/scene.ts
9
+ /**
10
+ * The scene: what to draw, back to front, in board coordinates.
11
+ *
12
+ * 1. Shapes that reach into the viewport plus a margin, as the viewer's and
13
+ * others' gestures move them right now.
14
+ * 2. Pending proposals as ghosts.
15
+ * 3. Other connections' selections and activity previews; for the viewer's
16
+ * own other connections (another tab), previews only.
17
+ * 4. With the viewer's editor state: hover, own selection, bounds, handles,
18
+ * marquee, drafts, ink, eraser trail, snap guides and the text editor.
19
+ * 5. Others' cursors, one per connection.
20
+ *
21
+ * A gesture moves a shape only if its connection holds the shape or nobody
22
+ * claims it (see `claims.ts`). Every item carries a `rev` (see `rev.ts`).
23
+ */
24
+ /** Adds an item drawn from its own fields. */
25
+ const add = (out, item) => {
26
+ out.push({
27
+ ...item,
28
+ rev: revOf(item)
29
+ });
30
+ };
31
+ /** The shapes an activity moves. */
32
+ const movedIds = (activity) => activity._tag === "Gesture" ? activity.shapes.map((target) => target.id) : activity._tag === "Resizing" ? activity.ids : activity._tag === "Connecting" && activity.arrow !== null ? [activity.arrow] : [];
33
+ /** Shapes as the given activity shows them in motion. */
34
+ const moved = (fonts, shapes, base, activity) => {
35
+ if (activity._tag === "Gesture") for (const { id, x, y } of activity.shapes) {
36
+ const shape = base.get(id);
37
+ if (shape !== void 0) shapes.set(id, translate(shape, x - shape.x, y - shape.y));
38
+ }
39
+ else if (activity._tag === "Resizing") {
40
+ const from = boundsOf(base, activity.ids);
41
+ if (from === null) return;
42
+ for (const id of activity.ids) {
43
+ const shape = base.get(id);
44
+ if (shape !== void 0) shapes.set(id, scale(fonts, shape, from, activity.box));
45
+ }
46
+ } else if (activity._tag === "Connecting" && activity.arrow !== null) {
47
+ const arrow = base.get(activity.arrow);
48
+ if (arrow?.kind === "Arrow") shapes.set(arrow.id, {
49
+ ...arrow,
50
+ from: activity.from,
51
+ to: activity.to
52
+ });
53
+ }
54
+ };
55
+ const draftKind = (draft) => draft === "sticky" ? "Sticky" : draft === "text" ? "Text" : "Box";
56
+ /** Placeholders for a connection's uploads; `prefix` is `actor/<actor>/<connection>`. */
57
+ const uploadItems = (prefix, actor, kind, uploads, mine, out) => {
58
+ for (const upload of new Map(uploads.map((u) => [u.id, u])).values()) add(out, {
59
+ _tag: "Upload",
60
+ key: `${prefix}/upload/${upload.id}`,
61
+ actor,
62
+ kind,
63
+ mine,
64
+ id: upload.id,
65
+ box: upload.box,
66
+ name: upload.name
67
+ });
68
+ };
69
+ /** The id of the next transaction the editor sends. */
70
+ const nextTransaction = (state) => `${state.session}-${state.seq}`;
71
+ /** The id the transaction `transaction` gives the `n`th shape it creates. */
72
+ const createdId = (transaction, kind, n = 1) => `${kind.toLowerCase()}-${transaction}-${n}`;
73
+ /** The id the editor gives the `n`th shape its next transaction creates. */
74
+ const newId = (state, kind, n = 1) => createdId(nextTransaction(state), kind, n);
75
+ /**
76
+ * The activity the viewer's editor mode shows to others, which the scene
77
+ * also previews locally: targets, not offsets (see `ActivityJson`).
78
+ */
79
+ const activityOf = (state, shapes) => {
80
+ const mode = state.mode;
81
+ switch (mode._tag) {
82
+ case "Dragging": {
83
+ if (mode.copy) return { _tag: "Idle" };
84
+ const targets = [];
85
+ for (const id of mode.ids) {
86
+ const shape = shapes.get(id);
87
+ if (shape !== void 0) targets.push({
88
+ id,
89
+ x: round(shape.x + mode.dx),
90
+ y: round(shape.y + mode.dy)
91
+ });
92
+ }
93
+ return {
94
+ _tag: "Gesture",
95
+ shapes: targets,
96
+ transaction: mode.transaction
97
+ };
98
+ }
99
+ case "Resizing": return {
100
+ _tag: "Resizing",
101
+ ids: mode.ids,
102
+ box: mode.box,
103
+ transaction: mode.transaction
104
+ };
105
+ case "Marquee": return {
106
+ _tag: "Selecting",
107
+ box: roundBox(boxFromPoints(mode.origin, mode.point))
108
+ };
109
+ case "Creating": return {
110
+ _tag: "Creating",
111
+ draft: mode.draft,
112
+ box: mode.box,
113
+ id: newId(state, draftKind(mode.draft))
114
+ };
115
+ case "Connecting": return {
116
+ _tag: "Connecting",
117
+ arrow: mode.arrow ?? newId(state, "Arrow"),
118
+ from: mode.from,
119
+ to: mode.to
120
+ };
121
+ case "Inking": return {
122
+ _tag: "Inking",
123
+ points: mode.points,
124
+ color: state.style.color ?? "ink",
125
+ size: state.style.size ?? "m",
126
+ brush: state.style.brush ?? "pen",
127
+ id: newId(state, "Ink")
128
+ };
129
+ case "Erasing": return {
130
+ _tag: "Erasing",
131
+ ids: mode.ids
132
+ };
133
+ case "EditingText": return {
134
+ _tag: "Typing",
135
+ id: mode.id
136
+ };
137
+ case "Idle":
138
+ case "Pressing":
139
+ case "Panning":
140
+ case "Placing": return { _tag: "Idle" };
141
+ }
142
+ };
143
+ const shapeItems = (fonts, shapes, shape, keyPrefix, ghost, out) => {
144
+ const key = `${keyPrefix}${shape.id}`;
145
+ const box = {
146
+ x: shape.x,
147
+ y: shape.y,
148
+ w: shape.w,
149
+ h: shape.h
150
+ };
151
+ const h = shape.kind === "Arrow" ? shapeHash(ghost, shape, anchored(shapes, shape.from), anchored(shapes, shape.to)) : shapeHash(ghost, shape, null, null);
152
+ switch (shape.kind) {
153
+ case "Sticky":
154
+ out.push({
155
+ _tag: "Rect",
156
+ key: `${key}/body`,
157
+ rev: finish(h, "Rect"),
158
+ id: shape.id,
159
+ box,
160
+ fill: shape.style.color,
161
+ tone: "note",
162
+ stroke: null,
163
+ dash: "solid",
164
+ radius: RADIUS.sticky,
165
+ shadow: true,
166
+ ghost
167
+ });
168
+ break;
169
+ case "Box":
170
+ if (shape.style.form === "ellipse") out.push({
171
+ _tag: "Ellipse",
172
+ key: `${key}/body`,
173
+ rev: finish(h, "Ellipse"),
174
+ id: shape.id,
175
+ box,
176
+ fill: shape.style.fill ? shape.style.color : null,
177
+ tone: "light",
178
+ stroke: shape.style.color,
179
+ dash: shape.style.dash,
180
+ ghost
181
+ });
182
+ else out.push({
183
+ _tag: "Rect",
184
+ key: `${key}/body`,
185
+ rev: finish(h, "Rect"),
186
+ id: shape.id,
187
+ box,
188
+ fill: shape.style.fill ? shape.style.color : null,
189
+ tone: "light",
190
+ stroke: shape.style.color,
191
+ dash: shape.style.dash,
192
+ radius: RADIUS.box,
193
+ shadow: false,
194
+ ghost
195
+ });
196
+ break;
197
+ case "Arrow":
198
+ out.push({
199
+ _tag: "Path",
200
+ key: `${key}/body`,
201
+ rev: finish(h, "Path"),
202
+ id: shape.id,
203
+ d: polylinePath(route(shapes, shape).points),
204
+ stroke: shape.style.color,
205
+ width: STROKE_WIDTH.arrow,
206
+ dash: shape.style.dash,
207
+ opacity: 1,
208
+ start: shape.style.start,
209
+ end: shape.style.end,
210
+ ghost
211
+ });
212
+ return;
213
+ case "Ink": {
214
+ const stroke = inkStroke(shape.style);
215
+ out.push({
216
+ _tag: "Path",
217
+ key: `${key}/body`,
218
+ rev: finish(h, "Path"),
219
+ id: shape.id,
220
+ d: strokePath(inkPoints(shape)),
221
+ stroke: shape.style.color,
222
+ width: stroke.width,
223
+ dash: "solid",
224
+ opacity: stroke.opacity,
225
+ start: "none",
226
+ end: "none",
227
+ ghost
228
+ });
229
+ return;
230
+ }
231
+ case "Image":
232
+ out.push({
233
+ _tag: "Image",
234
+ key: `${key}/body`,
235
+ rev: finish(h, "Image"),
236
+ id: shape.id,
237
+ box,
238
+ asset: shape.asset,
239
+ alt: shape.alt,
240
+ ghost
241
+ });
242
+ return;
243
+ case "Timestamp":
244
+ out.push({
245
+ _tag: "Timestamp",
246
+ key: `${key}/body`,
247
+ rev: finish(h, "Timestamp"),
248
+ id: shape.id,
249
+ box,
250
+ at: shape.at,
251
+ ghost
252
+ });
253
+ return;
254
+ }
255
+ if (hasText(shape) && shape.text !== "") {
256
+ const layout = layoutText(fonts, shape);
257
+ out.push({
258
+ _tag: "TextLines",
259
+ key: `${key}/text`,
260
+ rev: finish(h, "TextLines"),
261
+ id: shape.id,
262
+ box: roundBox(layout.area),
263
+ lines: layout.lines,
264
+ font: layout.font,
265
+ color: shape.kind === "Text" ? shape.style.color : "ink",
266
+ align: layout.align,
267
+ ghost
268
+ });
269
+ }
270
+ };
271
+ const anchored = (shapes, anchor) => anchor._tag === "Shape" ? shapes.get(anchor.id) ?? null : null;
272
+ const guidePath = (shapes, id, from, to) => {
273
+ return polylinePath(route(shapes, {
274
+ id,
275
+ kind: "Arrow",
276
+ x: 0,
277
+ y: 0,
278
+ w: 0,
279
+ h: 0,
280
+ z: "V",
281
+ group: null,
282
+ locked: false,
283
+ createdBy: id,
284
+ updatedBy: id,
285
+ from,
286
+ to,
287
+ style: {
288
+ color: "gray",
289
+ start: "none",
290
+ end: "arrow",
291
+ route: "straight",
292
+ dash: "solid"
293
+ }
294
+ }).points);
295
+ };
296
+ const handleBox = (p, zoom) => {
297
+ const size = INTERACTION.handleSize / zoom;
298
+ return roundBox({
299
+ x: p.x - size / 2,
300
+ y: p.y - size / 2,
301
+ w: size,
302
+ h: size
303
+ });
304
+ };
305
+ /** `prefix` is `actor/<actor>/<connection>`; `pointer` draws the marquee too. */
306
+ const activityItems = (shapes, prefix, actor, kind, activity, mine, pointer, out) => {
307
+ switch (activity._tag) {
308
+ case "Selecting":
309
+ if (!pointer) break;
310
+ add(out, {
311
+ _tag: "Marquee",
312
+ key: `${prefix}/marquee`,
313
+ actor,
314
+ kind,
315
+ mine,
316
+ box: activity.box
317
+ });
318
+ break;
319
+ case "Creating":
320
+ if (activity.id !== null && shapes.has(activity.id)) break;
321
+ add(out, {
322
+ _tag: "Draft",
323
+ key: `${prefix}/draft`,
324
+ actor,
325
+ kind,
326
+ draft: activity.draft,
327
+ box: activity.box
328
+ });
329
+ break;
330
+ case "Connecting":
331
+ if (activity.arrow === null || !shapes.has(activity.arrow)) add(out, {
332
+ _tag: "Guide",
333
+ key: `${prefix}/guide`,
334
+ actor,
335
+ kind,
336
+ d: guidePath(shapes, actor, activity.from, activity.to),
337
+ start: "none",
338
+ end: "arrow"
339
+ });
340
+ break;
341
+ case "Inking": {
342
+ if (activity.id !== null && shapes.has(activity.id)) break;
343
+ const stroke = inkStroke(activity);
344
+ add(out, {
345
+ _tag: "InkPreview",
346
+ key: `${prefix}/ink`,
347
+ actor,
348
+ d: strokePath(activity.points),
349
+ stroke: activity.color,
350
+ width: stroke.width,
351
+ opacity: stroke.opacity
352
+ });
353
+ break;
354
+ }
355
+ }
356
+ };
357
+ /** Each pending proposal's rows on the board it was last drawn over, kept while that board is passed again. */
358
+ const proposalViews = /* @__PURE__ */ new WeakMap();
359
+ const proposalRows = (index, record) => {
360
+ const cached = proposalViews.get(record);
361
+ if (cached?.base === index.shapes) return cached.after;
362
+ const after = new Map(index.shapes);
363
+ applyProposal(after, record);
364
+ proposalViews.set(record, {
365
+ base: index.shapes,
366
+ after
367
+ });
368
+ return after;
369
+ };
370
+ const buildScene = (fonts, board, index, view, presence, editor) => {
371
+ const viewer = view.viewer;
372
+ const entries = connections(presence);
373
+ const own = editor !== null ? {
374
+ actor: viewer ?? "",
375
+ connection: editor.session
376
+ } : entries.findLast((entry) => entry.actor === viewer) ?? null;
377
+ const self = viewer ?? editor?.session ?? "viewer";
378
+ const prefix = `actor/${self}/${own?.connection ?? "viewer"}`;
379
+ const others = entries.filter((entry) => !sameConnection(entry, own));
380
+ const claims = claimants(presence, editor === null ? null : own, editor === null ? null : claimsOf(editor.mode));
381
+ const holds = (who, activity) => movedIds(activity).every((id) => {
382
+ const holder = claims.get(id)?.[0];
383
+ return holder === void 0 || sameConnection(holder, who);
384
+ });
385
+ const live = new Map(index.shapes);
386
+ for (const entry of others) if (holds(entry, entry.activity)) moved(fonts, live, index.shapes, entry.activity);
387
+ const ownActivity = editor === null ? null : activityOf(editor, index.shapes);
388
+ if (ownActivity !== null && own !== null && holds(own, ownActivity)) moved(fonts, live, index.shapes, ownActivity);
389
+ const editing = editor?.mode._tag === "EditingText" ? editor.mode : null;
390
+ if (editing !== null) {
391
+ const shape = live.get(editing.id);
392
+ if (shape !== void 0 && hasText(shape)) live.set(shape.id, fitText(fonts, {
393
+ ...shape,
394
+ text: editing.draft
395
+ }));
396
+ }
397
+ const erasing = /* @__PURE__ */ new Map();
398
+ for (const entry of others) if (entry.activity._tag === "Erasing") for (const id of entry.activity.ids) erasing.set(id, entry.actor);
399
+ if (editor?.mode._tag === "Erasing") for (const id of editor.mode.ids) erasing.set(id, self);
400
+ const cull = inflate(view, INTERACTION.cullMargin / view.zoom);
401
+ const items = [];
402
+ for (const original of index.ordered) {
403
+ const shape = live.get(original.id) ?? original;
404
+ if (!intersects(bounds(live, shape), cull)) continue;
405
+ const by = erasing.get(shape.id);
406
+ shapeItems(fonts, live, shape, "shape/", by === void 0 ? null : {
407
+ _tag: "Erasing",
408
+ actor: by
409
+ }, items);
410
+ }
411
+ if (editor?.mode._tag === "Dragging" && editor.mode.copy) {
412
+ const { dx, dy } = editor.mode;
413
+ const ids = /* @__PURE__ */ new Map();
414
+ for (const id of editor.mode.ids) if (index.shapes.has(id)) ids.set(id, id);
415
+ const copies = new Map(live);
416
+ for (const id of ids.keys()) {
417
+ const shape = index.shapes.get(id);
418
+ if (shape !== void 0) copies.set(id, copyShape(index.shapes, shape, ids, dx, dy));
419
+ }
420
+ for (const id of ids.keys()) {
421
+ const shape = copies.get(id);
422
+ if (shape !== void 0) shapeItems(fonts, copies, shape, "copy/", null, items);
423
+ }
424
+ }
425
+ for (const record of pendingProposals(board, history(board))) {
426
+ const after = proposalRows(index, record);
427
+ for (const change of record.changes) {
428
+ const id = change.op === "set" ? change.shape.id : change.id;
429
+ const shown = change.op === "remove" ? index.shapes.get(id) : after.get(id);
430
+ if (shown === void 0 || !intersects(bounds(after, shown), cull)) continue;
431
+ const kind = change.op === "remove" ? "remove" : index.shapes.has(id) ? "change" : "add";
432
+ shapeItems(fonts, after, shown, `proposal/${record.id}/`, {
433
+ _tag: "Proposal",
434
+ transaction: record.id,
435
+ change: kind
436
+ }, items);
437
+ }
438
+ }
439
+ const selectionItems = (who, actor, kind, ids, mine) => {
440
+ for (const id of new Set(ids)) {
441
+ const shape = live.get(id);
442
+ if (shape === void 0) continue;
443
+ add(items, {
444
+ _tag: "Selection",
445
+ key: `${who}/selection/${id}`,
446
+ actor,
447
+ kind,
448
+ mine,
449
+ id,
450
+ box: roundBox(bounds(live, shape))
451
+ });
452
+ }
453
+ };
454
+ for (const entry of others) {
455
+ const who = `actor/${entry.actor}/${entry.connection}`;
456
+ const pointer = entry.actor !== viewer;
457
+ if (pointer) selectionItems(who, entry.actor, entry.kind, entry.selection, false);
458
+ activityItems(live, who, entry.actor, entry.kind, entry.activity, false, pointer, items);
459
+ uploadItems(who, entry.actor, entry.kind, entry.uploads, false, items);
460
+ }
461
+ if (editor === null) {
462
+ const mine = entries.find((entry) => sameConnection(entry, own));
463
+ if (mine !== void 0) {
464
+ uploadItems(prefix, self, mine.kind, mine.uploads, true, items);
465
+ selectionItems(prefix, self, mine.kind, mine.selection, true);
466
+ }
467
+ } else overlay(fonts, live, editor, self, prefix, view.zoom, items);
468
+ for (const entry of others) {
469
+ if (entry.cursor === null || entry.actor === viewer) continue;
470
+ const status = entry.activity._tag === "Thinking" || entry.activity._tag === "Working" ? entry.activity.label : "";
471
+ add(items, {
472
+ _tag: "Cursor",
473
+ key: `actor/${entry.actor}/${entry.connection}/cursor`,
474
+ actor: entry.actor,
475
+ kind: entry.kind,
476
+ point: entry.cursor.point,
477
+ label: status === "" ? entry.name : `${entry.name} · ${status}`,
478
+ arriveInMs: entry.cursor.arriveInMs,
479
+ chat: entry.chat
480
+ });
481
+ }
482
+ return {
483
+ box: roundBox(view),
484
+ zoom: view.zoom,
485
+ items
486
+ };
487
+ };
488
+ /** The viewer's own feedback; keys start with `prefix`. */
489
+ const overlay = (fonts, live, editor, self, prefix, zoom, items) => {
490
+ const mode = editor.mode;
491
+ const hover = editor.hover;
492
+ if ((mode._tag === "Idle" || mode._tag === "Pressing") && hover._tag === "Shape" && !editor.selection.includes(hover.id)) {
493
+ const shape = live.get(hover.id);
494
+ if (shape !== void 0) add(items, {
495
+ _tag: "Hover",
496
+ key: `${prefix}/hover`,
497
+ id: shape.id,
498
+ d: outlinePath(live, shape)
499
+ });
500
+ }
501
+ const gesturing = mode._tag === "Dragging" || mode._tag === "Marquee";
502
+ for (const id of new Set(editor.selection)) {
503
+ const shape = live.get(id);
504
+ if (shape === void 0) continue;
505
+ add(items, {
506
+ _tag: "Selection",
507
+ key: `${prefix}/selection/${id}`,
508
+ actor: self,
509
+ kind: "person",
510
+ mine: true,
511
+ id,
512
+ box: roundBox(bounds(live, shape))
513
+ });
514
+ }
515
+ if (!gesturing && mode._tag !== "EditingText") {
516
+ const handled = editor.readOnly ? [] : resizable(live, editor.selection);
517
+ const box = boundsOf(live, handled);
518
+ if (box !== null) {
519
+ add(items, {
520
+ _tag: "Bounds",
521
+ key: `${prefix}/bounds`,
522
+ box: roundBox(box)
523
+ });
524
+ for (const handle of RESIZE_HANDLES) add(items, {
525
+ _tag: "Handle",
526
+ key: `${prefix}/handle/${handle}`,
527
+ handle,
528
+ box: handleBox(handlePoint(box, handle), zoom)
529
+ });
530
+ }
531
+ const only = editor.selection.length === 1 ? live.get(editor.selection[0] ?? "") : void 0;
532
+ if (only?.kind === "Arrow" && !only.locked && !editor.readOnly) {
533
+ const r = route(live, only);
534
+ add(items, {
535
+ _tag: "Handle",
536
+ key: `${prefix}/handle/from`,
537
+ handle: "from",
538
+ box: handleBox(routeStart(r), zoom)
539
+ });
540
+ add(items, {
541
+ _tag: "Handle",
542
+ key: `${prefix}/handle/to`,
543
+ handle: "to",
544
+ box: handleBox(routeEnd(r), zoom)
545
+ });
546
+ }
547
+ }
548
+ activityItems(live, prefix, self, "person", activityOf(editor, live), true, true, items);
549
+ uploadItems(prefix, self, "person", editor.uploads, true, items);
550
+ if (mode._tag === "Erasing" && mode.trail.length > 0) add(items, {
551
+ _tag: "EraserTrail",
552
+ key: `${prefix}/eraser`,
553
+ d: polylinePath(mode.trail)
554
+ });
555
+ (mode._tag === "Dragging" || mode._tag === "Resizing" || mode._tag === "Creating" ? mode.guides : []).forEach((guide, i) => {
556
+ add(items, {
557
+ _tag: "SnapGuide",
558
+ key: `${prefix}/snap/${i}`,
559
+ guide: guide.guide,
560
+ from: guide.from,
561
+ to: guide.to
562
+ });
563
+ });
564
+ if (mode._tag === "EditingText") {
565
+ const shape = live.get(mode.id);
566
+ if (shape !== void 0 && hasText(shape)) {
567
+ const layout = layoutText(fonts, shape);
568
+ add(items, {
569
+ _tag: "TextEditor",
570
+ key: `${prefix}/editor`,
571
+ id: shape.id,
572
+ box: roundBox(layout.area),
573
+ font: layout.font,
574
+ color: shape.kind === "Text" ? shape.style.color : "ink",
575
+ align: layout.align
576
+ });
577
+ }
578
+ }
579
+ };
580
+ export { activityOf, buildScene, createdId, draftKind, newId, nextTransaction };