Skip to content

Commit

Permalink
fix: don't show single-element toc, handle clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Jan 5, 2024
1 parent 6335050 commit d0ede41
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
4 changes: 0 additions & 4 deletions docs/theme/pagetoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
--toc-width: 310px;
}

a[class^="pagetoc-H"]:only-child {
display: none;
}

.nav-chapters {
min-width: 50px;
}
Expand Down
80 changes: 48 additions & 32 deletions docs/theme/pagetoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function forPagetocElem(fun) {
forEach(getPagetocElems(), fun);
}

var activeHref = location.href;

function getRect(element) {
return element.getBoundingClientRect();
}
Expand All @@ -25,32 +23,39 @@ function overflowBottom(container, element) {
return getRect(container).bottom - getRect(element).bottom;
}

var updateFunction = function () {
var id = undefined;
var activeHref = location.href;

var updateFunction = function (elem = undefined) {
var id = elem;

if (location.href != activeHref) {
if (!elem && location.href != activeHref) {
activeHref = location.href;
forPagetocElem(function (el) {
if (el.href == activeHref) {
if (el.href === activeHref) {
id = el;
}
});
}

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

forEach(elements, function (el, i, arr) {
if (id === undefined && getRect(el).bottom >= menuBottom) {
if (getRect(el).top >= contentCenter + margin) {
id = arr[Math.max(0, i - 1)];
} else {
id = el;
if (!id) {
var elements = document.getElementsByClassName("header");
let menuBottom = getRect(document.getElementById("menu-bar")).bottom;
let contentCenter = window.innerHeight / 2;
let margin = contentCenter / 3;

forEach(elements, function (el, i, arr) {
if (id === undefined && getRect(el).bottom >= menuBottom) {
if (getRect(el).top >= contentCenter + margin) {
id = arr[Math.max(0, i - 1)];
} else {
id = el;
}
}
}
});
});
}

forPagetocElem(function (el) {
el.classList.remove("active");
Expand All @@ -72,19 +77,30 @@ var updateFunction = function () {
});
};

// Populate sidebar on load
window.addEventListener("load", function () {
var pagetoc = document.getElementsByClassName("pagetoc")[0];
var elements = document.getElementsByClassName("header");
forEach(elements, function (el) {
var link = document.createElement("a");
link.appendChild(document.createTextNode(el.text));
link.href = el.href;
link.classList.add("pagetoc-" + el.parentElement.tagName);
pagetoc.appendChild(link);
var elements = document.getElementsByClassName("header");

if (elements.length > 1) {
// Populate sidebar on load
window.addEventListener("load", function () {
var pagetoc = document.getElementsByClassName("pagetoc")[0];
var elements = document.getElementsByClassName("header");
forEach(elements, function (el) {
var link = document.createElement("a");
link.appendChild(document.createTextNode(el.text));
link.href = el.href;
link.classList.add("pagetoc-" + el.parentElement.tagName);
pagetoc.appendChild(link);
link.onclick = function () {
updateFunction(link);
};
});
updateFunction();
});
updateFunction.call();
});

// Handle active elements on scroll
window.addEventListener("scroll", updateFunction);
// Handle active elements on scroll
window.addEventListener("scroll", function () {
updateFunction();
});
} else {
document.getElementsByClassName("sidetoc")[0].remove();
}

0 comments on commit d0ede41

Please sign in to comment.