@adea-ai/ui 0.151.0 → 0.151.7
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/README.md +5 -0
- package/dist/components/composites/action-button/action-button.d.ts.map +1 -1
- package/dist/components/composites/action-button/action-button.js +8 -2
- package/dist/components/composites/action-button/action-button.js.map +1 -1
- package/dist/components/composites/inline-citation/inline-citation.d.ts.map +1 -1
- package/dist/components/composites/inline-citation/inline-citation.js +39 -36
- package/dist/components/composites/inline-citation/inline-citation.js.map +1 -1
- package/dist/components/conversation/internal/chat-composer-shell.d.ts.map +1 -1
- package/dist/components/conversation/internal/chat-composer-shell.js +28 -4
- package/dist/components/conversation/internal/chat-composer-shell.js.map +1 -1
- package/dist/components/conversation/paste-token-editor.d.ts.map +1 -1
- package/dist/components/conversation/paste-token-editor.js +127 -37
- package/dist/components/conversation/paste-token-editor.js.map +1 -1
- package/dist/components/conversation/scroll-follow-core.d.ts +13 -0
- package/dist/components/conversation/scroll-follow-core.d.ts.map +1 -1
- package/dist/components/conversation/scroll-follow-core.js +12 -1
- package/dist/components/conversation/scroll-follow-core.js.map +1 -1
- package/dist/components/conversation/scroll-follow.d.ts.map +1 -1
- package/dist/components/conversation/scroll-follow.js +52 -21
- package/dist/components/conversation/scroll-follow.js.map +1 -1
- package/dist/components/ui/calendar/calendar.d.ts.map +1 -1
- package/dist/components/ui/calendar/calendar.js +109 -115
- package/dist/components/ui/calendar/calendar.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/r/appearance-editor.json +1 -1
- package/dist/r/lib.json +1 -1
- package/dist/r/registry.json +3 -3
- package/dist/r/src/components/composites/action-button/action-button.tsx +11 -1
- package/dist/r/src/components/composites/inline-citation/inline-citation.tsx +112 -105
- package/dist/r/src/components/conversation/internal/chat-composer-shell.tsx +36 -2
- package/dist/r/src/components/conversation/paste-token-editor.tsx +191 -49
- package/dist/r/src/components/conversation/scroll-follow-core.ts +23 -0
- package/dist/r/src/components/conversation/scroll-follow.ts +69 -17
- package/dist/r/src/components/ui/calendar/calendar.tsx +99 -104
- package/dist/r/theming.json +1 -1
- package/package.json +9 -3
- package/registry.json +3 -3
- package/src/components/composites/action-button/action-button.tsx +11 -1
- package/src/components/composites/inline-citation/inline-citation.tsx +112 -105
- package/src/components/conversation/internal/chat-composer-shell.tsx +36 -2
- package/src/components/conversation/paste-token-editor.tsx +191 -49
- package/src/components/conversation/scroll-follow-core.ts +23 -0
- package/src/components/conversation/scroll-follow.ts +69 -17
- package/src/components/ui/calendar/calendar.tsx +99 -104
- package/src/index.ts +1 -0
|
@@ -33,6 +33,35 @@ import { Tooltip as KobalteTooltip } from '@kobalte/core/tooltip'
|
|
|
33
33
|
export type PasteTokenDraft = { text: string; blocks: PasteBlock[] }
|
|
34
34
|
type TokenRange = { start: number; end: number; block: PasteBlock }
|
|
35
35
|
type Segment = { text: string; block?: PasteBlock }
|
|
36
|
+
type SelectionDirection = 'forward' | 'backward' | 'none'
|
|
37
|
+
/**
|
|
38
|
+
* A selection the editor owes the field after one of its own edits or key
|
|
39
|
+
* handlers. `text` is the draft the claim was measured against. A claim
|
|
40
|
+
* exists only while it is the latest selection owner: every newer intent
|
|
41
|
+
* advances the epoch and drops it (see `yieldSelection`, `claimSelection`).
|
|
42
|
+
*/
|
|
43
|
+
type SelectionClaim = {
|
|
44
|
+
start: number
|
|
45
|
+
end: number
|
|
46
|
+
direction: SelectionDirection
|
|
47
|
+
text: string
|
|
48
|
+
blocks: readonly PasteBlock[]
|
|
49
|
+
focus: boolean
|
|
50
|
+
/** The field selection when the claim was made, to tell later `select`s apart. */
|
|
51
|
+
observed: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether a key is a selection intent. A bare modifier or a clipboard shortcut
|
|
56
|
+
* does not move the selection itself, so it must not cancel a claim the next
|
|
57
|
+
* paste builds on.
|
|
58
|
+
*/
|
|
59
|
+
function isSelectionIntent(event: KeyboardEvent): boolean {
|
|
60
|
+
return !(
|
|
61
|
+
/^(Shift|Control|Alt|Meta|CapsLock|Fn|OS)$/.test(event.key) ||
|
|
62
|
+
((event.metaKey || event.ctrlKey) && !event.altKey && /^[cvx]$/i.test(event.key))
|
|
63
|
+
)
|
|
64
|
+
}
|
|
36
65
|
|
|
37
66
|
export type PasteTokenEditorProps = {
|
|
38
67
|
value: string
|
|
@@ -88,8 +117,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
88
117
|
let scheduledBlockId: string | null = null
|
|
89
118
|
/**
|
|
90
119
|
* What scheduled the current preview. A caret event must not cancel a preview
|
|
91
|
-
* the pointer is owed: a programmatic caret restore (`
|
|
92
|
-
* animation frame) dispatches its `select` event as a separate task that can
|
|
120
|
+
* the pointer is owed: a programmatic caret restore (`claimSelection`, which
|
|
121
|
+
* may defer to an animation frame) dispatches its `select` event as a separate task that can
|
|
93
122
|
* land *after* a pointermove has already scheduled the hover preview, and a
|
|
94
123
|
* caret with no token under it would otherwise tear that preview down with no
|
|
95
124
|
* further pointermove ever arriving to reschedule it. The pointer's own leave,
|
|
@@ -102,6 +131,16 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
102
131
|
let keyboardNavigationPending = false
|
|
103
132
|
let acceptingTextDrop = false
|
|
104
133
|
let snappedCaretPreview: { blockId: string; position: number } | null = null
|
|
134
|
+
/**
|
|
135
|
+
* The latest selection owner wins. Every selection intent (a pointer press, a
|
|
136
|
+
* key, IME composition, an external `select`, or one of the editor's own
|
|
137
|
+
* claims) advances the epoch; a claim or deferred snap from an older epoch is
|
|
138
|
+
* discarded instead of overriding the newer selection. Comparing positions
|
|
139
|
+
* cannot do this: replacing a controlled value parks the caret at the end of
|
|
140
|
+
* the text, exactly where an intentional caret at the end also sits.
|
|
141
|
+
*/
|
|
142
|
+
let selectionEpoch = 0
|
|
143
|
+
let selectionClaim: SelectionClaim | null = null
|
|
105
144
|
|
|
106
145
|
const ranges = () => findTokenRanges(props.value, props.blocks)
|
|
107
146
|
const clearTimer = () => {
|
|
@@ -145,12 +184,79 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
145
184
|
}
|
|
146
185
|
return position
|
|
147
186
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
187
|
+
/** A newer selection intent takes ownership; any owed claim is dropped. */
|
|
188
|
+
const yieldSelection = () => {
|
|
189
|
+
selectionEpoch += 1
|
|
190
|
+
selectionClaim = null
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Applies the owed claim once the field holds the draft it was measured
|
|
194
|
+
* against; until then the claim stays owed.
|
|
195
|
+
*/
|
|
196
|
+
const settleSelectionClaim = () => {
|
|
197
|
+
const claim = selectionClaim
|
|
198
|
+
if (!claim) return
|
|
199
|
+
if (!field || field.value !== claim.text) return
|
|
200
|
+
selectionClaim = null
|
|
201
|
+
if (claim.focus && document.activeElement !== field) field.focus()
|
|
202
|
+
if (
|
|
203
|
+
field.selectionStart !== claim.start ||
|
|
204
|
+
field.selectionEnd !== claim.end ||
|
|
205
|
+
(claim.direction !== 'none' && field.selectionDirection !== claim.direction)
|
|
206
|
+
) {
|
|
207
|
+
field.setSelectionRange(claim.start, claim.end, claim.direction)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Claims the selection for an edit or key the editor just handled. The claim
|
|
212
|
+
* lands as soon as the field holds `text`: at once for a host that writes the
|
|
213
|
+
* controlled value synchronously (so a second paste in the same task is
|
|
214
|
+
* measured from this caret, not from the end-of-text caret a value write
|
|
215
|
+
* parks), otherwise on the host's write. A newer selection intent before that
|
|
216
|
+
* write owns the field instead, and nothing is ever restored a frame later.
|
|
217
|
+
*/
|
|
218
|
+
const claimSelection = (
|
|
219
|
+
start: number,
|
|
220
|
+
end = start,
|
|
221
|
+
direction: SelectionDirection = 'none',
|
|
222
|
+
text = props.value,
|
|
223
|
+
blocks: readonly PasteBlock[] = props.blocks,
|
|
224
|
+
focus = false
|
|
225
|
+
) => {
|
|
226
|
+
selectionEpoch += 1
|
|
227
|
+
selectionClaim = {
|
|
228
|
+
start,
|
|
229
|
+
end,
|
|
230
|
+
direction,
|
|
231
|
+
text,
|
|
232
|
+
blocks,
|
|
233
|
+
focus,
|
|
234
|
+
observed: field ? `${field.selectionStart}:${field.selectionEnd}` : '',
|
|
235
|
+
}
|
|
236
|
+
settleSelectionClaim()
|
|
237
|
+
}
|
|
238
|
+
/** Writes an edit and claims a collapsed caret in it. */
|
|
239
|
+
const commit = (
|
|
240
|
+
text: string,
|
|
241
|
+
caret: number,
|
|
242
|
+
blocks: readonly PasteBlock[] = props.blocks,
|
|
243
|
+
focus = false
|
|
244
|
+
) => {
|
|
245
|
+
const pruned = pruneBlocks(text, [...blocks])
|
|
246
|
+
props.onChange({ text, blocks: pruned })
|
|
247
|
+
claimSelection(caret, caret, 'none', text, pruned, focus)
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* A `select` event that moved the selection while a claim is still owed is a
|
|
251
|
+
* newer selection someone else made, so it owns the field. Replacing a value
|
|
252
|
+
* parks the caret without firing `select`, and a `select` queued before the
|
|
253
|
+
* claim reports the selection the claim already observed.
|
|
254
|
+
*/
|
|
255
|
+
const noticeSelect = () => {
|
|
256
|
+
const claim = selectionClaim
|
|
257
|
+
if (!claim || !field) return
|
|
258
|
+
if (`${field.selectionStart}:${field.selectionEnd}` === claim.observed) return
|
|
259
|
+
yieldSelection()
|
|
154
260
|
}
|
|
155
261
|
const replaceRange = (start: number, end: number) => {
|
|
156
262
|
if (!field) return
|
|
@@ -159,21 +265,22 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
159
265
|
const nextStart = Math.min(start, ...removed.map((range) => range.start))
|
|
160
266
|
const nextEnd = Math.max(end, ...removed.map((range) => range.end))
|
|
161
267
|
const next = props.value.slice(0, nextStart) + props.value.slice(nextEnd)
|
|
162
|
-
|
|
268
|
+
commit(
|
|
163
269
|
next,
|
|
270
|
+
nextStart,
|
|
164
271
|
props.blocks.filter((block) => !removed.some((range) => range.block.id === block.id))
|
|
165
272
|
)
|
|
166
|
-
setCaret(nextStart)
|
|
167
273
|
}
|
|
168
274
|
const expandToken = (range: TokenRange) => {
|
|
169
275
|
const next =
|
|
170
276
|
props.value.slice(0, range.start) + range.block.content + props.value.slice(range.end)
|
|
171
|
-
|
|
277
|
+
closePreview()
|
|
278
|
+
commit(
|
|
172
279
|
next,
|
|
173
|
-
|
|
280
|
+
range.start + range.block.content.length,
|
|
281
|
+
props.blocks.filter((block) => block.id !== range.block.id),
|
|
282
|
+
true
|
|
174
283
|
)
|
|
175
|
-
closePreview()
|
|
176
|
-
setCaret(range.start + range.block.content.length, true)
|
|
177
284
|
}
|
|
178
285
|
|
|
179
286
|
const segments = (): Segment[] => {
|
|
@@ -283,6 +390,7 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
283
390
|
|
|
284
391
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
285
392
|
snappedCaretPreview = null
|
|
393
|
+
if (isSelectionIntent(event)) yieldSelection()
|
|
286
394
|
rawPasteNext =
|
|
287
395
|
(event.metaKey || event.ctrlKey) &&
|
|
288
396
|
event.shiftKey &&
|
|
@@ -350,7 +458,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
350
458
|
: undefined
|
|
351
459
|
if (adjacent) {
|
|
352
460
|
event.preventDefault()
|
|
353
|
-
|
|
461
|
+
const caret = left ? adjacent.start : adjacent.end
|
|
462
|
+
claimSelection(caret)
|
|
354
463
|
props.onKeyDown(event)
|
|
355
464
|
return
|
|
356
465
|
}
|
|
@@ -366,20 +475,13 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
366
475
|
: undefined
|
|
367
476
|
if (adjacent) {
|
|
368
477
|
event.preventDefault()
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
adjacent.start,
|
|
377
|
-
start <= adjacent.start ? 'forward' : 'backward'
|
|
378
|
-
)
|
|
379
|
-
else if (direction === 'backward')
|
|
380
|
-
field.setSelectionRange(adjacent.end, end, adjacent.end <= end ? 'backward' : 'forward')
|
|
381
|
-
else field.setSelectionRange(start, adjacent.end, 'forward')
|
|
382
|
-
})
|
|
478
|
+
const anchor = direction === 'backward' ? end : start
|
|
479
|
+
const focus = event.key === 'ArrowLeft' ? adjacent.start : adjacent.end
|
|
480
|
+
claimSelection(
|
|
481
|
+
Math.min(anchor, focus),
|
|
482
|
+
Math.max(anchor, focus),
|
|
483
|
+
focus < anchor ? 'backward' : 'forward'
|
|
484
|
+
)
|
|
383
485
|
props.onKeyDown(event)
|
|
384
486
|
return
|
|
385
487
|
}
|
|
@@ -392,9 +494,12 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
392
494
|
(event.metaKey || event.ctrlKey || event.altKey))
|
|
393
495
|
if (navigation) {
|
|
394
496
|
const leftward = event.key === 'ArrowLeft' || event.key === 'Home'
|
|
497
|
+
const owner = selectionEpoch
|
|
395
498
|
keyboardNavigationPending = true
|
|
396
499
|
requestAnimationFrame(() => {
|
|
397
|
-
|
|
500
|
+
// A newer selection intent since this key owns the field; its own
|
|
501
|
+
// `select` snaps it.
|
|
502
|
+
if (field && owner === selectionEpoch) {
|
|
398
503
|
const nextRanges = findTokenRanges(field.value, props.blocks)
|
|
399
504
|
const edge = leftward ? 'start' : 'end'
|
|
400
505
|
const nextStart = snapToTokenEdge(field.selectionStart, nextRanges, edge)
|
|
@@ -432,17 +537,30 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
432
537
|
const pasted = event.clipboardData?.getData('text/plain') ?? ''
|
|
433
538
|
if (!pasted || !field || props.readOnly || props.disabled) return
|
|
434
539
|
const cleaned = forceRaw ? pasted : stripTrailingBlankLines(pasted)
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const
|
|
540
|
+
// Pastes batched before a deferred host writes the previous one build on
|
|
541
|
+
// that owed draft and caret, not on the stale value still in the field.
|
|
542
|
+
// The owed claim is the draft and caret the host has not written yet.
|
|
543
|
+
const owed = selectionClaim
|
|
544
|
+
const {
|
|
545
|
+
text: value,
|
|
546
|
+
blocks,
|
|
547
|
+
start,
|
|
548
|
+
end,
|
|
549
|
+
} = owed ?? {
|
|
550
|
+
text: props.value,
|
|
551
|
+
blocks: props.blocks,
|
|
552
|
+
start: field.selectionStart,
|
|
553
|
+
end: field.selectionEnd,
|
|
554
|
+
}
|
|
555
|
+
const before = value.slice(0, start)
|
|
556
|
+
const after = value.slice(end)
|
|
439
557
|
|
|
440
558
|
if (!forceRaw && shouldCollapse(cleaned)) {
|
|
441
559
|
const id = props.createBlockId()
|
|
442
|
-
if (
|
|
560
|
+
if (blocks.some((block) => block.id === id)) return
|
|
443
561
|
const block: PasteBlock = {
|
|
444
562
|
id,
|
|
445
|
-
seq: nextSeq([...
|
|
563
|
+
seq: nextSeq([...blocks]),
|
|
446
564
|
lines: countLines(cleaned),
|
|
447
565
|
content: cleaned,
|
|
448
566
|
}
|
|
@@ -453,9 +571,16 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
453
571
|
const trailing = after && !after.startsWith('\n') ? '\n' : ''
|
|
454
572
|
const insert = leading + formatToken(block) + trailing
|
|
455
573
|
event.preventDefault()
|
|
456
|
-
change(before + insert + after, [...props.blocks, block])
|
|
457
574
|
closePreview()
|
|
458
|
-
|
|
575
|
+
commit(before + insert + after, before.length + insert.length, [...blocks, block])
|
|
576
|
+
return
|
|
577
|
+
}
|
|
578
|
+
if (owed) {
|
|
579
|
+
// The field still shows an older value, so a native insertion would land
|
|
580
|
+
// in the wrong text: insert into the owed draft instead.
|
|
581
|
+
const text = forceRaw || cleaned === '' ? pasted : cleaned
|
|
582
|
+
event.preventDefault()
|
|
583
|
+
commit(before + text + after, before.length + text.length, blocks)
|
|
459
584
|
return
|
|
460
585
|
}
|
|
461
586
|
if (forceRaw) return
|
|
@@ -475,7 +600,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
475
600
|
if (!nativePasteInputSeen) suppressPasteInput = false
|
|
476
601
|
change(next)
|
|
477
602
|
if (inserted && field.value === next) return
|
|
478
|
-
|
|
603
|
+
const caret = before.length + cleaned.length
|
|
604
|
+
claimSelection(caret, caret, 'none', next)
|
|
479
605
|
}
|
|
480
606
|
}
|
|
481
607
|
|
|
@@ -519,9 +645,9 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
519
645
|
end = Math.max(end, ...removed.map((range) => range.end))
|
|
520
646
|
}
|
|
521
647
|
|
|
522
|
-
|
|
648
|
+
const next = props.value.slice(0, start) + dropped + props.value.slice(end)
|
|
523
649
|
closePreview()
|
|
524
|
-
|
|
650
|
+
commit(next, start + dropped.length)
|
|
525
651
|
}
|
|
526
652
|
|
|
527
653
|
const handleClipboard = (event: ClipboardEvent) => {
|
|
@@ -533,8 +659,8 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
533
659
|
event.clipboardData.setData('text/plain', expanded)
|
|
534
660
|
event.preventDefault()
|
|
535
661
|
if (event.type === 'cut') {
|
|
536
|
-
|
|
537
|
-
|
|
662
|
+
const next = props.value.slice(0, start) + props.value.slice(end)
|
|
663
|
+
commit(next, start)
|
|
538
664
|
}
|
|
539
665
|
}
|
|
540
666
|
|
|
@@ -543,18 +669,27 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
543
669
|
const range = tokenRangeAt(props.value, props.blocks, field.selectionStart)
|
|
544
670
|
if (!range) return
|
|
545
671
|
if (isTouchDevice() || event.detail >= 2) expandToken(range)
|
|
546
|
-
else
|
|
547
|
-
requestAnimationFrame(() => field?.setSelectionRange(range.start, range.end))
|
|
548
|
-
}
|
|
672
|
+
else claimSelection(range.start, range.end)
|
|
549
673
|
}
|
|
550
674
|
|
|
551
|
-
createEffect(
|
|
675
|
+
createEffect(
|
|
676
|
+
on(
|
|
677
|
+
() => props.value,
|
|
678
|
+
() => {
|
|
679
|
+
closePreview()
|
|
680
|
+
// The controlled value has just been written; a claim measured against
|
|
681
|
+
// it lands before any queued selection task can read the parked caret.
|
|
682
|
+
settleSelectionClaim()
|
|
683
|
+
}
|
|
684
|
+
)
|
|
685
|
+
)
|
|
552
686
|
createEffect(on(() => props.blocks, closePreview))
|
|
553
687
|
createEffect(
|
|
554
688
|
on(
|
|
555
689
|
() => props.resetKey,
|
|
556
690
|
() => {
|
|
557
691
|
rawPasteNext = false
|
|
692
|
+
yieldSelection()
|
|
558
693
|
closePreview()
|
|
559
694
|
}
|
|
560
695
|
)
|
|
@@ -634,7 +769,11 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
634
769
|
onCopy={handleClipboard}
|
|
635
770
|
onCut={handleClipboard}
|
|
636
771
|
onClick={handleClick}
|
|
637
|
-
onSelect={
|
|
772
|
+
onSelect={() => {
|
|
773
|
+
noticeSelect()
|
|
774
|
+
snapSelection()
|
|
775
|
+
}}
|
|
776
|
+
onPointerDown={yieldSelection}
|
|
638
777
|
onMouseMove={schedulePointerPreview}
|
|
639
778
|
onMouseLeave={closePreview}
|
|
640
779
|
onScroll={(event) => {
|
|
@@ -644,7 +783,10 @@ export function PasteTokenEditor(props: PasteTokenEditorProps) {
|
|
|
644
783
|
}
|
|
645
784
|
closePreview()
|
|
646
785
|
}}
|
|
647
|
-
onCompositionStart={
|
|
786
|
+
onCompositionStart={() => {
|
|
787
|
+
yieldSelection()
|
|
788
|
+
props.onCompositionStart()
|
|
789
|
+
}}
|
|
648
790
|
onCompositionEnd={props.onCompositionEnd}
|
|
649
791
|
onFocus={props.onFocus}
|
|
650
792
|
onBlur={() => {
|
|
@@ -86,6 +86,29 @@ export function isSelfScroll(
|
|
|
86
86
|
return lastWriteTop >= 0 && Math.abs(scrollTop - lastWriteTop) <= epsilon
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* A viewport can clamp our bottom write before its scroll/resize notification,
|
|
91
|
+
* then streamed content can reopen the gap before either callback runs. Accept
|
|
92
|
+
* only the exact offset explained by that growth, with no reader input. Other
|
|
93
|
+
* offsets still belong to the reader. A programmatic reveal landing at this
|
|
94
|
+
* same offset cannot be distinguished from native clamping by geometry alone.
|
|
95
|
+
*/
|
|
96
|
+
export function isViewportGrowthClamp(args: {
|
|
97
|
+
geom: ScrollGeom
|
|
98
|
+
lastWriteTop: number
|
|
99
|
+
lastWriteClientHeight: number
|
|
100
|
+
readerMovedSinceWrite: boolean
|
|
101
|
+
}): boolean {
|
|
102
|
+
const growth = args.geom.clientHeight - args.lastWriteClientHeight
|
|
103
|
+
return (
|
|
104
|
+
!args.readerMovedSinceWrite &&
|
|
105
|
+
args.lastWriteTop >= 0 &&
|
|
106
|
+
args.lastWriteClientHeight >= 0 &&
|
|
107
|
+
growth > SELF_SCROLL_EPSILON &&
|
|
108
|
+
Math.abs(args.geom.scrollTop - Math.max(0, args.lastWriteTop - growth)) <= SELF_SCROLL_EPSILON
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
89
112
|
/**
|
|
90
113
|
* Distance (px) from the true bottom within which a user scroll RE-ENGAGES
|
|
91
114
|
* follow. Deliberately much tighter than DEFAULT_BOTTOM_THRESHOLD: that 100px
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
evaluateAutoPin,
|
|
24
24
|
isScrollUpIntent,
|
|
25
25
|
isSelfScroll,
|
|
26
|
+
isViewportGrowthClamp,
|
|
26
27
|
resolveUserScrollStick,
|
|
27
28
|
SELF_SCROLL_EPSILON,
|
|
28
29
|
type ReaderInput,
|
|
@@ -52,6 +53,7 @@ export function createScrollFollow(options: {
|
|
|
52
53
|
let stick = true
|
|
53
54
|
let lastWriteTop = -1
|
|
54
55
|
let lastWriteClientHeight = -1
|
|
56
|
+
let readerMovedSinceWrite = false
|
|
55
57
|
let previousTop = -1
|
|
56
58
|
let lastScrollClientHeight = 0
|
|
57
59
|
let pendingRestore: { frame: number; fromTop: number; target: number } | undefined
|
|
@@ -88,13 +90,29 @@ export function createScrollFollow(options: {
|
|
|
88
90
|
}
|
|
89
91
|
onCleanup(reportPosition)
|
|
90
92
|
|
|
93
|
+
const rememberPosition = (element: HTMLDivElement) => {
|
|
94
|
+
lastWriteTop = element.scrollTop
|
|
95
|
+
lastWriteClientHeight = element.clientHeight
|
|
96
|
+
previousTop = element.scrollTop
|
|
97
|
+
readerMovedSinceWrite = false
|
|
98
|
+
}
|
|
91
99
|
const writePin = (element: HTMLDivElement, target: number) => {
|
|
92
100
|
// Instant writes keep the self-scroll reference synchronized. A smooth
|
|
93
101
|
// animation would produce intermediate positions that resemble user input.
|
|
94
102
|
element.scrollTop = target
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
103
|
+
rememberPosition(element)
|
|
104
|
+
}
|
|
105
|
+
const acceptViewportClamp = (element: HTMLDivElement) => {
|
|
106
|
+
if (
|
|
107
|
+
stick &&
|
|
108
|
+
isViewportGrowthClamp({
|
|
109
|
+
geom: geometry(element),
|
|
110
|
+
lastWriteTop,
|
|
111
|
+
lastWriteClientHeight,
|
|
112
|
+
readerMovedSinceWrite,
|
|
113
|
+
})
|
|
114
|
+
)
|
|
115
|
+
rememberPosition(element)
|
|
98
116
|
}
|
|
99
117
|
const pinHeld = (element: HTMLDivElement) => {
|
|
100
118
|
const geom = geometry(element)
|
|
@@ -102,11 +120,7 @@ export function createScrollFollow(options: {
|
|
|
102
120
|
if (distanceFromBottom(geom) > atBottomEpsilon()) writePin(element, bottomTarget(geom))
|
|
103
121
|
// An end the engine already clamped to is accepted as it stands, and is the reference
|
|
104
122
|
// from here on: the write before it is further down than the content now reaches.
|
|
105
|
-
else
|
|
106
|
-
lastWriteTop = element.scrollTop
|
|
107
|
-
lastWriteClientHeight = element.clientHeight
|
|
108
|
-
previousTop = element.scrollTop
|
|
109
|
-
}
|
|
123
|
+
else rememberPosition(element)
|
|
110
124
|
holdTop = element.scrollTop
|
|
111
125
|
setAtBottom(computeAtBottom(geometry(element), options.threshold()))
|
|
112
126
|
reportPosition()
|
|
@@ -114,6 +128,7 @@ export function createScrollFollow(options: {
|
|
|
114
128
|
const pinAuto = () => {
|
|
115
129
|
if (!scroller || !options.enabled() || pendingRestore) return
|
|
116
130
|
if (holdsPosition(scroller)) return pinHeld(scroller)
|
|
131
|
+
acceptViewportClamp(scroller)
|
|
117
132
|
const geom = geometry(scroller)
|
|
118
133
|
const result = evaluateAutoPin({
|
|
119
134
|
stick,
|
|
@@ -123,10 +138,7 @@ export function createScrollFollow(options: {
|
|
|
123
138
|
})
|
|
124
139
|
stick = result.stick
|
|
125
140
|
if (result.pin) writePin(scroller, result.target)
|
|
126
|
-
else if (result.stick)
|
|
127
|
-
lastWriteTop = result.target
|
|
128
|
-
lastWriteClientHeight = geom.clientHeight
|
|
129
|
-
}
|
|
141
|
+
else if (result.stick) rememberPosition(scroller)
|
|
130
142
|
setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))
|
|
131
143
|
reportPosition()
|
|
132
144
|
}
|
|
@@ -157,6 +169,7 @@ export function createScrollFollow(options: {
|
|
|
157
169
|
return
|
|
158
170
|
}
|
|
159
171
|
if (holdsPosition(scroller)) return pinHeld(scroller)
|
|
172
|
+
acceptViewportClamp(scroller)
|
|
160
173
|
const geom = geometry(scroller)
|
|
161
174
|
setAtBottom(computeAtBottom(geom, options.threshold()))
|
|
162
175
|
if (!isSelfScroll(geom.scrollTop, lastWriteTop)) {
|
|
@@ -173,6 +186,9 @@ export function createScrollFollow(options: {
|
|
|
173
186
|
lastWriteClientHeight = -1
|
|
174
187
|
}
|
|
175
188
|
}
|
|
189
|
+
// A content-collapse clamp can arrive before ResizeObserver. Keep its new
|
|
190
|
+
// bottom as our reference if viewport growth and more output follow next.
|
|
191
|
+
if (stick && distanceFromBottom(geom) <= atBottomEpsilon()) rememberPosition(scroller)
|
|
176
192
|
previousTop = geom.scrollTop
|
|
177
193
|
// This baseline belongs only to scroll events, not observer callbacks.
|
|
178
194
|
// Advancing it during resize would erase the viewport-clamp evidence.
|
|
@@ -219,15 +235,20 @@ export function createScrollFollow(options: {
|
|
|
219
235
|
}
|
|
220
236
|
}
|
|
221
237
|
const noteHoldInput = (event: Event) => {
|
|
222
|
-
if (holds === 0 || holdIntent) return
|
|
223
238
|
let input: ReaderInput | undefined
|
|
224
239
|
if (event instanceof WheelEvent) input = { kind: 'wheel', deltaY: event.deltaY }
|
|
225
|
-
else if (event instanceof KeyboardEvent)
|
|
240
|
+
else if (event instanceof KeyboardEvent) {
|
|
241
|
+
// Descendant controls own their keys; only the focused transcript can
|
|
242
|
+
// signal native keyboard scrolling, just as for snapshot restoration.
|
|
243
|
+
if (event.target !== scroller) return
|
|
226
244
|
input = { kind: 'key', key: event.key, shiftKey: event.shiftKey }
|
|
227
|
-
else if (event.type === 'touchmove') input = { kind: 'touchmove' }
|
|
245
|
+
} else if (event.type === 'touchmove') input = { kind: 'touchmove' }
|
|
228
246
|
else if (event.type === 'pointerdown')
|
|
229
247
|
input = { kind: 'pointerdown', onScroller: event.target === scroller }
|
|
230
|
-
if (input && isScrollUpIntent(input))
|
|
248
|
+
if (input && isScrollUpIntent(input)) {
|
|
249
|
+
readerMovedSinceWrite = true
|
|
250
|
+
if (holds > 0) holdIntent = true
|
|
251
|
+
}
|
|
231
252
|
}
|
|
232
253
|
|
|
233
254
|
createEffect(() => {
|
|
@@ -239,6 +260,7 @@ export function createScrollFollow(options: {
|
|
|
239
260
|
candidate && Number.isFinite(candidate.top) && candidate.top >= 0 ? candidate : undefined
|
|
240
261
|
let scheduledRestore: typeof pendingRestore
|
|
241
262
|
let observer: ResizeObserver | undefined
|
|
263
|
+
let contentObserver: MutationObserver | undefined
|
|
242
264
|
onCleanup(() => {
|
|
243
265
|
if (scheduledRestore) {
|
|
244
266
|
cancelAnimationFrame(scheduledRestore.frame)
|
|
@@ -248,6 +270,7 @@ export function createScrollFollow(options: {
|
|
|
248
270
|
removeRestoreIntentListeners()
|
|
249
271
|
removeRestoreIntentListeners = noop
|
|
250
272
|
observer?.disconnect()
|
|
273
|
+
contentObserver?.disconnect()
|
|
251
274
|
})
|
|
252
275
|
stick = true
|
|
253
276
|
holdGeneration += 1
|
|
@@ -256,6 +279,7 @@ export function createScrollFollow(options: {
|
|
|
256
279
|
holdIntent = false
|
|
257
280
|
lastWriteTop = -1
|
|
258
281
|
lastWriteClientHeight = -1
|
|
282
|
+
readerMovedSinceWrite = false
|
|
259
283
|
previousTop = -1
|
|
260
284
|
lastScrollClientHeight = 0
|
|
261
285
|
setAtBottom(true)
|
|
@@ -270,7 +294,35 @@ export function createScrollFollow(options: {
|
|
|
270
294
|
if (!enabled || typeof ResizeObserver === 'undefined') return
|
|
271
295
|
observer = new ResizeObserver(pinAuto)
|
|
272
296
|
observer.observe(scroller!)
|
|
273
|
-
if (content)
|
|
297
|
+
if (content) {
|
|
298
|
+
observer.observe(content)
|
|
299
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
300
|
+
contentObserver = new MutationObserver(() => {
|
|
301
|
+
// A collapsed turn can clamp the offset before any scroll/resize
|
|
302
|
+
// callback. Remember that bottom at the mutation boundary, before
|
|
303
|
+
// a later task grows the viewport or streams more content. This is
|
|
304
|
+
// bookkeeping only: never pin, re-arm, or compete with another owner.
|
|
305
|
+
if (
|
|
306
|
+
!scroller ||
|
|
307
|
+
!options.enabled() ||
|
|
308
|
+
pendingRestore ||
|
|
309
|
+
holds > 0 ||
|
|
310
|
+
!stick ||
|
|
311
|
+
readerMovedSinceWrite ||
|
|
312
|
+
lastWriteTop < 0
|
|
313
|
+
)
|
|
314
|
+
return
|
|
315
|
+
// Read the offset once per mutation batch. This can flush pending
|
|
316
|
+
// layout, but deferring it loses a collapse clamp before a later task
|
|
317
|
+
// grows the viewport/output. Ordinary growth needs no other geometry.
|
|
318
|
+
const top = scroller.scrollTop
|
|
319
|
+
if (top >= lastWriteTop - SELF_SCROLL_EPSILON) return
|
|
320
|
+
if (scroller.scrollHeight - top - scroller.clientHeight <= atBottomEpsilon())
|
|
321
|
+
rememberPosition(scroller)
|
|
322
|
+
})
|
|
323
|
+
contentObserver.observe(content, { childList: true, characterData: true, subtree: true })
|
|
324
|
+
}
|
|
325
|
+
}
|
|
274
326
|
}
|
|
275
327
|
|
|
276
328
|
const watchRestoreIntent = () => {
|