From 968a43cdc711c5cc0ee119f76067f429f13fb10e Mon Sep 17 00:00:00 2001 From: MrSpaar Date: Thu, 22 Feb 2024 15:17:34 +0100 Subject: [PATCH] Improved navigation & reduced JS size --- doc/static/markdown_spa.js | 28 ++++++++----------- doc/static/script.js | 7 ++--- .../cli/blank/assets/markdown_spa.js | 28 ++++++++----------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/doc/static/markdown_spa.js b/doc/static/markdown_spa.js index 931d672..2880760 100644 --- a/doc/static/markdown_spa.js +++ b/doc/static/markdown_spa.js @@ -1,17 +1,6 @@ if (typeof preFetch !== 'function') { function preFetch() {} } if (typeof postFetch !== 'function') { function postFetch() {} } -function overrideLinks() { - document.querySelectorAll('a').forEach(a => { - if (a.href.startsWith(window.location.origin) && !a.hasAttribute('onclick')) { - a.onclick = e => { - e.preventDefault(); - updatePage(a.href); - }; - } - }); -} - async function updatePage(path) { preFetch(); @@ -22,15 +11,10 @@ async function updatePage(path) { document.documentElement.innerHTML = text; } - overrideLinks(); postFetch(); - - window.history.pushState({}, '', path); } postFetch(); -overrideLinks(); - let curPath = window.location.pathname; window.addEventListener('popstate', async _ => { @@ -41,3 +25,15 @@ window.addEventListener('popstate', async _ => { curPath = window.location.pathname; await updatePage(window.location.href); }); + +window.addEventListener('click', e => { + if (!e.target.hasAttribute("href")) { + return; + } + + if (e.target.href.startsWith(window.location.origin)) { + e.preventDefault(); + updatePage(e.target.href); + window.history.pushState({}, '', e.target.href); + } +}) diff --git a/doc/static/script.js b/doc/static/script.js index 487bc8c..73e0aa5 100644 --- a/doc/static/script.js +++ b/doc/static/script.js @@ -1,9 +1,5 @@ let loader = document.getElementById('loader'); - -document.getElementById('show-nav-label').onkeydown = e => { - if (e.key == 'Enter') - e.target.children[0].click(); -}; +let input = document.getElementById('show-nav') function preFetch() { loader.classList.add('active'); @@ -28,4 +24,5 @@ function postFetch() { } loader.classList.remove('active'); + input.checked = false; } diff --git a/src/markdown_spa/cli/blank/assets/markdown_spa.js b/src/markdown_spa/cli/blank/assets/markdown_spa.js index 931d672..2880760 100644 --- a/src/markdown_spa/cli/blank/assets/markdown_spa.js +++ b/src/markdown_spa/cli/blank/assets/markdown_spa.js @@ -1,17 +1,6 @@ if (typeof preFetch !== 'function') { function preFetch() {} } if (typeof postFetch !== 'function') { function postFetch() {} } -function overrideLinks() { - document.querySelectorAll('a').forEach(a => { - if (a.href.startsWith(window.location.origin) && !a.hasAttribute('onclick')) { - a.onclick = e => { - e.preventDefault(); - updatePage(a.href); - }; - } - }); -} - async function updatePage(path) { preFetch(); @@ -22,15 +11,10 @@ async function updatePage(path) { document.documentElement.innerHTML = text; } - overrideLinks(); postFetch(); - - window.history.pushState({}, '', path); } postFetch(); -overrideLinks(); - let curPath = window.location.pathname; window.addEventListener('popstate', async _ => { @@ -41,3 +25,15 @@ window.addEventListener('popstate', async _ => { curPath = window.location.pathname; await updatePage(window.location.href); }); + +window.addEventListener('click', e => { + if (!e.target.hasAttribute("href")) { + return; + } + + if (e.target.href.startsWith(window.location.origin)) { + e.preventDefault(); + updatePage(e.target.href); + window.history.pushState({}, '', e.target.href); + } +})