refactor: 使用 Svelte 5 的 $effect 替换 onMount 以正确重新初始化目录
continuous-integration/drone/push Build is passing

This commit is contained in:
seahi committed 2026-09-24 11:35:30 +08:00
1 parent a986d74ef9
commit 1d036172d6
1 file changed
+13 -6
+13 -6
View File
@@ -2,7 +2,7 @@
import { page } from '$app/stores'; import { page } from '$app/stores';
import { resolve } from '$app/paths'; import { resolve } from '$app/paths';
import type { PageData } from './$types'; import type { PageData } from './$types';
import { onMount, tick } from 'svelte'; import { tick } from 'svelte';
import tocbot from 'tocbot'; import tocbot from 'tocbot';
import MarkdownRenderer from '$lib/components/MarkdownRenderer.svelte'; import MarkdownRenderer from '$lib/components/MarkdownRenderer.svelte';
@@ -45,7 +45,10 @@
} }
} }
onMount(() => { $effect(() => {
// track data.post.id to re-initialize TOC when navigating between posts
if (!data.post.id) return;
const headers = document.querySelectorAll( const headers = document.querySelectorAll(
'.js-toc-content h1, .js-toc-content h2, .js-toc-content h3, .js-toc-content h4, .js-toc-content h5, .js-toc-content h6' '.js-toc-content h1, .js-toc-content h2, .js-toc-content h3, .js-toc-content h4, .js-toc-content h5, .js-toc-content h6'
); );
@@ -72,8 +75,15 @@
sidebarShow = false; sidebarShow = false;
} }
}); });
} else {
hasToc = false;
} }
return () => {
tocbot.destroy();
};
});
$effect(() => { $effect(() => {
if (hasToc && sidebarShow) { if (hasToc && sidebarShow) {
document.body.classList.add('sidebar-show'); document.body.classList.add('sidebar-show');
@@ -86,13 +96,10 @@
}; };
}); });
$effect(() => {
if (seriesContentEl && isSeriesOpen) { if (seriesContentEl && isSeriesOpen) {
seriesContentEl.style.maxHeight = 'none'; seriesContentEl.style.maxHeight = 'none';
} }
return () => {
if (hasToc) tocbot.destroy();
};
}); });
</script> </script>