import * as params from '@params'; function initializeMenu() { let menu = document.getElementById('menu') if (menu) { menu.scrollLeft = localStorage.getItem("menu-scroll-position"); menu.onscroll = function () { localStorage.setItem("menu-scroll-position", menu.scrollLeft); } } document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener("click", function (e) { e.preventDefault(); var id = this.getAttribute("href").substr(1); if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) { document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({ behavior: "smooth" }); } else { document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView(); } if (id === "top") { history.replaceState(null, null, " "); } else { history.pushState(null, null, `#${id}`); } }); }); } function scrollToTop() { var mybutton = document.getElementById("top-link"); window.onscroll = function () { if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) { mybutton.style.visibility = "visible"; mybutton.style.opacity = "1"; } else { mybutton.style.visibility = "hidden"; mybutton.style.opacity = "0"; } }; } function themeToggle() { var themeButton = document.getElementById("theme-toggle"); themeButton.addEventListener("click", () => { if (document.body.className.includes("dark")) { document.body.classList.remove('dark'); localStorage.setItem("pref-theme", 'light'); } else { document.body.classList.add('dark'); localStorage.setItem("pref-theme", 'dark'); } }) } function showCodeCopyButtons() { document.querySelectorAll('pre > code').forEach((codeblock) => { const container = codeblock.parentNode.parentNode; const copybutton = document.createElement('button'); copybutton.classList.add('copy-code'); copybutton.innerHTML = 'copy'; function copyingDone() { copybutton.innerHTML = 'copied!'; setTimeout(() => { copybutton.innerHTML = 'copy'; }, 2000); } copybutton.addEventListener('click', (cb) => { if ('clipboard' in navigator) { navigator.clipboard.writeText(codeblock.textContent); copyingDone(); return; } const range = document.createRange(); range.selectNodeContents(codeblock); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); try { document.execCommand('copy'); copyingDone(); } catch (e) { }; selection.removeRange(range); }); if (container.classList.contains("highlight")) { container.appendChild(copybutton); } else if (container.parentNode.firstChild == container) { // td containing LineNos } else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") { // table containing LineNos and code codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton); } else { // code blocks not having highlight as parent class codeblock.parentNode.appendChild(copybutton); } }); } function openToc() { var toc = document.getElementById("toc"); if (window.innerWidth > 1500 && window.innerHeight > 800) { toc.open = true; } } function progressBar() { var bar = document.getElementById("progressBar"); document.addEventListener('scroll', () => { var scrollPercent = document.documentElement.scrollTop / (document.documentElement.scrollHeight - window.innerHeight) * 100 + '%'; bar.style.setProperty("--scrollAmount", scrollPercent); }) } initializeMenu(); if (params.scrollToTop) scrollToTop(); if (params.themeToggle) themeToggle(); if (params.showCodeCopyButtons) showCodeCopyButtons(); if (document.getElementById("toc")) openToc(); if (document.getElementById("progressBar")) progressBar();