@marver-design/marver 0.30.1 → 0.32.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 (112) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +12 -8
  3. package/dist/{anchor-check-Bv4o-R2T.mjs → anchor-check-DZtX13ly.mjs} +1 -1
  4. package/dist/{board-tree-BXR8dw4b.mjs → board-tree-DGj0HhEE.mjs} +6 -2
  5. package/dist/{boards-EH2onUXM.mjs → boards-Cim1GTKc.mjs} +32 -2
  6. package/dist/{boards-BeWtjGUM.mjs → boards-jHmEPLV7.mjs} +7 -7
  7. package/dist/{build-zWGXSzly.mjs → build-D2dHEWX-.mjs} +14 -17
  8. package/dist/{capture-DEtM4qm5.mjs → capture-HaS-1or8.mjs} +1 -1
  9. package/dist/cli.mjs +38 -20
  10. package/dist/{collab-BEAcXrBb.mjs → collab-D2NcrOB8.mjs} +53 -14
  11. package/dist/comment-actions-Bxf9Ld4g.mjs +351 -0
  12. package/dist/{comments-XNhMmSSu.mjs → comments-ocvsnt2j.mjs} +85 -10
  13. package/dist/config-B0YcFtqS.mjs +510 -0
  14. package/dist/{context-BUnFiyA3.mjs → context-Cj2TcC0H.mjs} +3 -3
  15. package/dist/{daemon-CChgWJNq.mjs → daemon-D6TrdgMi.mjs} +268 -28
  16. package/dist/deep-link-DYtuT740.mjs +163 -0
  17. package/dist/{dev-DOgIITpT.mjs → dev-DMQYbw-2.mjs} +23 -16
  18. package/dist/devices-uJZ2inMq.mjs +155 -0
  19. package/dist/{events-CLH4ijKP.mjs → events-Dieo1482.mjs} +49 -4
  20. package/dist/{init-CYlFaRzG.mjs → init-BgusplYn.mjs} +40 -10
  21. package/dist/{jam-Cj5gBvwD.mjs → jam-CkuLL8A8.mjs} +1 -1
  22. package/dist/{link-BFFRgrMt.mjs → link-aZq5cExV.mjs} +54 -7
  23. package/dist/{logo-CXY-1yrn.mjs → logo-C9s7-Cy9.mjs} +89 -66
  24. package/dist/{ls-NqmePKOf.mjs → ls-DGVIW-Io.mjs} +1 -1
  25. package/dist/{manifest-DPZ3WNg_.mjs → manifest-CK8Qh4Nq.mjs} +255 -13
  26. package/dist/{marks-BnNANic9.mjs → marks-C8oX8kbE.mjs} +3 -3
  27. package/dist/{marks-DBp3h1Mc.mjs → marks-Dt-5JXCp.mjs} +18 -5
  28. package/dist/{marver-id-gate-vBLb_Icb.mjs → marver-id-gate-BAeuMeaU.mjs} +11 -7
  29. package/dist/md-B9_13Q0R.mjs +105 -0
  30. package/dist/notes-CRAX4wgk.mjs +148 -0
  31. package/dist/own-C8Mq0Hnf.mjs +128 -0
  32. package/dist/pdf-Cf9GIoIM.mjs +2299 -0
  33. package/dist/pdf-HB0prF75.mjs +117 -0
  34. package/dist/{perf-6MOTHxay.mjs → perf-9ZEF5KLO.mjs} +5 -5
  35. package/dist/{perf-BCxAo0IS.mjs → perf-BA_NDarr.mjs} +2 -1
  36. package/dist/{plugin-CFWBcAba.mjs → plugin-DesQP9L5.mjs} +565 -107
  37. package/dist/{poster-DPPpQiva.mjs → poster-Bb6WraCA.mjs} +1 -1
  38. package/dist/{profile-B1FKOM1e.mjs → profile-cdmSer1k.mjs} +1 -1
  39. package/dist/{publish-bakes-_Zx8crP3.mjs → publish-bakes-EjMmukJB.mjs} +3 -3
  40. package/dist/{queue-DeSQm24g.mjs → queue-BLLfdlDw.mjs} +156 -11
  41. package/dist/{recover-mDUVsnPd.mjs → recover-B3dmatx4.mjs} +2 -2
  42. package/dist/{serve-DQv3Ti6X.mjs → serve-kcF6-H8V.mjs} +4 -4
  43. package/dist/{session-VDJfXEEm.mjs → session-DBF-nQvq.mjs} +29 -3
  44. package/dist/{share-Bu_kuOKL.mjs → share-CS2mg27O.mjs} +1 -1
  45. package/dist/{shot-d4h_hJi9.mjs → shot-Bjt2MQe3.mjs} +4 -4
  46. package/dist/{shot-DBWN8hPP.mjs → shot-BuUCG46-.mjs} +1 -1
  47. package/dist/show-DhmiUu01.mjs +101 -0
  48. package/dist/{sizes-wjXDev3b.mjs → sizes-BckcL4Zz.mjs} +1 -1
  49. package/dist/{slots-Dn4Onb0V.mjs → slots-BOcfIqxy.mjs} +15 -3
  50. package/dist/{summary-Y8MY1Wuc.mjs → summary-cPmRDGpX.mjs} +1 -1
  51. package/dist/{sync-CMozOwZ0.mjs → sync-BNWwh8OF.mjs} +205 -7
  52. package/dist/{work-DJxH7cCX.mjs → work-B4tydWnf.mjs} +38 -2
  53. package/dist/{work-BYWD_1Zf.mjs → work-CzIXp7n1.mjs} +3 -3
  54. package/docs/app.md +143 -6
  55. package/docs/boards-and-folders.md +41 -7
  56. package/docs/jam-session.md +5 -0
  57. package/docs/live-jam.md +9 -0
  58. package/docs/pdf.md +108 -0
  59. package/docs/publish.md +9 -9
  60. package/docs/sharing.md +11 -0
  61. package/docs/slides.md +11 -1
  62. package/package.json +3 -1
  63. package/src/client/content/index.tsx +4 -2
  64. package/src/client/frame-host/main.tsx +3 -0
  65. package/src/client/shell/App.tsx +71 -24
  66. package/src/client/shell/BoardList.tsx +16 -7
  67. package/src/client/shell/Comments.tsx +77 -16
  68. package/src/client/shell/Play.tsx +104 -27
  69. package/src/client/shell/Toolbar.tsx +4 -3
  70. package/src/client/shell/app-bridge.ts +40 -5
  71. package/src/client/shell/canvas/Canvas.tsx +30 -8
  72. package/src/client/shell/canvas/FrameNode.tsx +3 -1
  73. package/src/client/shell/canvas/ctl.ts +16 -0
  74. package/src/client/shell/commands.ts +20 -0
  75. package/src/client/shell/comments-store.ts +81 -2
  76. package/src/client/shell/devices.tsx +42 -0
  77. package/src/client/shell/goto.ts +56 -9
  78. package/src/client/shell/icons.tsx +11 -4
  79. package/src/client/shell/immersive.ts +123 -0
  80. package/src/client/shell/inset.ts +29 -3
  81. package/src/client/shell/jam-lines.ts +5 -3
  82. package/src/client/shell/jam-panel.tsx +17 -5
  83. package/src/client/shell/marks-ui.tsx +54 -0
  84. package/src/client/shell/notes.ts +21 -0
  85. package/src/client/shell/pdf-export.tsx +154 -0
  86. package/src/client/shell/pdf-notes.ts +20 -0
  87. package/src/client/shell/recorder-signals.ts +5 -2
  88. package/src/client/shell/show.ts +250 -0
  89. package/src/client/shell/store.ts +23 -38
  90. package/src/client/shell/styles.css +68 -6
  91. package/src/shared/app-api.ts +90 -4
  92. package/src/shared/auto-place.ts +32 -0
  93. package/src/shared/board-tree.ts +3 -0
  94. package/src/shared/board-types.ts +6 -2
  95. package/src/shared/deep-link.ts +212 -0
  96. package/src/shared/devices.ts +89 -0
  97. package/src/shared/events.ts +48 -5
  98. package/src/shared/export-plan.ts +98 -0
  99. package/src/shared/immersive.ts +13 -0
  100. package/src/shared/perf-records.ts +1 -1
  101. package/templates/AGENTS-embedded.md +76 -12
  102. package/templates/AGENTS-studio.md +76 -12
  103. package/templates/instructions/boards.md +6 -5
  104. package/templates/instructions/configure.md +32 -2
  105. package/templates/instructions/iterate.md +8 -0
  106. package/templates/instructions/jam.md +9 -0
  107. package/templates/instructions/publish.md +14 -4
  108. package/templates/instructions/review.md +4 -0
  109. package/templates/instructions/slides.md +67 -1
  110. package/templates/instructions/welcome.md +9 -5
  111. package/templates/playbooks/publish-canvas/PLAYBOOK.md +1 -1
  112. package/dist/config-BezZTiov.mjs +0 -182
@@ -24,6 +24,12 @@
24
24
  * three white-out windows a shell asks for (R8 #38), sent only when `perfInfo().watch` is true - app 1.0.0 would refuse
25
25
  * it and spend the tab's bucket; and `mv:menu-click`'s `in`, the menu a relayed item came from (#51), absent from 1.0.0
26
26
  * (the shell then records `in: 'app'`). Owners: app-store (`watch`, `perfInfo().watch`), commands (`in`).
27
+ *
28
+ * Spec 32 (app 1.2, marver 0.32.0) amends it in writing (F2, F3b #1), frozen before its streams split and `v` still 1,
29
+ * no new FEATURE name: `mv:immersive` / `mv:immersive-state`, immersive play's wish and main's answer (C2), with the
30
+ * optional `MarverAppV1.immersive` and `onImmersive` (a shell gates on the method); and two optional `LiveEvent` fields,
31
+ * `work` and `titles` (unknown fields are stripped, so a 0.31 app ignores them), the titles bounded by the new
32
+ * `LIVE_TITLES_MAX`. `LIVE_KINDS`, `LIVE_BOUNDS` and `LIVE_WIRE` are unchanged.
27
33
  */
28
34
 
29
35
  export const APP_API_VERSION = 1
@@ -51,7 +57,9 @@ export interface Busy { busy: boolean; unsaved: boolean; why: Why[] }
51
57
  /** `mv:theme` - the shell's `play || slides || viewTheme === 'dark'` (A2.14 #26). */
52
58
  export interface Theme { dark: boolean }
53
59
  /** `mv:menu` - at most 40, no separators in 0.1 (A2.7). */
54
- export interface MenuItem { id: string; group: 'view' | 'jam'; label: string; hint?: string; checked?: boolean }
60
+ /** `group: 'file'` (app 1.1, spec 31): an item for the File menu under the canvas's name - a shell offers one only to an
61
+ * app that has `exportPdf` (an older app refuses a menu naming a group it does not know). */
62
+ export interface MenuItem { id: string; group: 'view' | 'jam' | 'file'; label: string; hint?: string; checked?: boolean }
55
63
  /** `mv:app-state` (main to canvas). `untrusted`: stop every microphone track (A2.14 #13). A4 adds `mv:open` and
56
64
  * `mv:capture`; this stays these three (A4.6 #29). */
57
65
  export type AppState = 'shown' | 'hidden' | 'untrusted'
@@ -71,10 +79,11 @@ export type MenuIn = (typeof MENU_IN)[number]
71
79
  export interface MenuClick { id: string; in?: MenuIn }
72
80
  /**
73
81
  * The ids main sends of its own, never in the page's `mv:menu`: the tab's right-click Mark All as Seen (A2.4's
74
- * channels table: "Mark all as seen sends `mv:menu-click mark-all-seen` to the canvas"). A shell that does not know
75
- * one does nothing with it.
82
+ * channels table: "Mark all as seen sends `mv:menu-click mark-all-seen` to the canvas") and Resolve All Comments
83
+ * (0.31, spec 31 B.1: sent only to a canvas whose features list `resolve-all`). A shell that does not know one does
84
+ * nothing with it.
76
85
  */
77
- export const APP_MENU_IDS = ['mark-all-seen'] as const
86
+ export const APP_MENU_IDS = ['mark-all-seen', 'resolve-all-comments'] as const
78
87
 
79
88
  /**
80
89
  * `mv:present` (main to canvas; on the wire `{ doc, ...Present }`, stamped by main like `mv:app-state`): show the
@@ -157,6 +166,16 @@ export const CANVAS_CHANNELS = {
157
166
  opened: 'mv:opened',
158
167
  /** A4: a live picture of the document's own view - an invoke (A4.6 #35) */
159
168
  capture: 'mv:capture',
169
+ /** spec 31 D1: a `marver://` link into a tab already open - the navigation command (main to canvas), never a reload */
170
+ show: 'mv:show',
171
+ /** spec 31 D3: the shell showed what `marver show` asked - its tab to the front of the window (canvas to main) */
172
+ front: 'mv:front',
173
+ /** app 1.1 (spec 31): a finished deck PDF saved where the person says - an invoke carrying only the job id */
174
+ exportPdf: 'mv:export-pdf',
175
+ /** spec 32 C2: immersive play's wish (canvas to main, a held whole-state report like `theme`: `ImmersiveWish`) */
176
+ immersive: 'mv:immersive',
177
+ /** spec 32 C2: main's answer to the wish (main to canvas, `{ doc, ...ImmersiveAnswer }`) */
178
+ immersiveState: 'mv:immersive-state',
160
179
  } as const
161
180
 
162
181
  /** Bounds main checks again (A2.14 #19): unknown fields are stripped, a known one of the wrong type or past its bound refuses the message. */
@@ -216,6 +235,14 @@ export const FEATURE = {
216
235
  open: 'open',
217
236
  /** 0.29.0 (A4.6 #33-#37): the shell asks `mv:capture` for live pictures and the server takes `POST /api/jam/picture/live` */
218
237
  capture: 'capture',
238
+ /** 0.31.0 (spec 31 D1, D3): the shell takes `mv:show` (a deep link's place, moved to or noticed over unsent words)
239
+ * and asks `mv:front` once it showed a `marver show` */
240
+ show: 'show',
241
+ /** 0.31.0 (spec 31 B.1): the shell handles `mv:menu-click resolve-all-comments` - every open thread on the canvas resolved,
242
+ * but what Jam is at work on, with its Undo. The app offers the tab menu's Resolve All Comments only to a canvas that says so */
243
+ resolveAll: 'resolve-all',
244
+ /** 0.31.0 (spec 31 Part A): the server makes deck PDFs (`/__mv/api/pdf`) and the shell asks `mv:export-pdf` to save one */
245
+ pdf: 'pdf',
219
246
  } as const
220
247
  export type Feature = (typeof FEATURE)[keyof typeof FEATURE]
221
248
 
@@ -277,6 +304,15 @@ export interface LiveEvent {
277
304
  working: boolean
278
305
  /** a Jam session runs (`!!view.session`) */
279
306
  session: boolean
307
+ /**
308
+ * spec 32 F2 (0.32.0, optional): since when something has been at work - `marver work`'s CLI leases and the Jam runs
309
+ * the snapshot counts as working - and on which board (the newest working Jam card's, else the first board pinning a
310
+ * `marver work` frame). Absent when nothing works, and from a 0.31 server
311
+ */
312
+ work?: { board?: string; since: number }
313
+ /** spec 32 F2 (0.32.0, optional): the titles of the boards `items` and `work` name, at most `LIVE_TITLES_MAX` keys,
314
+ * each key a ref (at most `LIVE_BOUNDS.ref`) and each value at most `LIVE_BOUNDS.title` */
315
+ titles?: Record<string, string>
280
316
  }
281
317
 
282
318
  /**
@@ -294,6 +330,8 @@ export const LIVE_BOUNDS = {
294
330
  eventBytes: 65_536,
295
331
  id: /^(ask|review|failed|stalled):[0-9a-f]{20}$/,
296
332
  } as const
333
+ /** spec 32 F2: `LiveEvent.titles` has at most this many keys. A constant of its own: `LIVE_BOUNDS` is frozen (A4.6 #11). */
334
+ export const LIVE_TITLES_MAX = 32
297
335
 
298
336
  /**
299
337
  * The live stream's wire (A4.6 #5, #6, #14). `GET <origin><path>` with `x-mv-ws`, a fresh `x-mv-nonce` and
@@ -339,6 +377,32 @@ export type CaptureWhy = (typeof CAPTURE_WHY)[number]
339
377
  * size in device pixels, its long side at most `BOUNDS.captureLong`. */
340
378
  export type CaptureAnswer = { ok: true; png: Uint8Array; width: number; height: number } | { ok: false; why: CaptureWhy }
341
379
 
380
+ /**
381
+ * Spec 32 C2: `mv:immersive` (canvas to main; on the wire `{ doc, p }`) - the shell's wish that immersive play hide the
382
+ * strip, a whole state held and replayed after `menu`. `n` is the preload's, counting from 1 for each document; the
383
+ * shell never sees it. The bridge takes `on` a boolean and `n` an integer 1..2^31, unknown fields stripped.
384
+ */
385
+ export interface ImmersiveWish { on: boolean; n: number }
386
+ /**
387
+ * Spec 32 C2: `mv:immersive-state` (main to canvas; on the wire `{ doc, ...ImmersiveAnswer }`) - main's answer to the
388
+ * wish `n`. `on: true`: the strip is hidden, drop the inset; `on: false`: keep or restore the 44 px inset, Hide UI as it
389
+ * is; `end: true`: main took immersive away for this document (restore the inset, and turn Hide UI off if play is on).
390
+ */
391
+ export interface ImmersiveAnswer { n: number; on: boolean; end?: true }
392
+
393
+ /** A deck PDF job's id (src/server/pdf.ts JOB_ID): 128 random bits, base64url. */
394
+ export const PDF_JOB = /^[A-Za-z0-9_-]{22}$/
395
+ /** The largest PDF main fetches and writes (spec 31: 200 MB). */
396
+ export const PDF_MAX = 200 * 2 ** 20
397
+ /** `mv:export-pdf` (canvas to main, an invoke; on the wire `{ doc, p }`): the finished job to save. */
398
+ export interface ExportPdfAsk { job: string }
399
+ /** Why nothing was saved: `cancelled` - the person closed the save panel; `untrusted` - the document is not the admitted,
400
+ * trusted one (before the panel or after it); `bad-job` - no such finished job on this canvas; `too-large` - past
401
+ * `PDF_MAX`; `failed` - the fetch or the write failed. */
402
+ export const EXPORT_PDF_WHY = ['cancelled', 'untrusted', 'bad-job', 'too-large', 'failed'] as const
403
+ export type ExportPdfWhy = (typeof EXPORT_PDF_WHY)[number]
404
+ export type ExportPdfAnswer = { ok: true } | { ok: false; why: ExportPdfWhy }
405
+
342
406
  /**
343
407
  * `window.marverApp`, exposed by the canvas preload to verified local canvases. Typed methods only: a page never
344
408
  * names a channel. The six methods of 0.1 are required (marver-app.ts checks them); every A3 and A4 one is optional.
@@ -382,8 +446,30 @@ export interface MarverAppV1 {
382
446
  * answers `untrusted` with no IPC; main answers `hidden`, `busy`, `bounds`, `empty`, `timeout` or `stale` otherwise
383
447
  */
384
448
  capture?(a: CaptureAsk): Promise<CaptureAnswer>
449
+ /**
450
+ * spec 31 D1 (app 1.1): main's navigation command for a `marver://` link into this tab - the hash's own targets
451
+ * (shared/deep-link.ts `ShowTarget`), checked by the preload - null for the canvas's own board (a link whose place
452
+ * the app could not take); never a reload or a hash replaced under the page
453
+ */
454
+ onShow?(fn: (t: ShowTarget | null) => void): () => void
455
+ /** spec 31 D3 (app 1.1): this document showed a `marver show` - main brings its tab and the window forward */
456
+ front?(): void
457
+ /**
458
+ * app 1.1 (spec 31): save a finished deck PDF. Only the job id crosses: main fetches the file itself from the admitted
459
+ * canvas origin, shows the save panel, checks the document again, and writes the file where the panel says
460
+ */
461
+ exportPdf?(a: ExportPdfAsk): Promise<ExportPdfAnswer>
462
+ /**
463
+ * spec 32 C2 (app 1.2): the shell's wish for immersive play, sent on every change (the preload stamps it with `n`,
464
+ * holds it and replays it at an admit). What the shell shows follows `onImmersive`'s answer, never the wish
465
+ */
466
+ immersive?(p: { on: boolean }): void
467
+ /** spec 32 C2 (app 1.2): main's answer to the latest wish, for this document alone; an answer to an older wish is dropped */
468
+ onImmersive?(fn: (s: { on: boolean; end?: boolean }) => void): () => void
385
469
  }
386
470
 
471
+ import type { ShowTarget } from './deep-link.ts'
472
+
387
473
  /** Main's words for a reason (the hang sheet, the unsaved pill); `heights` is never named (A2.14 #18). */
388
474
  export const WHY_WORDS: Record<Why, string | null> = {
389
475
  'capture-recording': 'a capture being recorded',
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Where an auto board puts the frames it has not placed yet (friction log #15), one rule for the canvas (store.ts, as it
3
+ * loads a board) and the deck PDF's plan (pdf.ts), so a PDF's reading order is the one Play walks (spec 31 #2).
4
+ *
5
+ * Saved positions are kept. A grouped newcomer lands right of its variant group's rightmost node, on its row; any other
6
+ * goes past the board's right edge, on the top row - never at (0, 0) over a frame already there (the old published
7
+ * everything-board bug). Pure: the caller says each frame's size and builds its node.
8
+ */
9
+ export interface PlacedNode { frame: string; x: number; y: number; w: number }
10
+ export interface NewcomerFrame { id: string; variantGroup?: string }
11
+
12
+ /** Append a node for every frame in `frames` not on `nodes` yet, in `frames`' order. Mutates and returns `nodes`. */
13
+ export function placeNewcomers<N extends PlacedNode, F extends NewcomerFrame>(
14
+ nodes: N[], frames: readonly F[], size: (f: F) => { w: number; h: number }, make: (f: F, x: number, y: number, w: number, h: number) => N,
15
+ ): N[] {
16
+ const placed = new Set(nodes.map((n) => n.frame))
17
+ const groupOf = new Map(frames.map((f) => [f.id, f.variantGroup]))
18
+ let maxX = nodes.reduce((a, n) => Math.max(a, n.x + n.w), 0)
19
+ for (const f of frames) {
20
+ if (placed.has(f.id)) continue
21
+ placed.add(f.id)
22
+ const { w, h } = size(f)
23
+ const sibling = f.variantGroup
24
+ ? nodes.filter((n) => groupOf.get(n.frame) === f.variantGroup).reduce<N | null>((a, n) => (!a || n.x + n.w > a.x + a.w ? n : a), null)
25
+ : null
26
+ const x = sibling ? sibling.x + sibling.w + Math.max(140, sibling.w * 0.12) : nodes.length ? maxX + 96 : 0
27
+ const y = sibling ? sibling.y : 0
28
+ nodes.push(make(f, x, y, w, h))
29
+ maxX = Math.max(maxX, x + w)
30
+ }
31
+ return nodes
32
+ }
@@ -66,6 +66,9 @@ export const readTitle = (v: unknown): string | undefined => {
66
66
  }
67
67
  /** Title Case off a slug - the display when no title is set ("old-stuff" → "Old Stuff"). */
68
68
  export const humanize = (s: string): string => s.replace(/-/g, ' ').replace(/(^|\s)\S/g, (c) => c.toUpperCase())
69
+ /** The canvas's name on screen: a name someone wrote (a capital or a space in it - `name: "iPad Lab"`) stays exactly as
70
+ * written; a slug-like one (a folder's name, `my-repo`) is humanized ("My Repo") (spec 31 G). */
71
+ export const canvasLabel = (name: string): string => (/[A-Z\s]/.test(name) ? name : humanize(name))
69
72
  /** What a board, folder or scene is called on screen: its title, else its humanized slug. */
70
73
  export const labelOf = (name: string, title?: string): string => title ?? humanize(name)
71
74
 
@@ -30,6 +30,10 @@ export const TYPE_ROLES: Record<BoardType, string> = {
30
30
  export const TYPE_GRAMMAR = /^[a-z][a-z-]{0,31}$/
31
31
  /** A type off a file: the word when it is on-grammar, else absent. */
32
32
  export const readType = (v: unknown): string | undefined => (typeof v === 'string' && TYPE_GRAMMAR.test(v) ? v : undefined)
33
+ /** Words read as another type (spec 31): `slides` is a deck - for the type only; whether a board plays as slides stays
34
+ * its publish policy's. */
35
+ export const TYPE_ALIASES: Readonly<Record<string, KnownType>> = { slides: 'deck' }
36
+ const alias = (v: unknown): unknown => (typeof v === 'string' && Object.hasOwn(TYPE_ALIASES, v) ? TYPE_ALIASES[v] : v)
33
37
  /** A type this Marver draws. */
34
38
  export const knownType = (v: unknown): KnownType | undefined =>
35
39
  (BOARD_TYPES as readonly string[]).includes(v as string) ? (v as KnownType) : undefined
@@ -37,8 +41,8 @@ export const knownType = (v: unknown): KnownType | undefined =>
37
41
  /** A board's type: its own when it states one (an unknown word reads as plain - the board spoke
38
42
  * for itself), else its folder's, else that folder's parent's, else plain. */
39
43
  export function resolveType(own: unknown, folder?: unknown, parent?: unknown): BoardType {
40
- if (readType(own)) return knownType(own) ?? 'plain'
41
- return knownType(folder) ?? knownType(parent) ?? 'plain'
44
+ if (readType(own)) return knownType(alias(own)) ?? 'plain'
45
+ return knownType(alias(folder)) ?? knownType(alias(parent)) ?? 'plain'
42
46
  }
43
47
 
44
48
  /** The publish type a board's type proposes when its publish row names none - how a board
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Spec 31 Part D: the one contract for showing the human a place on a canvas - pure, no DOM, no Node, so the CLI,
3
+ * the dev server, the shell and the Mac app read it with one rule.
4
+ *
5
+ * - **A show target** (`ShowTarget`) is the hash's own targets on one board: `n` node keys, `f` frame ids, `s` scenes,
6
+ * `c` one comment thread. Node keys stay node keys (selecting one copy of a frame never selects every copy), and
7
+ * every value is checked against the hash grammar's characters - never a raw hash, never a URL, never script.
8
+ * - **The link hash** a target travels as: `#/b/<board>` and only the `f`, `s`, `n`, `c` query keys, each once, each
9
+ * value of the allowed characters (`targetFromHash` refuses anything else whole; `hashOfTarget` writes it back).
10
+ * - **A deep link** into the Mac app: `marver://open?ws=<folder>&to=<hash>[&port=<p>]`, both values written with
11
+ * `URLSearchParams` (so the hash's own `#`, `?` and `&` are percent-encoded inside `to`), 2 KB at most. `ws` is the
12
+ * canvas's folder (the app realpaths it and binds it to a running canvas); `port` is a hint the app uses only when
13
+ * that port's canvas has the same folder; a `to` that is not the grammar is dropped (the canvas opens on its default
14
+ * board), never the link.
15
+ */
16
+
17
+ export const DEEP_LINK_SCHEME = 'marver'
18
+ /** The longest deep link either side takes. */
19
+ export const DEEP_LINK_MAX = 2048
20
+ /** The most values one key carries. */
21
+ export const TARGET_LIST_MAX = 64
22
+
23
+ export interface ShowTarget {
24
+ board: string
25
+ /** node keys: exactly those nodes */
26
+ n?: string[]
27
+ /** frame ids: every node showing them */
28
+ f?: string[]
29
+ /** scenes: every frame of them */
30
+ s?: string[]
31
+ /** one comment thread, opened */
32
+ c?: string
33
+ }
34
+
35
+ /** The board-name grammar (shared/board-tree.ts BOARD_NAME, bounded) and the Not on a board view. */
36
+ const BOARD = /^[a-z0-9][a-z0-9-]{0,63}$/
37
+ export const UNPLACED_BOARD = '~unplaced'
38
+ const FRAME = /^[\w./-]{1,200}$/
39
+ const SCENE = /^[\w.-]{1,120}$/
40
+ /** a node key is the shell's: `<frame id>@<width>`, or a board file's own (`n-one`) */
41
+ const NODE = /^[\w./@:-]{1,200}$/
42
+ const THREAD = /^[\w-]{1,128}$/
43
+
44
+ const okBoard = (b: unknown): b is string => typeof b === 'string' && (BOARD.test(b) || b === UNPLACED_BOARD)
45
+ const okIn = (re: RegExp) => (v: unknown): v is string => typeof v === 'string' && re.test(v) && !v.includes('..')
46
+ const okFrame = okIn(FRAME), okScene = okIn(SCENE), okNode = okIn(NODE), okThread = okIn(THREAD)
47
+ const KEYS = { n: okNode, f: okFrame, s: okScene } as const
48
+
49
+ /**
50
+ * A show target off the wire (pure): `board` a board name, `n`/`f`/`s` arrays of their grammar's values (at most
51
+ * `TARGET_LIST_MAX`, de-duplicated, empty ones left out), `c` a thread id. Unknown fields are stripped; a known one of
52
+ * the wrong type or any value off its grammar refuses the whole (null).
53
+ */
54
+ export function checkShowTarget(v: unknown): ShowTarget | null {
55
+ if (!v || typeof v !== 'object' || Array.isArray(v)) return null
56
+ const r = v as Record<string, unknown>
57
+ if (!okBoard(r.board)) return null
58
+ const out: ShowTarget = { board: r.board }
59
+ for (const k of ['n', 'f', 's'] as const) {
60
+ if (r[k] === undefined) continue
61
+ const list = r[k]
62
+ if (!Array.isArray(list) || list.length > TARGET_LIST_MAX || !list.every(KEYS[k])) return null
63
+ const uniq = [...new Set(list as string[])]
64
+ if (uniq.length) out[k] = uniq
65
+ }
66
+ if (r.c !== undefined) {
67
+ if (!okThread(r.c)) return null
68
+ out.c = r.c
69
+ }
70
+ return out
71
+ }
72
+
73
+ /**
74
+ * The link hash's target (pure): `#/b/<board>` with only `f`, `s`, `n`, `c`, each at most once, every value of its
75
+ * grammar; anything else - another route, another key, a repeated key, a bad value, more than 2 KB - is null.
76
+ */
77
+ export function targetFromHash(hash: unknown): ShowTarget | null {
78
+ if (typeof hash !== 'string' || hash.length > DEEP_LINK_MAX) return null
79
+ const m = /^#\/b\/([^?#/]+)(?:\?([^#]*))?$/.exec(hash)
80
+ if (!m) return null
81
+ let board: string
82
+ try { board = decodeURIComponent(m[1]!) } catch { return null }
83
+ if (!okBoard(board)) return null
84
+ const raw: Record<string, unknown> = { board }
85
+ if (m[2] !== undefined && m[2] !== '') {
86
+ let params: URLSearchParams
87
+ try { params = new URLSearchParams(m[2]) } catch { return null }
88
+ const seen = new Set<string>()
89
+ for (const [k, v] of params) {
90
+ if (seen.has(k) || !['n', 'f', 's', 'c'].includes(k)) return null
91
+ seen.add(k)
92
+ if (k === 'c') raw.c = v
93
+ else raw[k] = v.split(',').map((x) => x.trim()).filter(Boolean)
94
+ }
95
+ }
96
+ return checkShowTarget(raw)
97
+ }
98
+
99
+ /** The target as the link hash (pure): the board, then `n`, `f`, `s`, `c` - every value already of URL-safe characters. */
100
+ export function hashOfTarget(t: ShowTarget): string {
101
+ const q = [
102
+ t.n?.length ? `n=${t.n.join(',')}` : '',
103
+ t.f?.length ? `f=${t.f.join(',')}` : '',
104
+ t.s?.length ? `s=${t.s.join(',')}` : '',
105
+ t.c ? `c=${t.c}` : '',
106
+ ].filter(Boolean).join('&')
107
+ return `#/b/${t.board}${q ? `?${q}` : ''}`
108
+ }
109
+
110
+ export interface DeepLink {
111
+ /** the canvas's folder, as the link wrote it (absolute; the app realpaths it) */
112
+ ws: string
113
+ /** the place, checked; null when the link named none or one off the grammar (the canvas's default board) */
114
+ to: ShowTarget | null
115
+ /** a hint: used only when that port's canvas has the same folder */
116
+ port?: number
117
+ }
118
+
119
+ const CONTROL = /[\u0000-\u001f\u007f]/
120
+
121
+ /** `marver://open?ws=<folder>&to=<hash>[&port=<p>]`, written with URLSearchParams; null past 2 KB or with a bad folder. */
122
+ export function buildDeepLink(o: { ws: string; to?: string; port?: number }): string | null {
123
+ if (!o.ws.startsWith('/') || CONTROL.test(o.ws)) return null
124
+ const p = new URLSearchParams({ ws: o.ws })
125
+ if (o.to) p.set('to', o.to)
126
+ if (o.port !== undefined && Number.isInteger(o.port) && o.port > 0 && o.port < 65536) p.set('port', String(o.port))
127
+ const link = `${DEEP_LINK_SCHEME}://open?${p}`
128
+ return link.length <= DEEP_LINK_MAX ? link : null
129
+ }
130
+
131
+ /**
132
+ * A deep link as the app hears it (pure): `marver://open` and nothing else on its path, at most 2 KB, only `ws`, `to`
133
+ * and `port` (each once), `ws` an absolute path with no control character (1 KB at most). A `to` off the grammar is
134
+ * dropped to null; a `port` that is not a port is ignored. Anything else is null: nothing opens.
135
+ */
136
+ export function parseDeepLink(raw: unknown): DeepLink | null {
137
+ if (typeof raw !== 'string' || raw.length > DEEP_LINK_MAX) return null
138
+ let u: URL
139
+ try { u = new URL(raw) } catch { return null }
140
+ if (u.protocol !== `${DEEP_LINK_SCHEME}:` || u.host !== 'open' || (u.pathname !== '' && u.pathname !== '/') || u.username || u.password || u.hash) return null
141
+ const seen = new Set<string>()
142
+ for (const k of u.searchParams.keys()) {
143
+ if (seen.has(k) || !['ws', 'to', 'port'].includes(k)) return null
144
+ seen.add(k)
145
+ }
146
+ const ws = u.searchParams.get('ws')
147
+ if (!ws || !ws.startsWith('/') || ws.length > 1024 || CONTROL.test(ws)) return null
148
+ const to = u.searchParams.has('to') ? targetFromHash(u.searchParams.get('to')) : null
149
+ const ps = u.searchParams.get('port')
150
+ const port = ps !== null && /^\d{1,5}$/.test(ps) && Number(ps) > 0 && Number(ps) < 65536 ? Number(ps) : undefined
151
+ return { ws, to, ...(port !== undefined ? { port } : {}) }
152
+ }
153
+
154
+ /** The first `marver://` argument of a command line (a second instance's argv), or null. */
155
+ export function deepLinkArg(argv: readonly unknown[]): string | null {
156
+ for (const a of argv) if (typeof a === 'string' && a.toLowerCase().startsWith(`${DEEP_LINK_SCHEME}://`)) return a
157
+ return null
158
+ }
159
+
160
+ /** Strictly newer x.y.z (pure): anything not three numbers compares false. */
161
+ export function newerVersion(a: unknown, b: unknown): boolean {
162
+ if (typeof a !== 'string' || typeof b !== 'string') return false
163
+ const pa = /^(\d+)\.(\d+)\.(\d+)$/.exec(a), pb = /^(\d+)\.(\d+)\.(\d+)$/.exec(b)
164
+ if (!pa || !pb) return false
165
+ for (let i = 1; i <= 3; i++) {
166
+ const x = Number(pa[i]), y = Number(pb[i])
167
+ if (x !== y) return x > y
168
+ }
169
+ return false
170
+ }
171
+
172
+ // ---- app-live.json: knowing when the app is in use (Part D2) ---------------------------------------------------
173
+
174
+ /** `<MARVER_HOME>/state/app-live.json`: the Mac app's word that it runs, rewritten every `APP_LIVE_EVERY_MS`. */
175
+ export const APP_LIVE_FILE = 'app-live.json'
176
+ export const APP_LIVE_EVERY_MS = 30_000
177
+ /** Older than this, the file is stale: the app is not in use (it quit without removing it, it hangs). */
178
+ export const APP_LIVE_FRESH_MS = 90_000
179
+
180
+ export interface AppLiveFile {
181
+ v: 1
182
+ pid: number
183
+ version: string
184
+ /** 1 when this app takes `marver://` links (an app that registered the scheme); absent otherwise */
185
+ scheme?: 1
186
+ /** when it was written, ms */
187
+ at: number
188
+ /** the real folders of the canvases open in its tabs */
189
+ workspaces: string[]
190
+ }
191
+
192
+ /** The file's shape (pure), or null. */
193
+ export function checkAppLive(v: unknown): AppLiveFile | null {
194
+ const r = v as Partial<AppLiveFile> | null
195
+ if (!r || typeof r !== 'object' || r.v !== 1) return null
196
+ if (!Number.isInteger(r.pid) || (r.pid as number) < 1 || typeof r.version !== 'string' || r.version.length > 32) return null
197
+ if (typeof r.at !== 'number' || !Number.isFinite(r.at)) return null
198
+ if (!Array.isArray(r.workspaces) || r.workspaces.length > 256 || !r.workspaces.every((w) => typeof w === 'string' && w.startsWith('/') && w.length <= 4096)) return null
199
+ return { v: 1, pid: r.pid as number, version: r.version, at: r.at, workspaces: [...r.workspaces], ...(r.scheme === 1 ? { scheme: 1 as const } : {}) }
200
+ }
201
+
202
+ /**
203
+ * Whether `marver link` gives the app's link for the canvas in `root` (pure): the file names a pid that runs, was
204
+ * written under 90 s ago (a stamp from the future is stale too), takes the scheme, and has this canvas's folder open.
205
+ * Anything else - no file, an older app, a stale file, the canvas not open in a tab - is the localhost link.
206
+ */
207
+ export function appTakesLink(file: AppLiveFile | null, root: string, now: number, alive: (pid: number) => boolean): boolean {
208
+ if (!file || file.scheme !== 1) return false
209
+ if (file.at > now + 5_000 || now - file.at > APP_LIVE_FRESH_MS) return false
210
+ if (!file.workspaces.includes(root)) return false
211
+ return alive(file.pid)
212
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Your own breakpoints (spec 31 Part F): what a device shows as - its glyph and its label. Pure, shared by the
3
+ * server (which resolves design/config.ts's `viewports.<name>.icon` and `.label` once, at load) and the shell (which
4
+ * falls back to the same rules for a device name it has no resolved look for, e.g. a Jam step recorded earlier).
5
+ */
6
+
7
+ /** The built-in device glyphs, as `viewports.<name>.icon` may name them (a `-landscape` suffix rotates one). */
8
+ export const DEVICE_GLYPHS = ['mobile', 'tablet', 'laptop', 'monitor', 'tv', 'watch', 'desktop', 'ereader'] as const
9
+ export type DeviceGlyph = typeof DEVICE_GLYPHS[number]
10
+
11
+ /** How a device shows: its label, and a built-in glyph (null: the generic devices glyph) turned a quarter for a
12
+ * landscape device - or `mask`, a sanitized SVG as a `data:image/svg+xml;base64,` URI drawn as a CSS mask in the
13
+ * ink (never inline DOM). */
14
+ export interface DeviceLook { label: string; glyph: DeviceGlyph | null; rotate: boolean; mask?: string }
15
+
16
+ /** The longest label a device carries. */
17
+ export const DEVICE_LABEL_MAX = 40
18
+
19
+ const words = (name: string): string[] => name.split(/[^A-Za-z0-9]+/).filter(Boolean)
20
+ const tokens = (name: string): string[] => words(name).map((w) => w.toLowerCase())
21
+
22
+ /** A `-landscape` device: the name says so (a word of its own). */
23
+ export const isLandscape = (name: string): boolean => tokens(name).includes('landscape')
24
+
25
+ /** The glyph a device's NAME suggests, or null (the generic devices glyph). Class words first (`pixel-tablet` is a
26
+ * tablet), then the brands. */
27
+ export function glyphFromName(name: string): DeviceGlyph | null {
28
+ const t = tokens(name)
29
+ const has = (...w: string[]) => t.some((x) => w.includes(x))
30
+ const starts = (p: string) => t.some((x) => x.startsWith(p))
31
+ // the class words: what a device IS beats whose it is
32
+ if (has('tablet')) return 'tablet'
33
+ if (has('laptop')) return 'laptop'
34
+ if (has('phone', 'mobile', 'smartphone')) return 'mobile'
35
+ if (has('monitor', 'desktop')) return 'monitor'
36
+ if (has('tv')) return 'tv'
37
+ if (has('watch')) return 'watch'
38
+ if (has('ereader')) return 'ereader'
39
+ // the brands and the models
40
+ if (starts('iphone') || has('android', 'pixel')) return 'mobile'
41
+ if (starts('ipad') || has('boox', 'kindle')) return 'tablet'
42
+ if (starts('macbook')) return 'laptop'
43
+ if (has('imac', 'studio')) return 'monitor'
44
+ return null
45
+ }
46
+
47
+ /** Brand casing, by word: "ipad" -> "iPad", "tv" -> "TV". */
48
+ const BRAND: Record<string, string> = {
49
+ iphone: 'iPhone', ipad: 'iPad', imac: 'iMac', ipod: 'iPod', macbook: 'MacBook', mac: 'Mac', ios: 'iOS', ipados: 'iPadOS',
50
+ macos: 'macOS', watchos: 'watchOS', tvos: 'tvOS', visionos: 'visionOS', tv: 'TV', appletv: 'Apple TV', android: 'Android', pixel: 'Pixel',
51
+ galaxy: 'Galaxy', samsung: 'Samsung', kindle: 'Kindle', boox: 'Boox', kobo: 'Kobo', remarkable: 'reMarkable', surface: 'Surface',
52
+ chromebook: 'Chromebook', apple: 'Apple', google: 'Google', oneplus: 'OnePlus', xl: 'XL', hd: 'HD', uhd: 'UHD', fhd: 'FHD',
53
+ qhd: 'QHD', '4k': '4K', '5k': '5K', '6k': '6K', '8k': '8K', xdr: 'XDR', oled: 'OLED', se: 'SE', xr: 'XR', xs: 'XS', ui: 'UI',
54
+ }
55
+ /** A model's suffix words, capitalised after a brand: "iphone-15-pro-max" -> "iPhone 15 Pro Max". */
56
+ const MODEL = new Set(['pro', 'max', 'plus', 'mini', 'air', 'ultra', 'fold', 'flip', 'lite', 'studio', 'display', 'note'])
57
+ /** Brands that start a model name, so the words after them are its model words. */
58
+ const BRANDED = new Set(['iphone', 'ipad', 'imac', 'ipod', 'macbook', 'mac', 'pixel', 'galaxy', 'kindle', 'boox', 'kobo', 'remarkable', 'surface', 'chromebook', 'apple', 'google', 'samsung', 'oneplus', 'studio'])
59
+
60
+ /** A device's label from its NAME: the words humanized, brand casing kept ("ipad-landscape" -> "iPad landscape",
61
+ * "boox-full" -> "Boox full", "iphone-15-pro" -> "iPhone 15 Pro"). The first word starts capitalised, the rest stay
62
+ * as written; a word already carrying capitals keeps them ("myDevice" stays "myDevice"). */
63
+ export function labelFromName(name: string): string {
64
+ const ws = words(name)
65
+ if (!ws.length) return name
66
+ let branded = false
67
+ return ws.map((w, i) => {
68
+ const low = w.toLowerCase()
69
+ // a brand word with a model number glued on: "iphone15" -> "iPhone15", "ipad13" -> "iPad13"
70
+ const glued = /^([a-z]+)(\d+)$/.exec(low)
71
+ const brand = BRAND[low] ?? (glued && BRANDED.has(glued[1]) && BRAND[glued[1]] ? BRAND[glued[1]] + glued[2] : undefined)
72
+ const isBrand = BRANDED.has(low) || (!!glued && BRANDED.has(glued[1]))
73
+ const out = brand ?? (/[A-Z]/.test(w) ? w
74
+ : branded && MODEL.has(low) ? low[0].toUpperCase() + low.slice(1)
75
+ : i === 0 ? low[0].toUpperCase() + low.slice(1)
76
+ : low)
77
+ if (isBrand) branded = true
78
+ return out
79
+ }).join(' ')
80
+ }
81
+
82
+ /** A built-in glyph as `viewports.<name>.icon` names it, with its rotation - or null when the value is no glyph. */
83
+ export function parseGlyph(v: string): { glyph: DeviceGlyph; rotate: boolean } | null {
84
+ const m = /^([a-z]+)(-landscape)?$/.exec(v)
85
+ return m && (DEVICE_GLYPHS as readonly string[]).includes(m[1]) ? { glyph: m[1] as DeviceGlyph, rotate: !!m[2] } : null
86
+ }
87
+
88
+ /** A device's look from its name alone - the shell's fallback, and the server's for a device with no `icon`/`label`. */
89
+ export const lookFromName = (name: string): DeviceLook => ({ label: labelFromName(name), glyph: glyphFromName(name), rotate: isLandscape(name) })
@@ -5,7 +5,8 @@
5
5
  */
6
6
 
7
7
  // 'reanchor' (Live Jam): re-pin a thread to a new element after the agent moved it.
8
- export type EventType = 'create' | 'reply' | 'edit' | 'resolve' | 'reopen' | 'react' | 'profile' | 'reanchor'
8
+ // 'delete' (0.31, spec 31 C.4): a terminal tombstone on one message - its author's only.
9
+ export type EventType = 'create' | 'reply' | 'edit' | 'resolve' | 'reopen' | 'react' | 'profile' | 'reanchor' | 'delete'
9
10
 
10
11
  // Live Jam: provenance stamped by the daemon on agent-authored events (who orchestrated the change).
11
12
  export interface AgentMeta { devUser?: string; harness?: string; model?: string; effort?: string; /** who ran it (spec 29b): the owner's session, a copy of it, or a fresh agent */ runner?: 'session' | 'fork' | 'fresh' }
@@ -49,6 +50,8 @@ export interface CommentEvent {
49
50
  capture?: string
50
51
  facts?: string
51
52
  origin?: string // server-stamped 'local' on dev-owner writes; the daemon's owner-trigger key
53
+ /** resolve and reopen events (0.31, spec 31 B.3): the resolve-all operation that wrote it - what its Undo matches */
54
+ op?: string
52
55
  }
53
56
 
54
57
  export interface Thread {
@@ -66,7 +69,11 @@ export interface Thread {
66
69
  asks?: boolean
67
70
  capture?: string // a capture's comment (0.26)
68
71
  facts?: string
69
- replies: { id: string; author?: CommentEvent['author']; body?: string; ts: number; agent?: boolean; agentMeta?: AgentMeta; mentions?: Mention[]; asks?: boolean; alsoChanged?: string[] }[]
72
+ /** the first comment was deleted (0.31): its replies stay under a "Comment deleted" placeholder - no body, author or mentions */
73
+ deleted?: true
74
+ /** when its words were last edited (0.31: "edited" is shown) - an edit that changes only a flag does not count */
75
+ edited?: number
76
+ replies: { id: string; author?: CommentEvent['author']; body?: string; ts: number; agent?: boolean; agentMeta?: AgentMeta; mentions?: Mention[]; asks?: boolean; alsoChanged?: string[]; edited?: number }[]
70
77
  reactions: Record<string, string[]> // emoji -> author emails (toggle semantics)
71
78
  }
72
79
 
@@ -82,10 +89,31 @@ export function diffEvents(mine: CommentEvent[], theirIds: Iterable<string>): Co
82
89
  return mine.filter((e) => !have.has(e.id))
83
90
  }
84
91
 
92
+ /** The messages (create or reply comment ids) a log's tombstones name (spec 31 C.4): terminal, whatever came
93
+ * before or after them. Every reader that turns a request into agent input filters by it, per message. */
94
+ export function deletedMessages(events: readonly CommentEvent[]): Set<string> {
95
+ const out = new Set<string>()
96
+ for (const e of events) if (e.type === 'delete' && typeof e.commentId === 'string' && e.commentId) out.add(e.commentId)
97
+ return out
98
+ }
99
+
100
+ /** An event as a browser may see it (spec 31 C.4): a create, reply or edit of a deleted message carries no words or
101
+ * mentions - the log keeps them (sync is by id), no transport to a page does. `gone`: deletedMessages of the log. */
102
+ export const withoutDeletedWords = (e: CommentEvent, gone: ReadonlySet<string>): CommentEvent =>
103
+ gone.size && (e.type === 'create' || e.type === 'reply' || e.type === 'edit') && !!e.commentId && gone.has(e.commentId) ? { ...e, body: '', mentions: undefined } : e
104
+
105
+ /** The copy a published page keeps of what it just sent (spec 31 C.2): its author wears the viewer's own opaque id, as
106
+ * the server's projection of it will - the echo is a duplicate by id and dropped, so without it a message just written
107
+ * would not be "mine" (no Edit, no Delete) until a reload. Never sent: the server assigns ids. */
108
+ export const asSentBy = (events: CommentEvent[], myId: string | undefined): CommentEvent[] =>
109
+ !myId ? events : events.map((e) => (e.author && !e.author.id ? { ...e, author: { ...e.author, id: myId } } : e))
110
+
85
111
  /** Replay a board's events into current thread state. Deterministic: replay order is
86
112
  * (ts, id) so two stores holding the same event SET always derive the same state.
87
113
  * TWO passes - creates first, then everything else - so a reply whose author's clock
88
- * ran ahead of the creator's still lands instead of being dropped forever. */
114
+ * ran ahead of the creator's still lands instead of being dropped forever. Deletes (0.31)
115
+ * apply after both, whatever their time: a deleted reply is gone; a deleted first comment
116
+ * keeps its surviving replies under a placeholder, and with none the thread is gone. */
89
117
  export function replay(events: CommentEvent[]): Thread[] {
90
118
  const ordered = [...events].sort((a, b) => a.ts - b.ts || (a.id < b.id ? -1 : 1))
91
119
  const threads = new Map<string, Thread>()
@@ -112,7 +140,7 @@ export function replay(events: CommentEvent[]): Thread[] {
112
140
  if (!ev.commentId || (ev.body === undefined && ev.asks === undefined && ev.alsoChanged === undefined)) break
113
141
  const m = threads.get(ev.commentId) ?? [...threads.values()].flatMap((th) => th.replies).find((r) => r.id === ev.commentId)
114
142
  if (!m) break
115
- if (ev.body !== undefined) m.body = ev.body
143
+ if (ev.body !== undefined) { if (ev.body !== m.body) m.edited = ev.ts; m.body = ev.body }
116
144
  if (ev.asks !== undefined) m.asks = ev.asks
117
145
  if (ev.alsoChanged !== undefined && !('replies' in m)) (m as { alsoChanged?: string[] }).alsoChanged = ev.alsoChanged // a reply's
118
146
  break
@@ -152,7 +180,22 @@ export function replay(events: CommentEvent[]): Thread[] {
152
180
  break
153
181
  }
154
182
  // 'profile' events update author snapshots at write time on the client - replay ignores them
183
+ // 'delete' events are applied below, after everything else
184
+ }
185
+ }
186
+ const gone = deletedMessages(ordered)
187
+ if (!gone.size) return [...threads.values()]
188
+ const out: Thread[] = []
189
+ for (const t of threads.values()) {
190
+ t.replies = t.replies.filter((r) => !gone.has(r.id))
191
+ if (gone.has(t.id)) {
192
+ if (!t.replies.length) continue
193
+ // the placeholder carries nothing of what was said, nor who said it
194
+ t.deleted = true
195
+ t.body = undefined; t.author = undefined; t.mentions = undefined; t.asks = undefined; t.edited = undefined
196
+ delete t.capture; delete t.facts
155
197
  }
198
+ out.push(t)
156
199
  }
157
- return [...threads.values()]
200
+ return out
158
201
  }