@tldraw/commenting 5.7.0-canary.ccf6cccc22cd → 5.7.0-canary.cd237d7ba6e3
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.
- package/commenting.css +280 -275
- package/dist-cjs/canvas/canvas-events.js +25 -1
- package/dist-cjs/canvas/canvas-events.js.map +2 -2
- package/dist-cjs/canvas/comment-tool.js +29 -28
- package/dist-cjs/canvas/comment-tool.js.map +2 -2
- package/dist-cjs/canvas/comments-filter-menu.js +55 -38
- package/dist-cjs/canvas/comments-filter-menu.js.map +2 -2
- package/dist-cjs/canvas/comments-overflow-menu.js +6 -24
- package/dist-cjs/canvas/comments-overflow-menu.js.map +2 -2
- package/dist-cjs/canvas/comments-overlay.js +1 -0
- package/dist-cjs/canvas/comments-overlay.js.map +2 -2
- package/dist-cjs/canvas/comments-sidebar.js +15 -3
- package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
- package/dist-cjs/canvas/comments-visibility-toggle.js +1 -1
- package/dist-cjs/canvas/comments-visibility-toggle.js.map +1 -1
- package/dist-cjs/canvas/options.js.map +2 -2
- package/dist-cjs/canvas/pending-composer.js +137 -8
- package/dist-cjs/canvas/pending-composer.js.map +2 -2
- package/dist-cjs/canvas/region-box.js +150 -35
- package/dist-cjs/canvas/region-box.js.map +3 -3
- package/dist-cjs/canvas/thread-pin.js +20 -4
- package/dist-cjs/canvas/thread-pin.js.map +2 -2
- package/dist-cjs/canvas/thread-view.js +132 -57
- package/dist-cjs/canvas/thread-view.js.map +2 -2
- package/dist-cjs/index.d.ts +39 -11
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/ui/byline.js +2 -2
- package/dist-cjs/ui/byline.js.map +2 -2
- package/dist-cjs/ui/comment-card.js +3 -1
- package/dist-cjs/ui/comment-card.js.map +2 -2
- package/dist-cjs/ui/comment-extensions.js +4 -1
- package/dist-cjs/ui/comment-extensions.js.map +2 -2
- package/dist-cjs/ui/comments-list.js +6 -7
- package/dist-cjs/ui/comments-list.js.map +2 -2
- package/dist-cjs/ui/icons.js +4 -4
- package/dist-cjs/ui/icons.js.map +2 -2
- package/dist-cjs/ui/reaction-picker.js +2 -2
- package/dist-cjs/ui/reaction-picker.js.map +1 -1
- package/dist-cjs/ui/send-button.js.map +1 -1
- package/dist-esm/canvas/canvas-events.mjs +25 -1
- package/dist-esm/canvas/canvas-events.mjs.map +2 -2
- package/dist-esm/canvas/comment-tool.mjs +30 -29
- package/dist-esm/canvas/comment-tool.mjs.map +2 -2
- package/dist-esm/canvas/comments-filter-menu.mjs +57 -39
- package/dist-esm/canvas/comments-filter-menu.mjs.map +2 -2
- package/dist-esm/canvas/comments-overflow-menu.mjs +8 -24
- package/dist-esm/canvas/comments-overflow-menu.mjs.map +2 -2
- package/dist-esm/canvas/comments-overlay.mjs +1 -0
- package/dist-esm/canvas/comments-overlay.mjs.map +2 -2
- package/dist-esm/canvas/comments-sidebar.mjs +16 -4
- package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
- package/dist-esm/canvas/comments-visibility-toggle.mjs +1 -1
- package/dist-esm/canvas/comments-visibility-toggle.mjs.map +1 -1
- package/dist-esm/canvas/options.mjs.map +2 -2
- package/dist-esm/canvas/pending-composer.mjs +147 -9
- package/dist-esm/canvas/pending-composer.mjs.map +2 -2
- package/dist-esm/canvas/region-box.mjs +161 -38
- package/dist-esm/canvas/region-box.mjs.map +3 -3
- package/dist-esm/canvas/thread-pin.mjs +22 -6
- package/dist-esm/canvas/thread-pin.mjs.map +2 -2
- package/dist-esm/canvas/thread-view.mjs +142 -58
- package/dist-esm/canvas/thread-view.mjs.map +2 -2
- package/dist-esm/index.d.mts +39 -11
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/ui/byline.mjs +2 -2
- package/dist-esm/ui/byline.mjs.map +2 -2
- package/dist-esm/ui/comment-card.mjs +3 -1
- package/dist-esm/ui/comment-card.mjs.map +2 -2
- package/dist-esm/ui/comment-extensions.mjs +11 -2
- package/dist-esm/ui/comment-extensions.mjs.map +2 -2
- package/dist-esm/ui/comments-list.mjs +6 -7
- package/dist-esm/ui/comments-list.mjs.map +2 -2
- package/dist-esm/ui/icons.mjs +4 -4
- package/dist-esm/ui/icons.mjs.map +2 -2
- package/dist-esm/ui/reaction-picker.mjs +2 -2
- package/dist-esm/ui/reaction-picker.mjs.map +1 -1
- package/dist-esm/ui/send-button.mjs.map +1 -1
- package/package.json +5 -5
- package/src/canvas/canvas-events.ts +39 -2
- package/src/canvas/canvas.css +76 -38
- package/src/canvas/comment-render.test.ts +8 -1
- package/src/canvas/comment-tool.test.ts +34 -44
- package/src/canvas/comment-tool.tsx +51 -55
- package/src/canvas/comments-filter-menu.tsx +19 -6
- package/src/canvas/comments-overflow-menu.tsx +12 -18
- package/src/canvas/comments-overlay.tsx +5 -2
- package/src/canvas/comments-sidebar.tsx +20 -5
- package/src/canvas/comments-visibility-toggle.tsx +1 -1
- package/src/canvas/options.ts +21 -2
- package/src/canvas/pending-composer.tsx +186 -17
- package/src/canvas/region-box.tsx +192 -42
- package/src/canvas/thread-pin.tsx +28 -6
- package/src/canvas/thread-view.test.ts +39 -0
- package/src/canvas/thread-view.tsx +188 -70
- package/src/ui/byline.tsx +5 -2
- package/src/ui/comment-card.tsx +7 -1
- package/src/ui/comment-extensions.ts +18 -5
- package/src/ui/comments-list.tsx +23 -17
- package/src/ui/comments.css +175 -201
- package/src/ui/icons.tsx +5 -5
- package/src/ui/reaction-picker.tsx +2 -2
- 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
|
-
//
|
|
23
|
-
const HIDE_SHORTCUT = '
|
|
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-
|
|
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
|
-
<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
|
90
|
-
//
|
|
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)
|
|
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
|
|
143
|
-
// the summarized row, so host state on `
|
|
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
|
|
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-
|
|
24
|
+
className="tlui-cmt-icon-btn"
|
|
25
25
|
onClick={() => toggleCommentsHidden(editor)}
|
|
26
26
|
>
|
|
27
27
|
{hidden ? <EyeClosedIcon /> : <EyeOpenIcon />}
|
package/src/canvas/options.ts
CHANGED
|
@@ -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<
|
|
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 {
|
|
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
|
|
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
|
-
<
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
//
|
|
90
|
-
|
|
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)
|
|
@@ -116,10 +165,6 @@ export function PendingComposer({
|
|
|
116
165
|
setText(EMPTY_COMMENT)
|
|
117
166
|
clearCommentDraft(NEW_COMMENT_DRAFT)
|
|
118
167
|
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
168
|
// The host's callback is its own operation, not part of the post's history scope. It runs
|
|
124
169
|
// last so a throwing host can't strand the composer holding a draft of a posted comment.
|
|
125
170
|
onPostComment?.(comment)
|
|
@@ -132,6 +177,7 @@ export function PendingComposer({
|
|
|
132
177
|
className={[
|
|
133
178
|
'tlui-cmt-canvas-composer',
|
|
134
179
|
pending.anchor.type === 'region' && 'tlui-cmt-canvas-composer--region',
|
|
180
|
+
drag.dragging && 'tlui-cmt-canvas-composer--dragging',
|
|
135
181
|
!canComment && 'tlui-cmt-canvas-composer--fallback',
|
|
136
182
|
]
|
|
137
183
|
.filter(Boolean)
|
|
@@ -139,9 +185,6 @@ export function PendingComposer({
|
|
|
139
185
|
style={{ left: placed.left, top: placed.top }}
|
|
140
186
|
onPointerDown={stop}
|
|
141
187
|
onContextMenu={stop}
|
|
142
|
-
onKeyDown={(e) => {
|
|
143
|
-
if (e.key === 'Escape' && !isMentionPickerOpen()) pendingComment.set(editor, null)
|
|
144
|
-
}}
|
|
145
188
|
>
|
|
146
189
|
{canComment ? (
|
|
147
190
|
<CommentComposer
|
|
@@ -168,3 +211,129 @@ export function PendingComposer({
|
|
|
168
211
|
</EditorPortal>
|
|
169
212
|
)
|
|
170
213
|
}
|
|
214
|
+
|
|
215
|
+
/** Drag the draft by its avatar pin, the way a placed pin drags: the composer follows the pointer
|
|
216
|
+
* and, on release, re-anchors to the shape under it (a region translates, keeping its size). */
|
|
217
|
+
function useDraftDrag(editor: Editor, pending: PendingComment) {
|
|
218
|
+
const [dragging, setDragging] = useState(false)
|
|
219
|
+
const dragRef = useRef<{
|
|
220
|
+
startX: number
|
|
221
|
+
startY: number
|
|
222
|
+
moved: boolean
|
|
223
|
+
// The pin's page offset from the grab point, so the pin keeps its place under the pointer.
|
|
224
|
+
offsetX: number
|
|
225
|
+
offsetY: number
|
|
226
|
+
clientX: number
|
|
227
|
+
clientY: number
|
|
228
|
+
endDrag?(): void
|
|
229
|
+
} | null>(null)
|
|
230
|
+
// Unmounting mid-drag (the draft is dismissed) gets no pointer up to end the drag, and the
|
|
231
|
+
// drop-target hint is editor-global state that would otherwise stay on the shape.
|
|
232
|
+
useEffect(
|
|
233
|
+
() => () => {
|
|
234
|
+
if (!dragRef.current) return
|
|
235
|
+
dragRef.current.endDrag?.()
|
|
236
|
+
editor.setHintingShapes([])
|
|
237
|
+
},
|
|
238
|
+
[editor]
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
const pagePointFor = (clientX: number, clientY: number) => {
|
|
242
|
+
const drag = dragRef.current!
|
|
243
|
+
const cursor = editor.screenToPage({ x: clientX, y: clientY })
|
|
244
|
+
return { x: cursor.x + drag.offsetX, y: cursor.y + drag.offsetY }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// A region moves as a whole, its box riding with the pin corner; a point draft moves only its
|
|
248
|
+
// composer until the drop, when its anchor resolves against what's under it.
|
|
249
|
+
const moveRegion = (anchor: Extract<TLCommentAnchor, { type: 'region' }>, point: VecLike) => {
|
|
250
|
+
const corner = regionAnchorPinCorner(anchor)
|
|
251
|
+
const moved = regionWithPinAt(anchor, corner, point)
|
|
252
|
+
pendingComment.set(editor, { anchor: moved, point: regionPinPoint(moved, corner) })
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Mid-drag, a point draft hints the shape a drop here would anchor to.
|
|
256
|
+
const previewAt = (point: VecLike) => {
|
|
257
|
+
const current = pendingComment.get(editor)
|
|
258
|
+
if (!current) return
|
|
259
|
+
if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
|
|
260
|
+
pendingComment.set(editor, { ...current, point: { x: point.x, y: point.y } })
|
|
261
|
+
const hit = commentTargetShapeAt(editor, point)
|
|
262
|
+
editor.setHintingShapes(hit ? [hit.id] : [])
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const settleAt = (point: VecLike, altKey: boolean) => {
|
|
266
|
+
const current = pendingComment.get(editor)
|
|
267
|
+
if (!current) return
|
|
268
|
+
if (current.anchor.type === 'region') return moveRegion(current.anchor, point)
|
|
269
|
+
pendingComment.set(editor, pendingCommentAt(editor, point, altKey))
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const takeDrag = (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
273
|
+
const drag = dragRef.current
|
|
274
|
+
dragRef.current = null
|
|
275
|
+
drag?.endDrag?.()
|
|
276
|
+
editor.setHintingShapes([])
|
|
277
|
+
setDragging(false)
|
|
278
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
|
|
279
|
+
e.currentTarget.releasePointerCapture(e.pointerId)
|
|
280
|
+
}
|
|
281
|
+
return drag
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const handlers = {
|
|
285
|
+
onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
286
|
+
// A middle/right-button or space-held press is a camera pan, not a drag.
|
|
287
|
+
if (isCanvasPanGesture(editor, e)) {
|
|
288
|
+
forwardPointerEventToCanvas(editor.getContainer(), e)
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
const grab = editor.screenToPage({ x: e.clientX, y: e.clientY })
|
|
292
|
+
dragRef.current = {
|
|
293
|
+
startX: e.clientX,
|
|
294
|
+
startY: e.clientY,
|
|
295
|
+
clientX: e.clientX,
|
|
296
|
+
clientY: e.clientY,
|
|
297
|
+
moved: false,
|
|
298
|
+
offsetX: pending.point.x - grab.x,
|
|
299
|
+
offsetY: pending.point.y - grab.y,
|
|
300
|
+
}
|
|
301
|
+
e.currentTarget.setPointerCapture(e.pointerId)
|
|
302
|
+
},
|
|
303
|
+
// Keep focus in the comment field: grabbing the pin shouldn't blur what's being typed.
|
|
304
|
+
onMouseDown: (e: ReactMouseEvent) => e.preventDefault(),
|
|
305
|
+
onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
306
|
+
const drag = dragRef.current
|
|
307
|
+
if (!drag) return
|
|
308
|
+
if (!drag.moved && Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < 4) return
|
|
309
|
+
drag.clientX = e.clientX
|
|
310
|
+
drag.clientY = e.clientY
|
|
311
|
+
if (!drag.moved) {
|
|
312
|
+
drag.moved = true
|
|
313
|
+
drag.endDrag = startCommentDrag(editor, {
|
|
314
|
+
cursor: 'move',
|
|
315
|
+
onCameraMove: () => {
|
|
316
|
+
const d = dragRef.current
|
|
317
|
+
if (d) previewAt(pagePointFor(d.clientX, d.clientY))
|
|
318
|
+
},
|
|
319
|
+
})
|
|
320
|
+
setDragging(true)
|
|
321
|
+
}
|
|
322
|
+
previewAt(pagePointFor(e.clientX, e.clientY))
|
|
323
|
+
},
|
|
324
|
+
onPointerUp: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
325
|
+
const moved = dragRef.current?.moved
|
|
326
|
+
const point = moved ? pagePointFor(e.clientX, e.clientY) : null
|
|
327
|
+
takeDrag(e)
|
|
328
|
+
if (point) settleAt(point, e.altKey)
|
|
329
|
+
},
|
|
330
|
+
// A cancelled pointer leaves the draft where the drag last put it, with its anchor resolved.
|
|
331
|
+
onPointerCancel: (e: ReactPointerEvent<HTMLDivElement>) => {
|
|
332
|
+
const drag = takeDrag(e)
|
|
333
|
+
const current = pendingComment.get(editor)
|
|
334
|
+
if (drag?.moved && current) settleAt(current.point, editor.inputs.getAltKey())
|
|
335
|
+
},
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return { dragging, handlers }
|
|
339
|
+
}
|