-
Notifications
You must be signed in to change notification settings - Fork 2
/
sideline.js
69 lines (60 loc) · 3.11 KB
/
sideline.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
function offset(el) {
let rect = el.getBoundingClientRect(),
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return { top: rect.top + scrollTop, left: rect.left + scrollLeft, height: rect.height, width: rect.width }
}
function setup_sideline() {
let old_svgs = document.getElementsByClassName('sideline');
while(old_svgs[0]) {
old_svgs[0].parentNode.removeChild(old_svgs[0]);
}
const labels = document.getElementsByClassName("sidenote-number");
for (const label of labels) {
const note = label.nextSibling.nextSibling; // :(
let svg_container = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg_container.classList.add("sideline");
svg_container.style.height = document.body.scrollHeight + "px";
svg_container.setAttribute("shape-rendering", "crispEdges");
const label_offset = offset(label);
const note_offset = offset(note);
let svg_line1 = document.createElementNS("http://www.w3.org/2000/svg", "line");
svg_container.appendChild(svg_line1);
svg_line1.setAttribute("stroke-width", "2px");
svg_line1.setAttribute("stroke", "#666");
svg_line1.setAttribute("x1", label_offset.left);
svg_line1.setAttribute("y1", label_offset.top + label_offset.height + 3);
svg_line1.setAttribute("x2", note_offset.left);
svg_line1.setAttribute("y2", label_offset.top + label_offset.height + 3);
let svg_line2 = document.createElementNS("http://www.w3.org/2000/svg", "line");
svg_container.appendChild(svg_line2);
svg_line2.setAttribute("stroke-width", "2px");
svg_line2.setAttribute("stroke", "#666");
svg_line2.setAttribute("x1", note_offset.left - 1);
svg_line2.setAttribute("y1", Math.min(label_offset.top + label_offset.height + 3, note_offset.top - 6));
svg_line2.setAttribute("x2", note_offset.left - 1);
svg_line2.setAttribute("y2", note_offset.top + note_offset.height + 6);
let svg_line3 = document.createElementNS("http://www.w3.org/2000/svg", "line");
svg_container.appendChild(svg_line3);
svg_line3.setAttribute("stroke-width", "2px");
svg_line3.setAttribute("stroke", "#666");
svg_line3.setAttribute("x1", note_offset.left - 1);
svg_line3.setAttribute("y1", note_offset.top - 6);
svg_line3.setAttribute("x2", note_offset.left + 18);
svg_line3.setAttribute("y2", note_offset.top - 6);
let svg_line4 = document.createElementNS("http://www.w3.org/2000/svg", "line");
svg_container.appendChild(svg_line4);
svg_line4.setAttribute("stroke-width", "2px");
svg_line4.setAttribute("stroke", "#666");
svg_line4.setAttribute("x1", note_offset.left - 1);
svg_line4.setAttribute("y1", note_offset.top + note_offset.height + 6);
svg_line4.setAttribute("x2", note_offset.left + 18);
svg_line4.setAttribute("y2", note_offset.top + note_offset.height + 6);
note.parentNode.insertBefore(svg_container, note.nextSibling); // might break if sidenote is last element in parent?
}
}
if (matchMedia('(hover:hover),(any-hover:hover)').matches) {
document.fonts.ready.then(setup_sideline);
window.addEventListener('load', setup_sideline);
window.addEventListener('resize', setup_sideline);
}