-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtooltip.js
92 lines (78 loc) · 2.59 KB
/
tooltip.js
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
(() => {
const simulateKeydown = (el, keyCode) => {
const keyboardEvent = new KeyboardEvent("keydown", {
bubbles: true,
cancelable: true,
keyCode,
});
return el.dispatchEvent(keyboardEvent);
};
const generate = () => {
const headingList = [...document.querySelectorAll("h3[name], h4[name]")];
return headingList
.filter(
(headingTag) =>
!headingTag.classList.contains("graf--title") &&
!headingTag.classList.contains("graf--subtitle"),
)
.map((headingTag) => {
let bullet;
switch (headingTag.tagName.toLowerCase()) {
case "h1":
case "h2":
case "h3":
bullet = `·`;
break;
case "h4":
bullet = ` ∘`;
break;
}
return `${bullet} <a href="#${headingTag.getAttribute(
"name",
)}" title="${headingTag.textContent}">${headingTag.textContent}</a>`;
});
};
const inject = () => {
const tooltipToggleMenu = document.querySelector(
"[data-action=inline-menu]",
);
const tooltip = document.querySelector(".inlineTooltip");
const tooltipMenu = document.querySelector(".inlineTooltip-menu");
const addEmbedButton = document.querySelector(
".inlineTooltip-menu [title='Add an embed']",
);
if (!tooltip || !tooltipToggleMenu || !tooltipMenu || !addEmbedButton) {
return;
}
tooltip.style.width = "auto";
const toCButton = document.querySelector("[data-action='inline-menu-toc']");
if (toCButton) {
return;
}
const handleAddToCClick = (e) => {
const container = document.querySelector(".is-selected");
tooltipToggleMenu.click();
container.innerHTML = generate().join("<br/>");
setTimeout(() => {
simulateKeydown(container, 13);
}, 0);
};
const addToCButton = addEmbedButton.cloneNode(true);
const title = "Add a Table of Contents";
addToCButton.setAttribute("title", title);
addToCButton.setAttribute("aria-label", title);
addToCButton.setAttribute("data-action", "inline-menu-toc");
addToCButton.setAttribute(
"data-action-value",
"Generate a table of contents",
);
addToCButton.setAttribute("data-default-value", "Table of contents");
addToCButton.innerHTML = "⋮";
addToCButton.style.color = "rgb(26, 137, 23)";
addToCButton.style.border = "1px solid";
addToCButton.style.fontWeight = "bold";
addToCButton.addEventListener("click", handleAddToCClick);
tooltipMenu.appendChild(addToCButton);
};
setInterval(inject, 500);
})();