js: Update range highlight on manual hash change

This commit is contained in:
Franciszek Stachura 2024-10-14 22:58:21 +02:00 committed by Théo Lebrun
parent 54bd3bc0a4
commit 9a09df90e9

View file

@ -166,6 +166,13 @@ function highlightFromTo(firstLine, lastLine) {
addClassToRangeOfElements(firstCodeLine, lastCodeLine, "line-highlight");
}
function clearRangeHighlight() {
const highlightElements = Array.from(document.getElementsByClassName("line-highlight"));
for (let el of highlightElements) {
el.classList.remove("line-highlight");
}
}
function addClassToRangeOfElements(first, last, class_name) {
let element = first;
const elementAfterLast = last !== null ? last.nextElementSibling : null;
@ -187,18 +194,26 @@ function setupLineRangeHandlers() {
let rangeStartLine, rangeEndLine;
const highlightedRange = parseLineRangeAnchor(window.location.hash);
// Set range start/end to elements from hash
if (highlightedRange !== undefined) {
rangeStartLine = highlightedRange[0];
rangeEndLine = highlightedRange[1];
highlightFromTo(rangeStartLine, rangeEndLine);
document.getElementById(`L${rangeStartLine}`).scrollIntoView();
} else if (location.hash !== "" && location.hash[1] === "L") {
const lineNum = parseLineId(location.hash.substring(1));
rangeStartLine = document.getElementById(`L${lineNum}`);
const parseFromHash = () => {
const highlightedRange = parseLineRangeAnchor(window.location.hash);
// Set range start/end to elements from hash
if (highlightedRange !== undefined) {
rangeStartLine = highlightedRange[0];
rangeEndLine = highlightedRange[1];
highlightFromTo(rangeStartLine, rangeEndLine);
document.getElementById(`L${rangeStartLine}`).scrollIntoView();
} else if (location.hash !== "" && location.hash[1] === "L") {
rangeStartLine = parseLineId(location.hash.substring(1));
}
}
window.addEventListener("hashchange", _ => {
clearRangeHighlight();
parseFromHash();
});
parseFromHash();
linenodiv.addEventListener("click", ev => {
if (ev.ctrlKey || ev.metaKey) {
return;
@ -212,11 +227,7 @@ function setupLineRangeHandlers() {
return;
}
// Remove range highlight
const highlightElements = Array.from(document.getElementsByClassName("line-highlight"));
for (let el of highlightElements) {
el.classList.remove("line-highlight");
}
clearRangeHighlight();
if (rangeStartLine === undefined || !ev.shiftKey) {
rangeStartLine = parseLineId(el.id);