Commit e1ce4fd

HPCesia <me@hpcesia.com>
2025-02-14 08:21:52
fix: setup script
1 parent e180d38
src/components/aside/siteinfo/Tags.astro
@@ -29,7 +29,7 @@ const tagsMap = await getTags();
 </div>
 
 <script>
-  function initTags() {
+  function setup() {
     const tags = document.getElementById('aside-siteinfo-tags');
     const tagsList = tags?.children[0] as HTMLDivElement;
     const moreBtn = tags?.children[1] as HTMLDivElement;
@@ -49,5 +49,6 @@ const tagsMap = await getTags();
     });
   }
 
-  document.addEventListener('astro:page-load', initTags);
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
src/components/comment/Twikoo.astro
@@ -11,9 +11,9 @@ import { CDN } from '@constants/cdn.mjs';
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
   declare const twikoo: any;
 
-  function initTwikoo() {
+  function setup() {
     if (typeof twikoo === 'undefined') {
-      setTimeout(initTwikoo, 100);
+      setTimeout(setup, 100);
     } else {
       twikoo.init({
         el: '#twikoo-wrap',
@@ -22,5 +22,6 @@ import { CDN } from '@constants/cdn.mjs';
     }
   }
 
-  document.addEventListener('astro:page-load', initTwikoo);
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
src/components/search/Pagefind.astro
@@ -24,7 +24,7 @@ const bundlePath = `${import.meta.env.BASE_URL}pagefind/`;
 </template>
 
 <script is:inline>
-  async function initPageFind() {
+  async function setup() {
     for (const el of document.querySelectorAll('[data-pagefind-ui]')) {
       const bundlePath = el.getAttribute('data-bundle-path');
       const baseUrl = el.getAttribute('data-base-url');
@@ -72,7 +72,8 @@ const bundlePath = `${import.meta.env.BASE_URL}pagefind/`;
     }
   }
 
-  document.addEventListener('astro:page-load', initPageFind);
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
 
 <style is:global>
src/components/widgets/DarkModeButton.astro
@@ -30,7 +30,7 @@ const { class: className, showText, ...rest } = Astro.props;
 </Button>
 
 <script>
-  function initDarkmodeButtons() {
+  function setup() {
     const darkmodeBtns = document.querySelectorAll('button.darkmode-btn');
 
     darkmodeBtns.forEach((btn) => {
@@ -94,5 +94,6 @@ const { class: className, showText, ...rest } = Astro.props;
     });
   }
 
-  document.addEventListener('astro:page-load', initDarkmodeButtons);
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
src/components/widgets/Pagination.astro
@@ -119,7 +119,7 @@ else {
 
 <script>
   import { navigate } from 'astro:transitions/client';
-  function initPagination() {
+  function setup() {
     const pageJumper = document.getElementById('page-jumper');
     const pageJumperInput = document.getElementById(
       'page-jumper-input'
@@ -170,5 +170,6 @@ else {
     });
     pageJumperButton?.addEventListener('click', pageJumperExecHandler);
   }
-  document.addEventListener('astro:page-load', initPagination);
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
src/components/Navbar.astro
@@ -139,14 +139,19 @@ if (!title) title = 'Astral Halo';
     navbarConfig.navbarRightItems.always,
   ].flat();
 
-  document.addEventListener('astro:page-load', () => {
-    rightItems.forEach((item) => {
-      if ('onclick' in item && item.onclick && typeof item.onclick !== 'string') {
-        const navEl = document.getElementById('nav-' + item.onclick.id);
-        if (navEl) navEl.addEventListener('click', item.onclick.function);
-        const sideEl = document.getElementById('side-' + item.onclick.id);
-        if (sideEl) sideEl.addEventListener('click', item.onclick.function);
-      }
+  function setup() {
+    window.swup?.hooks.on('content:replace', () => {
+      rightItems.forEach((item) => {
+        if ('onclick' in item && item.onclick && typeof item.onclick !== 'string') {
+          const navEl = document.getElementById('nav-' + item.onclick.id);
+          if (navEl) navEl.addEventListener('click', item.onclick.function);
+          const sideEl = document.getElementById('side-' + item.onclick.id);
+          if (sideEl) sideEl.addEventListener('click', item.onclick.function);
+        }
+      });
     });
-  });
+  }
+
+  if (window.swup) setup();
+  else document.addEventListener('swup:enable', setup);
 </script>
src/components/SideToolBar.astro
@@ -53,7 +53,7 @@ import DarkModeButton from './widgets/DarkModeButton.astro';
 <script>
   import { getReadingProgress } from '@scripts/utils';
 
-  function initSideToolBar() {
+  function setup() {
     const stbShow = document.getElementById('stb-show');
     const stbShowMore = document.getElementById('stb-show-more');
     const stbBackToTop = document.getElementById('stb-back-to-top');
@@ -132,8 +132,6 @@ import DarkModeButton from './widgets/DarkModeButton.astro';
     });
   }
 
-  document.addEventListener('astro:page-load', () => {
-    initSideToolBar();
-  });
-  initSideToolBar();
+  document.addEventListener('astro:page-load', setup);
+  setup();
 </script>
src/layouts/GlobalLayout.astro
@@ -110,18 +110,17 @@ const favicons: Favicon[] =
       document.documentElement.setAttribute('data-theme', 'dark');
     } else document.documentElement.setAttribute('data-theme', 'light');
   }
-  document.addEventListener('astro:after-swap', applyDarkMode);
-  applyDarkMode();
 
-  // 时间显示
-  document.addEventListener('astro:page-load', convertTimeToRelative);
-
-  // 图片缩放
-  document.addEventListener('astro:page-load', () => {
+  function setup() {
+    applyDarkMode();
+    convertTimeToRelative();
     mediumZoom('[data-zoom]', {
       background: 'rgba(0, 0, 0, 0.4)',
     });
-  });
+  }
+
+  document.addEventListener('astro:after-swap', setup);
+  setup();
 </script>
 
 <style is:global>