|
|
@ -1,6 +1,12 @@ |
|
|
|
let sideScrollingSelectors = ["pre.sourceCode"] |
|
|
|
|
|
|
|
window.addEventListener("load", () => { |
|
|
|
writeCopyrightYear() |
|
|
|
addCodeScrolling() |
|
|
|
sideScrollingSelectors.forEach(updateScrollShadowsSelector) |
|
|
|
}) |
|
|
|
|
|
|
|
window.addEventListener("resize", () => { |
|
|
|
sideScrollingSelectors.forEach(updateScrollShadowsSelector) |
|
|
|
}) |
|
|
|
|
|
|
|
function writeCopyrightYear() { |
|
|
@ -12,28 +18,58 @@ function writeCopyrightYear() { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function addCodeScrolling() { |
|
|
|
document.querySelectorAll("pre.sourceCode").forEach(wrapper => { |
|
|
|
let content = wrapper.querySelector("code.sourceCode") |
|
|
|
let contentScrollWidth = content.scrollWidth - wrapper.offsetWidth |
|
|
|
if (contentScrollWidth <= 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
function updateScrollShadowsSelector(selector) { |
|
|
|
document.querySelectorAll(selector).forEach(wrapper => updateScrollShadows(wrapper)) |
|
|
|
} |
|
|
|
|
|
|
|
function updateScrollShadows(wrapper) { |
|
|
|
// identity the scrolling content within the wrapper
|
|
|
|
let contentQuery = wrapper.querySelector(".scroll-content") |
|
|
|
let content |
|
|
|
if (contentQuery) { |
|
|
|
content = contentQuery |
|
|
|
} else { |
|
|
|
content = wrapper.firstChild |
|
|
|
content.classList.add("scroll-content") |
|
|
|
} |
|
|
|
|
|
|
|
let leftShadow = document.createElement("div") |
|
|
|
leftShadow.classList.add("shadow", "shadow-left") |
|
|
|
wrapper.appendChild(leftShadow) |
|
|
|
let contentScrollWidth = content.scrollWidth - wrapper.offsetWidth |
|
|
|
// remove shadows if no scrolling is needed
|
|
|
|
if (contentScrollWidth <= 0) { |
|
|
|
wrapper.querySelectorAll(".shadow").forEach(x => x.remove()) |
|
|
|
delete wrapper.updateScrollShadows |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
let rightShadow = document.createElement("div") |
|
|
|
rightShadow.classList.add("shadow", "shadow-right") |
|
|
|
wrapper.appendChild(rightShadow) |
|
|
|
// skip if scroll shadows already added
|
|
|
|
if (wrapper.updateScrollShadows) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
function setScroll() { |
|
|
|
let currentScroll = content.scrollLeft / contentScrollWidth |
|
|
|
leftShadow.style.opacity = currentScroll |
|
|
|
rightShadow.style.opacity = 1 - currentScroll |
|
|
|
// create scroll shadows
|
|
|
|
let [leftShadow, rightShadow] = ["left", "right"].map(side => { |
|
|
|
let shadowQuery = wrapper.querySelector(`.shadow-${side}`) |
|
|
|
let shadow |
|
|
|
if (shadowQuery) { |
|
|
|
shadow = shadowQuery |
|
|
|
} else { |
|
|
|
shadow = document.createElement("div") |
|
|
|
shadow.classList.add("shadow", `shadow-${side}`) |
|
|
|
wrapper.appendChild(shadow) |
|
|
|
} |
|
|
|
return shadow |
|
|
|
}) |
|
|
|
// add method to update shadow opacity relative to scroll
|
|
|
|
wrapper.updateScrollShadows = () => { |
|
|
|
let currentScroll = content.scrollLeft / contentScrollWidth |
|
|
|
leftShadow.style.opacity = currentScroll |
|
|
|
rightShadow.style.opacity = 1 - currentScroll |
|
|
|
} |
|
|
|
// attach event listener to update shadows
|
|
|
|
content.addEventListener("scroll", function () { |
|
|
|
if (wrapper.updateScrollShadows) { |
|
|
|
wrapper.updateScrollShadows() |
|
|
|
} |
|
|
|
setScroll(); |
|
|
|
content.addEventListener("scroll", setScroll); |
|
|
|
}) |
|
|
|
wrapper.updateScrollShadows() |
|
|
|
} |
|
|
|