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