trebol 0.1.2 → 0.2.0

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 (256) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +10040 -7519
  3. package/dist/lib/core/boot.js +10 -7
  4. package/dist/lib/core/eval.js +2 -2
  5. package/dist/lib/core/machine/checks.d.ts +1 -0
  6. package/dist/lib/core/machine/checks.js +250 -0
  7. package/dist/lib/core/machine/events.d.ts +1 -0
  8. package/dist/lib/core/machine/events.js +365 -0
  9. package/dist/lib/core/machine/running.d.ts +1 -0
  10. package/dist/lib/core/machine/running.js +180 -0
  11. package/dist/lib/core/machine/shared.d.ts +1 -0
  12. package/dist/lib/core/machine/shared.js +123 -0
  13. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  14. package/dist/lib/core/machine/snapshot.js +244 -0
  15. package/dist/lib/core/machine/spec.d.ts +1 -0
  16. package/dist/lib/core/machine/spec.js +274 -0
  17. package/dist/lib/core/machine/words.d.ts +1 -0
  18. package/dist/lib/core/machine/words.js +98 -0
  19. package/dist/lib/core/machine.d.ts +1 -0
  20. package/dist/lib/core/machine.js +17 -0
  21. package/dist/lib/core/mezz.js +31 -0
  22. package/dist/lib/core/natives/control.js +1 -1
  23. package/dist/lib/core/natives/data.js +6 -1
  24. package/dist/lib/core/natives/series.js +49 -9
  25. package/dist/lib/core/natives/text.js +0 -1
  26. package/dist/lib/host/browser/calendar.d.ts +34 -0
  27. package/dist/lib/host/browser/calendar.js +268 -0
  28. package/dist/lib/host/browser/count.d.ts +15 -0
  29. package/dist/lib/host/browser/count.js +48 -0
  30. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  31. package/dist/lib/host/browser/dialogs.js +443 -0
  32. package/dist/lib/host/browser/drag.d.ts +70 -0
  33. package/dist/lib/host/browser/drag.js +268 -0
  34. package/dist/lib/host/browser/drawings.d.ts +30 -0
  35. package/dist/lib/host/browser/drawings.js +176 -0
  36. package/dist/lib/host/browser/events.d.ts +16 -0
  37. package/dist/lib/host/browser/events.js +125 -0
  38. package/dist/lib/host/browser/faces.d.ts +35 -0
  39. package/dist/lib/host/browser/faces.js +131 -0
  40. package/dist/lib/host/browser/fields.d.ts +16 -0
  41. package/dist/lib/host/browser/fields.js +322 -0
  42. package/dist/lib/host/browser/keys.d.ts +29 -0
  43. package/dist/lib/host/browser/keys.js +151 -0
  44. package/dist/lib/host/browser/lookup.d.ts +25 -0
  45. package/dist/lib/host/browser/lookup.js +275 -0
  46. package/dist/lib/host/browser/menus.d.ts +20 -0
  47. package/dist/lib/host/browser/menus.js +149 -0
  48. package/dist/lib/host/browser/mount.d.ts +92 -0
  49. package/dist/lib/host/browser/mount.js +448 -0
  50. package/dist/lib/host/browser/orbits.d.ts +90 -0
  51. package/dist/lib/host/browser/orbits.js +379 -0
  52. package/dist/lib/host/browser/page.d.ts +13 -0
  53. package/dist/lib/host/browser/page.js +35 -0
  54. package/dist/lib/host/browser/richtext.d.ts +19 -0
  55. package/dist/lib/host/browser/richtext.js +379 -0
  56. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  57. package/dist/lib/host/browser/row-faces.js +74 -0
  58. package/dist/lib/host/browser/screens.d.ts +37 -0
  59. package/dist/lib/host/browser/screens.js +140 -0
  60. package/dist/lib/host/browser/swipe.d.ts +31 -0
  61. package/dist/lib/host/browser/swipe.js +178 -0
  62. package/dist/lib/host/browser/sync.d.ts +24 -0
  63. package/dist/lib/host/browser/sync.js +450 -0
  64. package/dist/lib/host/browser/tables.d.ts +65 -0
  65. package/dist/lib/host/browser/tables.js +505 -0
  66. package/dist/lib/host/browser/times.d.ts +14 -0
  67. package/dist/lib/host/browser/times.js +114 -0
  68. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  69. package/dist/lib/host/browser/toolbars.js +129 -0
  70. package/dist/lib/host/browser/trees.d.ts +20 -0
  71. package/dist/lib/host/browser/trees.js +235 -0
  72. package/dist/lib/host/browser/uploads.d.ts +29 -0
  73. package/dist/lib/host/browser/uploads.js +370 -0
  74. package/dist/lib/host/browser/widget.d.ts +13 -0
  75. package/dist/lib/host/browser/widget.js +105 -0
  76. package/dist/lib/host/browser.d.ts +25 -0
  77. package/dist/lib/host/browser.js +84 -4812
  78. package/dist/lib/host/devtools/console.d.ts +9 -0
  79. package/dist/lib/host/devtools/console.js +104 -0
  80. package/dist/lib/host/devtools/faces.d.ts +25 -0
  81. package/dist/lib/host/devtools/faces.js +169 -0
  82. package/dist/lib/host/devtools/log.d.ts +3 -0
  83. package/dist/lib/host/devtools/log.js +32 -0
  84. package/dist/lib/host/devtools/machines.d.ts +34 -0
  85. package/dist/lib/host/devtools/machines.js +212 -0
  86. package/dist/lib/host/devtools/read.d.ts +13 -0
  87. package/dist/lib/host/devtools/read.js +52 -0
  88. package/dist/lib/host/devtools/state.d.ts +90 -0
  89. package/dist/lib/host/devtools/state.js +123 -0
  90. package/dist/lib/host/devtools.d.ts +15 -19
  91. package/dist/lib/host/devtools.js +39 -427
  92. package/dist/lib/host/fonts.js +4 -3
  93. package/dist/lib/host/icons.js +4 -0
  94. package/dist/lib/host/index.d.ts +3 -1
  95. package/dist/lib/host/index.js +107 -4
  96. package/dist/lib/host/locale.d.ts +18 -0
  97. package/dist/lib/host/locale.js +75 -5
  98. package/dist/lib/host/markdown.d.ts +4 -0
  99. package/dist/lib/host/markdown.js +8 -0
  100. package/dist/lib/host/modules.js +118 -19
  101. package/dist/lib/host/motion.d.ts +32 -0
  102. package/dist/lib/host/motion.js +190 -0
  103. package/dist/lib/host/net.js +5 -2
  104. package/dist/lib/host/palette.d.ts +17 -0
  105. package/dist/lib/host/palette.js +131 -0
  106. package/dist/lib/host/scheduler.d.ts +2 -0
  107. package/dist/lib/host/scheduler.js +4 -0
  108. package/dist/lib/host/share.d.ts +31 -0
  109. package/dist/lib/host/share.js +134 -0
  110. package/dist/lib/host/theme/classic.d.ts +1 -0
  111. package/dist/lib/host/theme/classic.js +120 -0
  112. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  113. package/dist/lib/host/theme/cupertino.js +65 -0
  114. package/dist/lib/host/theme/data.d.ts +1 -0
  115. package/dist/lib/host/theme/data.js +109 -0
  116. package/dist/lib/host/theme/devtools.d.ts +1 -0
  117. package/dist/lib/host/theme/devtools.js +166 -0
  118. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  119. package/dist/lib/host/theme/dialogs.js +63 -0
  120. package/dist/lib/host/theme/fluent.d.ts +1 -0
  121. package/dist/lib/host/theme/fluent.js +64 -0
  122. package/dist/lib/host/theme/forms.d.ts +1 -0
  123. package/dist/lib/host/theme/forms.js +239 -0
  124. package/dist/lib/host/theme/fusion.d.ts +1 -0
  125. package/dist/lib/host/theme/fusion.js +66 -0
  126. package/dist/lib/host/theme/layout.d.ts +1 -0
  127. package/dist/lib/host/theme/layout.js +166 -0
  128. package/dist/lib/host/theme/media.d.ts +1 -0
  129. package/dist/lib/host/theme/media.js +106 -0
  130. package/dist/lib/host/theme/navigation.d.ts +1 -0
  131. package/dist/lib/host/theme/navigation.js +36 -0
  132. package/dist/lib/host/theme/panes.d.ts +1 -0
  133. package/dist/lib/host/theme/panes.js +99 -0
  134. package/dist/lib/host/theme/popups.d.ts +1 -0
  135. package/dist/lib/host/theme/popups.js +149 -0
  136. package/dist/lib/host/theme/responsive.d.ts +1 -0
  137. package/dist/lib/host/theme/responsive.js +103 -0
  138. package/dist/lib/host/theme/toasts.d.ts +1 -0
  139. package/dist/lib/host/theme/toasts.js +62 -0
  140. package/dist/lib/host/theme.js +19 -1384
  141. package/dist/lib/ui/dialect/events.trb +276 -0
  142. package/dist/lib/ui/dialect/faces.trb +135 -0
  143. package/dist/lib/ui/dialect/layout.trb +283 -0
  144. package/dist/lib/ui/dialect/palette.trb +87 -0
  145. package/dist/lib/ui/dialect/problems.trb +174 -0
  146. package/dist/lib/ui/dialect/reactions.trb +198 -0
  147. package/dist/lib/ui/dialect/rows.trb +208 -0
  148. package/dist/lib/ui/dialect/screens.trb +293 -0
  149. package/dist/lib/ui/dialect/steps.trb +203 -0
  150. package/dist/lib/ui/dialect/validation.trb +137 -0
  151. package/dist/lib/ui/dialect/view.trb +110 -0
  152. package/dist/lib/ui/dialect/walking.trb +98 -0
  153. package/dist/lib/ui/dialect/words.trb +70 -0
  154. package/dist/lib/ui/i18n.trb +17 -0
  155. package/dist/lib/ui/test.trb +3 -556
  156. package/dist/lib/ui/testing/actions.trb +421 -0
  157. package/dist/lib/ui/testing/faces.trb +148 -0
  158. package/dist/lib/ui/testing/server.trb +73 -0
  159. package/dist/lib/ui/ui.trb +25 -1675
  160. package/dist/trebol.js +4463 -1891
  161. package/package.json +1 -1
  162. package/src/core/boot.ts +9 -6
  163. package/src/core/eval.ts +2 -2
  164. package/src/core/machine/checks.ts +251 -0
  165. package/src/core/machine/events.ts +366 -0
  166. package/src/core/machine/running.ts +181 -0
  167. package/src/core/machine/shared.ts +124 -0
  168. package/src/core/machine/snapshot.ts +245 -0
  169. package/src/core/machine/spec.ts +275 -0
  170. package/src/core/machine/words.ts +99 -0
  171. package/src/core/machine.ts +20 -0
  172. package/src/core/mezz.ts +31 -0
  173. package/src/core/natives/control.ts +1 -1
  174. package/src/core/natives/data.ts +6 -1
  175. package/src/core/natives/parse.ts +1 -1
  176. package/src/core/natives/series.ts +47 -11
  177. package/src/core/natives/text.ts +0 -1
  178. package/src/host/browser/calendar.ts +264 -0
  179. package/src/host/browser/count.ts +48 -0
  180. package/src/host/browser/dialogs.ts +439 -0
  181. package/src/host/browser/drag.ts +278 -0
  182. package/src/host/browser/drawings.ts +178 -0
  183. package/src/host/browser/events.ts +117 -0
  184. package/src/host/browser/faces.ts +143 -0
  185. package/src/host/browser/fields.ts +315 -0
  186. package/src/host/browser/keys.ts +145 -0
  187. package/src/host/browser/lookup.ts +262 -0
  188. package/src/host/browser/menus.ts +146 -0
  189. package/src/host/browser/mount.ts +420 -0
  190. package/src/host/browser/orbits.ts +418 -0
  191. package/src/host/browser/page.ts +43 -0
  192. package/src/host/browser/richtext.ts +346 -0
  193. package/src/host/browser/row-faces.ts +75 -0
  194. package/src/host/browser/screens.ts +142 -0
  195. package/src/host/browser/swipe.ts +168 -0
  196. package/src/host/browser/sync.ts +354 -0
  197. package/src/host/browser/tables.ts +461 -0
  198. package/src/host/browser/times.ts +115 -0
  199. package/src/host/browser/toolbars.ts +126 -0
  200. package/src/host/browser/trees.ts +205 -0
  201. package/src/host/browser/uploads.ts +367 -0
  202. package/src/host/browser/widget.ts +103 -0
  203. package/src/host/browser.ts +82 -4612
  204. package/src/host/devtools/console.ts +102 -0
  205. package/src/host/devtools/faces.ts +160 -0
  206. package/src/host/devtools/log.ts +34 -0
  207. package/src/host/devtools/machines.ts +208 -0
  208. package/src/host/devtools/read.ts +58 -0
  209. package/src/host/devtools/state.ts +148 -0
  210. package/src/host/devtools.ts +39 -442
  211. package/src/host/fonts.ts +4 -3
  212. package/src/host/icons.ts +4 -0
  213. package/src/host/index.ts +107 -4
  214. package/src/host/locale.ts +71 -5
  215. package/src/host/markdown.ts +9 -0
  216. package/src/host/modules.ts +117 -21
  217. package/src/host/motion.ts +204 -0
  218. package/src/host/net.ts +4 -2
  219. package/src/host/palette.ts +139 -0
  220. package/src/host/scheduler.ts +5 -0
  221. package/src/host/share.ts +168 -0
  222. package/src/host/theme/classic.ts +121 -0
  223. package/src/host/theme/cupertino.ts +66 -0
  224. package/src/host/theme/data.ts +110 -0
  225. package/src/host/theme/devtools.ts +167 -0
  226. package/src/host/theme/dialogs.ts +64 -0
  227. package/src/host/theme/fluent.ts +65 -0
  228. package/src/host/theme/forms.ts +240 -0
  229. package/src/host/theme/fusion.ts +67 -0
  230. package/src/host/theme/layout.ts +167 -0
  231. package/src/host/theme/media.ts +107 -0
  232. package/src/host/theme/navigation.ts +37 -0
  233. package/src/host/theme/panes.ts +100 -0
  234. package/src/host/theme/popups.ts +150 -0
  235. package/src/host/theme/responsive.ts +104 -0
  236. package/src/host/theme/toasts.ts +63 -0
  237. package/src/host/theme.ts +19 -1384
  238. package/src/ui/dialect/events.trb +276 -0
  239. package/src/ui/dialect/faces.trb +135 -0
  240. package/src/ui/dialect/layout.trb +283 -0
  241. package/src/ui/dialect/palette.trb +87 -0
  242. package/src/ui/dialect/problems.trb +174 -0
  243. package/src/ui/dialect/reactions.trb +198 -0
  244. package/src/ui/dialect/rows.trb +208 -0
  245. package/src/ui/dialect/screens.trb +293 -0
  246. package/src/ui/dialect/steps.trb +203 -0
  247. package/src/ui/dialect/validation.trb +137 -0
  248. package/src/ui/dialect/view.trb +110 -0
  249. package/src/ui/dialect/walking.trb +98 -0
  250. package/src/ui/dialect/words.trb +70 -0
  251. package/src/ui/i18n.trb +17 -0
  252. package/src/ui/test.trb +3 -556
  253. package/src/ui/testing/actions.trb +421 -0
  254. package/src/ui/testing/faces.trb +148 -0
  255. package/src/ui/testing/server.trb +73 -0
  256. package/src/ui/ui.trb +25 -1675
@@ -0,0 +1,262 @@
1
+ // A lookup: a field where the user types and chooses among what on-search finds, as in
2
+ // the WAI-ARIA combobox. Only one list is open at a time, in the top layer as a calendar.
3
+
4
+ import { K, NONE, string, form, type Cell, type MapCell, type ObjectCell } from '../../core/index.ts';
5
+ import { mapAt, translate } from '../index.ts';
6
+ import { linkHref } from '../markdown.ts';
7
+ import { get, str, optionValue, optionText, optionsOf, own, flag, word, icon } from './faces.ts';
8
+ import { type Mounted, host, newId, fire, el } from './mount.ts';
9
+ import { int } from './drag.ts';
10
+ import { required } from './fields.ts';
11
+ import { screenHref } from './screens.ts';
12
+
13
+ export interface OpenLookup {
14
+ m: Mounted;
15
+ list: HTMLUListElement;
16
+ shown: Cell | null; // the options on show; null until the first answer
17
+ items: Cell[];
18
+ active: number;
19
+ }
20
+ export let lookup: OpenLookup | null = null;
21
+ export let lookupTimer: ReturnType<typeof setTimeout> | undefined;
22
+ export const LOOKUP_WAIT_MS = 200;
23
+
24
+ export function lookupValues(f: ObjectCell): Cell[] {
25
+ const v = get(f, 'value');
26
+ return v.t === K.None ? [] : v.t === K.Block ? v.s.data.slice(v.i) : [v];
27
+ }
28
+
29
+ export function lookupText(f: ObjectCell, v: Cell): string {
30
+ const known = get(f, 'known');
31
+ const t = known.t === K.Map ? mapAt(known as MapCell, form(v)) : null;
32
+ return t !== null ? translate(t, host.rt) : form(v);
33
+ }
34
+
35
+ /** Where a readonly lookup's value links to, from its `links` map, or null to show it as text. */
36
+ export function lookupHref(f: ObjectCell, v: Cell): string | null {
37
+ const links = get(f, 'links');
38
+ if (!flag(f, 'readonly') || links.t !== K.Map) return null;
39
+ const to = mapAt(links as MapCell, form(v));
40
+ if (to === null) return null;
41
+ if (to.t === K.LitWord) return screenHref({ ...to, t: K.Word } as Cell);
42
+ if (to.t === K.LitPath) return screenHref({ ...to, t: K.Path } as Cell);
43
+ return screenHref(to) ?? (to.t === K.Url || to.t === K.File || to.t === K.String ? linkHref(form(to)) : null);
44
+ }
45
+
46
+ export function buildLookup(face: ObjectCell, m: Mounted): HTMLElement {
47
+ const id = `tb-${newId()}`;
48
+ const wrap = el('div', 'tb-field');
49
+ const label = el('label', 'tb-label');
50
+ label.htmlFor = id;
51
+ const control = el('div', 'tb-control');
52
+ const box = el('div', 'tb-lookup');
53
+ const chosen = el('ul', 'tb-lookup-chosen');
54
+ const input = el('input', 'tb-input');
55
+ input.id = id;
56
+ input.type = 'text';
57
+ input.autocomplete = 'off';
58
+ input.setAttribute('role', 'combobox');
59
+ input.setAttribute('aria-autocomplete', 'list');
60
+ input.setAttribute('aria-expanded', 'false');
61
+ const name = word(face, 'name');
62
+ if (name !== null) input.name = name;
63
+ const error = el('div', 'tb-error');
64
+ error.id = `${id}-error`;
65
+ error.setAttribute('aria-live', 'polite');
66
+ input.setAttribute('aria-describedby', error.id);
67
+ input.addEventListener('click', () => {
68
+ if (lookup?.m !== m) openLookup(m, input, '');
69
+ });
70
+ input.addEventListener('input', () => {
71
+ if (lookup?.m !== m) openLookup(m, input, null);
72
+ clearTimeout(lookupTimer);
73
+ lookupTimer = setTimeout(() => void fire('ui-lookup-search', face, string(input.value)), LOOKUP_WAIT_MS);
74
+ });
75
+ input.addEventListener('keydown', ev => lookupKey(m, input, ev));
76
+ input.addEventListener('blur', () => {
77
+ closeLookup();
78
+ // what was typed is not a value: emptied, a lookup has none; else it goes back to what it had
79
+ if (input.readOnly) return;
80
+ const many = flag(face, 'multiple');
81
+ const emptied = !many && input.value === '' && get(face, 'value').t !== K.None;
82
+ if (emptied) void fire('ui-input', face, NONE);
83
+ else input.value = many ? '' : lookupValues(face).map(v => lookupText(face, v)).join('');
84
+ void fire('ui-blur', face);
85
+ });
86
+ box.append(chosen, input);
87
+ control.append(box, error);
88
+ wrap.append(label, control);
89
+ m.input = input;
90
+ m.label = label;
91
+ m.error = error;
92
+ m.box = chosen;
93
+ return wrap;
94
+ }
95
+
96
+ export function syncLookup(m: Mounted, err: string | null): void {
97
+ const f = m.face;
98
+ const input = m.input as HTMLInputElement;
99
+ const label = m.label!;
100
+ label.textContent = str(f, 'text') ?? '';
101
+ label.classList.toggle('tb-required', required(f));
102
+ const many = flag(f, 'multiple');
103
+ const readonly = flag(f, 'readonly');
104
+ const off = flag(f, 'disabled');
105
+ const values = lookupValues(f);
106
+ // readonly values with a link are shown as chips, so that each one is a real link
107
+ const chips = many || values.some(v => lookupHref(f, v) !== null);
108
+ input.readOnly = readonly;
109
+ input.disabled = off;
110
+ input.hidden = chips && readonly && values.length > 0;
111
+ input.setAttribute('aria-required', required(f) ? 'true' : 'false');
112
+ input.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
113
+ const hint = str(f, 'hint');
114
+ if (hint !== null) input.placeholder = hint;
115
+ m.error!.textContent = err ?? '';
116
+ if (chips) {
117
+ m.box!.replaceChildren(...values.map((v, i) => {
118
+ const li = el('li', 'tb-lookup-chip');
119
+ const href = lookupHref(f, v);
120
+ const shown = el(href !== null ? 'a' : 'span', href !== null ? 'tb-link' : '');
121
+ if (shown instanceof HTMLAnchorElement) shown.href = href!;
122
+ shown.textContent = lookupText(f, v);
123
+ li.append(shown);
124
+ if (!readonly && !off) {
125
+ const remove = el('button', 'tb-upload-remove');
126
+ remove.type = 'button';
127
+ remove.setAttribute('aria-label', own('remove-value', shown.textContent));
128
+ const x = icon('close');
129
+ if (x !== null) remove.append(x);
130
+ remove.addEventListener('click', () => {
131
+ input.focus();
132
+ void fire('ui-lookup-remove', f, int(i + 1));
133
+ });
134
+ li.append(remove);
135
+ }
136
+ return li;
137
+ }));
138
+ } else {
139
+ // a single value loses its chip when it stops being readonly or linked
140
+ m.box!.replaceChildren();
141
+ const want = values.map(v => lookupText(f, v)).join('');
142
+ if (document.activeElement !== input && input.value !== want) input.value = want;
143
+ }
144
+ if (lookup?.m === m) renderLookup();
145
+ }
146
+
147
+ export function openLookup(m: Mounted, input: HTMLInputElement, query: string | null): void {
148
+ if (input.readOnly || input.disabled) return;
149
+ closeLookup();
150
+ const list = el('ul', 'tb-lookup-list');
151
+ list.id = `tb-${newId()}`;
152
+ list.setAttribute('role', 'listbox');
153
+ list.setAttribute('popover', 'manual');
154
+ // choosing with the pointer must not take the focus away from the field
155
+ list.addEventListener('pointerdown', ev => ev.preventDefault());
156
+ lookup = { m, list, shown: null, items: [], active: -1 };
157
+ input.setAttribute('aria-expanded', 'true');
158
+ input.setAttribute('aria-controls', list.id);
159
+ // a modal dialog makes all outside it inert: there, a list in the body would take no clicks
160
+ (input.closest('dialog') ?? document.body).append(list);
161
+ list.showPopover?.();
162
+ placeLookup();
163
+ if (query !== null) void fire('ui-lookup-search', m.face, string(query));
164
+ }
165
+
166
+ export function placeLookup(): void {
167
+ if (lookup === null) return;
168
+ const { list, m } = lookup;
169
+ const r = m.input!.getBoundingClientRect();
170
+ list.style.width = `${r.width}px`;
171
+ list.style.left = `${r.left}px`;
172
+ const height = list.getBoundingClientRect().height;
173
+ list.style.top = `${r.bottom + 4 + height > innerHeight - 8 ? Math.max(8, r.top - 4 - height) : r.bottom + 4}px`;
174
+ }
175
+
176
+ export function closeLookup(): void {
177
+ clearTimeout(lookupTimer);
178
+ if (lookup === null) return;
179
+ const { list, m } = lookup;
180
+ lookup = null;
181
+ list.hidePopover?.();
182
+ list.remove();
183
+ m.input!.setAttribute('aria-expanded', 'false');
184
+ m.input!.removeAttribute('aria-controls');
185
+ m.input!.removeAttribute('aria-activedescendant');
186
+ }
187
+
188
+ export function renderLookup(): void {
189
+ if (lookup === null) return;
190
+ const open = lookup;
191
+ const options = get(open.m.face, 'options');
192
+ // what is on show stays until the search answers; a query still on its way shows nothing new
193
+ if (options.t !== K.Block || options === open.shown) return;
194
+ if (open.shown === null && get(open.m.face, 'query').t === K.None) return;
195
+ open.shown = options;
196
+ const taken = new Set(flag(open.m.face, 'multiple') ? lookupValues(open.m.face).map(v => form(v)) : []);
197
+ open.items = optionsOf(open.m.face).filter(o => !taken.has(optionValue(o)));
198
+ open.active = -1;
199
+ open.list.replaceChildren(...open.items.map((o, i) => {
200
+ const li = el('li', 'tb-lookup-option');
201
+ li.id = `${open.list.id}-${i}`;
202
+ li.setAttribute('role', 'option');
203
+ li.setAttribute('aria-selected', 'false');
204
+ li.textContent = optionText(o);
205
+ li.addEventListener('click', () => pickLookup(o));
206
+ return li;
207
+ }));
208
+ if (open.items.length === 0) {
209
+ const none = el('li', 'tb-lookup-none');
210
+ none.textContent = own('no-matches');
211
+ open.list.append(none);
212
+ }
213
+ placeLookup();
214
+ }
215
+
216
+ export function pickLookup(option: Cell): void {
217
+ if (lookup === null) return;
218
+ const { m } = lookup;
219
+ const input = m.input as HTMLInputElement;
220
+ closeLookup();
221
+ input.value = flag(m.face, 'multiple') ? '' : optionText(option);
222
+ void fire('ui-lookup-pick', m.face, option);
223
+ }
224
+
225
+ export function moveLookup(by: number): void {
226
+ if (lookup === null || lookup.items.length === 0) return;
227
+ const { list, items, m } = lookup;
228
+ const at = lookup.active < 0 && by < 0 ? items.length - 1 : (lookup.active + by + items.length) % items.length;
229
+ list.children[lookup.active]?.setAttribute('aria-selected', 'false');
230
+ lookup.active = at;
231
+ const li = list.children[at]!;
232
+ li.setAttribute('aria-selected', 'true');
233
+ li.scrollIntoView({ block: 'nearest' });
234
+ m.input!.setAttribute('aria-activedescendant', li.id);
235
+ }
236
+
237
+ export function lookupKey(m: Mounted, input: HTMLInputElement, ev: KeyboardEvent): void {
238
+ const open = lookup?.m === m;
239
+ const stop = (): void => { ev.preventDefault(); ev.stopPropagation(); };
240
+ switch (ev.key) {
241
+ case 'ArrowDown': case 'ArrowUp':
242
+ stop();
243
+ if (open) moveLookup(ev.key === 'ArrowDown' ? 1 : -1);
244
+ else openLookup(m, input, '');
245
+ break;
246
+ case 'Enter':
247
+ if (!open) break;
248
+ // with its list open, Enter chooses: it never sends the form
249
+ stop();
250
+ if (lookup!.active >= 0) pickLookup(lookup!.items[lookup!.active]!);
251
+ break;
252
+ case 'Escape':
253
+ if (open) { stop(); closeLookup(); }
254
+ break;
255
+ case 'Backspace':
256
+ if (flag(m.face, 'multiple') && input.value === '' && !input.readOnly) {
257
+ const n = lookupValues(m.face).length;
258
+ if (n > 0) void fire('ui-lookup-remove', m.face, int(n));
259
+ }
260
+ break;
261
+ }
262
+ }
@@ -0,0 +1,146 @@
1
+ // --- menus ---------------------------------------------------------------------------
2
+
3
+ import { K, type Cell, type ObjectCell, type BlockCell } from '../../core/index.ts';
4
+ import { get, str, flag, iconName, icon } from './faces.ts';
5
+ import { fire, el } from './mount.ts';
6
+ import { seenKeys } from './events.ts';
7
+
8
+ /** The open menus, outermost first. */
9
+ export const menus: HTMLElement[] = [];
10
+ /** Where focus goes back when the menus close. */
11
+ export let menuOpener: HTMLElement | null = null;
12
+
13
+ export function closeMenus(from = 0, refocus = true): void {
14
+ for (const mn of menus.splice(from)) {
15
+ mn.hidePopover?.();
16
+ mn.remove();
17
+ }
18
+ if (from === 0) {
19
+ menuOpener?.setAttribute('aria-expanded', 'false');
20
+ if (!menuOpener?.hasAttribute('aria-haspopup')) menuOpener?.removeAttribute('aria-expanded');
21
+ if (refocus) menuOpener?.focus();
22
+ menuOpener = null;
23
+ document.removeEventListener('pointerdown', outsideMenu, true);
24
+ } else {
25
+ (menus[from - 1]?.querySelector('[aria-expanded="true"]') as HTMLElement | null)?.setAttribute('aria-expanded', 'false');
26
+ }
27
+ }
28
+
29
+ export function outsideMenu(ev: PointerEvent): void {
30
+ if (!menus.some(mn => mn.contains(ev.target as Node))) closeMenus(0, false);
31
+ }
32
+
33
+ export function menuItems(mn: HTMLElement): HTMLElement[] {
34
+ return Array.from(mn.querySelectorAll<HTMLElement>(':scope > [role="menuitem"]'));
35
+ }
36
+
37
+ export interface MenuEntry {
38
+ text: string;
39
+ icon?: string | null;
40
+ divider?: boolean;
41
+ danger?: boolean;
42
+ sub?: MenuEntry[];
43
+ run?: () => void;
44
+ }
45
+
46
+ /** A menu block of the dialect as menu entries; choosing one runs ui-menu. */
47
+ export function trebolEntries(face: ObjectCell, items: BlockCell, row: Cell): MenuEntry[] {
48
+ return (items.s.data.slice(items.i).filter(x => x.t === K.Object) as ObjectCell[]).map(item => {
49
+ const sub = get(item, 'items');
50
+ return {
51
+ text: str(item, 'text') ?? '',
52
+ icon: iconName(item),
53
+ divider: flag(item, 'divider'),
54
+ sub: sub.t === K.Block ? trebolEntries(face, sub as BlockCell, row) : undefined,
55
+ danger: flag(item, 'danger'),
56
+ run: () => void fire('ui-menu', face, item, row),
57
+ };
58
+ });
59
+ }
60
+
61
+ /** Opens a menu at x, y; level > 0 is a submenu. */
62
+ export function openMenu(entries: MenuEntry[], x: number, y: number, opener: HTMLElement, level = 0): void {
63
+ closeMenus(level, false);
64
+ if (level === 0) {
65
+ menuOpener = opener;
66
+ if (opener.hasAttribute('aria-haspopup')) opener.setAttribute('aria-expanded', 'true');
67
+ document.addEventListener('pointerdown', outsideMenu, true);
68
+ }
69
+ const mn = el('div', 'tb-menu');
70
+ mn.setAttribute('role', 'menu');
71
+ mn.setAttribute('popover', 'manual');
72
+ const label = opener.getAttribute('aria-label') ?? opener.textContent;
73
+ if (label) mn.setAttribute('aria-label', label.trim());
74
+ for (const item of entries) {
75
+ if (item.divider) {
76
+ const d = el('div', 'tb-menu-divider');
77
+ d.setAttribute('role', 'separator');
78
+ mn.append(d);
79
+ continue;
80
+ }
81
+ const b = el('button', item.danger ? 'tb-menu-item tb-danger' : 'tb-menu-item');
82
+ b.type = 'button';
83
+ b.tabIndex = -1;
84
+ b.setAttribute('role', 'menuitem');
85
+ const svg = item.icon ? icon(item.icon) : null;
86
+ const text = el('span');
87
+ text.textContent = item.text;
88
+ b.append(svg ?? el('span', 'tb-menu-gap'), text);
89
+ const sub = item.sub;
90
+ if (sub !== undefined) {
91
+ b.setAttribute('aria-haspopup', 'menu');
92
+ b.setAttribute('aria-expanded', 'false');
93
+ const chevron = icon('chevron-right');
94
+ if (chevron !== null) b.append(chevron);
95
+ const openSub = (): void => {
96
+ const r = b.getBoundingClientRect();
97
+ openMenu(sub, r.right - 4, r.top - 5, b, level + 1);
98
+ b.setAttribute('aria-expanded', 'true');
99
+ };
100
+ b.addEventListener('click', openSub);
101
+ b.addEventListener('keydown', ev => { if (ev.key === 'ArrowRight') { ev.preventDefault(); openSub(); } });
102
+ } else {
103
+ b.addEventListener('click', () => {
104
+ closeMenus();
105
+ item.run?.();
106
+ });
107
+ }
108
+ b.addEventListener('pointerenter', () => b.focus());
109
+ mn.append(b);
110
+ }
111
+ mn.addEventListener('keydown', ev => {
112
+ seenKeys.add(ev);
113
+ const list = menuItems(mn);
114
+ const at = list.indexOf(document.activeElement as HTMLElement);
115
+ const n = list.length;
116
+ const to = ev.key === 'ArrowDown' ? (at + 1) % n : ev.key === 'ArrowUp' ? (at - 1 + n) % n
117
+ : ev.key === 'Home' ? 0 : ev.key === 'End' ? n - 1 : -1;
118
+ if (to >= 0) {
119
+ ev.preventDefault();
120
+ list[to]?.focus();
121
+ } else if (ev.key === 'Escape' || (ev.key === 'ArrowLeft' && level > 0)) {
122
+ ev.preventDefault();
123
+ ev.stopPropagation();
124
+ if (level > 0) { closeMenus(level); opener.focus(); } else closeMenus();
125
+ } else if (ev.key === 'Tab') {
126
+ ev.preventDefault();
127
+ closeMenus();
128
+ } else if (ev.key.length === 1 && /\S/.test(ev.key)) {
129
+ // type a letter to jump to the next item that starts with it
130
+ const k = ev.key.toLowerCase();
131
+ const next = [...list.slice(at + 1), ...list.slice(0, at + 1)].find(b => b.textContent?.trim().toLowerCase().startsWith(k));
132
+ next?.focus();
133
+ }
134
+ });
135
+ // keep the icon column only when some item has an icon
136
+ if (mn.querySelector('.tb-menu-item > svg:first-child') === null) mn.querySelectorAll('.tb-menu-gap').forEach(g => g.remove());
137
+ // in the top layer, so it shows above an open dialog
138
+ document.body.append(mn);
139
+ mn.showPopover?.();
140
+ const r = mn.getBoundingClientRect();
141
+ mn.style.left = `${Math.max(8, Math.min(x, innerWidth - r.width - 8))}px`;
142
+ mn.style.top = `${Math.max(8, y + r.height > innerHeight - 8 ? y - r.height : y)}px`;
143
+ menus.push(mn);
144
+ menuItems(mn)[0]?.focus();
145
+ }
146
+