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