@stacksjs/components 0.2.854 → 0.2.857
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/dist/CodeEditor-jg4w7e15.stx +497 -0
- package/dist/ContextMenu-43g3w5tp.stx +188 -0
- package/dist/DataGrid-7rt7v865.stx +376 -0
- package/dist/GraphView-0t3t9mec.stx +320 -0
- package/dist/JsonTree-satc4010.stx +317 -0
- package/dist/PreviewFrame-erbgvzj8.stx +257 -0
- package/dist/SplitPane-7sqfdwqa.stx +159 -0
- package/dist/code-editor/editor.js +2166 -0
- package/dist/component-api/component-api.js +36 -0
- package/dist/components/CodeBlock.d.ts +12 -5
- package/dist/context-menu/context-menu.js +575 -0
- package/dist/data-grid/data-grid.js +1244 -0
- package/dist/graph-view/graph-view.js +1408 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +34 -15
- package/dist/json-tree/json-tree.js +1121 -0
- package/dist/preview-frame/preview-frame.js +356 -0
- package/dist/split-pane/split-pane.js +364 -0
- package/dist/ui/code-editor/CodeEditor.d.ts +46 -0
- package/dist/ui/code-editor/controller.d.ts +103 -0
- package/dist/ui/code-editor/core/commands.d.ts +55 -0
- package/dist/ui/code-editor/core/completion.d.ts +40 -0
- package/dist/ui/code-editor/core/editor.d.ts +69 -0
- package/dist/ui/code-editor/core/highlight.d.ts +61 -0
- package/dist/ui/code-editor/core/history.d.ts +27 -0
- package/dist/ui/code-editor/core/index.d.ts +13 -0
- package/dist/ui/code-editor/core/keymap.d.ts +38 -0
- package/dist/ui/code-editor/core/selection.d.ts +30 -0
- package/dist/ui/code-editor/core/text.d.ts +64 -0
- package/dist/ui/code-editor/editor.d.ts +15 -0
- package/dist/ui/code-editor/index.d.ts +29 -0
- package/dist/ui/code-editor/tokenizer.d.ts +18 -0
- package/dist/ui/code-editor/view.d.ts +59 -0
- package/dist/ui/context-menu/ContextMenu.d.ts +14 -0
- package/dist/ui/context-menu/context-menu.d.ts +8 -0
- package/dist/ui/context-menu/core.d.ts +74 -0
- package/dist/ui/context-menu/index.d.ts +11 -0
- package/dist/ui/context-menu/view.d.ts +36 -0
- package/dist/ui/data-grid/DataGrid.d.ts +30 -0
- package/dist/ui/data-grid/core.d.ts +141 -0
- package/dist/ui/data-grid/data-grid.d.ts +9 -0
- package/dist/ui/data-grid/index.d.ts +20 -0
- package/dist/ui/data-grid/view.d.ts +56 -0
- package/dist/ui/graph-view/GraphView.d.ts +21 -0
- package/dist/ui/graph-view/core/edges.d.ts +34 -0
- package/dist/ui/graph-view/core/force.d.ts +10 -0
- package/dist/ui/graph-view/core/graph.d.ts +83 -0
- package/dist/ui/graph-view/core/index.d.ts +5 -0
- package/dist/ui/graph-view/core/layered.d.ts +24 -0
- package/dist/ui/graph-view/core/viewport.d.ts +34 -0
- package/dist/ui/graph-view/graph-view.d.ts +9 -0
- package/dist/ui/graph-view/index.d.ts +21 -0
- package/dist/ui/graph-view/view.d.ts +51 -0
- package/dist/ui/json-tree/JsonTree.d.ts +18 -0
- package/dist/ui/json-tree/core.d.ts +108 -0
- package/dist/ui/json-tree/index.d.ts +18 -0
- package/dist/ui/json-tree/json-tree.d.ts +10 -0
- package/dist/ui/json-tree/tinker.d.ts +84 -0
- package/dist/ui/json-tree/view.d.ts +59 -0
- package/dist/ui/preview-frame/PreviewFrame.d.ts +44 -0
- package/dist/ui/preview-frame/core.d.ts +81 -0
- package/dist/ui/preview-frame/index.d.ts +19 -0
- package/dist/ui/preview-frame/preview-frame.d.ts +8 -0
- package/dist/ui/preview-frame/view.d.ts +37 -0
- package/dist/ui/split-pane/SplitPane.d.ts +18 -0
- package/dist/ui/split-pane/core.d.ts +58 -0
- package/dist/ui/split-pane/index.d.ts +15 -0
- package/dist/ui/split-pane/split-pane.d.ts +8 -0
- package/dist/ui/split-pane/view.d.ts +27 -0
- package/dist/ui/virtual-table/index.d.ts +6 -0
- package/dist/utils/component-api.d.ts +4 -0
- package/dist/utils/highlighter.d.ts +19 -1
- package/dist/utils/highlighter.js +24 -7
- package/dist/utils/syntax-theme.d.ts +44 -0
- package/package.json +44 -4
- package/src/components/CodeBlock.stx +195 -17
- package/src/index.ts +10 -0
- package/src/ui/code-editor/CodeEditor.stx +497 -0
- package/src/ui/code-editor/README.md +135 -0
- package/src/ui/code-editor/controller.ts +517 -0
- package/src/ui/code-editor/core/commands.ts +381 -0
- package/src/ui/code-editor/core/completion.ts +212 -0
- package/src/ui/code-editor/core/editor.ts +319 -0
- package/src/ui/code-editor/core/highlight.ts +203 -0
- package/src/ui/code-editor/core/history.ts +190 -0
- package/src/ui/code-editor/core/index.ts +14 -0
- package/src/ui/code-editor/core/keymap.ts +125 -0
- package/src/ui/code-editor/core/selection.ts +104 -0
- package/src/ui/code-editor/core/text.ts +252 -0
- package/src/ui/code-editor/editor.ts +16 -0
- package/src/ui/code-editor/index.ts +46 -0
- package/src/ui/code-editor/tokenizer.ts +74 -0
- package/src/ui/code-editor/view.ts +1066 -0
- package/src/ui/context-menu/ContextMenu.stx +188 -0
- package/src/ui/context-menu/README.md +117 -0
- package/src/ui/context-menu/context-menu.ts +8 -0
- package/src/ui/context-menu/core.ts +250 -0
- package/src/ui/context-menu/index.ts +16 -0
- package/src/ui/context-menu/view.ts +540 -0
- package/src/ui/data-grid/DataGrid.stx +376 -0
- package/src/ui/data-grid/README.md +204 -0
- package/src/ui/data-grid/core.ts +813 -0
- package/src/ui/data-grid/data-grid.ts +9 -0
- package/src/ui/data-grid/index.ts +34 -0
- package/src/ui/data-grid/view.ts +806 -0
- package/src/ui/graph-view/GraphView.stx +320 -0
- package/src/ui/graph-view/README.md +210 -0
- package/src/ui/graph-view/core/edges.ts +180 -0
- package/src/ui/graph-view/core/force.ts +237 -0
- package/src/ui/graph-view/core/graph.ts +277 -0
- package/src/ui/graph-view/core/index.ts +5 -0
- package/src/ui/graph-view/core/layered.ts +256 -0
- package/src/ui/graph-view/core/viewport.ts +141 -0
- package/src/ui/graph-view/graph-view.ts +9 -0
- package/src/ui/graph-view/index.ts +35 -0
- package/src/ui/graph-view/view.ts +809 -0
- package/src/ui/json-tree/JsonTree.stx +317 -0
- package/src/ui/json-tree/README.md +193 -0
- package/src/ui/json-tree/core.ts +588 -0
- package/src/ui/json-tree/index.ts +28 -0
- package/src/ui/json-tree/json-tree.ts +10 -0
- package/src/ui/json-tree/tinker.ts +349 -0
- package/src/ui/json-tree/view.ts +665 -0
- package/src/ui/preview-frame/PreviewFrame.stx +257 -0
- package/src/ui/preview-frame/README.md +107 -0
- package/src/ui/preview-frame/core.ts +195 -0
- package/src/ui/preview-frame/index.ts +31 -0
- package/src/ui/preview-frame/preview-frame.ts +8 -0
- package/src/ui/preview-frame/view.ts +336 -0
- package/src/ui/split-pane/README.md +87 -0
- package/src/ui/split-pane/SplitPane.stx +159 -0
- package/src/ui/split-pane/core.ts +200 -0
- package/src/ui/split-pane/index.ts +24 -0
- package/src/ui/split-pane/split-pane.ts +8 -0
- package/src/ui/split-pane/view.ts +305 -0
- package/src/ui/virtual-table/index.ts +6 -0
- package/src/utils/component-api.ts +72 -0
- package/src/utils/highlighter.ts +121 -218
- package/src/utils/syntax-theme.ts +144 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/utils/component-api.ts
|
|
2
|
+
var SCOPE = "[data-stx-scope]";
|
|
3
|
+
function find(target) {
|
|
4
|
+
if (!target)
|
|
5
|
+
return null;
|
|
6
|
+
if (typeof target !== "string")
|
|
7
|
+
return target;
|
|
8
|
+
if (typeof document === "undefined")
|
|
9
|
+
return null;
|
|
10
|
+
const byId = /^[\w-]+$/.test(target) ? document.getElementById(target) : null;
|
|
11
|
+
if (byId)
|
|
12
|
+
return byId;
|
|
13
|
+
try {
|
|
14
|
+
return document.querySelector(target);
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function componentApi(target) {
|
|
20
|
+
const element = find(target);
|
|
21
|
+
if (!element || typeof element.closest !== "function")
|
|
22
|
+
return null;
|
|
23
|
+
if (element.__stx_exposed)
|
|
24
|
+
return element.__stx_exposed;
|
|
25
|
+
const around = element.closest(SCOPE);
|
|
26
|
+
if (around?.__stx_exposed)
|
|
27
|
+
return around.__stx_exposed;
|
|
28
|
+
for (const inside of Array.from(element.querySelectorAll(SCOPE))) {
|
|
29
|
+
if (inside.__stx_exposed)
|
|
30
|
+
return inside.__stx_exposed;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
componentApi
|
|
36
|
+
};
|
|
@@ -13,14 +13,21 @@ export declare const theme: unknown;
|
|
|
13
13
|
export declare const lineNumbers: unknown;
|
|
14
14
|
export declare const copyable: unknown;
|
|
15
15
|
export declare const className: unknown;
|
|
16
|
+
/*
|
|
17
|
+
* `lineNumbers` is still not passed on. `highlight()` can number lines now (as
|
|
18
|
+
* a CSS counter, so they never reach a copy), but this prop defaults to true
|
|
19
|
+
* and has always been dropped: honouring it would put numbers on every block
|
|
20
|
+
* already in use. Turning it on is its own change.
|
|
21
|
+
*/
|
|
16
22
|
declare const result: unknown;
|
|
17
23
|
export declare const highlightedCode: unknown;
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
// is fixed in stx, an app includes this itself (or sets its own token styles).
|
|
24
|
+
// The same sheet the <style> block below ships as literal text. A <style>
|
|
25
|
+
// inside a component is extracted from its source before interpolation runs,
|
|
26
|
+
// so `{{ highlightCss }}` in one would ship an empty tag; the literal copy is
|
|
27
|
+
// what a page gets, and this export is for a caller that wants the string.
|
|
23
28
|
export declare const highlightCss: unknown;
|
|
29
|
+
export declare const renderedOnServer: boolean;
|
|
30
|
+
export declare const liveClasses: unknown;
|
|
24
31
|
/*
|
|
25
32
|
* The clipboard payload, decided on the SERVER.
|
|
26
33
|
*
|
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
// src/ui/context-menu/core.ts
|
|
2
|
+
function resolveItems(items, parent = []) {
|
|
3
|
+
const resolved = [];
|
|
4
|
+
if (!Array.isArray(items))
|
|
5
|
+
return resolved;
|
|
6
|
+
for (const item of items) {
|
|
7
|
+
if (!item || typeof item !== "object")
|
|
8
|
+
continue;
|
|
9
|
+
if (item.separator) {
|
|
10
|
+
if (resolved.length > 0 && !resolved[resolved.length - 1].separator)
|
|
11
|
+
resolved.push({ separator: true, id: "", path: [] });
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
resolved.push({ ...item, id: item.id == null ? "" : String(item.id), path: [] });
|
|
15
|
+
}
|
|
16
|
+
while (resolved.length > 0 && resolved[resolved.length - 1].separator)
|
|
17
|
+
resolved.pop();
|
|
18
|
+
resolved.forEach((item, index) => {
|
|
19
|
+
item.path = [...parent, index];
|
|
20
|
+
if (item.separator || item.id === "")
|
|
21
|
+
item.id = item.path.join(".");
|
|
22
|
+
if (!item.separator && Array.isArray(item.children))
|
|
23
|
+
item.children = resolveItems(item.children, item.path);
|
|
24
|
+
});
|
|
25
|
+
return resolved;
|
|
26
|
+
}
|
|
27
|
+
function isFocusable(item) {
|
|
28
|
+
return Boolean(item) && !item.separator;
|
|
29
|
+
}
|
|
30
|
+
function isActionable(item) {
|
|
31
|
+
return Boolean(item) && !item.separator && !item.disabled;
|
|
32
|
+
}
|
|
33
|
+
function hasSubmenu(item) {
|
|
34
|
+
return Boolean(item) && !item.separator && Array.isArray(item.children) && item.children.length > 0;
|
|
35
|
+
}
|
|
36
|
+
function stepIndex(items, from, direction) {
|
|
37
|
+
const count = items.length;
|
|
38
|
+
if (count === 0)
|
|
39
|
+
return -1;
|
|
40
|
+
let index = from;
|
|
41
|
+
for (let tries = 0;tries < count; tries++) {
|
|
42
|
+
index = index < 0 ? direction === 1 ? 0 : count - 1 : (index + direction + count) % count;
|
|
43
|
+
if (isFocusable(items[index]))
|
|
44
|
+
return index;
|
|
45
|
+
}
|
|
46
|
+
return -1;
|
|
47
|
+
}
|
|
48
|
+
function firstIndex(items) {
|
|
49
|
+
return stepIndex(items, -1, 1);
|
|
50
|
+
}
|
|
51
|
+
function lastIndex(items) {
|
|
52
|
+
return stepIndex(items, -1, -1);
|
|
53
|
+
}
|
|
54
|
+
function typeAheadIndex(items, from, typed) {
|
|
55
|
+
const query = typed.toLowerCase();
|
|
56
|
+
if (query === "" || items.length === 0)
|
|
57
|
+
return -1;
|
|
58
|
+
const repeated = query.length > 1 && query.split("").every((letter) => letter === query.charAt(0));
|
|
59
|
+
const needle = repeated ? query.charAt(0) : query;
|
|
60
|
+
const start = query.length === 1 || repeated ? from + 1 : Math.max(from, 0);
|
|
61
|
+
for (let offset = 0;offset < items.length; offset++) {
|
|
62
|
+
const index = (start + offset + items.length) % items.length;
|
|
63
|
+
const item = items[index];
|
|
64
|
+
if (isFocusable(item) && (item.label || "").toLowerCase().startsWith(needle))
|
|
65
|
+
return index;
|
|
66
|
+
}
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
function findItem(items, id) {
|
|
70
|
+
for (const item of items) {
|
|
71
|
+
if (item.separator)
|
|
72
|
+
continue;
|
|
73
|
+
if (item.id === id)
|
|
74
|
+
return item;
|
|
75
|
+
if (item.children) {
|
|
76
|
+
const found = findItem(item.children, id);
|
|
77
|
+
if (found)
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
function placeAtPoint(point, menu, viewport, margin = 8) {
|
|
84
|
+
const fit = (at, size, limit) => {
|
|
85
|
+
if (at + size <= limit - margin)
|
|
86
|
+
return at;
|
|
87
|
+
if (at - size >= margin)
|
|
88
|
+
return at - size;
|
|
89
|
+
return Math.max(margin, limit - margin - size);
|
|
90
|
+
};
|
|
91
|
+
return { x: fit(point.x, menu.width, viewport.width), y: fit(point.y, menu.height, viewport.height) };
|
|
92
|
+
}
|
|
93
|
+
function placeAtBox(anchor, menu, viewport, margin = 8, gap = 4) {
|
|
94
|
+
let x = anchor.left;
|
|
95
|
+
if (x + menu.width > viewport.width - margin)
|
|
96
|
+
x = Math.max(margin, Math.min(anchor.right - menu.width, viewport.width - margin - menu.width));
|
|
97
|
+
let y = anchor.bottom + gap;
|
|
98
|
+
if (y + menu.height > viewport.height - margin) {
|
|
99
|
+
const above = anchor.top - gap - menu.height;
|
|
100
|
+
y = above >= margin ? above : Math.max(margin, viewport.height - margin - menu.height);
|
|
101
|
+
}
|
|
102
|
+
return { x: Math.max(margin, x), y };
|
|
103
|
+
}
|
|
104
|
+
function placeSubmenu(item, menu, viewport, margin = 8, overlap = 4) {
|
|
105
|
+
let side = "right";
|
|
106
|
+
let x = item.right - overlap;
|
|
107
|
+
if (x + menu.width > viewport.width - margin) {
|
|
108
|
+
const left = item.left + overlap - menu.width;
|
|
109
|
+
if (left >= margin) {
|
|
110
|
+
x = left;
|
|
111
|
+
side = "left";
|
|
112
|
+
} else {
|
|
113
|
+
x = Math.max(margin, viewport.width - margin - menu.width);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const y = Math.max(margin, Math.min(item.top - overlap, viewport.height - margin - menu.height));
|
|
117
|
+
return { x, y, side };
|
|
118
|
+
}
|
|
119
|
+
var APPLE_SYMBOLS = { mod: "⌘", meta: "⌘", cmd: "⌘", command: "⌘", ctrl: "⌃", control: "⌃", alt: "⌥", option: "⌥", shift: "⇧" };
|
|
120
|
+
var OTHER_NAMES = { mod: "Ctrl", meta: "Win", cmd: "Ctrl", command: "Ctrl", ctrl: "Ctrl", control: "Ctrl", alt: "Alt", option: "Alt", shift: "Shift" };
|
|
121
|
+
var APPLE_KEYS = { enter: "↩", backspace: "⌫", delete: "⌦", escape: "⎋", esc: "⎋", tab: "⇥", arrowup: "↑", arrowdown: "↓", arrowleft: "←", arrowright: "→", space: "Space" };
|
|
122
|
+
var OTHER_KEYS = { escape: "Esc", esc: "Esc", delete: "Del", arrowup: "↑", arrowdown: "↓", arrowleft: "←", arrowright: "→", space: "Space" };
|
|
123
|
+
var APPLE_ORDER = ["ctrl", "control", "alt", "option", "shift", "mod", "meta", "cmd", "command"];
|
|
124
|
+
var OTHER_ORDER = ["mod", "ctrl", "control", "cmd", "command", "meta", "alt", "option", "shift"];
|
|
125
|
+
function splitShortcut(shortcut) {
|
|
126
|
+
const parts = shortcut.endsWith("++") ? [...shortcut.slice(0, -2).split("+"), "+"] : shortcut.split("+");
|
|
127
|
+
const cleaned = parts.map((part) => part.trim()).filter((part) => part !== "");
|
|
128
|
+
const key = cleaned.pop() || "";
|
|
129
|
+
return { modifiers: cleaned.map((part) => part.toLowerCase()), key };
|
|
130
|
+
}
|
|
131
|
+
function capitalize(key) {
|
|
132
|
+
return key.length === 1 ? key.toUpperCase() : key.charAt(0).toUpperCase() + key.slice(1);
|
|
133
|
+
}
|
|
134
|
+
function formatShortcut(shortcut, apple) {
|
|
135
|
+
if (!shortcut)
|
|
136
|
+
return "";
|
|
137
|
+
const { modifiers, key } = splitShortcut(shortcut);
|
|
138
|
+
const order = apple ? APPLE_ORDER : OTHER_ORDER;
|
|
139
|
+
const sorted = [...modifiers].sort((a, b) => order.indexOf(a) - order.indexOf(b));
|
|
140
|
+
const names = sorted.map((modifier) => (apple ? APPLE_SYMBOLS : OTHER_NAMES)[modifier] ?? capitalize(modifier));
|
|
141
|
+
const keyName = (apple ? APPLE_KEYS : OTHER_KEYS)[key.toLowerCase()] ?? capitalize(key);
|
|
142
|
+
return apple ? names.join("") + keyName : [...new Set(names), keyName].join("+");
|
|
143
|
+
}
|
|
144
|
+
function ariaShortcut(shortcut, apple) {
|
|
145
|
+
if (!shortcut)
|
|
146
|
+
return "";
|
|
147
|
+
const { modifiers, key } = splitShortcut(shortcut);
|
|
148
|
+
const names = { mod: apple ? "Meta" : "Control", meta: "Meta", cmd: "Meta", command: "Meta", ctrl: "Control", control: "Control", alt: "Alt", option: "Alt", shift: "Shift" };
|
|
149
|
+
return [...new Set(modifiers.map((modifier) => names[modifier] ?? capitalize(modifier))), capitalize(key)].join("+");
|
|
150
|
+
}
|
|
151
|
+
// src/ui/context-menu/view.ts
|
|
152
|
+
var SUBMENU_DELAY = 160;
|
|
153
|
+
var TYPE_AHEAD_RESET = 600;
|
|
154
|
+
function detectApple() {
|
|
155
|
+
const platform = typeof navigator === "undefined" ? "" : navigator.platform || navigator.userAgent || "";
|
|
156
|
+
return /mac|iphone|ipad/i.test(platform);
|
|
157
|
+
}
|
|
158
|
+
var menuCounter = 0;
|
|
159
|
+
|
|
160
|
+
class ContextMenuView {
|
|
161
|
+
root;
|
|
162
|
+
callbacks;
|
|
163
|
+
items;
|
|
164
|
+
label;
|
|
165
|
+
disabled;
|
|
166
|
+
apple;
|
|
167
|
+
layer;
|
|
168
|
+
uid = `stx-menu-${++menuCounter}`;
|
|
169
|
+
stack = [];
|
|
170
|
+
target = null;
|
|
171
|
+
returnFocus = null;
|
|
172
|
+
hoverTimer = null;
|
|
173
|
+
typed = "";
|
|
174
|
+
typedAt = 0;
|
|
175
|
+
disposers = [];
|
|
176
|
+
constructor(root, options = {}, callbacks = {}) {
|
|
177
|
+
this.root = root;
|
|
178
|
+
this.callbacks = callbacks;
|
|
179
|
+
this.items = options.items ?? null;
|
|
180
|
+
this.label = options.label || "Context menu";
|
|
181
|
+
this.disabled = Boolean(options.disabled);
|
|
182
|
+
this.apple = options.apple ?? detectApple();
|
|
183
|
+
this.layer = document.createElement("div");
|
|
184
|
+
this.layer.className = "stx-menu-layer";
|
|
185
|
+
this.layer.setAttribute("data-menu-layer", "");
|
|
186
|
+
this.layer.__stx_inert_content = true;
|
|
187
|
+
if ("popover" in HTMLElement.prototype)
|
|
188
|
+
this.layer.setAttribute("popover", "manual");
|
|
189
|
+
else
|
|
190
|
+
this.layer.hidden = true;
|
|
191
|
+
this.root.appendChild(this.layer);
|
|
192
|
+
this.listen(this.root, "contextmenu", (event) => this.onContextMenu(event));
|
|
193
|
+
this.listen(this.root, "keydown", (event) => this.onHostKeyDown(event));
|
|
194
|
+
this.listen(this.layer, "keydown", (event) => this.onMenuKeyDown(event));
|
|
195
|
+
this.listen(this.layer, "click", (event) => this.onMenuClick(event));
|
|
196
|
+
this.listen(this.layer, "pointermove", (event) => this.onMenuPointerMove(event));
|
|
197
|
+
this.listen(this.layer, "contextmenu", (event) => event.preventDefault());
|
|
198
|
+
this.root.setAttribute("data-menu-ready", "");
|
|
199
|
+
}
|
|
200
|
+
get isOpen() {
|
|
201
|
+
return this.stack.length > 0;
|
|
202
|
+
}
|
|
203
|
+
setItems(items) {
|
|
204
|
+
this.items = items ?? null;
|
|
205
|
+
}
|
|
206
|
+
setDisabled(disabled) {
|
|
207
|
+
this.disabled = disabled;
|
|
208
|
+
if (disabled)
|
|
209
|
+
this.close("api");
|
|
210
|
+
}
|
|
211
|
+
setLabel(label) {
|
|
212
|
+
this.label = label || "Context menu";
|
|
213
|
+
}
|
|
214
|
+
open(at, items, options = {}) {
|
|
215
|
+
this.close("api", false);
|
|
216
|
+
const anchor = at instanceof Element ? at : null;
|
|
217
|
+
const target = options.target ?? anchor ?? null;
|
|
218
|
+
const source = items ?? (typeof this.items === "function" ? this.items(target) : this.items);
|
|
219
|
+
const resolved = resolveItems(source);
|
|
220
|
+
if (resolved.length === 0)
|
|
221
|
+
return false;
|
|
222
|
+
this.target = target;
|
|
223
|
+
const active = document.activeElement;
|
|
224
|
+
this.returnFocus = active instanceof HTMLElement && active !== document.body ? active : null;
|
|
225
|
+
this.showLayer();
|
|
226
|
+
const menu = this.buildMenu(resolved, null);
|
|
227
|
+
const size = menu.element.getBoundingClientRect();
|
|
228
|
+
const viewport = { width: window.innerWidth, height: window.innerHeight };
|
|
229
|
+
const place = anchor ? placeAtBox(boxOf(anchor), size, viewport) : placeAtPoint(at, size, viewport);
|
|
230
|
+
this.position(menu.element, place);
|
|
231
|
+
this.listenWhileOpen();
|
|
232
|
+
if (options.keyboard || anchor)
|
|
233
|
+
this.focusItem(menu, firstIndex(menu.items));
|
|
234
|
+
else
|
|
235
|
+
menu.element.focus({ preventScroll: true });
|
|
236
|
+
this.callbacks.onOpen?.({ x: place.x, y: place.y, target, keyboard: Boolean(options.keyboard) });
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
close(reason = "api", restoreFocus = true) {
|
|
240
|
+
if (!this.isOpen)
|
|
241
|
+
return;
|
|
242
|
+
this.clearHover();
|
|
243
|
+
for (const menu of this.stack)
|
|
244
|
+
menu.element.remove();
|
|
245
|
+
this.stack = [];
|
|
246
|
+
this.hideLayer();
|
|
247
|
+
this.stopListeningWhileOpen();
|
|
248
|
+
const back = this.returnFocus;
|
|
249
|
+
this.returnFocus = null;
|
|
250
|
+
this.target = null;
|
|
251
|
+
if (restoreFocus && reason !== "outside" && reason !== "blur" && back && back.isConnected)
|
|
252
|
+
back.focus({ preventScroll: true });
|
|
253
|
+
this.callbacks.onClose?.({ reason });
|
|
254
|
+
}
|
|
255
|
+
destroy() {
|
|
256
|
+
this.close("api", false);
|
|
257
|
+
for (const dispose of this.disposers)
|
|
258
|
+
dispose();
|
|
259
|
+
this.layer.remove();
|
|
260
|
+
this.root.removeAttribute("data-menu-ready");
|
|
261
|
+
}
|
|
262
|
+
listen(target, type, handler, options) {
|
|
263
|
+
target.addEventListener(type, handler, options);
|
|
264
|
+
const dispose = () => target.removeEventListener(type, handler, options);
|
|
265
|
+
this.disposers.push(dispose);
|
|
266
|
+
return dispose;
|
|
267
|
+
}
|
|
268
|
+
whileOpen = [];
|
|
269
|
+
listenWhileOpen() {
|
|
270
|
+
const add = (target, type, handler, options) => {
|
|
271
|
+
target.addEventListener(type, handler, options);
|
|
272
|
+
this.whileOpen.push(() => target.removeEventListener(type, handler, options));
|
|
273
|
+
};
|
|
274
|
+
const outside = (event) => {
|
|
275
|
+
if (!(event.target instanceof Node) || !this.layer.contains(event.target))
|
|
276
|
+
this.close("outside");
|
|
277
|
+
};
|
|
278
|
+
add(document, "pointerdown", outside, { capture: true });
|
|
279
|
+
add(document, "scroll", outside, { capture: true, passive: true });
|
|
280
|
+
add(window, "resize", () => this.close("blur"));
|
|
281
|
+
add(window, "blur", () => this.close("blur"));
|
|
282
|
+
}
|
|
283
|
+
stopListeningWhileOpen() {
|
|
284
|
+
for (const dispose of this.whileOpen)
|
|
285
|
+
dispose();
|
|
286
|
+
this.whileOpen = [];
|
|
287
|
+
}
|
|
288
|
+
showLayer() {
|
|
289
|
+
if (this.layer.hasAttribute("popover")) {
|
|
290
|
+
try {
|
|
291
|
+
this.layer.showPopover();
|
|
292
|
+
} catch {}
|
|
293
|
+
} else {
|
|
294
|
+
this.layer.hidden = false;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
hideLayer() {
|
|
298
|
+
if (this.layer.hasAttribute("popover")) {
|
|
299
|
+
try {
|
|
300
|
+
this.layer.hidePopover();
|
|
301
|
+
} catch {}
|
|
302
|
+
} else {
|
|
303
|
+
this.layer.hidden = true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
position(element, at) {
|
|
307
|
+
element.style.left = `${Math.round(at.x)}px`;
|
|
308
|
+
element.style.top = `${Math.round(at.y)}px`;
|
|
309
|
+
}
|
|
310
|
+
buildMenu(items, opener) {
|
|
311
|
+
const element = document.createElement("div");
|
|
312
|
+
element.className = "stx-menu";
|
|
313
|
+
element.setAttribute("role", "menu");
|
|
314
|
+
element.setAttribute("tabindex", "-1");
|
|
315
|
+
element.setAttribute("data-menu-depth", String(this.stack.length));
|
|
316
|
+
if (opener)
|
|
317
|
+
element.setAttribute("aria-labelledby", opener.id);
|
|
318
|
+
else
|
|
319
|
+
element.setAttribute("aria-label", this.label);
|
|
320
|
+
items.forEach((item, index) => {
|
|
321
|
+
if (item.separator) {
|
|
322
|
+
const line = document.createElement("div");
|
|
323
|
+
line.className = "stx-menu-separator";
|
|
324
|
+
line.setAttribute("role", "separator");
|
|
325
|
+
element.appendChild(line);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const row = document.createElement("div");
|
|
329
|
+
row.className = "stx-menu-item";
|
|
330
|
+
row.id = `${this.uid}-${item.path.join("-")}`;
|
|
331
|
+
row.setAttribute("role", typeof item.checked === "boolean" ? "menuitemcheckbox" : "menuitem");
|
|
332
|
+
row.setAttribute("tabindex", "-1");
|
|
333
|
+
row.setAttribute("data-menu-index", String(index));
|
|
334
|
+
row.setAttribute("data-menu-id", item.id);
|
|
335
|
+
if (typeof item.checked === "boolean")
|
|
336
|
+
row.setAttribute("aria-checked", String(item.checked));
|
|
337
|
+
if (item.disabled)
|
|
338
|
+
row.setAttribute("aria-disabled", "true");
|
|
339
|
+
if (item.danger)
|
|
340
|
+
row.setAttribute("data-danger", "");
|
|
341
|
+
const check = document.createElement("span");
|
|
342
|
+
check.className = "stx-menu-check";
|
|
343
|
+
check.setAttribute("aria-hidden", "true");
|
|
344
|
+
check.textContent = item.checked === true ? "✓" : "";
|
|
345
|
+
row.appendChild(check);
|
|
346
|
+
const label = document.createElement("span");
|
|
347
|
+
label.className = "stx-menu-label";
|
|
348
|
+
label.textContent = item.label || "";
|
|
349
|
+
row.appendChild(label);
|
|
350
|
+
if (hasSubmenu(item)) {
|
|
351
|
+
row.setAttribute("aria-haspopup", "menu");
|
|
352
|
+
row.setAttribute("aria-expanded", "false");
|
|
353
|
+
const arrow = document.createElement("span");
|
|
354
|
+
arrow.className = "stx-menu-arrow";
|
|
355
|
+
arrow.setAttribute("aria-hidden", "true");
|
|
356
|
+
arrow.textContent = "›";
|
|
357
|
+
row.appendChild(arrow);
|
|
358
|
+
} else if (item.shortcut) {
|
|
359
|
+
row.setAttribute("aria-keyshortcuts", ariaShortcut(item.shortcut, this.apple));
|
|
360
|
+
const shortcut = document.createElement("span");
|
|
361
|
+
shortcut.className = "stx-menu-shortcut";
|
|
362
|
+
shortcut.setAttribute("aria-hidden", "true");
|
|
363
|
+
shortcut.textContent = formatShortcut(item.shortcut, this.apple);
|
|
364
|
+
row.appendChild(shortcut);
|
|
365
|
+
}
|
|
366
|
+
element.appendChild(row);
|
|
367
|
+
});
|
|
368
|
+
this.layer.appendChild(element);
|
|
369
|
+
const menu = { element, items, opener };
|
|
370
|
+
this.stack.push(menu);
|
|
371
|
+
return menu;
|
|
372
|
+
}
|
|
373
|
+
menuOf(node) {
|
|
374
|
+
if (!(node instanceof Element))
|
|
375
|
+
return null;
|
|
376
|
+
const element = node.closest(".stx-menu");
|
|
377
|
+
return this.stack.find((menu) => menu.element === element) ?? null;
|
|
378
|
+
}
|
|
379
|
+
rowAt(menu, index) {
|
|
380
|
+
return index < 0 ? null : menu.element.querySelector(`:scope > [data-menu-index="${index}"]`);
|
|
381
|
+
}
|
|
382
|
+
activeIndex(menu) {
|
|
383
|
+
const active = document.activeElement;
|
|
384
|
+
if (!(active instanceof HTMLElement) || active.parentElement !== menu.element)
|
|
385
|
+
return -1;
|
|
386
|
+
const index = Number(active.getAttribute("data-menu-index"));
|
|
387
|
+
return Number.isInteger(index) ? index : -1;
|
|
388
|
+
}
|
|
389
|
+
focusItem(menu, index) {
|
|
390
|
+
const row = this.rowAt(menu, index);
|
|
391
|
+
if (row)
|
|
392
|
+
row.focus({ preventScroll: false });
|
|
393
|
+
else
|
|
394
|
+
menu.element.focus({ preventScroll: true });
|
|
395
|
+
}
|
|
396
|
+
closeBelow(menu) {
|
|
397
|
+
const depth = this.stack.indexOf(menu);
|
|
398
|
+
for (const closing of this.stack.splice(depth + 1)) {
|
|
399
|
+
closing.opener?.setAttribute("aria-expanded", "false");
|
|
400
|
+
closing.element.remove();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
openSubmenu(menu, index, focusFirst) {
|
|
404
|
+
const item = menu.items[index];
|
|
405
|
+
const row = this.rowAt(menu, index);
|
|
406
|
+
if (!row || !hasSubmenu(item) || item.disabled)
|
|
407
|
+
return;
|
|
408
|
+
const below = this.stack[this.stack.indexOf(menu) + 1];
|
|
409
|
+
if (below && below.opener === row) {
|
|
410
|
+
if (focusFirst)
|
|
411
|
+
this.focusItem(below, firstIndex(below.items));
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
this.closeBelow(menu);
|
|
415
|
+
row.setAttribute("aria-expanded", "true");
|
|
416
|
+
const submenu = this.buildMenu(item.children, row);
|
|
417
|
+
const place = placeSubmenu(boxOf(row), submenu.element.getBoundingClientRect(), { width: window.innerWidth, height: window.innerHeight });
|
|
418
|
+
submenu.element.setAttribute("data-side", place.side);
|
|
419
|
+
this.position(submenu.element, place);
|
|
420
|
+
if (focusFirst)
|
|
421
|
+
this.focusItem(submenu, firstIndex(submenu.items));
|
|
422
|
+
}
|
|
423
|
+
choose(menu, index) {
|
|
424
|
+
const item = menu.items[index];
|
|
425
|
+
if (!isActionable(item))
|
|
426
|
+
return;
|
|
427
|
+
if (hasSubmenu(item)) {
|
|
428
|
+
this.openSubmenu(menu, index, true);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const { children: _children, path, id, ...plain } = item;
|
|
432
|
+
const detail = { item: { ...plain, id }, id, path: [...path], target: this.target };
|
|
433
|
+
this.close("select");
|
|
434
|
+
this.callbacks.onSelect?.(detail);
|
|
435
|
+
}
|
|
436
|
+
clearHover() {
|
|
437
|
+
if (this.hoverTimer)
|
|
438
|
+
clearTimeout(this.hoverTimer);
|
|
439
|
+
this.hoverTimer = null;
|
|
440
|
+
}
|
|
441
|
+
onContextMenu(event) {
|
|
442
|
+
if (this.disabled || this.layer.contains(event.target))
|
|
443
|
+
return;
|
|
444
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
445
|
+
const keyboard = event.button !== 2 && event.detail === 0 && event.clientX === 0 && event.clientY === 0;
|
|
446
|
+
const opened = keyboard && target ? this.open(target, null, { keyboard: true, target }) : this.open({ x: event.clientX, y: event.clientY }, null, { target });
|
|
447
|
+
if (opened)
|
|
448
|
+
event.preventDefault();
|
|
449
|
+
}
|
|
450
|
+
onHostKeyDown(event) {
|
|
451
|
+
if (this.disabled || this.isOpen || this.layer.contains(event.target))
|
|
452
|
+
return;
|
|
453
|
+
if (event.key !== "ContextMenu" && !(event.key === "F10" && event.shiftKey))
|
|
454
|
+
return;
|
|
455
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
456
|
+
if (target && this.open(target, null, { keyboard: true, target }))
|
|
457
|
+
event.preventDefault();
|
|
458
|
+
}
|
|
459
|
+
onMenuClick(event) {
|
|
460
|
+
const menu = this.menuOf(event.target);
|
|
461
|
+
const row = event.target instanceof Element ? event.target.closest(".stx-menu-item") : null;
|
|
462
|
+
if (!menu || !row)
|
|
463
|
+
return;
|
|
464
|
+
this.choose(menu, Number(row.getAttribute("data-menu-index")));
|
|
465
|
+
}
|
|
466
|
+
onMenuPointerMove(event) {
|
|
467
|
+
const menu = this.menuOf(event.target);
|
|
468
|
+
const row = event.target instanceof Element ? event.target.closest(".stx-menu-item") : null;
|
|
469
|
+
if (!menu || !row || document.activeElement === row)
|
|
470
|
+
return;
|
|
471
|
+
const index = Number(row.getAttribute("data-menu-index"));
|
|
472
|
+
row.focus({ preventScroll: true });
|
|
473
|
+
this.clearHover();
|
|
474
|
+
this.hoverTimer = setTimeout(() => {
|
|
475
|
+
if (document.activeElement !== row)
|
|
476
|
+
return;
|
|
477
|
+
if (hasSubmenu(menu.items[index]))
|
|
478
|
+
this.openSubmenu(menu, index, false);
|
|
479
|
+
else
|
|
480
|
+
this.closeBelow(menu);
|
|
481
|
+
}, SUBMENU_DELAY);
|
|
482
|
+
}
|
|
483
|
+
onMenuKeyDown(event) {
|
|
484
|
+
const menu = this.menuOf(event.target) ?? this.stack[this.stack.length - 1];
|
|
485
|
+
if (!menu)
|
|
486
|
+
return;
|
|
487
|
+
const index = this.activeIndex(menu);
|
|
488
|
+
const depth = this.stack.indexOf(menu);
|
|
489
|
+
let handled = true;
|
|
490
|
+
switch (event.key) {
|
|
491
|
+
case "ArrowDown":
|
|
492
|
+
this.focusItem(menu, stepIndex(menu.items, index, 1));
|
|
493
|
+
break;
|
|
494
|
+
case "ArrowUp":
|
|
495
|
+
this.focusItem(menu, stepIndex(menu.items, index, -1));
|
|
496
|
+
break;
|
|
497
|
+
case "Home":
|
|
498
|
+
case "PageUp":
|
|
499
|
+
this.focusItem(menu, firstIndex(menu.items));
|
|
500
|
+
break;
|
|
501
|
+
case "End":
|
|
502
|
+
case "PageDown":
|
|
503
|
+
this.focusItem(menu, lastIndex(menu.items));
|
|
504
|
+
break;
|
|
505
|
+
case "ArrowRight":
|
|
506
|
+
if (hasSubmenu(menu.items[index]))
|
|
507
|
+
this.openSubmenu(menu, index, true);
|
|
508
|
+
break;
|
|
509
|
+
case "ArrowLeft":
|
|
510
|
+
if (depth > 0)
|
|
511
|
+
this.closeSubmenu(menu);
|
|
512
|
+
break;
|
|
513
|
+
case "Escape":
|
|
514
|
+
if (depth > 0)
|
|
515
|
+
this.closeSubmenu(menu);
|
|
516
|
+
else
|
|
517
|
+
this.close("escape");
|
|
518
|
+
break;
|
|
519
|
+
case "Enter":
|
|
520
|
+
case " ":
|
|
521
|
+
if (index >= 0)
|
|
522
|
+
this.choose(menu, index);
|
|
523
|
+
break;
|
|
524
|
+
case "Tab":
|
|
525
|
+
this.close("tab");
|
|
526
|
+
handled = false;
|
|
527
|
+
break;
|
|
528
|
+
default:
|
|
529
|
+
handled = this.typeAhead(menu, index, event);
|
|
530
|
+
}
|
|
531
|
+
if (handled) {
|
|
532
|
+
event.preventDefault();
|
|
533
|
+
event.stopPropagation();
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
closeSubmenu(menu) {
|
|
537
|
+
const opener = menu.opener;
|
|
538
|
+
const parent = this.stack[this.stack.indexOf(menu) - 1];
|
|
539
|
+
if (parent)
|
|
540
|
+
this.closeBelow(parent);
|
|
541
|
+
opener?.focus({ preventScroll: true });
|
|
542
|
+
}
|
|
543
|
+
typeAhead(menu, index, event) {
|
|
544
|
+
if (event.key.length !== 1 || event.metaKey || event.ctrlKey || event.altKey)
|
|
545
|
+
return false;
|
|
546
|
+
const now = Date.now();
|
|
547
|
+
this.typed = now - this.typedAt > TYPE_AHEAD_RESET ? event.key : this.typed + event.key;
|
|
548
|
+
this.typedAt = now;
|
|
549
|
+
const found = typeAheadIndex(menu.items, index, this.typed);
|
|
550
|
+
if (found >= 0)
|
|
551
|
+
this.focusItem(menu, found);
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function boxOf(element) {
|
|
556
|
+
const rect = element.getBoundingClientRect();
|
|
557
|
+
return { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom };
|
|
558
|
+
}
|
|
559
|
+
export {
|
|
560
|
+
ContextMenuView,
|
|
561
|
+
ariaShortcut,
|
|
562
|
+
findItem,
|
|
563
|
+
firstIndex,
|
|
564
|
+
formatShortcut,
|
|
565
|
+
hasSubmenu,
|
|
566
|
+
isActionable,
|
|
567
|
+
isFocusable,
|
|
568
|
+
lastIndex,
|
|
569
|
+
placeAtBox,
|
|
570
|
+
placeAtPoint,
|
|
571
|
+
placeSubmenu,
|
|
572
|
+
resolveItems,
|
|
573
|
+
stepIndex,
|
|
574
|
+
typeAheadIndex
|
|
575
|
+
};
|