Skip to content

Commit

Permalink
Fix toc (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
solstice23 authored Feb 6, 2024
1 parent 91c5994 commit f120ee2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class Router {
this.onBeforeNavigate = onBeforeNavigate;
this.onAfterNavigate = onAfterNavigate;
this.cache = {};
this.oldURL = window.location.href;
document.addEventListener('click', this.clickHandler.bind(this));
window.addEventListener('hashchange', this.hashChangeHandler.bind(this));
window.addEventListener('popstate', this.popStateHandler.bind(this));
window.addEventListener('DOMContentLoaded', () => {
this.hashChangeHandler(null, true);
});
}

async clickHandler(e) {
Expand All @@ -30,9 +35,17 @@ class Router {
}
if (new URL(url).pathname.replace(/\/$/, '') === location.pathname.replace(/\/$/, '')) {
e.preventDefault();
const oldHash = window.location.hash;
const newHash = new URL(url).hash;
if (oldHash !== newHash) {
e.preventDefault();
history.pushState(null, null, newHash);
this.hashChangeHandler();
}
return;
}
e.preventDefault();
this.oldURL = window.location.href;
await this.navigate(url);
}

Expand Down Expand Up @@ -94,6 +107,7 @@ class Router {
// run onAfterNavigate
if (this.onAfterNavigate) this.onAfterNavigate();
})
this.oldURL = url;
try {
await transition.finished;
}
Expand All @@ -104,6 +118,10 @@ class Router {
}

async popStateHandler(e) {
if (new URL(location.href).pathname.replace(/\/$/, '') === new URL(this.oldURL).pathname.replace(/\/$/, '')) {
// popstate ignored, because it is a hash change
return;
}
if (e.state) {
await this.loadHTML(
location.href,
Expand All @@ -119,11 +137,30 @@ class Router {
}
}

async hashChangeHandler(e, instantScroll = false) {
console.log('hashChangeHandler');
document.documentElement.scrollTo(0, 0);
document.body?.scrollTo(0, 0);
const hash = document.location.hash;
if (!hash) return;
const target = document.querySelector(hash);
if (!target) return;
const container = target.closest('#container');
if (!container) return;
document.querySelectorAll('.hash-target').forEach(e => e.classList.remove('hash-target'));
target.classList.add('hash-target');
const scroll = target.getBoundingClientRect().top + container.scrollTop - 60;
container.scrollTo({ top: scroll, behavior: instantScroll ? 'auto' : 'smooth' });
}

static init(onBeforeNavigate = null, onAfterNavigate = null) {
new Router(onBeforeNavigate, onAfterNavigate);
}
}
document.addEventListener('scroll', () => {
document.documentElement.scrollTo(0, 0);
document.body?.scrollTo(0, 0);
});

function transitionPolyfill(url) {
if (supportViewTransition) return;
Expand Down
30 changes: 30 additions & 0 deletions assets/sass/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@
padding: 25px 35px;
background-color: var(--main-card-background-colour);
//line-height: 1.8;

h1, h2, h3, h4, h5, h6 {
position: relative;
&:target, &.hash-target {
&::before {
content: '';
display: block;
top: 50%;
left: -20px;
height: 0.3em;
width: 0.3em;
border: 4px solid var(--accent-colour);
border-top: none;
border-left: none;
transform: translateY(-50%) rotate(-45deg) ;
position: absolute;
pointer-events: none;
animation: target-hash-arrow-move 0.8s ease infinite, target-hash-arrow-fade-out 0.5s 1.6s forwards;
@keyframes target-hash-arrow-move {
0% { left: -25px; }
50% { left: -20px; }
100% { left: -25px; }
}
@keyframes target-hash-arrow-fade-out {
0% { opacity: 1; }
100% { opacity: 0; }
}
}
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions assets/sass/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ html {
background-size: cover;
background-repeat: repeat-y;
height: 100%;
overflow: hidden;
}

body {
Expand Down

0 comments on commit f120ee2

Please sign in to comment.