-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontext-menu.js
More file actions
205 lines (183 loc) · 6.66 KB
/
Copy pathcontext-menu.js
File metadata and controls
205 lines (183 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Themed custom context menu for the new tab page
// Usage: contextMenu.init({ theme: '<theme-preset>', onAction: (action, payload)=>{} })
(function() {
const state = {
root: null,
menu: null,
theme: 'dark',
onAction: null,
currentPayload: null
};
function ensureRoot() {
if (!state.root) {
state.root = document.createElement('div');
state.root.className = 'context-menu-root';
document.body.appendChild(state.root);
}
}
function closeMenu() {
if (state.menu) {
state.menu.classList.remove('open');
const m = state.menu;
state.menu = null;
setTimeout(() => m.remove(), 100);
}
state.currentPayload = null;
}
function placeMenu(x, y) {
if (!state.menu) return;
const rect = state.menu.getBoundingClientRect();
const vw = window.innerWidth;
const vh = window.innerHeight;
const nx = Math.min(x, vw - rect.width - 8);
const ny = Math.min(y, vh - rect.height - 8);
state.menu.style.left = nx + 'px';
state.menu.style.top = ny + 'px';
requestAnimationFrame(() => state.menu.classList.add('open'));
}
function createItem(labelKey, action, kbd) {
const el = document.createElement('div');
el.className = 'ctx-item';
el.setAttribute('role', 'menuitem');
el.textContent = i18n.t(labelKey);
if (kbd) {
const kb = document.createElement('span');
kb.className = 'ctx-kbd';
kb.textContent = kbd;
el.appendChild(kb);
}
el.addEventListener('click', () => {
if (typeof state.onAction === 'function') {
state.onAction(action, state.currentPayload);
}
closeMenu();
});
return el;
}
function closestSafe(target, selector) {
if (!target) return null;
const el = target.nodeType === 1 ? target : target.parentElement;
return el ? el.closest(selector) : null;
}
function closestFromPoint(e, selector) {
const direct = closestSafe(e?.target, selector);
if (direct) return direct;
if (typeof e?.clientX !== 'number' || typeof e?.clientY !== 'number') return null;
const hit = document.elementFromPoint(e.clientX, e.clientY);
return closestSafe(hit, selector);
}
function createCheckItem(labelKey, action, checked) {
const el = document.createElement('div');
el.className = 'ctx-item';
el.setAttribute('role', 'menuitemcheckbox');
const box = document.createElement('span');
box.className = 'ctx-checkbox';
box.textContent = checked ? '☑' : '☐';
const label = document.createElement('span');
label.textContent = i18n.t(labelKey);
el.appendChild(box);
el.appendChild(label);
el.addEventListener('click', () => {
if (typeof state.onAction === 'function') {
state.onAction(action, state.currentPayload);
}
closeMenu();
});
return el;
}
function separator() {
const sep = document.createElement('div');
sep.className = 'ctx-sep';
return sep;
}
function hint() {
const el = document.createElement('div');
el.className = 'ctx-hint';
el.textContent = i18n.t('contextCloseHint');
return el;
}
function buildMenuForCategory(payload) {
const menu = document.createElement('div');
menu.className = 'context-menu';
menu.dataset.theme = state.theme;
menu.appendChild(createItem('openAll', 'open_all', '⇧Enter'));
menu.appendChild(separator());
menu.appendChild(hint());
return menu;
}
function buildMenuForSite(payload) {
const menu = document.createElement('div');
menu.className = 'context-menu';
menu.dataset.theme = state.theme;
menu.appendChild(createItem('openInNewTab', 'open', 'Enter'));
menu.appendChild(createItem('edit', 'edit', 'E'));
menu.appendChild(createItem('remove', 'delete', 'Del'));
menu.appendChild(separator());
menu.appendChild(hint());
return menu;
}
function onDocumentContextMenu(e) {
if (!state.root) return;
const blacklist = closestSafe(e.target, 'input, textarea, select, [contenteditable], .modal, .modal-form');
if (blacklist) return; // allow native menu on inputs or modals
e.preventDefault();
closeMenu();
const categoryEl = closestFromPoint(e, '.category-item, .category-nav-header');
const siteEl = closestFromPoint(e, '.shortcut-item:not(.add-shortcut)');
let payload;
if (categoryEl) {
payload = { type: 'category', id: categoryEl.dataset.category || 'all' };
} else if (siteEl) {
payload = { type: 'site', index: parseInt(siteEl.dataset.index || '-1', 10) };
} else {
payload = { type: 'blank' };
}
state.currentPayload = payload;
let menu;
if (payload.type === 'category') menu = buildMenuForCategory(payload);
else if (payload.type === 'site') menu = buildMenuForSite(payload);
else menu = buildMenuForBlank();
state.menu = menu;
state.root.appendChild(menu);
placeMenu(e.clientX, e.clientY);
}
function onGlobalPointerDown(e) {
if (state.menu && !state.menu.contains(e.target)) closeMenu();
}
function onKeydown(e) {
if (e.key === 'Escape') closeMenu();
}
function init(opts = {}) {
state.theme = opts.theme || 'dark';
state.onAction = opts.onAction || null;
ensureRoot();
document.addEventListener('contextmenu', onDocumentContextMenu);
document.addEventListener('pointerdown', onGlobalPointerDown, { passive: true });
document.addEventListener('keydown', onKeydown);
}
function destroy() {
document.removeEventListener('contextmenu', onDocumentContextMenu);
document.removeEventListener('pointerdown', onGlobalPointerDown);
document.removeEventListener('keydown', onKeydown);
closeMenu();
if (state.root) { state.root.remove(); state.root = null; }
}
function buildMenuForBlank() {
const menu = document.createElement('div');
menu.className = 'context-menu';
menu.dataset.theme = state.theme;
const comp = window.shortcutsComponentInstance;
const auto = !!(comp && comp.layout && comp.layout.autoArrange);
const align = !!(comp && comp.layout && comp.layout.alignToGrid);
const hidden = typeof window.dashboardHiddenState === 'boolean'
? window.dashboardHiddenState
: document.body.classList.contains('dashboard-hidden');
menu.appendChild(createCheckItem('autoArrangeIcons', 'layout_auto_arrange_toggle', auto));
menu.appendChild(createCheckItem('alignToGrid', 'layout_align_grid_toggle', align));
menu.appendChild(createCheckItem('toggleDashboardHidden', 'dashboard_visibility_toggle', hidden));
menu.appendChild(separator());
menu.appendChild(hint());
return menu;
}
window.contextMenu = { init, destroy };
})();