@yaks/ux 0.0.0-stage → 0.2.36
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/Answer.js +43 -0
- package/Answer.js.map +1 -0
- package/Answer.ts +84 -0
- package/Conversation.js +520 -0
- package/Conversation.js.map +1 -0
- package/Conversation.ts +649 -0
- package/ConversationBox.js +106 -0
- package/ConversationBox.js.map +1 -0
- package/ConversationBox.ts +198 -0
- package/ConversationLines.js +29 -0
- package/ConversationLines.js.map +1 -0
- package/ConversationLines.ts +48 -0
- package/Disclosure.js +79 -0
- package/Disclosure.js.map +1 -0
- package/Disclosure.ts +130 -0
- package/Edit.js +330 -0
- package/Edit.js.map +1 -0
- package/Edit.ts +488 -0
- package/EntityPopup.js +35 -0
- package/EntityPopup.js.map +1 -0
- package/EntityPopup.ts +52 -0
- package/Fields.js +161 -0
- package/Fields.js.map +1 -0
- package/Fields.ts +276 -0
- package/LICENSE +202 -0
- package/Order.js +105 -0
- package/Order.js.map +1 -0
- package/Order.ts +159 -0
- package/README.md +1023 -2
- package/Save.js +50 -0
- package/Save.js.map +1 -0
- package/Save.ts +115 -0
- package/Sort.js +49 -0
- package/Sort.js.map +1 -0
- package/Sort.ts +98 -0
- package/Submit.js +115 -0
- package/Submit.js.map +1 -0
- package/Submit.ts +174 -0
- package/Text.js +114 -0
- package/Text.js.map +1 -0
- package/Text.ts +140 -0
- package/TokenPicker.js +65 -0
- package/TokenPicker.js.map +1 -0
- package/TokenPicker.ts +117 -0
- package/_dist/Answer.d.ts +39 -0
- package/_dist/Answer.d.ts.map +1 -0
- package/_dist/Conversation.d.ts +131 -0
- package/_dist/Conversation.d.ts.map +1 -0
- package/_dist/ConversationBox.d.ts +29 -0
- package/_dist/ConversationBox.d.ts.map +1 -0
- package/_dist/ConversationLines.d.ts +13 -0
- package/_dist/ConversationLines.d.ts.map +1 -0
- package/_dist/Disclosure.d.ts +36 -0
- package/_dist/Disclosure.d.ts.map +1 -0
- package/_dist/Edit.d.ts +87 -0
- package/_dist/Edit.d.ts.map +1 -0
- package/_dist/EntityPopup.d.ts +12 -0
- package/_dist/EntityPopup.d.ts.map +1 -0
- package/_dist/Fields.d.ts +29 -0
- package/_dist/Fields.d.ts.map +1 -0
- package/_dist/Order.d.ts +40 -0
- package/_dist/Order.d.ts.map +1 -0
- package/_dist/Save.d.ts +28 -0
- package/_dist/Save.d.ts.map +1 -0
- package/_dist/Sort.d.ts +28 -0
- package/_dist/Sort.d.ts.map +1 -0
- package/_dist/Submit.d.ts +48 -0
- package/_dist/Submit.d.ts.map +1 -0
- package/_dist/Text.d.ts +27 -0
- package/_dist/Text.d.ts.map +1 -0
- package/_dist/TokenPicker.d.ts +19 -0
- package/_dist/TokenPicker.d.ts.map +1 -0
- package/_dist/anchor.d.ts +52 -0
- package/_dist/anchor.d.ts.map +1 -0
- package/_dist/anchored.d.ts +30 -0
- package/_dist/anchored.d.ts.map +1 -0
- package/_dist/completion-state.d.ts +65 -0
- package/_dist/completion-state.d.ts.map +1 -0
- package/_dist/completion.d.ts +73 -0
- package/_dist/completion.d.ts.map +1 -0
- package/_dist/composer-browser.d.ts +1 -0
- package/_dist/composer-browser.d.ts.map +1 -0
- package/_dist/composer.d.ts +31 -0
- package/_dist/composer.d.ts.map +1 -0
- package/_dist/context-menu-browser.d.ts +4 -0
- package/_dist/context-menu-browser.d.ts.map +1 -0
- package/_dist/context-menu.d.ts +95 -0
- package/_dist/context-menu.d.ts.map +1 -0
- package/_dist/drag.d.ts +29 -0
- package/_dist/drag.d.ts.map +1 -0
- package/_dist/edit-keys.d.ts +42 -0
- package/_dist/edit-keys.d.ts.map +1 -0
- package/_dist/emit.d.ts +7 -0
- package/_dist/emit.d.ts.map +1 -0
- package/_dist/entity-popup-browser.d.ts +3 -0
- package/_dist/entity-popup-browser.d.ts.map +1 -0
- package/_dist/face/FaceTagger.d.ts +25 -0
- package/_dist/face/FaceTagger.d.ts.map +1 -0
- package/_dist/face/review.d.ts +5 -0
- package/_dist/face/review.d.ts.map +1 -0
- package/_dist/face/tags.d.ts +99 -0
- package/_dist/face/tags.d.ts.map +1 -0
- package/_dist/field-values.d.ts +4 -0
- package/_dist/field-values.d.ts.map +1 -0
- package/_dist/fit-list.d.ts +4 -0
- package/_dist/fit-list.d.ts.map +1 -0
- package/_dist/hits.d.ts +27 -0
- package/_dist/hits.d.ts.map +1 -0
- package/_dist/host.d.ts +91 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/image-fallback.d.ts +5 -0
- package/_dist/image-fallback.d.ts.map +1 -0
- package/_dist/keys.d.ts +95 -0
- package/_dist/keys.d.ts.map +1 -0
- package/_dist/kit.d.ts +23 -0
- package/_dist/kit.d.ts.map +1 -0
- package/_dist/lightbox-browser.d.ts +2 -0
- package/_dist/lightbox-browser.d.ts.map +1 -0
- package/_dist/lightbox.d.ts +42 -0
- package/_dist/lightbox.d.ts.map +1 -0
- package/_dist/live.d.ts +21 -0
- package/_dist/live.d.ts.map +1 -0
- package/_dist/mod.d.ts +67 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +12 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/resize.d.ts +43 -0
- package/_dist/resize.d.ts.map +1 -0
- package/_dist/rows.d.ts +31 -0
- package/_dist/rows.d.ts.map +1 -0
- package/_dist/save-browser.d.ts +29 -0
- package/_dist/save-browser.d.ts.map +1 -0
- package/_dist/select-browser.d.ts +1 -0
- package/_dist/select-browser.d.ts.map +1 -0
- package/_dist/select.d.ts +12 -0
- package/_dist/select.d.ts.map +1 -0
- package/_dist/sending.d.ts +10 -0
- package/_dist/sending.d.ts.map +1 -0
- package/_dist/sort-browser.d.ts +11 -0
- package/_dist/sort-browser.d.ts.map +1 -0
- package/_dist/state.d.ts +74 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/suggest.d.ts +18 -0
- package/_dist/suggest.d.ts.map +1 -0
- package/_dist/swipe.d.ts +60 -0
- package/_dist/swipe.d.ts.map +1 -0
- package/_dist/ui.d.ts +6 -0
- package/_dist/ui.d.ts.map +1 -0
- package/_dist/upload-browser.d.ts +13 -0
- package/_dist/upload-browser.d.ts.map +1 -0
- package/_dist/views.d.ts +8 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/anchor.js +58 -0
- package/anchor.js.map +1 -0
- package/anchor.ts +87 -0
- package/anchored.js +180 -0
- package/anchored.js.map +1 -0
- package/anchored.ts +232 -0
- package/completion-state.js +76 -0
- package/completion-state.js.map +1 -0
- package/completion-state.ts +120 -0
- package/completion.js +301 -0
- package/completion.js.map +1 -0
- package/completion.ts +433 -0
- package/composer-browser.js +4 -0
- package/composer-browser.js.map +1 -0
- package/composer-browser.ts +5 -0
- package/composer.js +42 -0
- package/composer.js.map +1 -0
- package/composer.ts +53 -0
- package/context-menu-browser.js +221 -0
- package/context-menu-browser.js.map +1 -0
- package/context-menu-browser.ts +231 -0
- package/context-menu.js +146 -0
- package/context-menu.js.map +1 -0
- package/context-menu.ts +194 -0
- package/deno.json +1 -0
- package/drag.js +33 -0
- package/drag.js.map +1 -0
- package/drag.ts +41 -0
- package/edit-keys.js +51 -0
- package/edit-keys.js.map +1 -0
- package/edit-keys.ts +81 -0
- package/emit.js +34 -0
- package/emit.js.map +1 -0
- package/emit.ts +55 -0
- package/entity-popup-browser.js +94 -0
- package/entity-popup-browser.js.map +1 -0
- package/entity-popup-browser.ts +100 -0
- package/face/FaceTagger.js +361 -0
- package/face/FaceTagger.js.map +1 -0
- package/face/FaceTagger.ts +507 -0
- package/face/NameProposal.js +61 -0
- package/face/NameProposal.js.map +1 -0
- package/face/NameProposal.ts +100 -0
- package/face/review.js +33 -0
- package/face/review.js.map +1 -0
- package/face/review.ts +49 -0
- package/face/tags.js +344 -0
- package/face/tags.js.map +1 -0
- package/face/tags.ts +357 -0
- package/field-values.js +20 -0
- package/field-values.js.map +1 -0
- package/field-values.ts +35 -0
- package/fit-list.js +66 -0
- package/fit-list.js.map +1 -0
- package/fit-list.ts +81 -0
- package/hits.js +65 -0
- package/hits.js.map +1 -0
- package/hits.ts +75 -0
- package/host.js +88 -0
- package/host.js.map +1 -0
- package/host.ts +212 -0
- package/image-fallback.js +32 -0
- package/image-fallback.js.map +1 -0
- package/image-fallback.ts +37 -0
- package/keys.js +418 -0
- package/keys.js.map +1 -0
- package/keys.ts +444 -0
- package/kit.js +41 -0
- package/kit.js.map +1 -0
- package/kit.ts +71 -0
- package/lightbox-browser.js +248 -0
- package/lightbox-browser.js.map +1 -0
- package/lightbox-browser.ts +232 -0
- package/lightbox.js +84 -0
- package/lightbox.js.map +1 -0
- package/lightbox.ts +103 -0
- package/live.js +111 -0
- package/live.js.map +1 -0
- package/live.ts +139 -0
- package/mod.js +67 -0
- package/mod.js.map +1 -0
- package/mod.ts +180 -0
- package/package.json +143 -4
- package/read.js +33 -0
- package/read.js.map +1 -0
- package/read.ts +56 -0
- package/resize.js +159 -0
- package/resize.js.map +1 -0
- package/resize.ts +203 -0
- package/rows.js +73 -0
- package/rows.js.map +1 -0
- package/rows.ts +97 -0
- package/save-browser.js +469 -0
- package/save-browser.js.map +1 -0
- package/save-browser.ts +530 -0
- package/select-browser.js +4 -0
- package/select-browser.js.map +1 -0
- package/select-browser.ts +5 -0
- package/select.js +296 -0
- package/select.js.map +1 -0
- package/select.ts +343 -0
- package/sending.js +36 -0
- package/sending.js.map +1 -0
- package/sending.ts +44 -0
- package/sort-browser.js +51 -0
- package/sort-browser.js.map +1 -0
- package/sort-browser.ts +70 -0
- package/state.js +99 -0
- package/state.js.map +1 -0
- package/state.ts +121 -0
- package/suggest.js +28 -0
- package/suggest.js.map +1 -0
- package/suggest.ts +32 -0
- package/swipe.js +186 -0
- package/swipe.js.map +1 -0
- package/swipe.ts +250 -0
- package/ui.js +507 -0
- package/ui.js.map +1 -0
- package/ui.ts +378 -0
- package/upload-browser.js +195 -0
- package/upload-browser.js.map +1 -0
- package/upload-browser.ts +303 -0
- package/upload-files.js +41 -0
- package/upload-files.js.map +1 -0
- package/upload-files.ts +49 -0
- package/views.js +8 -0
- package/views.js.map +1 -0
- package/views.ts +9 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +287 -0
- package/vocab.ts +17 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binds every item's menu on the page, and on those later pages swap in
|
|
3
|
+
* (./context-menu.ts): a right-click on an item, a finger held on it, or the
|
|
4
|
+
* menu key or Shift+F10 while it holds the focus opens its menu, at the
|
|
5
|
+
* pointer or under the item; its ⋯ opens it under the ⋯. The arrow keys, Home
|
|
6
|
+
* and End walk the open menu's items; Escape, Tab, a press elsewhere or a
|
|
7
|
+
* press on an item closes it, and the focus goes back to what opened it. An
|
|
8
|
+
* item that copies or shares does so here.
|
|
9
|
+
*
|
|
10
|
+
* A right-click on a field, or on an item with nothing to offer, is left to
|
|
11
|
+
* the browser.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/ import { placeAt } from "@yaks/ui/placement";
|
|
15
|
+
import { HOLD, ITEM, moved, opensMenu, SLOP } from "./context-menu.js";
|
|
16
|
+
// The items of an open menu a person can press.
|
|
17
|
+
let items = (menu)=>[
|
|
18
|
+
...menu.querySelectorAll('[role="menuitem"]:not([disabled]):not([hidden])')
|
|
19
|
+
];
|
|
20
|
+
let typing = (el)=>el.closest('input, textarea, select, [contenteditable]');
|
|
21
|
+
// The element `sel` names at or around what an event happened on.
|
|
22
|
+
let near = (target, sel)=>{
|
|
23
|
+
let el = target;
|
|
24
|
+
return el?.closest ? el.closest(sel) : null;
|
|
25
|
+
};
|
|
26
|
+
// What can hold the focus: the item, else the first thing in it that can,
|
|
27
|
+
// such as its link or the summary of its fold, which the focus goes back to when its menu closes.
|
|
28
|
+
let FOCUSABLE = 'a[href], button:not([disabled]), summary, input, select, textarea, [tabindex]:not([tabindex="-1"])';
|
|
29
|
+
let focusable = (item)=>item.matches(FOCUSABLE) ? item : item.querySelector(FOCUSABLE) ?? item;
|
|
30
|
+
let open = (menu)=>menu.matches(':popover-open');
|
|
31
|
+
// Hand `url` to the share sheet, or put it on the clipboard where there is
|
|
32
|
+
// none (a desktop browser) or the sheet was refused.
|
|
33
|
+
let share = async (url)=>{
|
|
34
|
+
if (navigator.share) {
|
|
35
|
+
try {
|
|
36
|
+
return await navigator.share({
|
|
37
|
+
url
|
|
38
|
+
});
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (e.name == 'AbortError') return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
await navigator.clipboard?.writeText(url);
|
|
44
|
+
};
|
|
45
|
+
let timer = (ms, then)=>{
|
|
46
|
+
let at = setTimeout(then, ms);
|
|
47
|
+
return ()=>clearTimeout(at);
|
|
48
|
+
};
|
|
49
|
+
/** Follow every item's menu in `doc`, now and as its pages are drawn anew;
|
|
50
|
+
* `after` keeps time for a long press. */ export let contextMenus = (doc, after = timer)=>{
|
|
51
|
+
// The open menu, what gets the focus back when it closes, and what held the
|
|
52
|
+
// focus before it opened, which the browser gives it back to on its own.
|
|
53
|
+
let shown;
|
|
54
|
+
let held;
|
|
55
|
+
// A long press ends in a click the menu must not see as a press on the item.
|
|
56
|
+
let swallow = false;
|
|
57
|
+
// A link made whole against the page: a path, the page's own address's.
|
|
58
|
+
let whole = (link)=>new URL(link, doc.URL).href;
|
|
59
|
+
let menuFor = (item)=>doc.getElementById(item.getAttribute(ITEM) ?? '');
|
|
60
|
+
let show = (menu, back, at)=>{
|
|
61
|
+
letGo();
|
|
62
|
+
if (!menu.showPopover) return;
|
|
63
|
+
shown = {
|
|
64
|
+
menu,
|
|
65
|
+
back,
|
|
66
|
+
prior: doc.activeElement
|
|
67
|
+
};
|
|
68
|
+
if (!open(menu)) menu.showPopover();
|
|
69
|
+
placeAt(menu, at.x, at.y, at.align);
|
|
70
|
+
for (let more of doc.querySelectorAll(`[popovertarget="${menu.id}"]`)){
|
|
71
|
+
more.setAttribute('aria-expanded', 'true');
|
|
72
|
+
}
|
|
73
|
+
items(menu)[0]?.focus({
|
|
74
|
+
preventScroll: true
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
let atItem = (item)=>{
|
|
78
|
+
let box = item.getBoundingClientRect();
|
|
79
|
+
return {
|
|
80
|
+
x: box.left + Math.min(24, box.width / 2),
|
|
81
|
+
y: box.bottom
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
// The item under `target` and its menu, where it has one to offer.
|
|
85
|
+
let found = (target)=>{
|
|
86
|
+
let el = target;
|
|
87
|
+
if (!el?.closest || el.closest('[role="menu"]') || typing(el)) return;
|
|
88
|
+
let item = el.closest(`[${ITEM}]`);
|
|
89
|
+
let menu = item && menuFor(item);
|
|
90
|
+
return item && menu && items(menu).length ? {
|
|
91
|
+
item,
|
|
92
|
+
menu
|
|
93
|
+
} : undefined;
|
|
94
|
+
};
|
|
95
|
+
let letGo = ()=>{
|
|
96
|
+
held?.cancel();
|
|
97
|
+
held = undefined;
|
|
98
|
+
};
|
|
99
|
+
doc.addEventListener('contextmenu', (e)=>{
|
|
100
|
+
// Within a menu of ours, the browser's own would cover it.
|
|
101
|
+
if (near(e.target, '.Menu[role="menu"]')) {
|
|
102
|
+
return e.preventDefault();
|
|
103
|
+
}
|
|
104
|
+
let at = found(e.target);
|
|
105
|
+
if (!at) return;
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
// The browser's own answer to a long press or a menu key, after ours.
|
|
108
|
+
if (shown?.menu == at.menu) return;
|
|
109
|
+
let keyed = !e.clientX && !e.clientY;
|
|
110
|
+
show(at.menu, at.item.contains(doc.activeElement) && doc.activeElement != doc.body ? doc.activeElement : focusable(at.item), keyed ? atItem(at.item) : {
|
|
111
|
+
x: e.clientX,
|
|
112
|
+
y: e.clientY
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
doc.addEventListener('pointerdown', (e)=>{
|
|
116
|
+
letGo();
|
|
117
|
+
swallow = false;
|
|
118
|
+
if (e.pointerType != 'touch') return;
|
|
119
|
+
let at = found(e.target);
|
|
120
|
+
if (!at) return;
|
|
121
|
+
let { clientX: x, clientY: y } = e;
|
|
122
|
+
held = {
|
|
123
|
+
x,
|
|
124
|
+
y,
|
|
125
|
+
cancel: after(HOLD, ()=>{
|
|
126
|
+
swallow = true;
|
|
127
|
+
show(at.menu, focusable(at.item), {
|
|
128
|
+
x,
|
|
129
|
+
y
|
|
130
|
+
});
|
|
131
|
+
})
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
doc.addEventListener('pointermove', (e)=>{
|
|
135
|
+
if (held && Math.hypot(e.clientX - held.x, e.clientY - held.y) > SLOP) {
|
|
136
|
+
letGo();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
for (let end of [
|
|
140
|
+
'pointerup',
|
|
141
|
+
'pointercancel',
|
|
142
|
+
'scroll'
|
|
143
|
+
]){
|
|
144
|
+
doc.addEventListener(end, letGo, {
|
|
145
|
+
capture: true,
|
|
146
|
+
passive: true
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
doc.addEventListener('click', (e)=>{
|
|
150
|
+
if (swallow) {
|
|
151
|
+
swallow = false;
|
|
152
|
+
if (!near(e.target, '[role="menu"]')) {
|
|
153
|
+
e.preventDefault();
|
|
154
|
+
e.stopPropagation();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
let pressed = near(e.target, '[role="menuitem"]');
|
|
159
|
+
let menu = pressed?.closest('[role="menu"]');
|
|
160
|
+
if (!pressed || !menu) return;
|
|
161
|
+
let { copy, link: copied, share: link } = pressed.dataset;
|
|
162
|
+
if (copy != null) void navigator.clipboard?.writeText(copy);
|
|
163
|
+
if (copied != null) void navigator.clipboard?.writeText(whole(copied));
|
|
164
|
+
if (link != null) void share(whole(link));
|
|
165
|
+
// The menu closes, and a dialog it opens takes its place: opened by the
|
|
166
|
+
// press itself, the dialog would hold the menu open under it, its
|
|
167
|
+
// invoker's popover.
|
|
168
|
+
let dialog = pressed.getAttribute('popovertarget');
|
|
169
|
+
if (dialog) e.preventDefault();
|
|
170
|
+
if (open(menu)) menu.hidePopover();
|
|
171
|
+
if (dialog) doc.getElementById(dialog)?.showPopover();
|
|
172
|
+
}, true);
|
|
173
|
+
doc.addEventListener('keydown', (e)=>{
|
|
174
|
+
let menu = near(e.target, '[role="menu"]');
|
|
175
|
+
if (menu && open(menu)) {
|
|
176
|
+
if (e.key == 'Tab') return menu.hidePopover();
|
|
177
|
+
let all = items(menu);
|
|
178
|
+
let to = moved(e.key, all.indexOf(e.target), all.length);
|
|
179
|
+
if (to == undefined) return;
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
all[to].focus();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (!opensMenu(e)) return;
|
|
185
|
+
let at = found(e.target);
|
|
186
|
+
if (!at) return;
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
show(at.menu, e.target, atItem(at.item));
|
|
189
|
+
});
|
|
190
|
+
// A ⋯ opens its menu as the browser shows it: under the ⋯, from its right.
|
|
191
|
+
// A menu closing gives the focus back to what opened it.
|
|
192
|
+
doc.addEventListener('toggle', (e)=>{
|
|
193
|
+
let menu = e.target;
|
|
194
|
+
if (!menu.matches?.('.Menu[role="menu"]')) return;
|
|
195
|
+
let mores = doc.querySelectorAll(`[popovertarget="${menu.id}"]`);
|
|
196
|
+
if (e.newState == 'open') {
|
|
197
|
+
if (shown?.menu == menu) return;
|
|
198
|
+
let more = [
|
|
199
|
+
...mores
|
|
200
|
+
].find((b)=>b == doc.activeElement) ?? mores[0];
|
|
201
|
+
if (!more) return;
|
|
202
|
+
let box = more.getBoundingClientRect();
|
|
203
|
+
show(menu, more, {
|
|
204
|
+
x: box.right,
|
|
205
|
+
y: box.bottom + 4,
|
|
206
|
+
align: 'right'
|
|
207
|
+
});
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
for (let more of mores)more.setAttribute('aria-expanded', 'false');
|
|
211
|
+
if (shown?.menu != menu) return;
|
|
212
|
+
let { back, prior } = shown;
|
|
213
|
+
shown = undefined;
|
|
214
|
+
let at = doc.activeElement;
|
|
215
|
+
let lost = !at || at == doc.body || at == prior || menu.contains(at);
|
|
216
|
+
if (lost && back?.isConnected) back.focus({
|
|
217
|
+
preventScroll: true
|
|
218
|
+
});
|
|
219
|
+
}, true);
|
|
220
|
+
};
|
|
221
|
+
//# sourceMappingURL=context-menu-browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-menu-browser.js","sources":["./context-menu-browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;CAaC,GACD,SAAS,OAAO,kCAAuC;AACvD,SAAS,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,4BAA2B;AAEtE,gDAAgD;AAChD,IAAI,QAAQ,CAAC,OAAsB;OAC9B,KAAK,gBAAgB,CACtB;GAEH;AAED,IAAI,SAAS,CAAC,KACZ,GAAG,OAAO,CAAC;AAEb,kEAAkE;AAClE,IAAI,OAAO,CAAC,QAA4B;EACtC,IAAI,KAAK;EACT,OAAQ,IAAI,UAAU,GAAG,OAAO,CAAC,OAAO;AAC1C;AAEA,0EAA0E;AAC1E,kGAAkG;AAClG,IAAI,YACF;AACF,IAAI,YAAY,CAAC,OACf,KAAK,OAAO,CAAC,aACT,OACA,KAAK,aAAa,CAAc,cAAc;AAEpD,IAAI,OAAO,CAAC,OAAsB,KAAK,OAAO,CAAC;AAE/C,2EAA2E;AAC3E,qDAAqD;AACrD,IAAI,QAAQ,OAAO;EACjB,IAAI,UAAU,KAAK,EAAE;IACnB,IAAI;MACF,OAAO,MAAM,UAAU,KAAK,CAAC;QAAE;MAAI;IACrC,EAAE,OAAO,GAAG;MACV,IAAI,AAAC,EAAmB,IAAI,IAAI,cAAc;IAChD;EACF;EACA,MAAM,UAAU,SAAS,EAAE,UAAU;AACvC;AAKA,IAAI,QA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ts): a right-click on an item, a finger held on it, or the
|
|
4
|
+
* menu key or Shift+F10 while it holds the focus opens its menu, at the
|
|
5
|
+
* pointer or under the item; its ⋯ opens it under the ⋯. The arrow keys, Home
|
|
6
|
+
* and End walk the open menu's items; Escape, Tab, a press elsewhere or a
|
|
7
|
+
* press on an item closes it, and the focus goes back to what opened it. An
|
|
8
|
+
* item that copies or shares does so here.
|
|
9
|
+
*
|
|
10
|
+
* A right-click on a field, or on an item with nothing to offer, is left to
|
|
11
|
+
* the browser.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import { placeAt } from '@yaks/ui/placement'
|
|
16
|
+
import { HOLD, ITEM, moved, opensMenu, SLOP } from './context-menu.js'
|
|
17
|
+
|
|
18
|
+
// The items of an open menu a person can press.
|
|
19
|
+
let items = (menu: HTMLElement) => [
|
|
20
|
+
...menu.querySelectorAll<HTMLElement>(
|
|
21
|
+
'[role="menuitem"]:not([disabled]):not([hidden])',
|
|
22
|
+
),
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
let typing = (el: Element) =>
|
|
26
|
+
el.closest('input, textarea, select, [contenteditable]')
|
|
27
|
+
|
|
28
|
+
// The element `sel` names at or around what an event happened on.
|
|
29
|
+
let near = (target: EventTarget | null, sel: string) => {
|
|
30
|
+
let el = target as Element | null
|
|
31
|
+
return (el?.closest ? el.closest(sel) : null) as HTMLElement | null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// What can hold the focus: the item, else the first thing in it that can,
|
|
35
|
+
// such as its link or the summary of its fold, which the focus goes back to when its menu closes.
|
|
36
|
+
let FOCUSABLE =
|
|
37
|
+
'a[href], button:not([disabled]), summary, input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
38
|
+
let focusable = (item: HTMLElement) =>
|
|
39
|
+
item.matches(FOCUSABLE)
|
|
40
|
+
? item
|
|
41
|
+
: item.querySelector<HTMLElement>(FOCUSABLE) ?? item
|
|
42
|
+
|
|
43
|
+
let open = (menu: HTMLElement) => menu.matches(':popover-open')
|
|
44
|
+
|
|
45
|
+
// Hand `url` to the share sheet, or put it on the clipboard where there is
|
|
46
|
+
// none (a desktop browser) or the sheet was refused.
|
|
47
|
+
let share = async (url: string) => {
|
|
48
|
+
if (navigator.share) {
|
|
49
|
+
try {
|
|
50
|
+
return await navigator.share({ url })
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if ((e as DOMException).name == 'AbortError') return
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
await navigator.clipboard?.writeText(url)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Run `then` in `ms`; the answer cancels it. */
|
|
59
|
+
export type After = (ms: number, then: () => void) => () => void
|
|
60
|
+
|
|
61
|
+
let timer: After = (ms, then) => {
|
|
62
|
+
let at = setTimeout(then, ms)
|
|
63
|
+
return () => clearTimeout(at)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Follow every item's menu in `doc`, now and as its pages are drawn anew;
|
|
67
|
+
* `after` keeps time for a long press. */
|
|
68
|
+
export let contextMenus = (doc: Document, after: After = timer): void => {
|
|
69
|
+
// The open menu, what gets the focus back when it closes, and what held the
|
|
70
|
+
// focus before it opened, which the browser gives it back to on its own.
|
|
71
|
+
let shown:
|
|
72
|
+
| { menu: HTMLElement; back: HTMLElement | null; prior: Element | null }
|
|
73
|
+
| undefined
|
|
74
|
+
let held: { cancel: () => void; x: number; y: number } | undefined
|
|
75
|
+
// A long press ends in a click the menu must not see as a press on the item.
|
|
76
|
+
let swallow = false
|
|
77
|
+
|
|
78
|
+
// A link made whole against the page: a path, the page's own address's.
|
|
79
|
+
let whole = (link: string) => new URL(link, doc.URL).href
|
|
80
|
+
|
|
81
|
+
let menuFor = (item: Element) =>
|
|
82
|
+
doc.getElementById(item.getAttribute(ITEM) ?? '')
|
|
83
|
+
|
|
84
|
+
let show = (
|
|
85
|
+
menu: HTMLElement,
|
|
86
|
+
back: HTMLElement | null,
|
|
87
|
+
at: { x: number; y: number; align?: 'right' },
|
|
88
|
+
) => {
|
|
89
|
+
letGo()
|
|
90
|
+
if (!menu.showPopover) return
|
|
91
|
+
shown = { menu, back, prior: doc.activeElement }
|
|
92
|
+
if (!open(menu)) menu.showPopover()
|
|
93
|
+
placeAt(menu, at.x, at.y, at.align)
|
|
94
|
+
for (let more of doc.querySelectorAll(`[popovertarget="${menu.id}"]`)) {
|
|
95
|
+
more.setAttribute('aria-expanded', 'true')
|
|
96
|
+
}
|
|
97
|
+
items(menu)[0]?.focus({ preventScroll: true })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let atItem = (item: Element) => {
|
|
101
|
+
let box = item.getBoundingClientRect()
|
|
102
|
+
return { x: box.left + Math.min(24, box.width / 2), y: box.bottom }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The item under `target` and its menu, where it has one to offer.
|
|
106
|
+
let found = (target: EventTarget | null) => {
|
|
107
|
+
let el = target as Element | null
|
|
108
|
+
if (!el?.closest || el.closest('[role="menu"]') || typing(el)) return
|
|
109
|
+
let item = el.closest<HTMLElement>(`[${ITEM}]`)
|
|
110
|
+
let menu = item && menuFor(item)
|
|
111
|
+
return item && menu && items(menu).length ? { item, menu } : undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let letGo = () => {
|
|
115
|
+
held?.cancel()
|
|
116
|
+
held = undefined
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
doc.addEventListener('contextmenu', (e) => {
|
|
120
|
+
// Within a menu of ours, the browser's own would cover it.
|
|
121
|
+
if (near(e.target, '.Menu[role="menu"]')) {
|
|
122
|
+
return e.preventDefault()
|
|
123
|
+
}
|
|
124
|
+
let at = found(e.target)
|
|
125
|
+
if (!at) return
|
|
126
|
+
e.preventDefault()
|
|
127
|
+
// The browser's own answer to a long press or a menu key, after ours.
|
|
128
|
+
if (shown?.menu == at.menu) return
|
|
129
|
+
let keyed = !e.clientX && !e.clientY
|
|
130
|
+
show(
|
|
131
|
+
at.menu,
|
|
132
|
+
at.item.contains(doc.activeElement) && doc.activeElement != doc.body
|
|
133
|
+
? doc.activeElement as HTMLElement
|
|
134
|
+
: focusable(at.item),
|
|
135
|
+
keyed ? atItem(at.item) : { x: e.clientX, y: e.clientY },
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
doc.addEventListener('pointerdown', (e) => {
|
|
140
|
+
letGo()
|
|
141
|
+
swallow = false
|
|
142
|
+
if (e.pointerType != 'touch') return
|
|
143
|
+
let at = found(e.target)
|
|
144
|
+
if (!at) return
|
|
145
|
+
let { clientX: x, clientY: y } = e
|
|
146
|
+
held = {
|
|
147
|
+
x,
|
|
148
|
+
y,
|
|
149
|
+
cancel: after(HOLD, () => {
|
|
150
|
+
swallow = true
|
|
151
|
+
show(at.menu, focusable(at.item), { x, y })
|
|
152
|
+
}),
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
doc.addEventListener('pointermove', (e) => {
|
|
156
|
+
if (held && Math.hypot(e.clientX - held.x, e.clientY - held.y) > SLOP) {
|
|
157
|
+
letGo()
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
for (let end of ['pointerup', 'pointercancel', 'scroll']) {
|
|
161
|
+
doc.addEventListener(end, letGo, { capture: true, passive: true })
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
doc.addEventListener('click', (e) => {
|
|
165
|
+
if (swallow) {
|
|
166
|
+
swallow = false
|
|
167
|
+
if (!near(e.target, '[role="menu"]')) {
|
|
168
|
+
e.preventDefault()
|
|
169
|
+
e.stopPropagation()
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
let pressed = near(e.target, '[role="menuitem"]')
|
|
174
|
+
let menu = pressed?.closest<HTMLElement>('[role="menu"]')
|
|
175
|
+
if (!pressed || !menu) return
|
|
176
|
+
let { copy, link: copied, share: link } = pressed.dataset
|
|
177
|
+
if (copy != null) void navigator.clipboard?.writeText(copy)
|
|
178
|
+
if (copied != null) void navigator.clipboard?.writeText(whole(copied))
|
|
179
|
+
if (link != null) void share(whole(link))
|
|
180
|
+
// The menu closes, and a dialog it opens takes its place: opened by the
|
|
181
|
+
// press itself, the dialog would hold the menu open under it, its
|
|
182
|
+
// invoker's popover.
|
|
183
|
+
let dialog = pressed.getAttribute('popovertarget')
|
|
184
|
+
if (dialog) e.preventDefault()
|
|
185
|
+
if (open(menu)) menu.hidePopover()
|
|
186
|
+
if (dialog) doc.getElementById(dialog)?.showPopover()
|
|
187
|
+
}, true)
|
|
188
|
+
|
|
189
|
+
doc.addEventListener('keydown', (e) => {
|
|
190
|
+
let menu = near(e.target, '[role="menu"]')
|
|
191
|
+
if (menu && open(menu)) {
|
|
192
|
+
if (e.key == 'Tab') return menu.hidePopover()
|
|
193
|
+
let all = items(menu)
|
|
194
|
+
let to = moved(e.key, all.indexOf(e.target as HTMLElement), all.length)
|
|
195
|
+
if (to == undefined) return
|
|
196
|
+
e.preventDefault()
|
|
197
|
+
all[to].focus()
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
if (!opensMenu(e)) return
|
|
201
|
+
let at = found(e.target)
|
|
202
|
+
if (!at) return
|
|
203
|
+
e.preventDefault()
|
|
204
|
+
show(at.menu, e.target as HTMLElement, atItem(at.item))
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
// A ⋯ opens its menu as the browser shows it: under the ⋯, from its right.
|
|
208
|
+
// A menu closing gives the focus back to what opened it.
|
|
209
|
+
doc.addEventListener('toggle', (e) => {
|
|
210
|
+
let menu = e.target as HTMLElement
|
|
211
|
+
if (!menu.matches?.('.Menu[role="menu"]')) return
|
|
212
|
+
let mores = doc.querySelectorAll<HTMLElement>(
|
|
213
|
+
`[popovertarget="${menu.id}"]`,
|
|
214
|
+
)
|
|
215
|
+
if ((e as ToggleEvent).newState == 'open') {
|
|
216
|
+
if (shown?.menu == menu) return
|
|
217
|
+
let more = [...mores].find((b) => b == doc.activeElement) ?? mores[0]
|
|
218
|
+
if (!more) return
|
|
219
|
+
let box = more.getBoundingClientRect()
|
|
220
|
+
show(menu, more, { x: box.right, y: box.bottom + 4, align: 'right' })
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
for (let more of mores) more.setAttribute('aria-expanded', 'false')
|
|
224
|
+
if (shown?.menu != menu) return
|
|
225
|
+
let { back, prior } = shown
|
|
226
|
+
shown = undefined
|
|
227
|
+
let at = doc.activeElement
|
|
228
|
+
let lost = !at || at == doc.body || at == prior || menu.contains(at)
|
|
229
|
+
if (lost && back?.isConnected) back.focus({ preventScroll: true })
|
|
230
|
+
}, true)
|
|
231
|
+
}
|
package/context-menu.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What can be done with an item, and the menu that offers it. An item's
|
|
3
|
+
* actions are declared once, beside what draws that kind of item (an app's
|
|
4
|
+
* beside its tile, @yaks/platform/app-tile `appActions`), as data: a label
|
|
5
|
+
* and what a press does. The item's menu (`ActionMenu`) and its own buttons
|
|
6
|
+
* (`actionProps`) are drawn from the same declaration, so they cannot drift.
|
|
7
|
+
*
|
|
8
|
+
* An item opens its menu (`menuOf`) by a right-click, a long press on a touch
|
|
9
|
+
* screen, the menu key or Shift+F10 while it holds the focus, and its ⋯
|
|
10
|
+
* (`Menu.More`). The menu is @yaks/ui's `Menu.Context`, a popover: Escape and
|
|
11
|
+
* a press elsewhere close it, the arrow keys walk its items, and the focus
|
|
12
|
+
* goes back to the item. The page's script binds it all once
|
|
13
|
+
* (./context-menu-browser.ts `contextMenus`).
|
|
14
|
+
*
|
|
15
|
+
* A press does one of:
|
|
16
|
+
* - `href`: opens a page, in a new tab with `tab`;
|
|
17
|
+
* - `opens`: opens the dialog (a popover) of that id on the page;
|
|
18
|
+
* - `submits`: sends the form of that id on the page, as its own button
|
|
19
|
+
* would, with the name and value of `sends` where the button carries one;
|
|
20
|
+
* - `copy`: puts the words on the clipboard;
|
|
21
|
+
* - `link`: puts a link on the clipboard, a path made whole against the page;
|
|
22
|
+
* - `share`: hands a link to the device's share sheet, or copies it where
|
|
23
|
+
* there is none;
|
|
24
|
+
* - `data`: marks the press with data attributes the page's own script
|
|
25
|
+
* answers, as it answers the item's own button (the friends dock's
|
|
26
|
+
* `data-dock-friend`).
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { actionProps, moved, opensMenu } from '@yaks/ux/context-menu'
|
|
30
|
+
* import { equal } from '@yaks/testing'
|
|
31
|
+
*
|
|
32
|
+
* equal(opensMenu({ key: 'ContextMenu', shiftKey: false }), true)
|
|
33
|
+
* equal(opensMenu({ key: 'F10', shiftKey: true }), true)
|
|
34
|
+
* equal(opensMenu({ key: 'F10', shiftKey: false }), false)
|
|
35
|
+
* // Three items: down from the last wraps to the first; End to the last.
|
|
36
|
+
* equal(moved('ArrowDown', 2, 3), 0)
|
|
37
|
+
* equal(moved('ArrowUp', 0, 3), 2)
|
|
38
|
+
* equal(moved('End', 0, 3), 2)
|
|
39
|
+
* equal(moved('Enter', 1, 3), undefined)
|
|
40
|
+
* // A press the page's own script answers wears its marks.
|
|
41
|
+
* equal(actionProps({ label: 'Message', data: { 'dock-friend': 'bo' } }), {
|
|
42
|
+
* as: 'button',
|
|
43
|
+
* type: 'button',
|
|
44
|
+
* 'data-dock-friend': 'bo',
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @module
|
|
49
|
+
*/ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, Fragment } from "react";
|
|
50
|
+
import { Icon } from "@yaks/ui/Icon";
|
|
51
|
+
import { Menu } from "@yaks/ui/Menu";
|
|
52
|
+
// @ts-types="@yaks/ui/lucide"
|
|
53
|
+
import Ellipsis from "lucide/dist/esm/icons/ellipsis.mjs";
|
|
54
|
+
/** The attribute naming the menu an item opens. */ export let ITEM = 'data-context-menu';
|
|
55
|
+
/** How long a finger rests on an item before its menu opens, ms. */ export let HOLD = 500;
|
|
56
|
+
/** How far a finger may wander and still be holding, px. */ export let SLOP = 10;
|
|
57
|
+
/** The props an item wears to open the menu `id`. */ export let menuOf = (id)=>({
|
|
58
|
+
[ITEM]: id
|
|
59
|
+
});
|
|
60
|
+
/** The props of a button or link that does `a`: an `href` for a link, the
|
|
61
|
+
* rest for a `button`. A menu's items and an item's own buttons both wear
|
|
62
|
+
* them. */ export let actionProps = (a)=>a.href ? {
|
|
63
|
+
as: 'a',
|
|
64
|
+
href: a.href,
|
|
65
|
+
...a.tab === true ? {
|
|
66
|
+
target: '_blank',
|
|
67
|
+
rel: 'noopener'
|
|
68
|
+
} : {},
|
|
69
|
+
...a.tab === false ? {
|
|
70
|
+
target: '_self'
|
|
71
|
+
} : {}
|
|
72
|
+
} : a.submits ? {
|
|
73
|
+
as: 'button',
|
|
74
|
+
type: 'submit',
|
|
75
|
+
form: a.submits,
|
|
76
|
+
...a.sends ? {
|
|
77
|
+
name: a.sends[0],
|
|
78
|
+
value: a.sends[1]
|
|
79
|
+
} : {}
|
|
80
|
+
} : {
|
|
81
|
+
as: 'button',
|
|
82
|
+
type: 'button',
|
|
83
|
+
...a.opens ? {
|
|
84
|
+
popovertarget: a.opens
|
|
85
|
+
} : {},
|
|
86
|
+
...a.copy != null ? {
|
|
87
|
+
'data-copy': a.copy
|
|
88
|
+
} : {},
|
|
89
|
+
...a.link != null ? {
|
|
90
|
+
'data-link': a.link
|
|
91
|
+
} : {},
|
|
92
|
+
...a.share != null ? {
|
|
93
|
+
'data-share': a.share
|
|
94
|
+
} : {},
|
|
95
|
+
...Object.fromEntries(Object.entries(a.data ?? {}).map(([k, v])=>[
|
|
96
|
+
`data-${k}`,
|
|
97
|
+
v
|
|
98
|
+
]))
|
|
99
|
+
};
|
|
100
|
+
// What can be offered: those the page left undefined drop out, the ones that
|
|
101
|
+
// lose something go last.
|
|
102
|
+
let offered = (actions)=>{
|
|
103
|
+
let all = Object.values(actions).filter((a)=>!!a);
|
|
104
|
+
return [
|
|
105
|
+
all.filter((a)=>!a.danger),
|
|
106
|
+
all.filter((a)=>a.danger)
|
|
107
|
+
];
|
|
108
|
+
};
|
|
109
|
+
/** The item's menu, `id`, offering `actions`: what its right-click, long
|
|
110
|
+
* press and ⋯ open. `label` names it to a screen reader. */ export let ActionMenu = ({ id, label, actions })=>{
|
|
111
|
+
let [safe, danger] = offered(actions);
|
|
112
|
+
let item = (a)=>/*#__PURE__*/ h(Menu.Item, {
|
|
113
|
+
key: a.label,
|
|
114
|
+
role: 'menuitem',
|
|
115
|
+
mod: a.danger ? 'danger' : undefined,
|
|
116
|
+
...actionProps(a)
|
|
117
|
+
}, a.icon ? /*#__PURE__*/ h(Icon, {
|
|
118
|
+
of: a.icon
|
|
119
|
+
}) : null, a.label);
|
|
120
|
+
return /*#__PURE__*/ h(Menu.Context, {
|
|
121
|
+
id,
|
|
122
|
+
'aria-label': label
|
|
123
|
+
}, safe.map(item), safe.length && danger.length ? /*#__PURE__*/ h(Menu.Rule, {
|
|
124
|
+
role: 'separator'
|
|
125
|
+
}) : null, danger.map(item));
|
|
126
|
+
};
|
|
127
|
+
/** The item's ⋯, opening its menu `id`; `label` says whose menu it is. */ export let MoreButton = ({ id, label })=>/*#__PURE__*/ h(Menu.More, {
|
|
128
|
+
type: 'button',
|
|
129
|
+
popovertarget: id,
|
|
130
|
+
'aria-haspopup': 'menu',
|
|
131
|
+
'aria-expanded': 'false',
|
|
132
|
+
'aria-label': label,
|
|
133
|
+
'data-tip': 'More'
|
|
134
|
+
}, /*#__PURE__*/ h(Icon, {
|
|
135
|
+
of: Ellipsis
|
|
136
|
+
}));
|
|
137
|
+
/** An item's menu and its ⋯ together, for a row that shows both. */ export let MoreMenu = (props)=>/*#__PURE__*/ h(Fragment, null, /*#__PURE__*/ h(MoreButton, props), /*#__PURE__*/ h(ActionMenu, props));
|
|
138
|
+
/** Whether a key opens the focused item's menu: the menu key, or Shift+F10. */ export let opensMenu = (e)=>e.key == 'ContextMenu' || e.key == 'F10' && e.shiftKey;
|
|
139
|
+
/** Where the arrow keys, Home and End move from item `at` of `n`, wrapping
|
|
140
|
+
* at either end, or undefined for a key that does not move. */ export let moved = (key, at, n)=>!n ? undefined : ({
|
|
141
|
+
ArrowDown: (at + 1) % n,
|
|
142
|
+
ArrowUp: (at - 1 + n) % n,
|
|
143
|
+
Home: 0,
|
|
144
|
+
End: n - 1
|
|
145
|
+
})[key];
|
|
146
|
+
//# sourceMappingURL=context-menu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-menu.js","sources":["./context-menu.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgDC,GACD,uCAAuC,GAAG,SAAS,iBAAiB,CAAC,EAAE,QAAQ,gBAA0C;AACzH,SAAS,IAAI,6BAAkC;AAC/C,SAAS,IAAI,6BAAkC;AAE/C,8BAA8B;AAC9B,OAAO,mDAA6D;AA0BpE,iDAAiD,GACjD,OAAO,IAAI,OAAO,oBAAmB;AAErC,kEAAkE,GAClE,OAAO,IAAI,OAAO,IAAG;AACrB,0DAA0D,GAC1D,OAAO,IAAI,OAAO,GAAE;AAEpB,mDAAmD,GACnD,OAAO,IAAI,SAAS,CAAC,KAAuC,CAAC;IAAE,CAAC,KAAK,EAAE;EAAG,CAAC,EAAC;AAE5E;;SAES,GACT,OAAO,IAAI,cAAc,CAAC,IACxB,EAAE,IAAI,GACF;IACA,IAAI;IACJ,MAAM,EAAE,IAAI;IACZ,GAAG,EAAE,GAAG,KAAK,OAAO;MAAE,QAAQ;MAAU,KAAK;IAAW,IAAI,CAAC,CAAC;IAC9D,GAAG,EAAE,GAAG,KAAK,QAAQ;MAAE,QAAQ;IAAQ,IAAI,CAAC,CAAC;EAC/C,IACE,EAAE,OAAO,GACT;IACA,IAAI;IACJ,MAAM;IACN,MAAM,EAAE,OAAO;IACf,GAAG,EAAE,KAAK,GAAG;MAAE,MAAM,EAAE,KAAK,CAAC,EAAE;MAAE,OAAO,EAAE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