@tldraw/commenting 5.6.0-canary.5bfc37740468 → 5.6.0-canary.5e46744c5daa
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/dist-cjs/canvas/comment-tool.js +31 -14
- package/dist-cjs/canvas/comment-tool.js.map +2 -2
- package/dist-cjs/canvas/pending-composer.js +1 -0
- package/dist-cjs/canvas/pending-composer.js.map +2 -2
- package/dist-cjs/index.d.ts +19 -3
- package/dist-cjs/index.js +2 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/ui/comment-composer.js +5 -0
- package/dist-cjs/ui/comment-composer.js.map +2 -2
- package/dist-cjs/ui/gesture-focus.js +37 -0
- package/dist-cjs/ui/gesture-focus.js.map +7 -0
- package/dist-esm/canvas/comment-tool.mjs +31 -14
- package/dist-esm/canvas/comment-tool.mjs.map +2 -2
- package/dist-esm/canvas/pending-composer.mjs +1 -0
- package/dist-esm/canvas/pending-composer.mjs.map +2 -2
- package/dist-esm/index.d.mts +19 -3
- package/dist-esm/index.mjs +8 -2
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/ui/comment-composer.mjs +5 -0
- package/dist-esm/ui/comment-composer.mjs.map +2 -2
- package/dist-esm/ui/gesture-focus.mjs +17 -0
- package/dist-esm/ui/gesture-focus.mjs.map +7 -0
- package/package.json +5 -5
- package/src/canvas/comment-tool.test.ts +72 -7
- package/src/canvas/comment-tool.tsx +55 -18
- package/src/canvas/pending-composer.tsx +3 -0
- package/src/index.ts +6 -1
- package/src/ui/comment-composer.tsx +9 -0
- package/src/ui/gesture-focus.test.ts +45 -0
- package/src/ui/gesture-focus.ts +29 -0
|
@@ -53,8 +53,8 @@ export function regionBetween(a: VecLike, b: VecLike): BoxModel {
|
|
|
53
53
|
* release — like placing a sticky note — settling on a point, or on a shape when released over one.
|
|
54
54
|
* With region comments enabled, dragging past the threshold draws a region rectangle instead.
|
|
55
55
|
* Placement only opens a composer; the records are created when the comment is posted. The tool
|
|
56
|
-
* stays active while the composer is open — posting returns to select, and clicking
|
|
57
|
-
*
|
|
56
|
+
* stays active while the composer is open — posting returns to select, and so does clicking the
|
|
57
|
+
* canvas elsewhere, which cancels the placement.
|
|
58
58
|
* @public
|
|
59
59
|
*/
|
|
60
60
|
export class CommentTool extends StateNode {
|
|
@@ -113,6 +113,44 @@ export class CommentTool extends StateNode {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
function pendingCommentAt(editor: Editor, point: VecLike): PendingComment {
|
|
117
|
+
const hit = commentTargetShapeAt(editor, point)
|
|
118
|
+
const anchor: TLCommentAnchor = hit
|
|
119
|
+
? shapeAnchorAt(
|
|
120
|
+
editor,
|
|
121
|
+
hit.id,
|
|
122
|
+
point,
|
|
123
|
+
getCommentingOptions(editor).shouldBePrecise(editor, {
|
|
124
|
+
shapeId: hit.id,
|
|
125
|
+
point,
|
|
126
|
+
altKey: editor.inputs.getAltKey(),
|
|
127
|
+
})
|
|
128
|
+
)
|
|
129
|
+
: { type: 'point', x: point.x, y: point.y }
|
|
130
|
+
return { anchor, point: { x: point.x, y: point.y } }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Open the comment composer at a page point, anchored as a comment tool click there would be. Use
|
|
135
|
+
* it to start a comment from outside the tool, such as a context menu item. Enters the comment tool
|
|
136
|
+
* (which must be registered), since the draft composer closes when the tool exits. It doesn't check
|
|
137
|
+
* `canComment`: a viewer who can't comment sees the `ComposerFallback` slot instead.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```tsx
|
|
141
|
+
* <TldrawUiMenuItem id="comment" label="Comment" onSelect={() => startCommentAt(editor, point)} />
|
|
142
|
+
* ```
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
145
|
+
export function startCommentAt(editor: Editor, point: VecLike) {
|
|
146
|
+
editor.setCurrentTool('comment')
|
|
147
|
+
const pending = pendingCommentAt(editor, point)
|
|
148
|
+
pendingComment.set(editor, pending)
|
|
149
|
+
// Entering the tool hints the shape under the live pointer, which is wherever the caller's UI
|
|
150
|
+
// was (a menu item), not `point`.
|
|
151
|
+
editor.setHintingShapes(pending.anchor.type === 'shape' ? [pending.anchor.shapeId] : [])
|
|
152
|
+
}
|
|
153
|
+
|
|
116
154
|
/** Hint the shape a comment placed at the pointer would anchor to, using the same hit-test as the
|
|
117
155
|
* anchor resolution on release. Hinting shapes render an indicator ungated by the active tool, so
|
|
118
156
|
* this shows the select-style outline while the comment tool — not select — is active. */
|
|
@@ -142,6 +180,12 @@ class CommentIdle extends StateNode {
|
|
|
142
180
|
}
|
|
143
181
|
|
|
144
182
|
override onPointerDown() {
|
|
183
|
+
// A press away from an open composer cancels the placement rather than moving it. The
|
|
184
|
+
// tool's onExit drops the draft composer; its text survives in the draft store.
|
|
185
|
+
if (pendingComment.get(this.editor)) {
|
|
186
|
+
this.editor.setCurrentTool('select')
|
|
187
|
+
return
|
|
188
|
+
}
|
|
145
189
|
this.parent.transition('pointing')
|
|
146
190
|
}
|
|
147
191
|
}
|
|
@@ -183,20 +227,7 @@ class CommentPointing extends StateNode {
|
|
|
183
227
|
override onPointerUp() {
|
|
184
228
|
const { editor } = this
|
|
185
229
|
const point = editor.inputs.getCurrentPagePoint()
|
|
186
|
-
|
|
187
|
-
const anchor: TLCommentAnchor = hit
|
|
188
|
-
? shapeAnchorAt(
|
|
189
|
-
editor,
|
|
190
|
-
hit.id,
|
|
191
|
-
point,
|
|
192
|
-
getCommentingOptions(editor).shouldBePrecise(editor, {
|
|
193
|
-
shapeId: hit.id,
|
|
194
|
-
point,
|
|
195
|
-
altKey: editor.inputs.getAltKey(),
|
|
196
|
-
})
|
|
197
|
-
)
|
|
198
|
-
: { type: 'point', x: point.x, y: point.y }
|
|
199
|
-
pendingComment.set(editor, { anchor, point: { x: point.x, y: point.y } })
|
|
230
|
+
pendingComment.set(editor, pendingCommentAt(editor, point))
|
|
200
231
|
// Stay in the tool while the composer is open — the interaction isn't over until the
|
|
201
232
|
// comment is posted or dismissed, and staying keeps the surrounding UI (style panel,
|
|
202
233
|
// sidebar) from churning mid-placement.
|
|
@@ -278,7 +309,9 @@ class CommentDragging extends StateNode {
|
|
|
278
309
|
export const commentTools = [CommentTool]
|
|
279
310
|
|
|
280
311
|
/** Registers the comment tool in the UI (icon, label, shortcut). Compose into your overrides.
|
|
281
|
-
* Once registered, tldraw's `DefaultQuickActionsContent` shows the comment button
|
|
312
|
+
* Once registered, tldraw's `DefaultQuickActionsContent` shows the comment button, and its
|
|
313
|
+
* `DefaultContextMenuContent` shows a comment item that opens the composer where the menu was
|
|
314
|
+
* opened.
|
|
282
315
|
* @public */
|
|
283
316
|
export const commentToolOverrides: TLUiOverrides = {
|
|
284
317
|
tools(editor, tools) {
|
|
@@ -287,7 +320,11 @@ export const commentToolOverrides: TLUiOverrides = {
|
|
|
287
320
|
icon: 'comment',
|
|
288
321
|
label: 'Comment',
|
|
289
322
|
kbd: 'c',
|
|
290
|
-
onSelect: () =>
|
|
323
|
+
onSelect: (source) => {
|
|
324
|
+
// The menu was opened by the last pointer down; the live pointer is over the menu now.
|
|
325
|
+
if (source === 'context-menu') startCommentAt(editor, editor.inputs.getOriginPagePoint())
|
|
326
|
+
else editor.setCurrentTool('comment')
|
|
327
|
+
},
|
|
291
328
|
}
|
|
292
329
|
return tools
|
|
293
330
|
},
|
|
@@ -80,6 +80,9 @@ export function PendingComposer({
|
|
|
80
80
|
// A click in the composer, or in the mention picker it spawns (portaled elsewhere), is
|
|
81
81
|
// not "outside" — keep the draft open so the pick can insert.
|
|
82
82
|
if (el.contains(target) || target.closest('.tlui-cmt-mention-popup')) return
|
|
83
|
+
// A press on the canvas is the comment tool's to handle: it ends the placement and returns
|
|
84
|
+
// to select. Clearing here first would let that press re-place the composer instead.
|
|
85
|
+
if (editor.getContainer().querySelector('.tl-canvas')?.contains(target)) return
|
|
83
86
|
pendingComment.set(editor, null)
|
|
84
87
|
}
|
|
85
88
|
document.addEventListener('pointerdown', onPointerDown, true)
|
package/src/index.ts
CHANGED
|
@@ -80,7 +80,12 @@ export {
|
|
|
80
80
|
toggleCommentReaction,
|
|
81
81
|
useCommentReactions,
|
|
82
82
|
} from './canvas/comment-reactions'
|
|
83
|
-
export {
|
|
83
|
+
export {
|
|
84
|
+
CommentTool,
|
|
85
|
+
commentToolOverrides,
|
|
86
|
+
commentTools,
|
|
87
|
+
startCommentAt,
|
|
88
|
+
} from './canvas/comment-tool'
|
|
84
89
|
export {
|
|
85
90
|
getCommentReactions,
|
|
86
91
|
getCommentRecord,
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from 'react'
|
|
20
20
|
import { isEqual, TLRichText, useMaybeEditor } from 'tldraw'
|
|
21
21
|
import { commentTipTapExtensions, EMPTY_COMMENT, isCommentEmpty } from './comment-extensions'
|
|
22
|
+
import { focusOnGestureEnd } from './gesture-focus'
|
|
22
23
|
import { SendButton } from './send-button'
|
|
23
24
|
|
|
24
25
|
/** @public */
|
|
@@ -258,6 +259,14 @@ export function CommentComposer({
|
|
|
258
259
|
return () => cancelAnimationFrame(raf)
|
|
259
260
|
}, [autoFocus, editor])
|
|
260
261
|
|
|
262
|
+
// The frame-later focus above places the caret but can't raise iOS's software keyboard, leaving
|
|
263
|
+
// the composer looking ready while it swallows every keystroke. Focus once more from the release
|
|
264
|
+
// that ends the placing gesture, which can.
|
|
265
|
+
useEffect(() => {
|
|
266
|
+
if (!autoFocus || !editor) return
|
|
267
|
+
return focusOnGestureEnd(editor.view.dom.ownerDocument, () => editor.commands.focus('end'))
|
|
268
|
+
}, [autoFocus, editor])
|
|
269
|
+
|
|
261
270
|
// The whole field behaves like the text input: clicking its empty area (the padding, or the
|
|
262
271
|
// space beside/below a short line) focuses the editor rather than only the text glyphs being
|
|
263
272
|
// clickable. The input (caret placement) and the send button keep their own click handling.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { focusOnGestureEnd } from './gesture-focus'
|
|
3
|
+
|
|
4
|
+
const pointer = (type: 'pointerdown' | 'pointerup') =>
|
|
5
|
+
document.dispatchEvent(new Event(type, { bubbles: true }))
|
|
6
|
+
|
|
7
|
+
describe('focusOnGestureEnd', () => {
|
|
8
|
+
it('focuses on the release that ends the gesture the field was mounted during', () => {
|
|
9
|
+
const focus = vi.fn()
|
|
10
|
+
focusOnGestureEnd(document, focus)
|
|
11
|
+
pointer('pointerup')
|
|
12
|
+
expect(focus).toHaveBeenCalledTimes(1)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('only focuses once, so later gestures leave the caret alone', () => {
|
|
16
|
+
const focus = vi.fn()
|
|
17
|
+
focusOnGestureEnd(document, focus)
|
|
18
|
+
pointer('pointerup')
|
|
19
|
+
pointer('pointerdown')
|
|
20
|
+
pointer('pointerup')
|
|
21
|
+
expect(focus).toHaveBeenCalledTimes(1)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('gives up when a press comes first — that release belongs to another gesture', () => {
|
|
25
|
+
const focus = vi.fn()
|
|
26
|
+
focusOnGestureEnd(document, focus)
|
|
27
|
+
pointer('pointerdown')
|
|
28
|
+
pointer('pointerup')
|
|
29
|
+
expect(focus).not.toHaveBeenCalled()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('stops listening once detached', () => {
|
|
33
|
+
const focus = vi.fn()
|
|
34
|
+
const stop = focusOnGestureEnd(document, focus)
|
|
35
|
+
stop()
|
|
36
|
+
pointer('pointerup')
|
|
37
|
+
expect(focus).not.toHaveBeenCalled()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('can be detached more than once', () => {
|
|
41
|
+
const stop = focusOnGestureEnd(document, vi.fn())
|
|
42
|
+
stop()
|
|
43
|
+
expect(() => stop()).not.toThrow()
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* iOS opens the software keyboard only for a focus that runs inside a user gesture's own task. A
|
|
3
|
+
* focus scheduled for a later frame still moves the caret, so a field can look ready while
|
|
4
|
+
* swallowing every keystroke until the reader taps it a second time.
|
|
5
|
+
*
|
|
6
|
+
* This covers the case where a field is mounted *during* a press — the comment composer opens on
|
|
7
|
+
* pointer down and settles its anchor on pointer up — by running `focus` from the native pointerup
|
|
8
|
+
* that ends the same gesture. That is late enough to survive the canvas taking focus for itself on
|
|
9
|
+
* pointer down, and still inside the gesture, so iOS raises the keyboard.
|
|
10
|
+
*
|
|
11
|
+
* A pointerdown arriving first means the mounting gesture had already ended (a field opened from a
|
|
12
|
+
* button, say), so the next release belongs to an unrelated gesture: give up rather than pull the
|
|
13
|
+
* caret somewhere the reader didn't ask for.
|
|
14
|
+
*
|
|
15
|
+
* Returns a function that detaches the listeners; safe to call more than once.
|
|
16
|
+
*/
|
|
17
|
+
export function focusOnGestureEnd(doc: Document, focus: () => void): () => void {
|
|
18
|
+
const stop = () => {
|
|
19
|
+
doc.removeEventListener('pointerup', onPointerUp, true)
|
|
20
|
+
doc.removeEventListener('pointerdown', stop, true)
|
|
21
|
+
}
|
|
22
|
+
function onPointerUp() {
|
|
23
|
+
stop()
|
|
24
|
+
focus()
|
|
25
|
+
}
|
|
26
|
+
doc.addEventListener('pointerup', onPointerUp, true)
|
|
27
|
+
doc.addEventListener('pointerdown', stop, true)
|
|
28
|
+
return stop
|
|
29
|
+
}
|