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>

View file

@ -5,9 +5,9 @@ export const themeConfig: ThemeConfig = {
site: {
title: 'Retypeset',
subtitle: '再现版式之美',
description: '一个优美的博客主题',
description: '重新编排 - 再现版式之美 | Retypeset - Rediscover the beauty of typography',
author: 'radishzz',
url: 'https://retypeset.netlify.app',
url: 'https://retypeset.radishzz.cc',
favicon: '/image/logo.svg', // or https://image.example.com/logo.svg. Support only webp, svg, png
},
// SITE INFORMATION >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END
@ -32,10 +32,10 @@ export const themeConfig: ThemeConfig = {
// GLOBAL SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> START
global: {
locale: 'zh', // Support only zh, zh-tw, ja, en, es, ru. Default locale setting
locale: 'zh', // support only zh, zh-tw, ja, en, es, ru. Default locale setting
moreLocale: ['zh-tw', 'ja', 'en', 'es', 'ru'], // ['zh', 'zh-tw', 'ja', 'en', 'es', 'ru']. NOT fill in the default locale code again
font: 'sans', // Support only sans, serif. Choose the font style for posts
titleSpace: 4, // Support only 1, 2, 3, 4. Space between title and subtitle, 1 is the smallest, 4 is the largest
font: 'sans', // support only sans, serif. Choose the font style for posts
titleSpace: 4, // support only 1, 2, 3, 4. Space between title and subtitle, 1 is the smallest, 4 is the largest
},
// GLOBAL SETTINGS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> END