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,168 @@
1
+ // --- swipe ---------------------------------------------------------------------------
2
+ //
3
+ // On a phone, swiping a row of a table or list that has a menu to the left
4
+ // shows its first actions behind it, as mail apps do, the danger ones red, and
5
+ // More for the whole menu. A move more down than across still scrolls.
6
+
7
+ import { type ObjectCell } from '../../core/index.ts';
8
+ import { own, icon } from './faces.ts';
9
+ import { rowOf, el } from './mount.ts';
10
+ import { contextEntries } from './events.ts';
11
+ import { mountedAbove, dragging, swallowClick } from './drag.ts';
12
+ import { type MenuEntry, openMenu } from './menus.ts';
13
+
14
+ export interface Swipe {
15
+ face: ObjectCell; row: HTMLElement; x: number; y: number; id: number;
16
+ /** Where the row stood when the finger went down (0, or open), and where it is now: 0 or less. */
17
+ from: number; at: number; on: boolean; box: HTMLElement; width: number;
18
+ }
19
+ /** The row under the finger, and the one left open. */
20
+ export let swipe: Swipe | null = null;
21
+ export let swiped: Swipe | null = null;
22
+
23
+ export const SWIPE_BUTTON = 76;
24
+
25
+ export function swipeRows(node: HTMLElement, f: ObjectCell): void {
26
+ node.classList.add('tb-swipe');
27
+ node.addEventListener('pointerdown', ev => {
28
+ if (ev.pointerType !== 'touch' || swipe !== null || !matchMedia('(max-width: 640px)').matches) return;
29
+ const row = (ev.target as Element).closest<HTMLElement>('tr, li');
30
+ // a row of this face, not of one inside it
31
+ if (row === null || !rowOf.has(row) || mountedAbove(row)?.el !== node) return;
32
+ if (swiped !== null && swiped.row !== row) closeSwipe();
33
+ const open = swiped?.row === row ? swiped : null;
34
+ const box = open?.box ?? swipeBox(f, row);
35
+ if (box === null) return;
36
+ swipe = {
37
+ face: f, row, x: ev.clientX, y: ev.clientY, id: ev.pointerId,
38
+ from: open?.at ?? 0, at: open?.at ?? 0, on: false, box, width: box.childElementCount * SWIPE_BUTTON,
39
+ };
40
+ });
41
+ }
42
+
43
+ /** The buttons behind a row: its first two plain actions, and More if the menu has others. */
44
+ export function swipeBox(f: ObjectCell, row: HTMLElement): HTMLElement | null {
45
+ const all = contextEntries(f, row);
46
+ const shown = all.filter(e => !e.divider && e.sub === undefined).slice(0, 2);
47
+ if (shown.length === 0) return null;
48
+ const box = el('div', 'tb-swipe-actions');
49
+ // its corners are those of the row, or of the list a first or last row is the edge of
50
+ const rs = getComputedStyle(row);
51
+ const list = getComputedStyle(row.parentElement!);
52
+ const first = row.previousElementSibling === null;
53
+ const last = row.nextElementSibling === null;
54
+ box.style.borderTopRightRadius = first && rs.borderTopRightRadius === '0px' ? list.borderTopRightRadius : rs.borderTopRightRadius;
55
+ box.style.borderBottomRightRadius = last && rs.borderBottomRightRadius === '0px' ? list.borderBottomRightRadius : rs.borderBottomRightRadius;
56
+ box.style.borderTopLeftRadius = rs.borderTopLeftRadius;
57
+ box.style.borderBottomLeftRadius = rs.borderBottomLeftRadius;
58
+ const button = (e: MenuEntry, cls: string, run: (b: HTMLElement) => void): void => {
59
+ const b = el('button', cls);
60
+ b.type = 'button';
61
+ const svg = e.icon ? icon(e.icon) : null;
62
+ const text = el('span');
63
+ text.textContent = e.text;
64
+ b.append(...(svg !== null ? [svg] : []), text);
65
+ b.addEventListener('click', () => run(b));
66
+ box.append(b);
67
+ };
68
+ if (all.filter(e => !e.divider).length > shown.length) {
69
+ button({ text: own('more-actions'), icon: 'menu' }, 'tb-swipe-action tb-swipe-more', b => {
70
+ const r = b.getBoundingClientRect();
71
+ closeSwipe();
72
+ openMenu(all, r.left, r.bottom, row);
73
+ });
74
+ }
75
+ for (const e of shown.reverse()) {
76
+ button(e, e.danger ? 'tb-swipe-action tb-danger' : 'tb-swipe-action', () => {
77
+ closeSwipe();
78
+ e.run?.();
79
+ });
80
+ }
81
+ return box;
82
+ }
83
+
84
+ /** Shows the row moved to at (0 or less) and the buttons in the room it leaves. */
85
+ export function placeSwipe(s: Swipe): void {
86
+ s.row.style.transform = s.at === 0 ? '' : `translateX(${s.at}px)`;
87
+ // where the row ends without the move, so it holds while the row settles
88
+ const r = s.row.getBoundingClientRect();
89
+ const right = r.right - (new DOMMatrix(getComputedStyle(s.row).transform).m41);
90
+ Object.assign(s.box.style, {
91
+ top: `${r.top}px`, height: `${r.height}px`, left: `${right + s.at}px`, width: `${-s.at}px`,
92
+ });
93
+ }
94
+
95
+ export function settleSwipe(s: Swipe, at: number): void {
96
+ s.at = at;
97
+ s.row.classList.add('tb-swipe-settle');
98
+ s.box.classList.add('tb-swipe-settle');
99
+ placeSwipe(s);
100
+ if (at === 0) {
101
+ // the buttons go once the row is back
102
+ const done = (): void => { if (swiped !== s && swipe?.row !== s.row) { s.box.remove(); s.row.classList.remove('tb-swipe-settle'); } };
103
+ s.row.addEventListener('transitionend', done, { once: true });
104
+ setTimeout(done, 300);
105
+ }
106
+ }
107
+
108
+ export function closeSwipe(): void {
109
+ const s = swiped;
110
+ if (s === null) return;
111
+ swiped = null;
112
+ if (s.row.isConnected) settleSwipe(s, 0);
113
+ else s.box.remove();
114
+ }
115
+
116
+ export function onSwipeMove(ev: PointerEvent): void {
117
+ const s = swipe;
118
+ if (s === null || ev.pointerId !== s.id) return;
119
+ const dx = ev.clientX - s.x;
120
+ const dy = ev.clientY - s.y;
121
+ if (!s.on) {
122
+ if (Math.abs(dy) > 10 && Math.abs(dy) >= Math.abs(dx)) { swipe = null; return; }
123
+ if (Math.abs(dx) <= 10) return;
124
+ // a row held long enough is being dragged, not swiped
125
+ if (dragging?.on) { swipe = null; return; }
126
+ s.on = true;
127
+ s.row.classList.remove('tb-swipe-settle');
128
+ s.box.classList.remove('tb-swipe-settle');
129
+ if (!s.box.isConnected) document.body.append(s.box);
130
+ }
131
+ ev.preventDefault();
132
+ // past the buttons it moves at a third of the finger, as if held back
133
+ const at = Math.min(0, s.from + dx);
134
+ s.at = at < -s.width ? -s.width + (at + s.width) / 3 : at;
135
+ placeSwipe(s);
136
+ }
137
+
138
+ export function onSwipeEnd(ev: PointerEvent): void {
139
+ const s = swipe;
140
+ if (s === null || ev.pointerId !== s.id) return;
141
+ swipe = null;
142
+ if (!s.on) {
143
+ // a tap on the open row closes it, and does not select it
144
+ if (swiped?.row === s.row && ev.type === 'pointerup') { closeSwipe(); swallowNextClick(); }
145
+ return;
146
+ }
147
+ swallowNextClick();
148
+ const open = ev.type === 'pointerup' && s.at < -s.width / 2;
149
+ if (open) swiped = s;
150
+ else if (swiped?.row === s.row) swiped = null;
151
+ settleSwipe(s, open ? -s.width : 0);
152
+ }
153
+
154
+ /** The click that ends a move is not a click. */
155
+ export function swallowNextClick(): void {
156
+ addEventListener('click', swallowClick, true);
157
+ setTimeout(() => removeEventListener('click', swallowClick, true), 0);
158
+ }
159
+
160
+ /** A touch outside the open row and its buttons, or a scroll, closes it. */
161
+ export function outsideSwipe(ev: Event): void {
162
+ const s = swiped;
163
+ if (s === null) return;
164
+ const t = ev.target as Node | null;
165
+ if (ev.type === 'pointerdown' && t !== null && (s.box.contains(t) || s.row.contains(t))) return;
166
+ closeSwipe();
167
+ }
168
+
@@ -0,0 +1,354 @@
1
+ // --- syncing -----------------------------------------------------------------------
2
+
3
+ import { K, form, intern, text, blockOf, type Cell, type ObjectCell, type BlockCell, type StringCell } from '../../core/index.ts';
4
+ import { translate } from '../index.ts';
5
+ import { runNow, type HostTask } from '../scheduler.ts';
6
+ import { devLog } from '../devlog.ts';
7
+ import { changeScreen, mayMove, unfold, fold, isFolding } from '../motion.ts';
8
+ import { get, str, optionText, entriesOf, isGroup, optionsOf, optionsKey, fillSelect, own, display, flag, word } from './faces.ts';
9
+ import { type Mounted, host, byNode, byCtx, dirty, reactors, newReactor, reacting, fire, el, INPUTS, syncMark, iconLabel, appendChildren, setNewReactor, setReacting } from './mount.ts';
10
+ import { isData } from './drag.ts';
11
+ import { rangeText } from './calendar.ts';
12
+ import { shortcutAria, shortcutLabel } from './keys.ts';
13
+ import { countRun } from './count.ts';
14
+ import { syncSlider, syncProgress, syncIcon, required, syncRadio } from './fields.ts';
15
+ import { syncMedia, syncPhoto, syncUpload } from './uploads.ts';
16
+ import { syncWidget } from './widget.ts';
17
+ import { syncLookup } from './lookup.ts';
18
+ import { syncCanvas, syncChart } from './drawings.ts';
19
+ import { syncOrbit } from './orbits.ts';
20
+ import { syncRowsField, placeEntries } from './row-faces.ts';
21
+ import { syncRich } from './richtext.ts';
22
+ import { syncRows, morphName } from './tables.ts';
23
+ import { syncTree } from './trees.ts';
24
+ import { syncToolbar, syncBreadcrumb } from './toolbars.ts';
25
+ import { syncDialog, syncTabs, syncSteps, syncSplit } from './dialogs.ts';
26
+ import { comingScreen, enteredScreen, setComingScreen } from './screens.ts';
27
+
28
+ export function sync(m: Mounted): void {
29
+ const f = m.face;
30
+ const style = word(f, 'style') ?? '';
31
+ const role = word(f, 'role');
32
+ const t = str(f, 'text');
33
+ const hidden = flag(f, 'hidden');
34
+ const err = str(f, 'error');
35
+ const cls: string[] = [];
36
+ // a face shown or hidden after it was first on the page unfolds or folds away
37
+ const wasHidden = m.el.classList.contains('tb-hidden');
38
+ const turned = style !== 'dialog' && hidden !== wasHidden && m.el.dataset['tb'] !== undefined && m.el.isConnected && mayMove();
39
+ if (hidden && style !== 'dialog' && !(turned || isFolding(m.el))) cls.push('tb-hidden');
40
+ if (flag(f, 'fill')) cls.push('tb-fill');
41
+ if (flag(f, 'narrow-only')) cls.push('tb-narrow-only');
42
+ if (flag(f, 'wide-only')) cls.push('tb-wide-only');
43
+ if (flag(f, 'fit')) cls.push('tb-fit');
44
+ if (flag(f, 'drop')) cls.push('tb-drop-zone');
45
+ if (role !== null && !INPUTS.has(style)) cls.push(`tb-${role}`);
46
+ const size = word(f, 'size');
47
+ if (size !== null) cls.push(`tb-${size}`);
48
+ const align = word(f, 'align');
49
+ if (align !== null && style === 'row') cls.push(`tb-${align}`);
50
+ const valign = word(f, 'valign');
51
+ if (valign !== null && style === 'row') cls.push(`tb-${valign}`);
52
+ if (err !== null) cls.push('tb-invalid');
53
+ if (flag(f, 'pending')) cls.push('tb-pending');
54
+ setExtra(m.el, cls);
55
+ // the rows of a table or list carry its morph name, each with its key
56
+ const morph = style === 'table' || style === 'list' ? null : morphName(get(f, 'morph'));
57
+ if (morph !== null) m.el.dataset['tbMorph'] = morph;
58
+ else delete m.el.dataset['tbMorph'];
59
+ if (turned && hidden && !isFolding(m.el)) fold(m.el, () => { if (flag(m.face, 'hidden')) setExtra(m.el, [...(m.el.dataset['tb'] ?? '').split(' ').filter(Boolean), 'tb-hidden']); });
60
+ else if (!hidden && (turned || isFolding(m.el))) unfold(m.el);
61
+ // shown as it is, but it and its children cannot be used
62
+ m.el.inert = flag(f, 'inert');
63
+ // a face you can drag takes the focus, so the keyboard reaches its menu and moves it
64
+ if (flag(f, 'drag') && !isData(m) && m.el.tabIndex < 0) m.el.tabIndex = 0;
65
+ space(m.el, 'padding', get(f, 'pad'));
66
+ space(m.el, 'gap', get(f, 'gap'));
67
+
68
+ if (style === 'radio') return syncRadio(m, err);
69
+ if (style === 'slider') return syncSlider(m, err);
70
+ if (style === 'upload') return syncUpload(m, err);
71
+ if (style === 'photo') return syncPhoto(m, err);
72
+ if (style === 'widget') return syncWidget(m, err);
73
+ if (style === 'lookup') return syncLookup(m, err);
74
+ if (style === 'rich') return syncRich(m, err);
75
+ if (m.input !== null) {
76
+ if (m.label !== null) {
77
+ m.label.textContent = t ?? '';
78
+ m.label.classList.toggle('tb-required', required(f) && style !== 'check');
79
+ }
80
+ const v = get(f, 'value');
81
+ if (m.input instanceof HTMLInputElement && m.input.type === 'checkbox') {
82
+ m.input.checked = v.t === K.Logic && v.v;
83
+ } else {
84
+ // options given by code after the face was made
85
+ if (m.input instanceof HTMLSelectElement) {
86
+ if (m.input.dataset.options !== optionsKey(f)) fillSelect(m.input, f);
87
+ }
88
+ const want = v.t === K.None ? str(f, 'typed') ?? '' : style === 'date-range' ? rangeText(v)
89
+ : style === 'money' || style === 'date' || style === 'time' ? display(v) : form(v);
90
+ // never fight the user's typing: with the focus, only a value code set is drawn (set-values
91
+ // after Enter sent the form); a read-only field has no typing to fight
92
+ const theirs = document.activeElement === m.input && !flag(f, 'readonly') && (m.typing > 0 || v === m.typed);
93
+ if (!theirs && m.input.value !== want) m.input.value = want;
94
+ const hint = str(f, 'hint');
95
+ if (m.input instanceof HTMLSelectElement) {
96
+ const select = m.input;
97
+ // a required choice shows its hint until one is picked, and cannot be put back to none;
98
+ // one not required keeps the empty option to clear it
99
+ const empty = select.options[0]!;
100
+ empty.textContent = hint ?? own('choose');
101
+ empty.disabled = empty.hidden = required(f);
102
+ optionsOf(f).forEach((o, i) => {
103
+ const opt = select.options[i + 1];
104
+ if (opt) opt.textContent = optionText(o);
105
+ });
106
+ const groups = select.querySelectorAll('optgroup');
107
+ entriesOf(f).filter(isGroup).forEach((g, i) => {
108
+ if (groups[i]) groups[i]!.label = translate(g.title, host.rt);
109
+ });
110
+ }
111
+ else if (hint !== null) m.input.placeholder = hint;
112
+ }
113
+ const readonly = flag(f, 'readonly');
114
+ // text can be read-only; a choice or a checkbox can only be disabled
115
+ const typing = m.input instanceof HTMLTextAreaElement || (m.input instanceof HTMLInputElement && m.input.type !== 'checkbox');
116
+ if (typing) (m.input as HTMLInputElement | HTMLTextAreaElement).readOnly = readonly;
117
+ m.input.disabled = flag(f, 'disabled') || (readonly && !typing);
118
+ if (style === 'date' || style === 'date-range' || style === 'time') {
119
+ const pick = m.input.nextElementSibling as HTMLButtonElement;
120
+ pick.disabled = m.input.disabled || readonly;
121
+ const what = style === 'date' ? 'choose-date' : style === 'time' ? 'choose-time' : 'choose-dates';
122
+ pick.setAttribute('aria-label', t !== null ? own(`${what}-of`, t) : own(what));
123
+ }
124
+ m.input.setAttribute('aria-required', required(f) ? 'true' : 'false');
125
+ m.input.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
126
+ if (m.error !== null) m.error.textContent = err ?? '';
127
+ return;
128
+ }
129
+ switch (style) {
130
+ case 'title': case 'heading': case 'text': {
131
+ const tv = get(f, 'text');
132
+ if (flag(f, 'count') && countRun(m, tv)) break;
133
+ m.el.textContent = tv.t === K.None ? '' : tv.t === K.Map || tv.t === K.Word ? translate(tv, host.rt) : display(tv);
134
+ break;
135
+ }
136
+ case 'tile': {
137
+ (m.label ?? m.el).textContent = t ?? '';
138
+ syncMark(m);
139
+ break;
140
+ }
141
+ case 'button': case 'link': {
142
+ (m.label ?? m.el).textContent = t ?? '';
143
+ const named = iconLabel(f);
144
+ if (named !== null) m.el.setAttribute('aria-label', named);
145
+ else m.el.removeAttribute('aria-label');
146
+ const keys = str(f, 'shortcut');
147
+ if (keys !== null) m.el.setAttribute('aria-keyshortcuts', shortcutAria(keys));
148
+ else m.el.removeAttribute('aria-keyshortcuts');
149
+ // in a toolbar, narrow screens show only the icon, so the text goes in the tooltip too
150
+ const bar = m.label !== null && m.el.parentElement?.classList.contains('tb-toolbar') === true;
151
+ const tip = named ?? (keys !== null || bar ? t : null);
152
+ m.el.title = tip === null ? '' : keys === null ? tip : `${tip} (${shortcutLabel(keys)})`;
153
+ break;
154
+ }
155
+ // its data may have changed: the dialect deals the cards to their lanes again, if they moved
156
+ case 'board': void fire('ui-board', f); break;
157
+ case 'progress': syncProgress(m); return;
158
+ case 'canvas': syncCanvas(m); return;
159
+ case 'chart': syncChart(m); return;
160
+ case 'orbit': syncOrbit(m); return;
161
+ case 'icon': syncIcon(m); return;
162
+ case 'video': case 'audio': syncMedia(m); return;
163
+ case 'image': {
164
+ const img = m.el as HTMLImageElement;
165
+ const src = str(f, 'value') ?? '';
166
+ if (img.getAttribute('src') !== src) img.src = src;
167
+ img.alt = t ?? '';
168
+ img.classList.toggle('tb-round', flag(f, 'round'));
169
+ // a size, as in Red: the image fills it, cut to fit
170
+ const ext = get(f, 'extent');
171
+ img.style.width = ext.t === K.Pair ? `${ext.x}px` : '';
172
+ img.style.height = ext.t === K.Pair ? `${ext.y}px` : '';
173
+ return;
174
+ }
175
+ case 'table': case 'list': syncRows(m); return;
176
+ case 'rows': syncRowsField(m, err); break;
177
+ case 'tree': syncTree(m); return;
178
+ case 'breadcrumb': syncBreadcrumb(m); return;
179
+ case 'toolbar': {
180
+ if (t !== null) m.el.setAttribute('aria-label', t);
181
+ else m.el.removeAttribute('aria-label');
182
+ break;
183
+ }
184
+ case 'desktop': case 'backdrop': {
185
+ const src = str(f, 'value');
186
+ m.el.style.backgroundImage = src !== null ? `url(${JSON.stringify(src)})` : '';
187
+ break;
188
+ }
189
+ case 'dialog': syncDialog(m); break;
190
+ case 'tabs': syncTabs(m); return;
191
+ case 'steps': syncSteps(m); return;
192
+ // a step that comes to apply or not changes the bar of its steps
193
+ case 'step': {
194
+ const up = m.el.parentElement === null ? undefined : byNode.get(m.el.parentElement);
195
+ if (up !== undefined) syncSteps(up);
196
+ break;
197
+ }
198
+ case 'split': syncSplit(m); return;
199
+ case 'tab': if (m.button !== null) m.button.textContent = t ?? ''; break;
200
+ case 'panel': {
201
+ let h = m.el.querySelector<HTMLElement>(':scope > .tb-panel-title');
202
+ if (t === null) h?.remove();
203
+ else {
204
+ if (h === null) {
205
+ h = el('h2', 'tb-panel-title');
206
+ m.el.prepend(h);
207
+ }
208
+ h.textContent = t;
209
+ }
210
+ break;
211
+ }
212
+ case 'scroll': {
213
+ if (t !== null) m.el.setAttribute('aria-label', t);
214
+ else m.el.removeAttribute('aria-label');
215
+ break;
216
+ }
217
+ case 'grid': {
218
+ const cols = get(f, 'cols');
219
+ m.el.style.setProperty('--tb-cols', cols.t === K.Integer ? String(cols.v) : '2');
220
+ // [1 4 1]: a column per weight, never narrower than what it holds
221
+ const weights = cols.t === K.Block ? cols.s.data.slice(cols.i).filter(c => c.t === K.Integer) : [];
222
+ if (weights.length > 0) m.el.style.setProperty('--tb-tracks', weights.map(w => `minmax(min-content, ${form(w)}fr)`).join(' '));
223
+ else m.el.style.removeProperty('--tb-tracks');
224
+ break;
225
+ }
226
+ }
227
+ if (style === 'toolbar') syncToolbar(m.el);
228
+ if (m.el instanceof HTMLButtonElement) {
229
+ // a submit stays focusable while the form has errors; clicking it shows them
230
+ if (flag(f, 'submit')) m.el.setAttribute('aria-disabled', flag(f, 'disabled') ? 'true' : 'false');
231
+ else m.el.disabled = flag(f, 'disabled');
232
+ }
233
+ // children replaced by code: rebuild this container
234
+ const kids = get(f, 'pane');
235
+ if (kids.t === K.Block && (m.kids as BlockCell | undefined)?.s !== kids.s) {
236
+ m.kids = kids;
237
+ if (style === 'rows') {
238
+ placeEntries(m);
239
+ void fire('ui-pane', f);
240
+ return;
241
+ }
242
+ const box = m.box ?? m.el;
243
+ if (style === 'screens') placeScreen(m, box);
244
+ else {
245
+ const keep = box.querySelector(':scope > .tb-panel-title');
246
+ box.replaceChildren(...(keep ? [keep] : []));
247
+ appendChildren(box, f, style);
248
+ }
249
+ // let the dialect link the new faces to their view and form
250
+ void fire('ui-pane', f);
251
+ }
252
+ if (style === 'screens') settleScreen(m.box ?? m.el, f);
253
+ }
254
+
255
+ /**
256
+ * A screen is made away from the page; the page keeps the one it has until `entering`
257
+ * ends, or shows the new one empty, as gray bars, if its on-enter takes long.
258
+ */
259
+ export function placeScreen(m: Mounted, box: HTMLElement): void {
260
+ const coming = el('div');
261
+ setComingScreen(coming);
262
+ appendChildren(coming, m.face, 'screens');
263
+ settleScreen(box, m.face);
264
+ }
265
+
266
+ export function settleScreen(box: HTMLElement, f: ObjectCell): void {
267
+ const entering = flag(f, 'entering');
268
+ box.toggleAttribute('aria-busy', entering);
269
+ if (entering) {
270
+ // an on-enter that keeps the user waiting: the new screen comes now, gray where its data will be
271
+ if (comingScreen !== null && loadingTimer === null) {
272
+ loadingTimer = setTimeout(() => {
273
+ loadingTimer = null;
274
+ if (comingScreen === null || !flag(f, 'entering')) return;
275
+ showComing(box, f, true);
276
+ }, LOADING_AFTER);
277
+ }
278
+ return;
279
+ }
280
+ if (loadingTimer !== null) clearTimeout(loadingTimer);
281
+ loadingTimer = null;
282
+ if (comingScreen === null) delete box.dataset['tbLoading'];
283
+ else showComing(box, f, false);
284
+ }
285
+
286
+ /** Puts the screen made last on the page, when the browser has its picture of it; one entering since waits for its own. */
287
+ export function showComing(box: HTMLElement, f: ObjectCell, loading: boolean): void {
288
+ if (comingScreen === null) return;
289
+ changeScreen(box, comingScreen, () => {
290
+ const entering = flag(f, 'entering');
291
+ if (comingScreen === null || (entering && !loading)) return;
292
+ box.replaceChildren(...comingScreen.childNodes);
293
+ setComingScreen(null);
294
+ box.toggleAttribute('data-tb-loading', entering);
295
+ enteredScreen(box);
296
+ });
297
+ }
298
+
299
+ /** How long, in ms, a screen's on-enter may take before the screen comes without its data. */
300
+ export const LOADING_AFTER = 300;
301
+ export let loadingTimer: ReturnType<typeof setTimeout> | null = null;
302
+
303
+ /** Sets a spacing property to a step of the theme's scale, or back to the stylesheet's. */
304
+ export function space(e: HTMLElement, prop: string, v: Cell): void {
305
+ if (v.t === K.Integer) e.style.setProperty(prop, `var(--tb-space-${v.v})`);
306
+ else e.style.removeProperty(prop);
307
+ }
308
+
309
+ /** Sets the face-driven classes, keeping the base classes from build. */
310
+ export function setExtra(e: HTMLElement, cls: string[]): void {
311
+ const prev = (e.dataset['tb'] ?? '').split(' ').filter(Boolean);
312
+ for (const c of prev) if (!cls.includes(c)) e.classList.remove(c);
313
+ for (const c of cls) e.classList.add(c);
314
+ e.dataset['tb'] = cls.join(' ');
315
+ }
316
+
317
+ /**
318
+ * Runs the reactions whose facets changed in this round, at once, so the repaint
319
+ * that follows shows them: ui-react, given the faces on the page that have any.
320
+ */
321
+ export function react(): void {
322
+ for (const m of reactors) if (!m.el.isConnected && comingScreen?.contains(m.el) !== true) reactors.delete(m);
323
+ if (reacting !== null || reactors.size === 0 || (dirty.length === 0 && !newReactor)) return;
324
+ setNewReactor(false);
325
+ const faces = blockOf(K.Block, [...reactors].map(m => m.face));
326
+ const ht = runNow(host.sch, { data: [{ t: K.Word, sym: intern('ui-react'), bind: host.rt.lib }, faces] }, 0);
327
+ const warn = (t: HostTask) => {
328
+ if (t.state !== 'done' || t.result.t !== K.Block) return;
329
+ for (const c of (t.result as BlockCell).s.data) {
330
+ console.warn(`Trebol: ${text(c as StringCell)}`);
331
+ devLog.note('warning', text(c as StringCell));
332
+ }
333
+ };
334
+ if (ht.state === 'done') warn(ht);
335
+ else if (ht.state !== 'failed') {
336
+ setReacting(ht);
337
+ ht.watchers.push(t => {
338
+ setReacting(null);
339
+ // what changed while it waited is checked at the end of this round
340
+ setNewReactor(true);
341
+ warn(t);
342
+ });
343
+ }
344
+ }
345
+
346
+ export function syncDirty(): void {
347
+ for (const ctx of dirty) {
348
+ ctx.dirty = false;
349
+ const m = byCtx.get(ctx);
350
+ if (m !== undefined && (m.el.isConnected || comingScreen?.contains(m.el))) sync(m);
351
+ }
352
+ dirty.length = 0;
353
+ }
354
+