@tldraw/commenting 0.0.0-bootstrap → 5.3.0-internal.c5c7f1d817d0

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 (125) hide show
  1. package/commenting.css +131 -131
  2. package/dist-cjs/canvas/comment-body.js +1 -1
  3. package/dist-cjs/canvas/comment-body.js.map +2 -2
  4. package/dist-cjs/canvas/comments-filter-menu.js +1 -1
  5. package/dist-cjs/canvas/comments-filter-menu.js.map +1 -1
  6. package/dist-cjs/canvas/comments-overflow-menu.js +3 -3
  7. package/dist-cjs/canvas/comments-overflow-menu.js.map +2 -2
  8. package/dist-cjs/canvas/comments-overlay.js +65 -50
  9. package/dist-cjs/canvas/comments-overlay.js.map +2 -2
  10. package/dist-cjs/canvas/comments-sidebar.js +2 -2
  11. package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
  12. package/dist-cjs/clustering/runtime.js +119 -7
  13. package/dist-cjs/clustering/runtime.js.map +2 -2
  14. package/dist-cjs/index.d.ts +28 -8
  15. package/dist-cjs/index.js +1 -1
  16. package/dist-cjs/ui/avatar.js +10 -2
  17. package/dist-cjs/ui/avatar.js.map +2 -2
  18. package/dist-cjs/ui/byline.js +4 -4
  19. package/dist-cjs/ui/byline.js.map +2 -2
  20. package/dist-cjs/ui/comment-card.js +3 -3
  21. package/dist-cjs/ui/comment-card.js.map +2 -2
  22. package/dist-cjs/ui/comment-composer.js +30 -24
  23. package/dist-cjs/ui/comment-composer.js.map +2 -2
  24. package/dist-cjs/ui/comment-mention.js +1 -1
  25. package/dist-cjs/ui/comment-mention.js.map +2 -2
  26. package/dist-cjs/ui/comment-pin.js +5 -1
  27. package/dist-cjs/ui/comment-pin.js.map +2 -2
  28. package/dist-cjs/ui/comment-text.js +1 -1
  29. package/dist-cjs/ui/comment-text.js.map +2 -2
  30. package/dist-cjs/ui/comment-thread.js +6 -6
  31. package/dist-cjs/ui/comment-thread.js.map +2 -2
  32. package/dist-cjs/ui/comments-list.js +11 -11
  33. package/dist-cjs/ui/comments-list.js.map +2 -2
  34. package/dist-cjs/ui/count-badge.js +1 -1
  35. package/dist-cjs/ui/count-badge.js.map +2 -2
  36. package/dist-cjs/ui/empty-state.js +3 -3
  37. package/dist-cjs/ui/empty-state.js.map +2 -2
  38. package/dist-cjs/ui/mention-list.js +7 -7
  39. package/dist-cjs/ui/mention-list.js.map +2 -2
  40. package/dist-cjs/ui/mention-suggestion.js +3 -3
  41. package/dist-cjs/ui/mention-suggestion.js.map +2 -2
  42. package/dist-cjs/ui/mention.js +1 -1
  43. package/dist-cjs/ui/mention.js.map +2 -2
  44. package/dist-cjs/ui/reaction.js +11 -4
  45. package/dist-cjs/ui/reaction.js.map +2 -2
  46. package/dist-cjs/ui/reactions.js +10 -2
  47. package/dist-cjs/ui/reactions.js.map +2 -2
  48. package/dist-cjs/ui/send-button.js +1 -1
  49. package/dist-cjs/ui/send-button.js.map +2 -2
  50. package/dist-esm/canvas/comment-body.mjs +1 -1
  51. package/dist-esm/canvas/comment-body.mjs.map +2 -2
  52. package/dist-esm/canvas/comments-filter-menu.mjs +1 -1
  53. package/dist-esm/canvas/comments-filter-menu.mjs.map +1 -1
  54. package/dist-esm/canvas/comments-overflow-menu.mjs +3 -3
  55. package/dist-esm/canvas/comments-overflow-menu.mjs.map +2 -2
  56. package/dist-esm/canvas/comments-overlay.mjs +65 -50
  57. package/dist-esm/canvas/comments-overlay.mjs.map +2 -2
  58. package/dist-esm/canvas/comments-sidebar.mjs +2 -2
  59. package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
  60. package/dist-esm/clustering/runtime.mjs +119 -7
  61. package/dist-esm/clustering/runtime.mjs.map +2 -2
  62. package/dist-esm/index.d.mts +28 -8
  63. package/dist-esm/index.mjs +1 -1
  64. package/dist-esm/ui/avatar.mjs +10 -2
  65. package/dist-esm/ui/avatar.mjs.map +2 -2
  66. package/dist-esm/ui/byline.mjs +4 -4
  67. package/dist-esm/ui/byline.mjs.map +2 -2
  68. package/dist-esm/ui/comment-card.mjs +3 -3
  69. package/dist-esm/ui/comment-card.mjs.map +2 -2
  70. package/dist-esm/ui/comment-composer.mjs +30 -24
  71. package/dist-esm/ui/comment-composer.mjs.map +2 -2
  72. package/dist-esm/ui/comment-mention.mjs +1 -1
  73. package/dist-esm/ui/comment-mention.mjs.map +2 -2
  74. package/dist-esm/ui/comment-pin.mjs +5 -1
  75. package/dist-esm/ui/comment-pin.mjs.map +2 -2
  76. package/dist-esm/ui/comment-text.mjs +1 -1
  77. package/dist-esm/ui/comment-text.mjs.map +2 -2
  78. package/dist-esm/ui/comment-thread.mjs +6 -6
  79. package/dist-esm/ui/comment-thread.mjs.map +2 -2
  80. package/dist-esm/ui/comments-list.mjs +11 -11
  81. package/dist-esm/ui/comments-list.mjs.map +2 -2
  82. package/dist-esm/ui/count-badge.mjs +1 -1
  83. package/dist-esm/ui/count-badge.mjs.map +2 -2
  84. package/dist-esm/ui/empty-state.mjs +3 -3
  85. package/dist-esm/ui/empty-state.mjs.map +2 -2
  86. package/dist-esm/ui/mention-list.mjs +7 -7
  87. package/dist-esm/ui/mention-list.mjs.map +2 -2
  88. package/dist-esm/ui/mention-suggestion.mjs +3 -3
  89. package/dist-esm/ui/mention-suggestion.mjs.map +2 -2
  90. package/dist-esm/ui/mention.mjs +1 -1
  91. package/dist-esm/ui/mention.mjs.map +2 -2
  92. package/dist-esm/ui/reaction.mjs +11 -4
  93. package/dist-esm/ui/reaction.mjs.map +2 -2
  94. package/dist-esm/ui/reactions.mjs +10 -2
  95. package/dist-esm/ui/reactions.mjs.map +2 -2
  96. package/dist-esm/ui/send-button.mjs +1 -1
  97. package/dist-esm/ui/send-button.mjs.map +2 -2
  98. package/package.json +3 -3
  99. package/src/canvas/canvas.css +25 -25
  100. package/src/canvas/comment-body.tsx +1 -1
  101. package/src/canvas/comment-render.test.ts +1 -1
  102. package/src/canvas/comments-filter-menu.tsx +1 -1
  103. package/src/canvas/comments-overflow-menu.tsx +3 -3
  104. package/src/canvas/comments-overlay.tsx +109 -76
  105. package/src/canvas/comments-sidebar.tsx +2 -2
  106. package/src/clustering/runtime.test.ts +183 -7
  107. package/src/clustering/runtime.ts +164 -15
  108. package/src/ui/avatar.tsx +9 -2
  109. package/src/ui/byline.tsx +4 -4
  110. package/src/ui/comment-card.tsx +3 -3
  111. package/src/ui/comment-composer.tsx +49 -30
  112. package/src/ui/comment-mention.ts +2 -2
  113. package/src/ui/comment-pin.tsx +5 -1
  114. package/src/ui/comment-text.tsx +1 -1
  115. package/src/ui/comment-thread.tsx +8 -6
  116. package/src/ui/comments-list.tsx +16 -12
  117. package/src/ui/comments.css +106 -106
  118. package/src/ui/count-badge.tsx +1 -1
  119. package/src/ui/empty-state.tsx +3 -3
  120. package/src/ui/mention-list.tsx +8 -8
  121. package/src/ui/mention-suggestion.tsx +3 -3
  122. package/src/ui/mention.tsx +1 -1
  123. package/src/ui/reaction.tsx +6 -3
  124. package/src/ui/reactions.tsx +6 -2
  125. package/src/ui/send-button.tsx +1 -1
@@ -460,6 +460,7 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
460
460
  ])
461
461
  )
462
462
  expect(rt.k).toBe(1)
463
+ expect(rt.getSuppressedCount()).toBe(0)
463
464
  expect(visibleIds(rt)).toEqual(['cluster:3:a', 'd'])
464
465
  })
465
466
 
@@ -477,7 +478,8 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
477
478
  [D.id, D],
478
479
  ])
479
480
  )
480
- expect(rt.k).toBe(0)
481
+ expect(rt.k).toBe(1)
482
+ expect(rt.getSuppressedCount()).toBe(1)
481
483
  expect(visibleIds(rt)).toEqual(['a', 'b', 'c', 'd'])
482
484
  })
483
485
 
@@ -512,7 +514,9 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
512
514
  const table = microTraceTable()
513
515
  const rt = createClusterRuntime(table)
514
516
  rt.seedFrom(5, new Map())
515
- expect(rt.k).toBe(0)
517
+ expect(rt.k).toBe(1)
518
+ expect(rt.getSuppressedCount()).toBe(1)
519
+ expect(visibleIds(rt)).toEqual(['a', 'b', 'c', 'd'])
516
520
  const partial = node(['a', 'b'], 5, 0)
517
521
  rt.seedFrom(
518
522
  5,
@@ -522,7 +526,9 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
522
526
  [D.id, D],
523
527
  ])
524
528
  )
525
- expect(rt.k).toBe(0)
529
+ expect(rt.k).toBe(1)
530
+ expect(rt.getSuppressedCount()).toBe(1)
531
+ expect(visibleIds(rt)).toEqual(['a', 'b', 'c', 'd'])
526
532
  })
527
533
 
528
534
  it('membership in a superset cluster counts as merged', () => {
@@ -534,10 +540,58 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
534
540
  expect(visibleIds(rt)).toEqual(['cluster:4:a'])
535
541
  })
536
542
 
537
- it('classification stops at the first inactive event (prefix cut is conservative)', () => {
538
- // Two independent band events at zoom 4.5. The first is split in history, the
539
- // second merged but active events must be a prefix, so the second resolves
540
- // to split (conservative: never merges anything history did not sanction).
543
+ it('carries over mixed band states exactly (split before merged in table order)', () => {
544
+ // Two independent band events at zoom 4.5: the earlier-sorted one split in history,
545
+ // the later one merged. The old prefix cut forced the second to split (the mass-split
546
+ // bug); with suppression the carryover is exact each keeps its own state.
547
+ const AB = node(['a', 'b'], 5, 0)
548
+ const CD = node(['c', 'd'], 105, 0)
549
+ const table: ClusterTable = {
550
+ events: [mev(4, 6, [A, B], AB), mev(3, 5, [C, D], CD)],
551
+ leaves: [A, B, C, D],
552
+ }
553
+ const rt = createClusterRuntime(table)
554
+ rt.seedFrom(
555
+ 4.5,
556
+ new Map([
557
+ [A.id, A],
558
+ [B.id, B],
559
+ [CD.id, CD],
560
+ ])
561
+ )
562
+ expect(rt.k).toBe(2)
563
+ expect(rt.getSuppressedCount()).toBe(1)
564
+ expect(visibleIds(rt)).toEqual(['a', 'b', 'cluster:2:c'])
565
+ })
566
+
567
+ it('a suppressed event merges (heals) when a zoom-out crosses its own zMerge', () => {
568
+ const AB = node(['a', 'b'], 5, 0)
569
+ const CD = node(['c', 'd'], 105, 0)
570
+ const table: ClusterTable = {
571
+ events: [mev(4, 6, [A, B], AB), mev(3, 5, [C, D], CD)],
572
+ leaves: [A, B, C, D],
573
+ }
574
+ const rt = createClusterRuntime(table)
575
+ rt.seedFrom(
576
+ 4.5,
577
+ new Map([
578
+ [A.id, A],
579
+ [B.id, B],
580
+ [CD.id, CD],
581
+ ])
582
+ )
583
+ const versionBefore = rt.version
584
+ rt.onCamera(4.4) // still inside AB's band: nothing happens
585
+ expect(rt.version).toBe(versionBefore)
586
+ rt.onCamera(3.9) // below AB's zMerge (4): the held-out merge fires at its own threshold
587
+ expect(rt.version).toBeGreaterThan(versionBefore)
588
+ expect(rt.k).toBe(2)
589
+ expect(rt.getSuppressedCount()).toBe(0)
590
+ expect(visibleIds(rt)).toEqual(['cluster:2:a', 'cluster:2:c'])
591
+ expectPostconditions(rt, table, 3.9)
592
+ })
593
+
594
+ it('the split walk retreats past a suppressed event without corrupting the partition', () => {
541
595
  const AB = node(['a', 'b'], 5, 0)
542
596
  const CD = node(['c', 'd'], 105, 0)
543
597
  const table: ClusterTable = {
@@ -553,8 +607,18 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
553
607
  [CD.id, CD],
554
608
  ])
555
609
  )
610
+ rt.onCamera(5.5) // past CD's zSplit (5): CD splits; suppressed AB stays put
611
+ expect(rt.k).toBe(1)
612
+ expect(rt.getSuppressedCount()).toBe(1)
613
+ expect(visibleIds(rt)).toEqual(['a', 'b', 'c', 'd'])
614
+ rt.onCamera(6.5) // past AB's zSplit (6): retreat past the suppressed (never-applied) event
556
615
  expect(rt.k).toBe(0)
616
+ expect(rt.getSuppressedCount()).toBe(0)
557
617
  expect(visibleIds(rt)).toEqual(['a', 'b', 'c', 'd'])
618
+ rt.onCamera(3.9) // zoom back out: normal cursor walk from a clean state
619
+ expect(rt.k).toBe(1)
620
+ expect(visibleIds(rt)).toEqual(['c', 'cluster:2:a', 'd'])
621
+ expectPostconditions(rt, table, 3.9)
558
622
  })
559
623
 
560
624
  it('seedFrom followed by onCamera at the same zoom is a no-op', () => {
@@ -594,3 +658,115 @@ describe('createClusterRuntime seedFrom (carryover seeding)', () => {
594
658
  expect(() => rt.seedFrom(NaN, new Map())).toThrow()
595
659
  })
596
660
  })
661
+
662
+ describe('createClusterRuntime detachLeaf (local partition edits)', () => {
663
+ it('shrinks the containing badge in place: members, count, centroid', () => {
664
+ const table = microTraceTable()
665
+ const rt = createClusterRuntime(table)
666
+ rt.seed(4) // P = a+b+c visible, d separate
667
+ const versionBefore = rt.version
668
+ rt.detachLeaf('a')
669
+ expect(rt.version).toBeGreaterThan(versionBefore)
670
+ expect(rt.getDetachedCount()).toBe(1)
671
+ const patchedP = rt.getVisible().get(P.id)!
672
+ expect(patchedP.members).toEqual(['b', 'c'])
673
+ expect(patchedP.count).toBe(2)
674
+ // centroid recomputed from the remaining leaves: B (10,0), C (20,0)
675
+ expect(patchedP.centroid).toEqual({ x: 15, y: 0 })
676
+ // keeps the structural id so cursor events keep addressing it
677
+ expect(patchedP.id).toBe(P.id)
678
+ // everything else untouched
679
+ expect(rt.getVisible().get('d')).toEqual(D)
680
+ expect(rt.getVisible().size).toBe(2)
681
+ })
682
+
683
+ it('is idempotent and ignores unknown ids', () => {
684
+ const table = microTraceTable()
685
+ const rt = createClusterRuntime(table)
686
+ rt.seed(4)
687
+ rt.detachLeaf('a')
688
+ const version = rt.version
689
+ rt.detachLeaf('a')
690
+ rt.detachLeaf('nonexistent')
691
+ expect(rt.version).toBe(version)
692
+ expect(rt.getDetachedCount()).toBe(1)
693
+ })
694
+
695
+ it('collapses a badge to its surviving leaf, and to nothing', () => {
696
+ const table = microTraceTable()
697
+ const rt = createClusterRuntime(table)
698
+ rt.seed(4)
699
+ rt.detachLeaf('a')
700
+ rt.detachLeaf('b')
701
+ // P = {a,b,c} minus a,b → the leaf node c, keyed by its own id
702
+ expect(rt.getVisible().get('c')).toEqual(C)
703
+ expect(rt.getVisible().has(P.id)).toBe(false)
704
+ rt.detachLeaf('c')
705
+ expect(visibleIds(rt)).toEqual(['d'])
706
+ })
707
+
708
+ it('removes a leaf that is visible as its own pin', () => {
709
+ const table = microTraceTable()
710
+ const rt = createClusterRuntime(table)
711
+ rt.seed(8) // everything split
712
+ rt.detachLeaf('a')
713
+ expect(visibleIds(rt)).toEqual(['b', 'c', 'd'])
714
+ })
715
+
716
+ it('zoom walks stay correct while patches are active (split)', () => {
717
+ const table = microTraceTable()
718
+ const rt = createClusterRuntime(table)
719
+ rt.seed(4)
720
+ rt.detachLeaf('a')
721
+ rt.onCamera(6.5) // past P's zSplit (6): structural split of a+b+c
722
+ expect(rt.k).toBe(0)
723
+ // resolved view: a stays gone, b/c/d as pins — the split itself changed nothing for a
724
+ expect(visibleIds(rt)).toEqual(['b', 'c', 'd'])
725
+ })
726
+
727
+ it('zoom walks stay correct while patches are active (merge)', () => {
728
+ const table = microTraceTable()
729
+ const rt = createClusterRuntime(table)
730
+ rt.seed(4)
731
+ rt.detachLeaf('a')
732
+ rt.onCamera(0.9) // below E1's zMerge (1): P+D merge into Q
733
+ expect(rt.k).toBe(2)
734
+ const patchedQ = rt.getVisible().get(Q.id)!
735
+ expect(patchedQ.members).toEqual(['b', 'c', 'd'])
736
+ expect(patchedQ.count).toBe(3)
737
+ // mean of B (10,0), C (20,0), D (100,0)
738
+ expect(patchedQ.centroid.x).toBeCloseTo(130 / 3)
739
+ expect(rt.getVisible().size).toBe(1)
740
+ })
741
+
742
+ it('seed and seedFrom clear detaches', () => {
743
+ const table = microTraceTable()
744
+ const rt = createClusterRuntime(table)
745
+ rt.seed(4)
746
+ rt.detachLeaf('a')
747
+ rt.seed(4)
748
+ expect(rt.getDetachedCount()).toBe(0)
749
+ expect(rt.getVisible().get(P.id)).toEqual(P)
750
+ rt.detachLeaf('a')
751
+ rt.seedFrom(
752
+ 4,
753
+ new Map([
754
+ [P.id, P],
755
+ [D.id, D],
756
+ ])
757
+ )
758
+ expect(rt.getDetachedCount()).toBe(0)
759
+ expect(rt.getVisible().get(P.id)).toEqual(P)
760
+ })
761
+
762
+ it('getVisible returns a stable reference until the partition changes', () => {
763
+ const table = microTraceTable()
764
+ const rt = createClusterRuntime(table)
765
+ rt.seed(4)
766
+ rt.detachLeaf('a')
767
+ const first = rt.getVisible()
768
+ expect(rt.getVisible()).toBe(first)
769
+ rt.detachLeaf('b')
770
+ expect(rt.getVisible()).not.toBe(first)
771
+ })
772
+ })
@@ -2,23 +2,43 @@ import type { ClusterNode, ClusterTable, MergeEvent } from './types'
2
2
 
3
3
  /** @public */
4
4
  export interface ClusterRuntime {
5
- /** Events[0..k) are active. Exposed for tests and debugging. */
5
+ /** Events[0..k) are in the cursor (applied unless suppressed). Exposed for tests/debugging. */
6
6
  readonly k: number
7
- /** Reset state from scratch for the given zoom (cold start / after rebuild). */
7
+ /**
8
+ * Increments whenever the displayed partition changes. Subscribe to this, not `k`: detaches
9
+ * and suppressed-event healing change the partition without moving the cursor.
10
+ */
11
+ readonly version: number
12
+ /** Number of suppressed band events currently inside the cursor. Exposed for tests. */
13
+ getSuppressedCount(): number
14
+ /** Number of leaves currently detached from the displayed partition. Exposed for tests. */
15
+ getDetachedCount(): number
16
+ /** Reset state from scratch for the given zoom (cold start / after rebuild). Clears detaches. */
8
17
  seed(zoom: number): void
9
18
  /**
10
19
  * Reset state for the given zoom, carrying hysteresis state over from a previous partition
11
20
  * (the visible map of the model being replaced). Threshold-forced events ignore history:
12
- * zoom \<= zMerge is always active, zoom \>= zSplit always inactive. An event inside its band
13
- * is active iff its members were merged together in `previous`; a band event with no history
14
- * (e.g. introduced by the rebuild) stays inactive until the next zoom-out crosses its zMerge.
15
- * Active events must form a prefix of the table, so classification stops at the first
16
- * inactive event; later band-active events conservatively resolve to split.
21
+ * zoom \<= zMerge is always merged, zoom \>= zSplit always split. An event inside its band
22
+ * keeps its previous state: merged iff its members were merged together in `previous`; a band
23
+ * event that was unmerged (or has no history, e.g. introduced by the rebuild) stays unmerged
24
+ * inside the cursor but suppressed until a zoom-out crosses its own zMerge, exactly like any
25
+ * other pending merge. Carryover is exact: no group changes state because of the swap alone.
26
+ * Clears detaches.
17
27
  */
18
28
  seedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void
19
29
  /** Advance/retreat the cursor for a camera change. No-op if zoom sits inside all bands. */
20
30
  onCamera(zoom: number): void
21
- /** The current partition: cluster id → node. Do not mutate. */
31
+ /**
32
+ * Remove one leaf from the displayed partition without touching the event table. Local by
33
+ * construction: only the nodes containing the leaf change — a badge shrinks in place (count
34
+ * and centroid recomputed from its remaining members), a pair collapses to its surviving
35
+ * leaf, and the leaf on its own disappears. Everything else is untouched, so a deletion,
36
+ * pop-out, or thread-open never re-flows the rest of the document. The table's thresholds
37
+ * around the detached leaf go stale; the caller is expected to hold a corrected rebuild and
38
+ * adopt it (with seedFrom) at the next zoom-out. Unknown or already-detached ids are no-ops.
39
+ */
40
+ detachLeaf(leafId: string): void
41
+ /** The displayed partition: cluster id → node, with detaches applied. Do not mutate. */
22
42
  getVisible(): ReadonlyMap<string, ClusterNode>
23
43
  }
24
44
 
@@ -30,21 +50,49 @@ export function createClusterRuntime(table: ClusterTable): ClusterRuntime {
30
50
  class ClusterRuntimeImpl implements ClusterRuntime {
31
51
  // mutable internally; readonly through the ClusterRuntime interface
32
52
  k = 0
53
+ version = 0
54
+ // Structural partition: exactly leaves + applied events, never patched. The cursor invariants
55
+ // live here, untouched by detaches — resolution to the displayed partition happens at read
56
+ // time in getVisible().
33
57
  private visible = new Map<string, ClusterNode>()
34
58
  private seeded = false
59
+ // Indices (< k) of band events held unmerged by seedFrom carryover. The single cursor can
60
+ // only express "merged up to here", but a carried-over partition can be "merged except these"
61
+ // — the exceptions live here. Self-draining: an entry leaves via onCamera when the zoom
62
+ // crosses its own zMerge (merges) or its zSplit (the split walk retreats past it).
63
+ private suppressed = new Set<number>()
64
+ // Leaves removed from the displayed partition (deleted / popped out / opened). Patches map
65
+ // each structural node containing a detached leaf to its displayed replacement (or null to
66
+ // drop it). Rebuilt from the detached set; cleared by seed/seedFrom.
67
+ private detached = new Set<string>()
68
+ private patched = new Map<string, ClusterNode | null>()
69
+ private leafById: Map<string, ClusterNode> | null = null
70
+ private resolvedCache: { version: number; map: Map<string, ClusterNode> } | null = null
35
71
 
36
72
  constructor(private readonly table: ClusterTable) {
37
73
  this.resetVisible()
38
74
  }
39
75
 
76
+ getSuppressedCount(): number {
77
+ return this.suppressed.size
78
+ }
79
+
80
+ getDetachedCount(): number {
81
+ return this.detached.size
82
+ }
83
+
40
84
  seed(zoom: number): void {
41
85
  validateZoom(zoom)
42
86
  this.k = seedCount(this.table.events, zoom)
87
+ this.suppressed.clear()
88
+ this.detached.clear()
89
+ this.patched.clear()
43
90
  this.resetVisible()
44
91
  for (let i = 0; i < this.k; i++) {
45
92
  applyEvent(this.visible, this.table.events[i])
46
93
  }
47
94
  this.seeded = true
95
+ this.version++
48
96
  }
49
97
 
50
98
  seedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void {
@@ -56,21 +104,34 @@ class ClusterRuntimeImpl implements ClusterRuntime {
56
104
  }
57
105
  }
58
106
 
107
+ // Cut the cursor purely on split thresholds. zSplit is non-increasing down the table, so
108
+ // everything past the first mandatory split (zoom >= zSplit) is also mandatorily split —
109
+ // this cut can never conflict with an event that should be merged.
59
110
  const events = this.table.events
60
111
  let k = 0
61
- while (k < events.length) {
62
- const event = events[k]
63
- if (zoom >= event.zSplit) break
64
- if (zoom > event.zMerge && !wasMergedTogether(event.result.members, ownerByMember)) break
112
+ while (k < events.length && zoom < events[k].zSplit) {
65
113
  k++
66
114
  }
67
115
 
68
116
  this.k = k
117
+ this.suppressed.clear()
118
+ this.detached.clear()
119
+ this.patched.clear()
69
120
  this.resetVisible()
70
121
  for (let i = 0; i < k; i++) {
71
- applyEvent(this.visible, events[i])
122
+ const event = events[i]
123
+ if (zoom > event.zMerge && !wasMergedTogether(event.result.members, ownerByMember)) {
124
+ // In its band and previously unmerged: keep it unmerged, as an exception inside
125
+ // the cursor. Dependency-safe: an applied event can never consume a suppressed
126
+ // result — merged members imply merged (subset) children, and threshold-forced
127
+ // events force their children too (zMerge is non-increasing down the table).
128
+ this.suppressed.add(i)
129
+ } else {
130
+ applyEvent(this.visible, event)
131
+ }
72
132
  }
73
133
  this.seeded = true
134
+ this.version++
74
135
  }
75
136
 
76
137
  onCamera(zoom: number): void {
@@ -79,18 +140,106 @@ class ClusterRuntimeImpl implements ClusterRuntime {
79
140
  throw new Error('Cluster runtime must be seeded before onCamera')
80
141
  }
81
142
 
143
+ let changed = false
82
144
  while (this.k < this.table.events.length && zoom <= this.table.events[this.k].zMerge) {
83
145
  applyEvent(this.visible, this.table.events[this.k])
84
146
  this.k++
147
+ changed = true
85
148
  }
86
149
  while (this.k > 0 && zoom >= this.table.events[this.k - 1].zSplit) {
87
150
  this.k--
88
- unapplyEvent(this.visible, this.table.events[this.k])
151
+ // A suppressed event was never applied — retreating past it is bookkeeping only.
152
+ if (!this.suppressed.delete(this.k)) {
153
+ unapplyEvent(this.visible, this.table.events[this.k])
154
+ changed = true
155
+ }
89
156
  }
157
+ // Heal suppressed events at their own merge threshold: a zoom-out past zMerge merges a
158
+ // held-out band event exactly as if it had still been ahead of the cursor. Set iteration
159
+ // is insertion order (ascending index), so a healed event's suppressed children (larger
160
+ // zMerge, smaller index) always heal before it.
161
+ if (this.suppressed.size > 0) {
162
+ for (const i of [...this.suppressed]) {
163
+ if (zoom <= this.table.events[i].zMerge) {
164
+ this.suppressed.delete(i)
165
+ applyEvent(this.visible, this.table.events[i])
166
+ changed = true
167
+ }
168
+ }
169
+ }
170
+ if (changed) this.version++
171
+ }
172
+
173
+ detachLeaf(leafId: string): void {
174
+ if (this.detached.has(leafId)) return
175
+ if (!this.getLeafById().has(leafId)) return
176
+ this.detached.add(leafId)
177
+ this.rebuildPatches()
178
+ this.version++
90
179
  }
91
180
 
92
181
  getVisible(): ReadonlyMap<string, ClusterNode> {
93
- return this.visible
182
+ if (this.resolvedCache && this.resolvedCache.version === this.version) {
183
+ return this.resolvedCache.map
184
+ }
185
+ let map: Map<string, ClusterNode>
186
+ if (this.patched.size === 0) {
187
+ map = this.visible
188
+ } else {
189
+ map = new Map()
190
+ for (const node of this.visible.values()) {
191
+ const resolved = this.patched.has(node.id) ? this.patched.get(node.id)! : node
192
+ if (resolved) map.set(resolved.id, resolved)
193
+ }
194
+ }
195
+ this.resolvedCache = { version: this.version, map }
196
+ return map
197
+ }
198
+
199
+ private getLeafById(): Map<string, ClusterNode> {
200
+ if (!this.leafById) {
201
+ this.leafById = new Map(this.table.leaves.map((leaf) => [leaf.id, leaf]))
202
+ }
203
+ return this.leafById
204
+ }
205
+
206
+ /** Recompute the patch map from the detached set. A node is patched iff it contains a
207
+ * detached member; the patch drops those members and recomputes count/centroid, collapsing
208
+ * to the surviving leaf node at count 1 and to nothing at count 0. Patched nodes keep their
209
+ * structural id, so cursor events keep addressing them. */
210
+ private rebuildPatches() {
211
+ this.patched.clear()
212
+ const leafById = this.getLeafById()
213
+ for (const id of this.detached) {
214
+ this.patched.set(id, null)
215
+ }
216
+ const patch = (node: ClusterNode) => {
217
+ if (node.members.length === 1 || !node.members.some((m) => this.detached.has(m))) return
218
+ const members = node.members.filter((m) => !this.detached.has(m))
219
+ if (members.length === 0) {
220
+ this.patched.set(node.id, null)
221
+ } else if (members.length === 1) {
222
+ this.patched.set(node.id, leafById.get(members[0])!)
223
+ } else {
224
+ let x = 0
225
+ let y = 0
226
+ for (const m of members) {
227
+ const leaf = leafById.get(m)!
228
+ x += leaf.centroid.x
229
+ y += leaf.centroid.y
230
+ }
231
+ this.patched.set(node.id, {
232
+ id: node.id,
233
+ centroid: { x: x / members.length, y: y / members.length },
234
+ count: members.length,
235
+ members,
236
+ })
237
+ }
238
+ }
239
+ for (const event of this.table.events) {
240
+ patch(event.result)
241
+ for (const child of event.children) patch(child)
242
+ }
94
243
  }
95
244
 
96
245
  private resetVisible() {
package/src/ui/avatar.tsx CHANGED
@@ -17,11 +17,18 @@ function initial(name: string) {
17
17
  * @public @react */
18
18
  export function Avatar({ name, color, image }: AvatarProps) {
19
19
  if (image) {
20
- return <img className="cmt-avatar cmt-avatar--image" src={image} alt="" aria-hidden="true" />
20
+ return (
21
+ <img
22
+ className="tlui-cmt-avatar tlui-cmt-avatar--image"
23
+ src={image}
24
+ alt=""
25
+ aria-hidden="true"
26
+ />
27
+ )
21
28
  }
22
29
  return (
23
30
  <div
24
- className="cmt-avatar"
31
+ className="tlui-cmt-avatar"
25
32
  aria-hidden="true"
26
33
  style={color ? { backgroundColor: color } : undefined}
27
34
  >
package/src/ui/byline.tsx CHANGED
@@ -12,11 +12,11 @@ export interface BylineProps {
12
12
  /** A comment's metadata line: author name, relative time, and an edited marker. @public @react */
13
13
  export function Byline({ author, date, edited }: BylineProps) {
14
14
  return (
15
- <div className="cmt-head">
16
- <span className="cmt-author">{author}</span>
17
- <span className="cmt-time">
15
+ <div className="tlui-cmt-head">
16
+ <span className="tlui-cmt-author">{author}</span>
17
+ <span className="tlui-cmt-time">
18
18
  {formatRelativeTime(date)}
19
- {edited && <span className="cmt-edited"> · edited</span>}
19
+ {edited && <span className="tlui-cmt-edited"> · edited</span>}
20
20
  </span>
21
21
  </div>
22
22
  )
@@ -20,13 +20,13 @@ export interface CommentCardProps {
20
20
  /** A single comment: Avatar, Byline, and a body slot the consumer renders. @public @react */
21
21
  export function CommentCard({ author, body, date, you, edited, actions }: CommentCardProps) {
22
22
  return (
23
- <div className={you ? 'cmt-card cmt-card--you' : 'cmt-card'}>
23
+ <div className={you ? 'tlui-cmt-card tlui-cmt-card--you' : 'tlui-cmt-card'}>
24
24
  <Avatar name={author} />
25
- <div className="cmt-body">
25
+ <div className="tlui-cmt-body">
26
26
  <Byline author={author} date={date} edited={edited} />
27
27
  {body}
28
28
  </div>
29
- {actions !== undefined && <div className="cmt-card__actions">{actions}</div>}
29
+ {actions !== undefined && <div className="tlui-cmt-card__actions">{actions}</div>}
30
30
  </div>
31
31
  )
32
32
  }
@@ -1,4 +1,4 @@
1
- import { Extension, JSONContent } from '@tiptap/core'
1
+ import { JSONContent } from '@tiptap/core'
2
2
  import { EditorContent, useEditor } from '@tiptap/react'
3
3
  import { ReactNode, useEffect, useMemo, useRef, useState } from 'react'
4
4
  import { isEqual, TLRichText, useMaybeEditor } from 'tldraw'
@@ -69,28 +69,20 @@ export function CommentComposer({
69
69
 
70
70
  const [isEmpty, setIsEmpty] = useState(() => !value || isCommentEmpty(value))
71
71
 
72
- // Enter submits the comment. Shift+Enter (and Cmd/Ctrl+Enter) keep their default behavior — a
73
- // new line for the occasional multi-line comment.
74
- const submitExtension = useMemo(
75
- () =>
76
- Extension.create({
77
- name: 'commentComposerSubmit',
78
- priority: 1000,
79
- addKeyboardShortcuts() {
80
- return {
81
- Enter: () => {
82
- // While the @-mention picker is open, Enter selects the highlighted member.
83
- // This extension outranks the mention plugin (priority 1000), so defer to the
84
- // picker here or Enter would submit before the member could be inserted.
85
- if (isMentionPickerOpen()) return false
86
- if (!disabledRef.current) onSubmitRef.current?.()
87
- return true
88
- },
89
- }
90
- },
91
- }),
92
- []
93
- )
72
+ // The editor instance, reachable from `handleKeyDown` (which is created before `useEditor`
73
+ // returns). Updated on every render so the ref never points at a stale editor.
74
+ const editorRef = useRef<ReturnType<typeof useEditor>>(null)
75
+
76
+ // Set while we replay Enter through the keymaps for a Shift+Enter newline: `commands.enter()`
77
+ // re-dispatches Enter through `handleKeyDown`, and without this guard our own handler would catch
78
+ // that synthetic Enter and submit instead.
79
+ const replayingEnter = useRef(false)
80
+
81
+ // Enter and Cmd/Ctrl+Enter submit the comment; Shift+Enter inserts a new line for the occasional
82
+ // multi-line comment. This is handled through `editorProps.handleKeyDown` (below) rather than a
83
+ // keyboard-shortcut extension: ProseMirror runs `handleKeyDown` before every keymap plugin, so it
84
+ // can tell Shift+Enter apart from Enter an `Enter` keymap binding also fires on Shift/Cmd+Enter
85
+ // and would otherwise swallow the newline.
94
86
 
95
87
  // The suggestion plugin is built once (the editor is recreated only on `interactive`) and runs
96
88
  // outside React, so it must read the mention callbacks through refs — like onChange/onSubmit —
@@ -100,7 +92,7 @@ export function CommentComposer({
100
92
  const mentionsEnabled = !!getMentionSuggestions
101
93
  const hasCustomRow = !!renderMentionSuggestion
102
94
  const extensions = useMemo(() => {
103
- const list = [...commentTipTapExtensions, submitExtension]
95
+ const list = [...commentTipTapExtensions]
104
96
  // Always register the mention node so an existing body that contains a mention keeps it on
105
97
  // edit (an unregistered node would be stripped by ProseMirror when the content loads). The `@`
106
98
  // picker itself only turns on when the host provides a resolver; otherwise the node is present
@@ -122,7 +114,7 @@ export function CommentComposer({
122
114
  list.push(commentMention({ suggestion: { char: '@', allow: () => false } }))
123
115
  }
124
116
  return list
125
- }, [submitExtension, mentionsEnabled, hasCustomRow, tlEditor])
117
+ }, [mentionsEnabled, hasCustomRow, tlEditor])
126
118
 
127
119
  const editor = useEditor(
128
120
  {
@@ -134,7 +126,33 @@ export function CommentComposer({
134
126
  // mirrors RichTextArea's setup.
135
127
  enableCoreExtensions: { textDirection: false },
136
128
  textDirection: 'auto',
137
- editorProps: { attributes: { class: 'cmt-input' } },
129
+ editorProps: {
130
+ attributes: { class: 'tlui-cmt-input' },
131
+ // Runs before every keymap plugin, so it can distinguish Shift+Enter from Enter — an
132
+ // `Enter` keymap binding also fires on Shift+Enter and would otherwise swallow it.
133
+ handleKeyDown: (_view, event) => {
134
+ // Let the keymaps handle the synthetic Enter we replay for a Shift+Enter newline.
135
+ if (replayingEnter.current) return false
136
+ if (event.key !== 'Enter' || event.isComposing) return false
137
+ // While the @-mention picker is open, Enter selects the highlighted member — defer.
138
+ if (isMentionPickerOpen()) return false
139
+ // Shift+Enter inserts a new line. Replay a plain Enter through the keymaps (guarded so
140
+ // we don't re-enter and submit) to reuse the editor's list-aware Enter handling — a new
141
+ // list item in a list, a new paragraph otherwise. tldraw doesn't do soft breaks.
142
+ if (event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey) {
143
+ replayingEnter.current = true
144
+ try {
145
+ editorRef.current?.commands.enter()
146
+ } finally {
147
+ replayingEnter.current = false
148
+ }
149
+ return true
150
+ }
151
+ // Enter, Cmd+Enter, and Ctrl+Enter submit.
152
+ if (!disabledRef.current) onSubmitRef.current?.()
153
+ return true
154
+ },
155
+ },
138
156
  onUpdate: ({ editor }) => {
139
157
  setIsEmpty(editor.isEmpty)
140
158
  onChangeRef.current?.(editor.getJSON() as TLRichText)
@@ -142,6 +160,7 @@ export function CommentComposer({
142
160
  },
143
161
  [interactive]
144
162
  )
163
+ editorRef.current = editor
145
164
 
146
165
  // Sync controlled resets (e.g. the parent clearing to EMPTY_COMMENT after posting) into the
147
166
  // editor without echoing back the value the editor itself just emitted.
@@ -161,13 +180,13 @@ export function CommentComposer({
161
180
  }, [autoFocus, editor])
162
181
 
163
182
  return (
164
- <div className="cmt-composer">
183
+ <div className="tlui-cmt-composer">
165
184
  {leading ?? <Avatar name={author} />}
166
- <div className="cmt-composer__field">
167
- <div className="cmt-composer__input-wrap">
185
+ <div className="tlui-cmt-composer__field">
186
+ <div className="tlui-cmt-composer__input-wrap">
168
187
  <EditorContent editor={editor} />
169
188
  {isEmpty && (
170
- <div className="cmt-input__placeholder" aria-hidden="true">
189
+ <div className="tlui-cmt-input__placeholder" aria-hidden="true">
171
190
  {placeholder}
172
191
  </div>
173
192
  )}