@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/hits.ts ADDED
@@ -0,0 +1,75 @@
1
+ // A picker's options, asked of the GRAPH rather than scanned from what the
2
+ // page holds. A scan offers only what happens to be resident, so under a
3
+ // partial cache a picker silently narrows to the entities already loaded;
4
+ // the server answers over the whole graph. The host's `find` is the one
5
+ // server search; the hook here debounces it and aborts a stale request, so a
6
+ // late answer never repaints a newer query.
7
+ /* @ts-types="npm:@types/react@19.3.0" */ import { useEffect, useState } from 'react'
8
+ import { type Clock, system } from '@yaks/fp'
9
+
10
+ /** A server search: the entities a line answers, at most `limit`. */
11
+ export type Find<T> = (
12
+ line: string,
13
+ limit: number,
14
+ signal?: AbortSignal,
15
+ ) => Promise<T[]>
16
+
17
+ /// pickLine('T-3') -> 'T-3 *'
18
+ /// pickLine('') -> '.doc *'
19
+ /// pickLine('ali', 'person') -> 'ali .person *'
20
+ /// pickLine('', 'person') -> '.person *'
21
+ /**
22
+ * A picker's query line: its standing component filter ANDed with what's
23
+ * typed. `comp` is the component every candidate must wear ('' = any doc); an
24
+ * empty line still lists recent candidates by the presence filter. A doc
25
+ * picker passes '' rather than 'doc' on purpose: with no component filter a
26
+ * typed line stays a SINGLE text predicate, so the server's id addressing
27
+ * still resolves a typed human id (T-3); full-text search returns only
28
+ * documented entities anyway.
29
+ *
30
+ * The typed text leads and the presence filter trails ('ali .person', never
31
+ * '.person ali'): an &-segment that STARTS with a dot-param reads as one
32
+ * token, so a term after it would land inside the value; a leading term puts
33
+ * a ' .' in the segment, the boundary that splits it into a text predicate
34
+ * plus the presence one. An empty line is the bare filter, which parses
35
+ * alone.
36
+ *
37
+ * `*` asks for each candidate whole: a presence filter alone answers only the
38
+ * component it names, and a candidate is labelled by its title.
39
+ */
40
+ export let pickLine = (q: string, comp = ''): string => {
41
+ q = q.trim()
42
+ let filter = comp ? `.${comp}` : q ? '' : '.doc'
43
+ return [q, filter, '*'].filter(Boolean).join(' ')
44
+ }
45
+
46
+ /** A live picker's hits, asked of `find`: refetched as the line settles
47
+ * (`settle` ms on `clock` without a change, 150 by default), the last request
48
+ * aborted so a slow answer can't overwrite a newer one. An empty line clears
49
+ * the list without a round trip. The answer is an answer to a query, not a
50
+ * component's own state, so it is held here. */
51
+ export let useHits = <T>(
52
+ line: string,
53
+ limit: number,
54
+ find: Find<T>,
55
+ settle = 150,
56
+ clock: Clock = system,
57
+ ): T[] => {
58
+ let [found, setFound] = useState<T[]>([])
59
+ useEffect(() => {
60
+ if (!line) {
61
+ setFound([])
62
+ return
63
+ }
64
+ let abort = new AbortController()
65
+ let cancel = clock.after(
66
+ settle,
67
+ () => find(line, limit, abort.signal).then(setFound).catch(() => {}),
68
+ )
69
+ return () => {
70
+ cancel()
71
+ abort.abort()
72
+ }
73
+ }, [line, limit, settle, clock])
74
+ return found
75
+ }
package/host.js ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * What a UX component asks of the page it is on, handed down once from the
3
+ * root of the tree it draws (`Ux`), so one page may hold several: web's page
4
+ * and the inspector's views on one of its cards each hand down their own. The
5
+ * host says what the values mean (the vocabulary, what an entity is called),
6
+ * where each component keeps its own state (the page's graph), where what the
7
+ * person types waits until it is sent (their drafts), where a bundle it emits
8
+ * goes when its caller names nowhere else, and the platform's primitives
9
+ * (where a popout floats). The rest is optional: a host without it
10
+ * gets the plain behavior (no suggestions, a query typed as text, no inline
11
+ * markdown, a popout in the flow).
12
+ *
13
+ * @module
14
+ */ import { useStore } from "@yaks/ui/store";
15
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createContext, createElement as h, useCallback, useContext } from "react";
16
+ let Given = /*#__PURE__*/ createContext({
17
+ at: ''
18
+ });
19
+ /**
20
+ * `<Ux host={host}>…</Ux>`: every UX component under it reads through
21
+ * `host`. `at` names the consumer that owns what is under it (a card's own
22
+ * eid, a view drawn on it), so the same value drawn in two places keeps two
23
+ * states, and a remount finds its own again. An inner `Ux` names a consumer
24
+ * within the one above it, and keeps its host unless given one.
25
+ */ export let Ux = ({ host, at, children })=>{
26
+ let up = useContext(Given);
27
+ return /*#__PURE__*/ h(Given.Provider, {
28
+ value: {
29
+ host: host ?? up.host,
30
+ at: at == null ? up.at : up.at ? `${up.at}/${at}` : at
31
+ },
32
+ children
33
+ });
34
+ };
35
+ /** The host handed down. */ export let useHost = ()=>{
36
+ let { host } = useContext(Given);
37
+ if (!host) throw new Error('@yaks/ux: no host above (wrap the tree in Ux)');
38
+ return host;
39
+ };
40
+ /** The consumer that owns what is drawn here: every `Ux`'s `at` above it,
41
+ * outermost first. */ export let useOwner = ()=>useContext(Given).at;
42
+ /** Drafts supplied directly by a control's caller or inherited from its host. */ export let useDrafts = (drafts)=>{
43
+ let host = useContext(Given).host;
44
+ let found = drafts ?? host?.drafts;
45
+ if (!found) throw new Error('@yaks/ux: supply drafts or wrap the tree in Ux');
46
+ return found;
47
+ };
48
+ /** What `read` answers of `drafts`, read again whenever a text it read
49
+ * changes. `deps` are what `read` closes over. */ export let useFollowed = (drafts, read, deps)=>{
50
+ let subscribe = useCallback((wake)=>drafts.follow?.(()=>{
51
+ read();
52
+ wake();
53
+ }) ?? (()=>{}), [
54
+ drafts,
55
+ ...deps
56
+ ]);
57
+ return useStore(subscribe, read);
58
+ };
59
+ /** What is typed at `place` in `drafts` (the host's, unless given), drawn
60
+ * again as it changes. */ export let useDraft = (place, drafts)=>{
61
+ let d = useDrafts(drafts);
62
+ return useFollowed(d, ()=>d.text(place), [
63
+ place
64
+ ]);
65
+ };
66
+ /** Drafts held in this page's memory and lost with it: a specimen's, a
67
+ * test's. A page a person types in keeps theirs in a desk (@yaks/draft). */ export let heldDrafts = ()=>{
68
+ let texts = new Map();
69
+ let followers = new Set();
70
+ let set = (place, text)=>{
71
+ if ((texts.get(place) ?? '') == text) return;
72
+ texts.set(place, text);
73
+ for (let fn of [
74
+ ...followers
75
+ ])fn();
76
+ };
77
+ return {
78
+ text: (place)=>texts.get(place) ?? '',
79
+ type: set,
80
+ spend: (place)=>set(place, ''),
81
+ follow: (fn)=>{
82
+ followers.add(fn);
83
+ fn();
84
+ return ()=>void followers.delete(fn);
85
+ }
86
+ };
87
+ };
88
+ //# sourceMappingURL=host.js.map
package/host.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","sources":["./host.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;CAaC,GAED,SAAS,QAAQ,8BAAmC;AACpD,uCAAuC,GAAG,SACxC,aAAa,EACb,iBAAiB,CAAC,EAIlB,WAAW,EACX,UAAU,gBACa;AAiGzB,IAAI,sBAAQ,cAAqB;EAAE,IAAI;AAAG;AAE1C;;;;;;CAMC,GACD,OAAO,IAAI,KAAK,CACd,EAAE,IAAI,EAAE,EAAE,EAAE,QAAQ,EAInB;EAED,IAAI,KAAK,WAAW;EACpB,qBAAO,EAAE,MAAM,QAAQ,EAAE;IACvB,OAAO;MACL,MAAM,QAAQ,GAAG,IAAI;MACrB,IAAI,MAAM,OAAO,GAAG,EAAE,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,GAAG;IACtD;IACA;EACF;AACF,EAAC;AAED,0BAA0B,GAC1B,OAAO,IAAI,UAAU;EACnB,IAAI,EAAE,IAAI,EAAE,GAAG,WAAW;EAC1B,IAAI,CAAC,MAAM,MAAM,IAAI,MAAM;EAC3B,OAAO;AACT,EAAC;AAED;oBACoB,GACpB,OAAO,IAAI,WAAW,IAAc,WAAW,OAAO,EAAE,CAAA;AAExD,+EAA+E,GAC/E,OAAO,IAAI,YAAY,CAAC;EACtB,IAAI,OAAO,WAAW,OAAO,IAAI;EACjC,IAAI,QAAQ,UAAU,MAAM;EAC5B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM;EAC5B,OAAO;AACT,EAAC;AAED;gDACgD,GAChD,OAAO,IAAI,cAAc,CACvB,QACA,MACA;EAEA,IAAI,YAAY,YACd,CAAC,OACC,OAAO,MAAM,GAAG;MACd;MACA;IACF,MAAM,CAAC,KAAO,CAAC,GACjB;IAAC;OAAW;GAAK;EAEnB,OAAO,SAAS,WAAW;AAC7B,EAAC;AAED;wBACwB,GACxB,OAAO,IAAI,WAAW,CAAC,OAAe;EACpC,IAAI,IAAI,UAAU;EAClB,OAAO,YAAY,GAAG,IAAM,EAAE,IAAI,CAAC,QAAQ;IAAC;GAAM;AACpD,EAAC;AAED;0EAC0E,GAC1E,OAAO,IAAI,aAAa;EACtB,IAAI,QAAQ,IAAI;EAChB,IAAI,YAAY,IAAI;EACpB,IAAI,MAAM,CAAC,OAAe;IACxB,IAAI,CAAC,MAAM,GAAG,CAAC,UAAU,EAAE,KAAK,MAAM;IACtC,MAAM,GAAG,CAAC,OAAO;IACjB,KAAK,IAAI,MAAM;SAAI;KAAU,CAAE;EACjC;EACA,OAAO;IACL,MAAM,CAAC,QAAU,MAAM,GAAG,CAAC,UAAU;IACrC,MAAM;IACN,OAAO,CAAC,QAAU,IAAI,OAAO;IAC7B,QAAQ,CAAC;MACP,UAAU,GAAG,CAAC;MACd;MACA,OAAO,IAAM,KAAK,UAAU,MAAM,CAAC;IACrC;EACF;AACF,EAAC"}
package/host.ts ADDED
@@ -0,0 +1,212 @@
1
+ /**
2
+ * What a UX component asks of the page it is on, handed down once from the
3
+ * root of the tree it draws (`Ux`), so one page may hold several: web's page
4
+ * and the inspector's views on one of its cards each hand down their own. The
5
+ * host says what the values mean (the vocabulary, what an entity is called),
6
+ * where each component keeps its own state (the page's graph), where what the
7
+ * person types waits until it is sent (their drafts), where a bundle it emits
8
+ * goes when its caller names nowhere else, and the platform's primitives
9
+ * (where a popout floats). The rest is optional: a host without it
10
+ * gets the plain behavior (no suggestions, a query typed as text, no inline
11
+ * markdown, a popout in the flow).
12
+ *
13
+ * @module
14
+ */
15
+
16
+ import { useStore } from '@yaks/ui/store'
17
+ /* @ts-types="npm:@types/react@19.3.0" */ import {
18
+ createContext,
19
+ createElement as h,
20
+ type FunctionComponent,
21
+ type JSX,
22
+ type ReactNode,
23
+ useCallback,
24
+ useContext,
25
+ } from 'react'
26
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
27
+ import type { FieldProps } from './completion.ts'
28
+ import type { EditOptions } from 'jsr:@yaks/render@0.2.36'
29
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
30
+ import type { Find } from './hits.ts'
31
+
32
+ export type { Bundle }
33
+
34
+ /** A page's own graph, as much as a UX component needs of it: a @yaks/client
35
+ * `client()` over this package's vocabulary (./vocab.ts) is one. */
36
+ export type Front = {
37
+ mutate: (bundles: Bundle[]) => unknown
38
+ watch: (query: string) => {
39
+ value: Bundle[]
40
+ /** the first answer has arrived; a watch without it always has */
41
+ readonly ready?: boolean
42
+ /** why the query was refused */
43
+ readonly refused?: string
44
+ subscribe: (fn: (rows: Bundle[]) => void) => () => void
45
+ close?: () => void
46
+ }
47
+ ent: (eid: string) => Bundle | undefined
48
+ }
49
+
50
+ /** A person's drafts, by the place they type in: what is typed there and not
51
+ * yet sent ('' for nothing), kept as it is typed, and emptied once it is sent
52
+ * or discarded. Where they are kept is the host's choice; @yaks/draft's `desk`
53
+ * keeps them in the graph, synced to every interface the person uses. */
54
+ export type Drafts = {
55
+ text: (place: string) => string
56
+ type: (place: string, text: string) => void
57
+ spend: (place: string) => void
58
+ /** Run `fn` now and again whenever a text it read changes, until the
59
+ * returned function is called: how a component draws again as a draft
60
+ * changes (`useDraft`). */
61
+ follow?: (fn: () => void) => () => void
62
+ }
63
+
64
+ /** Where a popout floats: beside `anchor`, on `side` first. @yaks/ui `Float`
65
+ * is a browser's. */
66
+ export type Float = FunctionComponent<{
67
+ anchor: { current: HTMLElement | null }
68
+ side?: 'above' | 'below'
69
+ children?: ReactNode
70
+ }>
71
+
72
+ /** What a page supplies. */
73
+ export type Host = {
74
+ /** the vocabulary of the values it changes: what a property is, and
75
+ * whether a client may write it */
76
+ vocab: Vocab
77
+ /** the page's own graph, where each component keeps its own state */
78
+ front: Front
79
+ /** where what is typed over a value waits until it is sent or put back */
80
+ drafts: Drafts
81
+ /** where a bundle a component emits goes when its caller names nowhere
82
+ * else: a value to the graph it came from, an event (./vocab.json) to
83
+ * whoever says it */
84
+ write: (b: Bundle) => unknown
85
+ /** what to call the entity an eid names */
86
+ name: (eid: string) => string
87
+ /** the id a person reads: `T-9` */
88
+ id: (b: Bundle) => string
89
+ /** the kind it displays as: `task` */
90
+ kind: (b: Bundle) => string
91
+ /** a moment, in words */
92
+ when: (at: string) => string
93
+ /** the entities a query line answers, asked of the server: a picker's
94
+ * candidates, over the whole graph rather than what this page holds */
95
+ find: Find<Bundle>
96
+ /** how long, in ms, a picker's typed line rests unchanged before `find` is
97
+ * asked; 150 without it */
98
+ settle?: number
99
+ /** how typed input is read (@yaks/render `edit`) */
100
+ editing?: EditOptions
101
+ /** the values seen so far in a well (a property declared `well`): a hook,
102
+ * so the host may hold what it reads while the picker is mounted */
103
+ useValues?: (well: string) => string[]
104
+ /** the query fields (@yaks/filter) a saved query is typed in; without
105
+ * them a query is typed as text */
106
+ fields?: { Filter: FunctionComponent<FieldProps> }
107
+ /** inline markdown, drawn: a value shown `inline` at rest */
108
+ markup?: (text: string) => ReactNode
109
+ /** prose as its reader reads it (@yaks/markdown `drawn`): what a property
110
+ * declaring `prose` (@yaks/vocab) is typed over, so a person edits `T-12`
111
+ * where the store holds its eid, and the store swaps it back on the write;
112
+ * without it, the text as stored */
113
+ prose?: (text: string) => string
114
+ /** what a choice wears beside its word: a status's dot */
115
+ wears?: (comp: string, prop: string, value: string) => ReactNode
116
+ /** where a popout floats; without it, in the flow */
117
+ Float?: Float
118
+ }
119
+
120
+ type Given = { host?: Host; at: string }
121
+
122
+ let Given = createContext<Given>({ at: '' })
123
+
124
+ /**
125
+ * `<Ux host={host}>…</Ux>`: every UX component under it reads through
126
+ * `host`. `at` names the consumer that owns what is under it (a card's own
127
+ * eid, a view drawn on it), so the same value drawn in two places keeps two
128
+ * states, and a remount finds its own again. An inner `Ux` names a consumer
129
+ * within the one above it, and keeps its host unless given one.
130
+ */
131
+ export let Ux = (
132
+ { host, at, children }: {
133
+ host?: Host
134
+ at?: string
135
+ children?: ReactNode
136
+ },
137
+ ): JSX.Element => {
138
+ let up = useContext(Given)
139
+ return h(Given.Provider, {
140
+ value: {
141
+ host: host ?? up.host,
142
+ at: at == null ? up.at : up.at ? `${up.at}/${at}` : at,
143
+ },
144
+ children,
145
+ })
146
+ }
147
+
148
+ /** The host handed down. */
149
+ export let useHost = (): Host => {
150
+ let { host } = useContext(Given)
151
+ if (!host) throw new Error('@yaks/ux: no host above (wrap the tree in Ux)')
152
+ return host
153
+ }
154
+
155
+ /** The consumer that owns what is drawn here: every `Ux`'s `at` above it,
156
+ * outermost first. */
157
+ export let useOwner = (): string => useContext(Given).at
158
+
159
+ /** Drafts supplied directly by a control's caller or inherited from its host. */
160
+ export let useDrafts = (drafts?: Drafts): Drafts => {
161
+ let host = useContext(Given).host
162
+ let found = drafts ?? host?.drafts
163
+ if (!found) throw new Error('@yaks/ux: supply drafts or wrap the tree in Ux')
164
+ return found
165
+ }
166
+
167
+ /** What `read` answers of `drafts`, read again whenever a text it read
168
+ * changes. `deps` are what `read` closes over. */
169
+ export let useFollowed = <T>(
170
+ drafts: Pick<Drafts, 'follow'>,
171
+ read: () => T,
172
+ deps: unknown[],
173
+ ): T => {
174
+ let subscribe = useCallback(
175
+ (wake: () => void) =>
176
+ drafts.follow?.(() => {
177
+ read()
178
+ wake()
179
+ }) ?? (() => {}),
180
+ [drafts, ...deps],
181
+ )
182
+ return useStore(subscribe, read)
183
+ }
184
+
185
+ /** What is typed at `place` in `drafts` (the host's, unless given), drawn
186
+ * again as it changes. */
187
+ export let useDraft = (place: string, drafts?: Drafts): string => {
188
+ let d = useDrafts(drafts)
189
+ return useFollowed(d, () => d.text(place), [place])
190
+ }
191
+
192
+ /** Drafts held in this page's memory and lost with it: a specimen's, a
193
+ * test's. A page a person types in keeps theirs in a desk (@yaks/draft). */
194
+ export let heldDrafts = (): Drafts & Required<Pick<Drafts, 'follow'>> => {
195
+ let texts = new Map<string, string>()
196
+ let followers = new Set<() => void>()
197
+ let set = (place: string, text: string) => {
198
+ if ((texts.get(place) ?? '') == text) return
199
+ texts.set(place, text)
200
+ for (let fn of [...followers]) fn()
201
+ }
202
+ return {
203
+ text: (place) => texts.get(place) ?? '',
204
+ type: set,
205
+ spend: (place) => set(place, ''),
206
+ follow: (fn) => {
207
+ followers.add(fn)
208
+ fn()
209
+ return () => void followers.delete(fn)
210
+ },
211
+ }
212
+ }
@@ -0,0 +1,32 @@
1
+ import { PAGE } from "@yaks/ui/navigate";
2
+ let MARK = 'data-app-mark-image';
3
+ let READY = 'data-app-mark-ready';
4
+ let ready = (image)=>image.toggleAttribute(READY, image.complete && image.naturalWidth > 0 && image.currentSrc == image.src);
5
+ /** Hide an image's backdrop once its current source can be shown. A swap or
6
+ * a redraw (@yaks/ui/navigate `morph`) can keep the node and its pixels while
7
+ * taking the mark away, as the server never drew it: it is put back while the
8
+ * image can still be shown. */ export let imageFallbacks = (doc)=>{
9
+ let scan = ()=>doc.querySelectorAll(`img[${MARK}]`).forEach(ready);
10
+ let loaded = (event)=>{
11
+ let image = event.target;
12
+ if (image?.tagName == 'IMG' && image.hasAttribute(MARK)) ready(image);
13
+ };
14
+ doc.addEventListener('load', loaded, true);
15
+ doc.addEventListener('error', loaded, true);
16
+ doc.addEventListener(PAGE, scan);
17
+ let Observer = doc.defaultView?.MutationObserver ?? globalThis.MutationObserver;
18
+ new Observer((changes)=>{
19
+ for (let { target } of changes){
20
+ let image = target;
21
+ if (image.hasAttribute(MARK) && !image.hasAttribute(READY)) ready(image);
22
+ }
23
+ }).observe(doc.documentElement, {
24
+ subtree: true,
25
+ attributeFilter: [
26
+ READY
27
+ ]
28
+ });
29
+ scan();
30
+ };
31
+ if (typeof document != 'undefined') imageFallbacks(document);
32
+ //# sourceMappingURL=image-fallback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-fallback.js","sources":["./image-fallback.ts"],"names":[],"mappings":"AAAA,SAAS,IAAI,iCAAsC;AAEnD,IAAI,OAAO;AACX,IAAI,QAAQ;AAEZ,IAAI,QAAQ,CAAC,QACX,MAAM,eAAe,CACnB,OACA,MAAM,QAAQ,IAAI,MAAM,YAAY,GAAG,KAAK,MAAM,UAAU,IAAI,MAAM,GAAG;AAG7E;;;6BAG6B,GAC7B,OAAO,IAAI,iBAAiB,CAAC;EAC3B,IAAI,OAAO,IACT,IAAI,gBAAgB,CAAmB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC;EACjE,IAAI,SAAS,CAAC;IACZ,IAAI,QAAQ,MAAM,MAAM;IACxB,IAAI,OAAO,WAAW,SAAS,MAAM,YAAY,CAAC,OAAO,MAAM;EACjE;EACA,IAAI,gBAAgB,CAAC,QAAQ,QAAQ;EACrC,IAAI,gBAAgB,CAAC,SAAS,QAAQ;EACtC,IAAI,gBAAgB,CAAC,MAAM;EAC3B,IAAI,WAAW,IAAI,WAAW,EAAE,oBAC9B,WAAW,gBAAgB;EAC7B,IAAI,SAAS,CAAC;IACZ,KAAK,IAAI,EAAE,MAAM,EAAE,IAAI,QAAS;MAC9B,IAAI,QAAQ;MACZ,IAAI,MAAM,YAAY,CAAC,SAAS,CAAC,MAAM,YAAY,CAAC,QAAQ,MAAM;IACpE;EACF,GAAG,OAAO,CAAC,IAAI,eAAe,EAAE;IAAE,SAAS;IAAM,iBAAiB;MAAC;KAAM;EAAC;EAC1E;AACF,EAAC;AAED,IAAI,OAAO,YAAY,aAAa,eAAe"}
@@ -0,0 +1,37 @@
1
+ import { PAGE } from '@yaks/ui/navigate'
2
+
3
+ let MARK = 'data-app-mark-image'
4
+ let READY = 'data-app-mark-ready'
5
+
6
+ let ready = (image: HTMLImageElement) =>
7
+ image.toggleAttribute(
8
+ READY,
9
+ image.complete && image.naturalWidth > 0 && image.currentSrc == image.src,
10
+ )
11
+
12
+ /** Hide an image's backdrop once its current source can be shown. A swap or
13
+ * a redraw (@yaks/ui/navigate `morph`) can keep the node and its pixels while
14
+ * taking the mark away, as the server never drew it: it is put back while the
15
+ * image can still be shown. */
16
+ export let imageFallbacks = (doc: Document): void => {
17
+ let scan = () =>
18
+ doc.querySelectorAll<HTMLImageElement>(`img[${MARK}]`).forEach(ready)
19
+ let loaded = (event: Event) => {
20
+ let image = event.target as HTMLImageElement | null
21
+ if (image?.tagName == 'IMG' && image.hasAttribute(MARK)) ready(image)
22
+ }
23
+ doc.addEventListener('load', loaded, true)
24
+ doc.addEventListener('error', loaded, true)
25
+ doc.addEventListener(PAGE, scan)
26
+ let Observer = doc.defaultView?.MutationObserver ??
27
+ globalThis.MutationObserver
28
+ new Observer((changes) => {
29
+ for (let { target } of changes) {
30
+ let image = target as HTMLImageElement
31
+ if (image.hasAttribute(MARK) && !image.hasAttribute(READY)) ready(image)
32
+ }
33
+ }).observe(doc.documentElement, { subtree: true, attributeFilter: [READY] })
34
+ scan()
35
+ }
36
+
37
+ if (typeof document != 'undefined') imageFallbacks(document)