trebol 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -1
- package/dist/cli.js +17097 -12238
- package/dist/fonts/OFL.txt +93 -0
- package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/fonts/inter-greek.woff2 +0 -0
- package/dist/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/core/boot.js +12 -8
- package/dist/lib/core/eval.js +2 -2
- package/dist/lib/core/func.d.ts +4 -0
- package/dist/lib/core/machine/checks.d.ts +1 -0
- package/dist/lib/core/machine/checks.js +250 -0
- package/dist/lib/core/machine/events.d.ts +1 -0
- package/dist/lib/core/machine/events.js +365 -0
- package/dist/lib/core/machine/running.d.ts +1 -0
- package/dist/lib/core/machine/running.js +180 -0
- package/dist/lib/core/machine/shared.d.ts +1 -0
- package/dist/lib/core/machine/shared.js +123 -0
- package/dist/lib/core/machine/snapshot.d.ts +1 -0
- package/dist/lib/core/machine/snapshot.js +244 -0
- package/dist/lib/core/machine/spec.d.ts +1 -0
- package/dist/lib/core/machine/spec.js +274 -0
- package/dist/lib/core/machine/words.d.ts +1 -0
- package/dist/lib/core/machine/words.js +98 -0
- package/dist/lib/core/machine.d.ts +1 -0
- package/dist/lib/core/machine.js +17 -0
- package/dist/lib/core/mezz.js +31 -0
- package/dist/lib/core/natives/control.js +2 -2
- package/dist/lib/core/natives/data.js +6 -1
- package/dist/lib/core/natives/series.js +59 -9
- package/dist/lib/core/natives/text.js +3 -1
- package/dist/lib/host/browser/calendar.d.ts +34 -0
- package/dist/lib/host/browser/calendar.js +268 -0
- package/dist/lib/host/browser/count.d.ts +15 -0
- package/dist/lib/host/browser/count.js +48 -0
- package/dist/lib/host/browser/dialogs.d.ts +39 -0
- package/dist/lib/host/browser/dialogs.js +443 -0
- package/dist/lib/host/browser/drag.d.ts +70 -0
- package/dist/lib/host/browser/drag.js +268 -0
- package/dist/lib/host/browser/drawings.d.ts +30 -0
- package/dist/lib/host/browser/drawings.js +176 -0
- package/dist/lib/host/browser/events.d.ts +16 -0
- package/dist/lib/host/browser/events.js +125 -0
- package/dist/lib/host/browser/faces.d.ts +35 -0
- package/dist/lib/host/browser/faces.js +131 -0
- package/dist/lib/host/browser/fields.d.ts +16 -0
- package/dist/lib/host/browser/fields.js +322 -0
- package/dist/lib/host/browser/keys.d.ts +29 -0
- package/dist/lib/host/browser/keys.js +151 -0
- package/dist/lib/host/browser/lookup.d.ts +25 -0
- package/dist/lib/host/browser/lookup.js +275 -0
- package/dist/lib/host/browser/menus.d.ts +20 -0
- package/dist/lib/host/browser/menus.js +149 -0
- package/dist/lib/host/browser/mount.d.ts +92 -0
- package/dist/lib/host/browser/mount.js +448 -0
- package/dist/lib/host/browser/orbits.d.ts +90 -0
- package/dist/lib/host/browser/orbits.js +379 -0
- package/dist/lib/host/browser/page.d.ts +13 -0
- package/dist/lib/host/browser/page.js +35 -0
- package/dist/lib/host/browser/richtext.d.ts +19 -0
- package/dist/lib/host/browser/richtext.js +379 -0
- package/dist/lib/host/browser/row-faces.d.ts +5 -0
- package/dist/lib/host/browser/row-faces.js +74 -0
- package/dist/lib/host/browser/screens.d.ts +37 -0
- package/dist/lib/host/browser/screens.js +140 -0
- package/dist/lib/host/browser/swipe.d.ts +31 -0
- package/dist/lib/host/browser/swipe.js +178 -0
- package/dist/lib/host/browser/sync.d.ts +24 -0
- package/dist/lib/host/browser/sync.js +450 -0
- package/dist/lib/host/browser/tables.d.ts +65 -0
- package/dist/lib/host/browser/tables.js +505 -0
- package/dist/lib/host/browser/times.d.ts +14 -0
- package/dist/lib/host/browser/times.js +114 -0
- package/dist/lib/host/browser/toolbars.d.ts +17 -0
- package/dist/lib/host/browser/toolbars.js +129 -0
- package/dist/lib/host/browser/trees.d.ts +20 -0
- package/dist/lib/host/browser/trees.js +235 -0
- package/dist/lib/host/browser/uploads.d.ts +29 -0
- package/dist/lib/host/browser/uploads.js +370 -0
- package/dist/lib/host/browser/widget.d.ts +13 -0
- package/dist/lib/host/browser/widget.js +105 -0
- package/dist/lib/host/browser.d.ts +25 -0
- package/dist/lib/host/browser.js +129 -3964
- package/dist/lib/host/devlog.d.ts +11 -0
- package/dist/lib/host/devlog.js +13 -0
- package/dist/lib/host/devtools/console.d.ts +9 -0
- package/dist/lib/host/devtools/console.js +104 -0
- package/dist/lib/host/devtools/faces.d.ts +25 -0
- package/dist/lib/host/devtools/faces.js +169 -0
- package/dist/lib/host/devtools/log.d.ts +3 -0
- package/dist/lib/host/devtools/log.js +32 -0
- package/dist/lib/host/devtools/machines.d.ts +34 -0
- package/dist/lib/host/devtools/machines.js +212 -0
- package/dist/lib/host/devtools/read.d.ts +13 -0
- package/dist/lib/host/devtools/read.js +52 -0
- package/dist/lib/host/devtools/state.d.ts +90 -0
- package/dist/lib/host/devtools/state.js +123 -0
- package/dist/lib/host/devtools.d.ts +19 -0
- package/dist/lib/host/devtools.js +238 -0
- package/dist/lib/host/fonts/OFL.txt +93 -0
- package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
- package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
- package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
- package/dist/lib/host/fonts.d.ts +11 -0
- package/dist/lib/host/fonts.js +30 -0
- package/dist/lib/host/icons.js +4 -0
- package/dist/lib/host/index.d.ts +5 -1
- package/dist/lib/host/index.js +110 -5
- package/dist/lib/host/js.d.ts +5 -0
- package/dist/lib/host/js.js +130 -0
- package/dist/lib/host/locale.d.ts +18 -0
- package/dist/lib/host/locale.js +75 -5
- package/dist/lib/host/markdown.d.ts +6 -0
- package/dist/lib/host/markdown.js +18 -0
- package/dist/lib/host/modules.d.ts +4 -0
- package/dist/lib/host/modules.js +157 -20
- package/dist/lib/host/motion.d.ts +32 -0
- package/dist/lib/host/motion.js +190 -0
- package/dist/lib/host/net.js +14 -3
- package/dist/lib/host/orbit.d.ts +121 -0
- package/dist/lib/host/orbit.js +374 -0
- package/dist/lib/host/palette.d.ts +17 -0
- package/dist/lib/host/palette.js +131 -0
- package/dist/lib/host/rows.d.ts +14 -0
- package/dist/lib/host/rows.js +29 -0
- package/dist/lib/host/scheduler.d.ts +8 -0
- package/dist/lib/host/scheduler.js +64 -36
- package/dist/lib/host/share.d.ts +31 -0
- package/dist/lib/host/share.js +134 -0
- package/dist/lib/host/theme/classic.d.ts +1 -0
- package/dist/lib/host/theme/classic.js +120 -0
- package/dist/lib/host/theme/cupertino.d.ts +1 -0
- package/dist/lib/host/theme/cupertino.js +65 -0
- package/dist/lib/host/theme/data.d.ts +1 -0
- package/dist/lib/host/theme/data.js +109 -0
- package/dist/lib/host/theme/devtools.d.ts +1 -0
- package/dist/lib/host/theme/devtools.js +166 -0
- package/dist/lib/host/theme/dialogs.d.ts +1 -0
- package/dist/lib/host/theme/dialogs.js +63 -0
- package/dist/lib/host/theme/fluent.d.ts +1 -0
- package/dist/lib/host/theme/fluent.js +64 -0
- package/dist/lib/host/theme/forms.d.ts +1 -0
- package/dist/lib/host/theme/forms.js +239 -0
- package/dist/lib/host/theme/fusion.d.ts +1 -0
- package/dist/lib/host/theme/fusion.js +66 -0
- package/dist/lib/host/theme/layout.d.ts +1 -0
- package/dist/lib/host/theme/layout.js +166 -0
- package/dist/lib/host/theme/media.d.ts +1 -0
- package/dist/lib/host/theme/media.js +106 -0
- package/dist/lib/host/theme/navigation.d.ts +1 -0
- package/dist/lib/host/theme/navigation.js +36 -0
- package/dist/lib/host/theme/panes.d.ts +1 -0
- package/dist/lib/host/theme/panes.js +99 -0
- package/dist/lib/host/theme/popups.d.ts +1 -0
- package/dist/lib/host/theme/popups.js +149 -0
- package/dist/lib/host/theme/responsive.d.ts +1 -0
- package/dist/lib/host/theme/responsive.js +103 -0
- package/dist/lib/host/theme/toasts.d.ts +1 -0
- package/dist/lib/host/theme/toasts.js +62 -0
- package/dist/lib/host/theme.js +19 -1179
- package/dist/lib/ui/dialect/events.trb +276 -0
- package/dist/lib/ui/dialect/faces.trb +135 -0
- package/dist/lib/ui/dialect/layout.trb +283 -0
- package/dist/lib/ui/dialect/palette.trb +87 -0
- package/dist/lib/ui/dialect/problems.trb +174 -0
- package/dist/lib/ui/dialect/reactions.trb +198 -0
- package/dist/lib/ui/dialect/rows.trb +208 -0
- package/dist/lib/ui/dialect/screens.trb +293 -0
- package/dist/lib/ui/dialect/steps.trb +203 -0
- package/dist/lib/ui/dialect/validation.trb +137 -0
- package/dist/lib/ui/dialect/view.trb +110 -0
- package/dist/lib/ui/dialect/walking.trb +98 -0
- package/dist/lib/ui/dialect/words.trb +70 -0
- package/dist/lib/ui/i18n.trb +23 -0
- package/dist/lib/ui/test.trb +12 -0
- package/dist/lib/ui/testing/actions.trb +421 -0
- package/dist/lib/ui/testing/faces.trb +148 -0
- package/dist/lib/ui/testing/server.trb +73 -0
- package/dist/lib/ui/ui.trb +25 -1234
- package/dist/trebol.js +4882 -1088
- package/editors/vscode/README.md +15 -0
- package/editors/vscode/language-configuration.json +28 -0
- package/editors/vscode/package.json +42 -0
- package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
- package/package.json +7 -3
- package/src/core/boot.ts +10 -7
- package/src/core/eval.ts +2 -2
- package/src/core/func.ts +4 -0
- package/src/core/machine/checks.ts +251 -0
- package/src/core/machine/events.ts +366 -0
- package/src/core/machine/running.ts +181 -0
- package/src/core/machine/shared.ts +124 -0
- package/src/core/machine/snapshot.ts +245 -0
- package/src/core/machine/spec.ts +275 -0
- package/src/core/machine/words.ts +99 -0
- package/src/core/machine.ts +20 -0
- package/src/core/mezz.ts +31 -0
- package/src/core/natives/control.ts +2 -2
- package/src/core/natives/data.ts +6 -1
- package/src/core/natives/parse.ts +1 -1
- package/src/core/natives/series.ts +55 -11
- package/src/core/natives/text.ts +2 -1
- package/src/host/browser/calendar.ts +264 -0
- package/src/host/browser/count.ts +48 -0
- package/src/host/browser/dialogs.ts +439 -0
- package/src/host/browser/drag.ts +278 -0
- package/src/host/browser/drawings.ts +178 -0
- package/src/host/browser/events.ts +117 -0
- package/src/host/browser/faces.ts +143 -0
- package/src/host/browser/fields.ts +315 -0
- package/src/host/browser/keys.ts +145 -0
- package/src/host/browser/lookup.ts +262 -0
- package/src/host/browser/menus.ts +146 -0
- package/src/host/browser/mount.ts +420 -0
- package/src/host/browser/orbits.ts +418 -0
- package/src/host/browser/page.ts +43 -0
- package/src/host/browser/richtext.ts +346 -0
- package/src/host/browser/row-faces.ts +75 -0
- package/src/host/browser/screens.ts +142 -0
- package/src/host/browser/swipe.ts +168 -0
- package/src/host/browser/sync.ts +354 -0
- package/src/host/browser/tables.ts +461 -0
- package/src/host/browser/times.ts +115 -0
- package/src/host/browser/toolbars.ts +126 -0
- package/src/host/browser/trees.ts +205 -0
- package/src/host/browser/uploads.ts +367 -0
- package/src/host/browser/widget.ts +103 -0
- package/src/host/browser.ts +128 -3747
- package/src/host/devlog.ts +16 -0
- package/src/host/devtools/console.ts +102 -0
- package/src/host/devtools/faces.ts +160 -0
- package/src/host/devtools/log.ts +34 -0
- package/src/host/devtools/machines.ts +208 -0
- package/src/host/devtools/read.ts +58 -0
- package/src/host/devtools/state.ts +148 -0
- package/src/host/devtools.ts +238 -0
- package/src/host/fonts/OFL.txt +93 -0
- package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
- package/src/host/fonts/inter-cyrillic.woff2 +0 -0
- package/src/host/fonts/inter-greek-ext.woff2 +0 -0
- package/src/host/fonts/inter-greek.woff2 +0 -0
- package/src/host/fonts/inter-latin-ext.woff2 +0 -0
- package/src/host/fonts/inter-latin.woff2 +0 -0
- package/src/host/fonts/inter-vietnamese.woff2 +0 -0
- package/src/host/fonts.ts +40 -0
- package/src/host/icons.ts +4 -0
- package/src/host/index.ts +110 -5
- package/src/host/js.ts +127 -0
- package/src/host/locale.ts +71 -5
- package/src/host/markdown.ts +18 -0
- package/src/host/modules.ts +150 -22
- package/src/host/motion.ts +204 -0
- package/src/host/net.ts +13 -3
- package/src/host/orbit.ts +432 -0
- package/src/host/palette.ts +139 -0
- package/src/host/rows.ts +30 -0
- package/src/host/scheduler.ts +67 -36
- package/src/host/share.ts +168 -0
- package/src/host/theme/classic.ts +121 -0
- package/src/host/theme/cupertino.ts +66 -0
- package/src/host/theme/data.ts +110 -0
- package/src/host/theme/devtools.ts +167 -0
- package/src/host/theme/dialogs.ts +64 -0
- package/src/host/theme/fluent.ts +65 -0
- package/src/host/theme/forms.ts +240 -0
- package/src/host/theme/fusion.ts +67 -0
- package/src/host/theme/layout.ts +167 -0
- package/src/host/theme/media.ts +107 -0
- package/src/host/theme/navigation.ts +37 -0
- package/src/host/theme/panes.ts +100 -0
- package/src/host/theme/popups.ts +150 -0
- package/src/host/theme/responsive.ts +104 -0
- package/src/host/theme/toasts.ts +63 -0
- package/src/host/theme.ts +19 -1179
- package/src/ui/dialect/events.trb +276 -0
- package/src/ui/dialect/faces.trb +135 -0
- package/src/ui/dialect/layout.trb +283 -0
- package/src/ui/dialect/palette.trb +87 -0
- package/src/ui/dialect/problems.trb +174 -0
- package/src/ui/dialect/reactions.trb +198 -0
- package/src/ui/dialect/rows.trb +208 -0
- package/src/ui/dialect/screens.trb +293 -0
- package/src/ui/dialect/steps.trb +203 -0
- package/src/ui/dialect/validation.trb +137 -0
- package/src/ui/dialect/view.trb +110 -0
- package/src/ui/dialect/walking.trb +98 -0
- package/src/ui/dialect/words.trb +70 -0
- package/src/ui/i18n.trb +23 -0
- package/src/ui/test.trb +12 -0
- package/src/ui/testing/actions.trb +421 -0
- package/src/ui/testing/faces.trb +148 -0
- package/src/ui/testing/server.trb +73 -0
- package/src/ui/ui.trb +25 -1234
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
// An upload: a button that is also a place to drop files, and the list of the
|
|
2
|
+
// chosen ones. Files go to Trebol as handles; their bytes stay in the browser.
|
|
3
|
+
|
|
4
|
+
import { K, string, form, blockOf, type Cell, type ObjectCell } from '../../core/index.ts';
|
|
5
|
+
import { activeLanguage, fileSize } from '../index.ts';
|
|
6
|
+
import { handleOf } from '../scheduler.ts';
|
|
7
|
+
import { fileHandle } from '../net.ts';
|
|
8
|
+
import { get, str, own, flag, word, icon } from './faces.ts';
|
|
9
|
+
import { type Mounted, host, newId, fire, el } from './mount.ts';
|
|
10
|
+
import { int } from './drag.ts';
|
|
11
|
+
import { fraction, required } from './fields.ts';
|
|
12
|
+
|
|
13
|
+
export function buildUpload(face: ObjectCell, m: Mounted): HTMLElement {
|
|
14
|
+
const id = `tb-${newId()}`;
|
|
15
|
+
const wrap = el('div', 'tb-field');
|
|
16
|
+
const label = el('label', 'tb-label');
|
|
17
|
+
label.htmlFor = id;
|
|
18
|
+
const control = el('div', 'tb-control');
|
|
19
|
+
const zone = el('div', 'tb-upload');
|
|
20
|
+
const picker = el('input');
|
|
21
|
+
picker.type = 'file';
|
|
22
|
+
picker.hidden = true;
|
|
23
|
+
picker.tabIndex = -1;
|
|
24
|
+
const choose = el('button', 'tb-button');
|
|
25
|
+
choose.type = 'button';
|
|
26
|
+
choose.id = id;
|
|
27
|
+
const svg = icon('upload');
|
|
28
|
+
if (svg !== null) choose.append(svg);
|
|
29
|
+
choose.append(el('span'));
|
|
30
|
+
const hint = el('span', 'tb-upload-hint');
|
|
31
|
+
const error = el('div', 'tb-error');
|
|
32
|
+
error.id = `${id}-error`;
|
|
33
|
+
error.setAttribute('aria-live', 'polite');
|
|
34
|
+
choose.setAttribute('aria-describedby', error.id);
|
|
35
|
+
const list = el('ul', 'tb-upload-list');
|
|
36
|
+
const track = sentBar(label, id);
|
|
37
|
+
choose.addEventListener('click', () => picker.click());
|
|
38
|
+
picker.addEventListener('change', () => {
|
|
39
|
+
const files = [...(picker.files ?? [])];
|
|
40
|
+
picker.value = '';
|
|
41
|
+
if (files.length > 0) chooseFiles(face, files);
|
|
42
|
+
});
|
|
43
|
+
dropFiles(face, zone, choose);
|
|
44
|
+
zone.append(choose, hint, picker);
|
|
45
|
+
control.append(zone, list, track, error);
|
|
46
|
+
wrap.append(label, control);
|
|
47
|
+
m.label = label;
|
|
48
|
+
m.error = error;
|
|
49
|
+
m.button = choose;
|
|
50
|
+
m.box = list;
|
|
51
|
+
return wrap;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The accept list of an upload: extensions ("pdf") and types ("image/*"); a photo takes images unless told. */
|
|
55
|
+
export function acceptList(face: ObjectCell): string[] {
|
|
56
|
+
const a = get(face, 'accept');
|
|
57
|
+
if (a.t !== K.Block && word(face, 'style') === 'photo') return ['image/*'];
|
|
58
|
+
return a.t === K.Block ? a.s.data.slice(a.i).map(x => form(x).trim().toLowerCase()).filter(x => x !== '') : [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The picker's accept attribute for an accept list. */
|
|
62
|
+
export function pickerAccept(list: string[]): string {
|
|
63
|
+
return list.map(x => (x.includes('/') || x.startsWith('.') ? x : `.${x}`)).join(',');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Lets a zone take files dropped on it, unless its button is disabled. */
|
|
67
|
+
export function dropFiles(face: ObjectCell, zone: HTMLElement, button: HTMLButtonElement): void {
|
|
68
|
+
const hasFiles = (ev: DragEvent): boolean => ev.dataTransfer?.types.includes('Files') === true;
|
|
69
|
+
zone.addEventListener('dragover', ev => {
|
|
70
|
+
if (!hasFiles(ev) || button.disabled) return;
|
|
71
|
+
ev.preventDefault();
|
|
72
|
+
ev.dataTransfer!.dropEffect = 'copy';
|
|
73
|
+
zone.classList.add('tb-drop-over');
|
|
74
|
+
});
|
|
75
|
+
zone.addEventListener('dragleave', ev => {
|
|
76
|
+
if (!zone.contains(ev.relatedTarget as Node | null)) zone.classList.remove('tb-drop-over');
|
|
77
|
+
});
|
|
78
|
+
zone.addEventListener('drop', ev => {
|
|
79
|
+
zone.classList.remove('tb-drop-over');
|
|
80
|
+
if (!hasFiles(ev) || button.disabled) return;
|
|
81
|
+
ev.preventDefault();
|
|
82
|
+
chooseFiles(face, [...ev.dataTransfer!.files]);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The bar of how far the files went up, while a fetch with progress sends them. */
|
|
87
|
+
export function sentBar(label: HTMLElement, id: string): HTMLElement {
|
|
88
|
+
label.id = `${id}-label`;
|
|
89
|
+
const track = el('div', 'tb-progress tb-upload-progress');
|
|
90
|
+
track.setAttribute('role', 'progressbar');
|
|
91
|
+
track.setAttribute('aria-valuemin', '0');
|
|
92
|
+
track.setAttribute('aria-valuemax', '100');
|
|
93
|
+
track.setAttribute('aria-labelledby', label.id);
|
|
94
|
+
track.hidden = true;
|
|
95
|
+
track.append(el('div', 'tb-progress-bar'));
|
|
96
|
+
return track;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function syncSent(m: Mounted): void {
|
|
100
|
+
const track = m.el.querySelector<HTMLElement>('.tb-upload-progress')!;
|
|
101
|
+
const sent = get(m.face, 'sent');
|
|
102
|
+
const part = fraction(sent) ?? 0;
|
|
103
|
+
track.hidden = sent.t === K.None;
|
|
104
|
+
track.setAttribute('aria-valuenow', String(Math.round(part * 100)));
|
|
105
|
+
(track.firstChild as HTMLElement).style.width = `${part * 100}%`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// A photo: the image itself, a button to choose one and another to take it away. The
|
|
109
|
+
// image chosen shows from the browser's memory; one kept elsewhere, from its address.
|
|
110
|
+
|
|
111
|
+
// A video or a sound: the browser's own player. Captions are one track per language;
|
|
112
|
+
// the active language's is the one shown, and it follows set-language.
|
|
113
|
+
|
|
114
|
+
/** A video's captions as [language, address] pairs: a file alone is in the active language. */
|
|
115
|
+
export function captionTracks(f: ObjectCell): [string, string][] {
|
|
116
|
+
const c = get(f, 'captions');
|
|
117
|
+
if (c.t === K.File || c.t === K.Url || c.t === K.String) return [[activeLanguage(host.rt), form(c)]];
|
|
118
|
+
if (c.t !== K.Block) return [];
|
|
119
|
+
const d = c.s.data.slice(c.i);
|
|
120
|
+
const out: [string, string][] = [];
|
|
121
|
+
for (let i = 0; i + 1 < d.length; i += 2) out.push([form(d[i]!), form(d[i + 1]!)]);
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function syncMedia(m: Mounted): void {
|
|
126
|
+
const f = m.face;
|
|
127
|
+
const media = m.el as HTMLMediaElement;
|
|
128
|
+
const src = str(f, 'value') ?? '';
|
|
129
|
+
if (media.getAttribute('src') !== src) media.src = src;
|
|
130
|
+
const t = str(f, 'text');
|
|
131
|
+
if (t !== null) media.setAttribute('aria-label', t);
|
|
132
|
+
else media.removeAttribute('aria-label');
|
|
133
|
+
media.loop = flag(f, 'looping');
|
|
134
|
+
// browsers only let what plays on its own do so without sound
|
|
135
|
+
const auto = flag(f, 'autoplay');
|
|
136
|
+
media.muted = flag(f, 'silent') || auto;
|
|
137
|
+
media.autoplay = auto;
|
|
138
|
+
if (!(media instanceof HTMLVideoElement)) return;
|
|
139
|
+
const poster = str(f, 'poster') ?? '';
|
|
140
|
+
if (media.getAttribute('poster') !== poster) media.poster = poster;
|
|
141
|
+
const lang = activeLanguage(host.rt);
|
|
142
|
+
const tracks = captionTracks(f);
|
|
143
|
+
const names = new Intl.DisplayNames([lang], { type: 'language' });
|
|
144
|
+
const now = Array.from(media.querySelectorAll('track'));
|
|
145
|
+
if (now.map(x => `${x.srclang} ${x.getAttribute('src')}`).join('|') !== tracks.map(([l, s]) => `${l} ${s}`).join('|')) {
|
|
146
|
+
media.replaceChildren(...tracks.map(([l, s]) => {
|
|
147
|
+
const track = el('track') as HTMLTrackElement;
|
|
148
|
+
track.kind = 'captions';
|
|
149
|
+
track.srclang = l;
|
|
150
|
+
track.src = s;
|
|
151
|
+
return track;
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
// the captions shown, if any, follow the active language
|
|
155
|
+
const showing = Array.from(media.textTracks).some(x => x.mode === 'showing');
|
|
156
|
+
const pick = tracks.findIndex(([l]) => l === lang);
|
|
157
|
+
media.querySelectorAll('track').forEach((track, i) => {
|
|
158
|
+
track.label = names.of(track.srclang) ?? track.srclang;
|
|
159
|
+
track.default = i === Math.max(0, pick);
|
|
160
|
+
if (showing) track.track.mode = i === Math.max(0, pick) ? 'showing' : 'disabled';
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The address that shows each chosen file, made once and freed when the photo shows another. */
|
|
165
|
+
export const shownUrls = new WeakMap<HTMLElement, { blob: Blob; url: string }>();
|
|
166
|
+
|
|
167
|
+
export function buildPhoto(face: ObjectCell, m: Mounted): HTMLElement {
|
|
168
|
+
const id = `tb-${newId()}`;
|
|
169
|
+
const wrap = el('div', 'tb-field');
|
|
170
|
+
const label = el('label', 'tb-label');
|
|
171
|
+
label.htmlFor = id;
|
|
172
|
+
const control = el('div', 'tb-control');
|
|
173
|
+
const zone = el('div', 'tb-photo');
|
|
174
|
+
// the frame opens the picker for a pointer; the keyboard has the button
|
|
175
|
+
const frame = el('div', 'tb-photo-frame');
|
|
176
|
+
frame.addEventListener('click', () => { if (!choose.disabled) picker.click(); });
|
|
177
|
+
const picker = el('input');
|
|
178
|
+
picker.type = 'file';
|
|
179
|
+
picker.hidden = true;
|
|
180
|
+
picker.tabIndex = -1;
|
|
181
|
+
const actions = el('div', 'tb-photo-actions');
|
|
182
|
+
const choose = el('button', 'tb-button');
|
|
183
|
+
choose.type = 'button';
|
|
184
|
+
choose.id = id;
|
|
185
|
+
const svg = icon('upload');
|
|
186
|
+
if (svg !== null) choose.append(svg);
|
|
187
|
+
choose.append(el('span'));
|
|
188
|
+
const remove = el('button', 'tb-button tb-photo-remove');
|
|
189
|
+
remove.type = 'button';
|
|
190
|
+
remove.addEventListener('click', () => {
|
|
191
|
+
choose.focus();
|
|
192
|
+
void fire('ui-remove-file', face, int(1));
|
|
193
|
+
});
|
|
194
|
+
const error = el('div', 'tb-error');
|
|
195
|
+
error.id = `${id}-error`;
|
|
196
|
+
error.setAttribute('aria-live', 'polite');
|
|
197
|
+
choose.setAttribute('aria-describedby', error.id);
|
|
198
|
+
choose.addEventListener('click', () => picker.click());
|
|
199
|
+
picker.addEventListener('change', () => {
|
|
200
|
+
const files = [...(picker.files ?? [])];
|
|
201
|
+
picker.value = '';
|
|
202
|
+
if (files.length > 0) chooseFiles(face, files);
|
|
203
|
+
});
|
|
204
|
+
dropFiles(face, zone, choose);
|
|
205
|
+
actions.append(choose, remove);
|
|
206
|
+
zone.append(frame, actions, picker);
|
|
207
|
+
control.append(zone, sentBar(label, id), error);
|
|
208
|
+
wrap.append(label, control);
|
|
209
|
+
m.label = label;
|
|
210
|
+
m.error = error;
|
|
211
|
+
m.button = choose;
|
|
212
|
+
m.box = frame;
|
|
213
|
+
return wrap;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** What a photo shows: the image chosen, or the address of one kept elsewhere; null for none. */
|
|
217
|
+
export function photoSrc(frame: HTMLElement, v: Cell): string | null {
|
|
218
|
+
const kept = shownUrls.get(frame);
|
|
219
|
+
const file = v.t === K.Object ? handleOf<Blob>(get(v as ObjectCell, 'file'), 'file') : null;
|
|
220
|
+
if (file !== null && kept !== undefined && kept.blob === file) return kept.url;
|
|
221
|
+
if (kept !== undefined) {
|
|
222
|
+
URL.revokeObjectURL(kept.url);
|
|
223
|
+
shownUrls.delete(frame);
|
|
224
|
+
}
|
|
225
|
+
if (file !== null) {
|
|
226
|
+
const url = URL.createObjectURL(file);
|
|
227
|
+
shownUrls.set(frame, { blob: file, url });
|
|
228
|
+
return url;
|
|
229
|
+
}
|
|
230
|
+
return v.t === K.String || v.t === K.Url || v.t === K.File ? form(v) : null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function syncPhoto(m: Mounted, err: string | null): void {
|
|
234
|
+
const f = m.face;
|
|
235
|
+
const t = str(f, 'text') ?? '';
|
|
236
|
+
m.label!.textContent = t;
|
|
237
|
+
m.label!.classList.toggle('tb-required', required(f));
|
|
238
|
+
const readonly = flag(f, 'readonly');
|
|
239
|
+
const choose = m.button!;
|
|
240
|
+
choose.disabled = flag(f, 'disabled') || readonly;
|
|
241
|
+
choose.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
242
|
+
const frame = m.box!;
|
|
243
|
+
frame.classList.toggle('tb-round', flag(f, 'round'));
|
|
244
|
+
frame.classList.toggle('tb-photo-pick', !choose.disabled);
|
|
245
|
+
const src = photoSrc(frame, get(f, 'value'));
|
|
246
|
+
let img = frame.querySelector('img');
|
|
247
|
+
if (src === null) {
|
|
248
|
+
if (img !== null || frame.firstChild === null) {
|
|
249
|
+
const svg = icon(flag(f, 'round') ? 'user' : 'image');
|
|
250
|
+
frame.replaceChildren(...(svg !== null ? [svg] : []));
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
if (img === null) {
|
|
254
|
+
img = el('img') as HTMLImageElement;
|
|
255
|
+
frame.replaceChildren(img);
|
|
256
|
+
}
|
|
257
|
+
if (img.getAttribute('src') !== src) img.src = src;
|
|
258
|
+
img.alt = t;
|
|
259
|
+
}
|
|
260
|
+
(choose.lastChild as HTMLElement).textContent = own(src === null ? 'choose-photo' : 'change-photo');
|
|
261
|
+
const remove = m.el.querySelector<HTMLButtonElement>('.tb-photo-remove')!;
|
|
262
|
+
remove.textContent = own('remove-photo');
|
|
263
|
+
remove.hidden = src === null || readonly;
|
|
264
|
+
remove.disabled = choose.disabled;
|
|
265
|
+
// read only, there is nothing to choose: only the photo is left
|
|
266
|
+
(m.el.querySelector('.tb-photo-actions') as HTMLElement).hidden = readonly;
|
|
267
|
+
(m.el.querySelector('input[type="file"]') as HTMLInputElement).accept = pickerAccept(acceptList(f));
|
|
268
|
+
m.error!.textContent = err ?? '';
|
|
269
|
+
syncSent(m);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
export function accepts(file: File, list: string[]): boolean {
|
|
275
|
+
if (list.length === 0) return true;
|
|
276
|
+
const name = file.name.toLowerCase();
|
|
277
|
+
const type = file.type.toLowerCase();
|
|
278
|
+
return list.some(x => x.includes('/')
|
|
279
|
+
? (x.endsWith('/*') ? type.startsWith(x.slice(0, -1)) : type === x)
|
|
280
|
+
: name.endsWith(x.startsWith('.') ? x : `.${x}`));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function chooseFiles(face: ObjectCell, files: File[]): void {
|
|
284
|
+
const list = acceptList(face);
|
|
285
|
+
const ok = files.filter(f => accepts(f, list));
|
|
286
|
+
const taken = flag(face, 'multiple') ? ok : ok.slice(0, 1);
|
|
287
|
+
const cells: Cell[] = [];
|
|
288
|
+
for (const f of taken) cells.push(string(f.name), int(f.size), string(f.type), fileHandle(f));
|
|
289
|
+
const refused = files.filter(f => !accepts(f, list)).map(f => string(f.name));
|
|
290
|
+
void fire('ui-files', face, blockOf(K.Block, cells), blockOf(K.Block, refused));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** A link as the page would follow it, relative ones too; null unless it is http or https. */
|
|
294
|
+
export function webHref(url: string): string | null {
|
|
295
|
+
try {
|
|
296
|
+
const u = new URL(url, location.href);
|
|
297
|
+
return u.protocol === 'http:' || u.protocol === 'https:' ? u.href : null;
|
|
298
|
+
} catch {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function syncUpload(m: Mounted, err: string | null): void {
|
|
304
|
+
const f = m.face;
|
|
305
|
+
const label = m.label!;
|
|
306
|
+
label.textContent = str(f, 'text') ?? '';
|
|
307
|
+
label.classList.toggle('tb-required', required(f));
|
|
308
|
+
const many = flag(f, 'multiple');
|
|
309
|
+
const choose = m.button!;
|
|
310
|
+
(choose.lastChild as HTMLElement).textContent = own(many ? 'choose-files' : 'choose-file');
|
|
311
|
+
const readonly = flag(f, 'readonly');
|
|
312
|
+
choose.disabled = flag(f, 'disabled') || readonly;
|
|
313
|
+
choose.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
314
|
+
const zone = choose.parentElement!;
|
|
315
|
+
// read only, there is nothing to choose: only the files are left
|
|
316
|
+
zone.hidden = readonly;
|
|
317
|
+
(zone.querySelector('.tb-upload-hint') as HTMLElement).textContent = own(many ? 'drop-files' : 'drop-file');
|
|
318
|
+
const picker = zone.querySelector('input') as HTMLInputElement;
|
|
319
|
+
picker.multiple = many;
|
|
320
|
+
picker.accept = pickerAccept(acceptList(f));
|
|
321
|
+
m.error!.textContent = err ?? '';
|
|
322
|
+
const v = get(f, 'value');
|
|
323
|
+
const files = v.t === K.Block ? v.s.data.slice(v.i).filter(x => x.t === K.Object) as ObjectCell[] : [];
|
|
324
|
+
const lang = activeLanguage(host.rt);
|
|
325
|
+
const items = files.map((file, i) => {
|
|
326
|
+
const li = el('li');
|
|
327
|
+
// a file kept elsewhere has a url: its name opens it, and an image shows itself small
|
|
328
|
+
const url = str(file, 'url');
|
|
329
|
+
const href = url !== null ? webHref(url) : null;
|
|
330
|
+
const name = el(href !== null ? 'a' : 'span', 'tb-upload-name');
|
|
331
|
+
name.textContent = str(file, 'name') ?? '';
|
|
332
|
+
if (name instanceof HTMLAnchorElement && href !== null) {
|
|
333
|
+
name.href = href;
|
|
334
|
+
name.target = '_blank';
|
|
335
|
+
name.rel = 'noopener';
|
|
336
|
+
}
|
|
337
|
+
if (href !== null && (str(file, 'type') ?? '').startsWith('image/')) {
|
|
338
|
+
const thumb = el('img', 'tb-upload-thumb');
|
|
339
|
+
thumb.src = href;
|
|
340
|
+
thumb.alt = '';
|
|
341
|
+
li.append(thumb);
|
|
342
|
+
}
|
|
343
|
+
const size = el('span', 'tb-upload-size');
|
|
344
|
+
const n = get(file, 'size');
|
|
345
|
+
size.textContent = n.t === K.Integer ? fileSize(Number(n.v), lang) : '';
|
|
346
|
+
if (readonly) {
|
|
347
|
+
li.append(name, size);
|
|
348
|
+
return li;
|
|
349
|
+
}
|
|
350
|
+
const remove = el('button', 'tb-upload-remove');
|
|
351
|
+
remove.type = 'button';
|
|
352
|
+
remove.disabled = choose.disabled;
|
|
353
|
+
remove.setAttribute('aria-label', own('remove-file', name.textContent ?? ''));
|
|
354
|
+
const x = icon('close');
|
|
355
|
+
if (x !== null) remove.append(x);
|
|
356
|
+
remove.addEventListener('click', () => {
|
|
357
|
+
choose.focus();
|
|
358
|
+
void fire('ui-remove-file', f, int(i + 1));
|
|
359
|
+
});
|
|
360
|
+
li.append(name, size, remove);
|
|
361
|
+
return li;
|
|
362
|
+
});
|
|
363
|
+
m.box!.replaceChildren(...items);
|
|
364
|
+
m.box!.hidden = items.length === 0;
|
|
365
|
+
syncSent(m);
|
|
366
|
+
}
|
|
367
|
+
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// --- widget: an element the page's JavaScript defines ----------------------------
|
|
2
|
+
//
|
|
3
|
+
// The element is made by its tag, once the page has defined it (import %geo-map.js).
|
|
4
|
+
// The face's value is the element's value property, both ways, as JSON; its props set
|
|
5
|
+
// properties. Its change event gives the value back, and any other on-xxx of the face
|
|
6
|
+
// listens to the element's event xxx, with the event's detail. The theme's custom
|
|
7
|
+
// properties (--tb-*) reach it, as they reach every element of the page.
|
|
8
|
+
|
|
9
|
+
import { K, NONE, spell, toJson, parseJson, type Cell, type ObjectCell, type WordCell } from '../../core/index.ts';
|
|
10
|
+
import { get, str, flag } from './faces.ts';
|
|
11
|
+
import { type Mounted, newId, fire, el } from './mount.ts';
|
|
12
|
+
import { fireEvent } from './events.ts';
|
|
13
|
+
import { required } from './fields.ts';
|
|
14
|
+
|
|
15
|
+
/** What was last given to or taken from each widget, as JSON, so a repaint sets only what changed. */
|
|
16
|
+
export const widgetSet = new WeakMap<HTMLElement, { value: string; props: string }>();
|
|
17
|
+
|
|
18
|
+
/** Events every face has, which the host listens to itself. */
|
|
19
|
+
export const FACE_EVENTS = new Set(['on-change', 'on-focus', 'on-unfocus', 'on-over', 'on-dbl-click', 'on-key', 'on-end',
|
|
20
|
+
'on-drop', 'on-reorder', 'on-menu', 'on-select']);
|
|
21
|
+
|
|
22
|
+
/** A JavaScript value as Trebol's, as JSON carries it; none for what JSON cannot write. */
|
|
23
|
+
export function fromJs(x: unknown): Cell {
|
|
24
|
+
let json: string | undefined;
|
|
25
|
+
try {
|
|
26
|
+
json = JSON.stringify(x);
|
|
27
|
+
} catch {
|
|
28
|
+
json = undefined;
|
|
29
|
+
}
|
|
30
|
+
if (json === undefined) return NONE;
|
|
31
|
+
const v = parseJson(json);
|
|
32
|
+
return typeof v === 'string' ? NONE : v;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function buildWidget(face: ObjectCell, m: Mounted): HTMLElement {
|
|
36
|
+
const id = `tb-${newId()}`;
|
|
37
|
+
const wrap = el('div', 'tb-field');
|
|
38
|
+
const label = el('label', 'tb-label');
|
|
39
|
+
label.htmlFor = id;
|
|
40
|
+
const control = el('div', 'tb-control');
|
|
41
|
+
let node: HTMLElement;
|
|
42
|
+
try {
|
|
43
|
+
node = document.createElement(str(face, 'tag') ?? 'div');
|
|
44
|
+
} catch {
|
|
45
|
+
// a tag that is no element's name
|
|
46
|
+
node = el('div');
|
|
47
|
+
}
|
|
48
|
+
node.classList.add('tb-widget');
|
|
49
|
+
node.id = id;
|
|
50
|
+
const error = el('div', 'tb-error');
|
|
51
|
+
error.id = `${id}-error`;
|
|
52
|
+
error.setAttribute('aria-live', 'polite');
|
|
53
|
+
node.setAttribute('aria-describedby', error.id);
|
|
54
|
+
widgetSet.set(node, { value: '', props: '' });
|
|
55
|
+
node.addEventListener('change', ev => {
|
|
56
|
+
if (ev.target !== node) return;
|
|
57
|
+
const v = fromJs((node as unknown as { value?: unknown }).value);
|
|
58
|
+
widgetSet.get(node)!.value = toJson(v);
|
|
59
|
+
void fire('ui-input', face, v);
|
|
60
|
+
});
|
|
61
|
+
const events = get(face, 'events');
|
|
62
|
+
if (events.t === K.Block) {
|
|
63
|
+
for (const w of events.s.data) {
|
|
64
|
+
const name = w.t === K.Word ? spell((w as WordCell).sym).toLowerCase() : '';
|
|
65
|
+
if (!name.startsWith('on-') || FACE_EVENTS.has(name)) continue;
|
|
66
|
+
node.addEventListener(name.slice(3), ev => fireEvent(face, name, [['detail', fromJs((ev as CustomEvent).detail)]]));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
control.append(node, error);
|
|
70
|
+
wrap.append(label, control);
|
|
71
|
+
m.label = label;
|
|
72
|
+
m.error = error;
|
|
73
|
+
m.box = node;
|
|
74
|
+
return wrap;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function syncWidget(m: Mounted, err: string | null): void {
|
|
78
|
+
const f = m.face;
|
|
79
|
+
const t = str(f, 'text') ?? '';
|
|
80
|
+
m.label!.textContent = t;
|
|
81
|
+
m.label!.hidden = t === '';
|
|
82
|
+
m.label!.classList.toggle('tb-required', required(f));
|
|
83
|
+
const node = m.box! as HTMLElement & { value?: unknown; disabled?: boolean; readOnly?: boolean };
|
|
84
|
+
const set = widgetSet.get(node)!;
|
|
85
|
+
const props = get(f, 'props');
|
|
86
|
+
const propsJson = props.t === K.Object || props.t === K.Map ? toJson(props) : '{}';
|
|
87
|
+
if (propsJson !== set.props) {
|
|
88
|
+
Object.assign(node, JSON.parse(propsJson) as object);
|
|
89
|
+
set.props = propsJson;
|
|
90
|
+
}
|
|
91
|
+
const valueJson = toJson(get(f, 'value'));
|
|
92
|
+
if (valueJson !== set.value) {
|
|
93
|
+
node.value = JSON.parse(valueJson) as unknown;
|
|
94
|
+
set.value = valueJson;
|
|
95
|
+
}
|
|
96
|
+
const disabled = flag(f, 'disabled');
|
|
97
|
+
node.toggleAttribute('disabled', disabled);
|
|
98
|
+
if ('disabled' in node) node.disabled = disabled;
|
|
99
|
+
if ('readOnly' in node) node.readOnly = flag(f, 'readonly');
|
|
100
|
+
node.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
|
|
101
|
+
if (t !== '') node.setAttribute('aria-label', t);
|
|
102
|
+
m.error!.textContent = err ?? '';
|
|
103
|
+
}
|