@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.
Files changed (286) hide show
  1. package/Answer.js +43 -0
  2. package/Answer.js.map +1 -0
  3. package/Answer.ts +84 -0
  4. package/Conversation.js +520 -0
  5. package/Conversation.js.map +1 -0
  6. package/Conversation.ts +649 -0
  7. package/ConversationBox.js +106 -0
  8. package/ConversationBox.js.map +1 -0
  9. package/ConversationBox.ts +198 -0
  10. package/ConversationLines.js +29 -0
  11. package/ConversationLines.js.map +1 -0
  12. package/ConversationLines.ts +48 -0
  13. package/Disclosure.js +79 -0
  14. package/Disclosure.js.map +1 -0
  15. package/Disclosure.ts +130 -0
  16. package/Edit.js +330 -0
  17. package/Edit.js.map +1 -0
  18. package/Edit.ts +488 -0
  19. package/EntityPopup.js +35 -0
  20. package/EntityPopup.js.map +1 -0
  21. package/EntityPopup.ts +52 -0
  22. package/Fields.js +161 -0
  23. package/Fields.js.map +1 -0
  24. package/Fields.ts +276 -0
  25. package/LICENSE +202 -0
  26. package/Order.js +105 -0
  27. package/Order.js.map +1 -0
  28. package/Order.ts +159 -0
  29. package/README.md +1023 -2
  30. package/Save.js +50 -0
  31. package/Save.js.map +1 -0
  32. package/Save.ts +115 -0
  33. package/Sort.js +49 -0
  34. package/Sort.js.map +1 -0
  35. package/Sort.ts +98 -0
  36. package/Submit.js +115 -0
  37. package/Submit.js.map +1 -0
  38. package/Submit.ts +174 -0
  39. package/Text.js +114 -0
  40. package/Text.js.map +1 -0
  41. package/Text.ts +140 -0
  42. package/TokenPicker.js +65 -0
  43. package/TokenPicker.js.map +1 -0
  44. package/TokenPicker.ts +117 -0
  45. package/_dist/Answer.d.ts +39 -0
  46. package/_dist/Answer.d.ts.map +1 -0
  47. package/_dist/Conversation.d.ts +131 -0
  48. package/_dist/Conversation.d.ts.map +1 -0
  49. package/_dist/ConversationBox.d.ts +29 -0
  50. package/_dist/ConversationBox.d.ts.map +1 -0
  51. package/_dist/ConversationLines.d.ts +13 -0
  52. package/_dist/ConversationLines.d.ts.map +1 -0
  53. package/_dist/Disclosure.d.ts +36 -0
  54. package/_dist/Disclosure.d.ts.map +1 -0
  55. package/_dist/Edit.d.ts +87 -0
  56. package/_dist/Edit.d.ts.map +1 -0
  57. package/_dist/EntityPopup.d.ts +12 -0
  58. package/_dist/EntityPopup.d.ts.map +1 -0
  59. package/_dist/Fields.d.ts +29 -0
  60. package/_dist/Fields.d.ts.map +1 -0
  61. package/_dist/Order.d.ts +40 -0
  62. package/_dist/Order.d.ts.map +1 -0
  63. package/_dist/Save.d.ts +28 -0
  64. package/_dist/Save.d.ts.map +1 -0
  65. package/_dist/Sort.d.ts +28 -0
  66. package/_dist/Sort.d.ts.map +1 -0
  67. package/_dist/Submit.d.ts +48 -0
  68. package/_dist/Submit.d.ts.map +1 -0
  69. package/_dist/Text.d.ts +27 -0
  70. package/_dist/Text.d.ts.map +1 -0
  71. package/_dist/TokenPicker.d.ts +19 -0
  72. package/_dist/TokenPicker.d.ts.map +1 -0
  73. package/_dist/anchor.d.ts +52 -0
  74. package/_dist/anchor.d.ts.map +1 -0
  75. package/_dist/anchored.d.ts +30 -0
  76. package/_dist/anchored.d.ts.map +1 -0
  77. package/_dist/completion-state.d.ts +65 -0
  78. package/_dist/completion-state.d.ts.map +1 -0
  79. package/_dist/completion.d.ts +73 -0
  80. package/_dist/completion.d.ts.map +1 -0
  81. package/_dist/composer-browser.d.ts +1 -0
  82. package/_dist/composer-browser.d.ts.map +1 -0
  83. package/_dist/composer.d.ts +31 -0
  84. package/_dist/composer.d.ts.map +1 -0
  85. package/_dist/context-menu-browser.d.ts +4 -0
  86. package/_dist/context-menu-browser.d.ts.map +1 -0
  87. package/_dist/context-menu.d.ts +95 -0
  88. package/_dist/context-menu.d.ts.map +1 -0
  89. package/_dist/drag.d.ts +29 -0
  90. package/_dist/drag.d.ts.map +1 -0
  91. package/_dist/edit-keys.d.ts +42 -0
  92. package/_dist/edit-keys.d.ts.map +1 -0
  93. package/_dist/emit.d.ts +7 -0
  94. package/_dist/emit.d.ts.map +1 -0
  95. package/_dist/entity-popup-browser.d.ts +3 -0
  96. package/_dist/entity-popup-browser.d.ts.map +1 -0
  97. package/_dist/face/FaceTagger.d.ts +25 -0
  98. package/_dist/face/FaceTagger.d.ts.map +1 -0
  99. package/_dist/face/review.d.ts +5 -0
  100. package/_dist/face/review.d.ts.map +1 -0
  101. package/_dist/face/tags.d.ts +99 -0
  102. package/_dist/face/tags.d.ts.map +1 -0
  103. package/_dist/field-values.d.ts +4 -0
  104. package/_dist/field-values.d.ts.map +1 -0
  105. package/_dist/fit-list.d.ts +4 -0
  106. package/_dist/fit-list.d.ts.map +1 -0
  107. package/_dist/hits.d.ts +27 -0
  108. package/_dist/hits.d.ts.map +1 -0
  109. package/_dist/host.d.ts +91 -0
  110. package/_dist/host.d.ts.map +1 -0
  111. package/_dist/image-fallback.d.ts +5 -0
  112. package/_dist/image-fallback.d.ts.map +1 -0
  113. package/_dist/keys.d.ts +95 -0
  114. package/_dist/keys.d.ts.map +1 -0
  115. package/_dist/kit.d.ts +23 -0
  116. package/_dist/kit.d.ts.map +1 -0
  117. package/_dist/lightbox-browser.d.ts +2 -0
  118. package/_dist/lightbox-browser.d.ts.map +1 -0
  119. package/_dist/lightbox.d.ts +42 -0
  120. package/_dist/lightbox.d.ts.map +1 -0
  121. package/_dist/live.d.ts +21 -0
  122. package/_dist/live.d.ts.map +1 -0
  123. package/_dist/mod.d.ts +67 -0
  124. package/_dist/mod.d.ts.map +1 -0
  125. package/_dist/read.d.ts +12 -0
  126. package/_dist/read.d.ts.map +1 -0
  127. package/_dist/resize.d.ts +43 -0
  128. package/_dist/resize.d.ts.map +1 -0
  129. package/_dist/rows.d.ts +31 -0
  130. package/_dist/rows.d.ts.map +1 -0
  131. package/_dist/save-browser.d.ts +29 -0
  132. package/_dist/save-browser.d.ts.map +1 -0
  133. package/_dist/select-browser.d.ts +1 -0
  134. package/_dist/select-browser.d.ts.map +1 -0
  135. package/_dist/select.d.ts +12 -0
  136. package/_dist/select.d.ts.map +1 -0
  137. package/_dist/sending.d.ts +10 -0
  138. package/_dist/sending.d.ts.map +1 -0
  139. package/_dist/sort-browser.d.ts +11 -0
  140. package/_dist/sort-browser.d.ts.map +1 -0
  141. package/_dist/state.d.ts +74 -0
  142. package/_dist/state.d.ts.map +1 -0
  143. package/_dist/suggest.d.ts +18 -0
  144. package/_dist/suggest.d.ts.map +1 -0
  145. package/_dist/swipe.d.ts +60 -0
  146. package/_dist/swipe.d.ts.map +1 -0
  147. package/_dist/ui.d.ts +6 -0
  148. package/_dist/ui.d.ts.map +1 -0
  149. package/_dist/upload-browser.d.ts +13 -0
  150. package/_dist/upload-browser.d.ts.map +1 -0
  151. package/_dist/views.d.ts +8 -0
  152. package/_dist/views.d.ts.map +1 -0
  153. package/_dist/vocab.d.ts +6 -0
  154. package/_dist/vocab.d.ts.map +1 -0
  155. package/anchor.js +58 -0
  156. package/anchor.js.map +1 -0
  157. package/anchor.ts +87 -0
  158. package/anchored.js +180 -0
  159. package/anchored.js.map +1 -0
  160. package/anchored.ts +232 -0
  161. package/completion-state.js +76 -0
  162. package/completion-state.js.map +1 -0
  163. package/completion-state.ts +120 -0
  164. package/completion.js +301 -0
  165. package/completion.js.map +1 -0
  166. package/completion.ts +433 -0
  167. package/composer-browser.js +4 -0
  168. package/composer-browser.js.map +1 -0
  169. package/composer-browser.ts +5 -0
  170. package/composer.js +42 -0
  171. package/composer.js.map +1 -0
  172. package/composer.ts +53 -0
  173. package/context-menu-browser.js +221 -0
  174. package/context-menu-browser.js.map +1 -0
  175. package/context-menu-browser.ts +231 -0
  176. package/context-menu.js +146 -0
  177. package/context-menu.js.map +1 -0
  178. package/context-menu.ts +194 -0
  179. package/deno.json +1 -0
  180. package/drag.js +33 -0
  181. package/drag.js.map +1 -0
  182. package/drag.ts +41 -0
  183. package/edit-keys.js +51 -0
  184. package/edit-keys.js.map +1 -0
  185. package/edit-keys.ts +81 -0
  186. package/emit.js +34 -0
  187. package/emit.js.map +1 -0
  188. package/emit.ts +55 -0
  189. package/entity-popup-browser.js +94 -0
  190. package/entity-popup-browser.js.map +1 -0
  191. package/entity-popup-browser.ts +100 -0
  192. package/face/FaceTagger.js +361 -0
  193. package/face/FaceTagger.js.map +1 -0
  194. package/face/FaceTagger.ts +507 -0
  195. package/face/NameProposal.js +61 -0
  196. package/face/NameProposal.js.map +1 -0
  197. package/face/NameProposal.ts +100 -0
  198. package/face/review.js +33 -0
  199. package/face/review.js.map +1 -0
  200. package/face/review.ts +49 -0
  201. package/face/tags.js +344 -0
  202. package/face/tags.js.map +1 -0
  203. package/face/tags.ts +357 -0
  204. package/field-values.js +20 -0
  205. package/field-values.js.map +1 -0
  206. package/field-values.ts +35 -0
  207. package/fit-list.js +66 -0
  208. package/fit-list.js.map +1 -0
  209. package/fit-list.ts +81 -0
  210. package/hits.js +65 -0
  211. package/hits.js.map +1 -0
  212. package/hits.ts +75 -0
  213. package/host.js +88 -0
  214. package/host.js.map +1 -0
  215. package/host.ts +212 -0
  216. package/image-fallback.js +32 -0
  217. package/image-fallback.js.map +1 -0
  218. package/image-fallback.ts +37 -0
  219. package/keys.js +418 -0
  220. package/keys.js.map +1 -0
  221. package/keys.ts +444 -0
  222. package/kit.js +41 -0
  223. package/kit.js.map +1 -0
  224. package/kit.ts +71 -0
  225. package/lightbox-browser.js +248 -0
  226. package/lightbox-browser.js.map +1 -0
  227. package/lightbox-browser.ts +232 -0
  228. package/lightbox.js +84 -0
  229. package/lightbox.js.map +1 -0
  230. package/lightbox.ts +103 -0
  231. package/live.js +111 -0
  232. package/live.js.map +1 -0
  233. package/live.ts +139 -0
  234. package/mod.js +67 -0
  235. package/mod.js.map +1 -0
  236. package/mod.ts +180 -0
  237. package/package.json +143 -4
  238. package/read.js +33 -0
  239. package/read.js.map +1 -0
  240. package/read.ts +56 -0
  241. package/resize.js +159 -0
  242. package/resize.js.map +1 -0
  243. package/resize.ts +203 -0
  244. package/rows.js +73 -0
  245. package/rows.js.map +1 -0
  246. package/rows.ts +97 -0
  247. package/save-browser.js +469 -0
  248. package/save-browser.js.map +1 -0
  249. package/save-browser.ts +530 -0
  250. package/select-browser.js +4 -0
  251. package/select-browser.js.map +1 -0
  252. package/select-browser.ts +5 -0
  253. package/select.js +296 -0
  254. package/select.js.map +1 -0
  255. package/select.ts +343 -0
  256. package/sending.js +36 -0
  257. package/sending.js.map +1 -0
  258. package/sending.ts +44 -0
  259. package/sort-browser.js +51 -0
  260. package/sort-browser.js.map +1 -0
  261. package/sort-browser.ts +70 -0
  262. package/state.js +99 -0
  263. package/state.js.map +1 -0
  264. package/state.ts +121 -0
  265. package/suggest.js +28 -0
  266. package/suggest.js.map +1 -0
  267. package/suggest.ts +32 -0
  268. package/swipe.js +186 -0
  269. package/swipe.js.map +1 -0
  270. package/swipe.ts +250 -0
  271. package/ui.js +507 -0
  272. package/ui.js.map +1 -0
  273. package/ui.ts +378 -0
  274. package/upload-browser.js +195 -0
  275. package/upload-browser.js.map +1 -0
  276. package/upload-browser.ts +303 -0
  277. package/upload-files.js +41 -0
  278. package/upload-files.js.map +1 -0
  279. package/upload-files.ts +49 -0
  280. package/views.js +8 -0
  281. package/views.js.map +1 -0
  282. package/views.ts +9 -0
  283. package/vocab.js +16 -0
  284. package/vocab.js.map +1 -0
  285. package/vocab.json +287 -0
  286. package/vocab.ts +17 -0
package/anchored.ts ADDED
@@ -0,0 +1,232 @@
1
+ /**
2
+ * A list in a browser that stays where its reader left it while things load:
3
+ * where they are is an item and how far into it their view begins
4
+ * (./anchor.ts), so earlier items put above,
5
+ * an image or a code block above sizing in, a fold opening and items arriving
6
+ * below all leave the item they were reading where it was on the screen. A
7
+ * reader at the end follows it, and stays at it as items arrive and a reply
8
+ * grows, until they scroll up from it. A list whose newest come at its top
9
+ * (`edge: 'start'`) is followed at its top the same way. Items that arrive at
10
+ * the edge while the reader is away from it are unseen until they come back
11
+ * to it, those the owner says count (`counts`): a conversation counts the
12
+ * words written to its reader, not the steps taken meanwhile.
13
+ *
14
+ * It watches what the list holds (`content`) and the place that scrolls it
15
+ * (`pane`), and puts the reader back after every change, before it is
16
+ * painted. The browser's own scroll anchoring is turned off on the pane while
17
+ * it holds it, since the two would both move the reader. It draws nothing,
18
+ * and works on any list drawn into the page, by a renderer or by hand.
19
+ *
20
+ * @module
21
+ */
22
+
23
+ import {
24
+ anchorAt,
25
+ type Box,
26
+ follows,
27
+ rekey,
28
+ topOf,
29
+ type ViewportState,
30
+ } from './anchor.js'
31
+
32
+ /** What it holds the reader in. */
33
+ export type AnchoredOptions = {
34
+ /** What holds the list: its changes and its size are watched. */
35
+ content: Element
36
+ /** The list's items in order. */
37
+ rows: () => Iterable<Element>
38
+ /** What an item is known by: its `id`, unless told otherwise. */
39
+ key?: (row: Element) => string | null
40
+ /** The edge its newest items come at, which a reader standing there
41
+ * follows: its end (a conversation), unless told its start. */
42
+ edge?: 'start' | 'end'
43
+ /** Whether it starts following that edge (it does). */
44
+ follow?: boolean
45
+ /** Told when the reader comes to the edge, or leaves it. */
46
+ onFollow?: (follow: boolean) => void
47
+ /** Whether an item arriving at the edge the reader is away from is one
48
+ * they haven't seen: every one, unless told. */
49
+ counts?: (row: Element) => boolean
50
+ /** Told when an item that counts arrives at the edge the reader is away
51
+ * from, and when they come back to it. */
52
+ onUnseen?: (unseen: boolean) => void
53
+ /** While true it neither moves the reader nor reads where they are: the
54
+ * list is hidden, or something is shown over it. */
55
+ idle?: () => boolean
56
+ }
57
+
58
+ /** A list holding its reader. */
59
+ export type Anchored = {
60
+ /** Take the reader to the edge, to follow it; sliding there, unless they
61
+ * asked for less motion. */
62
+ newest: (slide?: boolean) => void
63
+ /** Take where the reader is now as their place, after something else moved
64
+ * them: following the edge if they are at it, unless told otherwise. */
65
+ read: (follow?: boolean) => void
66
+ /** Put the reader back where they were, after the list changed. Changes it
67
+ * can see are held at once; this is for one it can't. */
68
+ hold: () => void
69
+ /** Whether the reader follows the edge. */
70
+ follows: () => boolean
71
+ /** Whether an item that counts arrived at the edge since the reader left
72
+ * it. */
73
+ unseen: () => boolean
74
+ /** Let the reader go. */
75
+ stop: () => void
76
+ }
77
+
78
+ /** The nearest place around `at` that scrolls, or the page. */
79
+ export let scroller = (at: Element): HTMLElement => {
80
+ let doc = at.ownerDocument
81
+ let win = doc.defaultView
82
+ for (let p = at.parentElement; p; p = p.parentElement) {
83
+ let y = win?.getComputedStyle?.(p).overflowY
84
+ if (y == 'auto' || y == 'scroll') return p
85
+ }
86
+ return (doc.scrollingElement ?? doc.documentElement) as HTMLElement
87
+ }
88
+
89
+ /** Hold `pane`'s reader in place as its list changes. */
90
+ export let anchored = (pane: HTMLElement, o: AnchoredOptions): Anchored => {
91
+ let doc = pane.ownerDocument
92
+ let win = doc.defaultView
93
+ let page = pane == (doc.scrollingElement ?? doc.documentElement)
94
+ let state: ViewportState = { follow: o.follow ?? true }
95
+ let ids: string[] = []
96
+ let y = pane.scrollTop
97
+ // Sliding to the edge: the end it slides to moves as the list grows.
98
+ let sliding = false
99
+ let unseen = false
100
+ let idle = () => o.idle?.() ?? false
101
+ let key = o.key ?? ((row: Element) => row.id)
102
+ let start = o.edge == 'start'
103
+ let edge = () =>
104
+ !pane.clientHeight ||
105
+ (start
106
+ ? pane.scrollTop <= 2
107
+ : pane.scrollTop + pane.clientHeight >= pane.scrollHeight - 2)
108
+ // Each item where it is, measured from the top of what the pane shows.
109
+ let laid = (): Box[] => {
110
+ let top = page ? 0 : pane.getBoundingClientRect().top + pane.clientTop
111
+ let boxes: Box[] = []
112
+ for (let el of o.rows()) {
113
+ let id = key(el)
114
+ if (!id) continue
115
+ let r = el.getBoundingClientRect()
116
+ boxes.push({ id, top: r.top - top, height: r.height })
117
+ }
118
+ return boxes
119
+ }
120
+ let see = (now: boolean) => {
121
+ if (now == unseen) return
122
+ unseen = now
123
+ o.onUnseen?.(now)
124
+ }
125
+ // The list's ids are now `now`: any that came at the edge while the reader
126
+ // was away from it, past the ids they knew, may be unseen.
127
+ let known = (now: string[]) => {
128
+ let was = new Set(ids)
129
+ let had = (id: string) => was.has(id)
130
+ let first = start ? now.findLastIndex(had) : now.findIndex(had)
131
+ let came = new Set(
132
+ first < 0
133
+ ? []
134
+ : now.filter((id, i) => !had(id) && (start ? i < first : i > first)),
135
+ )
136
+ ids = now
137
+ if (state.follow || unseen || !came.size) return
138
+ let counts = o.counts ?? (() => true)
139
+ for (let el of o.rows()) {
140
+ let id = key(el)
141
+ if (id && came.has(id) && counts(el)) return see(true)
142
+ }
143
+ }
144
+ let follow = (now: boolean) => {
145
+ if (!now) sliding = false
146
+ else see(false)
147
+ if (now == state.follow) return
148
+ state.follow = now
149
+ o.onFollow?.(now)
150
+ }
151
+ let read = () => {
152
+ let boxes = laid()
153
+ state.anchor = anchorAt(boxes)
154
+ known(boxes.map((b) => b.id))
155
+ y = pane.scrollTop
156
+ }
157
+ let toEdge = (slide: boolean) => {
158
+ let top = start ? 0 : pane.scrollHeight
159
+ if (pane.scrollTo) {
160
+ pane.scrollTo({ top, behavior: slide ? 'smooth' : 'instant' })
161
+ } else pane.scrollTop = top
162
+ sliding = slide
163
+ y = pane.scrollTop
164
+ }
165
+ let hold = () => {
166
+ if (idle()) return
167
+ if (state.follow) {
168
+ if (!edge()) toEdge(sliding)
169
+ return
170
+ }
171
+ if (!state.anchor) return read()
172
+ let boxes = laid()
173
+ let now = boxes.map((b) => b.id)
174
+ state.anchor = rekey(state.anchor, ids, now)
175
+ known(now)
176
+ let by = state.anchor && topOf(state.anchor, boxes)
177
+ if (by && Math.abs(by) >= 1) pane.scrollTop += by
178
+ y = pane.scrollTop
179
+ }
180
+ let scrolled = () => {
181
+ if (idle()) return
182
+ let now = pane.scrollTop
183
+ let was = y
184
+ let at = edge()
185
+ if (at) sliding = false
186
+ // Where they are first: their owner may draw again when told they left
187
+ // the edge. A slide moves toward the edge; any move away leaves it.
188
+ read()
189
+ let way = start ? -1 : 1
190
+ follow(follows(state.follow, { was: was * way, now: now * way, end: at }))
191
+ }
192
+ let heard = page ? doc : pane
193
+ heard.addEventListener('scroll', scrolled, { passive: true })
194
+ let anchoring = pane.style.overflowAnchor
195
+ pane.style.overflowAnchor = 'none'
196
+ let changes = win?.MutationObserver ? new win.MutationObserver(hold) : null
197
+ changes?.observe(o.content, {
198
+ childList: true,
199
+ subtree: true,
200
+ characterData: true,
201
+ })
202
+ let sizes = win?.ResizeObserver ? new win.ResizeObserver(hold) : null
203
+ sizes?.observe(o.content)
204
+ if (!page) sizes?.observe(pane)
205
+ win?.addEventListener('resize', hold)
206
+ read()
207
+ hold()
208
+ return {
209
+ newest: (slide = false) => {
210
+ if (idle()) return
211
+ follow(true)
212
+ let smooth = slide &&
213
+ !!win?.matchMedia?.('(prefers-reduced-motion: no-preference)').matches
214
+ toEdge(smooth)
215
+ },
216
+ read: (to?: boolean) => {
217
+ if (idle()) return
218
+ read()
219
+ follow(to ?? edge())
220
+ },
221
+ hold,
222
+ follows: () => state.follow,
223
+ unseen: () => unseen,
224
+ stop: () => {
225
+ heard.removeEventListener('scroll', scrolled)
226
+ win?.removeEventListener('resize', hold)
227
+ changes?.disconnect()
228
+ sizes?.disconnect()
229
+ pane.style.overflowAnchor = anchoring
230
+ },
231
+ }
232
+ }
@@ -0,0 +1,76 @@
1
+ /** Pure completion transitions; text is supplied by the host's draft. @module */ /** A candidate's replacement text and the note shown beside it. */ /** The most candidates the list shows. */ export let CAP = 8;
2
+ /** The field as the person left it: the text, the caret, and what can come
3
+ * next there, the first picked unless what is typed already reads whole.
4
+ *
5
+ * ```ts
6
+ * import { assertEquals } from '@std/assert'
7
+ * import { typed } from '@yaks/ux/completion'
8
+ *
9
+ * let cands = [{ text: '.task.status', kind: 'prop' }]
10
+ * let found = { from: 0, to: 2, cands, whole: false }
11
+ * assertEquals(typed('.s', 2, found).cands, found.cands)
12
+ * ```
13
+ */ export let typed = (text, caret, found)=>({
14
+ text,
15
+ caret,
16
+ from: found.from,
17
+ to: found.to,
18
+ cands: found.cands.slice(0, CAP),
19
+ pick: found.whole ? -1 : 0
20
+ });
21
+ /** The field as its host put it: the text, and nothing offered. */ export let placed = (text, caret = text.length)=>({
22
+ text,
23
+ caret,
24
+ from: caret,
25
+ to: caret,
26
+ cands: [],
27
+ pick: 0
28
+ });
29
+ /** The pick `d` rows on, held to the list. */ export let moved = (r, d)=>({
30
+ pick: Math.min(Math.max(r.pick + d, 0), r.cands.length - 1)
31
+ });
32
+ /** The text with candidate `i` (the pick, or else the first) in place of the
33
+ * word being completed, and the caret after it.
34
+ *
35
+ * ```ts
36
+ * import { assertEquals } from '@std/assert'
37
+ * import { taken, typed } from '@yaks/ux/completion'
38
+ *
39
+ * let cands = [{ text: '.task.status=', kind: 'is' }]
40
+ * let found = { from: 6, to: 8, cands, whole: false }
41
+ * assertEquals(taken(typed('.task .s x', 8, found)), {
42
+ * text: '.task .task.status= x',
43
+ * caret: 19,
44
+ * })
45
+ * ```
46
+ */ export let taken = (r, i = Math.max(r.pick, 0))=>{
47
+ let word = r.cands[i]?.text ?? r.text.slice(r.from, r.to);
48
+ return {
49
+ text: r.text.slice(0, r.from) + word + r.text.slice(r.to),
50
+ caret: r.from + word.length
51
+ };
52
+ };
53
+ /** The list closed. */ export let dismissed = {
54
+ cands: [],
55
+ pick: 0
56
+ };
57
+ // The keys, named as a browser names them; a terminal spells its own the same.
58
+ let KEYS = {
59
+ Tab: 'accept',
60
+ Enter: 'accept',
61
+ ArrowUp: 'up',
62
+ ArrowDown: 'down',
63
+ Escape: 'dismiss'
64
+ };
65
+ /** The act a key is on this row: one of the list's while it is open, and none
66
+ * otherwise, so the key is its host's. Enter takes only a pick, so with
67
+ * nothing picked it is the host's too.
68
+ *
69
+ * ```ts
70
+ * import { assertEquals } from '@std/assert'
71
+ * import { act, placed } from '@yaks/ux/completion'
72
+ *
73
+ * assertEquals(act(placed('.s'), 'Enter'), undefined)
74
+ * ```
75
+ */ export let act = (r, key)=>!r?.cands.length || key == 'Enter' && r.pick < 0 ? undefined : KEYS[key];
76
+ //# sourceMappingURL=completion-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"completion-state.js","sources":["./completion-state.ts"],"names":[],"mappings":"AAAA,+EAA+E,GAE/E,iEAAiE,GAyBjE,wCAAwC,GACxC,OAAO,IAAI,MAAM,EAAC;AAElB;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,QAAQ,CAAC,MAAc,OAAe,QAAuB,CAAC;IACvE;IACA;IACA,MAAM,MAAM,IAAI;IAChB,IAAI,MAAM,EAAE;IACZ,OAAO,MAAM,KAAK,CAAC,KAAK,CAAC,GAAG;IAC5B,MAAM,MAAM,KAAK,GAAG,CAAC,IAAI;EAC3B,CAAC,EAAC;AAEF,iEAAiE,GACjE,OAAO,IAAI,SAAS,CAAC,MAAc,QAAQ,KAAK,MAAM,GAAU,CAAC;IAC/D;IACA;IACA,MAAM;IACN,IAAI;IACJ,OAAO,EAAE;IACT,MAAM;EACR,CAAC,EAAC;AAEF,4CAA4C,GAC5C,OAAO,IAAI,QAAQ,CAAC,GAAQ,IAA4B,CAAC;IACvD,MAAM,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,GAAG,GAAG,IAAI,EAAE,KAAK,CAAC,MAAM,GAAG;EAC3D,CAAC,EAAC;AAEF;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,QAAQ,CACjB,GACA,IAAY,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE;EAE/B,IAAI,OAAO,EAAE,KAAK,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE;EACxD,OAAO;IACL,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,IAAI,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE;IACxD,OAAO,EAAE,IAAI,GAAG,KAAK,MAAM;EAC7B;AACF,EAAC;AAED,qBAAqB,GACrB,OAAO,IAAI,YAA0B;EAAE,OAAO,EAAE;EAAE,MAAM;AAAE,EAAC;AAK3D,+EAA+E;AAC/E,IAAI,OAA4B;EAC9B,KAAK;EACL,OAAO;EACP,SAAS;EACT,WAAW;EACX,QAAQ;AACV;AAEA;;;;;;;;;;CAUC,GACD,OAAO,IAAI,MAAM,CAAC,GAAoB,MACpC,CAAC,GAAG,MAAM,UAAU,OAAO,WAAW,EAAE,IAAI,GAAG,IAAI,YAAY,IAAI,CAAC,IAAI,CAAA"}
@@ -0,0 +1,120 @@
1
+ /** Pure completion transitions; text is supplied by the host's draft. @module */
2
+
3
+ /** A candidate's replacement text and the note shown beside it. */
4
+ export type Cand = { text: string; kind: string }
5
+
6
+ /** What the domain offers at the caret. Offsets span the text to replace. */
7
+ export type Result = {
8
+ from: number
9
+ to: number
10
+ cands: Cand[]
11
+ whole: boolean
12
+ }
13
+
14
+ /** What the `Completion` component holds. */
15
+ export type Row = {
16
+ text: string
17
+ caret: number
18
+ /** where the word being completed starts */
19
+ from: number
20
+ /** where it ends */
21
+ to: number
22
+ /** what can replace it */
23
+ cands: Cand[]
24
+ /** which of `cands` is picked, -1 for none */
25
+ pick: number
26
+ }
27
+
28
+ /** The most candidates the list shows. */
29
+ export let CAP = 8
30
+
31
+ /** The field as the person left it: the text, the caret, and what can come
32
+ * next there, the first picked unless what is typed already reads whole.
33
+ *
34
+ * ```ts
35
+ * import { assertEquals } from '@std/assert'
36
+ * import { typed } from '@yaks/ux/completion'
37
+ *
38
+ * let cands = [{ text: '.task.status', kind: 'prop' }]
39
+ * let found = { from: 0, to: 2, cands, whole: false }
40
+ * assertEquals(typed('.s', 2, found).cands, found.cands)
41
+ * ```
42
+ */
43
+ export let typed = (text: string, caret: number, found: Result): Row => ({
44
+ text,
45
+ caret,
46
+ from: found.from,
47
+ to: found.to,
48
+ cands: found.cands.slice(0, CAP),
49
+ pick: found.whole ? -1 : 0,
50
+ })
51
+
52
+ /** The field as its host put it: the text, and nothing offered. */
53
+ export let placed = (text: string, caret = text.length): Row => ({
54
+ text,
55
+ caret,
56
+ from: caret,
57
+ to: caret,
58
+ cands: [],
59
+ pick: 0,
60
+ })
61
+
62
+ /** The pick `d` rows on, held to the list. */
63
+ export let moved = (r: Row, d: number): Partial<Row> => ({
64
+ pick: Math.min(Math.max(r.pick + d, 0), r.cands.length - 1),
65
+ })
66
+
67
+ /** The text with candidate `i` (the pick, or else the first) in place of the
68
+ * word being completed, and the caret after it.
69
+ *
70
+ * ```ts
71
+ * import { assertEquals } from '@std/assert'
72
+ * import { taken, typed } from '@yaks/ux/completion'
73
+ *
74
+ * let cands = [{ text: '.task.status=', kind: 'is' }]
75
+ * let found = { from: 6, to: 8, cands, whole: false }
76
+ * assertEquals(taken(typed('.task .s x', 8, found)), {
77
+ * text: '.task .task.status= x',
78
+ * caret: 19,
79
+ * })
80
+ * ```
81
+ */
82
+ export let taken = (
83
+ r: Row,
84
+ i: number = Math.max(r.pick, 0),
85
+ ): { text: string; caret: number } => {
86
+ let word = r.cands[i]?.text ?? r.text.slice(r.from, r.to)
87
+ return {
88
+ text: r.text.slice(0, r.from) + word + r.text.slice(r.to),
89
+ caret: r.from + word.length,
90
+ }
91
+ }
92
+
93
+ /** The list closed. */
94
+ export let dismissed: Partial<Row> = { cands: [], pick: 0 }
95
+
96
+ /** What a key does while the list is open. */
97
+ export type Act = 'accept' | 'up' | 'down' | 'dismiss'
98
+
99
+ // The keys, named as a browser names them; a terminal spells its own the same.
100
+ let KEYS: Record<string, Act> = {
101
+ Tab: 'accept',
102
+ Enter: 'accept',
103
+ ArrowUp: 'up',
104
+ ArrowDown: 'down',
105
+ Escape: 'dismiss',
106
+ }
107
+
108
+ /** The act a key is on this row: one of the list's while it is open, and none
109
+ * otherwise, so the key is its host's. Enter takes only a pick, so with
110
+ * nothing picked it is the host's too.
111
+ *
112
+ * ```ts
113
+ * import { assertEquals } from '@std/assert'
114
+ * import { act, placed } from '@yaks/ux/completion'
115
+ *
116
+ * assertEquals(act(placed('.s'), 'Enter'), undefined)
117
+ * ```
118
+ */
119
+ export let act = (r: Row | undefined, key: string): Act | undefined =>
120
+ !r?.cands.length || key == 'Enter' && r.pick < 0 ? undefined : KEYS[key]