Skip to content

Commit

Permalink
Improved navigation & reduced JS size
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSpaar committed Feb 22, 2024
1 parent e66e196 commit 968a43c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
28 changes: 12 additions & 16 deletions doc/static/markdown_spa.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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 _ => {
Expand All @@ -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);
}
})
7 changes: 2 additions & 5 deletions doc/static/script.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -28,4 +24,5 @@ function postFetch() {
}

loader.classList.remove('active');
input.checked = false;
}
28 changes: 12 additions & 16 deletions src/markdown_spa/cli/blank/assets/markdown_spa.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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 _ => {
Expand All @@ -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);
}
})

0 comments on commit 968a43c

Please sign in to comment.