From d0ede4169daec0f18d6ccbeeb8b4d6b18939014f Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Fri, 5 Jan 2024 15:40:42 +0300 Subject: [PATCH] fix: don't show single-element toc, handle clicks --- docs/theme/pagetoc.css | 4 --- docs/theme/pagetoc.js | 80 +++++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/docs/theme/pagetoc.css b/docs/theme/pagetoc.css index 5d0c4ace..8ae85dee 100644 --- a/docs/theme/pagetoc.css +++ b/docs/theme/pagetoc.css @@ -5,10 +5,6 @@ --toc-width: 310px; } -a[class^="pagetoc-H"]:only-child { - display: none; -} - .nav-chapters { min-width: 50px; } diff --git a/docs/theme/pagetoc.js b/docs/theme/pagetoc.js index 2ff8dcc4..f44aa1a8 100644 --- a/docs/theme/pagetoc.js +++ b/docs/theme/pagetoc.js @@ -11,8 +11,6 @@ function forPagetocElem(fun) { forEach(getPagetocElems(), fun); } -var activeHref = location.href; - function getRect(element) { return element.getBoundingClientRect(); } @@ -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"); @@ -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(); +}