@tldraw/commenting 5.7.0-canary.fd4a9d669e63 → 5.7.0-internal.5fe8922939ec

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 (111) hide show
  1. package/commenting.css +280 -275
  2. package/dist-cjs/canvas/canvas-events.js +25 -1
  3. package/dist-cjs/canvas/canvas-events.js.map +2 -2
  4. package/dist-cjs/canvas/comment-mutations.js +5 -0
  5. package/dist-cjs/canvas/comment-mutations.js.map +2 -2
  6. package/dist-cjs/canvas/comment-tool.js +29 -28
  7. package/dist-cjs/canvas/comment-tool.js.map +2 -2
  8. package/dist-cjs/canvas/comments-filter-menu.js +55 -38
  9. package/dist-cjs/canvas/comments-filter-menu.js.map +2 -2
  10. package/dist-cjs/canvas/comments-overflow-menu.js +6 -24
  11. package/dist-cjs/canvas/comments-overflow-menu.js.map +2 -2
  12. package/dist-cjs/canvas/comments-overlay.js +1 -0
  13. package/dist-cjs/canvas/comments-overlay.js.map +2 -2
  14. package/dist-cjs/canvas/comments-sidebar.js +15 -3
  15. package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
  16. package/dist-cjs/canvas/comments-visibility-toggle.js +1 -1
  17. package/dist-cjs/canvas/comments-visibility-toggle.js.map +1 -1
  18. package/dist-cjs/canvas/options.js +12 -0
  19. package/dist-cjs/canvas/options.js.map +2 -2
  20. package/dist-cjs/canvas/pending-composer.js +139 -9
  21. package/dist-cjs/canvas/pending-composer.js.map +2 -2
  22. package/dist-cjs/canvas/region-box.js +150 -35
  23. package/dist-cjs/canvas/region-box.js.map +3 -3
  24. package/dist-cjs/canvas/thread-pin.js +20 -4
  25. package/dist-cjs/canvas/thread-pin.js.map +2 -2
  26. package/dist-cjs/canvas/thread-view.js +134 -58
  27. package/dist-cjs/canvas/thread-view.js.map +2 -2
  28. package/dist-cjs/index.d.ts +46 -12
  29. package/dist-cjs/index.js +1 -1
  30. package/dist-cjs/ui/byline.js +2 -2
  31. package/dist-cjs/ui/byline.js.map +2 -2
  32. package/dist-cjs/ui/comment-card.js +3 -1
  33. package/dist-cjs/ui/comment-card.js.map +2 -2
  34. package/dist-cjs/ui/comment-extensions.js +4 -1
  35. package/dist-cjs/ui/comment-extensions.js.map +2 -2
  36. package/dist-cjs/ui/comments-list.js +6 -7
  37. package/dist-cjs/ui/comments-list.js.map +2 -2
  38. package/dist-cjs/ui/icons.js +4 -4
  39. package/dist-cjs/ui/icons.js.map +2 -2
  40. package/dist-cjs/ui/reaction-picker.js +2 -2
  41. package/dist-cjs/ui/reaction-picker.js.map +1 -1
  42. package/dist-cjs/ui/send-button.js.map +1 -1
  43. package/dist-esm/canvas/canvas-events.mjs +25 -1
  44. package/dist-esm/canvas/canvas-events.mjs.map +2 -2
  45. package/dist-esm/canvas/comment-mutations.mjs +5 -0
  46. package/dist-esm/canvas/comment-mutations.mjs.map +2 -2
  47. package/dist-esm/canvas/comment-tool.mjs +30 -29
  48. package/dist-esm/canvas/comment-tool.mjs.map +2 -2
  49. package/dist-esm/canvas/comments-filter-menu.mjs +57 -39
  50. package/dist-esm/canvas/comments-filter-menu.mjs.map +2 -2
  51. package/dist-esm/canvas/comments-overflow-menu.mjs +8 -24
  52. package/dist-esm/canvas/comments-overflow-menu.mjs.map +2 -2
  53. package/dist-esm/canvas/comments-overlay.mjs +1 -0
  54. package/dist-esm/canvas/comments-overlay.mjs.map +2 -2
  55. package/dist-esm/canvas/comments-sidebar.mjs +16 -4
  56. package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
  57. package/dist-esm/canvas/comments-visibility-toggle.mjs +1 -1
  58. package/dist-esm/canvas/comments-visibility-toggle.mjs.map +1 -1
  59. package/dist-esm/canvas/options.mjs +12 -0
  60. package/dist-esm/canvas/options.mjs.map +2 -2
  61. package/dist-esm/canvas/pending-composer.mjs +150 -11
  62. package/dist-esm/canvas/pending-composer.mjs.map +2 -2
  63. package/dist-esm/canvas/region-box.mjs +161 -38
  64. package/dist-esm/canvas/region-box.mjs.map +3 -3
  65. package/dist-esm/canvas/thread-pin.mjs +22 -6
  66. package/dist-esm/canvas/thread-pin.mjs.map +2 -2
  67. package/dist-esm/canvas/thread-view.mjs +145 -60
  68. package/dist-esm/canvas/thread-view.mjs.map +2 -2
  69. package/dist-esm/index.d.mts +46 -12
  70. package/dist-esm/index.mjs +1 -1
  71. package/dist-esm/ui/byline.mjs +2 -2
  72. package/dist-esm/ui/byline.mjs.map +2 -2
  73. package/dist-esm/ui/comment-card.mjs +3 -1
  74. package/dist-esm/ui/comment-card.mjs.map +2 -2
  75. package/dist-esm/ui/comment-extensions.mjs +11 -2
  76. package/dist-esm/ui/comment-extensions.mjs.map +2 -2
  77. package/dist-esm/ui/comments-list.mjs +6 -7
  78. package/dist-esm/ui/comments-list.mjs.map +2 -2
  79. package/dist-esm/ui/icons.mjs +4 -4
  80. package/dist-esm/ui/icons.mjs.map +2 -2
  81. package/dist-esm/ui/reaction-picker.mjs +2 -2
  82. package/dist-esm/ui/reaction-picker.mjs.map +1 -1
  83. package/dist-esm/ui/send-button.mjs.map +1 -1
  84. package/package.json +5 -5
  85. package/src/canvas/canvas-events.ts +39 -2
  86. package/src/canvas/canvas.css +76 -38
  87. package/src/canvas/comment-mutations.test.ts +51 -0
  88. package/src/canvas/comment-mutations.ts +14 -0
  89. package/src/canvas/comment-render.test.ts +8 -1
  90. package/src/canvas/comment-tool.test.ts +34 -44
  91. package/src/canvas/comment-tool.tsx +51 -55
  92. package/src/canvas/comments-filter-menu.tsx +19 -6
  93. package/src/canvas/comments-overflow-menu.tsx +12 -18
  94. package/src/canvas/comments-overlay.tsx +5 -2
  95. package/src/canvas/comments-sidebar.tsx +20 -5
  96. package/src/canvas/comments-visibility-toggle.tsx +1 -1
  97. package/src/canvas/options.test.ts +23 -1
  98. package/src/canvas/options.ts +41 -3
  99. package/src/canvas/pending-composer.tsx +190 -19
  100. package/src/canvas/region-box.tsx +192 -42
  101. package/src/canvas/thread-pin.tsx +28 -6
  102. package/src/canvas/thread-view.test.ts +39 -0
  103. package/src/canvas/thread-view.tsx +192 -72
  104. package/src/ui/byline.tsx +5 -2
  105. package/src/ui/comment-card.tsx +7 -1
  106. package/src/ui/comment-extensions.ts +18 -5
  107. package/src/ui/comments-list.tsx +23 -17
  108. package/src/ui/comments.css +175 -201
  109. package/src/ui/icons.tsx +5 -5
  110. package/src/ui/reaction-picker.tsx +2 -2
  111. package/src/ui/send-button.tsx +2 -2
@@ -1,32 +1,48 @@
1
1
  import { Avatar, isMentionPickerOpen } from '@tldraw/mentions'
2
- import { useEffect, useRef, useState } from 'react'
2
+ import {
3
+ type MouseEvent as ReactMouseEvent,
4
+ type PointerEvent as ReactPointerEvent,
5
+ useEffect,
6
+ useRef,
7
+ useState,
8
+ } from 'react'
3
9
  import {
4
10
  createComment,
5
11
  createCommentThread,
6
12
  defineMessages,
7
13
  Editor,
8
14
  EditorPortal,
15
+ TLCommentAnchor,
9
16
  TLRichText,
10
17
  usePassThroughWheelEvents,
11
18
  useTranslation,
12
19
  useValue,
20
+ VecLike,
13
21
  } from 'tldraw'
14
22
  import { CommentComposer } from '../ui/comment-composer'
15
23
  import { EMPTY_COMMENT, isCommentEmpty } from '../ui/comment-extensions'
16
24
  import { CommentPin } from '../ui/comment-pin'
25
+ import { forwardPointerEventToCanvas, isCanvasPanGesture, startCommentDrag } from './canvas-events'
17
26
  import {
18
27
  clearCommentDraft,
19
28
  getCommentDraft,
20
29
  NEW_COMMENT_DRAFT,
21
30
  saveCommentDraft,
22
31
  } from './comment-drafts'
23
- import { commitCommentMutation } from './comment-mutations'
32
+ import { commitCommentPost } from './comment-mutations'
24
33
  import { UNKNOWN_COMMENT_AUTHOR } from './comment-render'
25
- import { PendingComment } from './comment-tool'
34
+ import { PendingComment, pendingCommentAt } from './comment-tool'
26
35
  import { type CommentingContext } from './context'
27
36
  import { useIsMobileCommenting, useMobilePlacement } from './mobile-placement'
28
37
  import { useCanComment, useCommentingOptions } from './options'
29
38
  import { pendingComment } from './state'
39
+ import {
40
+ commentTargetShapeAt,
41
+ regionAnchorPinCorner,
42
+ regionPinPoint,
43
+ regionWithPinAt,
44
+ } from './thread-state'
45
+ import { avatarRenderer } from './thread-view'
30
46
 
31
47
  const messages = defineMessages({
32
48
  commentsSend: { id: 'comments.send', defaultMessage: 'Send' },
@@ -49,14 +65,25 @@ export function PendingComposer({
49
65
  getMentionSuggestions,
50
66
  renderMentionSuggestion,
51
67
  }: CommentingContext & { editor: Editor; pending: PendingComment }) {
52
- const ComposerFallback = useCommentingOptions().components.ComposerFallback
68
+ const { components } = useCommentingOptions()
69
+ const ComposerFallback = components.ComposerFallback
53
70
  const canComment = useCanComment(currentUserId)
54
71
  const me = currentUserId ? resolveAuthor(currentUserId) : undefined
72
+ const isMobile = useIsMobileCommenting()
73
+ const drag = useDraftDrag(editor, pending)
55
74
  // The leading pin previews the pin this draft becomes: a white pin holding the author's avatar.
56
75
  const draftAvatar = (
57
- <CommentPin>
58
- <Avatar author={me ?? UNKNOWN_COMMENT_AUTHOR} />
59
- </CommentPin>
76
+ <div
77
+ className="tlui-cmt-canvas-composer__handle"
78
+ // On mobile the composer floats free of its point, so there's no pin to drag.
79
+ {...(isMobile ? {} : drag.handlers)}
80
+ >
81
+ <CommentPin>
82
+ {avatarRenderer(components, currentUserId)?.(me ?? UNKNOWN_COMMENT_AUTHOR) ?? (
83
+ <Avatar author={me ?? UNKNOWN_COMMENT_AUTHOR} />
84
+ )}
85
+ </CommentPin>
86
+ </div>
60
87
  )
61
88
  // Click-away keeps the draft (saved on every change) and the next placement composer
62
89
  // restores it — the flip side of dismissing without a discard warning.
@@ -74,9 +101,32 @@ export function PendingComposer({
74
101
  ])
75
102
  // On mobile the composer floats free of the pin so it can clear the software keyboard; desktop
76
103
  // keeps it pinned to the point.
77
- const isMobile = useIsMobileCommenting()
78
104
  const placed = useMobilePlacement(ref, point, isMobile)
79
105
 
106
+ // Escape steps back one level at a time: a draft with text is cleared first, and only an empty
107
+ // one closes. A native listener on the composer, because the editor's own Escape handler (which
108
+ // would also cancel the comment tool) sits on its container, below React's root listener.
109
+ const textRef = useRef(text)
110
+ textRef.current = text
111
+ useEffect(() => {
112
+ const el = ref.current
113
+ if (!el) return
114
+ const onKeyDown = (e: KeyboardEvent) => {
115
+ if (e.key !== 'Escape' || isMentionPickerOpen()) return
116
+ e.stopPropagation()
117
+ if (!isCommentEmpty(textRef.current)) {
118
+ setText(EMPTY_COMMENT)
119
+ clearCommentDraft(NEW_COMMENT_DRAFT)
120
+ return
121
+ }
122
+ pendingComment.set(editor, null)
123
+ // Focus would drop to the page with the composer gone, out of reach of the next Escape.
124
+ editor.getContainer().focus()
125
+ }
126
+ el.addEventListener('keydown', onKeyDown)
127
+ return () => el.removeEventListener('keydown', onKeyDown)
128
+ }, [editor])
129
+
80
130
  // Dismiss on a click anywhere outside the composer (capture-phase, ahead of stopPropagation).
81
131
  useEffect(() => {
82
132
  const onPointerDown = (e: PointerEvent) => {
@@ -86,9 +136,8 @@ export function PendingComposer({
86
136
  // A click in the composer, or in the mention picker it spawns (portaled elsewhere), is
87
137
  // not "outside" — keep the draft open so the pick can insert.
88
138
  if (el.contains(target) || target.closest('.tlui-cmt-mention-popup')) return
89
- // A press on the canvas is the comment tool's to handle: it ends the placement and returns
90
- // to select. Clearing here first would let that press re-place the composer instead.
91
- if (editor.getContainer().querySelector('.tl-canvas')?.contains(target)) return
139
+ // Panning the camera to look around while writing isn't leaving the draft.
140
+ if (isCanvasPanGesture(editor, e) || (e as any).isSpecialRedispatchedEvent) return
92
141
  pendingComment.set(editor, null)
93
142
  }
94
143
  document.addEventListener('pointerdown', onPointerDown, true)
@@ -97,7 +146,7 @@ export function PendingComposer({
97
146
 
98
147
  const submit = () => {
99
148
  if (isCommentEmpty(text) || !currentUserId) return
100
- const comment = commitCommentMutation(editor, ({ put }) => {
149
+ const comment = commitCommentPost(editor, ({ put }) => {
101
150
  const pageId = editor.getCurrentPageId()
102
151
  const thread = createCommentThread({
103
152
  pageId,
@@ -113,13 +162,11 @@ export function PendingComposer({
113
162
  put([thread, comment])
114
163
  return comment
115
164
  })
165
+ // refused: keep the draft; the editor has emitted 'refused'
166
+ if (!comment) return
116
167
  setText(EMPTY_COMMENT)
117
168
  clearCommentDraft(NEW_COMMENT_DRAFT)
118
169
  pendingComment.set(editor, null)
119
- // Posting ends the placement interaction — hand the comment tool back to select. Only from
120
- // the settled idle state: Enter can land while a fresh press is mid-gesture (the composer
121
- // trails the pointer), and switching tools under a held pointer would strand the gesture.
122
- if (editor.isIn('comment.idle')) editor.setCurrentTool('select')
123
170
  // The host's callback is its own operation, not part of the post's history scope. It runs
124
171
  // last so a throwing host can't strand the composer holding a draft of a posted comment.
125
172
  onPostComment?.(comment)
@@ -132,6 +179,7 @@ export function PendingComposer({
132
179
  className={[
133
180
  'tlui-cmt-canvas-composer',
134
181
  pending.anchor.type === 'region' && 'tlui-cmt-canvas-composer--region',
182
+ drag.dragging && 'tlui-cmt-canvas-composer--dragging',
135
183
  !canComment && 'tlui-cmt-canvas-composer--fallback',
136
184
  ]
137
185
  .filter(Boolean)
@@ -139,9 +187,6 @@ export function PendingComposer({
139
187
  style={{ left: placed.left, top: placed.top }}
140
188
  onPointerDown={stop}
141
189
  onContextMenu={stop}
142
- onKeyDown={(e) => {
143
- if (e.key === 'Escape' && !isMentionPickerOpen()) pendingComment.set(editor, null)
144
- }}
145
190
  >
146
191
  {canComment ? (
147
192
  <CommentComposer
@@ -168,3 +213,129 @@ export function PendingComposer({
168
213
  </EditorPortal>
169
214
  )
170
215
  }
216
+
217
+ /** Drag the draft by its avatar pin, the way a placed pin drags: the composer follows the pointer
218
+ * and, on release, re-anchors to the shape under it (a region translates, keeping its size). */
219
+ function useDraftDrag(editor: Editor, pending: PendingComment) {
220
+ const [dragging, setDragging] = useState(false)
221
+ const dragRef = useRef<{
222
+ startX: number
223
+ startY: number
224
+ moved: boolean
225
+ // The pin's page offset from the grab point, so the pin keeps its place under the pointer.
226
+ offsetX: number
227
+ offsetY: number
228
+ clientX: number
229
+ clientY: number
230
+ endDrag?(): void
231
+ } | null>(null)
232
+ // Unmounting mid-drag (the draft is dismissed) gets no pointer up to end the drag, and the
233
+ // drop-target hint is editor-global state that would otherwise stay on the shape.
234
+ useEffect(
235
+ () => () => {
236
+ if (!dragRef.current) return
237
+ dragRef.current.endDrag?.()
238
+ editor.setHintingShapes([])
239
+ },
240
+ [editor]
241
+ )
242
+
243
+ const pagePointFor = (clientX: number, clientY: number) => {
244
+ const drag = dragRef.current!
245
+ const cursor = editor.screenToPage({ x: clientX, y: clientY })
246
+ return { x: cursor.x + drag.offsetX, y: cursor.y + drag.offsetY }
247
+ }
248
+
249
+ // A region moves as a whole, its box riding with the pin corner; a point draft moves only its
250
+ // composer until the drop, when its anchor resolves against what's under it.
251
+ const moveRegion = (anchor: Extract<TLCommentAnchor, { type: 'region' }>, point: VecLike) => {
252
+ const corner = regionAnchorPinCorner(anchor)
253
+ const moved = regionWithPinAt(anchor, corner, point)
254
+ pendingComment.set(editor, { anchor: moved, point: regionPinPoint(moved, corner) })
255
+ }
256
+
257
+ // Mid-drag, a point draft hints the shape a drop here would anchor to.
258
+ const previewAt = (point: VecLike) => {
259
+ const current = pendingComment.get(editor)
260
+ if (!current) return
261
+ if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
262
+ pendingComment.set(editor, { ...current, point: { x: point.x, y: point.y } })
263
+ const hit = commentTargetShapeAt(editor, point)
264
+ editor.setHintingShapes(hit ? [hit.id] : [])
265
+ }
266
+
267
+ const settleAt = (point: VecLike, altKey: boolean) => {
268
+ const current = pendingComment.get(editor)
269
+ if (!current) return
270
+ if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
271
+ pendingComment.set(editor, pendingCommentAt(editor, point, altKey))
272
+ }
273
+
274
+ const takeDrag = (e: ReactPointerEvent<HTMLDivElement>) => {
275
+ const drag = dragRef.current
276
+ dragRef.current = null
277
+ drag?.endDrag?.()
278
+ editor.setHintingShapes([])
279
+ setDragging(false)
280
+ if (e.currentTarget.hasPointerCapture(e.pointerId)) {
281
+ e.currentTarget.releasePointerCapture(e.pointerId)
282
+ }
283
+ return drag
284
+ }
285
+
286
+ const handlers = {
287
+ onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
288
+ // A middle/right-button or space-held press is a camera pan, not a drag.
289
+ if (isCanvasPanGesture(editor, e)) {
290
+ forwardPointerEventToCanvas(editor.getContainer(), e)
291
+ return
292
+ }
293
+ const grab = editor.screenToPage({ x: e.clientX, y: e.clientY })
294
+ dragRef.current = {
295
+ startX: e.clientX,
296
+ startY: e.clientY,
297
+ clientX: e.clientX,
298
+ clientY: e.clientY,
299
+ moved: false,
300
+ offsetX: pending.point.x - grab.x,
301
+ offsetY: pending.point.y - grab.y,
302
+ }
303
+ e.currentTarget.setPointerCapture(e.pointerId)
304
+ },
305
+ // Keep focus in the comment field: grabbing the pin shouldn't blur what's being typed.
306
+ onMouseDown: (e: ReactMouseEvent) => e.preventDefault(),
307
+ onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
308
+ const drag = dragRef.current
309
+ if (!drag) return
310
+ if (!drag.moved && Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < 4) return
311
+ drag.clientX = e.clientX
312
+ drag.clientY = e.clientY
313
+ if (!drag.moved) {
314
+ drag.moved = true
315
+ drag.endDrag = startCommentDrag(editor, {
316
+ cursor: 'move',
317
+ onCameraMove: () => {
318
+ const d = dragRef.current
319
+ if (d) previewAt(pagePointFor(d.clientX, d.clientY))
320
+ },
321
+ })
322
+ setDragging(true)
323
+ }
324
+ previewAt(pagePointFor(e.clientX, e.clientY))
325
+ },
326
+ onPointerUp: (e: ReactPointerEvent<HTMLDivElement>) => {
327
+ const moved = dragRef.current?.moved
328
+ const point = moved ? pagePointFor(e.clientX, e.clientY) : null
329
+ takeDrag(e)
330
+ if (point) settleAt(point, e.altKey)
331
+ },
332
+ // A cancelled pointer leaves the draft where the drag last put it, with its anchor resolved.
333
+ onPointerCancel: (e: ReactPointerEvent<HTMLDivElement>) => {
334
+ const drag = takeDrag(e)
335
+ const current = pendingComment.get(editor)
336
+ if (drag?.moved && current) settleAt(current.point, editor.inputs.getAltKey())
337
+ },
338
+ }
339
+
340
+ return { dragging, handlers }
341
+ }
@@ -1,5 +1,14 @@
1
- import { type PointerEvent as ReactPointerEvent, useRef } from 'react'
2
- import { type BoxModel, Editor, useValue, VecLike } from 'tldraw'
1
+ import { type PointerEvent as ReactPointerEvent, useEffect, useRef } from 'react'
2
+ import {
3
+ type BoxModel,
4
+ Editor,
5
+ getCursor,
6
+ getPerfectDashProps,
7
+ usePassThroughWheelEvents,
8
+ useValue,
9
+ VecLike,
10
+ } from 'tldraw'
11
+ import { forwardPointerEventToCanvas, isCanvasPanGesture, startCommentDrag } from './canvas-events'
3
12
  import { regionDraft } from './state'
4
13
 
5
14
  /** A region's dashed box. Purely visual — a region moves by its pin and resizes from its corner
@@ -16,7 +25,39 @@ export function RegionBox({ editor, box }: { editor: Editor; box: BoxModel }) {
16
25
  },
17
26
  [editor, box.x, box.y, box.w, box.h]
18
27
  )
19
- return <div className="tlui-cmt-canvas-region" style={rect} />
28
+ const { width: w, height: h } = rect
29
+ // The group outline's dashes: drawn per side with perfect dashes (so every side starts and ends
30
+ // on a dash), at the 1px dash scale and 1.5px stroke the editor's selection outline uses.
31
+ const sides = [
32
+ [0, 0, w, 0],
33
+ [w, 0, w, h],
34
+ [w, h, 0, h],
35
+ [0, h, 0, 0],
36
+ ]
37
+ return (
38
+ <div className="tlui-cmt-canvas-region" style={rect}>
39
+ <svg className="tlui-cmt-canvas-region__outline" width={w} height={h} aria-hidden="true">
40
+ {sides.map(([x1, y1, x2, y2], i) => {
41
+ const { strokeDasharray, strokeDashoffset } = getPerfectDashProps(
42
+ Math.hypot(x2 - x1, y2 - y1),
43
+ 1,
44
+ { style: 'dashed', lengthRatio: 4 }
45
+ )
46
+ return (
47
+ <line
48
+ key={i}
49
+ x1={x1}
50
+ y1={y1}
51
+ x2={x2}
52
+ y2={y2}
53
+ strokeDasharray={strokeDasharray}
54
+ strokeDashoffset={strokeDashoffset}
55
+ />
56
+ )
57
+ })}
58
+ </svg>
59
+ </div>
60
+ )
20
61
  }
21
62
 
22
63
  /** The live region being dragged out by the comment tool, or nothing when not dragging. */
@@ -26,15 +67,13 @@ export function RegionDraftBox({ editor }: { editor: Editor }) {
26
67
  return <RegionBox editor={editor} box={box} />
27
68
  }
28
69
 
29
- // A resize handle's normalized 0–1 spot on the box, and its cursor. An axis at 0.5 is *not*
30
- // controlled by that handle — the resize math reads the spot rather than special-casing corners.
70
+ // A corner resize handle: its normalized 0–1 spot on the box, and its cursor.
31
71
  export interface RegionHandle {
32
72
  x: number
33
73
  y: number
34
- cursor: string
74
+ cursor: 'nwse-resize' | 'nesw-resize'
35
75
  }
36
76
 
37
- // The four corners, each resizing both axes.
38
77
  export const REGION_CORNERS: readonly RegionHandle[] = [
39
78
  { x: 0, y: 0, cursor: 'nwse-resize' },
40
79
  { x: 1, y: 0, cursor: 'nesw-resize' },
@@ -46,24 +85,56 @@ export const REGION_CORNERS: readonly RegionHandle[] = [
46
85
  // the handles (which sit on the edge) are comfortably reachable.
47
86
  export const REGION_HANDLE_MARGIN_PX = 12
48
87
 
49
- /** Resize `box` by dragging `handle` to `cursor` (page coords). Each controlled axis spans from the
50
- * handle's fixed opposite edge to the cursor (normalized, so dragging past it flips); an axis the
51
- * handle doesn't control (a midpoint, at 0.5) keeps its original position and size. */
88
+ /** Resize `box` by dragging corner `handle` to `cursor` (page coords): the box spans from the
89
+ * opposite corner to the cursor, normalized so dragging past that corner flips it. */
52
90
  function resizeRegion(box: BoxModel, handle: RegionHandle, cursor: VecLike): BoxModel {
53
- const controlsX = handle.x !== 0.5
54
- const controlsY = handle.y !== 0.5
55
91
  const fixedX = box.x + (1 - handle.x) * box.w
56
92
  const fixedY = box.y + (1 - handle.y) * box.h
57
93
  return {
58
- x: controlsX ? Math.min(fixedX, cursor.x) : box.x,
59
- y: controlsY ? Math.min(fixedY, cursor.y) : box.y,
60
- w: controlsX ? Math.abs(cursor.x - fixedX) : box.w,
61
- h: controlsY ? Math.abs(cursor.y - fixedY) : box.h,
94
+ x: Math.min(fixedX, cursor.x),
95
+ y: Math.min(fixedY, cursor.y),
96
+ w: Math.abs(cursor.x - fixedX),
97
+ h: Math.abs(cursor.y - fixedY),
98
+ }
99
+ }
100
+
101
+ /**
102
+ * The selection's corner-handle sizing (`SelectionForegroundOverlayUtil`), in screen px for a box
103
+ * `w`×`h` px on screen, so a region's corners look and hit exactly like a shape's resize corners.
104
+ * Keep in step with that util: the constants are its, not tuned here.
105
+ */
106
+ export function selectionCornerSizing(w: number, h: number, isCoarsePointer: boolean) {
107
+ const handleSize = 8
108
+ const mobileHandleMultiplier = isCoarsePointer ? 1.75 : 1
109
+ const hitTargetSize = 6 * mobileHandleMultiplier
110
+ const isSmallX = w < handleSize * 4
111
+ const isSmallY = h < handleSize * 4
112
+ const hitTargetSizeX =
113
+ (isSmallX ? hitTargetSize / 2 : hitTargetSize) * (mobileHandleMultiplier * 0.75)
114
+ const hitTargetSizeY =
115
+ (isSmallY ? hitTargetSize / 2 : hitTargetSize) * (mobileHandleMultiplier * 0.75)
116
+ const isTinyX = w < handleSize * 2
117
+ const isTinyY = h < handleSize * 2
118
+ return {
119
+ // The corner hit square's full side: the util's `cornerHitHalfSize`, doubled.
120
+ hitSize: Math.max(hitTargetSizeX, hitTargetSizeY) * 1.5 * 2,
121
+ hideAlternateCorners: isTinyX || isTinyY,
122
+ showOnlyOneCorner: isTinyX && isTinyY,
62
123
  }
63
124
  }
64
125
 
65
- /** Draggable handles that resize a region — corners (both axes) or edges (one axis), per the resize
66
- * option. Previews live, commits on release. */
126
+ // The selection hides the top-right and bottom-left corners on a tiny box, and the bottom-right too
127
+ // when it's tiny both ways (top-left always shows).
128
+ function isHiddenCorner(
129
+ h: RegionHandle,
130
+ sizing: ReturnType<typeof selectionCornerSizing>
131
+ ): boolean {
132
+ if (h.x === 0 && h.y === 0) return false
133
+ if (h.x === 1 && h.y === 1) return sizing.showOnlyOneCorner
134
+ return sizing.hideAlternateCorners
135
+ }
136
+
137
+ /** Draggable corner handles that resize a region. Previews live, commits on release. */
67
138
  export function RegionResizeHandles({
68
139
  editor,
69
140
  box,
@@ -79,46 +150,125 @@ export function RegionResizeHandles({
79
150
  }) {
80
151
  // The box at pointer-down, captured so the box prop reflowing under the live preview doesn't move
81
152
  // the fixed edges mid-drag.
82
- const boxRef = useRef<BoxModel | null>(null)
83
- const points = useValue(
84
- 'region handle points',
85
- () =>
86
- handles.map((h) => {
87
- const p = editor.pageToViewport({ x: box.x + h.x * box.w, y: box.y + h.y * box.h })
88
- return { ...h, key: `${h.x}-${h.y}`, left: p.x, top: p.y }
89
- }),
153
+ const dragRef = useRef<{
154
+ box: BoxModel
155
+ handle: RegionHandle
156
+ clientX: number
157
+ clientY: number
158
+ endDrag(): void
159
+ } | null>(null)
160
+ const wheelRef = useRef<HTMLDivElement>(null)
161
+ usePassThroughWheelEvents(wheelRef)
162
+ // Unmounting mid-resize gets no pointer up to end the drag.
163
+ useEffect(() => () => dragRef.current?.endDrag(), [])
164
+ const layout = useValue(
165
+ 'region handle layout',
166
+ () => {
167
+ const zoom = editor.getZoomLevel()
168
+ const sizing = selectionCornerSizing(
169
+ box.w * zoom,
170
+ box.h * zoom,
171
+ editor.getInstanceState().isCoarsePointer
172
+ )
173
+ const dragged = dragRef.current?.handle
174
+ const points = handles
175
+ // The corner being dragged stays, even as the box shrinks to a size that hides it:
176
+ // unmounting it would drop its pointer capture and strand the resize.
177
+ .filter((h) => (h.x === dragged?.x && h.y === dragged?.y) || !isHiddenCorner(h, sizing))
178
+ .map((h) => {
179
+ const p = editor.pageToViewport({ x: box.x + h.x * box.w, y: box.y + h.y * box.h })
180
+ return { ...h, key: `${h.x}-${h.y}`, left: p.x, top: p.y }
181
+ })
182
+ // tldraw's own resize cursors (the editor's, in the theme's cursor colour), not the
183
+ // browser's native ones.
184
+ const { cursor: cursorColor } = editor.getCurrentTheme().colors[editor.getColorMode()]
185
+ const cursors = {
186
+ 'nwse-resize': getCursor('nwse-resize', 0, cursorColor),
187
+ 'nesw-resize': getCursor('nesw-resize', 0, cursorColor),
188
+ }
189
+ return { points, hitSize: sizing.hitSize, cursors }
190
+ },
90
191
  [editor, box.x, box.y, box.w, box.h, handles]
91
192
  )
92
- const startResize = (e: ReactPointerEvent<HTMLDivElement>) => {
193
+ const { points, hitSize, cursors } = layout
194
+ const resized = () => {
195
+ const drag = dragRef.current!
196
+ return resizeRegion(
197
+ drag.box,
198
+ drag.handle,
199
+ editor.screenToPage({ x: drag.clientX, y: drag.clientY })
200
+ )
201
+ }
202
+ const startResize = (h: RegionHandle) => (e: ReactPointerEvent<HTMLDivElement>) => {
203
+ // A middle/right-button or space-held press is a camera pan, not a resize.
204
+ if (isCanvasPanGesture(editor, e)) {
205
+ forwardPointerEventToCanvas(editor.getContainer(), e)
206
+ return
207
+ }
93
208
  e.stopPropagation()
94
- boxRef.current = box
209
+ dragRef.current?.endDrag()
210
+ dragRef.current = {
211
+ box,
212
+ handle: h,
213
+ clientX: e.clientX,
214
+ clientY: e.clientY,
215
+ endDrag: startCommentDrag(editor, {
216
+ cursor: h.cursor,
217
+ onCameraMove: () => {
218
+ if (dragRef.current) onPreview(resized())
219
+ },
220
+ }),
221
+ }
95
222
  e.currentTarget.setPointerCapture(e.pointerId)
96
223
  }
97
- const resizedTo = (h: RegionHandle, e: ReactPointerEvent<HTMLDivElement>): BoxModel =>
98
- resizeRegion(boxRef.current!, h, editor.screenToPage({ x: e.clientX, y: e.clientY }))
99
- const onResize = (h: RegionHandle) => (e: ReactPointerEvent<HTMLDivElement>) => {
100
- if (boxRef.current) onPreview(resizedTo(h, e))
224
+ const onResize = (e: ReactPointerEvent<HTMLDivElement>) => {
225
+ const drag = dragRef.current
226
+ if (!drag) return
227
+ drag.clientX = e.clientX
228
+ drag.clientY = e.clientY
229
+ onPreview(resized())
101
230
  }
102
- const endResize = (h: RegionHandle) => (e: ReactPointerEvent<HTMLDivElement>) => {
103
- if (!boxRef.current) return
104
- const bounds = resizedTo(h, e)
105
- boxRef.current = null
231
+ const endResize = (e: ReactPointerEvent<HTMLDivElement>) => {
232
+ const drag = dragRef.current
233
+ if (!drag) return
234
+ drag.clientX = e.clientX
235
+ drag.clientY = e.clientY
236
+ const bounds = resized()
237
+ dragRef.current = null
238
+ drag.endDrag()
106
239
  if (e.currentTarget.hasPointerCapture(e.pointerId))
107
240
  e.currentTarget.releasePointerCapture(e.pointerId)
108
241
  onCommit(bounds)
109
242
  }
243
+ // A cancelled pointer abandons the resize: the region snaps back to its stored bounds.
244
+ const cancelResize = () => {
245
+ const drag = dragRef.current
246
+ if (!drag) return
247
+ dragRef.current = null
248
+ drag.endDrag()
249
+ onPreview(null)
250
+ }
110
251
  return (
111
- <>
252
+ // A wheel over a handle (even mid-resize) scrolls the canvas, like everywhere else on it.
253
+ <div ref={wheelRef} className="tlui-cmt-canvas-region-handles">
112
254
  {points.map((h) => (
113
255
  <div
114
256
  key={h.key}
115
257
  className="tlui-cmt-canvas-region-handle"
116
- style={{ left: h.left, top: h.top, cursor: h.cursor }}
117
- onPointerDown={startResize}
118
- onPointerMove={onResize(h)}
119
- onPointerUp={endResize(h)}
258
+ style={{
259
+ left: h.left,
260
+ top: h.top,
261
+ width: hitSize,
262
+ height: hitSize,
263
+ cursor: cursors[h.cursor],
264
+ }}
265
+ onPointerDown={startResize(h)}
266
+ onPointerMove={onResize}
267
+ onPointerUp={endResize}
268
+ onPointerCancel={cancelResize}
269
+ onLostPointerCapture={cancelResize}
120
270
  />
121
271
  ))}
122
- </>
272
+ </div>
123
273
  )
124
274
  }
@@ -18,7 +18,7 @@ import {
18
18
  useValue,
19
19
  } from 'tldraw'
20
20
  import { CommentPin } from '../ui/comment-pin'
21
- import { forwardPointerEventToCanvas, isCanvasPanGesture } from './canvas-events'
21
+ import { forwardPointerEventToCanvas, isCanvasPanGesture, startCommentDrag } from './canvas-events'
22
22
  import { commitCommentMutation } from './comment-mutations'
23
23
  import { UNKNOWN_COMMENT_AUTHOR } from './comment-render'
24
24
  import { type CommentingContext } from './context'
@@ -45,7 +45,7 @@ import {
45
45
  regionWithPinAt,
46
46
  shapeAnchorAt,
47
47
  } from './thread-state'
48
- import { POPOVER_OFFSET, ThreadPopover, ThreadView } from './thread-view'
48
+ import { avatarRenderer, POPOVER_OFFSET, ThreadPopover, ThreadView } from './thread-view'
49
49
 
50
50
  const messages = defineMessages({
51
51
  commentsUnknownAuthor: { id: 'comments.unknown-author', defaultMessage: 'Someone' },
@@ -129,6 +129,9 @@ export const ThreadPin = memo(function ThreadPin({
129
129
  // cursor's delta (like RegionBox's move) instead of snapping the anchor to the cursor.
130
130
  offsetX: number
131
131
  offsetY: number
132
+ clientX: number
133
+ clientY: number
134
+ endDrag?(): void
132
135
  } | null>(null)
133
136
  const markerRef = useRef<HTMLButtonElement>(null)
134
137
  // Wheel pass-through sits on the marker (which is never scrollable), not the layer root —
@@ -140,7 +143,9 @@ export const ThreadPin = memo(function ThreadPin({
140
143
  // clear the hint here or it stays on the shape indefinitely.
141
144
  useEffect(() => {
142
145
  return () => {
143
- if (dragRef.current) editor.setHintingShapes([])
146
+ if (!dragRef.current) return
147
+ editor.setHintingShapes([])
148
+ dragRef.current.endDrag?.()
144
149
  }
145
150
  }, [editor])
146
151
 
@@ -199,10 +204,13 @@ export const ThreadPin = memo(function ThreadPin({
199
204
 
200
205
  const PinContent = options.components.PinContent
201
206
  const threadAuthor = resolveAuthor(thread.createdBy)
207
+ const pinAuthor = threadAuthor ?? UNKNOWN_COMMENT_AUTHOR
202
208
  const pinContent = PinContent ? (
203
209
  <PinContent thread={thread} comments={comments} />
204
210
  ) : (
205
- <Avatar author={threadAuthor ?? UNKNOWN_COMMENT_AUTHOR} />
211
+ (avatarRenderer(options.components, thread.createdBy)?.(pinAuthor) ?? (
212
+ <Avatar author={pinAuthor} />
213
+ ))
206
214
  )
207
215
  const pinLabel = msg(
208
216
  thread.resolved ? messages.commentsPinLabelResolved : messages.commentsPinLabel,
@@ -236,6 +244,8 @@ export const ThreadPin = memo(function ThreadPin({
236
244
  dragRef.current = {
237
245
  startX: e.clientX,
238
246
  startY: e.clientY,
247
+ clientX: e.clientX,
248
+ clientY: e.clientY,
239
249
  moved: false,
240
250
  offsetX: anchorPage ? anchorPage.x - grabPage.x : 0,
241
251
  offsetY: anchorPage ? anchorPage.y - grabPage.y : 0,
@@ -249,8 +259,19 @@ export const ThreadPin = memo(function ThreadPin({
249
259
  // press stays a click (`moved` never sets, so release toggles the popover and never commits).
250
260
  if (!canComment) return
251
261
  if (!drag.moved && Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < 4) return
252
- drag.moved = true
253
- const cursorPage = editor.screenToPage({ x: e.clientX, y: e.clientY })
262
+ drag.clientX = e.clientX
263
+ drag.clientY = e.clientY
264
+ if (!drag.moved) {
265
+ drag.moved = true
266
+ drag.endDrag = startCommentDrag(editor, { cursor: 'move', onCameraMove: dragToPointer })
267
+ }
268
+ dragToPointer()
269
+ }
270
+ // Place the pin under the drag's last pointer position, at the current camera.
271
+ const dragToPointer = () => {
272
+ const drag = dragRef.current
273
+ if (!drag) return
274
+ const cursorPage = editor.screenToPage({ x: drag.clientX, y: drag.clientY })
254
275
  const pagePoint = { x: cursorPage.x + drag.offsetX, y: cursorPage.y + drag.offsetY }
255
276
  setDragPagePoint(pagePoint)
256
277
  // Hint the shape the pin would re-anchor to on drop — the same hit-test endDrag resolves
@@ -264,6 +285,7 @@ export const ThreadPin = memo(function ThreadPin({
264
285
  const takeDrag = (e: ReactPointerEvent<HTMLButtonElement>) => {
265
286
  const drag = dragRef.current
266
287
  dragRef.current = null
288
+ drag?.endDrag?.()
267
289
  if (e.currentTarget.hasPointerCapture(e.pointerId)) {
268
290
  e.currentTarget.releasePointerCapture(e.pointerId)
269
291
  }