trebol 0.1.1 → 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.
- package/README.md +10 -1
- package/dist/cli.js +13747 -11409
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/core/boot.js +2 -1
- package/dist/lib/core/func.d.ts +4 -0
- package/dist/lib/core/natives/control.js +1 -1
- package/dist/lib/core/natives/series.js +11 -1
- package/dist/lib/core/natives/text.js +3 -0
- package/dist/lib/host/browser.js +951 -58
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools.d.ts +23 -0
- package/dist/lib/host/devtools.js +626 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +29 -0
- package/dist/lib/host/index.d.ts +2 -0
- package/dist/lib/host/index.js +3 -1
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/markdown.d.ts +2 -0
- package/dist/lib/host/markdown.js +10 -0
- package/dist/lib/host/modules.d.ts +4 -0
- package/dist/lib/host/modules.js +39 -1
- package/dist/lib/host/net.js +9 -1
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/rows.d.ts +14 -0
- package/dist/lib/host/rows.js +29 -0
- package/dist/lib/host/scheduler.d.ts +6 -0
- package/dist/lib/host/scheduler.js +60 -36
- package/dist/lib/host/theme.js +207 -2
- package/dist/lib/ui/i18n.trb +6 -0
- package/dist/lib/ui/test.trb +565 -0
- package/dist/lib/ui/ui.trb +523 -82
- package/dist/trebol.js +1346 -124
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +7 -3
- package/src/core/boot.ts +1 -1
- package/src/core/func.ts +4 -0
- package/src/core/natives/control.ts +1 -1
- package/src/core/natives/series.ts +9 -1
- package/src/core/natives/text.ts +2 -0
- package/src/host/browser.ts +962 -51
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools.ts +641 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +39 -0
- package/src/host/index.ts +3 -1
- package/src/host/js.ts +127 -0
- package/src/host/markdown.ts +9 -0
- package/src/host/modules.ts +33 -1
- package/src/host/net.ts +9 -1
- package/src/host/orbit.ts +432 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +62 -36
- package/src/host/theme.ts +207 -2
- package/src/ui/i18n.trb +6 -0
- package/src/ui/test.trb +565 -0
- package/src/ui/ui.trb +523 -82
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Cell, ObjectCell } from '../core/index.ts';
|
|
2
|
+
export declare const devLog: {
|
|
3
|
+
/** The host ran a UI event function (ui-click, ui-input...) on a face. */
|
|
4
|
+
event: (_fn: string, _face: ObjectCell, _args: Cell[]) => void;
|
|
5
|
+
/** A request ended: its status, or null and why it failed. */
|
|
6
|
+
request: (_verb: string, _url: string, _status: number | null, _ms: number, _problem: string | null) => void;
|
|
7
|
+
/** A toast, an error nobody caught or a warning (a cycle of reactions). */
|
|
8
|
+
note: (_kind: "toast" | "error" | "warning", _text: string) => void;
|
|
9
|
+
/** The faces on the page were repainted. */
|
|
10
|
+
repainted: () => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// What the app does, told to the dev tools of trebol dev (devtools.ts). Each
|
|
2
|
+
// hook does nothing until the dev tools start and set it, so the host calls
|
|
3
|
+
// them freely: in a built page, in tests and in Node they are never set.
|
|
4
|
+
export const devLog = {
|
|
5
|
+
/** The host ran a UI event function (ui-click, ui-input...) on a face. */
|
|
6
|
+
event: (_fn, _face, _args) => { },
|
|
7
|
+
/** A request ended: its status, or null and why it failed. */
|
|
8
|
+
request: (_verb, _url, _status, _ms, _problem) => { },
|
|
9
|
+
/** A toast, an error nobody caught or a warning (a cycle of reactions). */
|
|
10
|
+
note: (_kind, _text) => { },
|
|
11
|
+
/** The faces on the page were repainted. */
|
|
12
|
+
repainted: () => { },
|
|
13
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ObjectCell, type Runtime } from '../core/index.ts';
|
|
2
|
+
import { type Host } from './index.ts';
|
|
3
|
+
/** What the dev tools need from the browser host. */
|
|
4
|
+
export interface DevSide {
|
|
5
|
+
host: Host;
|
|
6
|
+
/** The folder trebol dev serves, to link a face to its file; null if unknown. */
|
|
7
|
+
root: string | null;
|
|
8
|
+
/** The face whose own node this is, if any. */
|
|
9
|
+
faceOf(e: Element): ObjectCell | null;
|
|
10
|
+
/** The face of a node or of the nearest node around it that is one. */
|
|
11
|
+
faceAt(e: Element): {
|
|
12
|
+
face: ObjectCell;
|
|
13
|
+
el: HTMLElement;
|
|
14
|
+
} | null;
|
|
15
|
+
/** The node of a face on the page, if it is on the page. */
|
|
16
|
+
nodeOf(face: ObjectCell): HTMLElement | null;
|
|
17
|
+
}
|
|
18
|
+
/** How a face is named in the tree, the log and errors: its style, name and text (`field email "Email"`). */
|
|
19
|
+
export declare function faceRef(face: ObjectCell, rt: Runtime): string;
|
|
20
|
+
/** Starts the dev tools on a page served by trebol dev; not in a frame (the documentation's examples). */
|
|
21
|
+
export declare function startDevtools(s: DevSide): void;
|
|
22
|
+
/** Keys typed in the panel are not the app's: its shortcuts and on-key leave them. */
|
|
23
|
+
export declare function inDevtools(target: EventTarget | null): boolean;
|
|
@@ -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
|
+
}
|