]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - js/right-click-menus.js
Add Plugins: remove author from card footer
[irc/unrealircd/unrealircd-webpanel.git] / js / right-click-menus.js
1 /**
2 * Right-click menus for use around the webpanel
3 */
4 let selection = null;
5 let click_target = null;
6 function can_clipboard()
7 {
8 if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.writeText === "function"
9 && typeof navigator.clipboard.readText === "function")
10 return true;
11 return false;
12 }
13
14 async function paste_from_clipboard()
15 {
16 let text = await navigator.clipboard.readText();
17 click_target.value = text;
18 }
19
20 async function copy_to_clipboard()
21 {
22 navigator.clipboard.writeText(selection);
23 }
24
25 function build_rclick_menu()
26 {
27 const m = document.createElement('div');
28 m.classList.add('nav-item','list-group');
29 m.id = 'rclickmenu';
30
31 const m1 = document.createElement('div');
32 m1.classList.add('item', 'list-group-item-action');
33 m1.id = 'rclick_opt1';
34 }
35
36 var rclickmenu = document.getElementById('rclickmenu');
37
38 document.addEventListener("click", (e) =>
39 {
40 rclickmenu.classList.remove("visible");
41 });
42
43
44 document.addEventListener("contextmenu", (event) =>
45 {
46 event.preventDefault();
47 click_target = event.target;
48
49 rclickmenu.classList.remove("visible"); // hide it if it was already elsweyr
50 var { clientX: mouseX, clientY: mouseY } = event;
51
52 rclickmenu.style.top = `${mouseY}px`;
53 rclickmenu.style.left = `${mouseX}px`;
54
55 /* "Copy" option */
56 selection = window.getSelection().toString();
57
58 if (selection.length == 0 || !can_clipboard())
59 document.getElementById('rclick_opt1').style.display = 'none';
60
61 else if (can_clipboard())
62 document.getElementById('rclick_opt1').style.display = '';
63
64 /* Check if the browser supports pasting */
65 if (!can_clipboard() || (!click_target || click_target.tagName.toLowerCase() !== "input"))
66 document.getElementById('rclick_opt2').style.display = 'none';
67
68 else if (click_target && click_target.tagName.toLowerCase() === "input")
69 document.getElementById('rclick_opt2').style.display = '';
70
71 setTimeout(() => { rclickmenu.classList.add("visible"); });
72 });
73 document.addEventListener('keydown', (event) => {
74 if (event.key === 'Escape')
75 {
76 rclickmenu.classList.remove("visible");
77 }
78 });