-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
77 lines (62 loc) · 2.48 KB
/
menu.js
File metadata and controls
77 lines (62 loc) · 2.48 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
export class BAEContextMenu {
static attach(el) {
el.addEventListener("contextmenu", e => {
e.preventDefault();
this.remove();
const menu = document.createElement("div");
menu.className = "bae-menu";
// Position it at mouse location
menu.style.position = "fixed";
menu.style.left = e.clientX + "px";
menu.style.top = e.clientY + "px";
menu.innerHTML = `
<div data-action="about">About ▸</div>
<div data-action="html">Copy HTML ▸</div>
<div data-action="text">Copy Plain Text</div>
`;
// Click events for menu items
menu.addEventListener("click", ev => {
const action = ev.target.dataset.action;
if (!action) return;
if (action === "about") alert("BasicAritEngine v2 — Math Renderer with fractions, exponents, roots");
if (action === "html") navigator.clipboard.writeText(el.innerHTML);
if (action === "text") navigator.clipboard.writeText(el.dataset.raw);
this.remove();
});
// Append to body so it floats above everything
document.body.appendChild(menu);
// Remove menu if you click anywhere else
setTimeout(() => {
document.addEventListener("click", this.remove, { once: true });
}, 0);
});
}
static remove() {
document.querySelectorAll(".bae-menu").forEach(m => m.remove());
}
}
export class BAESelection {
static attach(el) {
el.addEventListener("mouseup", () => {
const sel = window.getSelection();
if (!sel.toString()) return;
this.removeTooltip();
const rect = sel.getRangeAt(0).getBoundingClientRect();
const tip = document.createElement("div");
tip.className = "bae-tooltip";
tip.style.position = "fixed";
tip.style.left = rect.left + "px";
tip.style.top = rect.top - 30 + "px";
tip.textContent = "Highlight";
tip.onclick = () => {
const rgb = getComputedStyle(el).color;
alert("RGB: " + rgb);
this.removeTooltip();
};
document.body.appendChild(tip);
});
}
static removeTooltip() {
document.querySelectorAll(".bae-tooltip").forEach(t => t.remove());
}
}