Skip to content

Commit

Permalink
Fix navigation. (#2409)
Browse files Browse the repository at this point in the history
When left or right arrow key is held down the you will quickly navigate to next or previous chapters.
  • Loading branch information
zGadli authored Jul 7, 2024
1 parent 373dc9c commit ca918a2
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions lncrawl/assets/web/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,59 @@ function goToHref(el) {
window.location.href = href;
}

window.addEventListener("keyup", function (evt) {
clearInterval(keyPressTimer);
function startNavigation(direction) {
if (direction === "left") {
keyPressTimer = setTimeout(() => {
navigationInterval = setInterval(() => {
goToHref(document.querySelector("a.prev-button"));
}, 50);
}, 100);
} else if (direction === "right") {
keyPressTimer = setTimeout(() => {
navigationInterval = setInterval(() => {
goToHref(document.querySelector("a.next-button"));
}, 50);
}, 100);
}
}

function stopNavigation() {
clearTimeout(keyPressTimer);
clearInterval(navigationInterval);
}

window.addEventListener("keydown", function (evt) {
switch (evt.key) {
case "ArrowLeft":
goToHref(document.querySelector("a.prev-button"));
if (!keyPressTimer && !navigationInterval) {
startNavigation("left");
}
break;
case "ArrowRight":
goToHref(document.querySelector("a.next-button"));
if (!keyPressTimer && !navigationInterval) {
startNavigation("right");
}
break;
default:
break;
}
});

window.addEventListener("keydown", function (evt) {
clearInterval(keyPressTimer);
clearInterval(navigationInterval);
window.addEventListener("keyup", function (evt) {
stopNavigation();
switch (evt.key) {
case "ArrowLeft":
keyPressTimer = setTimeout(() => {
navigationInterval = setInterval(() => {
goToHref(document.querySelector("a.prev-button"));
}, 50);
}, 450);
goToHref(document.querySelector("a.prev-button"));
break;
case "ArrowRight":
keyPressTimer = setTimeout(() => {
navigationInterval = setInterval(() => {
goToHref(document.querySelector("a.next-button"));
}, 50);
}, 450);
goToHref(document.querySelector("a.next-button"));
break;
default:
break;
}
});


// Handle next TOC select
function addTocSelectListener() {
document.querySelectorAll("select.toc").forEach((select) => {
Expand Down

0 comments on commit ca918a2

Please sign in to comment.