trebol 0.1.1 → 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 (300) hide show
  1. package/README.md +12 -1
  2. package/dist/cli.js +17097 -12238
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +12 -8
  12. package/dist/lib/core/eval.js +2 -2
  13. package/dist/lib/core/func.d.ts +4 -0
  14. package/dist/lib/core/machine/checks.d.ts +1 -0
  15. package/dist/lib/core/machine/checks.js +250 -0
  16. package/dist/lib/core/machine/events.d.ts +1 -0
  17. package/dist/lib/core/machine/events.js +365 -0
  18. package/dist/lib/core/machine/running.d.ts +1 -0
  19. package/dist/lib/core/machine/running.js +180 -0
  20. package/dist/lib/core/machine/shared.d.ts +1 -0
  21. package/dist/lib/core/machine/shared.js +123 -0
  22. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  23. package/dist/lib/core/machine/snapshot.js +244 -0
  24. package/dist/lib/core/machine/spec.d.ts +1 -0
  25. package/dist/lib/core/machine/spec.js +274 -0
  26. package/dist/lib/core/machine/words.d.ts +1 -0
  27. package/dist/lib/core/machine/words.js +98 -0
  28. package/dist/lib/core/machine.d.ts +1 -0
  29. package/dist/lib/core/machine.js +17 -0
  30. package/dist/lib/core/mezz.js +31 -0
  31. package/dist/lib/core/natives/control.js +2 -2
  32. package/dist/lib/core/natives/data.js +6 -1
  33. package/dist/lib/core/natives/series.js +59 -9
  34. package/dist/lib/core/natives/text.js +3 -1
  35. package/dist/lib/host/browser/calendar.d.ts +34 -0
  36. package/dist/lib/host/browser/calendar.js +268 -0
  37. package/dist/lib/host/browser/count.d.ts +15 -0
  38. package/dist/lib/host/browser/count.js +48 -0
  39. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  40. package/dist/lib/host/browser/dialogs.js +443 -0
  41. package/dist/lib/host/browser/drag.d.ts +70 -0
  42. package/dist/lib/host/browser/drag.js +268 -0
  43. package/dist/lib/host/browser/drawings.d.ts +30 -0
  44. package/dist/lib/host/browser/drawings.js +176 -0
  45. package/dist/lib/host/browser/events.d.ts +16 -0
  46. package/dist/lib/host/browser/events.js +125 -0
  47. package/dist/lib/host/browser/faces.d.ts +35 -0
  48. package/dist/lib/host/browser/faces.js +131 -0
  49. package/dist/lib/host/browser/fields.d.ts +16 -0
  50. package/dist/lib/host/browser/fields.js +322 -0
  51. package/dist/lib/host/browser/keys.d.ts +29 -0
  52. package/dist/lib/host/browser/keys.js +151 -0
  53. package/dist/lib/host/browser/lookup.d.ts +25 -0
  54. package/dist/lib/host/browser/lookup.js +275 -0
  55. package/dist/lib/host/browser/menus.d.ts +20 -0
  56. package/dist/lib/host/browser/menus.js +149 -0
  57. package/dist/lib/host/browser/mount.d.ts +92 -0
  58. package/dist/lib/host/browser/mount.js +448 -0
  59. package/dist/lib/host/browser/orbits.d.ts +90 -0
  60. package/dist/lib/host/browser/orbits.js +379 -0
  61. package/dist/lib/host/browser/page.d.ts +13 -0
  62. package/dist/lib/host/browser/page.js +35 -0
  63. package/dist/lib/host/browser/richtext.d.ts +19 -0
  64. package/dist/lib/host/browser/richtext.js +379 -0
  65. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  66. package/dist/lib/host/browser/row-faces.js +74 -0
  67. package/dist/lib/host/browser/screens.d.ts +37 -0
  68. package/dist/lib/host/browser/screens.js +140 -0
  69. package/dist/lib/host/browser/swipe.d.ts +31 -0
  70. package/dist/lib/host/browser/swipe.js +178 -0
  71. package/dist/lib/host/browser/sync.d.ts +24 -0
  72. package/dist/lib/host/browser/sync.js +450 -0
  73. package/dist/lib/host/browser/tables.d.ts +65 -0
  74. package/dist/lib/host/browser/tables.js +505 -0
  75. package/dist/lib/host/browser/times.d.ts +14 -0
  76. package/dist/lib/host/browser/times.js +114 -0
  77. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  78. package/dist/lib/host/browser/toolbars.js +129 -0
  79. package/dist/lib/host/browser/trees.d.ts +20 -0
  80. package/dist/lib/host/browser/trees.js +235 -0
  81. package/dist/lib/host/browser/uploads.d.ts +29 -0
  82. package/dist/lib/host/browser/uploads.js +370 -0
  83. package/dist/lib/host/browser/widget.d.ts +13 -0
  84. package/dist/lib/host/browser/widget.js +105 -0
  85. package/dist/lib/host/browser.d.ts +25 -0
  86. package/dist/lib/host/browser.js +129 -3964
  87. package/dist/lib/host/devlog.d.ts +11 -0
  88. package/dist/lib/host/devlog.js +13 -0
  89. package/dist/lib/host/devtools/console.d.ts +9 -0
  90. package/dist/lib/host/devtools/console.js +104 -0
  91. package/dist/lib/host/devtools/faces.d.ts +25 -0
  92. package/dist/lib/host/devtools/faces.js +169 -0
  93. package/dist/lib/host/devtools/log.d.ts +3 -0
  94. package/dist/lib/host/devtools/log.js +32 -0
  95. package/dist/lib/host/devtools/machines.d.ts +34 -0
  96. package/dist/lib/host/devtools/machines.js +212 -0
  97. package/dist/lib/host/devtools/read.d.ts +13 -0
  98. package/dist/lib/host/devtools/read.js +52 -0
  99. package/dist/lib/host/devtools/state.d.ts +90 -0
  100. package/dist/lib/host/devtools/state.js +123 -0
  101. package/dist/lib/host/devtools.d.ts +19 -0
  102. package/dist/lib/host/devtools.js +238 -0
  103. package/dist/lib/host/fonts/OFL.txt +93 -0
  104. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  105. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  106. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  107. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  108. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  109. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  110. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  111. package/dist/lib/host/fonts.d.ts +11 -0
  112. package/dist/lib/host/fonts.js +30 -0
  113. package/dist/lib/host/icons.js +4 -0
  114. package/dist/lib/host/index.d.ts +5 -1
  115. package/dist/lib/host/index.js +110 -5
  116. package/dist/lib/host/js.d.ts +5 -0
  117. package/dist/lib/host/js.js +130 -0
  118. package/dist/lib/host/locale.d.ts +18 -0
  119. package/dist/lib/host/locale.js +75 -5
  120. package/dist/lib/host/markdown.d.ts +6 -0
  121. package/dist/lib/host/markdown.js +18 -0
  122. package/dist/lib/host/modules.d.ts +4 -0
  123. package/dist/lib/host/modules.js +157 -20
  124. package/dist/lib/host/motion.d.ts +32 -0
  125. package/dist/lib/host/motion.js +190 -0
  126. package/dist/lib/host/net.js +14 -3
  127. package/dist/lib/host/orbit.d.ts +121 -0
  128. package/dist/lib/host/orbit.js +374 -0
  129. package/dist/lib/host/palette.d.ts +17 -0
  130. package/dist/lib/host/palette.js +131 -0
  131. package/dist/lib/host/rows.d.ts +14 -0
  132. package/dist/lib/host/rows.js +29 -0
  133. package/dist/lib/host/scheduler.d.ts +8 -0
  134. package/dist/lib/host/scheduler.js +64 -36
  135. package/dist/lib/host/share.d.ts +31 -0
  136. package/dist/lib/host/share.js +134 -0
  137. package/dist/lib/host/theme/classic.d.ts +1 -0
  138. package/dist/lib/host/theme/classic.js +120 -0
  139. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  140. package/dist/lib/host/theme/cupertino.js +65 -0
  141. package/dist/lib/host/theme/data.d.ts +1 -0
  142. package/dist/lib/host/theme/data.js +109 -0
  143. package/dist/lib/host/theme/devtools.d.ts +1 -0
  144. package/dist/lib/host/theme/devtools.js +166 -0
  145. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  146. package/dist/lib/host/theme/dialogs.js +63 -0
  147. package/dist/lib/host/theme/fluent.d.ts +1 -0
  148. package/dist/lib/host/theme/fluent.js +64 -0
  149. package/dist/lib/host/theme/forms.d.ts +1 -0
  150. package/dist/lib/host/theme/forms.js +239 -0
  151. package/dist/lib/host/theme/fusion.d.ts +1 -0
  152. package/dist/lib/host/theme/fusion.js +66 -0
  153. package/dist/lib/host/theme/layout.d.ts +1 -0
  154. package/dist/lib/host/theme/layout.js +166 -0
  155. package/dist/lib/host/theme/media.d.ts +1 -0
  156. package/dist/lib/host/theme/media.js +106 -0
  157. package/dist/lib/host/theme/navigation.d.ts +1 -0
  158. package/dist/lib/host/theme/navigation.js +36 -0
  159. package/dist/lib/host/theme/panes.d.ts +1 -0
  160. package/dist/lib/host/theme/panes.js +99 -0
  161. package/dist/lib/host/theme/popups.d.ts +1 -0
  162. package/dist/lib/host/theme/popups.js +149 -0
  163. package/dist/lib/host/theme/responsive.d.ts +1 -0
  164. package/dist/lib/host/theme/responsive.js +103 -0
  165. package/dist/lib/host/theme/toasts.d.ts +1 -0
  166. package/dist/lib/host/theme/toasts.js +62 -0
  167. package/dist/lib/host/theme.js +19 -1179
  168. package/dist/lib/ui/dialect/events.trb +276 -0
  169. package/dist/lib/ui/dialect/faces.trb +135 -0
  170. package/dist/lib/ui/dialect/layout.trb +283 -0
  171. package/dist/lib/ui/dialect/palette.trb +87 -0
  172. package/dist/lib/ui/dialect/problems.trb +174 -0
  173. package/dist/lib/ui/dialect/reactions.trb +198 -0
  174. package/dist/lib/ui/dialect/rows.trb +208 -0
  175. package/dist/lib/ui/dialect/screens.trb +293 -0
  176. package/dist/lib/ui/dialect/steps.trb +203 -0
  177. package/dist/lib/ui/dialect/validation.trb +137 -0
  178. package/dist/lib/ui/dialect/view.trb +110 -0
  179. package/dist/lib/ui/dialect/walking.trb +98 -0
  180. package/dist/lib/ui/dialect/words.trb +70 -0
  181. package/dist/lib/ui/i18n.trb +23 -0
  182. package/dist/lib/ui/test.trb +12 -0
  183. package/dist/lib/ui/testing/actions.trb +421 -0
  184. package/dist/lib/ui/testing/faces.trb +148 -0
  185. package/dist/lib/ui/testing/server.trb +73 -0
  186. package/dist/lib/ui/ui.trb +25 -1234
  187. package/dist/trebol.js +4882 -1088
  188. package/editors/vscode/README.md +15 -0
  189. package/editors/vscode/language-configuration.json +28 -0
  190. package/editors/vscode/package.json +42 -0
  191. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  192. package/package.json +7 -3
  193. package/src/core/boot.ts +10 -7
  194. package/src/core/eval.ts +2 -2
  195. package/src/core/func.ts +4 -0
  196. package/src/core/machine/checks.ts +251 -0
  197. package/src/core/machine/events.ts +366 -0
  198. package/src/core/machine/running.ts +181 -0
  199. package/src/core/machine/shared.ts +124 -0
  200. package/src/core/machine/snapshot.ts +245 -0
  201. package/src/core/machine/spec.ts +275 -0
  202. package/src/core/machine/words.ts +99 -0
  203. package/src/core/machine.ts +20 -0
  204. package/src/core/mezz.ts +31 -0
  205. package/src/core/natives/control.ts +2 -2
  206. package/src/core/natives/data.ts +6 -1
  207. package/src/core/natives/parse.ts +1 -1
  208. package/src/core/natives/series.ts +55 -11
  209. package/src/core/natives/text.ts +2 -1
  210. package/src/host/browser/calendar.ts +264 -0
  211. package/src/host/browser/count.ts +48 -0
  212. package/src/host/browser/dialogs.ts +439 -0
  213. package/src/host/browser/drag.ts +278 -0
  214. package/src/host/browser/drawings.ts +178 -0
  215. package/src/host/browser/events.ts +117 -0
  216. package/src/host/browser/faces.ts +143 -0
  217. package/src/host/browser/fields.ts +315 -0
  218. package/src/host/browser/keys.ts +145 -0
  219. package/src/host/browser/lookup.ts +262 -0
  220. package/src/host/browser/menus.ts +146 -0
  221. package/src/host/browser/mount.ts +420 -0
  222. package/src/host/browser/orbits.ts +418 -0
  223. package/src/host/browser/page.ts +43 -0
  224. package/src/host/browser/richtext.ts +346 -0
  225. package/src/host/browser/row-faces.ts +75 -0
  226. package/src/host/browser/screens.ts +142 -0
  227. package/src/host/browser/swipe.ts +168 -0
  228. package/src/host/browser/sync.ts +354 -0
  229. package/src/host/browser/tables.ts +461 -0
  230. package/src/host/browser/times.ts +115 -0
  231. package/src/host/browser/toolbars.ts +126 -0
  232. package/src/host/browser/trees.ts +205 -0
  233. package/src/host/browser/uploads.ts +367 -0
  234. package/src/host/browser/widget.ts +103 -0
  235. package/src/host/browser.ts +128 -3747
  236. package/src/host/devlog.ts +16 -0
  237. package/src/host/devtools/console.ts +102 -0
  238. package/src/host/devtools/faces.ts +160 -0
  239. package/src/host/devtools/log.ts +34 -0
  240. package/src/host/devtools/machines.ts +208 -0
  241. package/src/host/devtools/read.ts +58 -0
  242. package/src/host/devtools/state.ts +148 -0
  243. package/src/host/devtools.ts +238 -0
  244. package/src/host/fonts/OFL.txt +93 -0
  245. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  246. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  247. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  248. package/src/host/fonts/inter-greek.woff2 +0 -0
  249. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  250. package/src/host/fonts/inter-latin.woff2 +0 -0
  251. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  252. package/src/host/fonts.ts +40 -0
  253. package/src/host/icons.ts +4 -0
  254. package/src/host/index.ts +110 -5
  255. package/src/host/js.ts +127 -0
  256. package/src/host/locale.ts +71 -5
  257. package/src/host/markdown.ts +18 -0
  258. package/src/host/modules.ts +150 -22
  259. package/src/host/motion.ts +204 -0
  260. package/src/host/net.ts +13 -3
  261. package/src/host/orbit.ts +432 -0
  262. package/src/host/palette.ts +139 -0
  263. package/src/host/rows.ts +30 -0
  264. package/src/host/scheduler.ts +67 -36
  265. package/src/host/share.ts +168 -0
  266. package/src/host/theme/classic.ts +121 -0
  267. package/src/host/theme/cupertino.ts +66 -0
  268. package/src/host/theme/data.ts +110 -0
  269. package/src/host/theme/devtools.ts +167 -0
  270. package/src/host/theme/dialogs.ts +64 -0
  271. package/src/host/theme/fluent.ts +65 -0
  272. package/src/host/theme/forms.ts +240 -0
  273. package/src/host/theme/fusion.ts +67 -0
  274. package/src/host/theme/layout.ts +167 -0
  275. package/src/host/theme/media.ts +107 -0
  276. package/src/host/theme/navigation.ts +37 -0
  277. package/src/host/theme/panes.ts +100 -0
  278. package/src/host/theme/popups.ts +150 -0
  279. package/src/host/theme/responsive.ts +104 -0
  280. package/src/host/theme/toasts.ts +63 -0
  281. package/src/host/theme.ts +19 -1179
  282. package/src/ui/dialect/events.trb +276 -0
  283. package/src/ui/dialect/faces.trb +135 -0
  284. package/src/ui/dialect/layout.trb +283 -0
  285. package/src/ui/dialect/palette.trb +87 -0
  286. package/src/ui/dialect/problems.trb +174 -0
  287. package/src/ui/dialect/reactions.trb +198 -0
  288. package/src/ui/dialect/rows.trb +208 -0
  289. package/src/ui/dialect/screens.trb +293 -0
  290. package/src/ui/dialect/steps.trb +203 -0
  291. package/src/ui/dialect/validation.trb +137 -0
  292. package/src/ui/dialect/view.trb +110 -0
  293. package/src/ui/dialect/walking.trb +98 -0
  294. package/src/ui/dialect/words.trb +70 -0
  295. package/src/ui/i18n.trb +23 -0
  296. package/src/ui/test.trb +12 -0
  297. package/src/ui/testing/actions.trb +421 -0
  298. package/src/ui/testing/faces.trb +148 -0
  299. package/src/ui/testing/server.trb +73 -0
  300. package/src/ui/ui.trb +25 -1234
@@ -0,0 +1,420 @@
1
+ // --- mounting ----------------------------------------------------------------------
2
+
3
+ import { K, NONE, string, ctxFind, intern, spell, type Cell, type ObjectCell, type WordCell, type Ctx } from '../../core/index.ts';
4
+ import { report, type Host } from '../index.ts';
5
+ import { spawn, taskResult, type HostTask } from '../scheduler.ts';
6
+ import { linkHref } from '../markdown.ts';
7
+ import { devLog } from '../devlog.ts';
8
+ import { faceRef } from '../devtools.ts';
9
+ import { get, str, flag, word, children, iconName, icon } from './faces.ts';
10
+ import { listen } from './events.ts';
11
+ import { buildInput, buildCheck, buildSlider, buildRadio, buildProgress } from './fields.ts';
12
+ import { buildUpload, buildPhoto } from './uploads.ts';
13
+ import { buildWidget } from './widget.ts';
14
+ import { buildLookup } from './lookup.ts';
15
+ import { buildCanvas, buildChart } from './drawings.ts';
16
+ import { buildOrbit } from './orbits.ts';
17
+ import { buildRowsField } from './row-faces.ts';
18
+ import { buildRich } from './richtext.ts';
19
+ import { buildRows } from './tables.ts';
20
+ import { buildTree } from './trees.ts';
21
+ import { buildToolbar, buildBreadcrumb } from './toolbars.ts';
22
+ import { buildDialog, buildTabs, buildSteps, buildSplit } from './dialogs.ts';
23
+ import { sync, placeScreen } from './sync.ts';
24
+ import { screenHref, setScreensFace } from './screens.ts';
25
+ import { setTitleFace } from './page.ts';
26
+
27
+ /** What the host keeps for each mounted face. */
28
+ export interface Mounted {
29
+ face: ObjectCell;
30
+ el: HTMLElement;
31
+ input: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null;
32
+ label: HTMLElement | null;
33
+ error: HTMLElement | null;
34
+ kids: unknown;
35
+ /** Where children or rows go, when it is not `el`. */
36
+ box: HTMLElement | null;
37
+ /** Rows of a table or list, by key. */
38
+ rows: Map<string, HTMLElement> | null;
39
+ /** A table or list: all its rows, in the order shown, with their keys; only some may be drawn. */
40
+ items: { row: Cell; k: string }[];
41
+ /** A table or list: the rows drawn, [first, end), and about how tall each is. */
42
+ first: number;
43
+ end: number;
44
+ rowHeight: number;
45
+ /** Shown instead of the rows when there are none. */
46
+ empty: HTMLElement | null;
47
+ /** The tab button of a tab face. */
48
+ button: HTMLButtonElement | null;
49
+ /** The options of a radio. */
50
+ radios: HTMLInputElement[] | null;
51
+ /** The keys of the open nodes of a tree. */
52
+ open: Set<string> | null;
53
+ /** Rows: what the rows were last checked against. Rich text: the Markdown on screen. */
54
+ shown: unknown;
55
+ /** An input: what the user typed still on its way to the face. */
56
+ typing: number;
57
+ /** An input: the value the user's typing gave the face; code that sets another one is shown even with the focus. */
58
+ typed: Cell | null;
59
+ }
60
+
61
+ export let host: Host;
62
+ export function setHost(v: Host): void {
63
+ host = v;
64
+ }
65
+ /** Mounted faces by their node and by their input. */
66
+ export const byNode = new WeakMap<Element, Mounted>();
67
+ /** The row cell each table row or list item shows. */
68
+ export const rowOf = new WeakMap<Element, Cell>();
69
+ /** Mounted faces by context; a face rebuilt in a new container replaces its entry. */
70
+ export const byCtx = new WeakMap<Ctx, Mounted>();
71
+ /** Contexts of mounted faces written since the last repaint (filled by the core). */
72
+ export const dirty: Ctx[] = [];
73
+ /** Mounted faces with react, is or calc; and whether one came since reactions last ran. */
74
+ export const reactors = new Set<Mounted>();
75
+ export let newReactor = false;
76
+ export function setNewReactor(v: typeof newReactor): void {
77
+ newReactor = v;
78
+ }
79
+ /** The ui-react still running because a reaction waits (a fetch); the next one waits for it. */
80
+ export let reacting: HostTask | null = null;
81
+ export function setReacting(v: HostTask | null): void {
82
+ reacting = v;
83
+ }
84
+ export let nextId = 1;
85
+ /** A new number for the id of an element. */
86
+ export function newId(): number {
87
+ return nextId++;
88
+ }
89
+
90
+ export const ACTIVE: ReadonlySet<number> = new Set([K.Word, K.SetWord, K.GetWord, K.Paren, K.Path, K.SetPath, K.GetPath, K.Function, K.Native]);
91
+
92
+ /** The handler each UI event function runs, to name it in an error; the others go by their own name. */
93
+ export const HANDLERS: Record<string, string> = {
94
+ 'ui-click': 'action', 'ui-input': 'on-change', 'ui-files': 'on-change', 'ui-remove-file': 'on-change',
95
+ 'ui-lookup-pick': 'on-change', 'ui-lookup-remove': 'on-change', 'ui-lookup-search': 'on-search',
96
+ 'ui-select': 'on-select', 'ui-sort': 'on-sort', 'ui-drop': 'on-drop', 'ui-menu': 'on-menu',
97
+ 'ui-reorder': 'on-reorder', 'ui-expand': 'on-expand', 'ui-close': 'on-close',
98
+ };
99
+
100
+ /**
101
+ * An error of a UI event function says where in the app it happened: `action of button "Save"`, not the
102
+ * name of Trebol's own function (ui-click) that ran it.
103
+ */
104
+ export function whereInApp(e: ObjectCell, fn: string, face: ObjectCell, args: Cell[]): void {
105
+ const i = ctxFind(e.ctx, intern('where'));
106
+ const w = i < 0 ? null : e.ctx.vals[i]!;
107
+ if (w === null || w.t !== K.Word || spell((w as WordCell).sym) !== fn) return;
108
+ const ev = fn === 'ui-event' && args[0] !== undefined && args[0].t === K.LitWord ? spell((args[0] as WordCell).sym) : null;
109
+ e.ctx.vals[i] = string(`${ev ?? HANDLERS[fn] ?? fn.replace(/^ui-/, '')} of ${faceRef(face, host.rt)}`);
110
+ }
111
+
112
+ /**
113
+ * Runs a UI event function (ui-input, ui-blur, ui-click) as a new task. An error is shown and gives none, so
114
+ * what the caller does next (focus the first problem, blur) still runs and nothing is left unhandled.
115
+ */
116
+ export function fire(fn: string, face: ObjectCell, ...args: Cell[]): Promise<Cell> {
117
+ // a word, path or paren given as data (a dropped `date`) is quoted, so it is passed, not evaluated
118
+ const quote: Cell = { t: K.Word, sym: intern('quote'), bind: host.rt.lib };
119
+ const call: Cell[] = [{ t: K.Word, sym: intern(fn), bind: host.rt.lib }, face, ...args.flatMap(a => ACTIVE.has(a.t) ? [quote, a] : [a])];
120
+ devLog.event(fn, face, args);
121
+ return taskResult(spawn(host.sch, { data: call }, 0)).catch((e: unknown) => {
122
+ if ((e as Cell).t === K.Error) whereInApp(e as ObjectCell, fn, face, args);
123
+ showError(report(e as ObjectCell));
124
+ return NONE;
125
+ });
126
+ }
127
+
128
+ /** The panel of errors trebol dev shows on the page; null until the first one. */
129
+ export let devErrors: HTMLElement | null = null;
130
+
131
+ /**
132
+ * An error nobody caught: in the console and, when trebol dev serves the page (it adds
133
+ * `<meta name="trebol-dev">`), also on the page, so a broken app is not a blank page.
134
+ */
135
+ export function showError(message: string): void {
136
+ console.error(message);
137
+ devLog.note('error', message);
138
+ if (document.querySelector('meta[name="trebol-dev"]') === null) return;
139
+ if (devErrors === null || !devErrors.isConnected) {
140
+ // a manual popover sits in the top layer, above open dialogs
141
+ const panel = el('section', 'tb-dev-errors');
142
+ panel.popover = 'manual';
143
+ panel.setAttribute('role', 'alert');
144
+ panel.setAttribute('aria-label', 'Trebol error');
145
+ const head = el('header');
146
+ const title = el('span');
147
+ title.textContent = 'Trebol error';
148
+ const close = el('button', 'tb-button');
149
+ close.type = 'button';
150
+ close.textContent = 'Close';
151
+ close.addEventListener('click', () => panel.remove());
152
+ panel.addEventListener('keydown', ev => { if (ev.key === 'Escape') panel.remove(); });
153
+ head.append(title, close);
154
+ panel.append(head);
155
+ document.body.append(panel);
156
+ panel.showPopover();
157
+ devErrors = panel;
158
+ }
159
+ const pre = el('pre');
160
+ pre.textContent = message;
161
+ devErrors.append(pre);
162
+ }
163
+
164
+ export function el<T extends keyof HTMLElementTagNameMap>(tag: T, cls?: string): HTMLElementTagNameMap[T] {
165
+ const e = document.createElement(tag);
166
+ if (cls) e.className = cls;
167
+ return e;
168
+ }
169
+
170
+ export const INPUTS = new Set(['field', 'password', 'number', 'money', 'date', 'date-range', 'time', 'area', 'choice']);
171
+
172
+ export function build(face: ObjectCell): HTMLElement {
173
+ const style = word(face, 'style') ?? 'face';
174
+ const m: Mounted = {
175
+ face, el: null!, input: null, label: null, error: null, kids: get(face, 'pane'),
176
+ box: null, rows: null, empty: null, button: null, radios: null, open: null, shown: undefined,
177
+ typing: 0, typed: null, items: [], first: 0, end: 0, rowHeight: 0,
178
+ };
179
+ let node: HTMLElement;
180
+ if (INPUTS.has(style)) {
181
+ node = buildInput(face, style, m);
182
+ } else if (style === 'check' || style === 'toggle') {
183
+ node = buildCheck(face, m, style === 'toggle');
184
+ } else if (style === 'radio') {
185
+ node = buildRadio(face, m);
186
+ } else if (style === 'slider') {
187
+ node = buildSlider(face, m);
188
+ } else if (style === 'upload') {
189
+ node = buildUpload(face, m);
190
+ } else if (style === 'photo') {
191
+ node = buildPhoto(face, m);
192
+ } else if (style === 'widget') {
193
+ node = buildWidget(face, m);
194
+ } else if (style === 'lookup') {
195
+ node = buildLookup(face, m);
196
+ } else if (style === 'rich') {
197
+ node = buildRich(face, m);
198
+ } else {
199
+ switch (style) {
200
+ case 'view': node = el('main', 'tb-view'); break;
201
+ case 'column': node = el('div', 'tb-column'); break;
202
+ case 'row': node = el('div', 'tb-row'); break;
203
+ case 'toolbar': node = buildToolbar(); break;
204
+ case 'breadcrumb': node = buildBreadcrumb(m); break;
205
+ case 'rows': node = buildRowsField(m); break;
206
+ case 'entry': {
207
+ node = el('div', 'tb-entry');
208
+ node.setAttribute('role', 'group');
209
+ break;
210
+ }
211
+ case 'grid': node = el('div', 'tb-grid'); break;
212
+ case 'board': node = el('div', 'tb-board'); break;
213
+ case 'desktop': node = el('div', 'tb-desktop'); break;
214
+ case 'scroll': {
215
+ // focusable, so keyboard users can scroll it too
216
+ node = el('div', 'tb-scroll');
217
+ node.tabIndex = 0;
218
+ node.setAttribute('role', 'region');
219
+ break;
220
+ }
221
+ case 'panel': node = el('section', 'tb-panel'); break;
222
+ case 'form': {
223
+ const f = el('form', 'tb-form');
224
+ f.noValidate = true;
225
+ // Enter in a field clicks the submit button, which handles it
226
+ f.addEventListener('submit', ev => ev.preventDefault());
227
+ node = f;
228
+ break;
229
+ }
230
+ case 'title': node = el('h1', 'tb-title'); break;
231
+ case 'heading': node = el('h2', 'tb-heading'); break;
232
+ case 'text': node = el('p', 'tb-text'); break;
233
+ case 'divider': node = el('hr', 'tb-divider'); break;
234
+ case 'link': {
235
+ const a = el('a', 'tb-link');
236
+ a.href = screenHref(get(face, 'value')) ?? linkHref(str(face, 'value') ?? '#') ?? '#';
237
+ withIcon(a, face, m);
238
+ a.addEventListener('click', ev => {
239
+ if (get(face, 'action').t === K.Block) {
240
+ ev.preventDefault();
241
+ void fire('ui-click', face);
242
+ }
243
+ });
244
+ node = a;
245
+ break;
246
+ }
247
+ case 'button': {
248
+ const b = el('button', 'tb-button');
249
+ const submit = flag(face, 'submit');
250
+ b.type = submit ? 'submit' : 'button';
251
+ b.addEventListener('click', ev => {
252
+ if (!submit) {
253
+ const done = fire('ui-click', face);
254
+ // Next of a form in steps that stays: take the user to the first problem of the step
255
+ const steps = b.closest('.tb-steps-nav')?.parentElement;
256
+ if (steps) void done.then(() => focusProblem(steps.querySelector<HTMLElement>(':scope > .tb-step-panel:not([hidden])')));
257
+ return;
258
+ }
259
+ ev.preventDefault();
260
+ clickSubmit(face, b.closest<HTMLElement>('form, .tb-view') ?? document.body);
261
+ });
262
+ withIcon(b, face, m);
263
+ node = b;
264
+ break;
265
+ }
266
+ case 'tile': {
267
+ const b = el('button', flag(face, 'linked') ? 'tb-tile tb-tile-linked' : 'tb-tile');
268
+ b.type = 'button';
269
+ b.addEventListener('click', () => void fire('ui-click', face));
270
+ withIcon(b, face, m);
271
+ node = b;
272
+ break;
273
+ }
274
+ case 'progress': node = buildProgress(m); break;
275
+ case 'image': node = el('img', 'tb-image'); break;
276
+ case 'video': case 'audio': {
277
+ const media = el(style, `tb-${style}`) as HTMLMediaElement;
278
+ media.controls = true;
279
+ media.preload = 'metadata';
280
+ if (media instanceof HTMLVideoElement) media.playsInline = true;
281
+ node = media;
282
+ break;
283
+ }
284
+ case 'backdrop': {
285
+ // decoration behind its container: screen readers skip it
286
+ node = el('div', 'tb-backdrop');
287
+ node.setAttribute('aria-hidden', 'true');
288
+ break;
289
+ }
290
+ case 'canvas': node = buildCanvas(m); break;
291
+ case 'chart': node = buildChart(m); break;
292
+ case 'orbit': node = buildOrbit(m); break;
293
+ case 'icon': node = el('span', 'tb-icon-face'); break;
294
+ case 'table': case 'list': node = buildRows(face, style, m); break;
295
+ case 'tree': node = buildTree(face, m); break;
296
+ case 'dialog': node = buildDialog(face, m); break;
297
+ case 'tabs': node = buildTabs(face, m); break;
298
+ case 'steps': node = buildSteps(face, m); break;
299
+ case 'split': node = buildSplit(face, m); break;
300
+ case 'screens': {
301
+ node = el('div', 'tb-screens');
302
+ setScreensFace(face);
303
+ break;
304
+ }
305
+ case 'screen': {
306
+ node = el('section', 'tb-screen');
307
+ const t = str(face, 'text');
308
+ if (t !== null) {
309
+ node.setAttribute('aria-label', t);
310
+ document.title = t;
311
+ setTitleFace(face);
312
+ }
313
+ break;
314
+ }
315
+ case 'tab': {
316
+ node = el('div', 'tb-tab-panel');
317
+ node.setAttribute('role', 'tabpanel');
318
+ break;
319
+ }
320
+ case 'step': {
321
+ node = el('div', 'tb-step-panel');
322
+ node.setAttribute('role', 'group');
323
+ node.tabIndex = -1;
324
+ break;
325
+ }
326
+ default: node = el('div');
327
+ }
328
+ if (style === 'screens') placeScreen(m, node);
329
+ else if (children(face).length > 0 && style !== 'tabs' && style !== 'steps' && style !== 'split') appendChildren(m.box ?? node, face, style);
330
+ }
331
+ m.el = node;
332
+ byNode.set(node, m);
333
+ if (m.input !== null) byNode.set(m.input, m);
334
+ byCtx.set(face.ctx, m);
335
+ face.ctx.watch = dirty;
336
+ if (get(face, 'reactions').t === K.Block) {
337
+ reactors.add(m);
338
+ newReactor = true;
339
+ }
340
+ listen(m, style);
341
+ sync(m);
342
+ return node;
343
+ }
344
+
345
+
346
+ /** Puts the face's icon before its caption, which then goes in `m.label`. */
347
+ export function withIcon(node: HTMLElement, face: ObjectCell, m: Mounted): void {
348
+ const name = iconName(face);
349
+ const svg = name !== null ? icon(name) : null;
350
+ if (svg === null) return;
351
+ m.label = el('span');
352
+ node.append(svg, m.label);
353
+ node.classList.add('tb-has-icon');
354
+ }
355
+
356
+ /** The small second icon of a tile (mark #list "List"), in a corner of its icon, and the name it adds. */
357
+ export function syncMark(m: Mounted): void {
358
+ const f = m.face;
359
+ const v = get(f, 'mark');
360
+ const name = v.t === K.Issue ? spell((v as WordCell).sym).toLowerCase() : null;
361
+ let mark = m.el.querySelector<HTMLElement>(':scope > .tb-tile-mark');
362
+ if (mark?.dataset.icon !== (name ?? undefined)) {
363
+ mark?.remove();
364
+ mark = null;
365
+ const svg = name !== null ? icon(name) : null;
366
+ if (svg !== null) {
367
+ mark = el('span', 'tb-tile-mark');
368
+ mark.dataset.icon = name!;
369
+ mark.setAttribute('aria-hidden', 'true');
370
+ mark.append(svg);
371
+ m.el.append(mark);
372
+ }
373
+ }
374
+ const said = mark !== null ? str(f, 'mark-text') : null;
375
+ if (said !== null && said !== '') m.el.setAttribute('aria-label', `${str(f, 'text') ?? ''}, ${said}`);
376
+ else m.el.removeAttribute('aria-label');
377
+ }
378
+
379
+ /** A face with an icon and no text is named after its icon: #arrow-left -> "Arrow left". */
380
+ export function iconLabel(face: ObjectCell): string | null {
381
+ const name = iconName(face);
382
+ if (name === null || (str(face, 'text') ?? '') !== '') return null;
383
+ const hint = str(face, 'hint');
384
+ if (hint !== null) return hint;
385
+ const words = name.replace(/-/g, ' ');
386
+ return words.charAt(0).toUpperCase() + words.slice(1);
387
+ }
388
+
389
+ /** Children of a container; consecutive buttons in a form, or in a step of one, share an action row. */
390
+ export function appendChildren(node: HTMLElement, face: ObjectCell, style: string): void {
391
+ let actions: HTMLElement | null = null;
392
+ for (const c of children(face)) {
393
+ const isButton = word(c, 'style') === 'button';
394
+ if ((style === 'form' || style === 'step') && isButton) {
395
+ if (actions === null) {
396
+ actions = el('div', 'tb-actions');
397
+ node.append(actions);
398
+ }
399
+ actions.append(build(c));
400
+ } else {
401
+ actions = null;
402
+ const n = build(c);
403
+ if (style === 'toolbar' && n.tagName === 'HR') n.setAttribute('aria-orientation', 'vertical');
404
+ node.append(n);
405
+ }
406
+ }
407
+ }
408
+
409
+ export function clickSubmit(face: ObjectCell, formEl: HTMLElement): void {
410
+ // after a failed submit, take the user to the first problem
411
+ void fire('ui-click', face).then(() => focusProblem(formEl));
412
+ }
413
+
414
+ export function focusProblem(scope: HTMLElement | null): void {
415
+ for (const i of Array.from(scope?.querySelectorAll<HTMLElement>('input, textarea, select') ?? [])) {
416
+ const m = byNode.get(i);
417
+ if (m !== undefined && get(m.face, 'error').t === K.String) return i.focus();
418
+ }
419
+ }
420
+