282 lines
9.0 KiB
Plaintext
282 lines
9.0 KiB
Plaintext
---
|
|
import { UI } from 'astrowind:config';
|
|
---
|
|
|
|
<script is:inline define:vars={{ defaultTheme: UI.theme }}>
|
|
if (window.basic_script) {
|
|
return;
|
|
}
|
|
|
|
window.basic_script = true;
|
|
|
|
function applyTheme(theme) {
|
|
if (theme === 'dark') {
|
|
document.documentElement.classList.add('dark');
|
|
} else {
|
|
document.documentElement.classList.remove('dark');
|
|
}
|
|
}
|
|
|
|
const initTheme = function () {
|
|
if ((defaultTheme && defaultTheme.endsWith(':only')) || (!localStorage.theme && defaultTheme !== 'system')) {
|
|
applyTheme(defaultTheme.replace(':only', ''));
|
|
} else if (
|
|
localStorage.theme === 'dark' ||
|
|
(!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)
|
|
) {
|
|
applyTheme('dark');
|
|
} else {
|
|
applyTheme('light');
|
|
}
|
|
};
|
|
initTheme();
|
|
|
|
function attachEvent(selector, event, fn) {
|
|
const matches = typeof selector === 'string' ? document.querySelectorAll(selector) : selector;
|
|
if (matches && matches.length) {
|
|
matches.forEach((elem) => {
|
|
elem.addEventListener(event, (e) => fn(e, elem), false);
|
|
});
|
|
}
|
|
}
|
|
|
|
const onLoad = function () {
|
|
let lastKnownScrollPosition = window.scrollY;
|
|
let ticking = true;
|
|
|
|
attachEvent('#header nav', 'click', function () {
|
|
document.querySelector('[data-aw-toggle-menu]')?.classList.remove('expanded');
|
|
document.body.classList.remove('overflow-hidden');
|
|
document.getElementById('header')?.classList.remove('h-screen');
|
|
document.getElementById('header')?.classList.remove('expanded');
|
|
document.getElementById('header')?.classList.remove('bg-page');
|
|
document.querySelector('#header nav')?.classList.add('hidden');
|
|
document.querySelector('#header > div > div:last-child')?.classList.add('hidden');
|
|
});
|
|
|
|
attachEvent('[data-aw-toggle-menu]', 'click', function (_, elem) {
|
|
elem.classList.toggle('expanded');
|
|
document.body.classList.toggle('overflow-hidden');
|
|
document.getElementById('header')?.classList.toggle('h-screen');
|
|
document.getElementById('header')?.classList.toggle('expanded');
|
|
document.getElementById('header')?.classList.toggle('bg-page');
|
|
document.querySelector('#header nav')?.classList.toggle('hidden');
|
|
document.querySelector('#header > div > div:last-child')?.classList.toggle('hidden');
|
|
});
|
|
|
|
attachEvent('[data-aw-toggle-color-scheme]', 'click', function () {
|
|
if (defaultTheme.endsWith(':only')) {
|
|
return;
|
|
}
|
|
document.documentElement.classList.toggle('dark');
|
|
localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
|
|
});
|
|
|
|
attachEvent('[data-aw-social-share]', 'click', function (_, elem) {
|
|
const network = elem.getAttribute('data-aw-social-share');
|
|
const url = encodeURIComponent(elem.getAttribute('data-aw-url'));
|
|
const text = encodeURIComponent(elem.getAttribute('data-aw-text'));
|
|
|
|
let href;
|
|
switch (network) {
|
|
case 'facebook':
|
|
href = `https://www.facebook.com/sharer.php?u=${url}`;
|
|
break;
|
|
case 'twitter':
|
|
href = `https://twitter.com/intent/tweet?url=${url}&text=${text}`;
|
|
break;
|
|
case 'linkedin':
|
|
href = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${text}`;
|
|
break;
|
|
case 'whatsapp':
|
|
href = `https://wa.me/?text=${text}%20${url}`;
|
|
break;
|
|
case 'mail':
|
|
href = `mailto:?subject=%22${text}%22&body=${text}%20${url}`;
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
const newlink = document.createElement('a');
|
|
newlink.target = '_blank';
|
|
newlink.href = href;
|
|
newlink.click();
|
|
});
|
|
|
|
const screenSize = window.matchMedia('(max-width: 767px)');
|
|
screenSize.addEventListener('change', function () {
|
|
document.querySelector('[data-aw-toggle-menu]')?.classList.remove('expanded');
|
|
document.body.classList.remove('overflow-hidden');
|
|
document.getElementById('header')?.classList.remove('h-screen');
|
|
document.getElementById('header')?.classList.remove('expanded');
|
|
document.getElementById('header')?.classList.remove('bg-page');
|
|
document.querySelector('#header nav')?.classList.add('hidden');
|
|
document.querySelector('#header > div > div:last-child')?.classList.add('hidden');
|
|
});
|
|
|
|
function applyHeaderStylesOnScroll() {
|
|
const header = document.querySelector('#header[data-aw-sticky-header]');
|
|
if (!header) return;
|
|
if (lastKnownScrollPosition > 60 && !header.classList.contains('scroll')) {
|
|
header.classList.add('scroll');
|
|
} else if (lastKnownScrollPosition <= 60 && header.classList.contains('scroll')) {
|
|
header.classList.remove('scroll');
|
|
}
|
|
ticking = false;
|
|
}
|
|
applyHeaderStylesOnScroll();
|
|
|
|
attachEvent([document], 'scroll', function () {
|
|
lastKnownScrollPosition = window.scrollY;
|
|
|
|
if (!ticking) {
|
|
window.requestAnimationFrame(() => {
|
|
applyHeaderStylesOnScroll();
|
|
});
|
|
ticking = true;
|
|
}
|
|
});
|
|
};
|
|
const onPageShow = function () {
|
|
document.documentElement.classList.add('motion-safe:scroll-smooth');
|
|
const elem = document.querySelector('[data-aw-toggle-menu]');
|
|
if (elem) {
|
|
elem.classList.remove('expanded');
|
|
}
|
|
document.body.classList.remove('overflow-hidden');
|
|
document.getElementById('header')?.classList.remove('h-screen');
|
|
document.getElementById('header')?.classList.remove('expanded');
|
|
document.querySelector('#header nav')?.classList.add('hidden');
|
|
};
|
|
|
|
window.onload = onLoad;
|
|
window.onpageshow = onPageShow;
|
|
|
|
document.addEventListener('astro:after-swap', () => {
|
|
initTheme();
|
|
onLoad();
|
|
onPageShow();
|
|
});
|
|
</script>
|
|
|
|
<script is:inline>
|
|
/* Inspired by: https://github.com/heidkaemper/tailwindcss-intersect */
|
|
const Observer = {
|
|
observers: {},
|
|
visibleElementsQueue: [],
|
|
processing: false,
|
|
delayBetweenAnimations: 100,
|
|
|
|
start() {
|
|
const selectors = [
|
|
'[class*=" intersect:"]',
|
|
'[class*=":intersect:"]',
|
|
'[class^="intersect:"]',
|
|
'[class="intersect"]',
|
|
'[class*=" intersect "]',
|
|
'[class^="intersect "]',
|
|
'[class$=" intersect"]',
|
|
];
|
|
|
|
const elements = Array.from(document.querySelectorAll(selectors.join(',')));
|
|
|
|
elements.forEach((el) => el.setAttribute('no-intersect', ''));
|
|
|
|
const getThreshold = (element) => {
|
|
if (element.classList.contains('intersect-full')) return 0.99;
|
|
if (element.classList.contains('intersect-half')) return 0.5;
|
|
if (element.classList.contains('intersect-quarter')) return 0.25;
|
|
return 0;
|
|
};
|
|
|
|
Object.values(this.observers).forEach((observer) => observer.disconnect());
|
|
this.observers = {};
|
|
|
|
const callback = (entries) => {
|
|
entries.forEach((entry) => {
|
|
const target = entry.target;
|
|
|
|
if (entry.isIntersecting) {
|
|
if (target.classList.contains('intercept-no-queue')) {
|
|
target.removeAttribute('no-intersect');
|
|
if (target.classList.contains('intersect-once')) {
|
|
Object.values(this.observers).forEach((observer) => observer.unobserve(target));
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (!this.visibleElementsQueue.includes(target)) {
|
|
this.visibleElementsQueue.push(target);
|
|
}
|
|
|
|
this.processQueue();
|
|
} else {
|
|
target.setAttribute('no-intersect', '');
|
|
|
|
const index = this.visibleElementsQueue.indexOf(target);
|
|
if (index > -1) {
|
|
this.visibleElementsQueue.splice(index, 1);
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
elements.forEach((el) => {
|
|
const threshold = getThreshold(el);
|
|
|
|
if (!this.observers[threshold]) {
|
|
this.observers[threshold] = new IntersectionObserver(callback, { threshold });
|
|
}
|
|
|
|
this.observers[threshold].observe(el);
|
|
});
|
|
},
|
|
|
|
async processQueue() {
|
|
if (this.processing) {
|
|
return;
|
|
}
|
|
|
|
this.processing = true;
|
|
|
|
while (this.visibleElementsQueue.length > 0) {
|
|
const element = this.visibleElementsQueue.shift();
|
|
|
|
element.removeAttribute('no-intersect');
|
|
|
|
if (element.classList.contains('intersect-once')) {
|
|
Object.values(this.observers).forEach((observer) => observer.unobserve(element));
|
|
}
|
|
|
|
if (this.isElementInViewport(element)) {
|
|
await this.delay(this.delayBetweenAnimations);
|
|
}
|
|
}
|
|
|
|
this.processing = false;
|
|
},
|
|
|
|
delay(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
},
|
|
|
|
isElementInViewport(element) {
|
|
const rect = element.getBoundingClientRect();
|
|
return (
|
|
rect.top < (window.innerHeight || document.documentElement.clientHeight) &&
|
|
rect.bottom > 0 &&
|
|
rect.left < (window.innerWidth || document.documentElement.clientWidth) &&
|
|
rect.right > 0
|
|
);
|
|
},
|
|
};
|
|
|
|
Observer.start();
|
|
|
|
document.addEventListener('astro:after-swap', () => {
|
|
Observer.start();
|
|
});
|
|
</script>
|