FIX: Switch to a two-level sidebar list (#25)

This commit is contained in:
Penar Musaraj 2022-01-19 17:17:30 -05:00 committed by GitHub
parent 0bc2aa28a3
commit 8c33f07dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 60 deletions

View File

@ -70,7 +70,6 @@ $padding-basis: 0.75em;
// active line marker // active line marker
$selector: "> ul > li.direct-active > a:before"; $selector: "> ul > li.direct-active > a:before";
$sub: "> ul > li";
$map: ( $map: (
"left": "html:not(.rtl)", "left": "html:not(.rtl)",
"right": "html.rtl", "right": "html.rtl",
@ -91,21 +90,9 @@ html.rtl SELECTOR {
#{$selector} { #{$selector} {
#{$prop}: (-$padding-basis); #{$prop}: (-$padding-basis);
} }
#{$sub} #{$selector} { > ul > li #{$selector} {
#{$prop}: (-$padding-basis) * 2; #{$prop}: (-$padding-basis) * 2;
} }
#{$sub} #{$sub} #{$selector} {
#{$prop}: (-$padding-basis) * 3;
}
#{$sub} #{$sub} #{$sub} #{$selector} {
#{$prop}: (-$padding-basis) * 4;
}
#{$sub} #{$sub} #{$sub} #{$sub} #{$selector} {
#{$prop}: (-$padding-basis) * 5;
}
#{$sub} #{$sub} #{$sub} #{$sub} #{$sub} #{$selector} {
#{$prop}: (-$padding-basis) * 6;
}
} }
} }
// END active line marker // END active line marker

View File

@ -152,10 +152,7 @@ export default {
document.querySelector(".d-toc-wrapper").appendChild(dToc); document.querySelector(".d-toc-wrapper").appendChild(dToc);
} }
const startingLevel = parseInt(headings[0].tagName.substring(1), 10) - 1; const result = this.buildTOC(Array.from(headings));
let result = document.createElement("div");
result.setAttribute("id", "d-toc");
buildTOC(headings, result, startingLevel || 1);
document.querySelector(".d-toc-main").appendChild(result); document.querySelector(".d-toc-main").appendChild(result);
document.addEventListener("click", this.clickTOC, false); document.addEventListener("click", this.clickTOC, false);
document.body.classList.add("d-toc-timeline-visible"); document.body.classList.add("d-toc-timeline-visible");
@ -167,7 +164,10 @@ export default {
} }
// link to each heading // link to each heading
if (e.target.hasAttribute("data-d-toc")) { if (
e.target.closest(".d-toc-item") &&
e.target.hasAttribute("data-d-toc")
) {
const target = `#${e.target.getAttribute("data-d-toc")}`; const target = `#${e.target.getAttribute("data-d-toc")}`;
const scrollTo = domUtils.offset( const scrollTo = domUtils.offset(
document.querySelector(`.d-toc-cooked ${target}`) document.querySelector(`.d-toc-cooked ${target}`)
@ -214,61 +214,63 @@ export default {
document.querySelector(".d-toc-wrapper").classList.remove("overlay"); document.querySelector(".d-toc-wrapper").classList.remove("overlay");
} }
}, },
};
function buildTOC(nodesList, elm, lv = 1) { buildTOC(headings) {
let nodes = Array.from(nodesList); const result = document.createElement("div");
node = nodes.shift(); result.setAttribute("id", "d-toc");
let node; const primaryH = headings[0].tagName;
if (node) { const primaryHeadings = headings.filter((n) => n.tagName === primaryH);
let li, cnt; let nextIndex = headings.length;
let curLv = parseInt(node.tagName.substring(1), 10);
if (curLv === lv) { primaryHeadings.forEach((primaryHeading, index) => {
// same level const ul = document.createElement("ul");
cnt = 0; ul.classList.add("d-toc-heading");
} else if (curLv < lv) {
// walk up then append let li = this.buildItem(primaryHeading);
cnt = 0; ul.appendChild(li);
do {
elm = elm.parentNode.parentNode; const currentIndex = headings.indexOf(primaryHeading);
cnt--; if (primaryHeadings[index + 1]) {
} while (cnt > curLv - lv); nextIndex = headings.indexOf(primaryHeadings[index + 1]);
} else if (curLv > lv) {
// add children
cnt = 0;
do {
li = elm.lastChild;
if (li == null) {
elm = elm.appendChild(document.createElement("ul"));
} else { } else {
elm = li.appendChild(document.createElement("ul")); nextIndex = headings.length;
} }
cnt++;
} while (cnt < curLv - lv);
}
if (curLv === 1 && elm.lastChild === null) {
elm = elm.appendChild(document.createElement("ul"));
}
// append list item
li = elm.appendChild(document.createElement("li")); headings.forEach((heading, subIndex) => {
li.classList.add("d-toc-item"); if (subIndex > currentIndex && subIndex < nextIndex) {
let subUl = li.lastChild;
if (subUl.tagName !== "ul") {
subUl = subUl.appendChild(document.createElement("ul"));
subUl.classList.add("d-toc-sublevel");
li.appendChild(subUl);
}
let subLi = this.buildItem(heading);
subUl.appendChild(subLi);
}
});
result.appendChild(ul);
});
return result;
},
buildItem(node) {
let clonedNode = node.cloneNode(true); let clonedNode = node.cloneNode(true);
clonedNode.querySelector("span.clicks")?.remove(); clonedNode.querySelector("span.clicks")?.remove();
const li = document.createElement("li");
li.classList.add("d-toc-item");
li.innerHTML = `<a data-d-toc="${clonedNode.getAttribute("id")}">${ li.innerHTML = `<a data-d-toc="${clonedNode.getAttribute("id")}">${
clonedNode.textContent clonedNode.textContent
}</a>`; }</a>`;
clonedNode.remove(); clonedNode.remove();
return li;
// recurse },
buildTOC(nodes, elm, lv + cnt); };
}
}
function parentsUntil(el, selector, filter) { function parentsUntil(el, selector, filter) {
const result = []; const result = [];