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,346 @@
1
+ // --- rich text -----------------------------------------------------------------------
2
+ //
3
+ // An editable box whose value is Markdown (see markdown.ts). The box is drawn from
4
+ // the tree with text nodes and read back the same way; pasting takes only the text.
5
+
6
+ import { NONE, native, value, string, text, type ObjectCell, type StringCell } from '../../core/index.ts';
7
+ import { parseMd, printMd, appendText, safeHref, type Block, type Inline } from '../markdown.ts';
8
+ import { inDevtools } from '../devtools.ts';
9
+ import { str, own, flag, word, icon } from './faces.ts';
10
+ import { type Mounted, byCtx, newId, fire, el } from './mount.ts';
11
+ import { required } from './fields.ts';
12
+
13
+ export const RICH_TOOLS: readonly [string, string][] = [
14
+ ['bold', 'bold'], ['italic', 'italic'], ['heading', 'heading'],
15
+ ['list', 'bullet-list'], ['list-ordered', 'number-list'], ['link', 'link'],
16
+ ];
17
+
18
+ export function inlineNodes(items: Inline[]): Node[] {
19
+ return items.flatMap((x): Node[] => {
20
+ switch (x.k) {
21
+ case 'text': return [document.createTextNode(x.text)];
22
+ case 'br': return [el('br')];
23
+ case 'b': case 'i': {
24
+ const e = el(x.k);
25
+ e.append(...inlineNodes(x.kids));
26
+ return [e];
27
+ }
28
+ case 'a': {
29
+ if (!safeHref(x.href)) return inlineNodes(x.kids);
30
+ const a = el('a');
31
+ a.href = x.href;
32
+ a.rel = 'noopener noreferrer';
33
+ a.target = '_blank';
34
+ a.append(...inlineNodes(x.kids));
35
+ return [a];
36
+ }
37
+ }
38
+ });
39
+ }
40
+
41
+ export function blockNodes(blocks: Block[]): Node[] {
42
+ return blocks.map(b => {
43
+ if (b.k === 'p' || b.k === 'h') {
44
+ const e = b.k === 'p' ? el('p') : el(`h${b.level}` as 'h1' | 'h2' | 'h3');
45
+ e.append(...inlineNodes(b.kids));
46
+ return e;
47
+ }
48
+ const list = el(b.k);
49
+ for (const item of b.items) {
50
+ const li = el('li');
51
+ li.append(...inlineNodes(item));
52
+ list.append(li);
53
+ }
54
+ return list;
55
+ });
56
+ }
57
+
58
+ export function readInline(nodes: Iterable<Node>): Inline[] {
59
+ const out: Inline[] = [];
60
+ for (const n of nodes) {
61
+ if (n.nodeType === Node.TEXT_NODE) {
62
+ const t = (n.textContent ?? '').replace(/\u00a0/g, ' ').replace(/[\n\r]+/g, ' ');
63
+ const last = out[out.length - 1];
64
+ if (t === '') continue;
65
+ if (last?.k === 'text') last.text += t;
66
+ else out.push({ k: 'text', text: t });
67
+ } else if (n instanceof HTMLElement) {
68
+ const kids = readInline(n.childNodes);
69
+ switch (n.tagName) {
70
+ case 'BR': out.push({ k: 'br' }); break;
71
+ case 'B': case 'STRONG': if (kids.length > 0) out.push({ k: 'b', kids }); break;
72
+ case 'I': case 'EM': if (kids.length > 0) out.push({ k: 'i', kids }); break;
73
+ case 'A': if (kids.length > 0) out.push({ k: 'a', href: n.getAttribute('href') ?? '', kids }); break;
74
+ default: out.push(...kids);
75
+ }
76
+ }
77
+ }
78
+ // the browser ends an empty or last line with a <br> that shows nothing
79
+ while (out[out.length - 1]?.k === 'br') out.pop();
80
+ return out;
81
+ }
82
+
83
+ export const RICH_BLOCK = /^(P|DIV|H[1-6]|UL|OL|BLOCKQUOTE|SECTION)$/;
84
+
85
+ export function readBlocks(box: HTMLElement): Block[] {
86
+ const out: Block[] = [];
87
+ let loose: Node[] = [];
88
+ const flush = (): void => {
89
+ const kids = readInline(loose);
90
+ if (kids.length > 0) out.push({ k: 'p', kids });
91
+ loose = [];
92
+ };
93
+ for (const n of box.childNodes) {
94
+ if (!(n instanceof HTMLElement) || !RICH_BLOCK.test(n.tagName)) {
95
+ loose.push(n);
96
+ continue;
97
+ }
98
+ flush();
99
+ if (n.tagName === 'UL' || n.tagName === 'OL') {
100
+ const items = [...n.children].map(li => readInline(li.childNodes)).filter(x => x.length > 0);
101
+ if (items.length > 0) out.push({ k: n.tagName === 'UL' ? 'ul' : 'ol', items });
102
+ } else if (n.tagName === 'DIV' || n.tagName === 'SECTION' || n.tagName === 'BLOCKQUOTE') {
103
+ out.push(...readBlocks(n));
104
+ } else {
105
+ const kids = readInline(n.childNodes);
106
+ if (kids.length === 0) continue;
107
+ if (n.tagName === 'P') out.push({ k: 'p', kids });
108
+ else out.push({ k: 'h', level: Math.min(3, Number(n.tagName[1])), kids });
109
+ }
110
+ }
111
+ flush();
112
+ return out;
113
+ }
114
+
115
+ export function buildRich(face: ObjectCell, m: Mounted): HTMLElement {
116
+ const id = `tb-${newId()}`;
117
+ const wrap = el('div', 'tb-field');
118
+ const label = el('div', 'tb-label');
119
+ label.id = id;
120
+ const control = el('div', 'tb-control');
121
+ const shell = el('div', 'tb-rich');
122
+ const tools = el('div', 'tb-rich-tools');
123
+ tools.setAttribute('role', 'toolbar');
124
+ const box = el('div', 'tb-input tb-rich-box');
125
+ box.setAttribute('role', 'textbox');
126
+ box.setAttribute('aria-multiline', 'true');
127
+ box.setAttribute('aria-labelledby', id);
128
+ const linkRow = el('div', 'tb-rich-link');
129
+ linkRow.hidden = true;
130
+ const linkInput = el('input', 'tb-input');
131
+ linkInput.type = 'url';
132
+ const linkOk = el('button', 'tb-button tb-small');
133
+ linkOk.type = 'button';
134
+ linkRow.append(linkInput, linkOk);
135
+ const error = el('div', 'tb-error');
136
+ error.id = `${id}-error`;
137
+ error.setAttribute('aria-live', 'polite');
138
+ box.setAttribute('aria-describedby', error.id);
139
+ label.addEventListener('click', () => box.focus());
140
+ keepCarets();
141
+
142
+ const changed = (): void => {
143
+ if (inserting) return;
144
+ const md = printMd(readBlocks(box));
145
+ if (md === m.shown) return;
146
+ m.shown = md;
147
+ void fire('ui-input', face, md === '' ? NONE : string(md));
148
+ };
149
+ box.addEventListener('input', changed);
150
+ box.addEventListener('paste', ev => {
151
+ ev.preventDefault();
152
+ document.execCommand('insertText', false, ev.clipboardData?.getData('text/plain') ?? '');
153
+ });
154
+ box.addEventListener('drop', ev => ev.preventDefault());
155
+ wrap.addEventListener('focusout', ev => {
156
+ if (!wrap.contains(ev.relatedTarget as Node | null) && !inDevtools(ev.relatedTarget)) void fire('ui-blur', face);
157
+ });
158
+
159
+ let saved: Range | null = null;
160
+ const inHeading = (): boolean => {
161
+ const n = getSelection()?.anchorNode ?? null;
162
+ return (n instanceof Element ? n : n?.parentElement)?.closest('h1, h2, h3') != null;
163
+ };
164
+ const command = (what: string): void => {
165
+ box.focus();
166
+ switch (what) {
167
+ case 'bold': case 'italic': document.execCommand(what); break;
168
+ case 'heading': document.execCommand('formatBlock', false, inHeading() ? 'P' : 'H2'); break;
169
+ case 'bullet-list': document.execCommand('insertUnorderedList'); break;
170
+ case 'number-list': document.execCommand('insertOrderedList'); break;
171
+ case 'link': {
172
+ const s = getSelection();
173
+ saved = s !== null && s.rangeCount > 0 && box.contains(s.anchorNode) ? s.getRangeAt(0).cloneRange() : null;
174
+ linkRow.hidden = false;
175
+ linkInput.value = '';
176
+ linkInput.removeAttribute('aria-invalid');
177
+ linkInput.focus();
178
+ return;
179
+ }
180
+ }
181
+ changed();
182
+ };
183
+ for (const [name, what] of RICH_TOOLS) {
184
+ const b = el('button', 'tb-rich-tool');
185
+ b.type = 'button';
186
+ b.dataset['tool'] = what;
187
+ const svg = icon(name);
188
+ if (svg !== null) b.append(svg);
189
+ // pressing a tool keeps the selection in the text
190
+ b.addEventListener('mousedown', ev => ev.preventDefault());
191
+ b.addEventListener('click', () => command(what));
192
+ tools.append(b);
193
+ }
194
+ const addLink = (): void => {
195
+ const typed = linkInput.value.trim();
196
+ const url = /^[a-z][a-z0-9+.-]*:/i.test(typed) ? typed : `https://${typed}`;
197
+ if (typed === '' || !safeHref(url)) {
198
+ linkInput.setAttribute('aria-invalid', 'true');
199
+ return;
200
+ }
201
+ linkRow.hidden = true;
202
+ box.focus();
203
+ const s = getSelection();
204
+ if (saved !== null && s !== null) {
205
+ s.removeAllRanges();
206
+ s.addRange(saved);
207
+ }
208
+ if (saved === null || saved.collapsed) document.execCommand('insertText', false, typed);
209
+ if (saved !== null && !saved.collapsed) document.execCommand('createLink', false, url);
210
+ else {
211
+ // what was just typed becomes the link
212
+ const r = getSelection()?.getRangeAt(0);
213
+ if (r !== undefined) {
214
+ const node = r.startContainer;
215
+ const start = Math.max(0, r.startOffset - typed.length);
216
+ const whole = document.createRange();
217
+ whole.setStart(node, start);
218
+ whole.setEnd(node, r.startOffset);
219
+ s?.removeAllRanges();
220
+ s?.addRange(whole);
221
+ document.execCommand('createLink', false, url);
222
+ }
223
+ }
224
+ changed();
225
+ };
226
+ linkOk.addEventListener('click', addLink);
227
+ linkInput.addEventListener('keydown', ev => {
228
+ if (ev.key === 'Enter') {
229
+ ev.preventDefault();
230
+ addLink();
231
+ } else if (ev.key === 'Escape') {
232
+ ev.stopPropagation();
233
+ linkRow.hidden = true;
234
+ box.focus();
235
+ }
236
+ });
237
+ shell.append(tools, linkRow, box);
238
+ control.append(shell, error);
239
+ wrap.append(label, control);
240
+ m.label = label;
241
+ m.error = error;
242
+ m.box = box;
243
+ return wrap;
244
+ }
245
+
246
+ export function syncRich(m: Mounted, err: string | null): void {
247
+ const f = m.face;
248
+ const box = m.box!;
249
+ m.label!.textContent = str(f, 'text') ?? '';
250
+ m.label!.classList.toggle('tb-required', required(f));
251
+ const shell = box.parentElement!;
252
+ const readonly = flag(f, 'readonly');
253
+ const disabled = flag(f, 'disabled');
254
+ const locked = readonly || disabled;
255
+ box.contentEditable = locked ? 'false' : 'true';
256
+ box.tabIndex = disabled ? -1 : 0;
257
+ box.setAttribute('aria-readonly', locked ? 'true' : 'false');
258
+ box.setAttribute('aria-disabled', disabled ? 'true' : 'false');
259
+ box.setAttribute('aria-required', required(f) ? 'true' : 'false');
260
+ box.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
261
+ const tools = shell.querySelector<HTMLElement>('.tb-rich-tools')!;
262
+ tools.hidden = locked;
263
+ tools.setAttribute('aria-label', str(f, 'text') ?? '');
264
+ for (const b of tools.querySelectorAll<HTMLButtonElement>('button')) {
265
+ const name = own(b.dataset['tool']!);
266
+ b.setAttribute('aria-label', name);
267
+ b.title = name;
268
+ }
269
+ const linkRow = shell.querySelector<HTMLElement>('.tb-rich-link')!;
270
+ if (locked) linkRow.hidden = true;
271
+ linkRow.querySelector('input')!.placeholder = own('link-url');
272
+ linkRow.querySelector('button')!.textContent = own('link-add');
273
+ m.error!.textContent = err ?? '';
274
+ // never fight the user's typing: only what code set is drawn again
275
+ const md = str(f, 'value') ?? '';
276
+ if (md !== m.shown && document.activeElement !== box) {
277
+ m.shown = md;
278
+ box.replaceChildren(...blockNodes(parseMd(md)));
279
+ richCaret.delete(box);
280
+ }
281
+ }
282
+
283
+ // --- inserting text at the cursor ---------------------------------------------------
284
+ //
285
+ // insert-text puts text where the user's cursor was, even after a click on a button or
286
+ // a choice took the focus away: an input keeps its selection, a rich text box has
287
+ // its last one kept here.
288
+
289
+ /** Inputs that have had the focus, so a cursor of their own. */
290
+ export const hadCaret = new WeakSet<Element>();
291
+ /** The last selection inside each rich text box. */
292
+ export const richCaret = new WeakMap<HTMLElement, Range>();
293
+ /** While insert-text writes in a rich text box, its own input event is not the user's. */
294
+ export let inserting = false;
295
+
296
+ export let keepingCarets = false;
297
+
298
+ export function keepCarets(): void {
299
+ if (keepingCarets) return;
300
+ keepingCarets = true;
301
+ document.addEventListener('selectionchange', () => {
302
+ const s = getSelection();
303
+ if (s === null || s.rangeCount === 0) return;
304
+ const n = s.anchorNode;
305
+ const box = (n instanceof Element ? n : n?.parentElement)?.closest<HTMLElement>('.tb-rich-box');
306
+ if (box != null) richCaret.set(box, s.getRangeAt(0).cloneRange());
307
+ });
308
+ }
309
+
310
+ native('insert-at-caret', '["The value of a field, area or rich text with text put where its cursor was; the cursor goes after it." face [object!] text [string!]]', (_t, f) => {
311
+ const face = f.args[0] as ObjectCell;
312
+ const add = text(f.args[1] as StringCell);
313
+ const rich = word(face, 'style') === 'rich';
314
+ const was = str(face, 'value') ?? '';
315
+ const m = byCtx.get(face.ctx);
316
+ if (m === undefined || !m.el.isConnected) return value(string(rich ? appendText(was, add) : was + add));
317
+ if (rich) {
318
+ const box = m.box!;
319
+ const at = richCaret.get(box);
320
+ box.focus();
321
+ const s = getSelection()!;
322
+ if (at !== undefined && box.contains(at.commonAncestorContainer)) {
323
+ s.removeAllRanges();
324
+ s.addRange(at);
325
+ } else {
326
+ s.selectAllChildren(box);
327
+ s.collapseToEnd();
328
+ }
329
+ inserting = true;
330
+ try {
331
+ document.execCommand('insertText', false, add);
332
+ } finally {
333
+ inserting = false;
334
+ }
335
+ const md = printMd(readBlocks(box));
336
+ m.shown = md;
337
+ return value(string(md));
338
+ }
339
+ const input = m.input as HTMLInputElement | HTMLTextAreaElement;
340
+ const end = input.value.length;
341
+ const [from, to] = hadCaret.has(input) ? [input.selectionStart ?? end, input.selectionEnd ?? end] : [end, end];
342
+ input.focus();
343
+ input.setRangeText(add, from, to, 'end');
344
+ return value(string(input.value));
345
+ });
346
+
@@ -0,0 +1,75 @@
1
+ // --- rows ---------------------------------------------------------------------------
2
+ //
3
+ // The dialect makes the rows as faces; the host only draws them, as one line each
4
+ // with the labels of the first line as the headers, or as cards on narrow screens.
5
+
6
+ import { K } from '../../core/index.ts';
7
+ import { get, str, flag, children } from './faces.ts';
8
+ import { type Mounted, byCtx, newId, fire, el, build } from './mount.ts';
9
+ import { required } from './fields.ts';
10
+
11
+ export function buildRowsField(m: Mounted): HTMLElement {
12
+ const id = `tb-${newId()}`;
13
+ const wrap = el('div', 'tb-field');
14
+ const label = el('div', 'tb-label');
15
+ label.id = id;
16
+ const control = el('div', 'tb-control');
17
+ const box = el('div', 'tb-rows');
18
+ box.setAttribute('role', 'group');
19
+ box.setAttribute('aria-labelledby', id);
20
+ const error = el('div', 'tb-error');
21
+ error.setAttribute('aria-live', 'polite');
22
+ control.append(box, error);
23
+ wrap.append(label, control);
24
+ m.label = label;
25
+ m.error = error;
26
+ m.box = box;
27
+ return wrap;
28
+ }
29
+
30
+ export function syncRowsField(m: Mounted, err: string | null): void {
31
+ const f = m.face;
32
+ m.label!.textContent = str(f, 'text') ?? '';
33
+ m.label!.classList.toggle('tb-required', required(f));
34
+ m.error!.textContent = err ?? '';
35
+ // code gave it a new value or changed its flags: the dialect makes the rows again
36
+ const v = get(f, 'value');
37
+ const now = [v.t === K.Block ? v.s : null, flag(f, 'readonly'), flag(f, 'disabled')];
38
+ const was = m.shown as unknown[] | undefined;
39
+ if (was === undefined || now.some((x, i) => x !== was[i])) {
40
+ m.shown = now;
41
+ void fire('ui-rows', f);
42
+ }
43
+ }
44
+
45
+ /** Puts the rows in their new order, keeping the nodes (and the focus) of the ones that stay. */
46
+ export function placeEntries(m: Mounted): void {
47
+ const box = m.box!;
48
+ const faces = children(m.face);
49
+ const active = document.activeElement;
50
+ const fresh: HTMLElement[] = [];
51
+ const nodes = faces.map(c => {
52
+ const old = byCtx.get(c.ctx);
53
+ if (old !== undefined && old.el.parentElement === box) return old.el;
54
+ const n = build(c);
55
+ fresh.push(n);
56
+ return n;
57
+ });
58
+ let cursor = box.firstChild;
59
+ for (const n of nodes) {
60
+ if (n === cursor) cursor = cursor.nextSibling;
61
+ else box.insertBefore(n, cursor);
62
+ }
63
+ while (cursor !== null) {
64
+ const next = cursor.nextSibling;
65
+ box.removeChild(cursor);
66
+ cursor = next;
67
+ }
68
+ const add = nodes[nodes.length - 1];
69
+ // the button pressed went away with its row, or a row was just added
70
+ if (active !== null && active !== document.body && !active.isConnected) (add as HTMLElement | undefined)?.focus();
71
+ else if (fresh.length === 1 && (active === add || active === document.body)) {
72
+ fresh[0]!.querySelector<HTMLElement>('input, textarea, select, [contenteditable="true"]')?.focus();
73
+ }
74
+ }
75
+
@@ -0,0 +1,142 @@
1
+ // --- screens ---------------------------------------------------------------------
2
+ //
3
+ // The page's link is `#/name/arg...`, each part URI-encoded. `go` pushes a
4
+ // history entry; back, forward and links to a screen change the hash, and the
5
+ // host passes the new route to `ui-route`.
6
+
7
+ import { logic, K, NONE, native, value, string, form, spell, text, blockOf, type Cell, type ObjectCell, type BlockCell, type WordCell, type StringCell } from '../../core/index.ts';
8
+ import { travelled } from '../motion.ts';
9
+ import { el } from './mount.ts';
10
+ import { int } from './drag.ts';
11
+
12
+ /** The screens face of the page. */
13
+ export let screensFace: ObjectCell | null = null;
14
+ export function setScreensFace(v: ObjectCell | null): void {
15
+ screensFace = v;
16
+ }
17
+ export let comingScreen: HTMLElement | null = null; // the screen being made, not on the page yet
18
+ export function setComingScreen(v: HTMLElement | null): void {
19
+ comingScreen = v;
20
+ }
21
+ /** Where a change of screen is announced. */
22
+ export let screenNews: HTMLElement | null = null;
23
+
24
+ export function routeParts(): string[] {
25
+ const h = location.hash.replace(/^#\/?/, '');
26
+ if (h === '') return [];
27
+ return h.split('/').map(p => {
28
+ try { return decodeURIComponent(p); } catch { return p; }
29
+ });
30
+ }
31
+
32
+ export function routeBlock(): BlockCell {
33
+ return blockOf(K.Block, routeParts().map(p => string(p)));
34
+ }
35
+
36
+ /** `#/client/42` for the word `client` or the path `client/42`; null for anything else. */
37
+ export function screenHref(v: Cell): string | null {
38
+ const part = (c: Cell): string => encodeURIComponent(c.t === K.String ? text(c as StringCell) : c.t === K.Word ? spell((c as WordCell).sym) : form(c));
39
+ if (v.t === K.Word) return '#/' + part(v);
40
+ if (v.t === K.Path) return '#/' + v.s.data.slice(v.i).map(part).join('/');
41
+ return null;
42
+ }
43
+
44
+ native('route', '["The screen and arguments in the address bar." ]', () => value(routeBlock()));
45
+
46
+ // Where the page stands in the browser's history: how many of its screens lie behind the
47
+ // current one (depth) and how far forward it has been (top). Each entry keeps both in
48
+ // history.state, so back, forward and a reload find them again.
49
+ export let here = { depth: 0, top: 0 };
50
+ export function setHere(v: typeof here): void {
51
+ here = v;
52
+ }
53
+
54
+ /** At start: an entry with no state is the first of the page. */
55
+ export function startHistory(): void {
56
+ const state = history.state as { depth?: number; top?: number } | null;
57
+ if (typeof state?.depth === 'number') here = { depth: state.depth, top: state.top ?? state.depth };
58
+ history.replaceState({ ...here }, '');
59
+ }
60
+
61
+ /** Where the page stood before the link last changed, so route-undo can return; set while it returns. */
62
+ export let before = { depth: 0, top: 0 };
63
+ export let undoing: { depth: number; top: number } | null = null;
64
+ export function setUndoing(v: { depth: number; top: number } | null): void {
65
+ undoing = v;
66
+ }
67
+
68
+ /** The link changed: back or forward lands on an entry the page made; a link or a typed address makes a new one, a step ahead. */
69
+ export function landHistory(): void {
70
+ const state = history.state as { depth?: number } | null;
71
+ before = { ...here };
72
+ if (typeof state?.depth === 'number') here = { depth: state.depth, top: here.top };
73
+ else here = { depth: here.depth + 1, top: here.depth + 1 };
74
+ travelled(here.depth < before.depth ? 'back' : 'on');
75
+ history.replaceState({ ...here }, '');
76
+ }
77
+
78
+ native('set-route', '["Puts a screen and arguments in the address bar." route [block!] replace [logic!]]', (_t, f) => {
79
+ const href = screenHref({ ...(f.args[0] as BlockCell), t: K.Path } as Cell) ?? '#/';
80
+ const replace = f.args[1]!.t === K.Logic && f.args[1]!.v;
81
+ if (replace) {
82
+ history.replaceState({ ...here }, '', href);
83
+ return value(f.args[0]!);
84
+ }
85
+ // a new screen after going back drops the ones ahead, as the browser does
86
+ here = { depth: here.depth + 1, top: here.depth + 1 };
87
+ travelled('on');
88
+ history.pushState({ ...here }, '', href);
89
+ return value(f.args[0]!);
90
+ });
91
+
92
+ // A screen that does not let the page leave puts its link back: back for a new entry,
93
+ // the other way for back or forward. The link that comes back is not a change of screen.
94
+ native('route-undo', '["Puts back the link the page had before the last change." ]', () => {
95
+ const by = before.depth - here.depth;
96
+ if (by === 0) return value(NONE);
97
+ undoing = before;
98
+ history.go(by);
99
+ return value(NONE);
100
+ });
101
+
102
+ // While the screen on show has changes not saved, the browser asks before closing or
103
+ // reloading the page, with its own words: no browser lets a page give its text.
104
+ export let unsaved = false;
105
+ native('set-unsaved', '["Whether the browser asks before closing the page." on [logic!]]', (_t, f) => {
106
+ unsaved = f.args[0]!.t === K.Logic && f.args[0]!.v;
107
+ return value(f.args[0]!);
108
+ });
109
+
110
+ native('route-steps', '["How many steps back and forward the history has." ]', () => value(blockOf(K.Block, [int(here.depth), int(here.top - here.depth)])));
111
+
112
+ native('route-back', '["Goes one step back in the history." ]', () => {
113
+ if (here.depth === 0) return value(logic(false));
114
+ history.back();
115
+ return value(logic(true));
116
+ });
117
+
118
+ native('route-forward', '["Goes one step forward in the history." ]', () => {
119
+ if (here.depth >= here.top) return value(logic(false));
120
+ history.forward();
121
+ return value(logic(true));
122
+ });
123
+
124
+ /** After code showed another screen: focus it and announce its title. */
125
+ export function enteredScreen(box: HTMLElement): void {
126
+ const screen = box.firstElementChild as HTMLElement | null;
127
+ if (screen === null) return;
128
+ const heading = screen.querySelector<HTMLElement>('h1, h2');
129
+ const target = heading ?? screen;
130
+ if (target.tabIndex < 0) target.tabIndex = -1;
131
+ target.focus({ preventScroll: true });
132
+ window.scrollTo(0, 0);
133
+ if (screenNews === null) {
134
+ screenNews = el('div', 'tb-sr-only');
135
+ screenNews.setAttribute('aria-live', 'polite');
136
+ document.body.append(screenNews);
137
+ }
138
+ const news = screenNews;
139
+ news.textContent = '';
140
+ setTimeout(() => { news.textContent = screen.getAttribute('aria-label') ?? ''; }, 50);
141
+ }
142
+