refactor: enhance LanguageSwitcher and PhotoSwipe initialization

- Simplified LanguageSwitcher logic for better readability and performance.
- Updated PhotoSwipe initialization to clean up existing instances before creating a new one, ensuring smoother transitions.
- Changed event listener for PhotoSwipe to trigger on page load, improving responsiveness.
- Revised theme configuration description and updated site URL for clarity.
This commit is contained in:
radishzzz 2025-01-24 13:23:24 +00:00
parent bca8b9b1cf
commit ee35006f7c
3 changed files with 11 additions and 13 deletions

View file

@ -35,11 +35,9 @@ document.addEventListener('astro:page-load', () => {
const firstSegment = segments[0] || ''
// Check if first segment is a valid language code
const currentLang = langs.includes(firstSegment) ? firstSegment : ''
// Get next language in rotation (empty string means default locale)
const currentIndex = langs.indexOf(currentLang)
const nextLang = langs[(currentIndex + 1) % langs.length]
const newPath = buildNewPath(currentLang, nextLang, segments, pathname) || '/'
window.location.href = `${newPath}${search}${hash}`
})

View file

@ -8,6 +8,9 @@
// Initialize PhotoSwipe lightbox with custom configuration
function createPhotoSwipe() {
// Clean up existing instance if any
cleanup()
lightbox = new PhotoSwipeLightbox({
gallery: 'article img',
pswpModule: () => pswp,
@ -43,10 +46,7 @@
}
}
// Initialize PhotoSwipe when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
createPhotoSwipe()
})
// Initialize PhotoSwipe when DOM is ready and after page transitions
document.addEventListener('astro:page-load', createPhotoSwipe)
document.addEventListener('astro:before-swap', cleanup)
</script>
</script>