trebol 0.1.0 → 0.1.2

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 (160) hide show
  1. package/README.md +18 -3
  2. package/dist/cli.js +13790 -11417
  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/access.d.ts +10 -0
  12. package/dist/lib/core/access.js +210 -0
  13. package/dist/lib/core/base.d.ts +4 -0
  14. package/dist/lib/core/base.js +50 -0
  15. package/dist/lib/core/bitset.d.ts +9 -0
  16. package/dist/lib/core/bitset.js +75 -0
  17. package/dist/lib/core/boot.d.ts +18 -0
  18. package/dist/lib/core/boot.js +73 -0
  19. package/dist/lib/core/compare.d.ts +9 -0
  20. package/dist/lib/core/compare.js +156 -0
  21. package/dist/lib/core/ctx.d.ts +36 -0
  22. package/dist/lib/core/ctx.js +251 -0
  23. package/dist/lib/core/dec.d.ts +28 -0
  24. package/dist/lib/core/dec.js +158 -0
  25. package/dist/lib/core/errors.d.ts +15 -0
  26. package/dist/lib/core/errors.js +44 -0
  27. package/dist/lib/core/escapes.d.ts +14 -0
  28. package/dist/lib/core/escapes.js +80 -0
  29. package/dist/lib/core/eval.d.ts +220 -0
  30. package/dist/lib/core/eval.js +666 -0
  31. package/dist/lib/core/func.d.ts +68 -0
  32. package/dist/lib/core/func.js +129 -0
  33. package/dist/lib/core/hash.d.ts +5 -0
  34. package/dist/lib/core/hash.js +155 -0
  35. package/dist/lib/core/index.d.ts +18 -0
  36. package/dist/lib/core/index.js +20 -0
  37. package/dist/lib/core/int64.d.ts +22 -0
  38. package/dist/lib/core/int64.js +113 -0
  39. package/dist/lib/core/kinds.d.ts +67 -0
  40. package/dist/lib/core/kinds.js +134 -0
  41. package/dist/lib/core/lexer.d.ts +13 -0
  42. package/dist/lib/core/lexer.js +702 -0
  43. package/dist/lib/core/mezz.d.ts +1 -0
  44. package/dist/lib/core/mezz.js +360 -0
  45. package/dist/lib/core/mold.d.ts +20 -0
  46. package/dist/lib/core/mold.js +434 -0
  47. package/dist/lib/core/natives/control.d.ts +1 -0
  48. package/dist/lib/core/natives/control.js +374 -0
  49. package/dist/lib/core/natives/csv.d.ts +15 -0
  50. package/dist/lib/core/natives/csv.js +315 -0
  51. package/dist/lib/core/natives/data.d.ts +11 -0
  52. package/dist/lib/core/natives/data.js +367 -0
  53. package/dist/lib/core/natives/define.d.ts +17 -0
  54. package/dist/lib/core/natives/define.js +47 -0
  55. package/dist/lib/core/natives/encode.d.ts +3 -0
  56. package/dist/lib/core/natives/encode.js +187 -0
  57. package/dist/lib/core/natives/json.d.ts +5 -0
  58. package/dist/lib/core/natives/json.js +273 -0
  59. package/dist/lib/core/natives/math.d.ts +3 -0
  60. package/dist/lib/core/natives/math.js +500 -0
  61. package/dist/lib/core/natives/parse.d.ts +1 -0
  62. package/dist/lib/core/natives/parse.js +600 -0
  63. package/dist/lib/core/natives/series.d.ts +4 -0
  64. package/dist/lib/core/natives/series.js +519 -0
  65. package/dist/lib/core/natives/text.d.ts +1 -0
  66. package/dist/lib/core/natives/text.js +487 -0
  67. package/dist/lib/core/natives/xml.d.ts +9 -0
  68. package/dist/lib/core/natives/xml.js +242 -0
  69. package/dist/lib/core/series.d.ts +26 -0
  70. package/dist/lib/core/series.js +73 -0
  71. package/dist/lib/core/symbols.d.ts +9 -0
  72. package/dist/lib/core/symbols.js +44 -0
  73. package/dist/lib/core/values.d.ts +206 -0
  74. package/dist/lib/core/values.js +223 -0
  75. package/dist/lib/host/browser.d.ts +4 -0
  76. package/dist/lib/host/browser.js +4974 -0
  77. package/dist/lib/host/chart.d.ts +26 -0
  78. package/dist/lib/host/chart.js +158 -0
  79. package/dist/lib/host/compress.d.ts +1 -0
  80. package/dist/lib/host/compress.js +37 -0
  81. package/dist/lib/host/devlog.d.ts +11 -0
  82. package/dist/lib/host/devlog.js +13 -0
  83. package/dist/lib/host/devtools.d.ts +23 -0
  84. package/dist/lib/host/devtools.js +626 -0
  85. package/dist/lib/host/draw.d.ts +31 -0
  86. package/dist/lib/host/draw.js +348 -0
  87. package/dist/lib/host/fonts/OFL.txt +93 -0
  88. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  89. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  90. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  91. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  92. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  93. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  94. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  95. package/dist/lib/host/fonts.d.ts +11 -0
  96. package/dist/lib/host/fonts.js +29 -0
  97. package/dist/lib/host/icons.d.ts +3 -0
  98. package/dist/lib/host/icons.js +97 -0
  99. package/dist/lib/host/index.d.ts +40 -0
  100. package/dist/lib/host/index.js +192 -0
  101. package/dist/lib/host/js.d.ts +5 -0
  102. package/dist/lib/host/js.js +130 -0
  103. package/dist/lib/host/locale.d.ts +40 -0
  104. package/dist/lib/host/locale.js +241 -0
  105. package/dist/lib/host/markdown.d.ts +30 -0
  106. package/dist/lib/host/markdown.js +161 -0
  107. package/dist/lib/host/modules.d.ts +14 -0
  108. package/dist/lib/host/modules.js +300 -0
  109. package/dist/lib/host/natives.d.ts +6 -0
  110. package/dist/lib/host/natives.js +98 -0
  111. package/dist/lib/host/net.d.ts +12 -0
  112. package/dist/lib/host/net.js +549 -0
  113. package/dist/lib/host/orbit.d.ts +121 -0
  114. package/dist/lib/host/orbit.js +374 -0
  115. package/dist/lib/host/page.d.ts +9 -0
  116. package/dist/lib/host/page.js +55 -0
  117. package/dist/lib/host/rows.d.ts +29 -0
  118. package/dist/lib/host/rows.js +109 -0
  119. package/dist/lib/host/scheduler.d.ts +51 -0
  120. package/dist/lib/host/scheduler.js +197 -0
  121. package/dist/lib/host/theme.d.ts +58 -0
  122. package/dist/lib/host/theme.js +1667 -0
  123. package/dist/lib/ui/i18n.trb +117 -0
  124. package/dist/lib/ui/test.trb +565 -0
  125. package/dist/lib/ui/ui.trb +1678 -0
  126. package/dist/trebol.js +1427 -138
  127. package/editors/vscode/README.md +15 -0
  128. package/editors/vscode/language-configuration.json +28 -0
  129. package/editors/vscode/package.json +42 -0
  130. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  131. package/package.json +23 -7
  132. package/src/core/boot.ts +1 -1
  133. package/src/core/func.ts +4 -0
  134. package/src/core/natives/control.ts +1 -1
  135. package/src/core/natives/series.ts +9 -1
  136. package/src/core/natives/text.ts +2 -0
  137. package/src/host/browser.ts +1235 -83
  138. package/src/host/devlog.ts +16 -0
  139. package/src/host/devtools.ts +641 -0
  140. package/src/host/fonts/OFL.txt +93 -0
  141. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  142. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  143. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  144. package/src/host/fonts/inter-greek.woff2 +0 -0
  145. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  146. package/src/host/fonts/inter-latin.woff2 +0 -0
  147. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  148. package/src/host/fonts.ts +39 -0
  149. package/src/host/index.ts +5 -3
  150. package/src/host/js.ts +127 -0
  151. package/src/host/markdown.ts +9 -0
  152. package/src/host/modules.ts +41 -2
  153. package/src/host/net.ts +9 -1
  154. package/src/host/orbit.ts +432 -0
  155. package/src/host/rows.ts +30 -0
  156. package/src/host/scheduler.ts +62 -36
  157. package/src/host/theme.ts +270 -6
  158. package/src/ui/i18n.trb +10 -0
  159. package/src/ui/test.trb +565 -0
  160. package/src/ui/ui.trb +564 -90
@@ -0,0 +1,626 @@
1
+ // The dev tools of trebol dev: a panel docked at the bottom of the page, opened
2
+ // with Ctrl+` or the small button in the corner. Only a page that trebol dev
3
+ // serves has them (it adds <meta name="trebol-dev">); a built page never does.
4
+ //
5
+ // Faces the faces on the page as a tree, and what one of them holds:
6
+ // pick one on the page or in the tree; where it was written links
7
+ // to the editor (vscode://).
8
+ // Console >> and == as in the trebol console, against the running app;
9
+ // `face` is the face picked; print shows here too.
10
+ // Log events, requests, toasts, errors and warnings, newest last.
11
+ import { K, NONE, load, LoadError, bindSeries, scriptStart, mold, form, ctxFind, intern, spell, } from "../core/index.js";
12
+ import { spawn, taskResult } from "./scheduler.js";
13
+ import { output, report, translate } from "./index.js";
14
+ import { devLog } from "./devlog.js";
15
+ const STATE_KEY = 'trebol-dev';
16
+ const HISTORY_KEY = 'trebol-dev-history';
17
+ const MAX_HISTORY = 100;
18
+ const MAX_LOG = 500;
19
+ const MAX_MOLD = 2000;
20
+ let side;
21
+ let state = { open: false, tab: 'faces', height: 320 };
22
+ let panel;
23
+ let opener;
24
+ let outline;
25
+ let pickButton;
26
+ let tree;
27
+ let detail;
28
+ let consoleOut;
29
+ let consoleIn;
30
+ let consoleFace;
31
+ let logList;
32
+ const tabs = new Map();
33
+ /** The face chosen in Faces, `face` in Console. */
34
+ let picked = null;
35
+ /** The face under the pointer or the tree row the pointer is on. */
36
+ let pointed = null;
37
+ let picking = false;
38
+ let history = [];
39
+ let back = 0;
40
+ let lastLog = null;
41
+ let treeFrame = 0;
42
+ /** The code typed so far runs one piece after another. */
43
+ let running = Promise.resolve();
44
+ function el(tag, cls, content) {
45
+ const e = document.createElement(tag);
46
+ if (cls)
47
+ e.className = cls;
48
+ if (content !== undefined)
49
+ e.textContent = content;
50
+ return e;
51
+ }
52
+ function button(cls, content, act) {
53
+ const b = el('button', cls, content);
54
+ b.type = 'button';
55
+ b.addEventListener('click', act);
56
+ return b;
57
+ }
58
+ function read(store, key, fallback) {
59
+ try {
60
+ const v = store().getItem(key);
61
+ return v === null ? fallback : { ...fallback, ...JSON.parse(v) };
62
+ }
63
+ catch {
64
+ return fallback;
65
+ }
66
+ }
67
+ function write(store, key, v) {
68
+ try {
69
+ store().setItem(key, JSON.stringify(v));
70
+ }
71
+ catch {
72
+ // a private window or blocked storage: the panel forgets, nothing else
73
+ }
74
+ }
75
+ // --- reading faces -----------------------------------------------------------------
76
+ function get(face, name) {
77
+ const i = ctxFind(face.ctx, intern(name));
78
+ return i < 0 ? NONE : face.ctx.vals[i];
79
+ }
80
+ function wordText(v) {
81
+ return v.t === K.Word || v.t === K.LitWord ? spell(v.sym) : null;
82
+ }
83
+ function short(s, most = 80) {
84
+ const line = s.replace(/\s+/g, ' ');
85
+ return line.length > most ? line.slice(0, most - 1) + '…' : line;
86
+ }
87
+ /** How a face is named in the tree, the log and errors: its style, name and text (`field email "Email"`). */
88
+ export function faceRef(face, rt) {
89
+ const style = wordText(get(face, 'style')) ?? 'face';
90
+ const name = wordText(get(face, 'name'));
91
+ const t = get(face, 'text');
92
+ const shown = t.t === K.None ? null : t.t === K.String || t.t === K.Map || t.t === K.Word ? translate(t, rt) : form(t);
93
+ return [style, name, shown !== null && shown !== '' ? `"${short(shown, 40)}"` : null].filter(x => x !== null).join(' ');
94
+ }
95
+ function on(face, name) {
96
+ const v = get(face, name);
97
+ return !(v.t === K.None || (v.t === K.Logic && !v.v));
98
+ }
99
+ function moldShort(v, most = MAX_MOLD) {
100
+ const s = mold(v);
101
+ return s.length > most ? s.slice(0, most - 1) + '…' : s;
102
+ }
103
+ /** A face's file:line:col and a vscode:// link to it, when the file is in the folder trebol dev serves. */
104
+ function placeOf(face) {
105
+ const v = get(face, 'place');
106
+ if (v.t !== K.String)
107
+ return null;
108
+ const at = form(v);
109
+ const m = /^(.*):(\d+):(\d+)$/.exec(at);
110
+ if (m === null)
111
+ return { text: at, href: null };
112
+ let url;
113
+ try {
114
+ url = new URL(m[1]);
115
+ }
116
+ catch {
117
+ return { text: at, href: null };
118
+ }
119
+ const path = decodeURIComponent(url.pathname);
120
+ const text = `${path.slice(1)}:${m[2]}:${m[3]}`;
121
+ const mine = url.origin === location.origin && !path.startsWith('/@trebol/') && side.root !== null;
122
+ return { text, href: mine ? `vscode://file${side.root.replace(/\/$/, '')}${path}:${m[2]}:${m[3]}` : null };
123
+ }
124
+ // --- the panel -----------------------------------------------------------------------
125
+ /** Starts the dev tools on a page served by trebol dev; not in a frame (the documentation's examples). */
126
+ export function startDevtools(s) {
127
+ if (window !== window.top)
128
+ return;
129
+ side = s;
130
+ state = read(() => sessionStorage, STATE_KEY, state);
131
+ history = read(() => localStorage, HISTORY_KEY, { lines: [] }).lines;
132
+ panel = el('section', 'tb-dev-tools');
133
+ panel.popover = 'manual';
134
+ panel.setAttribute('aria-label', 'Trebol dev tools');
135
+ const grip = el('div', 'tb-dev-grip');
136
+ grip.addEventListener('pointerdown', resizeFrom);
137
+ const head = el('header');
138
+ const list = el('div', 'tb-dev-tabs');
139
+ list.setAttribute('role', 'tablist');
140
+ head.append(list);
141
+ const body = el('div', 'tb-dev-body');
142
+ for (const [tab, label] of [['faces', 'Faces'], ['console', 'Console'], ['log', 'Log']]) {
143
+ const b = button('tb-dev-tab', label, () => openTab(tab));
144
+ b.setAttribute('role', 'tab');
145
+ b.id = `tb-dev-tab-${tab}`;
146
+ const pane = el('div', `tb-dev-pane tb-dev-${tab}`);
147
+ pane.setAttribute('role', 'tabpanel');
148
+ pane.setAttribute('aria-labelledby', b.id);
149
+ list.append(b);
150
+ body.append(pane);
151
+ tabs.set(tab, { button: b, pane });
152
+ }
153
+ pickButton = button('tb-dev-button', 'Pick', () => setPicking(!picking));
154
+ pickButton.title = 'Click a face on the page to see it here';
155
+ pickButton.setAttribute('aria-pressed', 'false');
156
+ const close = button('tb-dev-button', 'Close', () => setOpen(false));
157
+ close.title = 'Close (Ctrl+`)';
158
+ head.append(el('span', 'tb-dev-space'), pickButton, close);
159
+ panel.append(grip, head, body);
160
+ // keys typed here are not for a face around the panel, and Escape does not close a dialog it is in
161
+ panel.addEventListener('keydown', ev => {
162
+ ev.stopPropagation();
163
+ if (ev.key === 'Escape')
164
+ ev.preventDefault();
165
+ });
166
+ buildFaces(tabs.get('faces').pane);
167
+ buildConsole(tabs.get('console').pane);
168
+ buildLog(tabs.get('log').pane);
169
+ opener = button('tb-dev-opener', 'Trebol', () => setOpen(true));
170
+ opener.popover = 'manual';
171
+ opener.title = 'Dev tools (Ctrl+`)';
172
+ outline = el('div', 'tb-dev-outline');
173
+ outline.popover = 'manual';
174
+ outline.setAttribute('aria-hidden', 'true');
175
+ document.body.append(panel, opener, outline);
176
+ addEventListener('keydown', ev => {
177
+ if (ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.code === 'Backquote') {
178
+ ev.preventDefault();
179
+ ev.stopImmediatePropagation();
180
+ setOpen(!state.open);
181
+ }
182
+ else if (picking && ev.key === 'Escape') {
183
+ ev.preventDefault();
184
+ ev.stopImmediatePropagation();
185
+ setPicking(false);
186
+ }
187
+ }, true);
188
+ addEventListener('pointermove', pickMove, true);
189
+ addEventListener('pointerdown', pickDown, true);
190
+ addEventListener('click', pickClick, true);
191
+ addEventListener('scroll', () => placeOutline(), { capture: true, passive: true });
192
+ addEventListener('resize', () => {
193
+ setHeight(state.height);
194
+ placeOutline();
195
+ });
196
+ hook();
197
+ openTab(state.tab);
198
+ setOpen(state.open);
199
+ }
200
+ /** Keys typed in the panel are not the app's: its shortcuts and on-key leave them. */
201
+ export function inDevtools(target) {
202
+ return target instanceof Element && target.closest('.tb-dev-tools') !== null;
203
+ }
204
+ /**
205
+ * Puts the panel, its opener and the outline inside the dialog on show, or back in the body: while a
206
+ * modal dialog is open everything outside it is inert, the panel too.
207
+ */
208
+ function home() {
209
+ const modals = document.querySelectorAll('dialog:modal');
210
+ const into = modals.length > 0 ? modals[modals.length - 1] : document.body;
211
+ if (panel.parentElement === into)
212
+ return;
213
+ for (const e of [panel, opener, outline]) {
214
+ const shown = e.matches(':popover-open');
215
+ into.append(e);
216
+ if (shown)
217
+ e.showPopover();
218
+ }
219
+ }
220
+ function setOpen(open) {
221
+ state.open = open;
222
+ write(() => sessionStorage, STATE_KEY, state);
223
+ home();
224
+ if (open) {
225
+ opener.hidePopover();
226
+ panel.showPopover();
227
+ setHeight(state.height);
228
+ refresh();
229
+ if (state.tab === 'console')
230
+ consoleIn.focus();
231
+ }
232
+ else {
233
+ setPicking(false);
234
+ point(null);
235
+ const focused = panel.contains(document.activeElement);
236
+ panel.hidePopover();
237
+ opener.showPopover();
238
+ if (focused)
239
+ opener.focus();
240
+ document.body.style.paddingBottom = '';
241
+ }
242
+ }
243
+ function setHeight(h) {
244
+ state.height = Math.round(Math.max(140, Math.min(h, innerHeight - 60)));
245
+ panel.style.height = `${state.height}px`;
246
+ // the end of the page stays reachable above the panel
247
+ if (state.open)
248
+ document.body.style.paddingBottom = `${state.height}px`;
249
+ }
250
+ function resizeFrom(ev) {
251
+ ev.preventDefault();
252
+ const grip = ev.currentTarget;
253
+ grip.setPointerCapture(ev.pointerId);
254
+ const move = (e) => setHeight(innerHeight - e.clientY);
255
+ const up = () => {
256
+ grip.removeEventListener('pointermove', move);
257
+ grip.removeEventListener('pointerup', up);
258
+ write(() => sessionStorage, STATE_KEY, state);
259
+ };
260
+ grip.addEventListener('pointermove', move);
261
+ grip.addEventListener('pointerup', up);
262
+ }
263
+ function openTab(tab) {
264
+ state.tab = tab;
265
+ write(() => sessionStorage, STATE_KEY, state);
266
+ for (const [t, { button: b, pane }] of tabs) {
267
+ b.setAttribute('aria-selected', String(t === tab));
268
+ b.tabIndex = t === tab ? 0 : -1;
269
+ pane.hidden = t !== tab;
270
+ }
271
+ refresh();
272
+ placeOutline();
273
+ if (tab === 'console' && state.open)
274
+ consoleIn.focus();
275
+ if (tab === 'log')
276
+ logList.scrollTop = logList.scrollHeight;
277
+ }
278
+ /** What the host tells: events, requests, notes and repaints. */
279
+ function hook() {
280
+ devLog.event = (fn, face, args) => {
281
+ if (fn === 'ui-blur')
282
+ return;
283
+ let what = fn.replace(/^ui-/, '');
284
+ let rest = args;
285
+ if (fn === 'ui-event' && args[0] !== undefined) {
286
+ what = wordText(args[0]) ?? what;
287
+ rest = [];
288
+ }
289
+ const shown = rest.length > 0 && rest[0].t !== K.Object ? ' ' + short(mold(rest[0]), 60) : '';
290
+ log('event', `${what} · ${faceRef(face, side.host.rt)}${shown}`, `${fn} ${faceKey(face)}`);
291
+ };
292
+ devLog.request = (verb, url, status, ms, problem) => {
293
+ let where = url;
294
+ try {
295
+ const u = new URL(url, location.href);
296
+ if (u.origin === location.origin)
297
+ where = u.pathname + u.search;
298
+ }
299
+ catch {
300
+ // not a URL: shown as given
301
+ }
302
+ const outcome = status !== null ? String(status) : `failed (${problem ?? 'network'})`;
303
+ log(status !== null && status < 400 ? 'request' : 'failed', `${verb} ${where} ${outcome} · ${ms} ms`);
304
+ };
305
+ devLog.note = (kind, text) => log(kind, text);
306
+ devLog.repainted = () => {
307
+ home();
308
+ if (!state.open)
309
+ return;
310
+ placeOutline();
311
+ if (state.tab === 'faces' && treeFrame === 0)
312
+ treeFrame = requestAnimationFrame(() => {
313
+ treeFrame = 0;
314
+ refresh();
315
+ });
316
+ };
317
+ const line = output.line;
318
+ output.line = text => {
319
+ line(text);
320
+ say('out', text);
321
+ };
322
+ }
323
+ function refresh() {
324
+ if (!state.open)
325
+ return;
326
+ if (state.tab === 'faces') {
327
+ drawTree();
328
+ drawDetail();
329
+ }
330
+ consoleFace.textContent = picked !== null ? `face: ${faceRef(picked, side.host.rt)}` : 'face: none (pick one in Faces)';
331
+ }
332
+ // --- faces ---------------------------------------------------------------------------
333
+ const faceKeys = new WeakMap();
334
+ let nextKey = 1;
335
+ function faceKey(face) {
336
+ let k = faceKeys.get(face.ctx);
337
+ if (k === undefined)
338
+ faceKeys.set(face.ctx, k = nextKey++);
339
+ return k;
340
+ }
341
+ function buildFaces(pane) {
342
+ tree = el('div', 'tb-dev-tree');
343
+ tree.setAttribute('aria-label', 'Faces on the page');
344
+ tree.addEventListener('pointerleave', () => point(null));
345
+ detail = el('div', 'tb-dev-detail');
346
+ pane.append(tree, detail);
347
+ }
348
+ /** The faces on the page, in the order of their nodes, with how deep each is. */
349
+ function facesOnPage() {
350
+ const out = [];
351
+ const walk = (e, depth) => {
352
+ for (const c of Array.from(e.children)) {
353
+ if (c === panel || c === opener || c === outline)
354
+ continue;
355
+ const f = side.faceOf(c);
356
+ if (f !== null)
357
+ out.push({ face: f, el: c, depth });
358
+ walk(c, f !== null ? depth + 1 : depth);
359
+ }
360
+ };
361
+ walk(document.body, 0);
362
+ return out;
363
+ }
364
+ function drawTree() {
365
+ const top = tree.scrollTop;
366
+ tree.replaceChildren();
367
+ for (const { face, el: node, depth } of facesOnPage()) {
368
+ const row = button('tb-dev-node', faceRef(face, side.host.rt), () => pick(face));
369
+ row.style.paddingLeft = `${8 + depth * 14}px`;
370
+ if (face.ctx === picked?.ctx)
371
+ row.setAttribute('aria-current', 'true');
372
+ if (node.offsetParent === null && getComputedStyle(node).position !== 'fixed')
373
+ row.classList.add('tb-dev-unseen');
374
+ row.addEventListener('pointerenter', () => point(face));
375
+ tree.append(row);
376
+ }
377
+ tree.scrollTop = top;
378
+ }
379
+ function drawDetail() {
380
+ detail.replaceChildren();
381
+ if (picked === null) {
382
+ detail.append(el('p', 'tb-dev-hint', 'Pick a face on the page, or choose one in the tree.'));
383
+ return;
384
+ }
385
+ const f = picked;
386
+ const title = el('h3', undefined, faceRef(f, side.host.rt));
387
+ detail.append(title);
388
+ const where = placeOf(f);
389
+ if (where !== null) {
390
+ const p = el('p', 'tb-dev-place');
391
+ if (where.href !== null) {
392
+ const a = el('a', undefined, where.text);
393
+ a.href = where.href;
394
+ a.title = 'Open in VS Code';
395
+ p.append(a);
396
+ }
397
+ else
398
+ p.textContent = where.text;
399
+ detail.append(p);
400
+ }
401
+ if (side.nodeOf(f) === null)
402
+ detail.append(el('p', 'tb-dev-hint', 'Not on the page any more.'));
403
+ const dl = el('dl');
404
+ const row = (k, v) => {
405
+ dl.append(el('dt', undefined, k), el('dd', undefined, v));
406
+ };
407
+ for (const n of ['style', 'name', 'text', 'value', 'error', 'rules', 'hint', 'role']) {
408
+ const v = get(f, n);
409
+ if (v.t !== K.None)
410
+ row(n, moldShort(v, 400));
411
+ }
412
+ const events = [];
413
+ if (get(f, 'action').t === K.Block)
414
+ events.push('action');
415
+ const ev = get(f, 'events');
416
+ if (ev.t === K.Block) {
417
+ for (const c of ev.s.data.slice(ev.i))
418
+ if (c.t !== K.Block)
419
+ events.push(form(c));
420
+ }
421
+ if (events.length > 0)
422
+ row('events', events.join(' '));
423
+ const flags = ['required', 'disabled', 'hidden', 'readonly'].filter(n => on(f, n));
424
+ if (flags.length > 0)
425
+ row('flags', flags.join(' '));
426
+ const data = get(f, 'data');
427
+ if (data.t === K.Block)
428
+ row('rows', String(data.s.data.length - data.i));
429
+ detail.append(dl);
430
+ }
431
+ function pick(face) {
432
+ picked = face;
433
+ point(null);
434
+ refresh();
435
+ }
436
+ /** Draws the outline around a face, or hides it. */
437
+ function point(face) {
438
+ pointed = face;
439
+ placeOutline();
440
+ }
441
+ function placeOutline() {
442
+ const face = pointed ?? (state.open && state.tab === 'faces' ? picked : null);
443
+ const node = face !== null ? side.nodeOf(face) : null;
444
+ const r = node?.getBoundingClientRect();
445
+ if (r === undefined || (r.width === 0 && r.height === 0)) {
446
+ if (outline.matches(':popover-open'))
447
+ outline.hidePopover();
448
+ return;
449
+ }
450
+ Object.assign(outline.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${r.width}px`, height: `${r.height}px` });
451
+ outline.classList.toggle('tb-dev-pointed', pointed !== null);
452
+ // shown again so it stays above a dialog opened since
453
+ if (outline.matches(':popover-open'))
454
+ outline.hidePopover();
455
+ outline.showPopover();
456
+ }
457
+ function setPicking(on) {
458
+ picking = on;
459
+ pickButton.setAttribute('aria-pressed', String(on));
460
+ document.documentElement.classList.toggle('tb-dev-picking', on);
461
+ if (on)
462
+ openTab('faces');
463
+ else
464
+ point(null);
465
+ }
466
+ function pickTarget(ev) {
467
+ const t = ev.target;
468
+ if (!(t instanceof Element) || inDevtools(t) || t === opener)
469
+ return null;
470
+ return side.faceAt(t);
471
+ }
472
+ function pickMove(ev) {
473
+ if (!picking)
474
+ return;
475
+ const at = pickTarget(ev);
476
+ if (at !== null)
477
+ point(at.face);
478
+ }
479
+ function pickDown(ev) {
480
+ if (!picking || pickTarget(ev) === null)
481
+ return;
482
+ // the face does not get the press: no focus, no drag
483
+ ev.preventDefault();
484
+ ev.stopImmediatePropagation();
485
+ }
486
+ function pickClick(ev) {
487
+ if (!picking)
488
+ return;
489
+ const at = pickTarget(ev);
490
+ if (at === null)
491
+ return;
492
+ ev.preventDefault();
493
+ ev.stopImmediatePropagation();
494
+ setPicking(false);
495
+ pick(at.face);
496
+ }
497
+ // --- console -------------------------------------------------------------------------
498
+ // Load errors that mean "the input is not finished yet" (as in the trebol console).
499
+ const UNFINISHED = /: missing ([\])]|} to close (string|binary))$/;
500
+ function buildConsole(pane) {
501
+ consoleOut = el('div', 'tb-dev-out');
502
+ consoleOut.setAttribute('role', 'log');
503
+ consoleOut.setAttribute('aria-label', 'Console output');
504
+ consoleFace = el('div', 'tb-dev-face');
505
+ const row = el('div', 'tb-dev-prompt');
506
+ consoleIn = el('textarea', 'tb-dev-in');
507
+ consoleIn.rows = 1;
508
+ consoleIn.spellcheck = false;
509
+ consoleIn.setAttribute('aria-label', 'Trebol code to run');
510
+ consoleIn.placeholder = 'face/text: "Hello"';
511
+ consoleIn.addEventListener('keydown', consoleKey);
512
+ consoleIn.addEventListener('input', fitInput);
513
+ row.append(el('span', 'tb-dev-sign', '>>'), consoleIn);
514
+ row.addEventListener('click', () => consoleIn.focus());
515
+ pane.append(consoleOut, consoleFace, row);
516
+ }
517
+ function fitInput() {
518
+ consoleIn.style.height = 'auto';
519
+ consoleIn.style.height = `${Math.min(consoleIn.scrollHeight, 160)}px`;
520
+ }
521
+ function setInput(src) {
522
+ consoleIn.value = src;
523
+ fitInput();
524
+ consoleIn.setSelectionRange(src.length, src.length);
525
+ }
526
+ function consoleKey(ev) {
527
+ const src = consoleIn.value;
528
+ if (ev.key === 'Enter' && !ev.shiftKey) {
529
+ if (src.trim() === '' || unfinished(src))
530
+ return;
531
+ ev.preventDefault();
532
+ setInput('');
533
+ running = running.then(() => run(src));
534
+ }
535
+ else if (ev.key === 'ArrowUp' && !src.slice(0, consoleIn.selectionStart).includes('\n') && back < history.length) {
536
+ ev.preventDefault();
537
+ back++;
538
+ setInput(history[history.length - back]);
539
+ }
540
+ else if (ev.key === 'ArrowDown' && !src.slice(consoleIn.selectionEnd).includes('\n') && back > 0) {
541
+ ev.preventDefault();
542
+ back--;
543
+ setInput(back === 0 ? '' : history[history.length - back]);
544
+ }
545
+ }
546
+ function unfinished(src) {
547
+ try {
548
+ load(src);
549
+ return false;
550
+ }
551
+ catch (e) {
552
+ return e instanceof LoadError && UNFINISHED.test(e.message);
553
+ }
554
+ }
555
+ function say(kind, text) {
556
+ const line = el('pre', `tb-dev-said-${kind}`, text);
557
+ const end = consoleOut.scrollTop + consoleOut.clientHeight >= consoleOut.scrollHeight - 4;
558
+ consoleOut.append(line);
559
+ while (consoleOut.childElementCount > MAX_LOG)
560
+ consoleOut.firstElementChild.remove();
561
+ if (end)
562
+ consoleOut.scrollTop = consoleOut.scrollHeight;
563
+ }
564
+ /** Runs code typed in the console in the app's user context, `face` being the face picked. */
565
+ async function run(src) {
566
+ back = 0;
567
+ if (history[history.length - 1] !== src)
568
+ history.push(src);
569
+ history = history.slice(-MAX_HISTORY);
570
+ write(() => localStorage, HISTORY_KEY, { lines: history });
571
+ say('in', src.split('\n').map((l, i) => (i === 0 ? '>> ' : ' ') + l).join('\n'));
572
+ consoleOut.scrollTop = consoleOut.scrollHeight;
573
+ let code;
574
+ try {
575
+ code = load(src);
576
+ }
577
+ catch (e) {
578
+ say('error', `** Syntax error: ${e.message}`);
579
+ return;
580
+ }
581
+ const rt = side.host.rt;
582
+ bindSeries(code.s, rt.user, true);
583
+ if (picked !== null)
584
+ bindSeries(code.s, { keys: [intern('face')], vals: [picked], parent: null });
585
+ try {
586
+ const v = await taskResult(spawn(side.host.sch, code.s, scriptStart(code).start));
587
+ if (v.t === K.Error)
588
+ say('error', form(v));
589
+ else if (v.t !== K.Unset)
590
+ say('value', `== ${moldShort(v)}`);
591
+ }
592
+ catch (e) {
593
+ say('error', report(e));
594
+ }
595
+ }
596
+ // --- log -----------------------------------------------------------------------------
597
+ function buildLog(pane) {
598
+ const bar = el('div', 'tb-dev-bar');
599
+ bar.append(button('tb-dev-button', 'Clear', () => {
600
+ logList.replaceChildren();
601
+ lastLog = null;
602
+ }));
603
+ logList = el('ol', 'tb-dev-entries');
604
+ logList.setAttribute('aria-label', 'What the app did');
605
+ pane.append(bar, logList);
606
+ }
607
+ /** Adds a line to the log; the same one again (typing in a field) counts instead of adding. */
608
+ function log(kind, text, key) {
609
+ if (key !== undefined && lastLog?.key === key && lastLog.li.isConnected) {
610
+ lastLog.count++;
611
+ lastLog.text.textContent = `${text} (×${lastLog.count})`;
612
+ return;
613
+ }
614
+ const li = el('li', `tb-dev-${kind}`);
615
+ const now = new Date();
616
+ const time = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
617
+ const t = el('span', 'tb-dev-text', text);
618
+ li.append(el('time', undefined, time), el('span', 'tb-dev-kind', kind), t);
619
+ const end = logList.scrollTop + logList.clientHeight >= logList.scrollHeight - 4;
620
+ logList.append(li);
621
+ while (logList.childElementCount > MAX_LOG)
622
+ logList.firstElementChild.remove();
623
+ if (end)
624
+ logList.scrollTop = logList.scrollHeight;
625
+ lastLog = key !== undefined ? { key, li, text: t, count: 1 } : null;
626
+ }
@@ -0,0 +1,31 @@
1
+ import { type Cell } from '../core/index.ts';
2
+ /** An SVG element to make: its tag, attributes, children and text. */
3
+ export interface El {
4
+ tag: string;
5
+ attrs: Record<string, string>;
6
+ kids: El[];
7
+ text?: string;
8
+ }
9
+ export declare const svgEl: (tag: string, attrs?: Record<string, string | number>, kids?: El[], txt?: string) => El;
10
+ /** Theme roles a drawing may use as colors. */
11
+ export declare const DRAW_ROLES: readonly ["primary", "accent", "danger", "success", "warning", "muted", "text", "border", "surface", "bg"];
12
+ /**
13
+ * Red's color words Trebol knows, as OKLCH hue and chroma with a lightness for
14
+ * light and one for dark mode, so each one reads on the background of either.
15
+ * black and white stay what they say in both.
16
+ */
17
+ export declare const DRAW_COLORS: Record<string, {
18
+ h: number;
19
+ c: number;
20
+ light: number;
21
+ dark: number;
22
+ }>;
23
+ /** The commands of the dialect, and how each is written. */
24
+ export declare const DRAW_COMMANDS: Record<string, string>;
25
+ /** A color word as CSS; null for off, undefined when it is no color. */
26
+ export declare function drawColor(w: string): string | null | undefined;
27
+ /** The SVG elements of a draw block, and what was wrong with it, if anything. Drawing stops at the first error. */
28
+ export declare function drawSvg(block: Cell): {
29
+ kids: El[];
30
+ error: string | null;
31
+ };