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
@@ -7,161 +7,37 @@
7
7
  // to the editor (vscode://).
8
8
  // Console >> and == as in the trebol console, against the running app;
9
9
  // `face` is the face picked; print shows here too.
10
- // Log events, requests, toasts, errors and warnings, newest last.
10
+ // Machines the machines the program holds in its words: their states as a
11
+ // tree, the ones they are in marked, and of the one chosen its
12
+ // data, the ways out of its states and the events it takes now.
13
+ // Log events, requests, toasts, errors and warnings, and each change
14
+ // of state of a machine, newest last.
11
15
 
12
- import {
13
- K, NONE, load, LoadError, bindSeries, scriptStart, mold, form, ctxFind, intern, spell,
14
- type Cell, type ObjectCell, type WordCell, type BlockCell, type Ctx, type Runtime,
15
- } from '../core/index.ts';
16
- import { spawn, taskResult } from './scheduler.ts';
17
- import { output, report, translate, type Host } from './index.ts';
18
- import { devLog } from './devlog.ts';
19
-
20
- /** What the dev tools need from the browser host. */
21
- export interface DevSide {
22
- host: Host;
23
- /** The folder trebol dev serves, to link a face to its file; null if unknown. */
24
- root: string | null;
25
- /** The face whose own node this is, if any. */
26
- faceOf(e: Element): ObjectCell | null;
27
- /** The face of a node or of the nearest node around it that is one. */
28
- faceAt(e: Element): { face: ObjectCell; el: HTMLElement } | null;
29
- /** The node of a face on the page, if it is on the page. */
30
- nodeOf(face: ObjectCell): HTMLElement | null;
31
- }
32
-
33
- type Tab = 'faces' | 'console' | 'log';
34
-
35
- /** The panel's state, kept across the reloads of live reload. */
36
- interface State { open: boolean; tab: Tab; height: number }
37
-
38
- const STATE_KEY = 'trebol-dev';
39
- const HISTORY_KEY = 'trebol-dev-history';
40
- const MAX_HISTORY = 100;
41
- const MAX_LOG = 500;
42
- const MAX_MOLD = 2000;
43
-
44
- let side: DevSide;
45
- let state: State = { open: false, tab: 'faces', height: 320 };
46
- let panel: HTMLElement;
47
- let opener: HTMLButtonElement;
48
- let outline: HTMLElement;
49
- let pickButton: HTMLButtonElement;
50
- let tree: HTMLElement;
51
- let detail: HTMLElement;
52
- let consoleOut: HTMLElement;
53
- let consoleIn: HTMLTextAreaElement;
54
- let consoleFace: HTMLElement;
55
- let logList: HTMLElement;
56
- const tabs = new Map<Tab, { button: HTMLButtonElement; pane: HTMLElement }>();
57
-
58
- /** The face chosen in Faces, `face` in Console. */
59
- let picked: ObjectCell | null = null;
60
- /** The face under the pointer or the tree row the pointer is on. */
61
- let pointed: ObjectCell | null = null;
62
- let picking = false;
63
- let history: string[] = [];
64
- let back = 0;
65
- let lastLog: { key: string; li: HTMLElement; text: HTMLElement; count: number } | null = null;
66
- let treeFrame = 0;
67
- /** The code typed so far runs one piece after another. */
68
- let running: Promise<void> = Promise.resolve();
69
-
70
- function el<T extends keyof HTMLElementTagNameMap>(tag: T, cls?: string, content?: string): HTMLElementTagNameMap[T] {
71
- const e = document.createElement(tag);
72
- if (cls) e.className = cls;
73
- if (content !== undefined) e.textContent = content;
74
- return e;
75
- }
76
-
77
- function button(cls: string, content: string, act: () => void): HTMLButtonElement {
78
- const b = el('button', cls, content);
79
- b.type = 'button';
80
- b.addEventListener('click', act);
81
- return b;
82
- }
83
-
84
- function read<T>(store: () => Storage, key: string, fallback: T): T {
85
- try {
86
- const v = store().getItem(key);
87
- return v === null ? fallback : { ...fallback, ...(JSON.parse(v) as T) };
88
- } catch {
89
- return fallback;
90
- }
91
- }
92
-
93
- function write(store: () => Storage, key: string, v: unknown): void {
94
- try {
95
- store().setItem(key, JSON.stringify(v));
96
- } catch {
97
- // a private window or blocked storage: the panel forgets, nothing else
98
- }
99
- }
100
-
101
- // --- reading faces -----------------------------------------------------------------
102
-
103
- function get(face: ObjectCell, name: string): Cell {
104
- const i = ctxFind(face.ctx, intern(name));
105
- return i < 0 ? NONE : face.ctx.vals[i]!;
106
- }
107
-
108
- function wordText(v: Cell): string | null {
109
- return v.t === K.Word || v.t === K.LitWord ? spell((v as WordCell).sym) : null;
110
- }
111
-
112
- function short(s: string, most = 80): string {
113
- const line = s.replace(/\s+/g, ' ');
114
- return line.length > most ? line.slice(0, most - 1) + '…' : line;
115
- }
116
-
117
- /** How a face is named in the tree, the log and errors: its style, name and text (`field email "Email"`). */
118
- export function faceRef(face: ObjectCell, rt: Runtime): string {
119
- const style = wordText(get(face, 'style')) ?? 'face';
120
- const name = wordText(get(face, 'name'));
121
- const t = get(face, 'text');
122
- const shown = t.t === K.None ? null : t.t === K.String || t.t === K.Map || t.t === K.Word ? translate(t, rt) : form(t);
123
- return [style, name, shown !== null && shown !== '' ? `"${short(shown, 40)}"` : null].filter(x => x !== null).join(' ');
124
- }
125
-
126
- function on(face: ObjectCell, name: string): boolean {
127
- const v = get(face, name);
128
- return !(v.t === K.None || (v.t === K.Logic && !v.v));
129
- }
130
-
131
- function moldShort(v: Cell, most = MAX_MOLD): string {
132
- const s = mold(v);
133
- return s.length > most ? s.slice(0, most - 1) + '…' : s;
134
- }
16
+ //
17
+ // This file is the panel; the tabs and what they share are in devtools/.
135
18
 
136
- /** A face's file:line:col and a vscode:// link to it, when the file is in the folder trebol dev serves. */
137
- function placeOf(face: ObjectCell): { text: string; href: string | null } | null {
138
- const v = get(face, 'place');
139
- if (v.t !== K.String) return null;
140
- const at = form(v);
141
- const m = /^(.*):(\d+):(\d+)$/.exec(at);
142
- if (m === null) return { text: at, href: null };
143
- let url: URL;
144
- try {
145
- url = new URL(m[1]!);
146
- } catch {
147
- return { text: at, href: null };
148
- }
149
- const path = decodeURIComponent(url.pathname);
150
- const text = `${path.slice(1)}:${m[2]}:${m[3]}`;
151
- const mine = url.origin === location.origin && !path.startsWith('/@trebol/') && side.root !== null;
152
- return { text, href: mine ? `vscode://file${side.root!.replace(/\/$/, '')}${path}:${m[2]}:${m[3]}` : null };
153
- }
19
+ import { K, mold } from '../core/index.ts';
20
+ import { output } from './index.ts';
21
+ import { devLog } from './devlog.ts';
22
+ import { type DevSide, type Tab, STATE_KEY, HISTORY_KEY, side, setSide, state, setState, panel, setPanel, opener, setOpener, outline, setOutline, pickButton, setPickButton, consoleIn, consoleFace, logList, tabs, picked, picking, setHistory, treeFrame, setTreeFrame, el, button, read, write } from './devtools/state.ts';
23
+ import { wordText, short, faceRef } from './devtools/read.ts';
24
+ import { faceKey, buildFaces, drawTree, drawDetail, point, placeOutline, setPicking, pickMove, pickDown, pickClick } from './devtools/faces.ts';
25
+ import { buildMachines, noteMachines, drawMachines } from './devtools/machines.ts';
26
+ import { buildConsole, say } from './devtools/console.ts';
27
+ import { buildLog, log } from './devtools/log.ts';
28
+ export { faceRef } from './devtools/read.ts';
29
+ export type { DevSide } from './devtools/state.ts';
154
30
 
155
31
  // --- the panel -----------------------------------------------------------------------
156
32
 
157
33
  /** Starts the dev tools on a page served by trebol dev; not in a frame (the documentation's examples). */
158
34
  export function startDevtools(s: DevSide): void {
159
35
  if (window !== window.top) return;
160
- side = s;
161
- state = read(() => sessionStorage, STATE_KEY, state);
162
- history = read<{ lines: string[] }>(() => localStorage, HISTORY_KEY, { lines: [] }).lines;
36
+ setSide(s);
37
+ setState(read(() => sessionStorage, STATE_KEY, state));
38
+ setHistory(read<{ lines: string[] }>(() => localStorage, HISTORY_KEY, { lines: [] }).lines);
163
39
 
164
- panel = el('section', 'tb-dev-tools');
40
+ setPanel(el('section', 'tb-dev-tools'));
165
41
  panel.popover = 'manual';
166
42
  panel.setAttribute('aria-label', 'Trebol dev tools');
167
43
  const grip = el('div', 'tb-dev-grip');
@@ -171,7 +47,7 @@ export function startDevtools(s: DevSide): void {
171
47
  list.setAttribute('role', 'tablist');
172
48
  head.append(list);
173
49
  const body = el('div', 'tb-dev-body');
174
- for (const [tab, label] of [['faces', 'Faces'], ['console', 'Console'], ['log', 'Log']] as const) {
50
+ for (const [tab, label] of [['faces', 'Faces'], ['machines', 'Machines'], ['console', 'Console'], ['log', 'Log']] as const) {
175
51
  const b = button('tb-dev-tab', label, () => openTab(tab));
176
52
  b.setAttribute('role', 'tab');
177
53
  b.id = `tb-dev-tab-${tab}`;
@@ -182,7 +58,7 @@ export function startDevtools(s: DevSide): void {
182
58
  body.append(pane);
183
59
  tabs.set(tab, { button: b, pane });
184
60
  }
185
- pickButton = button('tb-dev-button', 'Pick', () => setPicking(!picking));
61
+ setPickButton(button('tb-dev-button', 'Pick', () => setPicking(!picking)));
186
62
  pickButton.title = 'Click a face on the page to see it here';
187
63
  pickButton.setAttribute('aria-pressed', 'false');
188
64
  const close = button('tb-dev-button', 'Close', () => setOpen(false));
@@ -195,13 +71,14 @@ export function startDevtools(s: DevSide): void {
195
71
  if (ev.key === 'Escape') ev.preventDefault();
196
72
  });
197
73
  buildFaces(tabs.get('faces')!.pane);
74
+ buildMachines(tabs.get('machines')!.pane);
198
75
  buildConsole(tabs.get('console')!.pane);
199
76
  buildLog(tabs.get('log')!.pane);
200
77
 
201
- opener = button('tb-dev-opener', 'Trebol', () => setOpen(true));
78
+ setOpener(button('tb-dev-opener', 'Trebol', () => setOpen(true)));
202
79
  opener.popover = 'manual';
203
80
  opener.title = 'Dev tools (Ctrl+`)';
204
- outline = el('div', 'tb-dev-outline');
81
+ setOutline(el('div', 'tb-dev-outline'));
205
82
  outline.popover = 'manual';
206
83
  outline.setAttribute('aria-hidden', 'true');
207
84
  document.body.append(panel, opener, outline);
@@ -240,7 +117,7 @@ export function inDevtools(target: EventTarget | null): boolean {
240
117
  * Puts the panel, its opener and the outline inside the dialog on show, or back in the body: while a
241
118
  * modal dialog is open everything outside it is inert, the panel too.
242
119
  */
243
- function home(): void {
120
+ export function home(): void {
244
121
  const modals = document.querySelectorAll('dialog:modal');
245
122
  const into = modals.length > 0 ? modals[modals.length - 1]! : document.body;
246
123
  if (panel.parentElement === into) return;
@@ -251,7 +128,7 @@ function home(): void {
251
128
  }
252
129
  }
253
130
 
254
- function setOpen(open: boolean): void {
131
+ export function setOpen(open: boolean): void {
255
132
  state.open = open;
256
133
  write(() => sessionStorage, STATE_KEY, state);
257
134
  home();
@@ -272,14 +149,14 @@ function setOpen(open: boolean): void {
272
149
  }
273
150
  }
274
151
 
275
- function setHeight(h: number): void {
152
+ export function setHeight(h: number): void {
276
153
  state.height = Math.round(Math.max(140, Math.min(h, innerHeight - 60)));
277
154
  panel.style.height = `${state.height}px`;
278
155
  // the end of the page stays reachable above the panel
279
156
  if (state.open) document.body.style.paddingBottom = `${state.height}px`;
280
157
  }
281
158
 
282
- function resizeFrom(ev: PointerEvent): void {
159
+ export function resizeFrom(ev: PointerEvent): void {
283
160
  ev.preventDefault();
284
161
  const grip = ev.currentTarget as HTMLElement;
285
162
  grip.setPointerCapture(ev.pointerId);
@@ -293,7 +170,7 @@ function resizeFrom(ev: PointerEvent): void {
293
170
  grip.addEventListener('pointerup', up);
294
171
  }
295
172
 
296
- function openTab(tab: Tab): void {
173
+ export function openTab(tab: Tab): void {
297
174
  state.tab = tab;
298
175
  write(() => sessionStorage, STATE_KEY, state);
299
176
  for (const [t, { button: b, pane }] of tabs) {
@@ -308,7 +185,7 @@ function openTab(tab: Tab): void {
308
185
  }
309
186
 
310
187
  /** What the host tells: events, requests, notes and repaints. */
311
- function hook(): void {
188
+ export function hook(): void {
312
189
  devLog.event = (fn, face, args) => {
313
190
  if (fn === 'ui-blur') return;
314
191
  let what = fn.replace(/^ui-/, '');
@@ -334,12 +211,13 @@ function hook(): void {
334
211
  devLog.note = (kind, text) => log(kind, text);
335
212
  devLog.repainted = () => {
336
213
  home();
214
+ noteMachines();
337
215
  if (!state.open) return;
338
216
  placeOutline();
339
- if (state.tab === 'faces' && treeFrame === 0) treeFrame = requestAnimationFrame(() => {
340
- treeFrame = 0;
217
+ if ((state.tab === 'faces' || state.tab === 'machines') && treeFrame === 0) setTreeFrame(requestAnimationFrame(() => {
218
+ setTreeFrame(0);
341
219
  refresh();
342
- });
220
+ }));
343
221
  };
344
222
  const line = output.line;
345
223
  output.line = text => {
@@ -348,294 +226,13 @@ function hook(): void {
348
226
  };
349
227
  }
350
228
 
351
- function refresh(): void {
229
+ export function refresh(): void {
352
230
  if (!state.open) return;
353
231
  if (state.tab === 'faces') {
354
232
  drawTree();
355
233
  drawDetail();
356
234
  }
235
+ if (state.tab === 'machines') void drawMachines();
357
236
  consoleFace.textContent = picked !== null ? `face: ${faceRef(picked, side.host.rt)}` : 'face: none (pick one in Faces)';
358
237
  }
359
238
 
360
- // --- faces ---------------------------------------------------------------------------
361
-
362
- const faceKeys = new WeakMap<Ctx, number>();
363
- let nextKey = 1;
364
-
365
- function faceKey(face: ObjectCell): number {
366
- let k = faceKeys.get(face.ctx);
367
- if (k === undefined) faceKeys.set(face.ctx, k = nextKey++);
368
- return k;
369
- }
370
-
371
- function buildFaces(pane: HTMLElement): void {
372
- tree = el('div', 'tb-dev-tree');
373
- tree.setAttribute('aria-label', 'Faces on the page');
374
- tree.addEventListener('pointerleave', () => point(null));
375
- detail = el('div', 'tb-dev-detail');
376
- pane.append(tree, detail);
377
- }
378
-
379
- /** The faces on the page, in the order of their nodes, with how deep each is. */
380
- function facesOnPage(): { face: ObjectCell; el: Element; depth: number }[] {
381
- const out: { face: ObjectCell; el: Element; depth: number }[] = [];
382
- const walk = (e: Element, depth: number): void => {
383
- for (const c of Array.from(e.children)) {
384
- if (c === panel || c === opener || c === outline) continue;
385
- const f = side.faceOf(c);
386
- if (f !== null) out.push({ face: f, el: c, depth });
387
- walk(c, f !== null ? depth + 1 : depth);
388
- }
389
- };
390
- walk(document.body, 0);
391
- return out;
392
- }
393
-
394
- function drawTree(): void {
395
- const top = tree.scrollTop;
396
- tree.replaceChildren();
397
- for (const { face, el: node, depth } of facesOnPage()) {
398
- const row = button('tb-dev-node', faceRef(face, side.host.rt), () => pick(face));
399
- row.style.paddingLeft = `${8 + depth * 14}px`;
400
- if (face.ctx === picked?.ctx) row.setAttribute('aria-current', 'true');
401
- if ((node as HTMLElement).offsetParent === null && getComputedStyle(node).position !== 'fixed') row.classList.add('tb-dev-unseen');
402
- row.addEventListener('pointerenter', () => point(face));
403
- tree.append(row);
404
- }
405
- tree.scrollTop = top;
406
- }
407
-
408
- function drawDetail(): void {
409
- detail.replaceChildren();
410
- if (picked === null) {
411
- detail.append(el('p', 'tb-dev-hint', 'Pick a face on the page, or choose one in the tree.'));
412
- return;
413
- }
414
- const f = picked;
415
- const title = el('h3', undefined, faceRef(f, side.host.rt));
416
- detail.append(title);
417
- const where = placeOf(f);
418
- if (where !== null) {
419
- const p = el('p', 'tb-dev-place');
420
- if (where.href !== null) {
421
- const a = el('a', undefined, where.text);
422
- a.href = where.href;
423
- a.title = 'Open in VS Code';
424
- p.append(a);
425
- } else p.textContent = where.text;
426
- detail.append(p);
427
- }
428
- if (side.nodeOf(f) === null) detail.append(el('p', 'tb-dev-hint', 'Not on the page any more.'));
429
- const dl = el('dl');
430
- const row = (k: string, v: string): void => {
431
- dl.append(el('dt', undefined, k), el('dd', undefined, v));
432
- };
433
- for (const n of ['style', 'name', 'text', 'value', 'error', 'rules', 'hint', 'role']) {
434
- const v = get(f, n);
435
- if (v.t !== K.None) row(n, moldShort(v, 400));
436
- }
437
- const events: string[] = [];
438
- if (get(f, 'action').t === K.Block) events.push('action');
439
- const ev = get(f, 'events');
440
- if (ev.t === K.Block) {
441
- for (const c of (ev as BlockCell).s.data.slice(ev.i)) if (c.t !== K.Block) events.push(form(c));
442
- }
443
- if (events.length > 0) row('events', events.join(' '));
444
- const flags = ['required', 'disabled', 'hidden', 'readonly'].filter(n => on(f, n));
445
- if (flags.length > 0) row('flags', flags.join(' '));
446
- const data = get(f, 'data');
447
- if (data.t === K.Block) row('rows', String((data as BlockCell).s.data.length - data.i));
448
- detail.append(dl);
449
- }
450
-
451
- function pick(face: ObjectCell | null): void {
452
- picked = face;
453
- point(null);
454
- refresh();
455
- }
456
-
457
- /** Draws the outline around a face, or hides it. */
458
- function point(face: ObjectCell | null): void {
459
- pointed = face;
460
- placeOutline();
461
- }
462
-
463
- function placeOutline(): void {
464
- const face = pointed ?? (state.open && state.tab === 'faces' ? picked : null);
465
- const node = face !== null ? side.nodeOf(face) : null;
466
- const r = node?.getBoundingClientRect();
467
- if (r === undefined || (r.width === 0 && r.height === 0)) {
468
- if (outline.matches(':popover-open')) outline.hidePopover();
469
- return;
470
- }
471
- Object.assign(outline.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${r.width}px`, height: `${r.height}px` });
472
- outline.classList.toggle('tb-dev-pointed', pointed !== null);
473
- // shown again so it stays above a dialog opened since
474
- if (outline.matches(':popover-open')) outline.hidePopover();
475
- outline.showPopover();
476
- }
477
-
478
- function setPicking(on: boolean): void {
479
- picking = on;
480
- pickButton.setAttribute('aria-pressed', String(on));
481
- document.documentElement.classList.toggle('tb-dev-picking', on);
482
- if (on) openTab('faces');
483
- else point(null);
484
- }
485
-
486
- function pickTarget(ev: Event): { face: ObjectCell; el: HTMLElement } | null {
487
- const t = ev.target;
488
- if (!(t instanceof Element) || inDevtools(t) || t === opener) return null;
489
- return side.faceAt(t);
490
- }
491
-
492
- function pickMove(ev: PointerEvent): void {
493
- if (!picking) return;
494
- const at = pickTarget(ev);
495
- if (at !== null) point(at.face);
496
- }
497
-
498
- function pickDown(ev: PointerEvent): void {
499
- if (!picking || pickTarget(ev) === null) return;
500
- // the face does not get the press: no focus, no drag
501
- ev.preventDefault();
502
- ev.stopImmediatePropagation();
503
- }
504
-
505
- function pickClick(ev: MouseEvent): void {
506
- if (!picking) return;
507
- const at = pickTarget(ev);
508
- if (at === null) return;
509
- ev.preventDefault();
510
- ev.stopImmediatePropagation();
511
- setPicking(false);
512
- pick(at.face);
513
- }
514
-
515
- // --- console -------------------------------------------------------------------------
516
-
517
- // Load errors that mean "the input is not finished yet" (as in the trebol console).
518
- const UNFINISHED = /: missing ([\])]|} to close (string|binary))$/;
519
-
520
- function buildConsole(pane: HTMLElement): void {
521
- consoleOut = el('div', 'tb-dev-out');
522
- consoleOut.setAttribute('role', 'log');
523
- consoleOut.setAttribute('aria-label', 'Console output');
524
- consoleFace = el('div', 'tb-dev-face');
525
- const row = el('div', 'tb-dev-prompt');
526
- consoleIn = el('textarea', 'tb-dev-in');
527
- consoleIn.rows = 1;
528
- consoleIn.spellcheck = false;
529
- consoleIn.setAttribute('aria-label', 'Trebol code to run');
530
- consoleIn.placeholder = 'face/text: "Hello"';
531
- consoleIn.addEventListener('keydown', consoleKey);
532
- consoleIn.addEventListener('input', fitInput);
533
- row.append(el('span', 'tb-dev-sign', '>>'), consoleIn);
534
- row.addEventListener('click', () => consoleIn.focus());
535
- pane.append(consoleOut, consoleFace, row);
536
- }
537
-
538
- function fitInput(): void {
539
- consoleIn.style.height = 'auto';
540
- consoleIn.style.height = `${Math.min(consoleIn.scrollHeight, 160)}px`;
541
- }
542
-
543
- function setInput(src: string): void {
544
- consoleIn.value = src;
545
- fitInput();
546
- consoleIn.setSelectionRange(src.length, src.length);
547
- }
548
-
549
- function consoleKey(ev: KeyboardEvent): void {
550
- const src = consoleIn.value;
551
- if (ev.key === 'Enter' && !ev.shiftKey) {
552
- if (src.trim() === '' || unfinished(src)) return;
553
- ev.preventDefault();
554
- setInput('');
555
- running = running.then(() => run(src));
556
- } else if (ev.key === 'ArrowUp' && !src.slice(0, consoleIn.selectionStart).includes('\n') && back < history.length) {
557
- ev.preventDefault();
558
- back++;
559
- setInput(history[history.length - back]!);
560
- } else if (ev.key === 'ArrowDown' && !src.slice(consoleIn.selectionEnd).includes('\n') && back > 0) {
561
- ev.preventDefault();
562
- back--;
563
- setInput(back === 0 ? '' : history[history.length - back]!);
564
- }
565
- }
566
-
567
- function unfinished(src: string): boolean {
568
- try {
569
- load(src);
570
- return false;
571
- } catch (e) {
572
- return e instanceof LoadError && UNFINISHED.test(e.message);
573
- }
574
- }
575
-
576
- function say(kind: 'in' | 'out' | 'value' | 'error', text: string): void {
577
- const line = el('pre', `tb-dev-said-${kind}`, text);
578
- const end = consoleOut.scrollTop + consoleOut.clientHeight >= consoleOut.scrollHeight - 4;
579
- consoleOut.append(line);
580
- while (consoleOut.childElementCount > MAX_LOG) consoleOut.firstElementChild!.remove();
581
- if (end) consoleOut.scrollTop = consoleOut.scrollHeight;
582
- }
583
-
584
- /** Runs code typed in the console in the app's user context, `face` being the face picked. */
585
- async function run(src: string): Promise<void> {
586
- back = 0;
587
- if (history[history.length - 1] !== src) history.push(src);
588
- history = history.slice(-MAX_HISTORY);
589
- write(() => localStorage, HISTORY_KEY, { lines: history });
590
- say('in', src.split('\n').map((l, i) => (i === 0 ? '>> ' : ' ') + l).join('\n'));
591
- consoleOut.scrollTop = consoleOut.scrollHeight;
592
- let code;
593
- try {
594
- code = load(src);
595
- } catch (e) {
596
- say('error', `** Syntax error: ${(e as Error).message}`);
597
- return;
598
- }
599
- const rt = side.host.rt;
600
- bindSeries(code.s, rt.user, true);
601
- if (picked !== null) bindSeries(code.s, { keys: [intern('face')], vals: [picked], parent: null });
602
- try {
603
- const v = await taskResult(spawn(side.host.sch, code.s, scriptStart(code).start));
604
- if (v.t === K.Error) say('error', form(v));
605
- else if (v.t !== K.Unset) say('value', `== ${moldShort(v)}`);
606
- } catch (e) {
607
- say('error', report(e as ObjectCell));
608
- }
609
- }
610
-
611
- // --- log -----------------------------------------------------------------------------
612
-
613
- function buildLog(pane: HTMLElement): void {
614
- const bar = el('div', 'tb-dev-bar');
615
- bar.append(button('tb-dev-button', 'Clear', () => {
616
- logList.replaceChildren();
617
- lastLog = null;
618
- }));
619
- logList = el('ol', 'tb-dev-entries');
620
- logList.setAttribute('aria-label', 'What the app did');
621
- pane.append(bar, logList);
622
- }
623
-
624
- /** Adds a line to the log; the same one again (typing in a field) counts instead of adding. */
625
- function log(kind: string, text: string, key?: string): void {
626
- if (key !== undefined && lastLog?.key === key && lastLog.li.isConnected) {
627
- lastLog.count++;
628
- lastLog.text.textContent = `${text} (×${lastLog.count})`;
629
- return;
630
- }
631
- const li = el('li', `tb-dev-${kind}`);
632
- const now = new Date();
633
- const time = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
634
- const t = el('span', 'tb-dev-text', text);
635
- li.append(el('time', undefined, time), el('span', 'tb-dev-kind', kind), t);
636
- const end = logList.scrollTop + logList.clientHeight >= logList.scrollHeight - 4;
637
- logList.append(li);
638
- while (logList.childElementCount > MAX_LOG) logList.firstElementChild!.remove();
639
- if (end) logList.scrollTop = logList.scrollHeight;
640
- lastLog = key !== undefined ? { key, li, text: t, count: 1 } : null;
641
- }
package/src/host/fonts.ts CHANGED
@@ -3,9 +3,10 @@
3
3
  // unicode-range of each face). They come from @fontsource-variable/inter
4
4
  // 5.3.0, under the SIL Open Font License (fonts/OFL.txt).
5
5
  //
6
- // trebol.js declares them with addresses next to itself; a page made by
7
- // `trebol build` carries the files inside and declares them in its own
8
- // <style data-tb-fonts>, which the host then leaves alone.
6
+ // trebol.js declares them, with addresses next to itself, in a constructed
7
+ // style sheet (a strict Content-Security-Policy drops a <style> made by
8
+ // script); a page made by `trebol build` carries the files inside and declares
9
+ // them in its own <style data-tb-fonts>, which the host then leaves alone.
9
10
 
10
11
  export interface FontFile {
11
12
  /** File name in fonts/. */
package/src/host/icons.ts CHANGED
@@ -41,6 +41,9 @@ const PATHS: Record<string, string> = {
41
41
  truck: 'M2 6h12v11H2zM14 10h4l4 4v3h-8zM6.5 15a2 2 0 1 0 0 4a2 2 0 1 0 0-4zM17.5 15a2 2 0 1 0 0 4a2 2 0 1 0 0-4z',
42
42
  box: 'M12 3l8 4.5v9L12 21l-8-4.5v-9zM4 7.5l8 4.5 8-4.5M12 12v9',
43
43
  file: 'M6 3h8l4 4v14H6zM14 3v4h4M9 12h6M9 16h6',
44
+ invoice: 'M6 3h12v18l-2-1.5-2 1.5-2-1.5-2 1.5-2-1.5-2 1.5zM9 8h6M9 12h6M9 16h3',
45
+ template: 'M4 4h16v5H4zM4 13h7v7H4zM15 13h5M15 17h5',
46
+ stack: 'M12 3l9 4.5-9 4.5-9-4.5zM3 12l9 4.5 9-4.5M3 16.5l9 4.5 9-4.5',
44
47
  image: 'M4 5h16v14H4zM4 16l5-5 4 4 2-2 5 5M15 8.5a1.5 1.5 0 1 0 0 3a1.5 1.5 0 1 0 0-3z',
45
48
  settings: 'M4 7h10M18 7h2M4 17h4M12 17h8M16 5v4M10 15v4',
46
49
  filter: 'M4 5h16l-6 8v6l-4-2v-4L4 5z',
@@ -51,6 +54,7 @@ const PATHS: Record<string, string> = {
51
54
  eye: 'M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12zM12 9a3 3 0 1 0 0 6a3 3 0 1 0 0-6z',
52
55
  lock: 'M5 11h14v10H5zM8 11V7a4 4 0 0 1 8 0v4',
53
56
  calendar: 'M4 6h16v15H4zM4 10h16M8 3v4M16 3v4',
57
+ clock: 'M12 3a9 9 0 1 0 0 18a9 9 0 1 0 0-18zM12 7v5l3 2',
54
58
  star: 'M12 3l2.8 5.8 6.2.9-4.5 4.4 1 6.3L12 17.5l-5.5 2.9 1-6.3L3 9.7l6.2-.9L12 3z',
55
59
  heart: 'M12 20s-8-5-8-11a4.5 4.5 0 0 1 8-2.8A4.5 4.5 0 0 1 20 9c0 6-8 11-8 11z',
56
60
  type: 'M5 7V4h14v3M12 4v16M9 20h6',