-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathcontext-menu.js
More file actions
400 lines (349 loc) · 13 KB
/
Copy pathcontext-menu.js
File metadata and controls
400 lines (349 loc) · 13 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Adapted context-menu — stripped file-related items, no Tauri opener
import { t } from "../core/i18n.js";
const isMac = /Mac|iPhone|iPad/.test(navigator.platform);
const modLabel = isMac ? "\u2318" : "Ctrl";
let menu = null;
let cleanupFns = [];
let savedRange = null;
// Detect clipboard API availability (blocked in Safari/Firefox sandboxed iframes)
let _clipboardApiSupported = null;
function _isClipboardApiAvailable() {
if (_clipboardApiSupported !== null) { return _clipboardApiSupported; }
if (!navigator.clipboard || !navigator.clipboard.readText) {
_clipboardApiSupported = false;
return false;
}
// Probe by calling readText — if permissions policy blocks it, it throws synchronously
// or rejects immediately. Cache the result after first context menu open.
navigator.clipboard.readText()
.then(() => { _clipboardApiSupported = true; })
.catch(() => { _clipboardApiSupported = false; });
// Optimistic for first open on Chromium; will correct on next open if blocked
_clipboardApiSupported = true;
return _clipboardApiSupported;
}
export function initContextMenu() {
menu = document.getElementById("context-menu");
if (!menu) return;
const onContextMenu = (e) => {
if (isEditMode()) {
const td = e.target.closest("td, th");
const contentEl = document.getElementById("viewer-content");
if (td && contentEl && contentEl.contains(td)) {
return;
}
}
e.preventDefault();
const context = detectContext(e);
const items = buildItems(context);
if (items.length === 0) return;
showMenu(e.clientX, e.clientY, items);
};
const onKeydown = (e) => {
if (e.shiftKey && e.key === "F10") {
e.preventDefault();
const pos = getCaretPosition();
const context = detectContext(null);
const items = buildItems(context);
if (items.length > 0) {
showMenu(pos.x, pos.y, items, true);
}
return;
}
if (menu && menu.classList.contains("open")) {
if (e.key === "Escape") {
e.preventDefault();
hideMenu();
return;
}
if (["ArrowDown", "ArrowUp", "Home", "End", "Enter", " "].includes(e.key)) {
e.preventDefault();
handleMenuKeydown(e.key);
}
}
};
const onClickOutside = (e) => {
if (!menu || !menu.classList.contains("open")) return;
if (menu.contains(e.target)) return;
hideMenu();
};
const onScroll = () => {
if (!menu || !menu.classList.contains("open")) return;
hideMenu();
};
const onRightMouseDown = (e) => {
if (e.button !== 2) return;
if (!isEditMode()) return;
const sel = window.getSelection();
if (!sel || sel.isCollapsed || sel.rangeCount === 0) return;
const clickRange = document.caretRangeFromPoint(e.clientX, e.clientY);
if (!clickRange) return;
const selRange = sel.getRangeAt(0);
const afterStart = selRange.compareBoundaryPoints(Range.START_TO_START, clickRange) <= 0;
const beforeEnd = selRange.compareBoundaryPoints(Range.END_TO_START, clickRange) >= 0;
if (afterStart && beforeEnd) {
e.preventDefault();
savedRange = selRange.cloneRange();
}
};
document.addEventListener("contextmenu", onContextMenu);
document.addEventListener("keydown", onKeydown);
document.addEventListener("mousedown", onClickOutside);
document.addEventListener("touchstart", onClickOutside);
document.addEventListener("mousedown", onRightMouseDown, true);
document.addEventListener("scroll", onScroll, true);
cleanupFns.push(() => {
document.removeEventListener("contextmenu", onContextMenu);
document.removeEventListener("keydown", onKeydown);
document.removeEventListener("mousedown", onClickOutside);
document.removeEventListener("touchstart", onClickOutside);
document.removeEventListener("mousedown", onRightMouseDown, true);
document.removeEventListener("scroll", onScroll, true);
});
}
function isEditMode() {
const contentEl = document.getElementById("viewer-content");
return contentEl && contentEl.isContentEditable;
}
function detectContext(e) {
const editMode = isEditMode();
const sel = window.getSelection();
const hasSelection = sel && !sel.isCollapsed && sel.toString().trim().length > 0;
let target = e ? e.target : null;
let link = target ? target.closest("a[href]") : null;
let image = target ? (target.tagName === "IMG" ? target : target.closest("img")) : null;
let diagram = target ? target.closest(".mermaid-diagram[data-mermaid-source]") : null;
const contentEl = document.getElementById("viewer-content");
if (link && contentEl && !contentEl.contains(link)) link = null;
if (image && contentEl && !contentEl.contains(image)) image = null;
if (diagram && contentEl && !contentEl.contains(diagram)) diagram = null;
return { editMode, hasSelection, link, image, diagram, sel };
}
function buildItems(ctx) {
const items = [];
if (ctx.editMode) {
if (ctx.hasSelection) {
items.push({
label: t("context.cut"),
shortcut: `${modLabel}+X`,
action: () => document.execCommand("cut")
});
items.push({
label: t("context.copy"),
shortcut: `${modLabel}+C`,
action: () => document.execCommand("copy")
});
}
// Clipboard API paste only works in Chromium with permissions policy.
// In Safari/Firefox sandboxed iframes, it's blocked. Users can still Ctrl/Cmd+V.
if (_isClipboardApiAvailable()) {
items.push({
label: t("context.paste"),
shortcut: `${modLabel}+V`,
action: () => pasteFromClipboard(false)
});
items.push({
label: t("context.paste_plain"),
shortcut: `${modLabel}+\u21E7+V`,
action: () => pasteFromClipboard(true)
});
}
items.push({ divider: true });
items.push({
label: t("context.select_all"),
shortcut: `${modLabel}+A`,
action: () => selectAllContent()
});
} else {
if (ctx.diagram) {
items.push({
label: t("context.copy_diagram_source") || "Copy diagram source",
action: () => copyToClipboard(ctx.diagram.getAttribute("data-mermaid-source"))
});
const svgEl = ctx.diagram.querySelector("svg");
if (svgEl) {
items.push({
label: t("context.copy_as_svg") || "Copy as SVG",
action: () => copyToClipboard(svgEl.outerHTML)
});
}
if (ctx.hasSelection || ctx.link || ctx.image) {
items.push({ divider: true });
}
}
if (ctx.link) {
const href = ctx.link.getAttribute("href");
items.push({
label: t("context.copy_link"),
action: () => copyToClipboard(href)
});
items.push({
label: t("context.open_link"),
action: () => window.open(href, "_blank", "noopener")
});
if (ctx.hasSelection || ctx.image) {
items.push({ divider: true });
}
}
if (ctx.image) {
const src = ctx.image.getAttribute("src");
items.push({
label: t("context.copy_image_address"),
action: () => copyToClipboard(src)
});
if (ctx.hasSelection) {
items.push({ divider: true });
}
}
if (ctx.hasSelection) {
items.push({
label: t("context.copy"),
shortcut: `${modLabel}+C`,
action: () => document.execCommand("copy")
});
}
items.push({
label: t("context.select_all"),
shortcut: `${modLabel}+A`,
action: () => selectAllContent()
});
}
return items;
}
function showMenu(x, y, items, autoFocus) {
if (!menu) return;
const sel = window.getSelection();
if (sel && sel.rangeCount > 0) {
savedRange = sel.getRangeAt(0).cloneRange();
}
menu.innerHTML = "";
let firstItem = null;
items.forEach((item) => {
if (item.divider) {
const div = document.createElement("div");
div.className = "context-menu-divider";
div.setAttribute("role", "separator");
menu.appendChild(div);
return;
}
const btn = document.createElement("button");
btn.className = "context-menu-item";
btn.setAttribute("role", "menuitem");
btn.setAttribute("tabindex", "-1");
const labelSpan = document.createElement("span");
labelSpan.textContent = item.label;
btn.appendChild(labelSpan);
if (item.shortcut) {
const shortcutSpan = document.createElement("span");
shortcutSpan.className = "context-menu-item-shortcut";
shortcutSpan.textContent = item.shortcut;
btn.appendChild(shortcutSpan);
}
btn.addEventListener("click", (e) => {
e.stopPropagation();
const range = savedRange;
savedRange = null;
hideMenu();
if (!item.disabled) {
if (range) {
if (isEditMode()) {
const contentEl = document.getElementById("viewer-content");
if (contentEl) contentEl.focus({ preventScroll: true });
}
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
item.action();
}
});
if (!firstItem) firstItem = btn;
menu.appendChild(btn);
});
menu.style.left = x + "px";
menu.style.top = y + "px";
menu.classList.add("open");
requestAnimationFrame(() => {
const rect = menu.getBoundingClientRect();
if (rect.right > window.innerWidth) {
menu.style.left = Math.max(0, window.innerWidth - rect.width - 4) + "px";
}
if (rect.bottom > window.innerHeight) {
menu.style.top = Math.max(0, window.innerHeight - rect.height - 4) + "px";
}
if (autoFocus && firstItem) firstItem.focus();
});
}
function handleMenuKeydown(key) {
if (!menu) return;
const menuItems = [...menu.querySelectorAll(".context-menu-item:not([aria-disabled='true'])")];
const current = document.activeElement;
const idx = menuItems.indexOf(current);
if (key === "ArrowDown") {
const next = idx < menuItems.length - 1 ? idx + 1 : 0;
menuItems[next]?.focus();
} else if (key === "ArrowUp") {
const prev = idx > 0 ? idx - 1 : menuItems.length - 1;
menuItems[prev]?.focus();
} else if (key === "Home") {
menuItems[0]?.focus();
} else if (key === "End") {
menuItems[menuItems.length - 1]?.focus();
} else if (key === "Enter" || key === " ") {
if (current && current.classList.contains("context-menu-item")) {
current.click();
}
}
}
function hideMenu() {
if (!menu) return;
menu.classList.remove("open");
savedRange = null;
}
function getCaretPosition() {
const sel = window.getSelection();
if (sel && sel.rangeCount) {
const range = sel.getRangeAt(0);
const rect = range.getBoundingClientRect();
if (rect.width || rect.height) {
return { x: rect.left, y: rect.bottom };
}
}
return { x: window.innerWidth / 2, y: window.innerHeight / 2 };
}
async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
} catch {
const ta = document.createElement("textarea");
ta.value = text;
ta.style.position = "fixed";
ta.style.left = "-9999px";
document.body.appendChild(ta);
ta.select();
document.execCommand("copy");
document.body.removeChild(ta);
}
}
async function pasteFromClipboard(plainOnly) {
const contentEl = document.getElementById("viewer-content");
if (!contentEl) return;
contentEl.focus({ preventScroll: true });
try {
const text = await navigator.clipboard.readText();
if (text) {
document.execCommand("insertText", false, text);
}
} catch {
document.execCommand("paste");
}
}
function selectAllContent() {
const contentEl = document.getElementById("viewer-content");
if (!contentEl) return;
const range = document.createRange();
range.selectNodeContents(contentEl);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}