mirror of
https://github.com/SqrtMinusOne/sqrtminusone.github.io.git
synced 2025-12-10 15:53:03 +03:00
feat(toc): expand/collapse
This commit is contained in:
parent
fb5996755b
commit
e0886d64a5
3 changed files with 62 additions and 18 deletions
|
|
@ -69,6 +69,10 @@ $toc-left-width: $toc-width + $max-width + 25px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
@media(max-width: 578px) {
|
@media(max-width: 578px) {
|
||||||
align-self: center;
|
align-self: center;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@
|
||||||
<b><a href="#">Table of Contents</a></b>
|
<b><a href="#">Table of Contents</a></b>
|
||||||
{{ .TableOfContents }}
|
{{ .TableOfContents }}
|
||||||
</div>
|
</div>
|
||||||
|
<a id="unhide-all-button" class="hidden"><Expand></a>
|
||||||
|
<a id="hide-all-button" class="hidden"><Collapse></a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
const tocId = "TableOfContents";
|
const tocId = "TableOfContents";
|
||||||
const actualContentId = "actual-content";
|
const actualContentId = "actual-content";
|
||||||
|
let showAll = false;
|
||||||
|
let currentActiveLinkId = null;
|
||||||
|
let elemsToHide = [];
|
||||||
|
let linksById = {};
|
||||||
|
|
||||||
let headerObserver;
|
let headerObserver;
|
||||||
|
|
||||||
function observeHeadings() {
|
function observeHeadings() {
|
||||||
const links = document.querySelectorAll(`#${tocId} a`);
|
const links = document.querySelectorAll(`#${tocId} a`);
|
||||||
const headings = document.querySelectorAll(`${actualContentId} h1,h2,h3,h4`);
|
const headings = document.querySelectorAll(`${actualContentId} h1,h2,h3,h4`);
|
||||||
const elemsToHide = [];
|
|
||||||
const linksById = {};
|
|
||||||
|
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
linksById[link.getAttribute("href")] = link;
|
linksById[link.getAttribute("href")] = link;
|
||||||
|
|
@ -19,30 +22,20 @@ function observeHeadings() {
|
||||||
|
|
||||||
headerObserver = new IntersectionObserver(
|
headerObserver = new IntersectionObserver(
|
||||||
(entries) => {
|
(entries) => {
|
||||||
let newActiveLinkId;
|
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
if (entry.isIntersecting && linksById[`#${entry.target.id}`]) {
|
if (entry.isIntersecting && linksById[`#${entry.target.id}`]) {
|
||||||
newActiveLinkId = `#${entry.target.id}`;
|
currentActiveLinkId = `#${entry.target.id}`;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (newActiveLinkId) {
|
if (currentActiveLinkId) {
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
link.classList.remove("active");
|
link.classList.remove("active");
|
||||||
}
|
}
|
||||||
for (const elem of elemsToHide) {
|
linksById[currentActiveLinkId].classList.add("active");
|
||||||
elem.classList.add("hidden");
|
|
||||||
}
|
if (!showAll) {
|
||||||
linksById[newActiveLinkId].classList.add("active");
|
hideHeadings();
|
||||||
for (
|
|
||||||
let elem = linksById[newActiveLinkId];
|
|
||||||
(elem = elem.parentElement);
|
|
||||||
elem.id !== tocId
|
|
||||||
) {
|
|
||||||
elem.classList.remove("hidden");
|
|
||||||
}
|
|
||||||
for (const elem of linksById[newActiveLinkId].parentElement.children) {
|
|
||||||
elem.classList.remove("hidden");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -55,11 +48,56 @@ function observeHeadings() {
|
||||||
for (const heading of headings) {
|
for (const heading of headings) {
|
||||||
headerObserver.observe(heading);
|
headerObserver.observe(heading);
|
||||||
}
|
}
|
||||||
|
hideHeadings();
|
||||||
|
}
|
||||||
|
|
||||||
|
function observeButtons() {
|
||||||
|
const unhideButton = document.getElementById("unhide-all-button");
|
||||||
|
const hideButton = document.getElementById("hide-all-button");
|
||||||
|
unhideButton.addEventListener("click", () => {
|
||||||
|
showAll = true;
|
||||||
|
showHeadings();
|
||||||
|
unhideButton.classList.add("hidden");
|
||||||
|
hideButton.classList.remove("hidden");
|
||||||
|
});
|
||||||
|
hideButton.addEventListener("click", () => {
|
||||||
|
showAll = false;
|
||||||
|
hideHeadings();
|
||||||
|
hideButton.classList.add("hidden");
|
||||||
|
unhideButton.classList.remove("hidden");
|
||||||
|
});
|
||||||
|
unhideButton.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideHeadings() {
|
||||||
|
for (const elem of elemsToHide) {
|
||||||
|
elem.classList.add("hidden");
|
||||||
|
}
|
||||||
|
if (!currentActiveLinkId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (
|
||||||
|
let elem = linksById[currentActiveLinkId];
|
||||||
|
(elem = elem.parentElement);
|
||||||
|
elem.id !== tocId
|
||||||
|
) {
|
||||||
|
elem.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
for (const elem of linksById[currentActiveLinkId].parentElement.children) {
|
||||||
|
elem.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function showHeadings() {
|
||||||
|
for (const elem of elemsToHide) {
|
||||||
|
elem.classList.remove("hidden");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("load", (event) => {
|
window.addEventListener("load", (event) => {
|
||||||
if ("IntersectionObserver" in window) {
|
if ("IntersectionObserver" in window) {
|
||||||
observeHeadings();
|
observeHeadings();
|
||||||
|
observeButtons();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue