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,131 @@
|
|
|
1
|
+
// --- reading faces -----------------------------------------------------------------
|
|
2
|
+
import { K, NONE, form, ctxFind, intern, spell } from "../../core/index.js";
|
|
3
|
+
import { mapAt, shown, translate, uiText, activeLanguage, LOCAL_KINDS } from "../index.js";
|
|
4
|
+
import { iconNode } from "../icons.js";
|
|
5
|
+
import { host, el } from "./mount.js";
|
|
6
|
+
export function get(face, name) {
|
|
7
|
+
const i = ctxFind(face.ctx, intern(name));
|
|
8
|
+
return i < 0 ? NONE : face.ctx.vals[i];
|
|
9
|
+
}
|
|
10
|
+
export function str(face, name) {
|
|
11
|
+
const v = get(face, name);
|
|
12
|
+
if (v.t === K.None)
|
|
13
|
+
return null;
|
|
14
|
+
// a text per language, or a word of the dictionary: in the active language
|
|
15
|
+
if (v.t === K.Map || ((name === 'text' || name === 'hint' || name === 'mark-text') && v.t === K.Word))
|
|
16
|
+
return translate(v, host.rt);
|
|
17
|
+
return form(v);
|
|
18
|
+
}
|
|
19
|
+
/** What a choice or radio option is worth: itself, or the `value` of a map #(value: .. text: ..). */
|
|
20
|
+
export function optionValue(o) {
|
|
21
|
+
const v = o.t === K.Map ? mapAt(o, 'value') : null;
|
|
22
|
+
return form(v ?? o);
|
|
23
|
+
}
|
|
24
|
+
/** What a choice or radio option shows, in the active language. */
|
|
25
|
+
export function optionText(o) {
|
|
26
|
+
const t = o.t === K.Map ? mapAt(o, 'text') : null;
|
|
27
|
+
return t !== null ? translate(t, host.rt) : optionValue(o);
|
|
28
|
+
}
|
|
29
|
+
/** The options of a choice or radio as written: options and groups, a title followed by a block. */
|
|
30
|
+
export function entriesOf(face) {
|
|
31
|
+
const opts = get(face, 'options');
|
|
32
|
+
if (opts.t !== K.Block)
|
|
33
|
+
return [];
|
|
34
|
+
const data = opts.s.data.slice(opts.i);
|
|
35
|
+
const out = [];
|
|
36
|
+
for (let i = 0; i < data.length; i++) {
|
|
37
|
+
const next = data[i + 1];
|
|
38
|
+
if (next !== undefined && next.t === K.Block && data[i].t !== K.Block) {
|
|
39
|
+
out.push({ title: data[i], options: next.s.data.slice(next.i) });
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
else if (data[i].t !== K.Block)
|
|
43
|
+
out.push(data[i]);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
export function isGroup(e) {
|
|
48
|
+
return 'options' in e;
|
|
49
|
+
}
|
|
50
|
+
/** The options of a choice or radio, those of its groups included, in order. */
|
|
51
|
+
export function optionsOf(face) {
|
|
52
|
+
return entriesOf(face).flatMap(e => isGroup(e) ? e.options : [e]);
|
|
53
|
+
}
|
|
54
|
+
/** What tells apart two sets of options: their values and where each group starts. */
|
|
55
|
+
export function optionsKey(face) {
|
|
56
|
+
return entriesOf(face).map(e => isGroup(e) ? `\u0001${e.options.map(optionValue).join('\0')}` : optionValue(e)).join('\0');
|
|
57
|
+
}
|
|
58
|
+
/** Writes the options of a select: an empty one first, then those of the face, each group in an optgroup. */
|
|
59
|
+
export function fillSelect(sel, face) {
|
|
60
|
+
const first = el('option');
|
|
61
|
+
first.value = '';
|
|
62
|
+
sel.replaceChildren(first);
|
|
63
|
+
const option = (o) => {
|
|
64
|
+
const opt = el('option');
|
|
65
|
+
opt.value = optionValue(o);
|
|
66
|
+
opt.textContent = optionText(o);
|
|
67
|
+
return opt;
|
|
68
|
+
};
|
|
69
|
+
for (const e of entriesOf(face)) {
|
|
70
|
+
if (isGroup(e)) {
|
|
71
|
+
const group = el('optgroup');
|
|
72
|
+
group.label = translate(e.title, host.rt);
|
|
73
|
+
group.append(...e.options.map(option));
|
|
74
|
+
sel.append(group);
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
sel.append(option(e));
|
|
78
|
+
}
|
|
79
|
+
sel.dataset.options = optionsKey(face);
|
|
80
|
+
}
|
|
81
|
+
/** One of Trebol's own texts (`i18n/ui`) in the active language. */
|
|
82
|
+
export function own(key, ...args) {
|
|
83
|
+
return uiText(host.rt, key, ...args);
|
|
84
|
+
}
|
|
85
|
+
/** Gives a node built once one of Trebol's own texts (its text or an attribute); `repaint` keeps it in the active language. */
|
|
86
|
+
export function keepOwn(node, key, attr) {
|
|
87
|
+
node.dataset.tbOwn = attr === undefined ? key : `${attr} ${key}`;
|
|
88
|
+
showOwn(node);
|
|
89
|
+
}
|
|
90
|
+
export function showOwn(node) {
|
|
91
|
+
const [a, k] = node.dataset.tbOwn.split(' ');
|
|
92
|
+
if (k === undefined)
|
|
93
|
+
node.textContent = own(a);
|
|
94
|
+
else
|
|
95
|
+
node.setAttribute(a, own(k));
|
|
96
|
+
}
|
|
97
|
+
/** A value as it shows on screen: dates, numbers and money in the active language. */
|
|
98
|
+
export function display(v) {
|
|
99
|
+
return LOCAL_KINDS.has(v.t) ? shown(v, activeLanguage(host.rt)) : form(v);
|
|
100
|
+
}
|
|
101
|
+
export function flag(face, name) {
|
|
102
|
+
const v = get(face, name);
|
|
103
|
+
return !(v.t === K.None || (v.t === K.Logic && !v.v));
|
|
104
|
+
}
|
|
105
|
+
export function word(face, name) {
|
|
106
|
+
const v = get(face, name);
|
|
107
|
+
return v.t === K.Word || v.t === K.LitWord ? spell(v.sym).toLowerCase() : null;
|
|
108
|
+
}
|
|
109
|
+
export function children(face) {
|
|
110
|
+
const c = get(face, 'pane');
|
|
111
|
+
if (c.t !== K.Block)
|
|
112
|
+
return [];
|
|
113
|
+
return c.s.data.slice(c.i).filter(x => x.t === K.Object);
|
|
114
|
+
}
|
|
115
|
+
/** The name of a face's icon (`#search` -> "search"). */
|
|
116
|
+
export function iconName(face) {
|
|
117
|
+
const v = get(face, 'icon');
|
|
118
|
+
return v.t === K.Issue ? spell(v.sym).toLowerCase() : null;
|
|
119
|
+
}
|
|
120
|
+
export function icon(name, label = null) {
|
|
121
|
+
const svg = iconNode(name, label);
|
|
122
|
+
if (svg === null)
|
|
123
|
+
console.warn(`Trebol: unknown icon #${name}`);
|
|
124
|
+
return svg;
|
|
125
|
+
}
|
|
126
|
+
export function hasRule(face, rule) {
|
|
127
|
+
const rules = get(face, 'rules');
|
|
128
|
+
if (rules.t !== K.Block)
|
|
129
|
+
return false;
|
|
130
|
+
return rules.s.data.some(r => r.t === K.Block && r.s.data[0]?.t === K.Word && spell(r.s.data[0].sym) === rule);
|
|
131
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell } from '../../core/index.ts';
|
|
2
|
+
import { type Mounted } from './mount.ts';
|
|
3
|
+
export declare function buildInput(face: ObjectCell, style: string, m: Mounted): HTMLElement;
|
|
4
|
+
export declare function buildCheck(face: ObjectCell, m: Mounted, toggle: boolean): HTMLElement;
|
|
5
|
+
export declare function buildSlider(face: ObjectCell, m: Mounted): HTMLElement;
|
|
6
|
+
export declare function ruleAmount(face: ObjectCell, rule: string): number | null;
|
|
7
|
+
export declare function syncSlider(m: Mounted, err: string | null): void;
|
|
8
|
+
export declare function buildRadio(face: ObjectCell, m: Mounted): HTMLElement;
|
|
9
|
+
export declare function buildProgress(m: Mounted): HTMLElement;
|
|
10
|
+
/** A progress value as a fraction, or null while it is unknown. */
|
|
11
|
+
export declare function fraction(v: Cell): number | null;
|
|
12
|
+
export declare function syncProgress(m: Mounted): void;
|
|
13
|
+
export declare function syncIcon(m: Mounted): void;
|
|
14
|
+
/** No rule applies to a read-only face, so it never asks for a value. */
|
|
15
|
+
export declare function required(f: ObjectCell): boolean;
|
|
16
|
+
export declare function syncRadio(m: Mounted, err: string | null): void;
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// --- fields ---------------------------------------------------------------------
|
|
2
|
+
//
|
|
3
|
+
// Text boxes, checks, sliders, radios and progress bars: building them and keeping them in step with their face.
|
|
4
|
+
import { K, NONE, TRUE, FALSE, string, form, spell } from "../../core/index.js";
|
|
5
|
+
import { translate, activeLanguage, editLocal, readLocal, readDate, readRange, readTime, amountOf } from "../index.js";
|
|
6
|
+
import { inDevtools } from "../devtools.js";
|
|
7
|
+
import { get, str, optionValue, optionText, entriesOf, isGroup, fillSelect, display, flag, word, iconName, icon, hasRule } from "./faces.js";
|
|
8
|
+
import { host, newId, fire, el } from "./mount.js";
|
|
9
|
+
import { calendar, openCalendar, closeCalendar } from "./calendar.js";
|
|
10
|
+
import { times, openTimes, closeTimes } from "./times.js";
|
|
11
|
+
import { hadCaret } from "./richtext.js";
|
|
12
|
+
export function buildInput(face, style, m) {
|
|
13
|
+
const id = `tb-${newId()}`;
|
|
14
|
+
const wrap = el('div', 'tb-field');
|
|
15
|
+
const label = el('label', 'tb-label');
|
|
16
|
+
label.htmlFor = id;
|
|
17
|
+
const control = el('div', 'tb-control');
|
|
18
|
+
let input;
|
|
19
|
+
if (style === 'area') {
|
|
20
|
+
input = el('textarea', 'tb-input');
|
|
21
|
+
}
|
|
22
|
+
else if (style === 'choice') {
|
|
23
|
+
const sel = el('select', 'tb-input');
|
|
24
|
+
fillSelect(sel, face);
|
|
25
|
+
input = sel;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const i = el('input', 'tb-input');
|
|
29
|
+
i.type = style === 'password' ? 'password' : hasRule(face, 'email') ? 'email' : hasRule(face, 'url') ? 'url' : 'text';
|
|
30
|
+
if (style === 'number' || style === 'money' || hasRule(face, 'integer'))
|
|
31
|
+
i.inputMode = hasRule(face, 'integer') ? 'numeric' : 'decimal';
|
|
32
|
+
if (style === 'password')
|
|
33
|
+
i.autocomplete = 'new-password';
|
|
34
|
+
input = i;
|
|
35
|
+
}
|
|
36
|
+
input.addEventListener('focus', () => hadCaret.add(input));
|
|
37
|
+
input.id = id;
|
|
38
|
+
const error = el('div', 'tb-error');
|
|
39
|
+
error.id = `${id}-error`;
|
|
40
|
+
error.setAttribute('aria-live', 'polite');
|
|
41
|
+
input.setAttribute('aria-describedby', error.id);
|
|
42
|
+
const name = word(face, 'name');
|
|
43
|
+
if (name !== null)
|
|
44
|
+
input.name = name;
|
|
45
|
+
input.addEventListener(style === 'choice' ? 'change' : 'input', () => {
|
|
46
|
+
// an amount goes to Trebol as 1234.5 and a date as 2026-11-03; what is not one goes as typed
|
|
47
|
+
const lang = activeLanguage(host.rt);
|
|
48
|
+
const typed = style === 'money' ? readLocal(input.value, lang) ?? input.value
|
|
49
|
+
: style === 'date' ? readDate(input.value, lang) ?? input.value
|
|
50
|
+
: style === 'date-range' ? readRange(input.value, lang) ?? input.value
|
|
51
|
+
: style === 'time' ? readTime(input.value) ?? input.value : input.value;
|
|
52
|
+
m.typing++;
|
|
53
|
+
const settled = () => {
|
|
54
|
+
m.typing--;
|
|
55
|
+
m.typed = get(face, 'value');
|
|
56
|
+
};
|
|
57
|
+
void fire('ui-input', face, typed === '' ? NONE : string(typed)).then(settled, settled);
|
|
58
|
+
});
|
|
59
|
+
// an amount shows formatted, and is edited as a plain number of the active language
|
|
60
|
+
if (style === 'money')
|
|
61
|
+
input.addEventListener('focus', () => {
|
|
62
|
+
if (!flag(face, 'readonly'))
|
|
63
|
+
input.value = editLocal(get(face, 'value'), activeLanguage(host.rt));
|
|
64
|
+
});
|
|
65
|
+
// going to the dev tools is not leaving the field: it shows no error for it
|
|
66
|
+
input.addEventListener('blur', ev => { if (!inDevtools(ev.relatedTarget))
|
|
67
|
+
void fire('ui-blur', face); });
|
|
68
|
+
const iconWord = style === 'area' || style === 'choice' ? null : iconName(face);
|
|
69
|
+
const svg = iconWord !== null ? icon(iconWord) : null;
|
|
70
|
+
if (style === 'date' || style === 'date-range' || style === 'time') {
|
|
71
|
+
const box = el('div', 'tb-date');
|
|
72
|
+
const pick = el('button', 'tb-date-button');
|
|
73
|
+
pick.type = 'button';
|
|
74
|
+
pick.setAttribute('aria-haspopup', style === 'time' ? 'listbox' : 'dialog');
|
|
75
|
+
pick.setAttribute('aria-expanded', 'false');
|
|
76
|
+
const cal = icon(style === 'time' ? 'clock' : 'calendar');
|
|
77
|
+
if (cal !== null)
|
|
78
|
+
pick.append(cal);
|
|
79
|
+
if (style === 'time')
|
|
80
|
+
pick.addEventListener('click', () => (times?.opener === pick ? closeTimes() : openTimes(face, pick)));
|
|
81
|
+
else
|
|
82
|
+
pick.addEventListener('click', () => (calendar?.opener === pick ? closeCalendar() : openCalendar(face, pick)));
|
|
83
|
+
box.append(input, pick);
|
|
84
|
+
control.append(box, error);
|
|
85
|
+
}
|
|
86
|
+
else if (svg !== null) {
|
|
87
|
+
const box = el('div', 'tb-input-icon');
|
|
88
|
+
box.append(svg, input);
|
|
89
|
+
control.append(box, error);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
control.append(input, error);
|
|
93
|
+
}
|
|
94
|
+
wrap.append(label, control);
|
|
95
|
+
m.input = input;
|
|
96
|
+
m.label = label;
|
|
97
|
+
m.error = error;
|
|
98
|
+
return wrap;
|
|
99
|
+
}
|
|
100
|
+
export function buildCheck(face, m, toggle) {
|
|
101
|
+
const row = el('div', 'tb-check-row tb-control');
|
|
102
|
+
const label = el('label', toggle ? 'tb-check tb-switch' : 'tb-check');
|
|
103
|
+
const box = el('input');
|
|
104
|
+
box.type = 'checkbox';
|
|
105
|
+
if (toggle)
|
|
106
|
+
box.setAttribute('role', 'switch');
|
|
107
|
+
const caption = el('span');
|
|
108
|
+
const error = el('div', 'tb-error');
|
|
109
|
+
error.id = `tb-${newId()}-error`;
|
|
110
|
+
error.setAttribute('aria-live', 'polite');
|
|
111
|
+
box.setAttribute('aria-describedby', error.id);
|
|
112
|
+
box.addEventListener('change', () => {
|
|
113
|
+
void fire('ui-input', face, box.checked ? TRUE : FALSE).then(() => fire('ui-blur', face));
|
|
114
|
+
});
|
|
115
|
+
label.append(box, caption);
|
|
116
|
+
row.append(label, error);
|
|
117
|
+
m.input = box;
|
|
118
|
+
m.label = caption;
|
|
119
|
+
m.error = error;
|
|
120
|
+
return row;
|
|
121
|
+
}
|
|
122
|
+
// A slider: the browser's range input on a track filled up to where it stands, and the number it
|
|
123
|
+
// stands at after it, written in the active language. Its arrows, Home, End and Page keys move it.
|
|
124
|
+
export function buildSlider(face, m) {
|
|
125
|
+
const id = `tb-${newId()}`;
|
|
126
|
+
const wrap = el('div', 'tb-field');
|
|
127
|
+
const label = el('label', 'tb-label');
|
|
128
|
+
label.htmlFor = id;
|
|
129
|
+
const control = el('div', 'tb-control');
|
|
130
|
+
const line = el('div', 'tb-slider');
|
|
131
|
+
const input = el('input', 'tb-range');
|
|
132
|
+
input.type = 'range';
|
|
133
|
+
input.id = id;
|
|
134
|
+
const name = word(face, 'name');
|
|
135
|
+
if (name !== null)
|
|
136
|
+
input.name = name;
|
|
137
|
+
const out = el('output', 'tb-slider-value');
|
|
138
|
+
out.htmlFor.add(id);
|
|
139
|
+
const error = el('div', 'tb-error');
|
|
140
|
+
error.id = `${id}-error`;
|
|
141
|
+
error.setAttribute('aria-live', 'polite');
|
|
142
|
+
input.setAttribute('aria-describedby', error.id);
|
|
143
|
+
input.addEventListener('input', () => {
|
|
144
|
+
m.typing++;
|
|
145
|
+
const settled = () => { m.typing--; };
|
|
146
|
+
void fire('ui-input', face, string(input.value)).then(settled, settled);
|
|
147
|
+
});
|
|
148
|
+
// letting it go, or a key that moved it, is leaving it: its rules show
|
|
149
|
+
input.addEventListener('change', () => void fire('ui-blur', face));
|
|
150
|
+
line.append(input, out);
|
|
151
|
+
control.append(line, error);
|
|
152
|
+
wrap.append(label, control);
|
|
153
|
+
m.input = input;
|
|
154
|
+
m.label = label;
|
|
155
|
+
m.error = error;
|
|
156
|
+
m.box = out;
|
|
157
|
+
return wrap;
|
|
158
|
+
}
|
|
159
|
+
export function ruleAmount(face, rule) {
|
|
160
|
+
const rules = get(face, 'rules');
|
|
161
|
+
if (rules.t !== K.Block)
|
|
162
|
+
return null;
|
|
163
|
+
for (const r of rules.s.data) {
|
|
164
|
+
if (r.t === K.Block && r.s.data[0]?.t === K.Word && spell(r.s.data[0].sym) === rule) {
|
|
165
|
+
const v = r.s.data[1];
|
|
166
|
+
return v === undefined ? null : amountOf(v);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
export function syncSlider(m, err) {
|
|
172
|
+
const f = m.face;
|
|
173
|
+
const input = m.input;
|
|
174
|
+
m.label.textContent = str(f, 'text') ?? '';
|
|
175
|
+
m.label.classList.toggle('tb-required', required(f));
|
|
176
|
+
const low = ruleAmount(f, 'min') ?? 0;
|
|
177
|
+
const high = ruleAmount(f, 'max') ?? 100;
|
|
178
|
+
const step = amountOf(get(f, 'step')) ?? 1;
|
|
179
|
+
input.min = String(low);
|
|
180
|
+
input.max = String(high);
|
|
181
|
+
input.step = String(step);
|
|
182
|
+
const v = get(f, 'value');
|
|
183
|
+
const at = amountOf(v) ?? low;
|
|
184
|
+
// while it is dragged, the browser draws it where the pointer is
|
|
185
|
+
if (m.typing === 0 && input.valueAsNumber !== at)
|
|
186
|
+
input.value = String(at);
|
|
187
|
+
const shown = v.t === K.None ? low.toLocaleString(activeLanguage(host.rt)) : display(v);
|
|
188
|
+
m.box.textContent = shown;
|
|
189
|
+
input.setAttribute('aria-valuetext', shown);
|
|
190
|
+
input.style.setProperty('--tb-at', `${high > low ? ((at - low) / (high - low)) * 100 : 0}%`);
|
|
191
|
+
input.disabled = flag(f, 'disabled') || flag(f, 'readonly');
|
|
192
|
+
input.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
193
|
+
m.error.textContent = err ?? '';
|
|
194
|
+
}
|
|
195
|
+
export function buildRadio(face, m) {
|
|
196
|
+
const id = `tb-${newId()}`;
|
|
197
|
+
const wrap = el('div', 'tb-field');
|
|
198
|
+
const label = el('span', 'tb-label');
|
|
199
|
+
label.id = id;
|
|
200
|
+
const control = el('div', 'tb-control');
|
|
201
|
+
const group = el('div', 'tb-radios');
|
|
202
|
+
group.setAttribute('role', 'radiogroup');
|
|
203
|
+
group.setAttribute('aria-labelledby', id);
|
|
204
|
+
const error = el('div', 'tb-error');
|
|
205
|
+
error.id = `${id}-error`;
|
|
206
|
+
error.setAttribute('aria-live', 'polite');
|
|
207
|
+
group.setAttribute('aria-describedby', error.id);
|
|
208
|
+
m.radios = [];
|
|
209
|
+
const radio = (o) => {
|
|
210
|
+
const option = el('label', 'tb-check');
|
|
211
|
+
const r = el('input');
|
|
212
|
+
r.type = 'radio';
|
|
213
|
+
r.name = id;
|
|
214
|
+
r.value = optionValue(o);
|
|
215
|
+
r.addEventListener('change', () => {
|
|
216
|
+
if (r.checked)
|
|
217
|
+
void fire('ui-input', face, string(r.value)).then(() => fire('ui-blur', face));
|
|
218
|
+
});
|
|
219
|
+
const caption = el('span');
|
|
220
|
+
caption.textContent = optionText(o);
|
|
221
|
+
option.append(r, caption);
|
|
222
|
+
m.radios.push(r);
|
|
223
|
+
return option;
|
|
224
|
+
};
|
|
225
|
+
// a group of options: its title over them, the group named by it
|
|
226
|
+
for (const e of entriesOf(face)) {
|
|
227
|
+
if (isGroup(e)) {
|
|
228
|
+
const sub = el('div', 'tb-radio-group');
|
|
229
|
+
sub.setAttribute('role', 'group');
|
|
230
|
+
const title = el('span', 'tb-radio-title');
|
|
231
|
+
title.id = `tb-${newId()}`;
|
|
232
|
+
title.textContent = translate(e.title, host.rt);
|
|
233
|
+
sub.setAttribute('aria-labelledby', title.id);
|
|
234
|
+
const items = el('div', 'tb-radios');
|
|
235
|
+
items.append(...e.options.map(radio));
|
|
236
|
+
sub.append(title, items);
|
|
237
|
+
group.append(sub);
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
group.append(radio(e));
|
|
241
|
+
}
|
|
242
|
+
control.append(group, error);
|
|
243
|
+
wrap.append(label, control);
|
|
244
|
+
m.label = label;
|
|
245
|
+
m.error = error;
|
|
246
|
+
return wrap;
|
|
247
|
+
}
|
|
248
|
+
export function buildProgress(m) {
|
|
249
|
+
const wrap = el('div', 'tb-progress-wrap');
|
|
250
|
+
const label = el('span', 'tb-progress-label');
|
|
251
|
+
label.id = `tb-${newId()}`;
|
|
252
|
+
const track = el('div', 'tb-progress');
|
|
253
|
+
track.setAttribute('role', 'progressbar');
|
|
254
|
+
track.setAttribute('aria-valuemin', '0');
|
|
255
|
+
track.setAttribute('aria-valuemax', '100');
|
|
256
|
+
track.append(el('div', 'tb-progress-bar'));
|
|
257
|
+
wrap.append(label, track);
|
|
258
|
+
m.label = label;
|
|
259
|
+
m.box = track;
|
|
260
|
+
return wrap;
|
|
261
|
+
}
|
|
262
|
+
/** A progress value as a fraction, or null while it is unknown. */
|
|
263
|
+
export function fraction(v) {
|
|
264
|
+
const n = v.t === K.Percent || v.t === K.Decimal ? v.v : v.t === K.Integer ? Number(v.v) : null;
|
|
265
|
+
return n === null || Number.isNaN(n) ? null : Math.min(1, Math.max(0, n));
|
|
266
|
+
}
|
|
267
|
+
export function syncProgress(m) {
|
|
268
|
+
const t = str(m.face, 'text');
|
|
269
|
+
const label = m.label;
|
|
270
|
+
label.textContent = t ?? '';
|
|
271
|
+
label.hidden = t === null;
|
|
272
|
+
const track = m.box;
|
|
273
|
+
if (t !== null)
|
|
274
|
+
track.setAttribute('aria-labelledby', label.id);
|
|
275
|
+
else
|
|
276
|
+
track.removeAttribute('aria-labelledby');
|
|
277
|
+
const n = fraction(get(m.face, 'value'));
|
|
278
|
+
track.classList.toggle('tb-indeterminate', n === null);
|
|
279
|
+
const bar = track.firstChild;
|
|
280
|
+
if (n === null) {
|
|
281
|
+
track.removeAttribute('aria-valuenow');
|
|
282
|
+
bar.style.removeProperty('width');
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
track.setAttribute('aria-valuenow', String(Math.round(n * 100)));
|
|
286
|
+
bar.style.width = `${n * 100}%`;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
export function syncIcon(m) {
|
|
290
|
+
const name = iconName(m.face) ?? '';
|
|
291
|
+
const label = str(m.face, 'text');
|
|
292
|
+
const key = `${name} ${label ?? ''}`;
|
|
293
|
+
if (m.el.dataset['icon'] === key)
|
|
294
|
+
return;
|
|
295
|
+
m.el.dataset['icon'] = key;
|
|
296
|
+
const svg = name !== '' ? icon(name, label) : null;
|
|
297
|
+
m.el.replaceChildren(...(svg !== null ? [svg] : []));
|
|
298
|
+
}
|
|
299
|
+
/** No rule applies to a read-only face, so it never asks for a value. */
|
|
300
|
+
export function required(f) {
|
|
301
|
+
return hasRule(f, 'required') && !flag(f, 'readonly');
|
|
302
|
+
}
|
|
303
|
+
export function syncRadio(m, err) {
|
|
304
|
+
const f = m.face;
|
|
305
|
+
m.label.textContent = str(f, 'text') ?? '';
|
|
306
|
+
m.label.classList.toggle('tb-required', required(f));
|
|
307
|
+
const v = get(f, 'value');
|
|
308
|
+
const want = v.t === K.None ? null : form(v);
|
|
309
|
+
for (const r of m.radios) {
|
|
310
|
+
r.checked = r.value === want;
|
|
311
|
+
r.disabled = flag(f, 'disabled') || flag(f, 'readonly');
|
|
312
|
+
}
|
|
313
|
+
const group = m.radios[0]?.closest('[role="radiogroup"]');
|
|
314
|
+
group?.querySelectorAll('.tb-radio-title').forEach((t, i) => {
|
|
315
|
+
const g = entriesOf(f).filter(isGroup)[i];
|
|
316
|
+
if (g)
|
|
317
|
+
t.textContent = translate(g.title, host.rt);
|
|
318
|
+
});
|
|
319
|
+
group?.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
320
|
+
group?.setAttribute('aria-required', required(f) ? 'true' : 'false');
|
|
321
|
+
m.error.textContent = err ?? '';
|
|
322
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell } from '../../core/index.ts';
|
|
2
|
+
export declare const MODIFIER_KEYS: Set<string>;
|
|
3
|
+
/** Keys that are characters in Red's event/key. */
|
|
4
|
+
export declare const KEY_CHARS: Record<string, number>;
|
|
5
|
+
/** Keys that are words in Red's event/key. */
|
|
6
|
+
export declare const KEY_WORDS: Record<string, string>;
|
|
7
|
+
/** A key as Red gives it in event/key: a char! (#"a", #"^M" for Enter) or a word ('left, 'F2). */
|
|
8
|
+
export declare function keyCell(ev: KeyboardEvent): Cell | null;
|
|
9
|
+
export declare function keyWords(ev: KeyboardEvent): [string, Cell][];
|
|
10
|
+
export declare const APPLE: boolean;
|
|
11
|
+
export declare const KEY_NAMES: Record<string, string>;
|
|
12
|
+
export interface Shortcut {
|
|
13
|
+
key: string;
|
|
14
|
+
ctrl: boolean;
|
|
15
|
+
shift: boolean;
|
|
16
|
+
alt: boolean;
|
|
17
|
+
meta: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** "Ctrl+Shift+K" -> its parts; Ctrl also means Cmd on Apple devices. */
|
|
20
|
+
export declare function parseShortcut(spec: string): Shortcut;
|
|
21
|
+
export declare function matches(ev: KeyboardEvent, s: Shortcut): boolean;
|
|
22
|
+
/** How a shortcut is announced (aria-keyshortcuts) and shown (tooltip). */
|
|
23
|
+
export declare function shortcutAria(spec: string): string;
|
|
24
|
+
export declare function shortcutLabel(spec: string): string;
|
|
25
|
+
/** Shortcuts beat the browser's own keys; the view's on-key gets keys nobody handled. */
|
|
26
|
+
export declare function onKeyDown(ev: KeyboardEvent): void;
|
|
27
|
+
/** Opens the palette of the view on show: what it finds comes from ui-palette. */
|
|
28
|
+
export declare function showPalette(view: ObjectCell): void;
|
|
29
|
+
export declare function onViewKey(ev: KeyboardEvent): void;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// --- keys -----------------------------------------------------------------------
|
|
2
|
+
//
|
|
3
|
+
// Keys as Trebol sees them (event/key), shortcuts of buttons and links, and the keys of the page as a whole.
|
|
4
|
+
import { K, TRUE, FALSE, string, form, spell, text, wordOf, char } from "../../core/index.js";
|
|
5
|
+
import { openPalette } from "../palette.js";
|
|
6
|
+
import { inDevtools } from "../devtools.js";
|
|
7
|
+
import { str, own } from "./faces.js";
|
|
8
|
+
import { byCtx, fire } from "./mount.js";
|
|
9
|
+
import { fireEvent, seenKeys, viewKeys, shortcuts } from "./events.js";
|
|
10
|
+
import { undoToasts, inTextBox } from "./dialogs.js";
|
|
11
|
+
import { rootFace } from "./page.js";
|
|
12
|
+
export const MODIFIER_KEYS = new Set(['Shift', 'Control', 'Alt', 'Meta', 'AltGraph', 'CapsLock', 'Dead', 'Unidentified']);
|
|
13
|
+
/** Keys that are characters in Red's event/key. */
|
|
14
|
+
export const KEY_CHARS = { Enter: 13, Escape: 27, Tab: 9, Backspace: 8 };
|
|
15
|
+
/** Keys that are words in Red's event/key. */
|
|
16
|
+
export const KEY_WORDS = {
|
|
17
|
+
ArrowLeft: 'left', ArrowRight: 'right', ArrowUp: 'up', ArrowDown: 'down', Home: 'home', End: 'end',
|
|
18
|
+
PageUp: 'page-up', PageDown: 'page-down', Insert: 'insert', Delete: 'delete',
|
|
19
|
+
};
|
|
20
|
+
/** A key as Red gives it in event/key: a char! (#"a", #"^M" for Enter) or a word ('left, 'F2). */
|
|
21
|
+
export function keyCell(ev) {
|
|
22
|
+
if (MODIFIER_KEYS.has(ev.key))
|
|
23
|
+
return null;
|
|
24
|
+
const c = KEY_CHARS[ev.key];
|
|
25
|
+
if (c !== undefined)
|
|
26
|
+
return char(c);
|
|
27
|
+
const cps = [...ev.key];
|
|
28
|
+
if (cps.length === 1)
|
|
29
|
+
return char(cps[0].codePointAt(0));
|
|
30
|
+
if (/^F\d+$/.test(ev.key))
|
|
31
|
+
return wordOf(K.LitWord, ev.key);
|
|
32
|
+
return wordOf(K.LitWord, KEY_WORDS[ev.key] ?? ev.key.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase());
|
|
33
|
+
}
|
|
34
|
+
export function keyWords(ev) {
|
|
35
|
+
const b = (x) => x ? TRUE : FALSE;
|
|
36
|
+
return [['key', keyCell(ev)], ['ctrl?', b(ev.ctrlKey)], ['shift?', b(ev.shiftKey)], ['alt?', b(ev.altKey)], ['meta?', b(ev.metaKey)]];
|
|
37
|
+
}
|
|
38
|
+
export const APPLE = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform);
|
|
39
|
+
export const KEY_NAMES = {
|
|
40
|
+
esc: 'escape', space: ' ', del: 'delete', up: 'arrowup', down: 'arrowdown', left: 'arrowleft', right: 'arrowright',
|
|
41
|
+
};
|
|
42
|
+
/** "Ctrl+Shift+K" -> its parts; Ctrl also means Cmd on Apple devices. */
|
|
43
|
+
export function parseShortcut(spec) {
|
|
44
|
+
const parts = spec.toLowerCase().split('+').map(p => p.trim());
|
|
45
|
+
const key = parts.pop() ?? '';
|
|
46
|
+
const has = (...names) => parts.some(p => names.includes(p));
|
|
47
|
+
return { key: KEY_NAMES[key] ?? key, ctrl: has('ctrl', 'control'), shift: has('shift'), alt: has('alt', 'option'), meta: has('meta', 'cmd') };
|
|
48
|
+
}
|
|
49
|
+
export function matches(ev, s) {
|
|
50
|
+
const ctrl = APPLE ? ev.metaKey || ev.ctrlKey : ev.ctrlKey;
|
|
51
|
+
if (ctrl !== (s.ctrl || (APPLE && s.meta)) || ev.shiftKey !== s.shift || ev.altKey !== s.alt)
|
|
52
|
+
return false;
|
|
53
|
+
if (!APPLE && ev.metaKey !== s.meta)
|
|
54
|
+
return false;
|
|
55
|
+
// letters and digits by physical key, so Shift or Alt do not change them
|
|
56
|
+
if (/^[a-z]$/.test(s.key))
|
|
57
|
+
return ev.code === `Key${s.key.toUpperCase()}`;
|
|
58
|
+
if (/^[0-9]$/.test(s.key))
|
|
59
|
+
return ev.code === `Digit${s.key}`;
|
|
60
|
+
return ev.key.toLowerCase() === s.key;
|
|
61
|
+
}
|
|
62
|
+
/** How a shortcut is announced (aria-keyshortcuts) and shown (tooltip). */
|
|
63
|
+
export function shortcutAria(spec) {
|
|
64
|
+
return spec.split('+').map(p => {
|
|
65
|
+
const w = p.trim().toLowerCase();
|
|
66
|
+
if (w === 'ctrl' || w === 'control')
|
|
67
|
+
return APPLE ? 'Meta' : 'Control';
|
|
68
|
+
if (w === 'cmd')
|
|
69
|
+
return 'Meta';
|
|
70
|
+
return p.trim().length === 1 ? p.trim().toUpperCase() : p.trim();
|
|
71
|
+
}).join('+');
|
|
72
|
+
}
|
|
73
|
+
export function shortcutLabel(spec) {
|
|
74
|
+
return APPLE ? spec.replace(/ctrl|control/i, 'Cmd') : spec;
|
|
75
|
+
}
|
|
76
|
+
/** Shortcuts beat the browser's own keys; the view's on-key gets keys nobody handled. */
|
|
77
|
+
export function onKeyDown(ev) {
|
|
78
|
+
if (inDevtools(ev.target))
|
|
79
|
+
return;
|
|
80
|
+
const modal = Array.from(document.querySelectorAll('dialog[open]')).pop();
|
|
81
|
+
for (const m of shortcuts) {
|
|
82
|
+
if (!m.el.isConnected) {
|
|
83
|
+
shortcuts.delete(m);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const spec = str(m.face, 'shortcut');
|
|
87
|
+
if (spec === null || !matches(ev, parseShortcut(spec)))
|
|
88
|
+
continue;
|
|
89
|
+
// only what the user can see: not in a hidden tab, a closed dialog or behind a modal
|
|
90
|
+
if (m.el.getClientRects().length === 0 || (modal !== undefined && !modal.contains(m.el)))
|
|
91
|
+
continue;
|
|
92
|
+
ev.preventDefault();
|
|
93
|
+
seenKeys.add(ev);
|
|
94
|
+
m.el.click();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
// Ctrl+Z (⌘Z) out of a text box: the Undo of the newest notice on show; a text box undoes its own typing
|
|
98
|
+
if ((ev.ctrlKey || ev.metaKey) && !ev.altKey && !ev.shiftKey && ev.key.toLowerCase() === 'z' && undoToasts.size > 0 && modal === undefined && !inTextBox(ev.target)) {
|
|
99
|
+
const newest = [...undoToasts.values()].pop();
|
|
100
|
+
ev.preventDefault();
|
|
101
|
+
seenKeys.add(ev);
|
|
102
|
+
newest.querySelector('.tb-toast-undo')?.click();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
// Ctrl+K (⌘K): the palette, unless a button of the app has those keys (above) or a dialog is open
|
|
106
|
+
if ((ev.ctrlKey || ev.metaKey) && !ev.altKey && !ev.shiftKey && ev.key.toLowerCase() === 'k' && rootFace !== null && modal === undefined) {
|
|
107
|
+
ev.preventDefault();
|
|
108
|
+
seenKeys.add(ev);
|
|
109
|
+
showPalette(rootFace);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Opens the palette of the view on show: what it finds comes from ui-palette. */
|
|
113
|
+
export function showPalette(view) {
|
|
114
|
+
openPalette({
|
|
115
|
+
label: own('palette'),
|
|
116
|
+
placeholder: own('palette-search'),
|
|
117
|
+
nothing: own('palette-none'),
|
|
118
|
+
search: async (typed) => {
|
|
119
|
+
const got = await fire('ui-palette', view, string(typed));
|
|
120
|
+
if (got.t !== K.Block)
|
|
121
|
+
return [];
|
|
122
|
+
const d = got.s.data.slice(got.i);
|
|
123
|
+
const out = [];
|
|
124
|
+
for (let i = 0; i + 3 < d.length; i += 4) {
|
|
125
|
+
const kind = d[i + 1].t === K.Word ? spell(d[i + 1].sym) : '';
|
|
126
|
+
const keys = d[i + 2].t === K.String ? text(d[i + 2]) : null;
|
|
127
|
+
const hint = kind === 'screen' ? own('palette-screen') : keys !== null ? shortcutLabel(keys) : '';
|
|
128
|
+
out.push({ label: form(d[i]), hint, target: [kind, d[i + 3]] });
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
},
|
|
132
|
+
pick: e => {
|
|
133
|
+
const [kind, target] = e.target;
|
|
134
|
+
// a button or link is clicked, as the user would
|
|
135
|
+
if (kind === 'action' && target.t === K.Object) {
|
|
136
|
+
const m = byCtx.get(target.ctx);
|
|
137
|
+
if (m?.el.isConnected === true)
|
|
138
|
+
m.el.click();
|
|
139
|
+
}
|
|
140
|
+
else
|
|
141
|
+
void fire('ui-palette-pick', view, wordOf(K.Word, kind), target);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
export function onViewKey(ev) {
|
|
146
|
+
if (inDevtools(ev.target))
|
|
147
|
+
return;
|
|
148
|
+
if (viewKeys === null || seenKeys.has(ev) || keyCell(ev) === null)
|
|
149
|
+
return;
|
|
150
|
+
fireEvent(viewKeys, 'on-key', keyWords(ev));
|
|
151
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell } from '../../core/index.ts';
|
|
2
|
+
import { type Mounted } from './mount.ts';
|
|
3
|
+
export interface OpenLookup {
|
|
4
|
+
m: Mounted;
|
|
5
|
+
list: HTMLUListElement;
|
|
6
|
+
shown: Cell | null;
|
|
7
|
+
items: Cell[];
|
|
8
|
+
active: number;
|
|
9
|
+
}
|
|
10
|
+
export declare let lookup: OpenLookup | null;
|
|
11
|
+
export declare let lookupTimer: ReturnType<typeof setTimeout> | undefined;
|
|
12
|
+
export declare const LOOKUP_WAIT_MS = 200;
|
|
13
|
+
export declare function lookupValues(f: ObjectCell): Cell[];
|
|
14
|
+
export declare function lookupText(f: ObjectCell, v: Cell): string;
|
|
15
|
+
/** Where a readonly lookup's value links to, from its `links` map, or null to show it as text. */
|
|
16
|
+
export declare function lookupHref(f: ObjectCell, v: Cell): string | null;
|
|
17
|
+
export declare function buildLookup(face: ObjectCell, m: Mounted): HTMLElement;
|
|
18
|
+
export declare function syncLookup(m: Mounted, err: string | null): void;
|
|
19
|
+
export declare function openLookup(m: Mounted, input: HTMLInputElement, query: string | null): void;
|
|
20
|
+
export declare function placeLookup(): void;
|
|
21
|
+
export declare function closeLookup(): void;
|
|
22
|
+
export declare function renderLookup(): void;
|
|
23
|
+
export declare function pickLookup(option: Cell): void;
|
|
24
|
+
export declare function moveLookup(by: number): void;
|
|
25
|
+
export declare function lookupKey(m: Mounted, input: HTMLInputElement, ev: KeyboardEvent): void;
|