@tldraw/commenting 5.7.0-canary.ea40e3961399 → 5.7.0-canary.ed10ee6bcceb

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 (102) hide show
  1. package/commenting.css +277 -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-tool.js +29 -28
  5. package/dist-cjs/canvas/comment-tool.js.map +2 -2
  6. package/dist-cjs/canvas/comments-filter-menu.js +55 -38
  7. package/dist-cjs/canvas/comments-filter-menu.js.map +2 -2
  8. package/dist-cjs/canvas/comments-overflow-menu.js +6 -24
  9. package/dist-cjs/canvas/comments-overflow-menu.js.map +2 -2
  10. package/dist-cjs/canvas/comments-overlay.js +1 -0
  11. package/dist-cjs/canvas/comments-overlay.js.map +2 -2
  12. package/dist-cjs/canvas/comments-sidebar.js +15 -3
  13. package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
  14. package/dist-cjs/canvas/comments-visibility-toggle.js +1 -1
  15. package/dist-cjs/canvas/comments-visibility-toggle.js.map +1 -1
  16. package/dist-cjs/canvas/options.js.map +2 -2
  17. package/dist-cjs/canvas/pending-composer.js +133 -8
  18. package/dist-cjs/canvas/pending-composer.js.map +2 -2
  19. package/dist-cjs/canvas/region-box.js +150 -35
  20. package/dist-cjs/canvas/region-box.js.map +3 -3
  21. package/dist-cjs/canvas/thread-pin.js +20 -4
  22. package/dist-cjs/canvas/thread-pin.js.map +2 -2
  23. package/dist-cjs/canvas/thread-view.js +130 -57
  24. package/dist-cjs/canvas/thread-view.js.map +2 -2
  25. package/dist-cjs/index.d.ts +39 -11
  26. package/dist-cjs/index.js +1 -1
  27. package/dist-cjs/ui/byline.js +2 -2
  28. package/dist-cjs/ui/byline.js.map +2 -2
  29. package/dist-cjs/ui/comment-card.js +3 -1
  30. package/dist-cjs/ui/comment-card.js.map +2 -2
  31. package/dist-cjs/ui/comment-extensions.js +4 -1
  32. package/dist-cjs/ui/comment-extensions.js.map +2 -2
  33. package/dist-cjs/ui/comments-list.js +6 -7
  34. package/dist-cjs/ui/comments-list.js.map +2 -2
  35. package/dist-cjs/ui/icons.js +4 -4
  36. package/dist-cjs/ui/icons.js.map +2 -2
  37. package/dist-cjs/ui/reaction-picker.js +2 -2
  38. package/dist-cjs/ui/reaction-picker.js.map +1 -1
  39. package/dist-cjs/ui/send-button.js.map +1 -1
  40. package/dist-esm/canvas/canvas-events.mjs +25 -1
  41. package/dist-esm/canvas/canvas-events.mjs.map +2 -2
  42. package/dist-esm/canvas/comment-tool.mjs +30 -29
  43. package/dist-esm/canvas/comment-tool.mjs.map +2 -2
  44. package/dist-esm/canvas/comments-filter-menu.mjs +57 -39
  45. package/dist-esm/canvas/comments-filter-menu.mjs.map +2 -2
  46. package/dist-esm/canvas/comments-overflow-menu.mjs +8 -24
  47. package/dist-esm/canvas/comments-overflow-menu.mjs.map +2 -2
  48. package/dist-esm/canvas/comments-overlay.mjs +1 -0
  49. package/dist-esm/canvas/comments-overlay.mjs.map +2 -2
  50. package/dist-esm/canvas/comments-sidebar.mjs +16 -4
  51. package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
  52. package/dist-esm/canvas/comments-visibility-toggle.mjs +1 -1
  53. package/dist-esm/canvas/comments-visibility-toggle.mjs.map +1 -1
  54. package/dist-esm/canvas/options.mjs.map +2 -2
  55. package/dist-esm/canvas/pending-composer.mjs +143 -9
  56. package/dist-esm/canvas/pending-composer.mjs.map +2 -2
  57. package/dist-esm/canvas/region-box.mjs +161 -38
  58. package/dist-esm/canvas/region-box.mjs.map +3 -3
  59. package/dist-esm/canvas/thread-pin.mjs +22 -6
  60. package/dist-esm/canvas/thread-pin.mjs.map +2 -2
  61. package/dist-esm/canvas/thread-view.mjs +140 -58
  62. package/dist-esm/canvas/thread-view.mjs.map +2 -2
  63. package/dist-esm/index.d.mts +39 -11
  64. package/dist-esm/index.mjs +1 -1
  65. package/dist-esm/ui/byline.mjs +2 -2
  66. package/dist-esm/ui/byline.mjs.map +2 -2
  67. package/dist-esm/ui/comment-card.mjs +3 -1
  68. package/dist-esm/ui/comment-card.mjs.map +2 -2
  69. package/dist-esm/ui/comment-extensions.mjs +11 -2
  70. package/dist-esm/ui/comment-extensions.mjs.map +2 -2
  71. package/dist-esm/ui/comments-list.mjs +6 -7
  72. package/dist-esm/ui/comments-list.mjs.map +2 -2
  73. package/dist-esm/ui/icons.mjs +4 -4
  74. package/dist-esm/ui/icons.mjs.map +2 -2
  75. package/dist-esm/ui/reaction-picker.mjs +2 -2
  76. package/dist-esm/ui/reaction-picker.mjs.map +1 -1
  77. package/dist-esm/ui/send-button.mjs.map +1 -1
  78. package/package.json +5 -5
  79. package/src/canvas/canvas-events.ts +39 -2
  80. package/src/canvas/canvas.css +73 -38
  81. package/src/canvas/comment-render.test.ts +8 -1
  82. package/src/canvas/comment-tool.test.ts +34 -44
  83. package/src/canvas/comment-tool.tsx +51 -55
  84. package/src/canvas/comments-filter-menu.tsx +19 -6
  85. package/src/canvas/comments-overflow-menu.tsx +12 -18
  86. package/src/canvas/comments-overlay.tsx +5 -2
  87. package/src/canvas/comments-sidebar.tsx +20 -5
  88. package/src/canvas/comments-visibility-toggle.tsx +1 -1
  89. package/src/canvas/options.ts +21 -2
  90. package/src/canvas/pending-composer.tsx +182 -17
  91. package/src/canvas/region-box.tsx +192 -42
  92. package/src/canvas/thread-pin.tsx +28 -6
  93. package/src/canvas/thread-view.test.ts +39 -0
  94. package/src/canvas/thread-view.tsx +185 -70
  95. package/src/ui/byline.tsx +5 -2
  96. package/src/ui/comment-card.tsx +7 -1
  97. package/src/ui/comment-extensions.ts +18 -5
  98. package/src/ui/comments-list.tsx +23 -17
  99. package/src/ui/comments.css +175 -201
  100. package/src/ui/icons.tsx +5 -5
  101. package/src/ui/reaction-picker.tsx +2 -2
  102. package/src/ui/send-button.tsx +2 -2
@@ -1,12 +1,14 @@
1
1
  import {
2
2
  defineMessages,
3
3
  TldrawUiButton,
4
+ TldrawUiButtonLabel,
4
5
  TldrawUiDropdownMenuContent,
5
6
  TldrawUiDropdownMenuGroup,
6
7
  TldrawUiDropdownMenuItem,
7
8
  TldrawUiDropdownMenuRoot,
8
9
  TldrawUiDropdownMenuTrigger,
9
10
  TldrawUiIcon,
11
+ TldrawUiKbd,
10
12
  useEditor,
11
13
  useTranslation,
12
14
  useValue,
@@ -19,8 +21,8 @@ const messages = defineMessages({
19
21
  commentsShow: { id: 'comments.show', defaultMessage: 'Show comments' },
20
22
  })
21
23
 
22
- // A keyboard-shortcut glyph, not translatable copy — kept out of JSX as a constant.
23
- const HIDE_SHORTCUT = '⇧C'
24
+ // tldraw's kbd notation (`!` = shift), rendered per platform by TldrawUiKbd.
25
+ const HIDE_SHORTCUT = '!c'
24
26
 
25
27
  /** The overflow (⋯) dropdown in the sidebar header. For now it holds the hide/show-comments
26
28
  * toggle; it's the home for later comment-wide controls (notifications, mark all as read).
@@ -37,28 +39,20 @@ export function CommentsOverflowMenu() {
37
39
  type="icon"
38
40
  tooltip={msg(messages.commentsMoreOptions)}
39
41
  title={msg(messages.commentsMoreOptions)}
40
- className="tlui-cmt-header-btn"
42
+ className="tlui-cmt-icon-btn"
41
43
  >
42
44
  <TldrawUiIcon icon="dots-vertical" label={msg(messages.commentsMoreOptions)} small />
43
45
  </TldrawUiButton>
44
46
  </TldrawUiDropdownMenuTrigger>
45
- <TldrawUiDropdownMenuContent
46
- className="tlui-cmt-menu"
47
- side="bottom"
48
- align="end"
49
- alignOffset={0}
50
- sideOffset={4}
51
- >
47
+ <TldrawUiDropdownMenuContent side="bottom" align="end" alignOffset={0} sideOffset={0}>
52
48
  <TldrawUiDropdownMenuGroup>
53
49
  <TldrawUiDropdownMenuItem>
54
- <button
55
- type="button"
56
- className="tlui-cmt-menu-item"
57
- onClick={() => toggleCommentsHidden(editor)}
58
- >
59
- <span>{hidden ? msg(messages.commentsShow) : msg(messages.commentsHide)}</span>
60
- <span className="tlui-cmt-menu-item__shortcut">{HIDE_SHORTCUT}</span>
61
- </button>
50
+ <TldrawUiButton type="menu" onClick={() => toggleCommentsHidden(editor)}>
51
+ <TldrawUiButtonLabel>
52
+ {hidden ? msg(messages.commentsShow) : msg(messages.commentsHide)}
53
+ </TldrawUiButtonLabel>
54
+ <TldrawUiKbd>{HIDE_SHORTCUT}</TldrawUiKbd>
55
+ </TldrawUiButton>
62
56
  </TldrawUiDropdownMenuItem>
63
57
  </TldrawUiDropdownMenuGroup>
64
58
  </TldrawUiDropdownMenuContent>
@@ -86,8 +86,8 @@ function CanvasCommentsLayer(props: CommentingContext) {
86
86
  const canComment = useCanComment(props.currentUserId)
87
87
  // Nothing renders a pending comment when composing is blocked and there's no fallback slot, and
88
88
  // the dismiss handlers live inside PendingComposer — so clear the atom rather than strand it,
89
- // and leave the tool too: it holds while a composer is open, so staying would turn every click
90
- // into a silently swallowed placement.
89
+ // and leave the tool too: it stays active after a placement, so every click would be a
90
+ // silently swallowed one.
91
91
  const canRenderComposer = canComment || options.components.ComposerFallback != null
92
92
  const showPendingComposer = pending != null && canRenderComposer
93
93
  useEffect(() => {
@@ -223,6 +223,9 @@ function CanvasCommentsLayer(props: CommentingContext) {
223
223
  openThreadId.set(editor, null)
224
224
  e.preventDefault()
225
225
  e.stopPropagation()
226
+ // Closing from inside the thread would leave focus on the page, out of reach of the next
227
+ // Escape (the editor listens on its container).
228
+ if (target && target.closest('.tlui-cmt-thread')) editor.getContainer().focus()
226
229
  }
227
230
  document.addEventListener('keydown', onKeyDown, true)
228
231
  return () => document.removeEventListener('keydown', onKeyDown, true)
@@ -2,6 +2,7 @@ import { ReactNode, useCallback } from 'react'
2
2
  import {
3
3
  defineMessages,
4
4
  EditorPortal,
5
+ TLComment,
5
6
  TLCommentThread,
6
7
  useEditor,
7
8
  useTranslation,
@@ -16,8 +17,11 @@ import { getCommentsByThread, useCommentThreads } from './hooks'
16
17
  import { useCommentingEnabled } from './license'
17
18
  import { useCommentingOptions } from './options'
18
19
  import { richTextToPlaintext } from './rich-text'
19
- import { commentsSidebarOpen, openThreadId, sidebarFilters } from './state'
20
+ import { commentsHidden, commentsSidebarOpen, openThreadId, sidebarFilters } from './state'
20
21
  import { focusThread } from './thread-state'
22
+ import { avatarRenderer } from './thread-view'
23
+
24
+ const EMPTY_COMMENTS: TLComment[] = []
21
25
 
22
26
  const messages = defineMessages({
23
27
  commentsEmpty: { id: 'comments.empty', defaultMessage: 'No comments on this page yet.' },
@@ -127,6 +131,7 @@ export function CanvasCommentsSidebar(props: CanvasCommentsSidebarProps) {
127
131
  count: threadComments.length,
128
132
  selected: openId === thread.id,
129
133
  href: getThreadHref?.(thread.id),
134
+ renderAvatar: avatarRenderer(options.components, thread.createdBy),
130
135
  },
131
136
  lastActivity: (last ?? thread).createdAt,
132
137
  }
@@ -136,11 +141,15 @@ export function CanvasCommentsSidebar(props: CanvasCommentsSidebarProps) {
136
141
 
137
142
  const focus = (id: string) => {
138
143
  const thread = threads.find((t) => t.id === id)
139
- if (thread) focusThread(editor, thread)
144
+ if (!thread) return
145
+ // The thread popover lives on the layer that hiding withholds, so it would open invisibly.
146
+ commentsHidden.set(editor, false)
147
+ focusThread(editor, thread)
140
148
  }
141
149
 
142
- // The `ThreadRow` component slot overrides the built-in row. It gets the thread record alongside
143
- // the summarized row, so host state on `thread.meta` is in reach without a second lookup. Indexed
150
+ // The `ThreadRow` component slot overrides the built-in row. It gets the thread record and its
151
+ // comments alongside the summarized row, so host state on their `meta` is in reach without each
152
+ // row subscribing to the store again. Indexed
144
153
  // once rather than scanned per row — this runs for every row on every render of the panel.
145
154
  const ThreadRow = options.components.ThreadRow
146
155
  const renderItem = ThreadRow
@@ -154,7 +163,13 @@ export function CanvasCommentsSidebar(props: CanvasCommentsSidebarProps) {
154
163
  // Can't happen — the items are built from these threads — but a row with no thread has
155
164
  // nothing to render, and the slot's contract says the record is there.
156
165
  if (!thread) return null
157
- return <ThreadRow {...rowProps} thread={thread} />
166
+ return (
167
+ <ThreadRow
168
+ {...rowProps}
169
+ thread={thread}
170
+ comments={byThread.get(thread.id) ?? EMPTY_COMMENTS}
171
+ />
172
+ )
158
173
  }
159
174
  })()
160
175
  : undefined
@@ -21,7 +21,7 @@ export function CommentsVisibilityToggle() {
21
21
  type="icon"
22
22
  tooltip={label}
23
23
  title={label}
24
- className="tlui-cmt-header-btn"
24
+ className="tlui-cmt-icon-btn"
25
25
  onClick={() => toggleCommentsHidden(editor)}
26
26
  >
27
27
  {hidden ? <EyeClosedIcon /> : <EyeOpenIcon />}
@@ -1,3 +1,4 @@
1
+ import { type AvatarProps } from '@tldraw/mentions'
1
2
  import { useMemo, type ComponentType } from 'react'
2
3
  import {
3
4
  type Editor,
@@ -9,6 +10,7 @@ import {
9
10
  useEditor,
10
11
  useValue,
11
12
  } from 'tldraw'
13
+ import { type BylineProps } from '../ui/byline'
12
14
  import { type CommentListItemRenderProps } from '../ui/comments-list'
13
15
  import { isAllowedReactionEmoji, type EmojiPickerProps } from '../ui/emoji-picker'
14
16
  import { type ReactionTooltipProps } from '../ui/reaction'
@@ -60,6 +62,20 @@ export type CommentModificationContext = {
60
62
  export interface CommentingComponents {
61
63
  /** A comment's body. Replaces the default rich-text `<CommentBody>`. */
62
64
  CommentBody?: ComponentType<{ comment: TLComment }>
65
+ /**
66
+ * A comment's byline: the author's avatar and name, the time, and the edited marker. Replaces the
67
+ * default `<Byline>`, which is exported — so a byline that only changes how the author is shown
68
+ * (a badge read from `comment.meta`, say) can spread these props into it with a different
69
+ * `author`. `author` is what `resolveAuthor` returned for the comment's `authorId`.
70
+ */
71
+ CommentByline?: ComponentType<BylineProps & { comment: TLComment }>
72
+ /**
73
+ * An author's avatar, wherever the layer draws one: comment bylines, sidebar rows, the composers,
74
+ * and the default pin content. Replaces the default `<Avatar>`, which is exported — so an avatar
75
+ * that only adds something (a presence ring, a badge) can spread these props into it. `authorId`
76
+ * is the id `author` was resolved from, or `null` in a composer with no signed-in user.
77
+ */
78
+ Avatar?: ComponentType<AvatarProps & { authorId: string | null }>
63
79
  /** A pin's inner content. Replaces the author-initial default. */
64
80
  PinContent?: ComponentType<{ thread: TLCommentThread; comments: TLComment[] }>
65
81
  /** A sidebar row's preview. Replaces the plaintext default. */
@@ -67,9 +83,12 @@ export interface CommentingComponents {
67
83
  /**
68
84
  * A whole sidebar row. Replaces the default `<CommentListItem>`, which is exported — so a row
69
85
  * that only adds an unread dot or a status chip can spread these props into it. Use
70
- * `ThreadPreview` instead when only the preview text is changing.
86
+ * `ThreadPreview` instead when only the preview text is changing. `comments` are the thread's
87
+ * live comments, oldest first.
71
88
  */
72
- ThreadRow?: ComponentType<CommentListItemRenderProps & { thread: TLCommentThread }>
89
+ ThreadRow?: ComponentType<
90
+ CommentListItemRenderProps & { thread: TLCommentThread; comments: TLComment[] }
91
+ >
73
92
  /**
74
93
  * Extra controls in an open thread's header, added ahead of the built-in resolve and dismiss
75
94
  * buttons rather than replacing them. "Copy link" is already built in whenever the host supplies
@@ -1,19 +1,28 @@
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,
@@ -22,11 +31,18 @@ import {
22
31
  } from './comment-drafts'
23
32
  import { commitCommentMutation } 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,26 @@ 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
+ // Handle Escape before it reaches the editor's container so closing the composer doesn't
107
+ // also cancel the comment tool.
108
+ useEffect(() => {
109
+ const el = ref.current
110
+ if (!el) return
111
+ const onKeyDown = (e: KeyboardEvent) => {
112
+ if (e.key !== 'Escape' || isMentionPickerOpen()) return
113
+ e.stopPropagation()
114
+ setText(EMPTY_COMMENT)
115
+ clearCommentDraft(NEW_COMMENT_DRAFT)
116
+ pendingComment.set(editor, null)
117
+ // Focus would drop to the page with the composer gone, out of reach of the next Escape.
118
+ editor.getContainer().focus()
119
+ }
120
+ el.addEventListener('keydown', onKeyDown)
121
+ return () => el.removeEventListener('keydown', onKeyDown)
122
+ }, [editor])
123
+
80
124
  // Dismiss on a click anywhere outside the composer (capture-phase, ahead of stopPropagation).
81
125
  useEffect(() => {
82
126
  const onPointerDown = (e: PointerEvent) => {
@@ -86,9 +130,8 @@ export function PendingComposer({
86
130
  // A click in the composer, or in the mention picker it spawns (portaled elsewhere), is
87
131
  // not "outside" — keep the draft open so the pick can insert.
88
132
  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
133
+ // Panning the camera to look around while writing isn't leaving the draft.
134
+ if (isCanvasPanGesture(editor, e) || (e as any).isSpecialRedispatchedEvent) return
92
135
  pendingComment.set(editor, null)
93
136
  }
94
137
  document.addEventListener('pointerdown', onPointerDown, true)
@@ -116,10 +159,8 @@ export function PendingComposer({
116
159
  setText(EMPTY_COMMENT)
117
160
  clearCommentDraft(NEW_COMMENT_DRAFT)
118
161
  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')
162
+ // Removing the focused composer would leave Escape outside the editor's keyboard listener.
163
+ editor.getContainer().focus()
123
164
  // The host's callback is its own operation, not part of the post's history scope. It runs
124
165
  // last so a throwing host can't strand the composer holding a draft of a posted comment.
125
166
  onPostComment?.(comment)
@@ -132,6 +173,7 @@ export function PendingComposer({
132
173
  className={[
133
174
  'tlui-cmt-canvas-composer',
134
175
  pending.anchor.type === 'region' && 'tlui-cmt-canvas-composer--region',
176
+ drag.dragging && 'tlui-cmt-canvas-composer--dragging',
135
177
  !canComment && 'tlui-cmt-canvas-composer--fallback',
136
178
  ]
137
179
  .filter(Boolean)
@@ -139,9 +181,6 @@ export function PendingComposer({
139
181
  style={{ left: placed.left, top: placed.top }}
140
182
  onPointerDown={stop}
141
183
  onContextMenu={stop}
142
- onKeyDown={(e) => {
143
- if (e.key === 'Escape' && !isMentionPickerOpen()) pendingComment.set(editor, null)
144
- }}
145
184
  >
146
185
  {canComment ? (
147
186
  <CommentComposer
@@ -168,3 +207,129 @@ export function PendingComposer({
168
207
  </EditorPortal>
169
208
  )
170
209
  }
210
+
211
+ /** Drag the draft by its avatar pin, the way a placed pin drags: the composer follows the pointer
212
+ * and, on release, re-anchors to the shape under it (a region translates, keeping its size). */
213
+ function useDraftDrag(editor: Editor, pending: PendingComment) {
214
+ const [dragging, setDragging] = useState(false)
215
+ const dragRef = useRef<{
216
+ startX: number
217
+ startY: number
218
+ moved: boolean
219
+ // The pin's page offset from the grab point, so the pin keeps its place under the pointer.
220
+ offsetX: number
221
+ offsetY: number
222
+ clientX: number
223
+ clientY: number
224
+ endDrag?(): void
225
+ } | null>(null)
226
+ // Unmounting mid-drag (the draft is dismissed) gets no pointer up to end the drag, and the
227
+ // drop-target hint is editor-global state that would otherwise stay on the shape.
228
+ useEffect(
229
+ () => () => {
230
+ if (!dragRef.current) return
231
+ dragRef.current.endDrag?.()
232
+ editor.setHintingShapes([])
233
+ },
234
+ [editor]
235
+ )
236
+
237
+ const pagePointFor = (clientX: number, clientY: number) => {
238
+ const drag = dragRef.current!
239
+ const cursor = editor.screenToPage({ x: clientX, y: clientY })
240
+ return { x: cursor.x + drag.offsetX, y: cursor.y + drag.offsetY }
241
+ }
242
+
243
+ // A region moves as a whole, its box riding with the pin corner; a point draft moves only its
244
+ // composer until the drop, when its anchor resolves against what's under it.
245
+ const moveRegion = (anchor: Extract<TLCommentAnchor, { type: 'region' }>, point: VecLike) => {
246
+ const corner = regionAnchorPinCorner(anchor)
247
+ const moved = regionWithPinAt(anchor, corner, point)
248
+ pendingComment.set(editor, { anchor: moved, point: regionPinPoint(moved, corner) })
249
+ }
250
+
251
+ // Mid-drag, a point draft hints the shape a drop here would anchor to.
252
+ const previewAt = (point: VecLike) => {
253
+ const current = pendingComment.get(editor)
254
+ if (!current) return
255
+ if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
256
+ pendingComment.set(editor, { ...current, point: { x: point.x, y: point.y } })
257
+ const hit = commentTargetShapeAt(editor, point)
258
+ editor.setHintingShapes(hit ? [hit.id] : [])
259
+ }
260
+
261
+ const settleAt = (point: VecLike, altKey: boolean) => {
262
+ const current = pendingComment.get(editor)
263
+ if (!current) return
264
+ if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
265
+ pendingComment.set(editor, pendingCommentAt(editor, point, altKey))
266
+ }
267
+
268
+ const takeDrag = (e: ReactPointerEvent<HTMLDivElement>) => {
269
+ const drag = dragRef.current
270
+ dragRef.current = null
271
+ drag?.endDrag?.()
272
+ editor.setHintingShapes([])
273
+ setDragging(false)
274
+ if (e.currentTarget.hasPointerCapture(e.pointerId)) {
275
+ e.currentTarget.releasePointerCapture(e.pointerId)
276
+ }
277
+ return drag
278
+ }
279
+
280
+ const handlers = {
281
+ onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
282
+ // A middle/right-button or space-held press is a camera pan, not a drag.
283
+ if (isCanvasPanGesture(editor, e)) {
284
+ forwardPointerEventToCanvas(editor.getContainer(), e)
285
+ return
286
+ }
287
+ const grab = editor.screenToPage({ x: e.clientX, y: e.clientY })
288
+ dragRef.current = {
289
+ startX: e.clientX,
290
+ startY: e.clientY,
291
+ clientX: e.clientX,
292
+ clientY: e.clientY,
293
+ moved: false,
294
+ offsetX: pending.point.x - grab.x,
295
+ offsetY: pending.point.y - grab.y,
296
+ }
297
+ e.currentTarget.setPointerCapture(e.pointerId)
298
+ },
299
+ // Keep focus in the comment field: grabbing the pin shouldn't blur what's being typed.
300
+ onMouseDown: (e: ReactMouseEvent) => e.preventDefault(),
301
+ onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
302
+ const drag = dragRef.current
303
+ if (!drag) return
304
+ if (!drag.moved && Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < 4) return
305
+ drag.clientX = e.clientX
306
+ drag.clientY = e.clientY
307
+ if (!drag.moved) {
308
+ drag.moved = true
309
+ drag.endDrag = startCommentDrag(editor, {
310
+ cursor: 'move',
311
+ onCameraMove: () => {
312
+ const d = dragRef.current
313
+ if (d) previewAt(pagePointFor(d.clientX, d.clientY))
314
+ },
315
+ })
316
+ setDragging(true)
317
+ }
318
+ previewAt(pagePointFor(e.clientX, e.clientY))
319
+ },
320
+ onPointerUp: (e: ReactPointerEvent<HTMLDivElement>) => {
321
+ const moved = dragRef.current?.moved
322
+ const point = moved ? pagePointFor(e.clientX, e.clientY) : null
323
+ takeDrag(e)
324
+ if (point) settleAt(point, e.altKey)
325
+ },
326
+ // A cancelled pointer leaves the draft where the drag last put it, with its anchor resolved.
327
+ onPointerCancel: (e: ReactPointerEvent<HTMLDivElement>) => {
328
+ const drag = takeDrag(e)
329
+ const current = pendingComment.get(editor)
330
+ if (drag?.moved && current) settleAt(current.point, editor.inputs.getAltKey())
331
+ },
332
+ }
333
+
334
+ return { dragging, handlers }
335
+ }