trebol 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/cli.js +10040 -7519
- package/dist/lib/core/boot.js +10 -7
- package/dist/lib/core/eval.js +2 -2
- 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 +1 -1
- package/dist/lib/core/natives/data.js +6 -1
- package/dist/lib/core/natives/series.js +49 -9
- package/dist/lib/core/natives/text.js +0 -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 +84 -4812
- 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 +15 -19
- package/dist/lib/host/devtools.js +39 -427
- package/dist/lib/host/fonts.js +4 -3
- package/dist/lib/host/icons.js +4 -0
- package/dist/lib/host/index.d.ts +3 -1
- package/dist/lib/host/index.js +107 -4
- package/dist/lib/host/locale.d.ts +18 -0
- package/dist/lib/host/locale.js +75 -5
- package/dist/lib/host/markdown.d.ts +4 -0
- package/dist/lib/host/markdown.js +8 -0
- package/dist/lib/host/modules.js +118 -19
- package/dist/lib/host/motion.d.ts +32 -0
- package/dist/lib/host/motion.js +190 -0
- package/dist/lib/host/net.js +5 -2
- package/dist/lib/host/palette.d.ts +17 -0
- package/dist/lib/host/palette.js +131 -0
- package/dist/lib/host/scheduler.d.ts +2 -0
- package/dist/lib/host/scheduler.js +4 -0
- 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 -1384
- 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 +17 -0
- package/dist/lib/ui/test.trb +3 -556
- 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 -1675
- package/dist/trebol.js +4463 -1891
- package/package.json +1 -1
- package/src/core/boot.ts +9 -6
- package/src/core/eval.ts +2 -2
- 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 +1 -1
- package/src/core/natives/data.ts +6 -1
- package/src/core/natives/parse.ts +1 -1
- package/src/core/natives/series.ts +47 -11
- package/src/core/natives/text.ts +0 -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 +82 -4612
- 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 +39 -442
- package/src/host/fonts.ts +4 -3
- package/src/host/icons.ts +4 -0
- package/src/host/index.ts +107 -4
- package/src/host/locale.ts +71 -5
- package/src/host/markdown.ts +9 -0
- package/src/host/modules.ts +117 -21
- package/src/host/motion.ts +204 -0
- package/src/host/net.ts +4 -2
- package/src/host/palette.ts +139 -0
- package/src/host/scheduler.ts +5 -0
- 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 -1384
- 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 +17 -0
- package/src/ui/test.trb +3 -556
- 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 -1675
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// A lookup: a field where the user types and chooses among what on-search finds, as in
|
|
2
|
+
// the WAI-ARIA combobox. Only one list is open at a time, in the top layer as a calendar.
|
|
3
|
+
import { K, NONE, string, form } from "../../core/index.js";
|
|
4
|
+
import { mapAt, translate } from "../index.js";
|
|
5
|
+
import { linkHref } from "../markdown.js";
|
|
6
|
+
import { get, str, optionValue, optionText, optionsOf, own, flag, word, icon } from "./faces.js";
|
|
7
|
+
import { host, newId, fire, el } from "./mount.js";
|
|
8
|
+
import { int } from "./drag.js";
|
|
9
|
+
import { required } from "./fields.js";
|
|
10
|
+
import { screenHref } from "./screens.js";
|
|
11
|
+
export let lookup = null;
|
|
12
|
+
export let lookupTimer;
|
|
13
|
+
export const LOOKUP_WAIT_MS = 200;
|
|
14
|
+
export function lookupValues(f) {
|
|
15
|
+
const v = get(f, 'value');
|
|
16
|
+
return v.t === K.None ? [] : v.t === K.Block ? v.s.data.slice(v.i) : [v];
|
|
17
|
+
}
|
|
18
|
+
export function lookupText(f, v) {
|
|
19
|
+
const known = get(f, 'known');
|
|
20
|
+
const t = known.t === K.Map ? mapAt(known, form(v)) : null;
|
|
21
|
+
return t !== null ? translate(t, host.rt) : form(v);
|
|
22
|
+
}
|
|
23
|
+
/** Where a readonly lookup's value links to, from its `links` map, or null to show it as text. */
|
|
24
|
+
export function lookupHref(f, v) {
|
|
25
|
+
const links = get(f, 'links');
|
|
26
|
+
if (!flag(f, 'readonly') || links.t !== K.Map)
|
|
27
|
+
return null;
|
|
28
|
+
const to = mapAt(links, form(v));
|
|
29
|
+
if (to === null)
|
|
30
|
+
return null;
|
|
31
|
+
if (to.t === K.LitWord)
|
|
32
|
+
return screenHref({ ...to, t: K.Word });
|
|
33
|
+
if (to.t === K.LitPath)
|
|
34
|
+
return screenHref({ ...to, t: K.Path });
|
|
35
|
+
return screenHref(to) ?? (to.t === K.Url || to.t === K.File || to.t === K.String ? linkHref(form(to)) : null);
|
|
36
|
+
}
|
|
37
|
+
export function buildLookup(face, m) {
|
|
38
|
+
const id = `tb-${newId()}`;
|
|
39
|
+
const wrap = el('div', 'tb-field');
|
|
40
|
+
const label = el('label', 'tb-label');
|
|
41
|
+
label.htmlFor = id;
|
|
42
|
+
const control = el('div', 'tb-control');
|
|
43
|
+
const box = el('div', 'tb-lookup');
|
|
44
|
+
const chosen = el('ul', 'tb-lookup-chosen');
|
|
45
|
+
const input = el('input', 'tb-input');
|
|
46
|
+
input.id = id;
|
|
47
|
+
input.type = 'text';
|
|
48
|
+
input.autocomplete = 'off';
|
|
49
|
+
input.setAttribute('role', 'combobox');
|
|
50
|
+
input.setAttribute('aria-autocomplete', 'list');
|
|
51
|
+
input.setAttribute('aria-expanded', 'false');
|
|
52
|
+
const name = word(face, 'name');
|
|
53
|
+
if (name !== null)
|
|
54
|
+
input.name = name;
|
|
55
|
+
const error = el('div', 'tb-error');
|
|
56
|
+
error.id = `${id}-error`;
|
|
57
|
+
error.setAttribute('aria-live', 'polite');
|
|
58
|
+
input.setAttribute('aria-describedby', error.id);
|
|
59
|
+
input.addEventListener('click', () => {
|
|
60
|
+
if (lookup?.m !== m)
|
|
61
|
+
openLookup(m, input, '');
|
|
62
|
+
});
|
|
63
|
+
input.addEventListener('input', () => {
|
|
64
|
+
if (lookup?.m !== m)
|
|
65
|
+
openLookup(m, input, null);
|
|
66
|
+
clearTimeout(lookupTimer);
|
|
67
|
+
lookupTimer = setTimeout(() => void fire('ui-lookup-search', face, string(input.value)), LOOKUP_WAIT_MS);
|
|
68
|
+
});
|
|
69
|
+
input.addEventListener('keydown', ev => lookupKey(m, input, ev));
|
|
70
|
+
input.addEventListener('blur', () => {
|
|
71
|
+
closeLookup();
|
|
72
|
+
// what was typed is not a value: emptied, a lookup has none; else it goes back to what it had
|
|
73
|
+
if (input.readOnly)
|
|
74
|
+
return;
|
|
75
|
+
const many = flag(face, 'multiple');
|
|
76
|
+
const emptied = !many && input.value === '' && get(face, 'value').t !== K.None;
|
|
77
|
+
if (emptied)
|
|
78
|
+
void fire('ui-input', face, NONE);
|
|
79
|
+
else
|
|
80
|
+
input.value = many ? '' : lookupValues(face).map(v => lookupText(face, v)).join('');
|
|
81
|
+
void fire('ui-blur', face);
|
|
82
|
+
});
|
|
83
|
+
box.append(chosen, input);
|
|
84
|
+
control.append(box, error);
|
|
85
|
+
wrap.append(label, control);
|
|
86
|
+
m.input = input;
|
|
87
|
+
m.label = label;
|
|
88
|
+
m.error = error;
|
|
89
|
+
m.box = chosen;
|
|
90
|
+
return wrap;
|
|
91
|
+
}
|
|
92
|
+
export function syncLookup(m, err) {
|
|
93
|
+
const f = m.face;
|
|
94
|
+
const input = m.input;
|
|
95
|
+
const label = m.label;
|
|
96
|
+
label.textContent = str(f, 'text') ?? '';
|
|
97
|
+
label.classList.toggle('tb-required', required(f));
|
|
98
|
+
const many = flag(f, 'multiple');
|
|
99
|
+
const readonly = flag(f, 'readonly');
|
|
100
|
+
const off = flag(f, 'disabled');
|
|
101
|
+
const values = lookupValues(f);
|
|
102
|
+
// readonly values with a link are shown as chips, so that each one is a real link
|
|
103
|
+
const chips = many || values.some(v => lookupHref(f, v) !== null);
|
|
104
|
+
input.readOnly = readonly;
|
|
105
|
+
input.disabled = off;
|
|
106
|
+
input.hidden = chips && readonly && values.length > 0;
|
|
107
|
+
input.setAttribute('aria-required', required(f) ? 'true' : 'false');
|
|
108
|
+
input.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
109
|
+
const hint = str(f, 'hint');
|
|
110
|
+
if (hint !== null)
|
|
111
|
+
input.placeholder = hint;
|
|
112
|
+
m.error.textContent = err ?? '';
|
|
113
|
+
if (chips) {
|
|
114
|
+
m.box.replaceChildren(...values.map((v, i) => {
|
|
115
|
+
const li = el('li', 'tb-lookup-chip');
|
|
116
|
+
const href = lookupHref(f, v);
|
|
117
|
+
const shown = el(href !== null ? 'a' : 'span', href !== null ? 'tb-link' : '');
|
|
118
|
+
if (shown instanceof HTMLAnchorElement)
|
|
119
|
+
shown.href = href;
|
|
120
|
+
shown.textContent = lookupText(f, v);
|
|
121
|
+
li.append(shown);
|
|
122
|
+
if (!readonly && !off) {
|
|
123
|
+
const remove = el('button', 'tb-upload-remove');
|
|
124
|
+
remove.type = 'button';
|
|
125
|
+
remove.setAttribute('aria-label', own('remove-value', shown.textContent));
|
|
126
|
+
const x = icon('close');
|
|
127
|
+
if (x !== null)
|
|
128
|
+
remove.append(x);
|
|
129
|
+
remove.addEventListener('click', () => {
|
|
130
|
+
input.focus();
|
|
131
|
+
void fire('ui-lookup-remove', f, int(i + 1));
|
|
132
|
+
});
|
|
133
|
+
li.append(remove);
|
|
134
|
+
}
|
|
135
|
+
return li;
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// a single value loses its chip when it stops being readonly or linked
|
|
140
|
+
m.box.replaceChildren();
|
|
141
|
+
const want = values.map(v => lookupText(f, v)).join('');
|
|
142
|
+
if (document.activeElement !== input && input.value !== want)
|
|
143
|
+
input.value = want;
|
|
144
|
+
}
|
|
145
|
+
if (lookup?.m === m)
|
|
146
|
+
renderLookup();
|
|
147
|
+
}
|
|
148
|
+
export function openLookup(m, input, query) {
|
|
149
|
+
if (input.readOnly || input.disabled)
|
|
150
|
+
return;
|
|
151
|
+
closeLookup();
|
|
152
|
+
const list = el('ul', 'tb-lookup-list');
|
|
153
|
+
list.id = `tb-${newId()}`;
|
|
154
|
+
list.setAttribute('role', 'listbox');
|
|
155
|
+
list.setAttribute('popover', 'manual');
|
|
156
|
+
// choosing with the pointer must not take the focus away from the field
|
|
157
|
+
list.addEventListener('pointerdown', ev => ev.preventDefault());
|
|
158
|
+
lookup = { m, list, shown: null, items: [], active: -1 };
|
|
159
|
+
input.setAttribute('aria-expanded', 'true');
|
|
160
|
+
input.setAttribute('aria-controls', list.id);
|
|
161
|
+
// a modal dialog makes all outside it inert: there, a list in the body would take no clicks
|
|
162
|
+
(input.closest('dialog') ?? document.body).append(list);
|
|
163
|
+
list.showPopover?.();
|
|
164
|
+
placeLookup();
|
|
165
|
+
if (query !== null)
|
|
166
|
+
void fire('ui-lookup-search', m.face, string(query));
|
|
167
|
+
}
|
|
168
|
+
export function placeLookup() {
|
|
169
|
+
if (lookup === null)
|
|
170
|
+
return;
|
|
171
|
+
const { list, m } = lookup;
|
|
172
|
+
const r = m.input.getBoundingClientRect();
|
|
173
|
+
list.style.width = `${r.width}px`;
|
|
174
|
+
list.style.left = `${r.left}px`;
|
|
175
|
+
const height = list.getBoundingClientRect().height;
|
|
176
|
+
list.style.top = `${r.bottom + 4 + height > innerHeight - 8 ? Math.max(8, r.top - 4 - height) : r.bottom + 4}px`;
|
|
177
|
+
}
|
|
178
|
+
export function closeLookup() {
|
|
179
|
+
clearTimeout(lookupTimer);
|
|
180
|
+
if (lookup === null)
|
|
181
|
+
return;
|
|
182
|
+
const { list, m } = lookup;
|
|
183
|
+
lookup = null;
|
|
184
|
+
list.hidePopover?.();
|
|
185
|
+
list.remove();
|
|
186
|
+
m.input.setAttribute('aria-expanded', 'false');
|
|
187
|
+
m.input.removeAttribute('aria-controls');
|
|
188
|
+
m.input.removeAttribute('aria-activedescendant');
|
|
189
|
+
}
|
|
190
|
+
export function renderLookup() {
|
|
191
|
+
if (lookup === null)
|
|
192
|
+
return;
|
|
193
|
+
const open = lookup;
|
|
194
|
+
const options = get(open.m.face, 'options');
|
|
195
|
+
// what is on show stays until the search answers; a query still on its way shows nothing new
|
|
196
|
+
if (options.t !== K.Block || options === open.shown)
|
|
197
|
+
return;
|
|
198
|
+
if (open.shown === null && get(open.m.face, 'query').t === K.None)
|
|
199
|
+
return;
|
|
200
|
+
open.shown = options;
|
|
201
|
+
const taken = new Set(flag(open.m.face, 'multiple') ? lookupValues(open.m.face).map(v => form(v)) : []);
|
|
202
|
+
open.items = optionsOf(open.m.face).filter(o => !taken.has(optionValue(o)));
|
|
203
|
+
open.active = -1;
|
|
204
|
+
open.list.replaceChildren(...open.items.map((o, i) => {
|
|
205
|
+
const li = el('li', 'tb-lookup-option');
|
|
206
|
+
li.id = `${open.list.id}-${i}`;
|
|
207
|
+
li.setAttribute('role', 'option');
|
|
208
|
+
li.setAttribute('aria-selected', 'false');
|
|
209
|
+
li.textContent = optionText(o);
|
|
210
|
+
li.addEventListener('click', () => pickLookup(o));
|
|
211
|
+
return li;
|
|
212
|
+
}));
|
|
213
|
+
if (open.items.length === 0) {
|
|
214
|
+
const none = el('li', 'tb-lookup-none');
|
|
215
|
+
none.textContent = own('no-matches');
|
|
216
|
+
open.list.append(none);
|
|
217
|
+
}
|
|
218
|
+
placeLookup();
|
|
219
|
+
}
|
|
220
|
+
export function pickLookup(option) {
|
|
221
|
+
if (lookup === null)
|
|
222
|
+
return;
|
|
223
|
+
const { m } = lookup;
|
|
224
|
+
const input = m.input;
|
|
225
|
+
closeLookup();
|
|
226
|
+
input.value = flag(m.face, 'multiple') ? '' : optionText(option);
|
|
227
|
+
void fire('ui-lookup-pick', m.face, option);
|
|
228
|
+
}
|
|
229
|
+
export function moveLookup(by) {
|
|
230
|
+
if (lookup === null || lookup.items.length === 0)
|
|
231
|
+
return;
|
|
232
|
+
const { list, items, m } = lookup;
|
|
233
|
+
const at = lookup.active < 0 && by < 0 ? items.length - 1 : (lookup.active + by + items.length) % items.length;
|
|
234
|
+
list.children[lookup.active]?.setAttribute('aria-selected', 'false');
|
|
235
|
+
lookup.active = at;
|
|
236
|
+
const li = list.children[at];
|
|
237
|
+
li.setAttribute('aria-selected', 'true');
|
|
238
|
+
li.scrollIntoView({ block: 'nearest' });
|
|
239
|
+
m.input.setAttribute('aria-activedescendant', li.id);
|
|
240
|
+
}
|
|
241
|
+
export function lookupKey(m, input, ev) {
|
|
242
|
+
const open = lookup?.m === m;
|
|
243
|
+
const stop = () => { ev.preventDefault(); ev.stopPropagation(); };
|
|
244
|
+
switch (ev.key) {
|
|
245
|
+
case 'ArrowDown':
|
|
246
|
+
case 'ArrowUp':
|
|
247
|
+
stop();
|
|
248
|
+
if (open)
|
|
249
|
+
moveLookup(ev.key === 'ArrowDown' ? 1 : -1);
|
|
250
|
+
else
|
|
251
|
+
openLookup(m, input, '');
|
|
252
|
+
break;
|
|
253
|
+
case 'Enter':
|
|
254
|
+
if (!open)
|
|
255
|
+
break;
|
|
256
|
+
// with its list open, Enter chooses: it never sends the form
|
|
257
|
+
stop();
|
|
258
|
+
if (lookup.active >= 0)
|
|
259
|
+
pickLookup(lookup.items[lookup.active]);
|
|
260
|
+
break;
|
|
261
|
+
case 'Escape':
|
|
262
|
+
if (open) {
|
|
263
|
+
stop();
|
|
264
|
+
closeLookup();
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
case 'Backspace':
|
|
268
|
+
if (flag(m.face, 'multiple') && input.value === '' && !input.readOnly) {
|
|
269
|
+
const n = lookupValues(m.face).length;
|
|
270
|
+
if (n > 0)
|
|
271
|
+
void fire('ui-lookup-remove', m.face, int(n));
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell, type BlockCell } from '../../core/index.ts';
|
|
2
|
+
/** The open menus, outermost first. */
|
|
3
|
+
export declare const menus: HTMLElement[];
|
|
4
|
+
/** Where focus goes back when the menus close. */
|
|
5
|
+
export declare let menuOpener: HTMLElement | null;
|
|
6
|
+
export declare function closeMenus(from?: number, refocus?: boolean): void;
|
|
7
|
+
export declare function outsideMenu(ev: PointerEvent): void;
|
|
8
|
+
export declare function menuItems(mn: HTMLElement): HTMLElement[];
|
|
9
|
+
export interface MenuEntry {
|
|
10
|
+
text: string;
|
|
11
|
+
icon?: string | null;
|
|
12
|
+
divider?: boolean;
|
|
13
|
+
danger?: boolean;
|
|
14
|
+
sub?: MenuEntry[];
|
|
15
|
+
run?: () => void;
|
|
16
|
+
}
|
|
17
|
+
/** A menu block of the dialect as menu entries; choosing one runs ui-menu. */
|
|
18
|
+
export declare function trebolEntries(face: ObjectCell, items: BlockCell, row: Cell): MenuEntry[];
|
|
19
|
+
/** Opens a menu at x, y; level > 0 is a submenu. */
|
|
20
|
+
export declare function openMenu(entries: MenuEntry[], x: number, y: number, opener: HTMLElement, level?: number): void;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// --- menus ---------------------------------------------------------------------------
|
|
2
|
+
import { K } from "../../core/index.js";
|
|
3
|
+
import { get, str, flag, iconName, icon } from "./faces.js";
|
|
4
|
+
import { fire, el } from "./mount.js";
|
|
5
|
+
import { seenKeys } from "./events.js";
|
|
6
|
+
/** The open menus, outermost first. */
|
|
7
|
+
export const menus = [];
|
|
8
|
+
/** Where focus goes back when the menus close. */
|
|
9
|
+
export let menuOpener = null;
|
|
10
|
+
export function closeMenus(from = 0, refocus = true) {
|
|
11
|
+
for (const mn of menus.splice(from)) {
|
|
12
|
+
mn.hidePopover?.();
|
|
13
|
+
mn.remove();
|
|
14
|
+
}
|
|
15
|
+
if (from === 0) {
|
|
16
|
+
menuOpener?.setAttribute('aria-expanded', 'false');
|
|
17
|
+
if (!menuOpener?.hasAttribute('aria-haspopup'))
|
|
18
|
+
menuOpener?.removeAttribute('aria-expanded');
|
|
19
|
+
if (refocus)
|
|
20
|
+
menuOpener?.focus();
|
|
21
|
+
menuOpener = null;
|
|
22
|
+
document.removeEventListener('pointerdown', outsideMenu, true);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
menus[from - 1]?.querySelector('[aria-expanded="true"]')?.setAttribute('aria-expanded', 'false');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function outsideMenu(ev) {
|
|
29
|
+
if (!menus.some(mn => mn.contains(ev.target)))
|
|
30
|
+
closeMenus(0, false);
|
|
31
|
+
}
|
|
32
|
+
export function menuItems(mn) {
|
|
33
|
+
return Array.from(mn.querySelectorAll(':scope > [role="menuitem"]'));
|
|
34
|
+
}
|
|
35
|
+
/** A menu block of the dialect as menu entries; choosing one runs ui-menu. */
|
|
36
|
+
export function trebolEntries(face, items, row) {
|
|
37
|
+
return items.s.data.slice(items.i).filter(x => x.t === K.Object).map(item => {
|
|
38
|
+
const sub = get(item, 'items');
|
|
39
|
+
return {
|
|
40
|
+
text: str(item, 'text') ?? '',
|
|
41
|
+
icon: iconName(item),
|
|
42
|
+
divider: flag(item, 'divider'),
|
|
43
|
+
sub: sub.t === K.Block ? trebolEntries(face, sub, row) : undefined,
|
|
44
|
+
danger: flag(item, 'danger'),
|
|
45
|
+
run: () => void fire('ui-menu', face, item, row),
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Opens a menu at x, y; level > 0 is a submenu. */
|
|
50
|
+
export function openMenu(entries, x, y, opener, level = 0) {
|
|
51
|
+
closeMenus(level, false);
|
|
52
|
+
if (level === 0) {
|
|
53
|
+
menuOpener = opener;
|
|
54
|
+
if (opener.hasAttribute('aria-haspopup'))
|
|
55
|
+
opener.setAttribute('aria-expanded', 'true');
|
|
56
|
+
document.addEventListener('pointerdown', outsideMenu, true);
|
|
57
|
+
}
|
|
58
|
+
const mn = el('div', 'tb-menu');
|
|
59
|
+
mn.setAttribute('role', 'menu');
|
|
60
|
+
mn.setAttribute('popover', 'manual');
|
|
61
|
+
const label = opener.getAttribute('aria-label') ?? opener.textContent;
|
|
62
|
+
if (label)
|
|
63
|
+
mn.setAttribute('aria-label', label.trim());
|
|
64
|
+
for (const item of entries) {
|
|
65
|
+
if (item.divider) {
|
|
66
|
+
const d = el('div', 'tb-menu-divider');
|
|
67
|
+
d.setAttribute('role', 'separator');
|
|
68
|
+
mn.append(d);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const b = el('button', item.danger ? 'tb-menu-item tb-danger' : 'tb-menu-item');
|
|
72
|
+
b.type = 'button';
|
|
73
|
+
b.tabIndex = -1;
|
|
74
|
+
b.setAttribute('role', 'menuitem');
|
|
75
|
+
const svg = item.icon ? icon(item.icon) : null;
|
|
76
|
+
const text = el('span');
|
|
77
|
+
text.textContent = item.text;
|
|
78
|
+
b.append(svg ?? el('span', 'tb-menu-gap'), text);
|
|
79
|
+
const sub = item.sub;
|
|
80
|
+
if (sub !== undefined) {
|
|
81
|
+
b.setAttribute('aria-haspopup', 'menu');
|
|
82
|
+
b.setAttribute('aria-expanded', 'false');
|
|
83
|
+
const chevron = icon('chevron-right');
|
|
84
|
+
if (chevron !== null)
|
|
85
|
+
b.append(chevron);
|
|
86
|
+
const openSub = () => {
|
|
87
|
+
const r = b.getBoundingClientRect();
|
|
88
|
+
openMenu(sub, r.right - 4, r.top - 5, b, level + 1);
|
|
89
|
+
b.setAttribute('aria-expanded', 'true');
|
|
90
|
+
};
|
|
91
|
+
b.addEventListener('click', openSub);
|
|
92
|
+
b.addEventListener('keydown', ev => { if (ev.key === 'ArrowRight') {
|
|
93
|
+
ev.preventDefault();
|
|
94
|
+
openSub();
|
|
95
|
+
} });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
b.addEventListener('click', () => {
|
|
99
|
+
closeMenus();
|
|
100
|
+
item.run?.();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
b.addEventListener('pointerenter', () => b.focus());
|
|
104
|
+
mn.append(b);
|
|
105
|
+
}
|
|
106
|
+
mn.addEventListener('keydown', ev => {
|
|
107
|
+
seenKeys.add(ev);
|
|
108
|
+
const list = menuItems(mn);
|
|
109
|
+
const at = list.indexOf(document.activeElement);
|
|
110
|
+
const n = list.length;
|
|
111
|
+
const to = ev.key === 'ArrowDown' ? (at + 1) % n : ev.key === 'ArrowUp' ? (at - 1 + n) % n
|
|
112
|
+
: ev.key === 'Home' ? 0 : ev.key === 'End' ? n - 1 : -1;
|
|
113
|
+
if (to >= 0) {
|
|
114
|
+
ev.preventDefault();
|
|
115
|
+
list[to]?.focus();
|
|
116
|
+
}
|
|
117
|
+
else if (ev.key === 'Escape' || (ev.key === 'ArrowLeft' && level > 0)) {
|
|
118
|
+
ev.preventDefault();
|
|
119
|
+
ev.stopPropagation();
|
|
120
|
+
if (level > 0) {
|
|
121
|
+
closeMenus(level);
|
|
122
|
+
opener.focus();
|
|
123
|
+
}
|
|
124
|
+
else
|
|
125
|
+
closeMenus();
|
|
126
|
+
}
|
|
127
|
+
else if (ev.key === 'Tab') {
|
|
128
|
+
ev.preventDefault();
|
|
129
|
+
closeMenus();
|
|
130
|
+
}
|
|
131
|
+
else if (ev.key.length === 1 && /\S/.test(ev.key)) {
|
|
132
|
+
// type a letter to jump to the next item that starts with it
|
|
133
|
+
const k = ev.key.toLowerCase();
|
|
134
|
+
const next = [...list.slice(at + 1), ...list.slice(0, at + 1)].find(b => b.textContent?.trim().toLowerCase().startsWith(k));
|
|
135
|
+
next?.focus();
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
// keep the icon column only when some item has an icon
|
|
139
|
+
if (mn.querySelector('.tb-menu-item > svg:first-child') === null)
|
|
140
|
+
mn.querySelectorAll('.tb-menu-gap').forEach(g => g.remove());
|
|
141
|
+
// in the top layer, so it shows above an open dialog
|
|
142
|
+
document.body.append(mn);
|
|
143
|
+
mn.showPopover?.();
|
|
144
|
+
const r = mn.getBoundingClientRect();
|
|
145
|
+
mn.style.left = `${Math.max(8, Math.min(x, innerWidth - r.width - 8))}px`;
|
|
146
|
+
mn.style.top = `${Math.max(8, y + r.height > innerHeight - 8 ? y - r.height : y)}px`;
|
|
147
|
+
menus.push(mn);
|
|
148
|
+
menuItems(mn)[0]?.focus();
|
|
149
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell, type Ctx } from '../../core/index.ts';
|
|
2
|
+
import { type Host } from '../index.ts';
|
|
3
|
+
import { type HostTask } from '../scheduler.ts';
|
|
4
|
+
/** What the host keeps for each mounted face. */
|
|
5
|
+
export interface Mounted {
|
|
6
|
+
face: ObjectCell;
|
|
7
|
+
el: HTMLElement;
|
|
8
|
+
input: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null;
|
|
9
|
+
label: HTMLElement | null;
|
|
10
|
+
error: HTMLElement | null;
|
|
11
|
+
kids: unknown;
|
|
12
|
+
/** Where children or rows go, when it is not `el`. */
|
|
13
|
+
box: HTMLElement | null;
|
|
14
|
+
/** Rows of a table or list, by key. */
|
|
15
|
+
rows: Map<string, HTMLElement> | null;
|
|
16
|
+
/** A table or list: all its rows, in the order shown, with their keys; only some may be drawn. */
|
|
17
|
+
items: {
|
|
18
|
+
row: Cell;
|
|
19
|
+
k: string;
|
|
20
|
+
}[];
|
|
21
|
+
/** A table or list: the rows drawn, [first, end), and about how tall each is. */
|
|
22
|
+
first: number;
|
|
23
|
+
end: number;
|
|
24
|
+
rowHeight: number;
|
|
25
|
+
/** Shown instead of the rows when there are none. */
|
|
26
|
+
empty: HTMLElement | null;
|
|
27
|
+
/** The tab button of a tab face. */
|
|
28
|
+
button: HTMLButtonElement | null;
|
|
29
|
+
/** The options of a radio. */
|
|
30
|
+
radios: HTMLInputElement[] | null;
|
|
31
|
+
/** The keys of the open nodes of a tree. */
|
|
32
|
+
open: Set<string> | null;
|
|
33
|
+
/** Rows: what the rows were last checked against. Rich text: the Markdown on screen. */
|
|
34
|
+
shown: unknown;
|
|
35
|
+
/** An input: what the user typed still on its way to the face. */
|
|
36
|
+
typing: number;
|
|
37
|
+
/** An input: the value the user's typing gave the face; code that sets another one is shown even with the focus. */
|
|
38
|
+
typed: Cell | null;
|
|
39
|
+
}
|
|
40
|
+
export declare let host: Host;
|
|
41
|
+
export declare function setHost(v: Host): void;
|
|
42
|
+
/** Mounted faces by their node and by their input. */
|
|
43
|
+
export declare const byNode: WeakMap<Element, Mounted>;
|
|
44
|
+
/** The row cell each table row or list item shows. */
|
|
45
|
+
export declare const rowOf: WeakMap<Element, Cell>;
|
|
46
|
+
/** Mounted faces by context; a face rebuilt in a new container replaces its entry. */
|
|
47
|
+
export declare const byCtx: WeakMap<Ctx, Mounted>;
|
|
48
|
+
/** Contexts of mounted faces written since the last repaint (filled by the core). */
|
|
49
|
+
export declare const dirty: Ctx[];
|
|
50
|
+
/** Mounted faces with react, is or calc; and whether one came since reactions last ran. */
|
|
51
|
+
export declare const reactors: Set<Mounted>;
|
|
52
|
+
export declare let newReactor: boolean;
|
|
53
|
+
export declare function setNewReactor(v: typeof newReactor): void;
|
|
54
|
+
/** The ui-react still running because a reaction waits (a fetch); the next one waits for it. */
|
|
55
|
+
export declare let reacting: HostTask | null;
|
|
56
|
+
export declare function setReacting(v: HostTask | null): void;
|
|
57
|
+
export declare let nextId: number;
|
|
58
|
+
/** A new number for the id of an element. */
|
|
59
|
+
export declare function newId(): number;
|
|
60
|
+
export declare const ACTIVE: ReadonlySet<number>;
|
|
61
|
+
/** The handler each UI event function runs, to name it in an error; the others go by their own name. */
|
|
62
|
+
export declare const HANDLERS: Record<string, string>;
|
|
63
|
+
/**
|
|
64
|
+
* An error of a UI event function says where in the app it happened: `action of button "Save"`, not the
|
|
65
|
+
* name of Trebol's own function (ui-click) that ran it.
|
|
66
|
+
*/
|
|
67
|
+
export declare function whereInApp(e: ObjectCell, fn: string, face: ObjectCell, args: Cell[]): void;
|
|
68
|
+
/**
|
|
69
|
+
* Runs a UI event function (ui-input, ui-blur, ui-click) as a new task. An error is shown and gives none, so
|
|
70
|
+
* what the caller does next (focus the first problem, blur) still runs and nothing is left unhandled.
|
|
71
|
+
*/
|
|
72
|
+
export declare function fire(fn: string, face: ObjectCell, ...args: Cell[]): Promise<Cell>;
|
|
73
|
+
/** The panel of errors trebol dev shows on the page; null until the first one. */
|
|
74
|
+
export declare let devErrors: HTMLElement | null;
|
|
75
|
+
/**
|
|
76
|
+
* An error nobody caught: in the console and, when trebol dev serves the page (it adds
|
|
77
|
+
* `<meta name="trebol-dev">`), also on the page, so a broken app is not a blank page.
|
|
78
|
+
*/
|
|
79
|
+
export declare function showError(message: string): void;
|
|
80
|
+
export declare function el<T extends keyof HTMLElementTagNameMap>(tag: T, cls?: string): HTMLElementTagNameMap[T];
|
|
81
|
+
export declare const INPUTS: Set<string>;
|
|
82
|
+
export declare function build(face: ObjectCell): HTMLElement;
|
|
83
|
+
/** Puts the face's icon before its caption, which then goes in `m.label`. */
|
|
84
|
+
export declare function withIcon(node: HTMLElement, face: ObjectCell, m: Mounted): void;
|
|
85
|
+
/** The small second icon of a tile (mark #list "List"), in a corner of its icon, and the name it adds. */
|
|
86
|
+
export declare function syncMark(m: Mounted): void;
|
|
87
|
+
/** A face with an icon and no text is named after its icon: #arrow-left -> "Arrow left". */
|
|
88
|
+
export declare function iconLabel(face: ObjectCell): string | null;
|
|
89
|
+
/** Children of a container; consecutive buttons in a form, or in a step of one, share an action row. */
|
|
90
|
+
export declare function appendChildren(node: HTMLElement, face: ObjectCell, style: string): void;
|
|
91
|
+
export declare function clickSubmit(face: ObjectCell, formEl: HTMLElement): void;
|
|
92
|
+
export declare function focusProblem(scope: HTMLElement | null): void;
|