@adea-ai/ui 0.151.0 → 0.151.2

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.
@@ -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 (`setCaret` defers to an
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
- const setCaret = (position: number, focus = false) => {
149
- requestAnimationFrame(() => {
150
- if (!field) return
151
- if (focus) field.focus()
152
- field.setSelectionRange(position, position)
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
- change(
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
- change(
277
+ closePreview()
278
+ commit(
172
279
  next,
173
- props.blocks.filter((block) => block.id !== range.block.id)
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
- setCaret(left ? adjacent.start : adjacent.end)
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
- requestAnimationFrame(() => {
370
- if (!field) return
371
- if (event.key === 'ArrowLeft' && direction === 'backward')
372
- field.setSelectionRange(adjacent.start, end, 'backward')
373
- else if (event.key === 'ArrowLeft')
374
- field.setSelectionRange(
375
- start,
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
- if (field) {
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
- const start = field.selectionStart
436
- const end = field.selectionEnd
437
- const before = props.value.slice(0, start)
438
- const after = props.value.slice(end)
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 (props.blocks.some((block) => block.id === id)) return
560
+ if (blocks.some((block) => block.id === id)) return
443
561
  const block: PasteBlock = {
444
562
  id,
445
- seq: nextSeq([...props.blocks]),
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
- setCaret(before.length + insert.length)
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
- setCaret(before.length + cleaned.length)
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
- change(props.value.slice(0, start) + dropped + props.value.slice(end))
648
+ const next = props.value.slice(0, start) + dropped + props.value.slice(end)
523
649
  closePreview()
524
- setCaret(start + dropped.length)
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
- change(props.value.slice(0, start) + props.value.slice(end))
537
- setCaret(start)
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(on(() => props.value, closePreview))
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={snapSelection}
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={props.onCompositionStart}
786
+ onCompositionStart={() => {
787
+ yieldSelection()
788
+ props.onCompositionStart()
789
+ }}
648
790
  onCompositionEnd={props.onCompositionEnd}
649
791
  onFocus={props.onFocus}
650
792
  onBlur={() => {
@@ -4,7 +4,7 @@
4
4
  "title": "Theming",
5
5
  "description": "",
6
6
  "dependencies": [
7
- "@adea-ai/themes@^0.11.6",
7
+ "@adea-ai/themes@^0.11.7",
8
8
  "@kobalte/core@^0.13.14",
9
9
  "lucide-solid@^1.65.0"
10
10
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adea-ai/ui",
3
- "version": "0.151.0",
3
+ "version": "0.151.2",
4
4
  "description": "The Adea design system: Kobalte-backed Solid components, semantic OKLCH tokens, and the app-shell layout primitives shared by Adea and Cortana.",
5
5
  "keywords": [
6
6
  "adea",
@@ -150,7 +150,7 @@
150
150
  "check:packed-paste-model": "node scripts/check-packed-paste-model.mjs"
151
151
  },
152
152
  "dependencies": {
153
- "@adea-ai/themes": "^0.11.6",
153
+ "@adea-ai/themes": "^0.11.7",
154
154
  "@corvu/calendar": "^0.1.2",
155
155
  "@corvu/drawer": "^0.2.4",
156
156
  "@corvu/otp-field": "^0.1.4",
package/registry.json CHANGED
@@ -2298,7 +2298,7 @@
2298
2298
  "title": "Appearance Editor",
2299
2299
  "description": "",
2300
2300
  "dependencies": [
2301
- "@adea-ai/themes@^0.11.6",
2301
+ "@adea-ai/themes@^0.11.7",
2302
2302
  "@kobalte/core@^0.13.14",
2303
2303
  "lucide-solid@^1.65.0"
2304
2304
  ],
@@ -3458,7 +3458,7 @@
3458
3458
  "title": "Theming",
3459
3459
  "description": "",
3460
3460
  "dependencies": [
3461
- "@adea-ai/themes@^0.11.6",
3461
+ "@adea-ai/themes@^0.11.7",
3462
3462
  "@kobalte/core@^0.13.14",
3463
3463
  "lucide-solid@^1.65.0"
3464
3464
  ],
@@ -3643,7 +3643,7 @@
3643
3643
  "title": "Class-name and token helpers",
3644
3644
  "description": "The private helpers the components import: the class-name merge, the variant recipes, the token manifest and the theme bridge. Installed automatically as a peer of any component that needs it.",
3645
3645
  "dependencies": [
3646
- "@adea-ai/themes@^0.11.6",
3646
+ "@adea-ai/themes@^0.11.7",
3647
3647
  "class-variance-authority@^0.7.1",
3648
3648
  "cn@^0.5.1"
3649
3649
  ],
@@ -72,6 +72,7 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
72
72
  const [error, setError] = createSignal<string | null>(null)
73
73
  let field: HTMLTextAreaElement | undefined
74
74
  let restoreBar: HTMLButtonElement | undefined
75
+ let root: HTMLDivElement | undefined
75
76
  let focusFrame: number | undefined
76
77
  let focusTarget: 'input' | 'bar' | undefined
77
78
  let selection:
@@ -89,9 +90,24 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
89
90
  const requestFocus = (target: 'input' | 'bar') => {
90
91
  cancelFocus()
91
92
  focusTarget = target
93
+ const requester = document.activeElement
92
94
  focusFrame = requestAnimationFrame(() => {
93
95
  focusFrame = undefined
94
96
  if (!active || props.approvalFocus) return
97
+ // Focus that moved elsewhere since the request (another conversation,
98
+ // say, while a collapse or expand waited for this frame) is a newer
99
+ // owner; taking it back would also drop whatever is being typed there.
100
+ const current = document.activeElement
101
+ if (
102
+ current &&
103
+ current !== document.body &&
104
+ current !== requester &&
105
+ !root?.contains(current)
106
+ ) {
107
+ focusTarget = undefined
108
+ if (target === 'input') selection = undefined
109
+ return
110
+ }
95
111
  if (target === 'bar' && collapsed()) restoreBar?.focus()
96
112
  if (target === 'input' && !collapsed() && !props.disabled && field) {
97
113
  field.focus()
@@ -105,6 +121,16 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
105
121
  focusTarget = undefined
106
122
  })
107
123
  }
124
+ /**
125
+ * A press or key in the input before the deferred expand restore runs is a
126
+ * newer selection owner: the restore must not move its caret back.
127
+ */
128
+ const yieldInputRestore = () => {
129
+ if (focusTarget !== 'input') return
130
+ cancelFocus()
131
+ focusTarget = undefined
132
+ selection = undefined
133
+ }
108
134
  const changeCollapsed = (value: boolean) => {
109
135
  if (!props.collapse || props.approvalFocus) return
110
136
  if (value && field)
@@ -183,6 +209,7 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
183
209
  return first.length > 120 ? `${first.slice(0, 120)}…` : first
184
210
  }
185
211
  const handleInputKeyDown = (event: KeyboardEvent) => {
212
+ yieldInputRestore()
186
213
  if (event.key === 'Escape' && error()) {
187
214
  setError(null)
188
215
  return
@@ -211,7 +238,11 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
211
238
  ime.onBlur()
212
239
  }
213
240
  return (
214
- <div data-slot="chat-composer" class="mx-auto flex w-full max-w-4xl flex-col px-4 pb-1">
241
+ <div
242
+ ref={(element) => (root = element)}
243
+ data-slot="chat-composer"
244
+ class="mx-auto flex w-full max-w-4xl flex-col px-4 pb-1"
245
+ >
215
246
  <Show when={!props.approvalFocus}>
216
247
  <Show when={props.knowledge}>
217
248
  <div data-slot="composer-knowledge">{props.knowledge}</div>
@@ -287,10 +318,13 @@ export function ChatComposerShell<Extra extends object>(props: ChatComposerShell
287
318
  {props.renderInput({
288
319
  ref: (element) => {
289
320
  field = element
290
- if (element)
321
+ if (element) {
322
+ element.addEventListener('pointerdown', yieldInputRestore)
291
323
  onCleanup(() => {
324
+ element.removeEventListener('pointerdown', yieldInputRestore)
292
325
  if (field === element) field = undefined
293
326
  })
327
+ }
294
328
  },
295
329
  value: () => props.value,
296
330
  resetKey: () => props.resetKey,