@zsherman/canvas 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 (196) hide show
  1. package/CHANGELOG.md +24 -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 +1354 -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 +60 -4
@@ -0,0 +1,559 @@
1
+ import { instantFull, instantLabel } from "./instant.js";
2
+ import { pendingClass, timestampClass } from "./scene.styles.js";
3
+ import { NOTE_INK, SELECTION, SHADOW, SNAP, actorColor, paintOf, strokeOf } from "./palette.js";
4
+ // src/canvas/scene-renderer.ts
5
+ const SVG_NS = "http://www.w3.org/2000/svg";
6
+ const XHTML_NS = "http://www.w3.org/1999/xhtml";
7
+ /** Text CSS the kernel measures with; see the kernel README. */
8
+ const TEXT_STYLE = "font-kerning:none;font-variant-ligatures:none;font-feature-settings:'calt' 0;font-optical-sizing:auto;white-space:pre";
9
+ /** Outline width of boxes and ellipses, in board units. */
10
+ const OUTLINE_WIDTH = 2;
11
+ const number = (value) => String(Math.round(value * 100) / 100);
12
+ function attributes(entries) {
13
+ return new Map(Object.entries(entries));
14
+ }
15
+ /** Overlay strokes stay this many screen pixels wide at any zoom. */
16
+ function setHairline(target, width = 1) {
17
+ target.set("stroke-width", String(width));
18
+ target.set("vector-effect", "non-scaling-stroke");
19
+ }
20
+ function setDash(target, dash, width) {
21
+ if (dash === "dashed") target.set("stroke-dasharray", `${number(width * 4)} ${number(width * 3)}`);
22
+ else if (dash === "dotted") {
23
+ target.set("stroke-dasharray", `0 ${number(width * 2.5)}`);
24
+ target.set("stroke-linecap", "round");
25
+ }
26
+ }
27
+ function setGhost(target, ghost) {
28
+ if (ghost !== null) target.set("opacity", ghost._tag === "Erasing" ? "0.3" : "0.45");
29
+ }
30
+ function setOutline(target, stroke, dash) {
31
+ if (stroke === null) return;
32
+ target.set("stroke", strokeOf(stroke));
33
+ target.set("stroke-width", String(OUTLINE_WIDTH));
34
+ setDash(target, dash, OUTLINE_WIDTH);
35
+ }
36
+ function setOpacity(target, opacity) {
37
+ if (opacity !== 1) target.set("stroke-opacity", number(opacity));
38
+ }
39
+ function boxAttributes(box) {
40
+ return {
41
+ height: number(Math.max(box.h, 0)),
42
+ width: number(Math.max(box.w, 0)),
43
+ x: number(box.x),
44
+ y: number(box.y)
45
+ };
46
+ }
47
+ function fontAttributes(font) {
48
+ return {
49
+ "font-family": font.families.map((family) => family.includes(" ") ? `"${family}"` : family).join(", "),
50
+ "font-size": number(font.size),
51
+ "font-weight": String(font.weight),
52
+ style: TEXT_STYLE
53
+ };
54
+ }
55
+ /** Renderers made so far; each prefixes its SVG ids, so two boards on a page keep their own. */
56
+ let renderers = 0;
57
+ /** Shape ids drawn as sticky-note paper. */
58
+ function notesOf(scene) {
59
+ return new Set(scene.items.flatMap((item) => item._tag === "Rect" && item.tone === "note" ? [item.id] : []));
60
+ }
61
+ function specOf(item, options, facts, markerFor) {
62
+ const nameOf = (actor) => options.names.get(actor) ?? actor;
63
+ switch (item._tag) {
64
+ case "Rect": {
65
+ const target = attributes({
66
+ ...boxAttributes(item.box),
67
+ fill: item.fill === null ? "none" : paintOf(item.fill, item.tone),
68
+ rx: number(item.radius)
69
+ });
70
+ setOutline(target, item.stroke, item.dash);
71
+ if (item.shadow) target.set("filter", facts.shadow);
72
+ setGhost(target, item.ghost);
73
+ return {
74
+ attributes: target,
75
+ tag: "rect"
76
+ };
77
+ }
78
+ case "Ellipse": {
79
+ const target = attributes({
80
+ cx: number(item.box.x + item.box.w / 2),
81
+ cy: number(item.box.y + item.box.h / 2),
82
+ fill: item.fill === null ? "none" : paintOf(item.fill, item.tone),
83
+ rx: number(Math.max(item.box.w / 2, 0)),
84
+ ry: number(Math.max(item.box.h / 2, 0))
85
+ });
86
+ setOutline(target, item.stroke, item.dash);
87
+ setGhost(target, item.ghost);
88
+ return {
89
+ attributes: target,
90
+ tag: "ellipse"
91
+ };
92
+ }
93
+ case "TextLines": {
94
+ const target = attributes({
95
+ ...fontAttributes(item.font),
96
+ fill: facts.notes.has(item.id) ? NOTE_INK : strokeOf(item.color)
97
+ });
98
+ if (options.editing === item.id) target.set("visibility", "hidden");
99
+ setGhost(target, item.ghost);
100
+ return {
101
+ attributes: target,
102
+ lines: item.lines,
103
+ tag: "text"
104
+ };
105
+ }
106
+ case "Path": {
107
+ const target = attributes({
108
+ d: item.d,
109
+ fill: "none",
110
+ stroke: strokeOf(item.stroke),
111
+ "stroke-linecap": "round",
112
+ "stroke-linejoin": "round",
113
+ "stroke-width": number(item.width)
114
+ });
115
+ setDash(target, item.dash, item.width);
116
+ setOpacity(target, item.opacity);
117
+ if (item.start !== "none") target.set("marker-start", markerFor(item.start, item.stroke));
118
+ if (item.end !== "none") target.set("marker-end", markerFor(item.end, item.stroke));
119
+ setGhost(target, item.ghost);
120
+ return {
121
+ attributes: target,
122
+ tag: "path"
123
+ };
124
+ }
125
+ case "InkPreview": {
126
+ const target = attributes({
127
+ d: item.d,
128
+ fill: "none",
129
+ stroke: strokeOf(item.stroke),
130
+ "stroke-linecap": "round",
131
+ "stroke-linejoin": "round",
132
+ "stroke-width": number(item.width)
133
+ });
134
+ setOpacity(target, item.opacity);
135
+ return {
136
+ attributes: target,
137
+ tag: "path"
138
+ };
139
+ }
140
+ case "Guide": {
141
+ const target = attributes({
142
+ d: item.d,
143
+ fill: "none",
144
+ stroke: actorColor(item.kind, nameOf(item.actor)),
145
+ "stroke-dasharray": "6 4"
146
+ });
147
+ setHairline(target, 2);
148
+ return {
149
+ attributes: target,
150
+ tag: "path"
151
+ };
152
+ }
153
+ case "Draft": {
154
+ const target = attributes({
155
+ ...boxAttributes(item.box),
156
+ fill: "none",
157
+ stroke: actorColor(item.kind, nameOf(item.actor)),
158
+ "stroke-dasharray": "4 4"
159
+ });
160
+ setHairline(target);
161
+ return {
162
+ attributes: target,
163
+ tag: "rect"
164
+ };
165
+ }
166
+ case "Hover": {
167
+ const target = attributes({
168
+ d: item.d,
169
+ fill: "none",
170
+ stroke: SELECTION
171
+ });
172
+ setHairline(target, 1.5);
173
+ return {
174
+ attributes: target,
175
+ tag: "path"
176
+ };
177
+ }
178
+ case "Selection": {
179
+ const target = attributes({
180
+ ...boxAttributes(item.box),
181
+ fill: "none",
182
+ stroke: item.mine ? SELECTION : actorColor(item.kind, nameOf(item.actor))
183
+ });
184
+ setHairline(target, item.mine ? 1 : 2);
185
+ if (item.mine && facts.grouped) target.set("stroke-opacity", "0.4");
186
+ return {
187
+ attributes: target,
188
+ tag: "rect"
189
+ };
190
+ }
191
+ case "Bounds": {
192
+ const target = attributes({
193
+ ...boxAttributes(item.box),
194
+ fill: "none",
195
+ stroke: SELECTION
196
+ });
197
+ setHairline(target);
198
+ return {
199
+ attributes: target,
200
+ tag: "rect"
201
+ };
202
+ }
203
+ case "Handle": {
204
+ const end = item.handle === "from" || item.handle === "to";
205
+ const target = attributes({
206
+ ...boxAttributes(item.box),
207
+ fill: "var(--sui-surface)",
208
+ rx: number(end ? item.box.w / 2 : item.box.w / 4),
209
+ stroke: SELECTION
210
+ });
211
+ setHairline(target, 1.5);
212
+ return {
213
+ attributes: target,
214
+ tag: "rect"
215
+ };
216
+ }
217
+ case "Marquee": {
218
+ const color = item.mine ? SELECTION : actorColor(item.kind, nameOf(item.actor));
219
+ const target = attributes({
220
+ ...boxAttributes(item.box),
221
+ fill: color,
222
+ "fill-opacity": "0.08",
223
+ stroke: color
224
+ });
225
+ setHairline(target);
226
+ return {
227
+ attributes: target,
228
+ tag: "rect"
229
+ };
230
+ }
231
+ case "SnapGuide": {
232
+ const target = attributes({
233
+ stroke: SNAP,
234
+ x1: number(item.from.x),
235
+ x2: number(item.to.x),
236
+ y1: number(item.from.y),
237
+ y2: number(item.to.y)
238
+ });
239
+ setHairline(target);
240
+ if (item.guide === "spacing") target.set("stroke-dasharray", "2 2");
241
+ return {
242
+ attributes: target,
243
+ tag: "line"
244
+ };
245
+ }
246
+ case "EraserTrail": return {
247
+ attributes: attributes({
248
+ d: item.d,
249
+ fill: "none",
250
+ stroke: "var(--sui-text-muted)",
251
+ "stroke-linecap": "round",
252
+ "stroke-linejoin": "round",
253
+ "stroke-opacity": "0.35",
254
+ "stroke-width": "8",
255
+ "vector-effect": "non-scaling-stroke"
256
+ }),
257
+ tag: "path"
258
+ };
259
+ case "Image": {
260
+ const source = options.image?.(item.asset, item.box.w * facts.density) ?? MISSING;
261
+ if (source.status === "ready") {
262
+ const target = attributes({
263
+ ...boxAttributes(item.box),
264
+ decoding: "async",
265
+ href: source.src,
266
+ preserveAspectRatio: "none"
267
+ });
268
+ setGhost(target, item.ghost);
269
+ return {
270
+ attributes: target,
271
+ tag: "image"
272
+ };
273
+ }
274
+ const target = attributes({
275
+ ...boxAttributes(item.box),
276
+ "data-state": source.status,
277
+ fill: "var(--sui-surface-muted)",
278
+ stroke: "var(--sui-border)"
279
+ });
280
+ setHairline(target);
281
+ if (source.status === "pending") target.set("class", pendingClass);
282
+ else target.set("stroke-dasharray", "4 4");
283
+ setGhost(target, item.ghost);
284
+ return {
285
+ attributes: target,
286
+ tag: "rect"
287
+ };
288
+ }
289
+ case "Timestamp": {
290
+ const target = attributes({
291
+ "data-timestamp": String(item.at),
292
+ role: "img"
293
+ });
294
+ setGhost(target, item.ghost);
295
+ return {
296
+ attributes: target,
297
+ chip: {
298
+ box: item.box,
299
+ label: instantLabel(item.at),
300
+ title: instantFull(item.at)
301
+ },
302
+ tag: "g"
303
+ };
304
+ }
305
+ case "Upload": {
306
+ const color = item.mine ? SELECTION : actorColor(item.kind, nameOf(item.actor));
307
+ const target = attributes({
308
+ ...boxAttributes(item.box),
309
+ fill: "var(--sui-surface-muted)",
310
+ stroke: color,
311
+ "stroke-dasharray": "6 4"
312
+ });
313
+ setHairline(target, 1.5);
314
+ return {
315
+ attributes: target,
316
+ tag: "rect"
317
+ };
318
+ }
319
+ case "Cursor":
320
+ case "TextEditor": return null;
321
+ }
322
+ }
323
+ const MISSING = {
324
+ reason: "missing",
325
+ status: "failed"
326
+ };
327
+ /**
328
+ * The glyph drawn at a connector end, in a 10×10 marker box drawn at 8× the
329
+ * stroke width; the chevron's line matches the stroke. Mirrors the kernel's `toSvg`.
330
+ */
331
+ function headGlyph(head) {
332
+ switch (head) {
333
+ case "arrow": return {
334
+ attributes: attributes({
335
+ d: "M1 1L9 5L1 9",
336
+ fill: "none",
337
+ "stroke-linecap": "round",
338
+ "stroke-linejoin": "round",
339
+ "stroke-width": "1.25"
340
+ }),
341
+ tag: "path"
342
+ };
343
+ case "triangle": return {
344
+ attributes: attributes({ d: "M0 0L10 5L0 10Z" }),
345
+ tag: "path"
346
+ };
347
+ case "dot": return {
348
+ attributes: attributes({
349
+ cx: "5",
350
+ cy: "5",
351
+ r: "4"
352
+ }),
353
+ tag: "ellipse"
354
+ };
355
+ case "none": return null;
356
+ }
357
+ }
358
+ const applied = /* @__PURE__ */ new WeakMap();
359
+ const appliedLines = /* @__PURE__ */ new WeakMap();
360
+ function patchAttributes(element, next) {
361
+ const previous = applied.get(element);
362
+ for (const name of previous?.keys() ?? []) if (!next.has(name)) element.removeAttribute(name);
363
+ for (const [name, value] of next) if (previous?.get(name) !== value) element.setAttribute(name, value);
364
+ applied.set(element, next);
365
+ }
366
+ /** True when two text layouts place the same lines in the same spots. */
367
+ function sameLines(a, b) {
368
+ if (a === b) return true;
369
+ if (a.length !== b.length) return false;
370
+ return a.every((line, index) => {
371
+ const other = b[index];
372
+ return other !== void 0 && line.text === other.text && line.x === other.x && line.y === other.y;
373
+ });
374
+ }
375
+ function patchLines(element, lines = []) {
376
+ const applied = appliedLines.get(element);
377
+ if (applied !== void 0 && sameLines(applied, lines)) return;
378
+ appliedLines.set(element, lines);
379
+ element.replaceChildren(...lines.map((line) => {
380
+ const span = document.createElementNS(SVG_NS, "tspan");
381
+ span.setAttribute("x", number(line.x));
382
+ span.setAttribute("y", number(line.y));
383
+ span.textContent = line.text;
384
+ return span;
385
+ }));
386
+ }
387
+ /** Fills a timestamp's `g`: the full time as its title, and a `Badge` of the time in its box. */
388
+ function patchChip(element, chip) {
389
+ const title = create("title");
390
+ title.textContent = chip.title;
391
+ const holder = create("foreignObject", boxAttributes(chip.box));
392
+ const badge = document.createElementNS(XHTML_NS, "span");
393
+ badge.setAttribute("class", timestampClass);
394
+ badge.setAttribute("data-slot", "badge");
395
+ badge.textContent = chip.label;
396
+ holder.append(badge);
397
+ element.replaceChildren(title, holder);
398
+ }
399
+ function create(tag, attributes = {}) {
400
+ const element = document.createElementNS(SVG_NS, tag);
401
+ for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value);
402
+ return element;
403
+ }
404
+ /**
405
+ * Draws kernel scenes into an `<svg>` by key: each frame updates the
406
+ * elements whose items changed, moves them into scene order and removes the
407
+ * rest, so a frame costs what changed, not the whole board.
408
+ */
409
+ function createSceneRenderer(svg) {
410
+ renderers += 1;
411
+ const prefix = `canvas-${renderers}`;
412
+ const shadowId = `${prefix}-shadow`;
413
+ const gridId = `${prefix}-grid`;
414
+ const defs = create("defs");
415
+ const shadow = create("filter", {
416
+ height: "140%",
417
+ id: shadowId,
418
+ width: "130%",
419
+ x: "-15%",
420
+ y: "-15%"
421
+ });
422
+ shadow.append(create("feDropShadow", {
423
+ dx: "0",
424
+ dy: "2",
425
+ "flood-color": SHADOW,
426
+ "flood-opacity": "0.14",
427
+ stdDeviation: "3"
428
+ }));
429
+ const gridPattern = create("pattern", {
430
+ id: gridId,
431
+ patternUnits: "userSpaceOnUse"
432
+ });
433
+ const gridDot = create("circle", { fill: "var(--sui-border)" });
434
+ gridPattern.append(gridDot);
435
+ defs.append(shadow, gridPattern);
436
+ const background = create("rect", { fill: "transparent" });
437
+ const layer = create("g");
438
+ svg.replaceChildren(defs, background, layer);
439
+ /** Each item's element by key, with the rev it was drawn at. */
440
+ const drawn = /* @__PURE__ */ new Map();
441
+ const markers = /* @__PURE__ */ new Map();
442
+ let frame = 0;
443
+ let lastContext = null;
444
+ let lastImage;
445
+ const markerFor = (head, color) => {
446
+ const id = `${prefix}-head-${head}-${color}`;
447
+ if (!markers.has(id)) {
448
+ const marker = create("marker", {
449
+ fill: strokeOf(color),
450
+ id,
451
+ markerHeight: "8",
452
+ markerWidth: "8",
453
+ orient: "auto-start-reverse",
454
+ refX: head === "dot" ? "5" : "9",
455
+ refY: "5",
456
+ stroke: strokeOf(color),
457
+ viewBox: "0 0 10 10"
458
+ });
459
+ const glyph = headGlyph(head);
460
+ if (glyph !== null) marker.append(create(glyph.tag, Object.fromEntries(glyph.attributes)));
461
+ defs.append(marker);
462
+ markers.set(id, marker);
463
+ }
464
+ return `url(#${id})`;
465
+ };
466
+ function render(scene, view, options) {
467
+ const box = `${number(view.x)} ${number(view.y)} ${number(view.w)} ${number(view.h)}`;
468
+ if (svg.getAttribute("viewBox") !== box) svg.setAttribute("viewBox", box);
469
+ patchAttributes(background, attributes({
470
+ ...boxAttributes(view),
471
+ fill: options.grid === null ? "transparent" : `url(#${gridId})`
472
+ }));
473
+ if (options.grid !== null) {
474
+ patchAttributes(gridPattern, attributes({
475
+ height: number(options.grid),
476
+ id: gridId,
477
+ patternUnits: "userSpaceOnUse",
478
+ width: number(options.grid),
479
+ x: number(-options.grid / 2),
480
+ y: number(-options.grid / 2)
481
+ }));
482
+ patchAttributes(gridDot, attributes({
483
+ cx: number(options.grid / 2),
484
+ cy: number(options.grid / 2),
485
+ fill: "var(--sui-border)",
486
+ r: number(1.25 / view.zoom)
487
+ }));
488
+ }
489
+ const facts = {
490
+ grouped: scene.items.filter((item) => item._tag === "Selection" && item.mine).length > 1,
491
+ density: view.zoom * ("devicePixelRatio" in globalThis ? globalThis.devicePixelRatio : 1),
492
+ notes: notesOf(scene),
493
+ shadow: `url(#${shadowId})`
494
+ };
495
+ const context = [
496
+ options.editing,
497
+ facts.grouped,
498
+ facts.density,
499
+ ...options.names
500
+ ].join("\n");
501
+ const fresh = context !== lastContext || options.image !== lastImage;
502
+ lastContext = context;
503
+ lastImage = options.image;
504
+ frame += 1;
505
+ let index = 0;
506
+ for (const item of scene.items) {
507
+ let entry = drawn.get(item.key);
508
+ if (entry === void 0 || entry.rev !== item.rev || fresh || item._tag === "Image") {
509
+ entry = patch(item, entry, specOf(item, options, facts, markerFor));
510
+ drawn.set(item.key, entry);
511
+ }
512
+ entry.frame = frame;
513
+ const { element } = entry;
514
+ if (element === null) continue;
515
+ const current = layer.childNodes[index];
516
+ if (current !== element) layer.insertBefore(element, current ?? null);
517
+ index += 1;
518
+ }
519
+ for (const [key, entry] of drawn) if (entry.frame !== frame) {
520
+ entry.element?.remove();
521
+ drawn.delete(key);
522
+ }
523
+ }
524
+ /** Brings an item's element up to its spec, making one when the tag changed. */
525
+ function patch(item, previous, spec) {
526
+ let element = previous?.element ?? null;
527
+ if (spec === null) {
528
+ element?.remove();
529
+ return {
530
+ element: null,
531
+ frame,
532
+ rev: item.rev
533
+ };
534
+ }
535
+ if (element === null || element.localName !== spec.tag) {
536
+ element?.remove();
537
+ element = create(spec.tag);
538
+ }
539
+ const next = new Map(spec.attributes);
540
+ next.set("data-key", item.key);
541
+ patchAttributes(element, next);
542
+ if (spec.tag === "text") patchLines(element, spec.lines);
543
+ if (spec.chip !== void 0) patchChip(element, spec.chip);
544
+ return {
545
+ element,
546
+ frame,
547
+ rev: item.rev
548
+ };
549
+ }
550
+ return {
551
+ render,
552
+ dispose() {
553
+ svg.replaceChildren();
554
+ drawn.clear();
555
+ markers.clear();
556
+ }
557
+ };
558
+ }
559
+ export { createSceneRenderer };
@@ -0,0 +1,7 @@
1
+ import "@stylexjs/stylex";
2
+ // src/canvas/scene.styles.ts
3
+ /** The pulse of a waiting image's tile. */
4
+ const pendingClass = { className: "xcx56e2 x1aquc0h x1c74tu6 xw1l40g xa4qsjk" }.className ?? "";
5
+ /** A timestamp's badge. */
6
+ const timestampClass = { className: "x78zum5 x6s0dn4 xl56j7k x9f619 xh8yej3 x5yr21d xxsu4ee xmkeg23 x1y0btm7 x1b6xorz x15fh5h0 x1w1tqly xm9f0jk x195sdm2 xe2mk2a x13rcfk xb5fjo1 x1lyc3gu xss6m8b xo5v014 xuxw1ft x87ps6o" }.className ?? "";
7
+ export { pendingClass, timestampClass };