Skip to content

Commit d0ede41

Browse files
committed
fix: don't show single-element toc, handle clicks
1 parent 6335050 commit d0ede41

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

docs/theme/pagetoc.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
--toc-width: 310px;
66
}
77

8-
a[class^="pagetoc-H"]:only-child {
9-
display: none;
10-
}
11-
128
.nav-chapters {
139
min-width: 50px;
1410
}

docs/theme/pagetoc.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function forPagetocElem(fun) {
1111
forEach(getPagetocElems(), fun);
1212
}
1313

14-
var activeHref = location.href;
15-
1614
function getRect(element) {
1715
return element.getBoundingClientRect();
1816
}
@@ -25,32 +23,39 @@ function overflowBottom(container, element) {
2523
return getRect(container).bottom - getRect(element).bottom;
2624
}
2725

28-
var updateFunction = function () {
29-
var id = undefined;
26+
var activeHref = location.href;
27+
28+
var updateFunction = function (elem = undefined) {
29+
var id = elem;
3030

31-
if (location.href != activeHref) {
31+
if (!elem && location.href != activeHref) {
3232
activeHref = location.href;
3333
forPagetocElem(function (el) {
34-
if (el.href == activeHref) {
34+
if (el.href === activeHref) {
3535
id = el;
3636
}
3737
});
3838
}
3939

40-
var elements = document.getElementsByClassName("header");
41-
let menuBottom = getRect(document.getElementById("menu-bar")).bottom;
42-
let contentCenter = window.innerHeight / 2;
43-
let margin = contentCenter / 3;
40+
console.log("id");
41+
console.log(id);
4442

45-
forEach(elements, function (el, i, arr) {
46-
if (id === undefined && getRect(el).bottom >= menuBottom) {
47-
if (getRect(el).top >= contentCenter + margin) {
48-
id = arr[Math.max(0, i - 1)];
49-
} else {
50-
id = el;
43+
if (!id) {
44+
var elements = document.getElementsByClassName("header");
45+
let menuBottom = getRect(document.getElementById("menu-bar")).bottom;
46+
let contentCenter = window.innerHeight / 2;
47+
let margin = contentCenter / 3;
48+
49+
forEach(elements, function (el, i, arr) {
50+
if (id === undefined && getRect(el).bottom >= menuBottom) {
51+
if (getRect(el).top >= contentCenter + margin) {
52+
id = arr[Math.max(0, i - 1)];
53+
} else {
54+
id = el;
55+
}
5156
}
52-
}
53-
});
57+
});
58+
}
5459

5560
forPagetocElem(function (el) {
5661
el.classList.remove("active");
@@ -72,19 +77,30 @@ var updateFunction = function () {
7277
});
7378
};
7479

75-
// Populate sidebar on load
76-
window.addEventListener("load", function () {
77-
var pagetoc = document.getElementsByClassName("pagetoc")[0];
78-
var elements = document.getElementsByClassName("header");
79-
forEach(elements, function (el) {
80-
var link = document.createElement("a");
81-
link.appendChild(document.createTextNode(el.text));
82-
link.href = el.href;
83-
link.classList.add("pagetoc-" + el.parentElement.tagName);
84-
pagetoc.appendChild(link);
80+
var elements = document.getElementsByClassName("header");
81+
82+
if (elements.length > 1) {
83+
// Populate sidebar on load
84+
window.addEventListener("load", function () {
85+
var pagetoc = document.getElementsByClassName("pagetoc")[0];
86+
var elements = document.getElementsByClassName("header");
87+
forEach(elements, function (el) {
88+
var link = document.createElement("a");
89+
link.appendChild(document.createTextNode(el.text));
90+
link.href = el.href;
91+
link.classList.add("pagetoc-" + el.parentElement.tagName);
92+
pagetoc.appendChild(link);
93+
link.onclick = function () {
94+
updateFunction(link);
95+
};
96+
});
97+
updateFunction();
8598
});
86-
updateFunction.call();
87-
});
8899

89-
// Handle active elements on scroll
90-
window.addEventListener("scroll", updateFunction);
100+
// Handle active elements on scroll
101+
window.addEventListener("scroll", function () {
102+
updateFunction();
103+
});
104+
} else {
105+
document.getElementsByClassName("sidetoc")[0].remove();
106+
}

0 commit comments

Comments
 (0)