trebol 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -1
- package/dist/cli.js +17097 -12238
- 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 +12 -8
- package/dist/lib/core/eval.js +2 -2
- package/dist/lib/core/func.d.ts +4 -0
- package/dist/lib/core/machine/checks.d.ts +1 -0
- package/dist/lib/core/machine/checks.js +250 -0
- package/dist/lib/core/machine/events.d.ts +1 -0
- package/dist/lib/core/machine/events.js +365 -0
- package/dist/lib/core/machine/running.d.ts +1 -0
- package/dist/lib/core/machine/running.js +180 -0
- package/dist/lib/core/machine/shared.d.ts +1 -0
- package/dist/lib/core/machine/shared.js +123 -0
- package/dist/lib/core/machine/snapshot.d.ts +1 -0
- package/dist/lib/core/machine/snapshot.js +244 -0
- package/dist/lib/core/machine/spec.d.ts +1 -0
- package/dist/lib/core/machine/spec.js +274 -0
- package/dist/lib/core/machine/words.d.ts +1 -0
- package/dist/lib/core/machine/words.js +98 -0
- package/dist/lib/core/machine.d.ts +1 -0
- package/dist/lib/core/machine.js +17 -0
- package/dist/lib/core/mezz.js +31 -0
- package/dist/lib/core/natives/control.js +2 -2
- package/dist/lib/core/natives/data.js +6 -1
- package/dist/lib/core/natives/series.js +59 -9
- package/dist/lib/core/natives/text.js +3 -1
- package/dist/lib/host/browser/calendar.d.ts +34 -0
- package/dist/lib/host/browser/calendar.js +268 -0
- package/dist/lib/host/browser/count.d.ts +15 -0
- package/dist/lib/host/browser/count.js +48 -0
- package/dist/lib/host/browser/dialogs.d.ts +39 -0
- package/dist/lib/host/browser/dialogs.js +443 -0
- package/dist/lib/host/browser/drag.d.ts +70 -0
- package/dist/lib/host/browser/drag.js +268 -0
- package/dist/lib/host/browser/drawings.d.ts +30 -0
- package/dist/lib/host/browser/drawings.js +176 -0
- package/dist/lib/host/browser/events.d.ts +16 -0
- package/dist/lib/host/browser/events.js +125 -0
- package/dist/lib/host/browser/faces.d.ts +35 -0
- package/dist/lib/host/browser/faces.js +131 -0
- package/dist/lib/host/browser/fields.d.ts +16 -0
- package/dist/lib/host/browser/fields.js +322 -0
- package/dist/lib/host/browser/keys.d.ts +29 -0
- package/dist/lib/host/browser/keys.js +151 -0
- package/dist/lib/host/browser/lookup.d.ts +25 -0
- package/dist/lib/host/browser/lookup.js +275 -0
- package/dist/lib/host/browser/menus.d.ts +20 -0
- package/dist/lib/host/browser/menus.js +149 -0
- package/dist/lib/host/browser/mount.d.ts +92 -0
- package/dist/lib/host/browser/mount.js +448 -0
- package/dist/lib/host/browser/orbits.d.ts +90 -0
- package/dist/lib/host/browser/orbits.js +379 -0
- package/dist/lib/host/browser/page.d.ts +13 -0
- package/dist/lib/host/browser/page.js +35 -0
- package/dist/lib/host/browser/richtext.d.ts +19 -0
- package/dist/lib/host/browser/richtext.js +379 -0
- package/dist/lib/host/browser/row-faces.d.ts +5 -0
- package/dist/lib/host/browser/row-faces.js +74 -0
- package/dist/lib/host/browser/screens.d.ts +37 -0
- package/dist/lib/host/browser/screens.js +140 -0
- package/dist/lib/host/browser/swipe.d.ts +31 -0
- package/dist/lib/host/browser/swipe.js +178 -0
- package/dist/lib/host/browser/sync.d.ts +24 -0
- package/dist/lib/host/browser/sync.js +450 -0
- package/dist/lib/host/browser/tables.d.ts +65 -0
- package/dist/lib/host/browser/tables.js +505 -0
- package/dist/lib/host/browser/times.d.ts +14 -0
- package/dist/lib/host/browser/times.js +114 -0
- package/dist/lib/host/browser/toolbars.d.ts +17 -0
- package/dist/lib/host/browser/toolbars.js +129 -0
- package/dist/lib/host/browser/trees.d.ts +20 -0
- package/dist/lib/host/browser/trees.js +235 -0
- package/dist/lib/host/browser/uploads.d.ts +29 -0
- package/dist/lib/host/browser/uploads.js +370 -0
- package/dist/lib/host/browser/widget.d.ts +13 -0
- package/dist/lib/host/browser/widget.js +105 -0
- package/dist/lib/host/browser.d.ts +25 -0
- package/dist/lib/host/browser.js +129 -3964
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools/console.d.ts +9 -0
- package/dist/lib/host/devtools/console.js +104 -0
- package/dist/lib/host/devtools/faces.d.ts +25 -0
- package/dist/lib/host/devtools/faces.js +169 -0
- package/dist/lib/host/devtools/log.d.ts +3 -0
- package/dist/lib/host/devtools/log.js +32 -0
- package/dist/lib/host/devtools/machines.d.ts +34 -0
- package/dist/lib/host/devtools/machines.js +212 -0
- package/dist/lib/host/devtools/read.d.ts +13 -0
- package/dist/lib/host/devtools/read.js +52 -0
- package/dist/lib/host/devtools/state.d.ts +90 -0
- package/dist/lib/host/devtools/state.js +123 -0
- package/dist/lib/host/devtools.d.ts +19 -0
- package/dist/lib/host/devtools.js +238 -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 +30 -0
- package/dist/lib/host/icons.js +4 -0
- package/dist/lib/host/index.d.ts +5 -1
- package/dist/lib/host/index.js +110 -5
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/locale.d.ts +18 -0
- package/dist/lib/host/locale.js +75 -5
- package/dist/lib/host/markdown.d.ts +6 -0
- package/dist/lib/host/markdown.js +18 -0
- package/dist/lib/host/modules.d.ts +4 -0
- package/dist/lib/host/modules.js +157 -20
- package/dist/lib/host/motion.d.ts +32 -0
- package/dist/lib/host/motion.js +190 -0
- package/dist/lib/host/net.js +14 -3
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/palette.d.ts +17 -0
- package/dist/lib/host/palette.js +131 -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 +8 -0
- package/dist/lib/host/scheduler.js +64 -36
- package/dist/lib/host/share.d.ts +31 -0
- package/dist/lib/host/share.js +134 -0
- package/dist/lib/host/theme/classic.d.ts +1 -0
- package/dist/lib/host/theme/classic.js +120 -0
- package/dist/lib/host/theme/cupertino.d.ts +1 -0
- package/dist/lib/host/theme/cupertino.js +65 -0
- package/dist/lib/host/theme/data.d.ts +1 -0
- package/dist/lib/host/theme/data.js +109 -0
- package/dist/lib/host/theme/devtools.d.ts +1 -0
- package/dist/lib/host/theme/devtools.js +166 -0
- package/dist/lib/host/theme/dialogs.d.ts +1 -0
- package/dist/lib/host/theme/dialogs.js +63 -0
- package/dist/lib/host/theme/fluent.d.ts +1 -0
- package/dist/lib/host/theme/fluent.js +64 -0
- package/dist/lib/host/theme/forms.d.ts +1 -0
- package/dist/lib/host/theme/forms.js +239 -0
- package/dist/lib/host/theme/fusion.d.ts +1 -0
- package/dist/lib/host/theme/fusion.js +66 -0
- package/dist/lib/host/theme/layout.d.ts +1 -0
- package/dist/lib/host/theme/layout.js +166 -0
- package/dist/lib/host/theme/media.d.ts +1 -0
- package/dist/lib/host/theme/media.js +106 -0
- package/dist/lib/host/theme/navigation.d.ts +1 -0
- package/dist/lib/host/theme/navigation.js +36 -0
- package/dist/lib/host/theme/panes.d.ts +1 -0
- package/dist/lib/host/theme/panes.js +99 -0
- package/dist/lib/host/theme/popups.d.ts +1 -0
- package/dist/lib/host/theme/popups.js +149 -0
- package/dist/lib/host/theme/responsive.d.ts +1 -0
- package/dist/lib/host/theme/responsive.js +103 -0
- package/dist/lib/host/theme/toasts.d.ts +1 -0
- package/dist/lib/host/theme/toasts.js +62 -0
- package/dist/lib/host/theme.js +19 -1179
- package/dist/lib/ui/dialect/events.trb +276 -0
- package/dist/lib/ui/dialect/faces.trb +135 -0
- package/dist/lib/ui/dialect/layout.trb +283 -0
- package/dist/lib/ui/dialect/palette.trb +87 -0
- package/dist/lib/ui/dialect/problems.trb +174 -0
- package/dist/lib/ui/dialect/reactions.trb +198 -0
- package/dist/lib/ui/dialect/rows.trb +208 -0
- package/dist/lib/ui/dialect/screens.trb +293 -0
- package/dist/lib/ui/dialect/steps.trb +203 -0
- package/dist/lib/ui/dialect/validation.trb +137 -0
- package/dist/lib/ui/dialect/view.trb +110 -0
- package/dist/lib/ui/dialect/walking.trb +98 -0
- package/dist/lib/ui/dialect/words.trb +70 -0
- package/dist/lib/ui/i18n.trb +23 -0
- package/dist/lib/ui/test.trb +12 -0
- package/dist/lib/ui/testing/actions.trb +421 -0
- package/dist/lib/ui/testing/faces.trb +148 -0
- package/dist/lib/ui/testing/server.trb +73 -0
- package/dist/lib/ui/ui.trb +25 -1234
- package/dist/trebol.js +4882 -1088
- 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 +10 -7
- package/src/core/eval.ts +2 -2
- package/src/core/func.ts +4 -0
- package/src/core/machine/checks.ts +251 -0
- package/src/core/machine/events.ts +366 -0
- package/src/core/machine/running.ts +181 -0
- package/src/core/machine/shared.ts +124 -0
- package/src/core/machine/snapshot.ts +245 -0
- package/src/core/machine/spec.ts +275 -0
- package/src/core/machine/words.ts +99 -0
- package/src/core/machine.ts +20 -0
- package/src/core/mezz.ts +31 -0
- package/src/core/natives/control.ts +2 -2
- package/src/core/natives/data.ts +6 -1
- package/src/core/natives/parse.ts +1 -1
- package/src/core/natives/series.ts +55 -11
- package/src/core/natives/text.ts +2 -1
- package/src/host/browser/calendar.ts +264 -0
- package/src/host/browser/count.ts +48 -0
- package/src/host/browser/dialogs.ts +439 -0
- package/src/host/browser/drag.ts +278 -0
- package/src/host/browser/drawings.ts +178 -0
- package/src/host/browser/events.ts +117 -0
- package/src/host/browser/faces.ts +143 -0
- package/src/host/browser/fields.ts +315 -0
- package/src/host/browser/keys.ts +145 -0
- package/src/host/browser/lookup.ts +262 -0
- package/src/host/browser/menus.ts +146 -0
- package/src/host/browser/mount.ts +420 -0
- package/src/host/browser/orbits.ts +418 -0
- package/src/host/browser/page.ts +43 -0
- package/src/host/browser/richtext.ts +346 -0
- package/src/host/browser/row-faces.ts +75 -0
- package/src/host/browser/screens.ts +142 -0
- package/src/host/browser/swipe.ts +168 -0
- package/src/host/browser/sync.ts +354 -0
- package/src/host/browser/tables.ts +461 -0
- package/src/host/browser/times.ts +115 -0
- package/src/host/browser/toolbars.ts +126 -0
- package/src/host/browser/trees.ts +205 -0
- package/src/host/browser/uploads.ts +367 -0
- package/src/host/browser/widget.ts +103 -0
- package/src/host/browser.ts +128 -3747
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools/console.ts +102 -0
- package/src/host/devtools/faces.ts +160 -0
- package/src/host/devtools/log.ts +34 -0
- package/src/host/devtools/machines.ts +208 -0
- package/src/host/devtools/read.ts +58 -0
- package/src/host/devtools/state.ts +148 -0
- package/src/host/devtools.ts +238 -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 +40 -0
- package/src/host/icons.ts +4 -0
- package/src/host/index.ts +110 -5
- package/src/host/js.ts +127 -0
- package/src/host/locale.ts +71 -5
- package/src/host/markdown.ts +18 -0
- package/src/host/modules.ts +150 -22
- package/src/host/motion.ts +204 -0
- package/src/host/net.ts +13 -3
- package/src/host/orbit.ts +432 -0
- package/src/host/palette.ts +139 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +67 -36
- package/src/host/share.ts +168 -0
- package/src/host/theme/classic.ts +121 -0
- package/src/host/theme/cupertino.ts +66 -0
- package/src/host/theme/data.ts +110 -0
- package/src/host/theme/devtools.ts +167 -0
- package/src/host/theme/dialogs.ts +64 -0
- package/src/host/theme/fluent.ts +65 -0
- package/src/host/theme/forms.ts +240 -0
- package/src/host/theme/fusion.ts +67 -0
- package/src/host/theme/layout.ts +167 -0
- package/src/host/theme/media.ts +107 -0
- package/src/host/theme/navigation.ts +37 -0
- package/src/host/theme/panes.ts +100 -0
- package/src/host/theme/popups.ts +150 -0
- package/src/host/theme/responsive.ts +104 -0
- package/src/host/theme/toasts.ts +63 -0
- package/src/host/theme.ts +19 -1179
- package/src/ui/dialect/events.trb +276 -0
- package/src/ui/dialect/faces.trb +135 -0
- package/src/ui/dialect/layout.trb +283 -0
- package/src/ui/dialect/palette.trb +87 -0
- package/src/ui/dialect/problems.trb +174 -0
- package/src/ui/dialect/reactions.trb +198 -0
- package/src/ui/dialect/rows.trb +208 -0
- package/src/ui/dialect/screens.trb +293 -0
- package/src/ui/dialect/steps.trb +203 -0
- package/src/ui/dialect/validation.trb +137 -0
- package/src/ui/dialect/view.trb +110 -0
- package/src/ui/dialect/walking.trb +98 -0
- package/src/ui/dialect/words.trb +70 -0
- package/src/ui/i18n.trb +23 -0
- package/src/ui/test.trb +12 -0
- package/src/ui/testing/actions.trb +421 -0
- package/src/ui/testing/faces.trb +148 -0
- package/src/ui/testing/server.trb +73 -0
- package/src/ui/ui.trb +25 -1234
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
// --- dialogs, tabs and toasts --------------------------------------------------------
|
|
2
|
+
|
|
3
|
+
import { K, NONE, TRUE, FALSE, native, value, intern, spell, text, UNSET, throwError, type Cell, type ObjectCell, type WordCell, type StringCell } from '../../core/index.ts';
|
|
4
|
+
import { spawn } from '../scheduler.ts';
|
|
5
|
+
import { iconNode } from '../icons.ts';
|
|
6
|
+
import { devLog } from '../devlog.ts';
|
|
7
|
+
import { get, str, own, keepOwn, flag, word, children } from './faces.ts';
|
|
8
|
+
import { type Mounted, host, byCtx, newId, fire, el, build } from './mount.ts';
|
|
9
|
+
import { int } from './drag.ts';
|
|
10
|
+
import { here, setHere } from './screens.ts';
|
|
11
|
+
import { rootFace } from './page.ts';
|
|
12
|
+
|
|
13
|
+
export function buildDialog(face: ObjectCell, m: Mounted): HTMLElement {
|
|
14
|
+
const d = el('dialog', 'tb-dialog');
|
|
15
|
+
const head = el('header', 'tb-dialog-head');
|
|
16
|
+
const title = el('h2', 'tb-dialog-title');
|
|
17
|
+
title.id = `tb-${newId()}`;
|
|
18
|
+
d.setAttribute('aria-labelledby', title.id);
|
|
19
|
+
const x = el('button', 'tb-dialog-close');
|
|
20
|
+
x.type = 'button';
|
|
21
|
+
keepOwn(x, 'close', 'aria-label');
|
|
22
|
+
x.textContent = '×';
|
|
23
|
+
x.addEventListener('click', () => void fire('ui-close', face));
|
|
24
|
+
head.append(title, x);
|
|
25
|
+
const body = el('div', 'tb-dialog-body');
|
|
26
|
+
d.append(head, body);
|
|
27
|
+
// Esc: let the face decide, so code that keeps it open still wins
|
|
28
|
+
d.addEventListener('cancel', ev => {
|
|
29
|
+
ev.preventDefault();
|
|
30
|
+
void fire('ui-close', face);
|
|
31
|
+
});
|
|
32
|
+
m.label = title;
|
|
33
|
+
m.box = body;
|
|
34
|
+
return d;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function syncDialog(m: Mounted): void {
|
|
38
|
+
const d = m.el as HTMLDialogElement;
|
|
39
|
+
m.label!.textContent = str(m.face, 'text') ?? '';
|
|
40
|
+
const open = !flag(m.face, 'hidden');
|
|
41
|
+
if (open === d.open) return;
|
|
42
|
+
if (!d.isConnected) {
|
|
43
|
+
queueMicrotask(() => { if (d.isConnected) syncDialog(m); });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!open) return d.close();
|
|
47
|
+
d.showModal();
|
|
48
|
+
// after this repaint, which never writes into a focused input
|
|
49
|
+
queueMicrotask(() => d.querySelector<HTMLElement>('input, textarea, select')?.focus());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function buildTabs(face: ObjectCell, m: Mounted): HTMLElement {
|
|
53
|
+
const node = el('div', 'tb-tabs');
|
|
54
|
+
const list = el('div', 'tb-tablist');
|
|
55
|
+
list.setAttribute('role', 'tablist');
|
|
56
|
+
node.append(list);
|
|
57
|
+
const tabs = children(face);
|
|
58
|
+
const pick = (i: number): void => {
|
|
59
|
+
void fire('ui-input', face, { t: K.Integer, v: i + 1 });
|
|
60
|
+
(list.children[i] as HTMLElement | undefined)?.focus();
|
|
61
|
+
};
|
|
62
|
+
tabs.forEach((t, i) => {
|
|
63
|
+
const panel = build(t);
|
|
64
|
+
const b = el('button', 'tb-tab');
|
|
65
|
+
b.type = 'button';
|
|
66
|
+
b.id = `tb-${newId()}`;
|
|
67
|
+
panel.id = `${b.id}-panel`;
|
|
68
|
+
b.setAttribute('role', 'tab');
|
|
69
|
+
b.setAttribute('aria-controls', panel.id);
|
|
70
|
+
panel.setAttribute('aria-labelledby', b.id);
|
|
71
|
+
b.textContent = str(t, 'text') ?? '';
|
|
72
|
+
b.addEventListener('click', () => pick(i));
|
|
73
|
+
const tm = byCtx.get(t.ctx)!;
|
|
74
|
+
tm.button = b;
|
|
75
|
+
list.append(b);
|
|
76
|
+
node.append(panel);
|
|
77
|
+
});
|
|
78
|
+
list.addEventListener('keydown', ev => {
|
|
79
|
+
const at = Array.prototype.indexOf.call(list.children, document.activeElement);
|
|
80
|
+
const n = tabs.length;
|
|
81
|
+
const to = ev.key === 'ArrowRight' ? (at + 1) % n : ev.key === 'ArrowLeft' ? (at - 1 + n) % n
|
|
82
|
+
: ev.key === 'Home' ? 0 : ev.key === 'End' ? n - 1 : -1;
|
|
83
|
+
if (to < 0 || at < 0) return;
|
|
84
|
+
ev.preventDefault();
|
|
85
|
+
pick(to);
|
|
86
|
+
});
|
|
87
|
+
m.box = list;
|
|
88
|
+
return node;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function syncTabs(m: Mounted): void {
|
|
92
|
+
const v = get(m.face, 'value');
|
|
93
|
+
const tabs = children(m.face);
|
|
94
|
+
const at = v.t === K.Integer ? Math.min(Math.max(Number(v.v), 1), tabs.length) - 1 : 0;
|
|
95
|
+
tabs.forEach((t, i) => {
|
|
96
|
+
const tm = byCtx.get(t.ctx);
|
|
97
|
+
if (tm === undefined || tm.button === null) return;
|
|
98
|
+
const on = i === at;
|
|
99
|
+
tm.button.setAttribute('aria-selected', on ? 'true' : 'false');
|
|
100
|
+
tm.button.tabIndex = on ? 0 : -1;
|
|
101
|
+
tm.el.hidden = !on;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// A form in steps shows one step at a time under a bar of their titles; the steps
|
|
106
|
+
// done can be chosen again. Each step the user moves on to is an entry in the
|
|
107
|
+
// history, so the browser's Back returns to the step before.
|
|
108
|
+
export interface StepsState {
|
|
109
|
+
steps: { face: ObjectCell; li: HTMLLIElement; button: HTMLButtonElement; num: HTMLElement; title: HTMLElement }[];
|
|
110
|
+
count: HTMLElement;
|
|
111
|
+
/** The history entries of this face: the depth of each and the step it shows. */
|
|
112
|
+
trail: { depth: number; step: number }[];
|
|
113
|
+
shown: number;
|
|
114
|
+
}
|
|
115
|
+
export const stepsState = new WeakMap<Mounted, StepsState>();
|
|
116
|
+
export const mountedSteps = new Set<Mounted>();
|
|
117
|
+
|
|
118
|
+
export function buildSteps(face: ObjectCell, m: Mounted): HTMLElement {
|
|
119
|
+
const node = el('div', 'tb-steps');
|
|
120
|
+
const bar = el('ol', 'tb-steps-bar');
|
|
121
|
+
keepOwn(bar, 'steps', 'aria-label');
|
|
122
|
+
const count = el('p', 'tb-steps-count');
|
|
123
|
+
node.append(bar, count);
|
|
124
|
+
const v = get(face, 'value');
|
|
125
|
+
const st: StepsState = { steps: [], count, trail: [{ depth: here.depth, step: v.t === K.Integer ? Number(v.v) : 1 }], shown: 0 };
|
|
126
|
+
let nav: ObjectCell | null = null;
|
|
127
|
+
children(face).forEach((c, i) => {
|
|
128
|
+
if (word(c, 'style') !== 'step') {
|
|
129
|
+
nav = c;
|
|
130
|
+
const n = build(c);
|
|
131
|
+
n.classList.add('tb-steps-nav');
|
|
132
|
+
node.append(n);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const li = el('li');
|
|
136
|
+
const b = el('button', 'tb-steps-item');
|
|
137
|
+
b.type = 'button';
|
|
138
|
+
const num = el('span', 'tb-steps-num');
|
|
139
|
+
const title = el('span', 'tb-steps-title');
|
|
140
|
+
b.append(num, title);
|
|
141
|
+
b.addEventListener('click', () => void fire('ui-step-go', face, int(i + 1)));
|
|
142
|
+
li.append(b);
|
|
143
|
+
bar.append(li);
|
|
144
|
+
node.append(build(c));
|
|
145
|
+
st.steps.push({ face: c, li, button: b, num, title });
|
|
146
|
+
});
|
|
147
|
+
// Enter in a field goes to the next step; on the last one it sends the form
|
|
148
|
+
node.addEventListener('keydown', ev => {
|
|
149
|
+
if (ev.key !== 'Enter' || ev.defaultPrevented || !(ev.target instanceof HTMLInputElement) || !inTextBox(ev.target)) return;
|
|
150
|
+
const next = nav === null ? undefined : children(nav)[1];
|
|
151
|
+
if (next === undefined || flag(next, 'hidden')) return;
|
|
152
|
+
ev.preventDefault();
|
|
153
|
+
byCtx.get(next.ctx)?.el.click();
|
|
154
|
+
});
|
|
155
|
+
stepsState.set(m, st);
|
|
156
|
+
mountedSteps.add(m);
|
|
157
|
+
return node;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function syncSteps(m: Mounted): void {
|
|
161
|
+
const st = stepsState.get(m);
|
|
162
|
+
if (st === undefined) return;
|
|
163
|
+
const v = get(m.face, 'value');
|
|
164
|
+
const at = v.t === K.Integer ? Number(v.v) : 1;
|
|
165
|
+
const live = st.steps.filter(s => !flag(s.face, 'hidden'));
|
|
166
|
+
const k = live.findIndex(s => s === st.steps[at - 1]);
|
|
167
|
+
st.steps.forEach((s, i) => {
|
|
168
|
+
const j = live.indexOf(s);
|
|
169
|
+
const title = str(s.face, 'text') ?? '';
|
|
170
|
+
s.li.hidden = j < 0;
|
|
171
|
+
s.li.classList.toggle('tb-done', j >= 0 && j < k);
|
|
172
|
+
s.li.classList.toggle('tb-current', j === k);
|
|
173
|
+
s.num.textContent = String(j + 1);
|
|
174
|
+
s.title.textContent = title;
|
|
175
|
+
s.button.disabled = j < 0 || j >= k;
|
|
176
|
+
if (j === k) s.button.setAttribute('aria-current', 'step');
|
|
177
|
+
else s.button.removeAttribute('aria-current');
|
|
178
|
+
const panel = byCtx.get(s.face.ctx)?.el;
|
|
179
|
+
if (panel === undefined) return;
|
|
180
|
+
panel.hidden = i !== at - 1;
|
|
181
|
+
panel.setAttribute('aria-label', title);
|
|
182
|
+
});
|
|
183
|
+
st.count.textContent = k < 0 ? '' : `${own('step-of', String(k + 1), String(live.length))} · ${str(live[k]!.face, 'text') ?? ''}`;
|
|
184
|
+
if (st.shown !== 0 && st.shown !== at) {
|
|
185
|
+
stepHistory(st, at, st.shown);
|
|
186
|
+
const panel = byCtx.get(st.steps[at - 1]!.face.ctx)?.el;
|
|
187
|
+
if (m.el.getBoundingClientRect().top < 0) m.el.scrollIntoView({ block: 'start' });
|
|
188
|
+
panel?.focus({ preventScroll: true });
|
|
189
|
+
}
|
|
190
|
+
st.shown = at;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A new step on show: forward is a new history entry; back returns to the entry that showed it. */
|
|
194
|
+
export function stepHistory(st: StepsState, to: number, from: number): void {
|
|
195
|
+
const cur = st.trail.find(x => x.depth === here.depth);
|
|
196
|
+
// Back or Forward brought it: the history is already there
|
|
197
|
+
if (cur?.step === to) return;
|
|
198
|
+
if (to > from) {
|
|
199
|
+
setHere({ depth: here.depth + 1, top: here.depth + 1 });
|
|
200
|
+
st.trail = st.trail.filter(x => x.depth < here.depth);
|
|
201
|
+
st.trail.push({ depth: here.depth, step: to });
|
|
202
|
+
history.pushState({ ...here }, '');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const back = st.trail.filter(x => x.depth < here.depth && x.step === to).pop();
|
|
206
|
+
if (back !== undefined) history.go(back.depth - here.depth);
|
|
207
|
+
else if (cur !== undefined) cur.step = to;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Back or Forward landed on an entry a form in steps made: it shows that step again. */
|
|
211
|
+
export function stepsLanded(): void {
|
|
212
|
+
const d = (history.state as { depth?: number } | null)?.depth;
|
|
213
|
+
if (typeof d !== 'number') return;
|
|
214
|
+
for (const m of mountedSteps) {
|
|
215
|
+
if (!m.el.isConnected) {
|
|
216
|
+
mountedSteps.delete(m);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const hit = stepsState.get(m)!.trail.find(x => x.depth === d);
|
|
220
|
+
if (hit === undefined) continue;
|
|
221
|
+
setHere({ depth: d, top: here.top });
|
|
222
|
+
void fire('ui-step-go', m.face, int(hit.step));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// A split shows two panes side by side with a handle between them; on narrow
|
|
227
|
+
// screens it shows one pane, the one its value names, and the second pane
|
|
228
|
+
// gets a back button.
|
|
229
|
+
export function buildSplit(face: ObjectCell, m: Mounted): HTMLElement {
|
|
230
|
+
const node = el('div', 'tb-split');
|
|
231
|
+
const [a, b] = children(face);
|
|
232
|
+
const first = el('div', 'tb-split-pane');
|
|
233
|
+
const second = el('div', 'tb-split-pane');
|
|
234
|
+
first.tabIndex = second.tabIndex = -1;
|
|
235
|
+
const handle = el('div', 'tb-split-handle');
|
|
236
|
+
handle.tabIndex = 0;
|
|
237
|
+
handle.setAttribute('role', 'separator');
|
|
238
|
+
handle.setAttribute('aria-orientation', 'vertical');
|
|
239
|
+
keepOwn(handle, 'resize-panes', 'aria-label');
|
|
240
|
+
handle.setAttribute('aria-valuemin', '20');
|
|
241
|
+
handle.setAttribute('aria-valuemax', '80');
|
|
242
|
+
const resize = (pct: number): void => {
|
|
243
|
+
const p = Math.round(Math.min(80, Math.max(20, pct)));
|
|
244
|
+
node.style.setProperty('--tb-split', `${p}%`);
|
|
245
|
+
handle.setAttribute('aria-valuenow', String(p));
|
|
246
|
+
};
|
|
247
|
+
resize(40);
|
|
248
|
+
handle.addEventListener('pointerdown', ev => {
|
|
249
|
+
handle.setPointerCapture(ev.pointerId);
|
|
250
|
+
ev.preventDefault();
|
|
251
|
+
});
|
|
252
|
+
handle.addEventListener('pointermove', ev => {
|
|
253
|
+
if (!handle.hasPointerCapture(ev.pointerId)) return;
|
|
254
|
+
const r = node.getBoundingClientRect();
|
|
255
|
+
resize(((ev.clientX - r.left) / r.width) * 100);
|
|
256
|
+
});
|
|
257
|
+
handle.addEventListener('keydown', ev => {
|
|
258
|
+
const now = Number(handle.getAttribute('aria-valuenow'));
|
|
259
|
+
const to = ev.key === 'ArrowLeft' ? now - 5 : ev.key === 'ArrowRight' ? now + 5
|
|
260
|
+
: ev.key === 'Home' ? 20 : ev.key === 'End' ? 80 : null;
|
|
261
|
+
if (to === null) return;
|
|
262
|
+
ev.preventDefault();
|
|
263
|
+
resize(to);
|
|
264
|
+
});
|
|
265
|
+
const back = el('button', 'tb-button tb-split-back');
|
|
266
|
+
back.type = 'button';
|
|
267
|
+
const arrow = iconNode('arrow-left');
|
|
268
|
+
const caption = el('span');
|
|
269
|
+
keepOwn(caption, 'back');
|
|
270
|
+
back.append(...(arrow !== null ? [arrow] : []), caption);
|
|
271
|
+
back.addEventListener('click', () => void fire('ui-input', face, { t: K.Integer, v: 1 }));
|
|
272
|
+
if (a !== undefined) first.append(build(a));
|
|
273
|
+
second.append(back);
|
|
274
|
+
if (b !== undefined) second.append(build(b));
|
|
275
|
+
node.append(first, handle, second);
|
|
276
|
+
return node;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function syncSplit(m: Mounted): void {
|
|
280
|
+
const v = get(m.face, 'value');
|
|
281
|
+
const pane = v.t === K.Integer && Number(v.v) === 2 ? '2' : '1';
|
|
282
|
+
const was = m.el.dataset['pane'];
|
|
283
|
+
if (was === pane) return;
|
|
284
|
+
m.el.dataset['pane'] = pane;
|
|
285
|
+
// on narrow screens the other pane just appeared: take the focus there
|
|
286
|
+
if (was !== undefined && matchMedia('(max-width: 640px)').matches) {
|
|
287
|
+
const shown = m.el.children[pane === '2' ? 2 : 0] as HTMLElement;
|
|
288
|
+
(shown.querySelector<HTMLElement>('.tb-selected') ?? shown).focus();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export let toasts: HTMLElement | null = null;
|
|
293
|
+
/** The notices with an Undo button on show, by the id ui.trb gave them. */
|
|
294
|
+
export const undoToasts = new Map<number, HTMLElement>();
|
|
295
|
+
|
|
296
|
+
native('show-toast', '["Shows a short message at the edge of the screen." message [string!] kind [word! none!] undo [integer! none!] "With an Undo button: the id of what it undoes"]', (_t, f) => {
|
|
297
|
+
if (toasts === null) {
|
|
298
|
+
// a manual popover sits in the top layer, so toasts show above open dialogs
|
|
299
|
+
toasts = el('div', 'tb-toasts');
|
|
300
|
+
toasts.popover = 'manual';
|
|
301
|
+
toasts.setAttribute('aria-live', 'polite');
|
|
302
|
+
document.body.append(toasts);
|
|
303
|
+
}
|
|
304
|
+
const kind = f.args[1]!.t === K.Word ? spell((f.args[1] as WordCell).sym).toLowerCase() : null;
|
|
305
|
+
const id = f.args[2]!.t === K.Integer ? Number((f.args[2] as { v: number | bigint }).v) : null;
|
|
306
|
+
const t = el('div', kind !== null ? `tb-toast tb-${kind}` : 'tb-toast');
|
|
307
|
+
if (kind === 'danger') t.setAttribute('role', 'alert');
|
|
308
|
+
const message = text(f.args[0] as StringCell);
|
|
309
|
+
devLog.note('toast', kind !== null ? `${kind}: ${message}` : message);
|
|
310
|
+
const said = el('span', 'tb-toast-text');
|
|
311
|
+
t.append(said);
|
|
312
|
+
const box = toasts;
|
|
313
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
314
|
+
const dismiss = (): void => {
|
|
315
|
+
clearTimeout(timer);
|
|
316
|
+
t.remove();
|
|
317
|
+
if (box.childElementCount === 0 && box.matches(':popover-open')) box.hidePopover();
|
|
318
|
+
};
|
|
319
|
+
// gone without Undo: ui.trb runs what waited for it
|
|
320
|
+
const gone = (): void => {
|
|
321
|
+
dismiss();
|
|
322
|
+
if (id !== null && undoToasts.delete(id)) fireRoot('ui-toast-gone', id);
|
|
323
|
+
};
|
|
324
|
+
t.addEventListener('click', gone);
|
|
325
|
+
const wait = id !== null ? 8000 : kind === 'danger' ? 6000 : 4000;
|
|
326
|
+
if (id !== null) {
|
|
327
|
+
const undo = el('button', 'tb-toast-undo');
|
|
328
|
+
undo.type = 'button';
|
|
329
|
+
keepOwn(undo, 'undo');
|
|
330
|
+
undo.addEventListener('click', ev => {
|
|
331
|
+
ev.stopPropagation();
|
|
332
|
+
dismiss();
|
|
333
|
+
if (undoToasts.delete(id)) fireRoot('ui-toast-undo', id);
|
|
334
|
+
});
|
|
335
|
+
t.append(undo);
|
|
336
|
+
t.dataset.tbUndo = String(id);
|
|
337
|
+
undoToasts.set(id, t);
|
|
338
|
+
// while the pointer or the keyboard is on it, it waits: the user has time to read and choose
|
|
339
|
+
const hold = (): void => clearTimeout(timer);
|
|
340
|
+
const resume = (): void => {
|
|
341
|
+
if (!t.matches(':hover') && !t.contains(document.activeElement)) timer = setTimeout(gone, wait);
|
|
342
|
+
};
|
|
343
|
+
t.addEventListener('pointerenter', hold);
|
|
344
|
+
t.addEventListener('focusin', hold);
|
|
345
|
+
t.addEventListener('pointerleave', resume);
|
|
346
|
+
t.addEventListener('focusout', () => setTimeout(resume));
|
|
347
|
+
}
|
|
348
|
+
timer = setTimeout(gone, wait);
|
|
349
|
+
if (box.matches(':popover-open')) box.hidePopover();
|
|
350
|
+
box.append(t);
|
|
351
|
+
box.showPopover();
|
|
352
|
+
// screen readers announce text that changes inside a live region already shown
|
|
353
|
+
setTimeout(() => { said.textContent = message; }, 50);
|
|
354
|
+
return value(f.args[0]!);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
native('hide-toast', '["Takes away a notice with an Undo button, by its id." id [integer!]]', (_t, f) => {
|
|
358
|
+
const id = Number((f.args[0] as { v: number | bigint }).v);
|
|
359
|
+
const t = undoToasts.get(id);
|
|
360
|
+
if (t !== undefined) {
|
|
361
|
+
undoToasts.delete(id);
|
|
362
|
+
const box = t.parentElement;
|
|
363
|
+
t.remove();
|
|
364
|
+
if (box !== null && box.childElementCount === 0 && box.matches(':popover-open')) box.hidePopover();
|
|
365
|
+
}
|
|
366
|
+
return value(UNSET);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
/** A box where Ctrl+Z undoes typing: a text input, a text area or rich text. */
|
|
370
|
+
export function inTextBox(target: EventTarget | null): boolean {
|
|
371
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
372
|
+
if (target.isContentEditable || target instanceof HTMLTextAreaElement) return true;
|
|
373
|
+
return target instanceof HTMLInputElement && !['button', 'checkbox', 'radio', 'range', 'color', 'file', 'submit', 'reset'].includes(target.type);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Runs a function of ui.trb for the page, not for one face: the view on show is its face. */
|
|
377
|
+
export function fireRoot(fn: string, id: number): void {
|
|
378
|
+
const n = int(id);
|
|
379
|
+
if (rootFace !== null) void fire(fn, rootFace, n);
|
|
380
|
+
else spawn(host.sch, { data: [{ t: K.Word, sym: intern(fn), bind: host.rt.lib }, NONE, n] }, 0);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
native('show-confirm', '["Asks a question in a small dialog; true when the user says yes." question [string!] yes [string! none!] no [string! none!] kind [word! none!]]', (_t, f, v) => {
|
|
384
|
+
if (f.phase === 1) return value(v!);
|
|
385
|
+
f.phase = 1;
|
|
386
|
+
const answer = (at: number, key: string): string => f.args[at]!.t === K.String ? text(f.args[at] as StringCell) : own(key);
|
|
387
|
+
const danger = f.args[3]!.t === K.Word && spell((f.args[3] as WordCell).sym).toLowerCase() === 'danger';
|
|
388
|
+
const d = el('dialog', 'tb-dialog tb-confirm');
|
|
389
|
+
d.setAttribute('role', 'alertdialog');
|
|
390
|
+
const body = el('div', 'tb-dialog-body');
|
|
391
|
+
const q = el('p', 'tb-confirm-question');
|
|
392
|
+
q.id = `tb-${newId()}`;
|
|
393
|
+
q.textContent = text(f.args[0] as StringCell);
|
|
394
|
+
d.setAttribute('aria-labelledby', q.id);
|
|
395
|
+
const row = el('div', 'tb-row tb-right');
|
|
396
|
+
const no = el('button', 'tb-button');
|
|
397
|
+
const yes = el('button', danger ? 'tb-button tb-danger' : 'tb-button tb-primary');
|
|
398
|
+
no.type = yes.type = 'button';
|
|
399
|
+
no.textContent = answer(2, 'no');
|
|
400
|
+
yes.textContent = answer(1, 'yes');
|
|
401
|
+
row.append(no, yes);
|
|
402
|
+
body.append(q, row);
|
|
403
|
+
d.append(body);
|
|
404
|
+
const p = new Promise<Cell>(resolve => {
|
|
405
|
+
const done = (ok: boolean): void => {
|
|
406
|
+
d.close();
|
|
407
|
+
d.remove();
|
|
408
|
+
resolve(ok ? TRUE : FALSE);
|
|
409
|
+
};
|
|
410
|
+
yes.addEventListener('click', () => done(true));
|
|
411
|
+
no.addEventListener('click', () => done(false));
|
|
412
|
+
// Esc is no
|
|
413
|
+
d.addEventListener('cancel', ev => { ev.preventDefault(); done(false); });
|
|
414
|
+
});
|
|
415
|
+
document.body.append(d);
|
|
416
|
+
// over an open dialog too: the top layer stacks dialogs in the order they open
|
|
417
|
+
d.showModal();
|
|
418
|
+
// what cannot be undone starts on no, so Enter does not do it by mistake
|
|
419
|
+
(danger ? no : yes).focus();
|
|
420
|
+
return { k: 'await', p };
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
native('print-faces', '["Opens the print dialog for the page, or for one face only." face [object! none!]]', (_t, f) => {
|
|
424
|
+
const target = f.args[0]!.t === K.Object ? byCtx.get((f.args[0] as ObjectCell).ctx)?.el : undefined;
|
|
425
|
+
if (f.args[0]!.t === K.Object && (target === undefined || !target.isConnected)) {
|
|
426
|
+
return throwError('script', 'not-shown', 'print-page/only needs a face on the screen');
|
|
427
|
+
}
|
|
428
|
+
if (target !== undefined) {
|
|
429
|
+
target.classList.add('tb-print-target');
|
|
430
|
+
document.documentElement.classList.add('tb-print-only');
|
|
431
|
+
addEventListener('afterprint', () => {
|
|
432
|
+
target.classList.remove('tb-print-target');
|
|
433
|
+
document.documentElement.classList.remove('tb-print-only');
|
|
434
|
+
}, { once: true });
|
|
435
|
+
}
|
|
436
|
+
print();
|
|
437
|
+
return value(UNSET);
|
|
438
|
+
});
|
|
439
|
+
|