-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<div {{ attrs.render(class="copy") }}> | ||
Website content copyright © <a href="https://jpscaletti.com">Juan-Pablo Scaletti</a>. | ||
JinjaX and its documentation are licensed under the MIT license. | ||
<p>Website © <a href="https://jpscaletti.com">Juan-Pablo Scaletti</a>.</p> | ||
<p>JinjaX and the documentation text licensed under the MIT license.</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,97 @@ | ||
import { on } from "./jxui.js"; | ||
|
||
const ACTIVE = "active"; | ||
const SEL_TARGET = ".cd-toc-page a"; | ||
const SEL_BACKTOTOP = ".cd-back-to-top" | ||
const SEL_PAGETOC = ".cd-toc-page" | ||
const SEL_TARGET = `${SEL_PAGETOC} a`; | ||
const SEL_ACTIVE = `${SEL_TARGET}.${ACTIVE}`; | ||
const SEL_SECTIONS = "main.page section[id]"; | ||
const SEL_PAGE = "#main.page"; | ||
const SEL_SECTIONS = `${SEL_PAGE} section[id]`; | ||
const DESKTOP_THRESHOLD = 1024; | ||
|
||
on("click", SEL_TARGET, handleClick); | ||
on("click", SEL_BACKTOTOP, backToTop); | ||
|
||
function handleClick (event, target) { | ||
deActivateAll(); | ||
target.classList.add(ACTIVE); | ||
function handleClick(event, target) { | ||
removeHighlight(); | ||
setTimeout(function () { updateHighlight(target) }, 10); | ||
} | ||
|
||
/* =========================================================== */ | ||
function updateHighlight (elem) { | ||
if (window.innerWidth > DESKTOP_THRESHOLD && !elem?.classList.contains(ACTIVE)) { | ||
removeHighlight(); | ||
if (!elem) return; | ||
elem.classList.add(ACTIVE); | ||
} | ||
} | ||
|
||
function deActivateAll() { | ||
function removeHighlight () { | ||
document.querySelectorAll(SEL_ACTIVE).forEach(function (node) { | ||
node.classList.remove(ACTIVE); | ||
}); | ||
} | ||
|
||
function resetNavPosition () { | ||
var pagetoc = document.querySelector(SEL_TOC); | ||
pagetoc?.scroll({ top: 0 }); | ||
} | ||
|
||
export function backToTop () { | ||
window.scrollTo({ top: 0, behavior: "smooth" }); | ||
resetNavPosition(); | ||
} | ||
|
||
export function scrollSpy() { | ||
const labels = {}; | ||
Array.from(document.querySelectorAll(SEL_TARGET)).forEach(function(aNode){ | ||
labels[aNode.href.split("#").slice(-1)] = aNode; | ||
}); | ||
const sections = Array.from(document.querySelectorAll(SEL_SECTIONS)); | ||
|
||
function observe(entries) { | ||
for(const entry of entries) { | ||
if (entry.isIntersecting) { | ||
const aNode = labels[entry.target.id]; | ||
if (aNode) { | ||
deActivateAll(); | ||
aNode.classList.add(ACTIVE) | ||
} | ||
function matchingNavLink(elem) { | ||
if (!elem) return; | ||
var index = sections.indexOf(elem); | ||
|
||
var match; | ||
while (index >= 0 && !match) { | ||
var sectionId = sections[index].getAttribute("id"); | ||
if (sectionId) { | ||
match = document.querySelector(`${SEL_PAGETOC} [href="#${sectionId}"]`); | ||
} | ||
index--; | ||
} | ||
return match; | ||
} | ||
const observer = new IntersectionObserver(observe, {rootMargin: "-50% 0px"}); | ||
const sections = document.querySelectorAll(SEL_SECTIONS); | ||
console.log(labels); | ||
console.log(sections); | ||
for (let i=0; i<sections.length; i++) { | ||
observer.observe(sections[i]); | ||
|
||
function belowBottomHalf(i) { | ||
return i.boundingClientRect.bottom > (i.rootBounds.bottom + i.rootBounds.top) / 2; | ||
} | ||
|
||
function prevElem(elem) { | ||
var index = sections.indexOf(elem); | ||
if (index <= 0) { | ||
return null; | ||
} | ||
return sections[index - 1]; | ||
} | ||
|
||
const PAGE_LOAD_BUFFER = 1000; | ||
|
||
function navHighlight(entries) { | ||
entries.forEach(function (entry) { | ||
if (entry.isIntersecting) { | ||
updateHighlight(matchingNavLink(entry.target)); | ||
} else if (entry.time >= PAGE_LOAD_BUFFER && belowBottomHalf(entry)) { | ||
updateHighlight(matchingNavLink(prevElem(entry.target))); | ||
} | ||
}); | ||
} | ||
|
||
const observer = new IntersectionObserver(navHighlight, { | ||
threshold: 0, | ||
rootMargin: "0% 0px -95% 0px" | ||
}); | ||
|
||
sections.forEach(function (elem) { | ||
observer.observe(elem); | ||
}) | ||
observer.observe(document.querySelector(SEL_PAGE)); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", scrollSpy); |