@yaks/ux 0.0.0-stage → 0.2.36
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/Answer.js +43 -0
- package/Answer.js.map +1 -0
- package/Answer.ts +84 -0
- package/Conversation.js +520 -0
- package/Conversation.js.map +1 -0
- package/Conversation.ts +649 -0
- package/ConversationBox.js +106 -0
- package/ConversationBox.js.map +1 -0
- package/ConversationBox.ts +198 -0
- package/ConversationLines.js +29 -0
- package/ConversationLines.js.map +1 -0
- package/ConversationLines.ts +48 -0
- package/Disclosure.js +79 -0
- package/Disclosure.js.map +1 -0
- package/Disclosure.ts +130 -0
- package/Edit.js +330 -0
- package/Edit.js.map +1 -0
- package/Edit.ts +488 -0
- package/EntityPopup.js +35 -0
- package/EntityPopup.js.map +1 -0
- package/EntityPopup.ts +52 -0
- package/Fields.js +161 -0
- package/Fields.js.map +1 -0
- package/Fields.ts +276 -0
- package/LICENSE +202 -0
- package/Order.js +105 -0
- package/Order.js.map +1 -0
- package/Order.ts +159 -0
- package/README.md +1023 -2
- package/Save.js +50 -0
- package/Save.js.map +1 -0
- package/Save.ts +115 -0
- package/Sort.js +49 -0
- package/Sort.js.map +1 -0
- package/Sort.ts +98 -0
- package/Submit.js +115 -0
- package/Submit.js.map +1 -0
- package/Submit.ts +174 -0
- package/Text.js +114 -0
- package/Text.js.map +1 -0
- package/Text.ts +140 -0
- package/TokenPicker.js +65 -0
- package/TokenPicker.js.map +1 -0
- package/TokenPicker.ts +117 -0
- package/_dist/Answer.d.ts +39 -0
- package/_dist/Answer.d.ts.map +1 -0
- package/_dist/Conversation.d.ts +131 -0
- package/_dist/Conversation.d.ts.map +1 -0
- package/_dist/ConversationBox.d.ts +29 -0
- package/_dist/ConversationBox.d.ts.map +1 -0
- package/_dist/ConversationLines.d.ts +13 -0
- package/_dist/ConversationLines.d.ts.map +1 -0
- package/_dist/Disclosure.d.ts +36 -0
- package/_dist/Disclosure.d.ts.map +1 -0
- package/_dist/Edit.d.ts +87 -0
- package/_dist/Edit.d.ts.map +1 -0
- package/_dist/EntityPopup.d.ts +12 -0
- package/_dist/EntityPopup.d.ts.map +1 -0
- package/_dist/Fields.d.ts +29 -0
- package/_dist/Fields.d.ts.map +1 -0
- package/_dist/Order.d.ts +40 -0
- package/_dist/Order.d.ts.map +1 -0
- package/_dist/Save.d.ts +28 -0
- package/_dist/Save.d.ts.map +1 -0
- package/_dist/Sort.d.ts +28 -0
- package/_dist/Sort.d.ts.map +1 -0
- package/_dist/Submit.d.ts +48 -0
- package/_dist/Submit.d.ts.map +1 -0
- package/_dist/Text.d.ts +27 -0
- package/_dist/Text.d.ts.map +1 -0
- package/_dist/TokenPicker.d.ts +19 -0
- package/_dist/TokenPicker.d.ts.map +1 -0
- package/_dist/anchor.d.ts +52 -0
- package/_dist/anchor.d.ts.map +1 -0
- package/_dist/anchored.d.ts +30 -0
- package/_dist/anchored.d.ts.map +1 -0
- package/_dist/completion-state.d.ts +65 -0
- package/_dist/completion-state.d.ts.map +1 -0
- package/_dist/completion.d.ts +73 -0
- package/_dist/completion.d.ts.map +1 -0
- package/_dist/composer-browser.d.ts +1 -0
- package/_dist/composer-browser.d.ts.map +1 -0
- package/_dist/composer.d.ts +31 -0
- package/_dist/composer.d.ts.map +1 -0
- package/_dist/context-menu-browser.d.ts +4 -0
- package/_dist/context-menu-browser.d.ts.map +1 -0
- package/_dist/context-menu.d.ts +95 -0
- package/_dist/context-menu.d.ts.map +1 -0
- package/_dist/drag.d.ts +29 -0
- package/_dist/drag.d.ts.map +1 -0
- package/_dist/edit-keys.d.ts +42 -0
- package/_dist/edit-keys.d.ts.map +1 -0
- package/_dist/emit.d.ts +7 -0
- package/_dist/emit.d.ts.map +1 -0
- package/_dist/entity-popup-browser.d.ts +3 -0
- package/_dist/entity-popup-browser.d.ts.map +1 -0
- package/_dist/face/FaceTagger.d.ts +25 -0
- package/_dist/face/FaceTagger.d.ts.map +1 -0
- package/_dist/face/review.d.ts +5 -0
- package/_dist/face/review.d.ts.map +1 -0
- package/_dist/face/tags.d.ts +99 -0
- package/_dist/face/tags.d.ts.map +1 -0
- package/_dist/field-values.d.ts +4 -0
- package/_dist/field-values.d.ts.map +1 -0
- package/_dist/fit-list.d.ts +4 -0
- package/_dist/fit-list.d.ts.map +1 -0
- package/_dist/hits.d.ts +27 -0
- package/_dist/hits.d.ts.map +1 -0
- package/_dist/host.d.ts +91 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/image-fallback.d.ts +5 -0
- package/_dist/image-fallback.d.ts.map +1 -0
- package/_dist/keys.d.ts +95 -0
- package/_dist/keys.d.ts.map +1 -0
- package/_dist/kit.d.ts +23 -0
- package/_dist/kit.d.ts.map +1 -0
- package/_dist/lightbox-browser.d.ts +2 -0
- package/_dist/lightbox-browser.d.ts.map +1 -0
- package/_dist/lightbox.d.ts +42 -0
- package/_dist/lightbox.d.ts.map +1 -0
- package/_dist/live.d.ts +21 -0
- package/_dist/live.d.ts.map +1 -0
- package/_dist/mod.d.ts +67 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +12 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/resize.d.ts +43 -0
- package/_dist/resize.d.ts.map +1 -0
- package/_dist/rows.d.ts +31 -0
- package/_dist/rows.d.ts.map +1 -0
- package/_dist/save-browser.d.ts +29 -0
- package/_dist/save-browser.d.ts.map +1 -0
- package/_dist/select-browser.d.ts +1 -0
- package/_dist/select-browser.d.ts.map +1 -0
- package/_dist/select.d.ts +12 -0
- package/_dist/select.d.ts.map +1 -0
- package/_dist/sending.d.ts +10 -0
- package/_dist/sending.d.ts.map +1 -0
- package/_dist/sort-browser.d.ts +11 -0
- package/_dist/sort-browser.d.ts.map +1 -0
- package/_dist/state.d.ts +74 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/suggest.d.ts +18 -0
- package/_dist/suggest.d.ts.map +1 -0
- package/_dist/swipe.d.ts +60 -0
- package/_dist/swipe.d.ts.map +1 -0
- package/_dist/ui.d.ts +6 -0
- package/_dist/ui.d.ts.map +1 -0
- package/_dist/upload-browser.d.ts +13 -0
- package/_dist/upload-browser.d.ts.map +1 -0
- package/_dist/views.d.ts +8 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/anchor.js +58 -0
- package/anchor.js.map +1 -0
- package/anchor.ts +87 -0
- package/anchored.js +180 -0
- package/anchored.js.map +1 -0
- package/anchored.ts +232 -0
- package/completion-state.js +76 -0
- package/completion-state.js.map +1 -0
- package/completion-state.ts +120 -0
- package/completion.js +301 -0
- package/completion.js.map +1 -0
- package/completion.ts +433 -0
- package/composer-browser.js +4 -0
- package/composer-browser.js.map +1 -0
- package/composer-browser.ts +5 -0
- package/composer.js +42 -0
- package/composer.js.map +1 -0
- package/composer.ts +53 -0
- package/context-menu-browser.js +221 -0
- package/context-menu-browser.js.map +1 -0
- package/context-menu-browser.ts +231 -0
- package/context-menu.js +146 -0
- package/context-menu.js.map +1 -0
- package/context-menu.ts +194 -0
- package/deno.json +1 -0
- package/drag.js +33 -0
- package/drag.js.map +1 -0
- package/drag.ts +41 -0
- package/edit-keys.js +51 -0
- package/edit-keys.js.map +1 -0
- package/edit-keys.ts +81 -0
- package/emit.js +34 -0
- package/emit.js.map +1 -0
- package/emit.ts +55 -0
- package/entity-popup-browser.js +94 -0
- package/entity-popup-browser.js.map +1 -0
- package/entity-popup-browser.ts +100 -0
- package/face/FaceTagger.js +361 -0
- package/face/FaceTagger.js.map +1 -0
- package/face/FaceTagger.ts +507 -0
- package/face/NameProposal.js +61 -0
- package/face/NameProposal.js.map +1 -0
- package/face/NameProposal.ts +100 -0
- package/face/review.js +33 -0
- package/face/review.js.map +1 -0
- package/face/review.ts +49 -0
- package/face/tags.js +344 -0
- package/face/tags.js.map +1 -0
- package/face/tags.ts +357 -0
- package/field-values.js +20 -0
- package/field-values.js.map +1 -0
- package/field-values.ts +35 -0
- package/fit-list.js +66 -0
- package/fit-list.js.map +1 -0
- package/fit-list.ts +81 -0
- package/hits.js +65 -0
- package/hits.js.map +1 -0
- package/hits.ts +75 -0
- package/host.js +88 -0
- package/host.js.map +1 -0
- package/host.ts +212 -0
- package/image-fallback.js +32 -0
- package/image-fallback.js.map +1 -0
- package/image-fallback.ts +37 -0
- package/keys.js +418 -0
- package/keys.js.map +1 -0
- package/keys.ts +444 -0
- package/kit.js +41 -0
- package/kit.js.map +1 -0
- package/kit.ts +71 -0
- package/lightbox-browser.js +248 -0
- package/lightbox-browser.js.map +1 -0
- package/lightbox-browser.ts +232 -0
- package/lightbox.js +84 -0
- package/lightbox.js.map +1 -0
- package/lightbox.ts +103 -0
- package/live.js +111 -0
- package/live.js.map +1 -0
- package/live.ts +139 -0
- package/mod.js +67 -0
- package/mod.js.map +1 -0
- package/mod.ts +180 -0
- package/package.json +143 -4
- package/read.js +33 -0
- package/read.js.map +1 -0
- package/read.ts +56 -0
- package/resize.js +159 -0
- package/resize.js.map +1 -0
- package/resize.ts +203 -0
- package/rows.js +73 -0
- package/rows.js.map +1 -0
- package/rows.ts +97 -0
- package/save-browser.js +469 -0
- package/save-browser.js.map +1 -0
- package/save-browser.ts +530 -0
- package/select-browser.js +4 -0
- package/select-browser.js.map +1 -0
- package/select-browser.ts +5 -0
- package/select.js +296 -0
- package/select.js.map +1 -0
- package/select.ts +343 -0
- package/sending.js +36 -0
- package/sending.js.map +1 -0
- package/sending.ts +44 -0
- package/sort-browser.js +51 -0
- package/sort-browser.js.map +1 -0
- package/sort-browser.ts +70 -0
- package/state.js +99 -0
- package/state.js.map +1 -0
- package/state.ts +121 -0
- package/suggest.js +28 -0
- package/suggest.js.map +1 -0
- package/suggest.ts +32 -0
- package/swipe.js +186 -0
- package/swipe.js.map +1 -0
- package/swipe.ts +250 -0
- package/ui.js +507 -0
- package/ui.js.map +1 -0
- package/ui.ts +378 -0
- package/upload-browser.js +195 -0
- package/upload-browser.js.map +1 -0
- package/upload-browser.ts +303 -0
- package/upload-files.js +41 -0
- package/upload-files.js.map +1 -0
- package/upload-files.ts +49 -0
- package/views.js +8 -0
- package/views.js.map +1 -0
- package/views.ts +9 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +287 -0
- package/vocab.ts +17 -0
package/completion.ts
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Completion behavior bound to a host graph and durable host-owned drafts.
|
|
3
|
+
* The domain supplies replacements; @yaks/ui Field and Choices present them.
|
|
4
|
+
* State is Completion{caret,from,to,cands,pick}, or the component the host names.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { useStore } from '@yaks/ui/store'
|
|
9
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import {
|
|
10
|
+
createElement as h,
|
|
11
|
+
Fragment,
|
|
12
|
+
type FunctionComponent,
|
|
13
|
+
type ReactElement,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
useCallback,
|
|
16
|
+
useLayoutEffect,
|
|
17
|
+
useRef,
|
|
18
|
+
} from 'react'
|
|
19
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
20
|
+
import {
|
|
21
|
+
Combobox,
|
|
22
|
+
comboboxAttrs,
|
|
23
|
+
Field as Input,
|
|
24
|
+
type Suggestion,
|
|
25
|
+
} from '@yaks/ui/parts'
|
|
26
|
+
import type { Drafts as HostDrafts } from './host.ts'
|
|
27
|
+
import type { Result } from './completion-state.js'
|
|
28
|
+
import {
|
|
29
|
+
type Act,
|
|
30
|
+
act,
|
|
31
|
+
dismissed,
|
|
32
|
+
moved,
|
|
33
|
+
placed,
|
|
34
|
+
type Row,
|
|
35
|
+
taken,
|
|
36
|
+
typed,
|
|
37
|
+
} from './completion-state.js'
|
|
38
|
+
|
|
39
|
+
/** As much of a front-end graph as a field needs; a @yaks/client `client()`
|
|
40
|
+
* is one. */
|
|
41
|
+
export type Front = {
|
|
42
|
+
mutate: (bundles: Bundle[]) => unknown
|
|
43
|
+
watch: (query: string, opts?: { remote?: boolean }) => {
|
|
44
|
+
value: Bundle[]
|
|
45
|
+
subscribe: (fn: (rows: Bundle[]) => void) => () => void
|
|
46
|
+
close?: () => void
|
|
47
|
+
}
|
|
48
|
+
ent: (eid: string) => Bundle | undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** An element a list floats beside. */
|
|
52
|
+
export type Anchor = { current: HTMLInputElement | HTMLTextAreaElement | null }
|
|
53
|
+
|
|
54
|
+
/** Where the list of candidates floats: beside the element it belongs to,
|
|
55
|
+
* its field or the well around it. */
|
|
56
|
+
export type Float = FunctionComponent<
|
|
57
|
+
{ anchor: { current: HTMLElement | null }; children?: ReactNode }
|
|
58
|
+
>
|
|
59
|
+
|
|
60
|
+
/** A person's drafts, by the place they type in: what is typed there ('' for
|
|
61
|
+
* nothing), kept as it is typed, and followed as it changes (host.ts
|
|
62
|
+
* `Drafts`). @yaks/draft's `desk` is one. */
|
|
63
|
+
export type Drafts = Pick<HostDrafts, 'text' | 'type' | 'follow'>
|
|
64
|
+
|
|
65
|
+
/** What the host supplies; the domain owns completion, the host owns drafts. */
|
|
66
|
+
export type Opts = {
|
|
67
|
+
/** What the domain offers, read anew each time the person types. */
|
|
68
|
+
complete: (text: string, caret: number) => Result | Promise<Result>
|
|
69
|
+
/** The graph component holding the caret and choices (default: Completion). */
|
|
70
|
+
component?: string
|
|
71
|
+
/** where what is typed in each field is kept, by the field's name */
|
|
72
|
+
drafts: Drafts
|
|
73
|
+
/** where the list floats (default: below the field) */
|
|
74
|
+
Float?: Float
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What a completion `Field` takes. */
|
|
78
|
+
export type FieldProps = {
|
|
79
|
+
/** the field's entity in the front-end graph */
|
|
80
|
+
id: string
|
|
81
|
+
placeholder?: string
|
|
82
|
+
/** the field's accessible name */
|
|
83
|
+
'aria-label'?: string
|
|
84
|
+
/** several lines: a newline can be typed (shift+Enter) */
|
|
85
|
+
lines?: boolean
|
|
86
|
+
/** @yaks/ui `Field` variants */
|
|
87
|
+
mod?: string
|
|
88
|
+
className?: string
|
|
89
|
+
class?: string
|
|
90
|
+
/** the text a field starts with when nothing is typed in it yet */
|
|
91
|
+
initial?: string
|
|
92
|
+
/** paint the caret, for a terminal, where no element has the focus */
|
|
93
|
+
active?: boolean
|
|
94
|
+
/** take the keyboard on mount */
|
|
95
|
+
focus?: boolean
|
|
96
|
+
/** the element, for a host that measures it or moves the focus */
|
|
97
|
+
elRef?: Anchor
|
|
98
|
+
/** a key the list did not take */
|
|
99
|
+
onKey?: (e: KeyboardEvent) => void
|
|
100
|
+
/** after the field took what was typed */
|
|
101
|
+
onInput?: (e: InputEvent) => void
|
|
102
|
+
onBlur?: (e: FocusEvent) => void
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The fields in one front-end graph. */
|
|
106
|
+
export type Controller = {
|
|
107
|
+
/** the completion field */
|
|
108
|
+
Field: FunctionComponent<FieldProps>
|
|
109
|
+
/** Choices for a separately hosted input; reads its row reactively. */
|
|
110
|
+
List: FunctionComponent<{ id: string; anchor: Anchor }>
|
|
111
|
+
/** Wire an imperative input, returning its listener cleanup. */
|
|
112
|
+
bind: (id: string, el: HTMLInputElement | HTMLTextAreaElement) => () => void
|
|
113
|
+
/** Release the controller's graph subscription when its host closes. */
|
|
114
|
+
dispose: () => void
|
|
115
|
+
/** a field's row */
|
|
116
|
+
row: (id: string) => Row | undefined
|
|
117
|
+
/** a field's row, read by a mounted view */
|
|
118
|
+
useRow: (id: string) => Row | undefined
|
|
119
|
+
/** what is typed in a field */
|
|
120
|
+
text: (id: string) => string
|
|
121
|
+
/** the person typed: the text, the caret, and what can come next there */
|
|
122
|
+
type: (id: string, text: string, caret?: number) => void
|
|
123
|
+
/** the host put this text in the field, offering nothing */
|
|
124
|
+
set: (id: string, text: string, caret?: number) => void
|
|
125
|
+
/** move the pick `d` rows */
|
|
126
|
+
move: (id: string, d: number) => void
|
|
127
|
+
/** take the picked candidate (or candidate `i`), and complete on from it */
|
|
128
|
+
accept: (id: string, i?: number) => void
|
|
129
|
+
/** close the list */
|
|
130
|
+
dismiss: (id: string) => void
|
|
131
|
+
/** a key, named as a browser names it: whether the list took it */
|
|
132
|
+
press: (id: string, key: string) => boolean
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// The list in the flow, under the field.
|
|
136
|
+
|
|
137
|
+
// A key with a modifier is always the host's: ⌘⏎ opens a tab, ⇧⏎ a new line.
|
|
138
|
+
let modified = (e: KeyboardEvent) =>
|
|
139
|
+
e.metaKey || e.ctrlKey || e.altKey || e.shiftKey
|
|
140
|
+
|
|
141
|
+
/** Bind completion to a front-end graph and to what its host knows. */
|
|
142
|
+
export let completion = (front: Front, opts: Opts): Controller => {
|
|
143
|
+
let { Float, component = 'Completion' } = opts
|
|
144
|
+
// One watch for every field; each field reads its own row of it, so a
|
|
145
|
+
// keystroke repaints the field it landed in and whoever reads that one.
|
|
146
|
+
// Choices and carets stay on this interface even on a connected graph.
|
|
147
|
+
let seen = front.watch(`.${component}`, { remote: false })
|
|
148
|
+
let dispose = () => seen.close?.()
|
|
149
|
+
// A field's row: what is typed (its draft) over its state component, and none
|
|
150
|
+
// while it has neither.
|
|
151
|
+
let rowOf = (id: string, f?: Bundle, text = opts.drafts.text(id)) => {
|
|
152
|
+
let at = f?.[component] as Omit<Row, 'text'> | undefined
|
|
153
|
+
return at || text ? { ...placed(text), ...at, text } : undefined
|
|
154
|
+
}
|
|
155
|
+
// The row as it stands, the same object until its state or its draft
|
|
156
|
+
// changes.
|
|
157
|
+
let drawn = new Map<string, { f?: Bundle; text: string; row?: Row }>()
|
|
158
|
+
let live = (id: string) => {
|
|
159
|
+
let f = seen.value.find((b) => b.entity.eid == id)
|
|
160
|
+
let text = opts.drafts.text(id)
|
|
161
|
+
let was = drawn.get(id)
|
|
162
|
+
if (was && was.f === f && was.text === text) return was.row
|
|
163
|
+
let row = rowOf(id, f, text)
|
|
164
|
+
drawn.set(id, { f, text, row })
|
|
165
|
+
return row
|
|
166
|
+
}
|
|
167
|
+
// Wake `fn` whenever a field's state or draft may have changed.
|
|
168
|
+
let follow = (id: string, fn: () => void) => {
|
|
169
|
+
let off = seen.subscribe(() => fn())
|
|
170
|
+
let unfollow = opts.drafts.follow?.(() => {
|
|
171
|
+
opts.drafts.text(id)
|
|
172
|
+
fn()
|
|
173
|
+
})
|
|
174
|
+
return () => {
|
|
175
|
+
off()
|
|
176
|
+
unfollow?.()
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
let useLive = (id: string) =>
|
|
180
|
+
useStore(
|
|
181
|
+
useCallback((wake: () => void) => follow(id, wake), [id]),
|
|
182
|
+
() => live(id),
|
|
183
|
+
)
|
|
184
|
+
let now = (id: string) => rowOf(id, front.ent(id))
|
|
185
|
+
let write = (id: string, { text, ...at }: Partial<Row>) => {
|
|
186
|
+
if (text != null) opts.drafts.type(id, text)
|
|
187
|
+
if (Object.keys(at).length) {
|
|
188
|
+
front.mutate([{ entity: { eid: id }, [component]: at }])
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let versions = new Map<string, number>()
|
|
193
|
+
let next = (id: string) => {
|
|
194
|
+
let version = (versions.get(id) ?? 0) + 1
|
|
195
|
+
versions.set(id, version)
|
|
196
|
+
return version
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let type = (id: string, text: string, caret = text.length) => {
|
|
200
|
+
let version = next(id)
|
|
201
|
+
let found = opts.complete(text, caret)
|
|
202
|
+
if (!(found instanceof Promise)) {
|
|
203
|
+
return write(id, typed(text, caret, found))
|
|
204
|
+
}
|
|
205
|
+
// A source that answers later: the text lands now, the list when it
|
|
206
|
+
// arrives, unless the person has typed on since.
|
|
207
|
+
write(id, placed(text, caret))
|
|
208
|
+
found.then((f) => {
|
|
209
|
+
let r = now(id)
|
|
210
|
+
if (versions.get(id) == version && r?.text == text && r.caret == caret) {
|
|
211
|
+
write(id, typed(text, caret, f))
|
|
212
|
+
}
|
|
213
|
+
}, () => {
|
|
214
|
+
// An unavailable source leaves the draft and its empty list intact.
|
|
215
|
+
// In particular, a failed older request must not clear newer choices.
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
let set = (id: string, text: string, caret?: number) => {
|
|
219
|
+
next(id)
|
|
220
|
+
write(id, placed(text, caret))
|
|
221
|
+
}
|
|
222
|
+
let move = (id: string, d: number) => {
|
|
223
|
+
let r = now(id)
|
|
224
|
+
if (r?.cands.length) write(id, moved(r, d))
|
|
225
|
+
}
|
|
226
|
+
let accept = (id: string, i?: number) => {
|
|
227
|
+
let r = now(id)
|
|
228
|
+
if (!r?.cands.length) return
|
|
229
|
+
let t = taken(r, i)
|
|
230
|
+
type(id, t.text, t.caret)
|
|
231
|
+
}
|
|
232
|
+
let dismiss = (id: string) => {
|
|
233
|
+
next(id)
|
|
234
|
+
if (now(id)?.cands.length) write(id, dismissed)
|
|
235
|
+
}
|
|
236
|
+
let acts: Record<Act, (id: string) => void> = {
|
|
237
|
+
accept,
|
|
238
|
+
up: (id) => move(id, -1),
|
|
239
|
+
down: (id) => move(id, 1),
|
|
240
|
+
dismiss,
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Accepting in a browser edits the element the way typing does, caret and
|
|
244
|
+
// all, so the host's own input listener hears it too and the list rolls on
|
|
245
|
+
// from the word taken (`.task.status=` into its values).
|
|
246
|
+
let take = (
|
|
247
|
+
id: string,
|
|
248
|
+
el: HTMLInputElement | HTMLTextAreaElement,
|
|
249
|
+
i?: number,
|
|
250
|
+
) => {
|
|
251
|
+
let r = now(id)
|
|
252
|
+
if (!r?.cands.length) return
|
|
253
|
+
let t = taken(r, i)
|
|
254
|
+
el.value = t.text
|
|
255
|
+
el.setSelectionRange(t.caret, t.caret)
|
|
256
|
+
// an event of the element's own window, which may not be this realm's
|
|
257
|
+
let E = el.ownerDocument.defaultView?.Event ?? Event
|
|
258
|
+
el.dispatchEvent(new E('input', { bubbles: true }))
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// A key, taken through the element where there is one. An Enter the list
|
|
262
|
+
// leaves to the host sends what is typed as it stands, so the list closes.
|
|
263
|
+
let key = (id: string, name: string, el?: Anchor['current']) => {
|
|
264
|
+
let a = act(now(id), name)
|
|
265
|
+
if (a == 'accept' && el) take(id, el)
|
|
266
|
+
else if (a) acts[a](id)
|
|
267
|
+
else if (name == 'Enter') dismiss(id)
|
|
268
|
+
return !!a
|
|
269
|
+
}
|
|
270
|
+
let press = (id: string, name: string) => key(id, name)
|
|
271
|
+
|
|
272
|
+
let choices = (r?: Row): Suggestion[] =>
|
|
273
|
+
(r?.cands ?? []).map((c) => ({
|
|
274
|
+
value: c.text,
|
|
275
|
+
label: c.text,
|
|
276
|
+
note: c.kind,
|
|
277
|
+
}))
|
|
278
|
+
|
|
279
|
+
let listId = (id: string) => id + '-completion'
|
|
280
|
+
|
|
281
|
+
let ChoicesList = (
|
|
282
|
+
{ id, r, anchor }: { id: string; r: Row; anchor: Anchor },
|
|
283
|
+
) =>
|
|
284
|
+
h(Combobox.List, {
|
|
285
|
+
id: listId(id),
|
|
286
|
+
anchor,
|
|
287
|
+
options: choices(r),
|
|
288
|
+
active: r.pick,
|
|
289
|
+
code: true,
|
|
290
|
+
Float,
|
|
291
|
+
onDismiss: () => dismiss(id),
|
|
292
|
+
onChoose: (_value, i) => {
|
|
293
|
+
if (anchor.current) take(id, anchor.current, i)
|
|
294
|
+
else accept(id, i)
|
|
295
|
+
},
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
let List = ({ id, anchor }: { id: string; anchor: Anchor }) => {
|
|
299
|
+
let r = useLive(id)
|
|
300
|
+
return r?.cands.length ? h(ChoicesList, { id, r, anchor }) : null
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
let selection = (id: string, el: HTMLInputElement | HTMLTextAreaElement) => {
|
|
304
|
+
let caret = el.selectionStart ?? el.value.length
|
|
305
|
+
if (now(id)?.caret != caret) type(id, el.value, caret)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
let bind = (id: string, el: HTMLInputElement | HTMLTextAreaElement) => {
|
|
309
|
+
if (!now(id)) set(id, el.value)
|
|
310
|
+
let input = () => type(id, el.value, el.selectionStart ?? el.value.length)
|
|
311
|
+
let keydown = (event: Event) => {
|
|
312
|
+
let e = event as KeyboardEvent
|
|
313
|
+
if (modified(e) || !key(id, e.key, el)) return
|
|
314
|
+
e.preventDefault()
|
|
315
|
+
e.stopPropagation()
|
|
316
|
+
}
|
|
317
|
+
let blur = () => dismiss(id)
|
|
318
|
+
let selected = () => selection(id, el)
|
|
319
|
+
el.addEventListener('input', input)
|
|
320
|
+
el.addEventListener('keydown', keydown)
|
|
321
|
+
el.addEventListener('blur', blur)
|
|
322
|
+
for (let event of ['click', 'select', 'keyup']) {
|
|
323
|
+
el.addEventListener(event, selected)
|
|
324
|
+
}
|
|
325
|
+
let last: Row | undefined | null = null
|
|
326
|
+
let off = follow(id, () => {
|
|
327
|
+
let row = live(id)
|
|
328
|
+
if (row === last) return
|
|
329
|
+
last = row
|
|
330
|
+
let r = row ?? placed('')
|
|
331
|
+
let changed = el.value != r.text
|
|
332
|
+
if (changed) el.value = r.text
|
|
333
|
+
// Selection reports its start as the caret; preserve the selected range
|
|
334
|
+
// when that report comes back through the graph.
|
|
335
|
+
if (changed || el.selectionStart != r.caret) {
|
|
336
|
+
el.setSelectionRange(r.caret, r.caret)
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
return () => {
|
|
340
|
+
off()
|
|
341
|
+
el.removeEventListener('input', input)
|
|
342
|
+
el.removeEventListener('keydown', keydown)
|
|
343
|
+
el.removeEventListener('blur', blur)
|
|
344
|
+
for (let event of ['click', 'select', 'keyup']) {
|
|
345
|
+
el.removeEventListener(event, selected)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let Field = (p: FieldProps): ReactElement => {
|
|
351
|
+
let own = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
|
|
352
|
+
let box = p.elRef ?? own
|
|
353
|
+
useLayoutEffect(() => {
|
|
354
|
+
if (!now(p.id)) set(p.id, p.initial ?? '')
|
|
355
|
+
}, [p.id])
|
|
356
|
+
useLayoutEffect(() => {
|
|
357
|
+
let el = box.current
|
|
358
|
+
if (!el) return
|
|
359
|
+
let caret = now(p.id)?.caret ?? el.value.length
|
|
360
|
+
el.setSelectionRange(caret, caret)
|
|
361
|
+
if (p.focus) el.focus()
|
|
362
|
+
}, [p.id])
|
|
363
|
+
let r = useLive(p.id)
|
|
364
|
+
let text = r?.text ?? p.initial ?? ''
|
|
365
|
+
return h(
|
|
366
|
+
Fragment,
|
|
367
|
+
null,
|
|
368
|
+
h(Input, {
|
|
369
|
+
elRef: box,
|
|
370
|
+
lines: p.lines,
|
|
371
|
+
mod: p.mod,
|
|
372
|
+
className: p.className ?? p.class,
|
|
373
|
+
placeholder: p.placeholder ?? 'Type to search…',
|
|
374
|
+
'aria-label': p['aria-label'],
|
|
375
|
+
...comboboxAttrs(listId(p.id), choices(r), r?.pick ?? 0),
|
|
376
|
+
value: text,
|
|
377
|
+
caret: p.active ? r?.caret ?? text.length : undefined,
|
|
378
|
+
onInput: (e: InputEvent) => {
|
|
379
|
+
let el = e.currentTarget as HTMLInputElement
|
|
380
|
+
type(p.id, el.value, el.selectionStart ?? el.value.length)
|
|
381
|
+
p.onInput?.(e)
|
|
382
|
+
},
|
|
383
|
+
onKeyDown: (e: KeyboardEvent) => {
|
|
384
|
+
let el = e.currentTarget as HTMLInputElement
|
|
385
|
+
if (modified(e) || !key(p.id, e.key, el)) return p.onKey?.(e)
|
|
386
|
+
e.preventDefault()
|
|
387
|
+
e.stopPropagation()
|
|
388
|
+
},
|
|
389
|
+
onClick: (e: MouseEvent) =>
|
|
390
|
+
selection(p.id, e.currentTarget as HTMLInputElement),
|
|
391
|
+
onSelect: (e: Event) =>
|
|
392
|
+
selection(p.id, e.currentTarget as HTMLInputElement),
|
|
393
|
+
onKeyUp: (e: KeyboardEvent) =>
|
|
394
|
+
selection(p.id, e.currentTarget as HTMLInputElement),
|
|
395
|
+
onBlur: (e: FocusEvent) => {
|
|
396
|
+
dismiss(p.id)
|
|
397
|
+
p.onBlur?.(e)
|
|
398
|
+
},
|
|
399
|
+
}),
|
|
400
|
+
r?.cands.length ? h(ChoicesList, { id: p.id, r, anchor: box }) : null,
|
|
401
|
+
)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return {
|
|
405
|
+
Field,
|
|
406
|
+
List,
|
|
407
|
+
bind,
|
|
408
|
+
dispose,
|
|
409
|
+
row: live,
|
|
410
|
+
useRow: useLive,
|
|
411
|
+
text: (id) => live(id)?.text ?? '',
|
|
412
|
+
type,
|
|
413
|
+
set,
|
|
414
|
+
move,
|
|
415
|
+
accept,
|
|
416
|
+
dismiss,
|
|
417
|
+
press,
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export {
|
|
422
|
+
type Act,
|
|
423
|
+
act,
|
|
424
|
+
type Cand,
|
|
425
|
+
CAP,
|
|
426
|
+
dismissed,
|
|
427
|
+
moved,
|
|
428
|
+
placed,
|
|
429
|
+
type Result,
|
|
430
|
+
type Row,
|
|
431
|
+
taken,
|
|
432
|
+
typed,
|
|
433
|
+
} from './completion-state.js'
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Binds every composer on the page, and those later pages swap in, to
|
|
2
|
+
* send on Enter (./composer.ts `composing`). @module */ import { composing } from "./composer.js";
|
|
3
|
+
if (typeof document != 'undefined') composing(document);
|
|
4
|
+
//# sourceMappingURL=composer-browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composer-browser.js","sources":["./composer-browser.ts"],"names":[],"mappings":"AAAA;sDACsD,GACtD,SAAS,SAAS,wBAAuB;AAEzC,IAAI,OAAO,YAAY,aAAa,UAAU"}
|
package/composer.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a composer (@yaks/ui `Composer`) behaves: Enter sends what is typed,
|
|
3
|
+
* Shift+Enter starts a new line, and the Enter that ends a composition (an
|
|
4
|
+
* IME, a phone's keyboard) only ends it. The field grows with its lines by
|
|
5
|
+
* its own CSS (`field-sizing: content`), with no script.
|
|
6
|
+
*
|
|
7
|
+
* A part drawn in the browser asks {@link sends} of its own keys. A page
|
|
8
|
+
* drawn on a server binds every composer it holds, and every one a later
|
|
9
|
+
* page swaps in, once with {@link composing} (`@yaks/ux/composer-browser`).
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { sends } from '@yaks/ux/composer'
|
|
13
|
+
* import { equal } from '@yaks/testing'
|
|
14
|
+
*
|
|
15
|
+
* let key = (key: string, more = {}) =>
|
|
16
|
+
* sends({ key, shiftKey: false, isComposing: false, ...more })
|
|
17
|
+
* equal(key('Enter'), true)
|
|
18
|
+
* equal(key('Enter', { shiftKey: true }), false)
|
|
19
|
+
* equal(key('Enter', { isComposing: true }), false)
|
|
20
|
+
* equal(key('a'), false)
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @module
|
|
24
|
+
*/ /** What of a key press decides whether it sends. */ /** Whether `ev` sends a composer's words rather than typing in it. */ export let sends = (ev)=>ev.key == 'Enter' && !ev.shiftKey && !ev.isComposing;
|
|
25
|
+
/** The class every composer's field wears. */ export let FIELD = 'Composer_Field';
|
|
26
|
+
let bound = new WeakSet();
|
|
27
|
+
/** Enter in any composer's field in `doc` sends its form, as its button
|
|
28
|
+
* does (`requestSubmit`, so the form checks its fields and its listeners
|
|
29
|
+
* hear it). A field whose own handler took the key (`defaultPrevented`) is
|
|
30
|
+
* left to it. Binding a document twice binds it once. */ export let composing = (doc)=>{
|
|
31
|
+
if (bound.has(doc)) return;
|
|
32
|
+
bound.add(doc);
|
|
33
|
+
doc.addEventListener('keydown', (ev)=>{
|
|
34
|
+
let field = ev.target;
|
|
35
|
+
if (ev.defaultPrevented || !sends(ev)) return;
|
|
36
|
+
let form = field?.classList?.contains(FIELD) && field.closest('form');
|
|
37
|
+
if (!form) return;
|
|
38
|
+
ev.preventDefault();
|
|
39
|
+
form.requestSubmit();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=composer.js.map
|
package/composer.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composer.js","sources":["./composer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;CAuBC,GAED,kDAAkD,GAGlD,oEAAoE,GACpE,OAAO,IAAI,QAAQ,CAAC,KAClB,GAAG,GAAG,IAAI,WAAW,CAAC,GAAG,QAAQ,IAAI,CAAC,GAAG,WAAW,CAAA;AAEtD,4CAA4C,GAC5C,OAAO,IAAI,QAAQ,iBAAgB;AAEnC,IAAI,QAAQ,IAAI;AAEhB;;;uDAGuD,GACvD,OAAO,IAAI,YAAY,CAAC;EACtB,IAAI,MAAM,GAAG,CAAC,MAAM;EACpB,MAAM,GAAG,CAAC;EACV,IAAI,gBAAgB,CAAC,WAAW,CAAC;IAC/B,IAAI,QAAQ,GAAG,MAAM;IACrB,IAAI,GAAG,gBAAgB,IAAI,CAAC,MAAM,KAAK;IACvC,IAAI,OAAO,OAAO,WAAW,SAAS,UAAU,MAAM,OAAO,CAAC;IAC9D,IAAI,CAAC,MAAM;IACX,GAAG,cAAc;IACjB,KAAK,aAAa;EACpB;AACF,EAAC"}
|
package/composer.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a composer (@yaks/ui `Composer`) behaves: Enter sends what is typed,
|
|
3
|
+
* Shift+Enter starts a new line, and the Enter that ends a composition (an
|
|
4
|
+
* IME, a phone's keyboard) only ends it. The field grows with its lines by
|
|
5
|
+
* its own CSS (`field-sizing: content`), with no script.
|
|
6
|
+
*
|
|
7
|
+
* A part drawn in the browser asks {@link sends} of its own keys. A page
|
|
8
|
+
* drawn on a server binds every composer it holds, and every one a later
|
|
9
|
+
* page swaps in, once with {@link composing} (`@yaks/ux/composer-browser`).
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { sends } from '@yaks/ux/composer'
|
|
13
|
+
* import { equal } from '@yaks/testing'
|
|
14
|
+
*
|
|
15
|
+
* let key = (key: string, more = {}) =>
|
|
16
|
+
* sends({ key, shiftKey: false, isComposing: false, ...more })
|
|
17
|
+
* equal(key('Enter'), true)
|
|
18
|
+
* equal(key('Enter', { shiftKey: true }), false)
|
|
19
|
+
* equal(key('Enter', { isComposing: true }), false)
|
|
20
|
+
* equal(key('a'), false)
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @module
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** What of a key press decides whether it sends. */
|
|
27
|
+
export type Key = Pick<KeyboardEvent, 'key' | 'shiftKey' | 'isComposing'>
|
|
28
|
+
|
|
29
|
+
/** Whether `ev` sends a composer's words rather than typing in it. */
|
|
30
|
+
export let sends = (ev: Key): boolean =>
|
|
31
|
+
ev.key == 'Enter' && !ev.shiftKey && !ev.isComposing
|
|
32
|
+
|
|
33
|
+
/** The class every composer's field wears. */
|
|
34
|
+
export let FIELD = 'Composer_Field'
|
|
35
|
+
|
|
36
|
+
let bound = new WeakSet<Document>()
|
|
37
|
+
|
|
38
|
+
/** Enter in any composer's field in `doc` sends its form, as its button
|
|
39
|
+
* does (`requestSubmit`, so the form checks its fields and its listeners
|
|
40
|
+
* hear it). A field whose own handler took the key (`defaultPrevented`) is
|
|
41
|
+
* left to it. Binding a document twice binds it once. */
|
|
42
|
+
export let composing = (doc: Document): void => {
|
|
43
|
+
if (bound.has(doc)) return
|
|
44
|
+
bound.add(doc)
|
|
45
|
+
doc.addEventListener('keydown', (ev) => {
|
|
46
|
+
let field = ev.target as Element | null
|
|
47
|
+
if (ev.defaultPrevented || !sends(ev)) return
|
|
48
|
+
let form = field?.classList?.contains(FIELD) && field.closest('form')
|
|
49
|
+
if (!form) return
|
|
50
|
+
ev.preventDefault()
|
|
51
|
+
form.requestSubmit()
|
|
52
|
+
})
|
|
53
|
+
}
|