FIX: Switch to a two-level sidebar list (#25)
This commit is contained in:
parent
0bc2aa28a3
commit
8c33f07dc5
|
@ -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
|
||||||
|
|
|
@ -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) {
|
} else {
|
||||||
// add children
|
nextIndex = headings.length;
|
||||||
cnt = 0;
|
}
|
||||||
do {
|
|
||||||
li = elm.lastChild;
|
headings.forEach((heading, subIndex) => {
|
||||||
if (li == null) {
|
if (subIndex > currentIndex && subIndex < nextIndex) {
|
||||||
elm = elm.appendChild(document.createElement("ul"));
|
let subUl = li.lastChild;
|
||||||
} else {
|
if (subUl.tagName !== "ul") {
|
||||||
elm = li.appendChild(document.createElement("ul"));
|
subUl = subUl.appendChild(document.createElement("ul"));
|
||||||
|
subUl.classList.add("d-toc-sublevel");
|
||||||
|
li.appendChild(subUl);
|
||||||
|
}
|
||||||
|
|
||||||
|
let subLi = this.buildItem(heading);
|
||||||
|
subUl.appendChild(subLi);
|
||||||
}
|
}
|
||||||
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"));
|
result.appendChild(ul);
|
||||||
li.classList.add("d-toc-item");
|
});
|
||||||
|
|
||||||
|
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 = [];
|
||||||
|
|
Loading…
Reference in New Issue