-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfootnotes.js
61 lines (51 loc) · 2.35 KB
/
footnotes.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
document.addEventListener('DOMContentLoaded', () => {
const footnotes = document.querySelectorAll('footnote');
footnotes.forEach((footnote, index) => {
const id = `footnote-${index + 1}`;
// Create the superscript index
const superscript = document.createElement('sup');
superscript.className = 'footnote-index';
superscript.textContent = `(${index + 1})`;
superscript.dataset.popoverTarget = id;
// Insert the superscript after the footnote
footnote.parentNode.insertBefore(superscript, footnote.nextSibling);
// Create the popover element
const popover = document.createElement('div');
popover.id = id;
popover.className = 'popover';
// Clone child nodes of footnote into popover
footnote.childNodes.forEach(node => {
popover.appendChild(node.cloneNode(true));
});
document.body.appendChild(popover);
// Function to hide the popover
const hidePopover = (event) => {
// Don't hide if text is selected, to be copy-paste friendly.
const selection = window.getSelection();
if (!selection.isCollapsed) {
return;
}
if (event && event.target.closest('.popover') !== popover) {
popover.style.display = 'none';
document.removeEventListener('click', hidePopover);
window.removeEventListener('scroll', hidePopover);
}
};
// Add event listener to the superscript
superscript.addEventListener('click', (event) => {
const popover = document.getElementById(event.target.dataset.popoverTarget);
const rect = event.target.getBoundingClientRect();
const scrollY = window.scrollY;
const scrollX = window.scrollX;
// Calculate the position relative to the document
popover.style.top = `${rect.bottom + scrollY + 5}px`; // Position below the text
popover.style.left = `${rect.left + scrollX}px`;
popover.style.display = 'block';
// Close popover when clicking outside or scrolling
setTimeout(() => {
document.addEventListener('click', hidePopover);
window.addEventListener('scroll', hidePopover);
}, 0);
});
});
});