trebol 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +10040 -7519
  3. package/dist/lib/core/boot.js +10 -7
  4. package/dist/lib/core/eval.js +2 -2
  5. package/dist/lib/core/machine/checks.d.ts +1 -0
  6. package/dist/lib/core/machine/checks.js +250 -0
  7. package/dist/lib/core/machine/events.d.ts +1 -0
  8. package/dist/lib/core/machine/events.js +365 -0
  9. package/dist/lib/core/machine/running.d.ts +1 -0
  10. package/dist/lib/core/machine/running.js +180 -0
  11. package/dist/lib/core/machine/shared.d.ts +1 -0
  12. package/dist/lib/core/machine/shared.js +123 -0
  13. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  14. package/dist/lib/core/machine/snapshot.js +244 -0
  15. package/dist/lib/core/machine/spec.d.ts +1 -0
  16. package/dist/lib/core/machine/spec.js +274 -0
  17. package/dist/lib/core/machine/words.d.ts +1 -0
  18. package/dist/lib/core/machine/words.js +98 -0
  19. package/dist/lib/core/machine.d.ts +1 -0
  20. package/dist/lib/core/machine.js +17 -0
  21. package/dist/lib/core/mezz.js +31 -0
  22. package/dist/lib/core/natives/control.js +1 -1
  23. package/dist/lib/core/natives/data.js +6 -1
  24. package/dist/lib/core/natives/series.js +49 -9
  25. package/dist/lib/core/natives/text.js +0 -1
  26. package/dist/lib/host/browser/calendar.d.ts +34 -0
  27. package/dist/lib/host/browser/calendar.js +268 -0
  28. package/dist/lib/host/browser/count.d.ts +15 -0
  29. package/dist/lib/host/browser/count.js +48 -0
  30. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  31. package/dist/lib/host/browser/dialogs.js +443 -0
  32. package/dist/lib/host/browser/drag.d.ts +70 -0
  33. package/dist/lib/host/browser/drag.js +268 -0
  34. package/dist/lib/host/browser/drawings.d.ts +30 -0
  35. package/dist/lib/host/browser/drawings.js +176 -0
  36. package/dist/lib/host/browser/events.d.ts +16 -0
  37. package/dist/lib/host/browser/events.js +125 -0
  38. package/dist/lib/host/browser/faces.d.ts +35 -0
  39. package/dist/lib/host/browser/faces.js +131 -0
  40. package/dist/lib/host/browser/fields.d.ts +16 -0
  41. package/dist/lib/host/browser/fields.js +322 -0
  42. package/dist/lib/host/browser/keys.d.ts +29 -0
  43. package/dist/lib/host/browser/keys.js +151 -0
  44. package/dist/lib/host/browser/lookup.d.ts +25 -0
  45. package/dist/lib/host/browser/lookup.js +275 -0
  46. package/dist/lib/host/browser/menus.d.ts +20 -0
  47. package/dist/lib/host/browser/menus.js +149 -0
  48. package/dist/lib/host/browser/mount.d.ts +92 -0
  49. package/dist/lib/host/browser/mount.js +448 -0
  50. package/dist/lib/host/browser/orbits.d.ts +90 -0
  51. package/dist/lib/host/browser/orbits.js +379 -0
  52. package/dist/lib/host/browser/page.d.ts +13 -0
  53. package/dist/lib/host/browser/page.js +35 -0
  54. package/dist/lib/host/browser/richtext.d.ts +19 -0
  55. package/dist/lib/host/browser/richtext.js +379 -0
  56. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  57. package/dist/lib/host/browser/row-faces.js +74 -0
  58. package/dist/lib/host/browser/screens.d.ts +37 -0
  59. package/dist/lib/host/browser/screens.js +140 -0
  60. package/dist/lib/host/browser/swipe.d.ts +31 -0
  61. package/dist/lib/host/browser/swipe.js +178 -0
  62. package/dist/lib/host/browser/sync.d.ts +24 -0
  63. package/dist/lib/host/browser/sync.js +450 -0
  64. package/dist/lib/host/browser/tables.d.ts +65 -0
  65. package/dist/lib/host/browser/tables.js +505 -0
  66. package/dist/lib/host/browser/times.d.ts +14 -0
  67. package/dist/lib/host/browser/times.js +114 -0
  68. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  69. package/dist/lib/host/browser/toolbars.js +129 -0
  70. package/dist/lib/host/browser/trees.d.ts +20 -0
  71. package/dist/lib/host/browser/trees.js +235 -0
  72. package/dist/lib/host/browser/uploads.d.ts +29 -0
  73. package/dist/lib/host/browser/uploads.js +370 -0
  74. package/dist/lib/host/browser/widget.d.ts +13 -0
  75. package/dist/lib/host/browser/widget.js +105 -0
  76. package/dist/lib/host/browser.d.ts +25 -0
  77. package/dist/lib/host/browser.js +84 -4812
  78. package/dist/lib/host/devtools/console.d.ts +9 -0
  79. package/dist/lib/host/devtools/console.js +104 -0
  80. package/dist/lib/host/devtools/faces.d.ts +25 -0
  81. package/dist/lib/host/devtools/faces.js +169 -0
  82. package/dist/lib/host/devtools/log.d.ts +3 -0
  83. package/dist/lib/host/devtools/log.js +32 -0
  84. package/dist/lib/host/devtools/machines.d.ts +34 -0
  85. package/dist/lib/host/devtools/machines.js +212 -0
  86. package/dist/lib/host/devtools/read.d.ts +13 -0
  87. package/dist/lib/host/devtools/read.js +52 -0
  88. package/dist/lib/host/devtools/state.d.ts +90 -0
  89. package/dist/lib/host/devtools/state.js +123 -0
  90. package/dist/lib/host/devtools.d.ts +15 -19
  91. package/dist/lib/host/devtools.js +39 -427
  92. package/dist/lib/host/fonts.js +4 -3
  93. package/dist/lib/host/icons.js +4 -0
  94. package/dist/lib/host/index.d.ts +3 -1
  95. package/dist/lib/host/index.js +107 -4
  96. package/dist/lib/host/locale.d.ts +18 -0
  97. package/dist/lib/host/locale.js +75 -5
  98. package/dist/lib/host/markdown.d.ts +4 -0
  99. package/dist/lib/host/markdown.js +8 -0
  100. package/dist/lib/host/modules.js +118 -19
  101. package/dist/lib/host/motion.d.ts +32 -0
  102. package/dist/lib/host/motion.js +190 -0
  103. package/dist/lib/host/net.js +5 -2
  104. package/dist/lib/host/palette.d.ts +17 -0
  105. package/dist/lib/host/palette.js +131 -0
  106. package/dist/lib/host/scheduler.d.ts +2 -0
  107. package/dist/lib/host/scheduler.js +4 -0
  108. package/dist/lib/host/share.d.ts +31 -0
  109. package/dist/lib/host/share.js +134 -0
  110. package/dist/lib/host/theme/classic.d.ts +1 -0
  111. package/dist/lib/host/theme/classic.js +120 -0
  112. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  113. package/dist/lib/host/theme/cupertino.js +65 -0
  114. package/dist/lib/host/theme/data.d.ts +1 -0
  115. package/dist/lib/host/theme/data.js +109 -0
  116. package/dist/lib/host/theme/devtools.d.ts +1 -0
  117. package/dist/lib/host/theme/devtools.js +166 -0
  118. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  119. package/dist/lib/host/theme/dialogs.js +63 -0
  120. package/dist/lib/host/theme/fluent.d.ts +1 -0
  121. package/dist/lib/host/theme/fluent.js +64 -0
  122. package/dist/lib/host/theme/forms.d.ts +1 -0
  123. package/dist/lib/host/theme/forms.js +239 -0
  124. package/dist/lib/host/theme/fusion.d.ts +1 -0
  125. package/dist/lib/host/theme/fusion.js +66 -0
  126. package/dist/lib/host/theme/layout.d.ts +1 -0
  127. package/dist/lib/host/theme/layout.js +166 -0
  128. package/dist/lib/host/theme/media.d.ts +1 -0
  129. package/dist/lib/host/theme/media.js +106 -0
  130. package/dist/lib/host/theme/navigation.d.ts +1 -0
  131. package/dist/lib/host/theme/navigation.js +36 -0
  132. package/dist/lib/host/theme/panes.d.ts +1 -0
  133. package/dist/lib/host/theme/panes.js +99 -0
  134. package/dist/lib/host/theme/popups.d.ts +1 -0
  135. package/dist/lib/host/theme/popups.js +149 -0
  136. package/dist/lib/host/theme/responsive.d.ts +1 -0
  137. package/dist/lib/host/theme/responsive.js +103 -0
  138. package/dist/lib/host/theme/toasts.d.ts +1 -0
  139. package/dist/lib/host/theme/toasts.js +62 -0
  140. package/dist/lib/host/theme.js +19 -1384
  141. package/dist/lib/ui/dialect/events.trb +276 -0
  142. package/dist/lib/ui/dialect/faces.trb +135 -0
  143. package/dist/lib/ui/dialect/layout.trb +283 -0
  144. package/dist/lib/ui/dialect/palette.trb +87 -0
  145. package/dist/lib/ui/dialect/problems.trb +174 -0
  146. package/dist/lib/ui/dialect/reactions.trb +198 -0
  147. package/dist/lib/ui/dialect/rows.trb +208 -0
  148. package/dist/lib/ui/dialect/screens.trb +293 -0
  149. package/dist/lib/ui/dialect/steps.trb +203 -0
  150. package/dist/lib/ui/dialect/validation.trb +137 -0
  151. package/dist/lib/ui/dialect/view.trb +110 -0
  152. package/dist/lib/ui/dialect/walking.trb +98 -0
  153. package/dist/lib/ui/dialect/words.trb +70 -0
  154. package/dist/lib/ui/i18n.trb +17 -0
  155. package/dist/lib/ui/test.trb +3 -556
  156. package/dist/lib/ui/testing/actions.trb +421 -0
  157. package/dist/lib/ui/testing/faces.trb +148 -0
  158. package/dist/lib/ui/testing/server.trb +73 -0
  159. package/dist/lib/ui/ui.trb +25 -1675
  160. package/dist/trebol.js +4463 -1891
  161. package/package.json +1 -1
  162. package/src/core/boot.ts +9 -6
  163. package/src/core/eval.ts +2 -2
  164. package/src/core/machine/checks.ts +251 -0
  165. package/src/core/machine/events.ts +366 -0
  166. package/src/core/machine/running.ts +181 -0
  167. package/src/core/machine/shared.ts +124 -0
  168. package/src/core/machine/snapshot.ts +245 -0
  169. package/src/core/machine/spec.ts +275 -0
  170. package/src/core/machine/words.ts +99 -0
  171. package/src/core/machine.ts +20 -0
  172. package/src/core/mezz.ts +31 -0
  173. package/src/core/natives/control.ts +1 -1
  174. package/src/core/natives/data.ts +6 -1
  175. package/src/core/natives/parse.ts +1 -1
  176. package/src/core/natives/series.ts +47 -11
  177. package/src/core/natives/text.ts +0 -1
  178. package/src/host/browser/calendar.ts +264 -0
  179. package/src/host/browser/count.ts +48 -0
  180. package/src/host/browser/dialogs.ts +439 -0
  181. package/src/host/browser/drag.ts +278 -0
  182. package/src/host/browser/drawings.ts +178 -0
  183. package/src/host/browser/events.ts +117 -0
  184. package/src/host/browser/faces.ts +143 -0
  185. package/src/host/browser/fields.ts +315 -0
  186. package/src/host/browser/keys.ts +145 -0
  187. package/src/host/browser/lookup.ts +262 -0
  188. package/src/host/browser/menus.ts +146 -0
  189. package/src/host/browser/mount.ts +420 -0
  190. package/src/host/browser/orbits.ts +418 -0
  191. package/src/host/browser/page.ts +43 -0
  192. package/src/host/browser/richtext.ts +346 -0
  193. package/src/host/browser/row-faces.ts +75 -0
  194. package/src/host/browser/screens.ts +142 -0
  195. package/src/host/browser/swipe.ts +168 -0
  196. package/src/host/browser/sync.ts +354 -0
  197. package/src/host/browser/tables.ts +461 -0
  198. package/src/host/browser/times.ts +115 -0
  199. package/src/host/browser/toolbars.ts +126 -0
  200. package/src/host/browser/trees.ts +205 -0
  201. package/src/host/browser/uploads.ts +367 -0
  202. package/src/host/browser/widget.ts +103 -0
  203. package/src/host/browser.ts +82 -4612
  204. package/src/host/devtools/console.ts +102 -0
  205. package/src/host/devtools/faces.ts +160 -0
  206. package/src/host/devtools/log.ts +34 -0
  207. package/src/host/devtools/machines.ts +208 -0
  208. package/src/host/devtools/read.ts +58 -0
  209. package/src/host/devtools/state.ts +148 -0
  210. package/src/host/devtools.ts +39 -442
  211. package/src/host/fonts.ts +4 -3
  212. package/src/host/icons.ts +4 -0
  213. package/src/host/index.ts +107 -4
  214. package/src/host/locale.ts +71 -5
  215. package/src/host/markdown.ts +9 -0
  216. package/src/host/modules.ts +117 -21
  217. package/src/host/motion.ts +204 -0
  218. package/src/host/net.ts +4 -2
  219. package/src/host/palette.ts +139 -0
  220. package/src/host/scheduler.ts +5 -0
  221. package/src/host/share.ts +168 -0
  222. package/src/host/theme/classic.ts +121 -0
  223. package/src/host/theme/cupertino.ts +66 -0
  224. package/src/host/theme/data.ts +110 -0
  225. package/src/host/theme/devtools.ts +167 -0
  226. package/src/host/theme/dialogs.ts +64 -0
  227. package/src/host/theme/fluent.ts +65 -0
  228. package/src/host/theme/forms.ts +240 -0
  229. package/src/host/theme/fusion.ts +67 -0
  230. package/src/host/theme/layout.ts +167 -0
  231. package/src/host/theme/media.ts +107 -0
  232. package/src/host/theme/navigation.ts +37 -0
  233. package/src/host/theme/panes.ts +100 -0
  234. package/src/host/theme/popups.ts +150 -0
  235. package/src/host/theme/responsive.ts +104 -0
  236. package/src/host/theme/toasts.ts +63 -0
  237. package/src/host/theme.ts +19 -1384
  238. package/src/ui/dialect/events.trb +276 -0
  239. package/src/ui/dialect/faces.trb +135 -0
  240. package/src/ui/dialect/layout.trb +283 -0
  241. package/src/ui/dialect/palette.trb +87 -0
  242. package/src/ui/dialect/problems.trb +174 -0
  243. package/src/ui/dialect/reactions.trb +198 -0
  244. package/src/ui/dialect/rows.trb +208 -0
  245. package/src/ui/dialect/screens.trb +293 -0
  246. package/src/ui/dialect/steps.trb +203 -0
  247. package/src/ui/dialect/validation.trb +137 -0
  248. package/src/ui/dialect/view.trb +110 -0
  249. package/src/ui/dialect/walking.trb +98 -0
  250. package/src/ui/dialect/words.trb +70 -0
  251. package/src/ui/i18n.trb +17 -0
  252. package/src/ui/test.trb +3 -556
  253. package/src/ui/testing/actions.trb +421 -0
  254. package/src/ui/testing/faces.trb +148 -0
  255. package/src/ui/testing/server.trb +73 -0
  256. package/src/ui/ui.trb +25 -1675
@@ -0,0 +1,102 @@
1
+ // --- console -------------------------------------------------------------------------
2
+
3
+ import { K, load, LoadError, bindSeries, scriptStart, form, intern, type ObjectCell } from '../../core/index.ts';
4
+ import { spawn, taskResult } from '../scheduler.ts';
5
+ import { report } from '../index.ts';
6
+ import { HISTORY_KEY, MAX_HISTORY, MAX_LOG, side, consoleOut, setConsoleOut, consoleIn, setConsoleIn, consoleFace, setConsoleFace, picked, history, setHistory, back, setBack, running, setRunning, el, write } from './state.ts';
7
+ import { moldShort } from './read.ts';
8
+
9
+ // Load errors that mean "the input is not finished yet" (as in the trebol console).
10
+ export const UNFINISHED = /: missing ([\])]|} to close (string|binary))$/;
11
+
12
+ export function buildConsole(pane: HTMLElement): void {
13
+ setConsoleOut(el('div', 'tb-dev-out'));
14
+ consoleOut.setAttribute('role', 'log');
15
+ consoleOut.setAttribute('aria-label', 'Console output');
16
+ setConsoleFace(el('div', 'tb-dev-face'));
17
+ const row = el('div', 'tb-dev-prompt');
18
+ setConsoleIn(el('textarea', 'tb-dev-in'));
19
+ consoleIn.rows = 1;
20
+ consoleIn.spellcheck = false;
21
+ consoleIn.setAttribute('aria-label', 'Trebol code to run');
22
+ consoleIn.placeholder = 'face/text: "Hello"';
23
+ consoleIn.addEventListener('keydown', consoleKey);
24
+ consoleIn.addEventListener('input', fitInput);
25
+ row.append(el('span', 'tb-dev-sign', '>>'), consoleIn);
26
+ row.addEventListener('click', () => consoleIn.focus());
27
+ pane.append(consoleOut, consoleFace, row);
28
+ }
29
+
30
+ export function fitInput(): void {
31
+ consoleIn.style.height = 'auto';
32
+ consoleIn.style.height = `${Math.min(consoleIn.scrollHeight, 160)}px`;
33
+ }
34
+
35
+ export function setInput(src: string): void {
36
+ consoleIn.value = src;
37
+ fitInput();
38
+ consoleIn.setSelectionRange(src.length, src.length);
39
+ }
40
+
41
+ export function consoleKey(ev: KeyboardEvent): void {
42
+ const src = consoleIn.value;
43
+ if (ev.key === 'Enter' && !ev.shiftKey) {
44
+ if (src.trim() === '' || unfinished(src)) return;
45
+ ev.preventDefault();
46
+ setInput('');
47
+ setRunning(running.then(() => run(src)));
48
+ } else if (ev.key === 'ArrowUp' && !src.slice(0, consoleIn.selectionStart).includes('\n') && back < history.length) {
49
+ ev.preventDefault();
50
+ setBack(back + 1);
51
+ setInput(history[history.length - back]!);
52
+ } else if (ev.key === 'ArrowDown' && !src.slice(consoleIn.selectionEnd).includes('\n') && back > 0) {
53
+ ev.preventDefault();
54
+ setBack(back - 1);
55
+ setInput(back === 0 ? '' : history[history.length - back]!);
56
+ }
57
+ }
58
+
59
+ export function unfinished(src: string): boolean {
60
+ try {
61
+ load(src);
62
+ return false;
63
+ } catch (e) {
64
+ return e instanceof LoadError && UNFINISHED.test(e.message);
65
+ }
66
+ }
67
+
68
+ export function say(kind: 'in' | 'out' | 'value' | 'error', text: string): void {
69
+ const line = el('pre', `tb-dev-said-${kind}`, text);
70
+ const end = consoleOut.scrollTop + consoleOut.clientHeight >= consoleOut.scrollHeight - 4;
71
+ consoleOut.append(line);
72
+ while (consoleOut.childElementCount > MAX_LOG) consoleOut.firstElementChild!.remove();
73
+ if (end) consoleOut.scrollTop = consoleOut.scrollHeight;
74
+ }
75
+
76
+ /** Runs code typed in the console in the app's user context, `face` being the face picked. */
77
+ export async function run(src: string): Promise<void> {
78
+ setBack(0);
79
+ if (history[history.length - 1] !== src) history.push(src);
80
+ setHistory(history.slice(-MAX_HISTORY));
81
+ write(() => localStorage, HISTORY_KEY, { lines: history });
82
+ say('in', src.split('\n').map((l, i) => (i === 0 ? '>> ' : ' ') + l).join('\n'));
83
+ consoleOut.scrollTop = consoleOut.scrollHeight;
84
+ let code;
85
+ try {
86
+ code = load(src);
87
+ } catch (e) {
88
+ say('error', `** Syntax error: ${(e as Error).message}`);
89
+ return;
90
+ }
91
+ const rt = side.host.rt;
92
+ bindSeries(code.s, rt.user, true);
93
+ if (picked !== null) bindSeries(code.s, { keys: [intern('face')], vals: [picked], parent: null });
94
+ try {
95
+ const v = await taskResult(spawn(side.host.sch, code.s, scriptStart(code).start));
96
+ if (v.t === K.Error) say('error', form(v));
97
+ else if (v.t !== K.Unset) say('value', `== ${moldShort(v)}`);
98
+ } catch (e) {
99
+ say('error', report(e as ObjectCell));
100
+ }
101
+ }
102
+
@@ -0,0 +1,160 @@
1
+ // --- faces ---------------------------------------------------------------------------
2
+
3
+ import { K, form, type ObjectCell, type BlockCell, type Ctx } from '../../core/index.ts';
4
+ import { side, state, panel, opener, outline, pickButton, tree, setTree, detail, setDetail, picked, setPicked, pointed, setPointed, picking, storePicking, el, button } from './state.ts';
5
+ import { get, faceRef, on, moldShort, placeOf } from './read.ts';
6
+ import { inDevtools, openTab, refresh } from '../devtools.ts';
7
+
8
+ export const faceKeys = new WeakMap<Ctx, number>();
9
+ export let nextKey = 1;
10
+
11
+ export function faceKey(face: ObjectCell): number {
12
+ let k = faceKeys.get(face.ctx);
13
+ if (k === undefined) faceKeys.set(face.ctx, k = nextKey++);
14
+ return k;
15
+ }
16
+
17
+ export function buildFaces(pane: HTMLElement): void {
18
+ setTree(el('div', 'tb-dev-tree'));
19
+ tree.setAttribute('aria-label', 'Faces on the page');
20
+ tree.addEventListener('pointerleave', () => point(null));
21
+ setDetail(el('div', 'tb-dev-detail'));
22
+ pane.append(tree, detail);
23
+ }
24
+
25
+ /** The faces on the page, in the order of their nodes, with how deep each is. */
26
+ export function facesOnPage(): { face: ObjectCell; el: Element; depth: number }[] {
27
+ const out: { face: ObjectCell; el: Element; depth: number }[] = [];
28
+ const walk = (e: Element, depth: number): void => {
29
+ for (const c of Array.from(e.children)) {
30
+ if (c === panel || c === opener || c === outline) continue;
31
+ const f = side.faceOf(c);
32
+ if (f !== null) out.push({ face: f, el: c, depth });
33
+ walk(c, f !== null ? depth + 1 : depth);
34
+ }
35
+ };
36
+ walk(document.body, 0);
37
+ return out;
38
+ }
39
+
40
+ export function drawTree(): void {
41
+ const top = tree.scrollTop;
42
+ tree.replaceChildren();
43
+ for (const { face, el: node, depth } of facesOnPage()) {
44
+ const row = button('tb-dev-node', faceRef(face, side.host.rt), () => pick(face));
45
+ row.style.paddingLeft = `${8 + depth * 14}px`;
46
+ if (face.ctx === picked?.ctx) row.setAttribute('aria-current', 'true');
47
+ if ((node as HTMLElement).offsetParent === null && getComputedStyle(node).position !== 'fixed') row.classList.add('tb-dev-unseen');
48
+ row.addEventListener('pointerenter', () => point(face));
49
+ tree.append(row);
50
+ }
51
+ tree.scrollTop = top;
52
+ }
53
+
54
+ export function drawDetail(): void {
55
+ detail.replaceChildren();
56
+ if (picked === null) {
57
+ detail.append(el('p', 'tb-dev-hint', 'Pick a face on the page, or choose one in the tree.'));
58
+ return;
59
+ }
60
+ const f = picked;
61
+ const title = el('h3', undefined, faceRef(f, side.host.rt));
62
+ detail.append(title);
63
+ const where = placeOf(f);
64
+ if (where !== null) {
65
+ const p = el('p', 'tb-dev-place');
66
+ if (where.href !== null) {
67
+ const a = el('a', undefined, where.text);
68
+ a.href = where.href;
69
+ a.title = 'Open in VS Code';
70
+ p.append(a);
71
+ } else p.textContent = where.text;
72
+ detail.append(p);
73
+ }
74
+ if (side.nodeOf(f) === null) detail.append(el('p', 'tb-dev-hint', 'Not on the page any more.'));
75
+ const dl = el('dl');
76
+ const row = (k: string, v: string): void => {
77
+ dl.append(el('dt', undefined, k), el('dd', undefined, v));
78
+ };
79
+ for (const n of ['style', 'name', 'text', 'value', 'error', 'rules', 'hint', 'role']) {
80
+ const v = get(f, n);
81
+ if (v.t !== K.None) row(n, moldShort(v, 400));
82
+ }
83
+ const events: string[] = [];
84
+ if (get(f, 'action').t === K.Block) events.push('action');
85
+ const ev = get(f, 'events');
86
+ if (ev.t === K.Block) {
87
+ for (const c of (ev as BlockCell).s.data.slice(ev.i)) if (c.t !== K.Block) events.push(form(c));
88
+ }
89
+ if (events.length > 0) row('events', events.join(' '));
90
+ const flags = ['required', 'disabled', 'hidden', 'readonly'].filter(n => on(f, n));
91
+ if (flags.length > 0) row('flags', flags.join(' '));
92
+ const data = get(f, 'data');
93
+ if (data.t === K.Block) row('rows', String((data as BlockCell).s.data.length - data.i));
94
+ detail.append(dl);
95
+ }
96
+
97
+ export function pick(face: ObjectCell | null): void {
98
+ setPicked(face);
99
+ point(null);
100
+ refresh();
101
+ }
102
+
103
+ /** Draws the outline around a face, or hides it. */
104
+ export function point(face: ObjectCell | null): void {
105
+ setPointed(face);
106
+ placeOutline();
107
+ }
108
+
109
+ export function placeOutline(): void {
110
+ const face = pointed ?? (state.open && state.tab === 'faces' ? picked : null);
111
+ const node = face !== null ? side.nodeOf(face) : null;
112
+ const r = node?.getBoundingClientRect();
113
+ if (r === undefined || (r.width === 0 && r.height === 0)) {
114
+ if (outline.matches(':popover-open')) outline.hidePopover();
115
+ return;
116
+ }
117
+ Object.assign(outline.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${r.width}px`, height: `${r.height}px` });
118
+ outline.classList.toggle('tb-dev-pointed', pointed !== null);
119
+ // shown again so it stays above a dialog opened since
120
+ if (outline.matches(':popover-open')) outline.hidePopover();
121
+ outline.showPopover();
122
+ }
123
+
124
+ export function setPicking(on: boolean): void {
125
+ storePicking(on);
126
+ pickButton.setAttribute('aria-pressed', String(on));
127
+ document.documentElement.classList.toggle('tb-dev-picking', on);
128
+ if (on) openTab('faces');
129
+ else point(null);
130
+ }
131
+
132
+ export function pickTarget(ev: Event): { face: ObjectCell; el: HTMLElement } | null {
133
+ const t = ev.target;
134
+ if (!(t instanceof Element) || inDevtools(t) || t === opener) return null;
135
+ return side.faceAt(t);
136
+ }
137
+
138
+ export function pickMove(ev: PointerEvent): void {
139
+ if (!picking) return;
140
+ const at = pickTarget(ev);
141
+ if (at !== null) point(at.face);
142
+ }
143
+
144
+ export function pickDown(ev: PointerEvent): void {
145
+ if (!picking || pickTarget(ev) === null) return;
146
+ // the face does not get the press: no focus, no drag
147
+ ev.preventDefault();
148
+ ev.stopImmediatePropagation();
149
+ }
150
+
151
+ export function pickClick(ev: MouseEvent): void {
152
+ if (!picking) return;
153
+ const at = pickTarget(ev);
154
+ if (at === null) return;
155
+ ev.preventDefault();
156
+ ev.stopImmediatePropagation();
157
+ setPicking(false);
158
+ pick(at.face);
159
+ }
160
+
@@ -0,0 +1,34 @@
1
+ // --- log -----------------------------------------------------------------------------
2
+
3
+ import { MAX_LOG, logList, setLogList, lastLog, setLastLog, el, button } from './state.ts';
4
+
5
+ export function buildLog(pane: HTMLElement): void {
6
+ const bar = el('div', 'tb-dev-bar');
7
+ bar.append(button('tb-dev-button', 'Clear', () => {
8
+ logList.replaceChildren();
9
+ setLastLog(null);
10
+ }));
11
+ setLogList(el('ol', 'tb-dev-entries'));
12
+ logList.setAttribute('aria-label', 'What the app did');
13
+ pane.append(bar, logList);
14
+ }
15
+
16
+ /** Adds a line to the log; the same one again (typing in a field) counts instead of adding. */
17
+ export function log(kind: string, text: string, key?: string): void {
18
+ if (key !== undefined && lastLog?.key === key && lastLog.li.isConnected) {
19
+ lastLog.count++;
20
+ lastLog.text.textContent = `${text} (×${lastLog.count})`;
21
+ return;
22
+ }
23
+ const li = el('li', `tb-dev-${kind}`);
24
+ const now = new Date();
25
+ const time = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
26
+ const t = el('span', 'tb-dev-text', text);
27
+ li.append(el('time', undefined, time), el('span', 'tb-dev-kind', kind), t);
28
+ const end = logList.scrollTop + logList.clientHeight >= logList.scrollHeight - 4;
29
+ logList.append(li);
30
+ while (logList.childElementCount > MAX_LOG) logList.firstElementChild!.remove();
31
+ if (end) logList.scrollTop = logList.scrollHeight;
32
+ setLastLog(key !== undefined ? { key, li, text: t, count: 1 } : null);
33
+ }
34
+
@@ -0,0 +1,208 @@
1
+ // --- machines ------------------------------------------------------------------------
2
+
3
+ import { K, NONE, mold, form, ctxFind, intern, spell, type Cell, type ObjectCell, type WordCell, type BlockCell, type Ctx } from '../../core/index.ts';
4
+ import { spawn, taskResult } from '../scheduler.ts';
5
+ import { side, el, button } from './state.ts';
6
+ import { wordText, short, moldShort } from './read.ts';
7
+ import { log } from './log.ts';
8
+
9
+ /** A machine the program holds: the word (or object/field) it is in. */
10
+ export interface Found { name: string; m: ObjectCell }
11
+
12
+ export let machineList: HTMLElement;
13
+ export let machineDetail: HTMLElement;
14
+ /** The machine chosen, by its context, and the state chosen in it (its key as mold gives it). */
15
+ export let chosen: { ctx: Ctx; state: string | null } | null = null;
16
+ /** What each machine's state was at the last repaint, for the log. */
17
+ export const seenState = new WeakMap<Ctx, string>();
18
+ export let drawing = 0;
19
+
20
+ export function buildMachines(pane: HTMLElement): void {
21
+ machineList = el('div', 'tb-dev-tree');
22
+ machineList.setAttribute('aria-label', 'Machines and their states');
23
+ machineDetail = el('div', 'tb-dev-detail');
24
+ pane.append(machineList, machineDetail);
25
+ }
26
+
27
+ export function field(o: ObjectCell, name: string): Cell | null {
28
+ const i = ctxFind(o.ctx, intern(name));
29
+ return i < 0 ? null : o.ctx.vals[i]!;
30
+ }
31
+
32
+ export function isMachine(v: Cell): v is ObjectCell {
33
+ return v.t === K.Object && field(v as ObjectCell, 'nodes') !== null && field(v as ObjectCell, 'queue') !== null
34
+ && field(v as ObjectCell, 'scope') !== null && field(v as ObjectCell, 'state') !== null;
35
+ }
36
+
37
+ /** The machines in the words of the program, and in the fields of the objects in them; no others. */
38
+ export function findMachines(): Found[] {
39
+ const out: Found[] = [];
40
+ const seen = new Set<Ctx>();
41
+ const user = side.host.rt.user;
42
+ const add = (name: string, v: Cell): void => {
43
+ if (!isMachine(v) || seen.has(v.ctx)) return;
44
+ seen.add(v.ctx);
45
+ out.push({ name, m: v });
46
+ };
47
+ user.keys.forEach((k, i) => {
48
+ const v = user.vals[i]!;
49
+ add(spell(k), v);
50
+ if (v.t === K.Object && !isMachine(v) && field(v as ObjectCell, 'style') === null) {
51
+ const o = v as ObjectCell;
52
+ o.ctx.keys.forEach((f, j) => add(`${spell(k)}/${spell(f)}`, o.ctx.vals[j]!));
53
+ }
54
+ });
55
+ return out;
56
+ }
57
+
58
+ /** A line in the log for each machine whose state changed since the last repaint. */
59
+ export function noteMachines(): void {
60
+ for (const { name, m } of findMachines()) {
61
+ const now = mold(field(m, 'state') ?? NONE);
62
+ const was = seenState.get(m.ctx);
63
+ seenState.set(m.ctx, now);
64
+ if (was !== undefined && was !== now) log('machine', `${name}: ${was} → ${now}`);
65
+ }
66
+ }
67
+
68
+ /** The value after a word in a block of plain data, as graph-of gives it. */
69
+ export function pickKey(b: Cell | null, key: string): Cell | null {
70
+ if (b === null || b.t !== K.Block) return null;
71
+ const d = (b as BlockCell).s.data.slice((b as BlockCell).i);
72
+ for (let i = 0; i + 1 < d.length; i += 2) if (wordText(d[i]!) === key) return d[i + 1]!;
73
+ return null;
74
+ }
75
+
76
+ export function pairs(b: Cell | null): [Cell, Cell][] {
77
+ if (b === null || b.t !== K.Block) return [];
78
+ const d = (b as BlockCell).s.data.slice((b as BlockCell).i);
79
+ const out: [Cell, Cell][] = [];
80
+ for (let i = 0; i + 1 < d.length; i += 2) out.push([d[i]!, d[i + 1]!]);
81
+ return out;
82
+ }
83
+
84
+ /** The values of a block or a path. */
85
+ export function items(b: Cell | null): Cell[] {
86
+ return b === null || (b.t !== K.Block && b.t !== K.Path) ? [] : (b as BlockCell).s.data.slice((b as BlockCell).i);
87
+ }
88
+
89
+ export function truthy(v: Cell | null): boolean {
90
+ return v !== null && !(v.t === K.None || (v.t === K.Logic && !v.v));
91
+ }
92
+
93
+ export async function graphOf(m: ObjectCell): Promise<Cell> {
94
+ const fn: WordCell = { t: K.Word, sym: intern('graph-of'), bind: side.host.rt.lib };
95
+ return taskResult(spawn(side.host.sch, { data: [fn, m] } as unknown as BlockCell['s'], 0));
96
+ }
97
+
98
+ /** A way as one line: `on start → work`, with where and set when it has them. */
99
+ export function wayText(w: Cell): string {
100
+ const on = pickKey(w, 'on');
101
+ const ev = on !== null && on.t === K.Block ? items(on).map(form).join(' ') : null;
102
+ const to = pickKey(w, 'to');
103
+ const parts: string[] = [];
104
+ if (ev !== null) parts.push(`on ${ev}`);
105
+ if (to !== null) parts.push(`→ ${mold(to)}`);
106
+ else {
107
+ const branches = items(pickKey(w, 'branches')).map(b => `${wordText(items(b)[0]!) === 'else' ? 'else ' : ''}${mold(pickKey(b, 'to') ?? NONE)}`);
108
+ parts.push(`→ ${wordText(items(w)[2]!) ?? 'choose'} [${branches.join(' | ')}]`);
109
+ }
110
+ const where = pickKey(w, 'where');
111
+ if (truthy(where)) parts.push(`where ${short(mold(where!), 40)}`);
112
+ const set = pickKey(w, 'set');
113
+ if (truthy(set)) parts.push(`set ${short(mold(set!), 40)}`);
114
+ if (ev === null) parts.unshift('at once');
115
+ return parts.join(' ');
116
+ }
117
+
118
+ export async function drawMachines(): Promise<void> {
119
+ const ticket = ++drawing;
120
+ const found = findMachines();
121
+ const graphs: Cell[] = [];
122
+ for (const f of found) {
123
+ try {
124
+ graphs.push(await graphOf(f.m));
125
+ } catch {
126
+ graphs.push(NONE);
127
+ }
128
+ }
129
+ if (ticket !== drawing) return;
130
+ const top = machineList.scrollTop;
131
+ machineList.replaceChildren();
132
+ machineDetail.replaceChildren();
133
+ if (found.length === 0) {
134
+ machineList.append(el('p', 'tb-dev-hint', 'No machine in the words of the program.'));
135
+ return;
136
+ }
137
+ if (chosen === null || !found.some(f => f.m.ctx === chosen!.ctx)) chosen = { ctx: found[0]!.m.ctx, state: null };
138
+ found.forEach((f, i) => {
139
+ const g = graphs[i]!;
140
+ const head = button('tb-dev-node tb-dev-machine-head', `${f.name} ${mold(field(f.m, 'state') ?? NONE)}`, () => {
141
+ chosen = { ctx: f.m.ctx, state: null };
142
+ void drawMachines();
143
+ });
144
+ if (chosen!.ctx === f.m.ctx && chosen!.state === null) head.setAttribute('aria-current', 'true');
145
+ machineList.append(head);
146
+ for (const [key, info] of pairs(pickKey(g, 'states'))) {
147
+ const k = mold(key);
148
+ const depth = key.t === K.Path ? items(key).length : 1;
149
+ const name = key.t === K.Path ? form(items(key).at(-1)!) : form(key);
150
+ const kind = wordText(pickKey(info, 'kind') ?? NONE);
151
+ const marks = [
152
+ truthy(pickKey(info, 'initial')) ? '›' : ' ',
153
+ kind === 'final' ? '(final)' : kind === 'parallel' ? '(parallel)' : '',
154
+ ];
155
+ const row = button('tb-dev-node', `${marks[0]} ${name} ${marks[1]}`.trimEnd(), () => {
156
+ chosen = { ctx: f.m.ctx, state: k };
157
+ void drawMachines();
158
+ });
159
+ row.style.paddingLeft = `${8 + depth * 14}px`;
160
+ if (truthy(pickKey(info, 'on'))) row.classList.add('tb-dev-on');
161
+ if (chosen!.ctx === f.m.ctx && chosen!.state === k) row.setAttribute('aria-current', 'true');
162
+ machineList.append(row);
163
+ }
164
+ });
165
+ machineList.scrollTop = top;
166
+ const at = found.findIndex(f => f.m.ctx === chosen!.ctx);
167
+ drawMachine(found[at]!, graphs[at]!);
168
+ }
169
+
170
+ export function drawMachine(f: Found, g: Cell): void {
171
+ const d = machineDetail;
172
+ d.append(el('h3', undefined, chosen!.state === null ? f.name : `${f.name} · ${chosen!.state}`));
173
+ const dl = el('dl');
174
+ const row = (k: string, v: string): void => {
175
+ dl.append(el('dt', undefined, k), el('dd', undefined, v));
176
+ };
177
+ const states = pairs(pickKey(g, 'states'));
178
+ const on = new Set(states.filter(([, info]) => truthy(pickKey(info, 'on'))).map(([k]) => mold(k)));
179
+ const ways = items(pickKey(g, 'ways'));
180
+ row('state', mold(field(f.m, 'state') ?? NONE));
181
+ const data = field(f.m, 'data');
182
+ if (data !== null && data.t === K.Object) {
183
+ const o = data as ObjectCell;
184
+ row('data', o.ctx.keys.map((k, i) => `${spell(k)}: ${moldShort(o.ctx.vals[i]!, 200)}`).join('\n') || '(none)');
185
+ }
186
+ // what can? says yes to: the events of the ways out of the states it is in
187
+ const now = new Set<string>();
188
+ for (const w of ways) {
189
+ const from = pickKey(w, 'from');
190
+ if (from !== null && on.has(mold(from))) for (const e of items(pickKey(w, 'on'))) now.add(form(e));
191
+ }
192
+ row('takes now', now.size > 0 ? [...now].join(' ') : '(nothing)');
193
+ if (truthy(field(f.m, 'done'))) row('done', mold(field(f.m, 'outcome') ?? NONE));
194
+ if (chosen!.state !== null) {
195
+ const info = states.find(([k]) => mold(k) === chosen!.state)?.[1] ?? null;
196
+ for (const n of ['kind', 'timeout', 'outcome', 'invoke', 'for', 'defer']) {
197
+ const v = pickKey(info, n);
198
+ if (v !== null && truthy(v) && !(v.t === K.Block && items(v).length === 0)) row(n, n === 'invoke' && v.t === K.Block ? 'a machine of its own' : moldShort(v, 200));
199
+ }
200
+ if (truthy(pickKey(info, 'run'))) row('run', moldShort(pickKey(info, 'run')!, 200));
201
+ }
202
+ const mine = ways.filter(w => chosen!.state === null || mold(pickKey(w, 'from') ?? NONE) === chosen!.state);
203
+ if (mine.length > 0) {
204
+ row('ways', mine.map(w => (chosen!.state === null ? `${mold(pickKey(w, 'from') ?? NONE)} ` : '') + wayText(w)).join('\n'));
205
+ }
206
+ d.append(dl);
207
+ }
208
+
@@ -0,0 +1,58 @@
1
+ // --- reading faces -----------------------------------------------------------------
2
+
3
+ import { K, NONE, mold, form, ctxFind, intern, spell, type Cell, type ObjectCell, type WordCell, type Runtime } from '../../core/index.ts';
4
+ import { translate } from '../index.ts';
5
+ import { MAX_MOLD, side } from './state.ts';
6
+
7
+ export function get(face: ObjectCell, name: string): Cell {
8
+ const i = ctxFind(face.ctx, intern(name));
9
+ return i < 0 ? NONE : face.ctx.vals[i]!;
10
+ }
11
+
12
+ export function wordText(v: Cell): string | null {
13
+ return v.t === K.Word || v.t === K.LitWord ? spell((v as WordCell).sym) : null;
14
+ }
15
+
16
+ export function short(s: string, most = 80): string {
17
+ const line = s.replace(/\s+/g, ' ');
18
+ return line.length > most ? line.slice(0, most - 1) + '…' : line;
19
+ }
20
+
21
+ /** How a face is named in the tree, the log and errors: its style, name and text (`field email "Email"`). */
22
+ export function faceRef(face: ObjectCell, rt: Runtime): string {
23
+ const style = wordText(get(face, 'style')) ?? 'face';
24
+ const name = wordText(get(face, 'name'));
25
+ const t = get(face, 'text');
26
+ const shown = t.t === K.None ? null : t.t === K.String || t.t === K.Map || t.t === K.Word ? translate(t, rt) : form(t);
27
+ return [style, name, shown !== null && shown !== '' ? `"${short(shown, 40)}"` : null].filter(x => x !== null).join(' ');
28
+ }
29
+
30
+ export function on(face: ObjectCell, name: string): boolean {
31
+ const v = get(face, name);
32
+ return !(v.t === K.None || (v.t === K.Logic && !v.v));
33
+ }
34
+
35
+ export function moldShort(v: Cell, most = MAX_MOLD): string {
36
+ const s = mold(v);
37
+ return s.length > most ? s.slice(0, most - 1) + '…' : s;
38
+ }
39
+
40
+ /** A face's file:line:col and a vscode:// link to it, when the file is in the folder trebol dev serves. */
41
+ export function placeOf(face: ObjectCell): { text: string; href: string | null } | null {
42
+ const v = get(face, 'place');
43
+ if (v.t !== K.String) return null;
44
+ const at = form(v);
45
+ const m = /^(.*):(\d+):(\d+)$/.exec(at);
46
+ if (m === null) return { text: at, href: null };
47
+ let url: URL;
48
+ try {
49
+ url = new URL(m[1]!);
50
+ } catch {
51
+ return { text: at, href: null };
52
+ }
53
+ const path = decodeURIComponent(url.pathname);
54
+ const text = `${path.slice(1)}:${m[2]}:${m[3]}`;
55
+ const mine = url.origin === location.origin && !path.startsWith('/@trebol/') && side.root !== null;
56
+ return { text, href: mine ? `vscode://file${side.root!.replace(/\/$/, '')}${path}:${m[2]}:${m[3]}` : null };
57
+ }
58
+