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,268 @@
|
|
|
1
|
+
// --- drag and drop -----------------------------------------------------------------
|
|
2
|
+
//
|
|
3
|
+
// Pointer events rather than HTML drag and drop, so it also works with touch
|
|
4
|
+
// (after a long press) and shows the theme's own indicator.
|
|
5
|
+
import { K, NONE } from "../../core/index.js";
|
|
6
|
+
import { inDevtools } from "../devtools.js";
|
|
7
|
+
import { get, str, flag, word, children } from "./faces.js";
|
|
8
|
+
import { byNode, rowOf, byCtx, fire, el } from "./mount.js";
|
|
9
|
+
import { seenKeys } from "./events.js";
|
|
10
|
+
import { swallowNextClick } from "./swipe.js";
|
|
11
|
+
import { hostSorted } from "./tables.js";
|
|
12
|
+
import { groupParent, treeTarget } from "./trees.js";
|
|
13
|
+
import { syncDirty } from "./sync.js";
|
|
14
|
+
/** Mounted faces that accept drops. */
|
|
15
|
+
export const dropZones = new Set();
|
|
16
|
+
export const isData = (m) => m.rows !== null;
|
|
17
|
+
/** The elements a drop target places things between: rows, or child faces. */
|
|
18
|
+
export function slots(m) {
|
|
19
|
+
if (isData(m))
|
|
20
|
+
return Array.from(m.box.children);
|
|
21
|
+
return children(m.face).map(c => byCtx.get(c.ctx)?.el).filter((e) => e !== undefined && e.isConnected);
|
|
22
|
+
}
|
|
23
|
+
export function mountedAbove(e) {
|
|
24
|
+
for (let x = e; x !== null; x = x.parentElement) {
|
|
25
|
+
const m = byNode.get(x);
|
|
26
|
+
if (m !== undefined && m.el === x)
|
|
27
|
+
return m;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
/** The drag source at an element: a draggable face, a row, or a child of a reorderable container. */
|
|
32
|
+
export function sourceAt(start) {
|
|
33
|
+
if (start.closest('input, textarea, select, [contenteditable], .tb-split-handle, .tb-menu') !== null)
|
|
34
|
+
return null;
|
|
35
|
+
for (let e = start; e !== null; e = e.parentElement) {
|
|
36
|
+
const row = rowOf.get(e);
|
|
37
|
+
if (row !== undefined) {
|
|
38
|
+
const m = mountedAbove(e);
|
|
39
|
+
// rows the host sorted are not in the order of data, so they cannot be moved
|
|
40
|
+
const reorder = flag(m.face, 'reorder') && hostSorted(m.face) === null;
|
|
41
|
+
if (!flag(m.face, 'drag') && !reorder)
|
|
42
|
+
return null;
|
|
43
|
+
const group = e.parentElement;
|
|
44
|
+
if (m.open === null) {
|
|
45
|
+
// a table or list may not draw its first rows
|
|
46
|
+
const index = m.first + Array.prototype.indexOf.call(group.children, e);
|
|
47
|
+
return { face: m.face, el: e, payload: row, within: reorder ? m : null, index, group: null };
|
|
48
|
+
}
|
|
49
|
+
const index = Array.prototype.indexOf.call(group.children, e);
|
|
50
|
+
return { face: m.face, el: e, payload: row, within: reorder ? m : null, index, group };
|
|
51
|
+
}
|
|
52
|
+
const m = byNode.get(e);
|
|
53
|
+
if (m === undefined || m.el !== e)
|
|
54
|
+
continue;
|
|
55
|
+
const parent = mountedAbove(e.parentElement);
|
|
56
|
+
const reorder = parent !== undefined && !isData(parent) && flag(parent.face, 'reorder');
|
|
57
|
+
if (flag(m.face, 'drag') && !isData(m) || reorder) {
|
|
58
|
+
const index = reorder ? slots(parent).indexOf(e) : -1;
|
|
59
|
+
return { face: m.face, el: e, payload: get(m.face, 'value'), within: reorder ? parent : null, index, group: null };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/** The drop target under a point, and the insertion index there. */
|
|
65
|
+
export function targetAt(src, x, y) {
|
|
66
|
+
for (let m = mountedAbove(document.elementFromPoint(x, y)); m !== undefined; m = mountedAbove(m.el.parentElement)) {
|
|
67
|
+
if (m.open !== null) {
|
|
68
|
+
const t = treeTarget(m, src, x, y);
|
|
69
|
+
if (t !== null)
|
|
70
|
+
return t;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const reorder = m === src.within;
|
|
74
|
+
if ((reorder || flag(m.face, 'drop')) && m.el !== src.el && !src.el.contains(m.el)) {
|
|
75
|
+
return { m, index: insertionAt(m, x, y), reorder };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
export const horizontal = (m) => word(m.face, 'style') === 'row';
|
|
81
|
+
export function insertionAt(m, x, y) {
|
|
82
|
+
const across = horizontal(m);
|
|
83
|
+
let i = 0;
|
|
84
|
+
for (const e of slots(m)) {
|
|
85
|
+
const r = e.getBoundingClientRect();
|
|
86
|
+
if ((across ? x : y) > (across ? r.left + r.width / 2 : r.top + r.height / 2))
|
|
87
|
+
i++;
|
|
88
|
+
}
|
|
89
|
+
return m.first + i;
|
|
90
|
+
}
|
|
91
|
+
/** How many things a container places: all the rows of a table or list, drawn or not. */
|
|
92
|
+
export function slotCount(m) {
|
|
93
|
+
return isData(m) && m.open === null ? m.items.length : slots(m).length;
|
|
94
|
+
}
|
|
95
|
+
export let line = null;
|
|
96
|
+
/** Shows where a drop would land: a line between slots, and the target outlined. */
|
|
97
|
+
export function indicate(t) {
|
|
98
|
+
document.querySelectorAll('.tb-drop-over').forEach(e => e.classList.remove('tb-drop-over'));
|
|
99
|
+
if (t === null) {
|
|
100
|
+
line?.remove();
|
|
101
|
+
line = null;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (t.into !== undefined) {
|
|
105
|
+
// into a folder: outline its row instead of drawing a line
|
|
106
|
+
t.into.querySelector(':scope > .tb-tree-row').classList.add('tb-drop-over');
|
|
107
|
+
line?.remove();
|
|
108
|
+
line = null;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
t.m.el.classList.add('tb-drop-over');
|
|
112
|
+
if (line === null) {
|
|
113
|
+
line = el('div', 'tb-drop-line');
|
|
114
|
+
line.setAttribute('aria-hidden', 'true');
|
|
115
|
+
document.body.append(line);
|
|
116
|
+
}
|
|
117
|
+
const across = horizontal(t.m);
|
|
118
|
+
const all = t.group !== undefined ? Array.from(t.group.children) : slots(t.m);
|
|
119
|
+
const box = (t.group ?? t.m.box ?? t.m.el).getBoundingClientRect();
|
|
120
|
+
const at = t.group !== undefined ? t.index : t.index - t.m.first;
|
|
121
|
+
const before = all[at]?.getBoundingClientRect();
|
|
122
|
+
const after = all[at - 1]?.getBoundingClientRect();
|
|
123
|
+
line.classList.toggle('tb-across', across);
|
|
124
|
+
if (across) {
|
|
125
|
+
const at = before && after ? (after.right + before.left) / 2 : before ? before.left - 3 : after ? after.right + 3 : box.left + 8;
|
|
126
|
+
const r = before ?? after ?? box;
|
|
127
|
+
Object.assign(line.style, { left: `${at - 1}px`, top: `${r.top}px`, width: '', height: `${r.height}px` });
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const at = before && after ? (after.bottom + before.top) / 2 : before ? before.top - 3 : after ? after.bottom + 3 : box.top + 8;
|
|
131
|
+
const r = before ?? after ?? box;
|
|
132
|
+
Object.assign(line.style, { left: `${r.left}px`, top: `${at - 1}px`, width: `${r.width}px`, height: '' });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Ends a drag on a target: a move inside its container, or a drop. */
|
|
136
|
+
export function land(src, t) {
|
|
137
|
+
if (t.reorder) {
|
|
138
|
+
const to = t.index > src.index ? t.index - 1 : t.index;
|
|
139
|
+
if (to !== src.index)
|
|
140
|
+
void reorder(t.m, src.index, to, t.group, t.parent);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
void fire('ui-drop', t.m.face, src.payload, src.face, int(t.index + 1), t.parent ?? NONE);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export const int = (n) => ({ t: K.Integer, v: n });
|
|
147
|
+
/** Moves a child or row and keeps the focus on it once the page is repainted. */
|
|
148
|
+
export async function reorder(m, from, to, group, parent = NONE) {
|
|
149
|
+
const had = document.activeElement !== null && document.activeElement !== document.body && m.el.contains(document.activeElement);
|
|
150
|
+
await fire('ui-reorder', m.face, int(from + 1), int(to + 1), parent);
|
|
151
|
+
syncDirty();
|
|
152
|
+
if (!had)
|
|
153
|
+
return;
|
|
154
|
+
const e = group !== undefined ? group.children[to] : slots(m)[to - m.first];
|
|
155
|
+
const focusable = e?.matches('[tabindex], button, a, input, textarea, select') ? e : e?.querySelector('[tabindex], button, a, input, textarea, select');
|
|
156
|
+
(focusable ?? null)?.focus();
|
|
157
|
+
}
|
|
158
|
+
/** "Move to" entries: the visible drop zones other than the face itself. */
|
|
159
|
+
export function moveTargets(face, payload) {
|
|
160
|
+
const out = [];
|
|
161
|
+
for (const z of dropZones) {
|
|
162
|
+
if (!z.el.isConnected) {
|
|
163
|
+
dropZones.delete(z);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (z.face.ctx === face.ctx || z.el.getClientRects().length === 0)
|
|
167
|
+
continue;
|
|
168
|
+
// nor the place it is already in: the lane of a card
|
|
169
|
+
if (z.el.contains(byCtx.get(face.ctx)?.el ?? null))
|
|
170
|
+
continue;
|
|
171
|
+
// a nameless zone around a named one is the same place
|
|
172
|
+
if (str(z.face, 'text') === null && z.el.querySelector('.tb-drop-zone') !== null)
|
|
173
|
+
continue;
|
|
174
|
+
out.push({
|
|
175
|
+
text: str(z.face, 'text') ?? word(z.face, 'style') ?? '',
|
|
176
|
+
run: () => void fire('ui-drop', z.face, payload, face, int(slotCount(z) + 1), NONE),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
export let dragging = null;
|
|
182
|
+
export function startDrag(d) {
|
|
183
|
+
d.on = true;
|
|
184
|
+
d.ghost = el('div', 'tb-drag-ghost');
|
|
185
|
+
d.ghost.textContent = (d.src.el.innerText || d.src.el.textContent || '').split('\n').find(t => t.trim() !== '')?.trim() ?? '';
|
|
186
|
+
d.ghost.setAttribute('aria-hidden', 'true');
|
|
187
|
+
document.body.append(d.ghost);
|
|
188
|
+
d.src.el.classList.add('tb-dragging');
|
|
189
|
+
getSelection()?.removeAllRanges();
|
|
190
|
+
document.documentElement.classList.add('tb-drag-active');
|
|
191
|
+
moveDrag(d, d.x, d.y);
|
|
192
|
+
}
|
|
193
|
+
export function moveDrag(d, x, y) {
|
|
194
|
+
d.ghost.style.transform = `translate(${x + 12}px, ${y + 12}px)`;
|
|
195
|
+
d.target = targetAt(d.src, x, y);
|
|
196
|
+
indicate(d.target);
|
|
197
|
+
}
|
|
198
|
+
export function endDrag(drop) {
|
|
199
|
+
const d = dragging;
|
|
200
|
+
if (d === null)
|
|
201
|
+
return;
|
|
202
|
+
dragging = null;
|
|
203
|
+
clearTimeout(d.timer);
|
|
204
|
+
if (!d.on)
|
|
205
|
+
return;
|
|
206
|
+
d.ghost?.remove();
|
|
207
|
+
d.src.el.classList.remove('tb-dragging');
|
|
208
|
+
document.documentElement.classList.remove('tb-drag-active');
|
|
209
|
+
indicate(null);
|
|
210
|
+
if (drop && d.target !== null)
|
|
211
|
+
land(d.src, d.target);
|
|
212
|
+
// the click that ends a drag is not a click
|
|
213
|
+
swallowNextClick();
|
|
214
|
+
}
|
|
215
|
+
export function swallowClick(ev) {
|
|
216
|
+
ev.stopPropagation();
|
|
217
|
+
ev.preventDefault();
|
|
218
|
+
}
|
|
219
|
+
export function onPointerDown(ev) {
|
|
220
|
+
if (ev.button !== 0 || dragging !== null)
|
|
221
|
+
return;
|
|
222
|
+
const src = sourceAt(ev.target);
|
|
223
|
+
if (src === null)
|
|
224
|
+
return;
|
|
225
|
+
const d = { src, x: ev.clientX, y: ev.clientY, id: ev.pointerId, on: false, ghost: null, target: null, timer: 0 };
|
|
226
|
+
dragging = d;
|
|
227
|
+
// on touch a long press starts the drag, so a swipe still scrolls
|
|
228
|
+
if (ev.pointerType === 'touch')
|
|
229
|
+
d.timer = setTimeout(() => { if (dragging === d)
|
|
230
|
+
startDrag(d); }, 400);
|
|
231
|
+
}
|
|
232
|
+
export function onPointerMove(ev) {
|
|
233
|
+
const d = dragging;
|
|
234
|
+
if (d === null || ev.pointerId !== d.id)
|
|
235
|
+
return;
|
|
236
|
+
if (!d.on) {
|
|
237
|
+
const far = Math.hypot(ev.clientX - d.x, ev.clientY - d.y) > 5;
|
|
238
|
+
if (!far)
|
|
239
|
+
return;
|
|
240
|
+
if (ev.pointerType === 'touch')
|
|
241
|
+
return endDrag(false);
|
|
242
|
+
d.x = ev.clientX;
|
|
243
|
+
d.y = ev.clientY;
|
|
244
|
+
startDrag(d);
|
|
245
|
+
}
|
|
246
|
+
ev.preventDefault();
|
|
247
|
+
moveDrag(d, ev.clientX, ev.clientY);
|
|
248
|
+
}
|
|
249
|
+
/** Alt+Up and Alt+Down move the focused row or child inside a reorderable container. */
|
|
250
|
+
export function onReorderKey(ev) {
|
|
251
|
+
if (inDevtools(ev.target))
|
|
252
|
+
return;
|
|
253
|
+
if (!ev.altKey || ev.ctrlKey || ev.metaKey || (ev.key !== 'ArrowUp' && ev.key !== 'ArrowDown'))
|
|
254
|
+
return;
|
|
255
|
+
const at = document.activeElement;
|
|
256
|
+
if (at === null)
|
|
257
|
+
return;
|
|
258
|
+
const src = sourceAt(at);
|
|
259
|
+
if (src === null || src.within === null)
|
|
260
|
+
return;
|
|
261
|
+
const to = src.index + (ev.key === 'ArrowUp' ? -1 : 1);
|
|
262
|
+
if (to < 0 || to >= (src.group !== null ? src.group.children.length : slotCount(src.within)))
|
|
263
|
+
return;
|
|
264
|
+
ev.preventDefault();
|
|
265
|
+
seenKeys.add(ev);
|
|
266
|
+
const group = src.group ?? undefined;
|
|
267
|
+
void reorder(src.within, src.index, to, group, group !== undefined && src.within.open !== null ? groupParent(src.within, group) : NONE);
|
|
268
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Cell } from '../../core/index.ts';
|
|
2
|
+
import { type El } from '../draw.ts';
|
|
3
|
+
import { type Mounted } from './mount.ts';
|
|
4
|
+
export declare const SVG_NS = "http://www.w3.org/2000/svg";
|
|
5
|
+
export declare function svgNode(e: El): SVGElement;
|
|
6
|
+
/** A canvas: one SVG whose viewBox is the canvas's extent, as wide as it fits up to that. */
|
|
7
|
+
export declare function buildCanvas(m: Mounted): HTMLElement;
|
|
8
|
+
export declare function syncCanvas(m: Mounted): void;
|
|
9
|
+
export interface ChartParts {
|
|
10
|
+
caption: HTMLElement;
|
|
11
|
+
plot: HTMLElement;
|
|
12
|
+
svg: SVGSVGElement;
|
|
13
|
+
legend: HTMLElement;
|
|
14
|
+
table: HTMLTableElement;
|
|
15
|
+
empty: HTMLElement;
|
|
16
|
+
width: number;
|
|
17
|
+
}
|
|
18
|
+
export declare const CHART_HEIGHT: Record<string, number>;
|
|
19
|
+
/**
|
|
20
|
+
* A chart: its text as a caption, the plot, a legend, and the same numbers as a
|
|
21
|
+
* table that only a screen reader reads. It is drawn at the width it gets, and
|
|
22
|
+
* again when that changes, so its text stays the size of the page's.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildChart(m: Mounted): HTMLElement;
|
|
25
|
+
/** A cell of a chart as a number, or null. */
|
|
26
|
+
export declare function chartNumber(v: Cell): number | null;
|
|
27
|
+
/** How a number of the chart shows: as the values it stands for, money as money and percent as percent. */
|
|
28
|
+
export declare function chartFormat(sample: Cell | null): (n: number) => string;
|
|
29
|
+
export declare function syncChart(m: Mounted): void;
|
|
30
|
+
export declare function drawChart(m: Mounted): void;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// --- drawings and charts ---------------------------------------------------------------
|
|
2
|
+
import { K, money, decToNumber, decFromNumber, intern } from "../../core/index.js";
|
|
3
|
+
import { drawSvg } from "../draw.js";
|
|
4
|
+
import { rowField } from "../rows.js";
|
|
5
|
+
import { chartSvg, SERIES_COLORS, pieColor } from "../chart.js";
|
|
6
|
+
import { get, str, own, display, flag, word } from "./faces.js";
|
|
7
|
+
import { el } from "./mount.js";
|
|
8
|
+
import { columns, cellText } from "./tables.js";
|
|
9
|
+
export const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
10
|
+
export function svgNode(e) {
|
|
11
|
+
const n = document.createElementNS(SVG_NS, e.tag);
|
|
12
|
+
for (const [k, v] of Object.entries(e.attrs))
|
|
13
|
+
n.setAttribute(k, v);
|
|
14
|
+
if (e.text !== undefined)
|
|
15
|
+
n.textContent = e.text;
|
|
16
|
+
for (const k of e.kids)
|
|
17
|
+
n.append(svgNode(k));
|
|
18
|
+
return n;
|
|
19
|
+
}
|
|
20
|
+
/** A canvas: one SVG whose viewBox is the canvas's extent, as wide as it fits up to that. */
|
|
21
|
+
export function buildCanvas(m) {
|
|
22
|
+
const node = el('div', 'tb-canvas');
|
|
23
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
24
|
+
node.append(svg);
|
|
25
|
+
m.kids = svg;
|
|
26
|
+
return node;
|
|
27
|
+
}
|
|
28
|
+
export function syncCanvas(m) {
|
|
29
|
+
const svg = m.kids;
|
|
30
|
+
const ext = get(m.face, 'extent');
|
|
31
|
+
const [w, h] = ext.t === K.Pair ? [ext.x, ext.y] : [300, 150];
|
|
32
|
+
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
33
|
+
svg.setAttribute('width', String(w));
|
|
34
|
+
svg.setAttribute('height', String(h));
|
|
35
|
+
// with a text, the drawing is a picture with that name; without, it is decoration
|
|
36
|
+
const t = str(m.face, 'text');
|
|
37
|
+
if (t !== null) {
|
|
38
|
+
svg.setAttribute('role', 'img');
|
|
39
|
+
svg.setAttribute('aria-label', t);
|
|
40
|
+
svg.removeAttribute('aria-hidden');
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
svg.removeAttribute('role');
|
|
44
|
+
svg.removeAttribute('aria-label');
|
|
45
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
46
|
+
}
|
|
47
|
+
const { kids, error } = drawSvg(get(m.face, 'draw'));
|
|
48
|
+
if (error !== null)
|
|
49
|
+
console.warn(`Trebol: ${error}`);
|
|
50
|
+
svg.replaceChildren(...kids.map(svgNode));
|
|
51
|
+
}
|
|
52
|
+
export const CHART_HEIGHT = { small: 160, medium: 240, large: 360 };
|
|
53
|
+
/**
|
|
54
|
+
* A chart: its text as a caption, the plot, a legend, and the same numbers as a
|
|
55
|
+
* table that only a screen reader reads. It is drawn at the width it gets, and
|
|
56
|
+
* again when that changes, so its text stays the size of the page's.
|
|
57
|
+
*/
|
|
58
|
+
export function buildChart(m) {
|
|
59
|
+
const node = el('figure', 'tb-chart');
|
|
60
|
+
const caption = el('figcaption', 'tb-chart-caption');
|
|
61
|
+
const plot = el('div', 'tb-chart-plot');
|
|
62
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
63
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
64
|
+
plot.append(svg);
|
|
65
|
+
const legend = el('ul', 'tb-chart-legend');
|
|
66
|
+
const table = el('table', 'tb-sr-only');
|
|
67
|
+
const empty = el('p', 'tb-chart-empty');
|
|
68
|
+
node.append(caption, plot, legend, empty, table);
|
|
69
|
+
const parts = { caption, plot, svg, legend, table, empty, width: 0 };
|
|
70
|
+
m.kids = parts;
|
|
71
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
72
|
+
new ResizeObserver(() => {
|
|
73
|
+
if (plot.clientWidth !== parts.width)
|
|
74
|
+
drawChart(m);
|
|
75
|
+
}).observe(plot);
|
|
76
|
+
}
|
|
77
|
+
return node;
|
|
78
|
+
}
|
|
79
|
+
/** A cell of a chart as a number, or null. */
|
|
80
|
+
export function chartNumber(v) {
|
|
81
|
+
if (v.t === K.Integer)
|
|
82
|
+
return Number(v.v);
|
|
83
|
+
if (v.t === K.Decimal || v.t === K.Percent)
|
|
84
|
+
return v.v;
|
|
85
|
+
if (v.t === K.Money)
|
|
86
|
+
return decToNumber(v.v);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/** How a number of the chart shows: as the values it stands for, money as money and percent as percent. */
|
|
90
|
+
export function chartFormat(sample) {
|
|
91
|
+
return n => {
|
|
92
|
+
if (sample !== null && sample.t === K.Money) {
|
|
93
|
+
const d = decFromNumber(Math.round(n * 100) / 100);
|
|
94
|
+
if (d !== null)
|
|
95
|
+
return display(money(d, sample.cur));
|
|
96
|
+
}
|
|
97
|
+
if (sample !== null && sample.t === K.Percent)
|
|
98
|
+
return display({ t: K.Percent, v: n });
|
|
99
|
+
return display(Number.isInteger(n) ? { t: K.Integer, v: n } : { t: K.Decimal, v: n });
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
export function syncChart(m) {
|
|
103
|
+
const p = m.kids;
|
|
104
|
+
const t = str(m.face, 'text');
|
|
105
|
+
p.caption.textContent = t ?? '';
|
|
106
|
+
p.caption.hidden = t === null;
|
|
107
|
+
drawChart(m);
|
|
108
|
+
}
|
|
109
|
+
export function drawChart(m) {
|
|
110
|
+
const f = m.face;
|
|
111
|
+
const p = m.kids;
|
|
112
|
+
const cols = columns(f);
|
|
113
|
+
const data = get(f, 'data');
|
|
114
|
+
const rows = data.t === K.Block ? data.s.data.slice(data.i) : [];
|
|
115
|
+
const kind = (word(f, 'kind') ?? 'bar');
|
|
116
|
+
const labels = rows.map(r => cellText(rowField(r, cols[0]?.sym ?? intern('label'))));
|
|
117
|
+
let sample = null;
|
|
118
|
+
const series = cols.slice(1).map((c, k) => ({
|
|
119
|
+
name: c.label,
|
|
120
|
+
color: SERIES_COLORS[k % SERIES_COLORS.length],
|
|
121
|
+
values: rows.map(r => {
|
|
122
|
+
const v = rowField(r, c.sym);
|
|
123
|
+
const n = chartNumber(v);
|
|
124
|
+
if (n !== null && sample === null)
|
|
125
|
+
sample = v;
|
|
126
|
+
return n;
|
|
127
|
+
}),
|
|
128
|
+
}));
|
|
129
|
+
const format = chartFormat(sample);
|
|
130
|
+
const nothing = rows.length === 0 || series.length === 0;
|
|
131
|
+
p.empty.hidden = !nothing;
|
|
132
|
+
p.plot.hidden = nothing;
|
|
133
|
+
if (nothing) {
|
|
134
|
+
p.empty.textContent = own('empty');
|
|
135
|
+
p.legend.replaceChildren();
|
|
136
|
+
p.table.replaceChildren();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const width = p.plot.clientWidth || 600;
|
|
140
|
+
const height = CHART_HEIGHT[word(f, 'size') ?? 'medium'] ?? 240;
|
|
141
|
+
p.width = p.plot.clientWidth;
|
|
142
|
+
// no width of its own: it takes the plot's, so it never widens what holds it
|
|
143
|
+
p.svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
|
144
|
+
p.svg.replaceChildren(...chartSvg({ kind, labels, series, stacked: flag(f, 'stacked'), width, height, format }).map(svgNode));
|
|
145
|
+
// the legend names the series, or a pie's slices
|
|
146
|
+
const keys = kind === 'pie' ? labels.map((l, i) => [l, pieColor(i)]) : series.length > 1 ? series.map(s => [s.name, s.color]) : [];
|
|
147
|
+
p.legend.replaceChildren(...keys.map(([name, color]) => {
|
|
148
|
+
const li = el('li', 'tb-chart-key');
|
|
149
|
+
const swatch = el('span', 'tb-chart-swatch');
|
|
150
|
+
swatch.style.setProperty('--tb-swatch', color);
|
|
151
|
+
li.append(swatch, name);
|
|
152
|
+
return li;
|
|
153
|
+
}));
|
|
154
|
+
p.legend.hidden = keys.length === 0;
|
|
155
|
+
// the same numbers for a screen reader
|
|
156
|
+
const cap = el('caption');
|
|
157
|
+
cap.textContent = str(f, 'text') ?? '';
|
|
158
|
+
const head = el('tr');
|
|
159
|
+
for (const c of cols) {
|
|
160
|
+
const th = el('th');
|
|
161
|
+
th.textContent = c.label;
|
|
162
|
+
head.append(th);
|
|
163
|
+
}
|
|
164
|
+
const body = rows.map(r => {
|
|
165
|
+
const tr = el('tr');
|
|
166
|
+
for (const c of cols) {
|
|
167
|
+
const td = el('td');
|
|
168
|
+
td.textContent = cellText(rowField(r, c.sym));
|
|
169
|
+
tr.append(td);
|
|
170
|
+
}
|
|
171
|
+
return tr;
|
|
172
|
+
});
|
|
173
|
+
p.table.replaceChildren(cap, el('thead'), el('tbody'));
|
|
174
|
+
p.table.tHead.append(head);
|
|
175
|
+
p.table.tBodies[0].append(...body);
|
|
176
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell } from '../../core/index.ts';
|
|
2
|
+
import { type Mounted } from './mount.ts';
|
|
3
|
+
import { type MenuEntry } from './menus.ts';
|
|
4
|
+
/** Runs a face's on-xxx handler; `words` are the event's own [word: value ...]. */
|
|
5
|
+
export declare function fireEvent(face: ObjectCell, name: string, words?: [string, Cell][]): void;
|
|
6
|
+
/** Keydown events some face's on-key already saw; the view gets the rest. */
|
|
7
|
+
export declare const seenKeys: WeakSet<Event>;
|
|
8
|
+
/** The view with an on-key handler, if any. */
|
|
9
|
+
export declare let viewKeys: ObjectCell | null;
|
|
10
|
+
export declare function setViewKeys(v: ObjectCell | null): void;
|
|
11
|
+
/** Mounted buttons and links with a shortcut. */
|
|
12
|
+
export declare const shortcuts: Set<Mounted>;
|
|
13
|
+
export declare function listen(m: Mounted, style: string): void;
|
|
14
|
+
/** The context menu of a face, or of one of its rows: its own items, then "Move to" if it can be dragged. */
|
|
15
|
+
export declare function contextEntries(f: ObjectCell, item: Element | null): MenuEntry[];
|
|
16
|
+
export declare function handlerFor(face: ObjectCell, name: string): boolean;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// --- general events ------------------------------------------------------------------
|
|
2
|
+
import { K, NONE, TRUE, FALSE, intern, canon, blockOf, wordOf } from "../../core/index.js";
|
|
3
|
+
import { get, str, own, flag, icon } from "./faces.js";
|
|
4
|
+
import { rowOf, fire, el } from "./mount.js";
|
|
5
|
+
import { swipeRows } from "./swipe.js";
|
|
6
|
+
import { dropZones, moveTargets } from "./drag.js";
|
|
7
|
+
import { trebolEntries, openMenu } from "./menus.js";
|
|
8
|
+
import { keyCell, keyWords } from "./keys.js";
|
|
9
|
+
/** Runs a face's on-xxx handler; `words` are the event's own [word: value ...]. */
|
|
10
|
+
export function fireEvent(face, name, words = []) {
|
|
11
|
+
const extra = words.flatMap(([w, v]) => [wordOf(K.SetWord, w), v]);
|
|
12
|
+
void fire('ui-event', face, wordOf(K.LitWord, name), blockOf(K.Block, extra));
|
|
13
|
+
}
|
|
14
|
+
/** Keydown events some face's on-key already saw; the view gets the rest. */
|
|
15
|
+
export const seenKeys = new WeakSet();
|
|
16
|
+
/** The view with an on-key handler, if any. */
|
|
17
|
+
export let viewKeys = null;
|
|
18
|
+
export function setViewKeys(v) {
|
|
19
|
+
viewKeys = v;
|
|
20
|
+
}
|
|
21
|
+
/** Mounted buttons and links with a shortcut. */
|
|
22
|
+
export const shortcuts = new Set();
|
|
23
|
+
export function listen(m, style) {
|
|
24
|
+
const f = m.face;
|
|
25
|
+
const node = m.el;
|
|
26
|
+
const left = (ev) => !node.contains(ev.relatedTarget);
|
|
27
|
+
if (handlerFor(f, 'on-focus'))
|
|
28
|
+
node.addEventListener('focusin', ev => { if (left(ev))
|
|
29
|
+
fireEvent(f, 'on-focus'); });
|
|
30
|
+
if (handlerFor(f, 'on-unfocus'))
|
|
31
|
+
node.addEventListener('focusout', ev => { if (left(ev))
|
|
32
|
+
fireEvent(f, 'on-unfocus'); });
|
|
33
|
+
if (handlerFor(f, 'on-over')) {
|
|
34
|
+
// as in Red, event/away? is true when the pointer leaves
|
|
35
|
+
node.addEventListener('pointerenter', () => fireEvent(f, 'on-over', [['away?', FALSE]]));
|
|
36
|
+
node.addEventListener('pointerleave', () => fireEvent(f, 'on-over', [['away?', TRUE]]));
|
|
37
|
+
}
|
|
38
|
+
if (handlerFor(f, 'on-dbl-click'))
|
|
39
|
+
node.addEventListener('dblclick', () => fireEvent(f, 'on-dbl-click'));
|
|
40
|
+
if (handlerFor(f, 'on-end'))
|
|
41
|
+
node.addEventListener('ended', () => fireEvent(f, 'on-end'));
|
|
42
|
+
if (handlerFor(f, 'on-key')) {
|
|
43
|
+
if (style === 'view')
|
|
44
|
+
viewKeys = f;
|
|
45
|
+
else
|
|
46
|
+
node.addEventListener('keydown', ev => {
|
|
47
|
+
if (seenKeys.has(ev) || keyCell(ev) === null)
|
|
48
|
+
return;
|
|
49
|
+
seenKeys.add(ev);
|
|
50
|
+
fireEvent(f, 'on-key', keyWords(ev));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (str(f, 'shortcut') !== null)
|
|
54
|
+
shortcuts.add(m);
|
|
55
|
+
if (flag(f, 'drop'))
|
|
56
|
+
dropZones.add(m);
|
|
57
|
+
const menu = get(f, 'menu');
|
|
58
|
+
// a button or link without an action opens its menu; anything else has it as its context menu
|
|
59
|
+
if (menu.t === K.Block && (style === 'button' || style === 'link') && get(f, 'action').t !== K.Block) {
|
|
60
|
+
node.setAttribute('aria-haspopup', 'menu');
|
|
61
|
+
node.setAttribute('aria-expanded', 'false');
|
|
62
|
+
// a chevron after the text (or alone after the icon) says it opens a menu; it turns up while open
|
|
63
|
+
if (m.label === null) {
|
|
64
|
+
m.label = el('span');
|
|
65
|
+
node.prepend(m.label);
|
|
66
|
+
}
|
|
67
|
+
const chevron = icon('chevron-down');
|
|
68
|
+
if (chevron !== null) {
|
|
69
|
+
chevron.classList.add('tb-menu-chevron');
|
|
70
|
+
node.append(chevron);
|
|
71
|
+
}
|
|
72
|
+
node.addEventListener('click', ev => {
|
|
73
|
+
ev.preventDefault();
|
|
74
|
+
const r = node.getBoundingClientRect();
|
|
75
|
+
openMenu(trebolEntries(f, menu, NONE), r.left, r.bottom + 4, node);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else if (menu.t === K.Block || flag(f, 'drag')) {
|
|
79
|
+
if (menu.t === K.Block && (style === 'table' || style === 'list'))
|
|
80
|
+
swipeRows(node, f);
|
|
81
|
+
node.addEventListener('contextmenu', ev => {
|
|
82
|
+
const target = ev.target;
|
|
83
|
+
const data = style === 'table' || style === 'list' || style === 'tree';
|
|
84
|
+
const item = data ? target.closest(style === 'tree' ? '[role="treeitem"]' : 'tr, li') : null;
|
|
85
|
+
if (item === null && data && get(f, 'menu').t !== K.Block)
|
|
86
|
+
return;
|
|
87
|
+
const entries = contextEntries(f, item);
|
|
88
|
+
if (entries.length === 0)
|
|
89
|
+
return;
|
|
90
|
+
ev.preventDefault();
|
|
91
|
+
ev.stopPropagation();
|
|
92
|
+
// from the keyboard (Menu key, Shift+F10) there is no pointer: open by the focused element
|
|
93
|
+
let { clientX: x, clientY: y } = ev;
|
|
94
|
+
if (x === 0 && y === 0) {
|
|
95
|
+
const r = (item ?? (document.activeElement && node.contains(document.activeElement) ? document.activeElement : node)).getBoundingClientRect();
|
|
96
|
+
x = r.left + 8;
|
|
97
|
+
y = r.top + Math.min(r.height, 40);
|
|
98
|
+
}
|
|
99
|
+
openMenu(entries, x, y, item ?? target);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** The context menu of a face, or of one of its rows: its own items, then "Move to" if it can be dragged. */
|
|
104
|
+
export function contextEntries(f, item) {
|
|
105
|
+
const menu = get(f, 'menu');
|
|
106
|
+
const row = item !== null ? rowOf.get(item) ?? NONE : NONE;
|
|
107
|
+
const entries = menu.t === K.Block ? trebolEntries(f, menu, row) : [];
|
|
108
|
+
// faces you can drag can also be moved to a drop zone from the menu, without a mouse
|
|
109
|
+
if (flag(f, 'drag')) {
|
|
110
|
+
const zones = moveTargets(f, item !== null ? row : get(f, 'value'));
|
|
111
|
+
if (zones.length > 0) {
|
|
112
|
+
if (entries.length > 0)
|
|
113
|
+
entries.push({ text: '', divider: true });
|
|
114
|
+
entries.push({ text: own('move-to'), sub: zones });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return entries;
|
|
118
|
+
}
|
|
119
|
+
export function handlerFor(face, name) {
|
|
120
|
+
const ev = get(face, 'events');
|
|
121
|
+
if (ev.t !== K.Block)
|
|
122
|
+
return false;
|
|
123
|
+
const c = canon(intern(name));
|
|
124
|
+
return ev.s.data.some(x => x.t === K.Word && canon(x.sym) === c);
|
|
125
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Cell, type ObjectCell } from '../../core/index.ts';
|
|
2
|
+
export declare function get(face: ObjectCell, name: string): Cell;
|
|
3
|
+
export declare function str(face: ObjectCell, name: string): string | null;
|
|
4
|
+
/** What a choice or radio option is worth: itself, or the `value` of a map #(value: .. text: ..). */
|
|
5
|
+
export declare function optionValue(o: Cell): string;
|
|
6
|
+
/** What a choice or radio option shows, in the active language. */
|
|
7
|
+
export declare function optionText(o: Cell): string;
|
|
8
|
+
/** A group of options: its title and its options. */
|
|
9
|
+
export interface OptionGroup {
|
|
10
|
+
title: Cell;
|
|
11
|
+
options: Cell[];
|
|
12
|
+
}
|
|
13
|
+
/** The options of a choice or radio as written: options and groups, a title followed by a block. */
|
|
14
|
+
export declare function entriesOf(face: ObjectCell): (Cell | OptionGroup)[];
|
|
15
|
+
export declare function isGroup(e: Cell | OptionGroup): e is OptionGroup;
|
|
16
|
+
/** The options of a choice or radio, those of its groups included, in order. */
|
|
17
|
+
export declare function optionsOf(face: ObjectCell): Cell[];
|
|
18
|
+
/** What tells apart two sets of options: their values and where each group starts. */
|
|
19
|
+
export declare function optionsKey(face: ObjectCell): string;
|
|
20
|
+
/** Writes the options of a select: an empty one first, then those of the face, each group in an optgroup. */
|
|
21
|
+
export declare function fillSelect(sel: HTMLSelectElement, face: ObjectCell): void;
|
|
22
|
+
/** One of Trebol's own texts (`i18n/ui`) in the active language. */
|
|
23
|
+
export declare function own(key: string, ...args: string[]): string;
|
|
24
|
+
/** Gives a node built once one of Trebol's own texts (its text or an attribute); `repaint` keeps it in the active language. */
|
|
25
|
+
export declare function keepOwn(node: HTMLElement, key: string, attr?: string): void;
|
|
26
|
+
export declare function showOwn(node: HTMLElement): void;
|
|
27
|
+
/** A value as it shows on screen: dates, numbers and money in the active language. */
|
|
28
|
+
export declare function display(v: Cell): string;
|
|
29
|
+
export declare function flag(face: ObjectCell, name: string): boolean;
|
|
30
|
+
export declare function word(face: ObjectCell, name: string): string | null;
|
|
31
|
+
export declare function children(face: ObjectCell): ObjectCell[];
|
|
32
|
+
/** The name of a face's icon (`#search` -> "search"). */
|
|
33
|
+
export declare function iconName(face: ObjectCell): string | null;
|
|
34
|
+
export declare function icon(name: string, label?: string | null): SVGSVGElement | null;
|
|
35
|
+
export declare function hasRule(face: ObjectCell, rule: string): boolean;
|