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
+26 -19
+26 -19
View File
@@ -2,7 +2,7 @@
import { page } from '$app/stores';
import { resolve } from '$app/paths';
import type { PageData } from './$types';
import { onMount, tick } from 'svelte';
import { tick } from 'svelte';
import tocbot from 'tocbot';
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(
'.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,28 +75,32 @@
sidebarShow = false;
}
});
}
$effect(() => {
if (hasToc && sidebarShow) {
document.body.classList.add('sidebar-show');
} else {
document.body.classList.remove('sidebar-show');
}
return () => {
document.body.classList.remove('sidebar-show');
};
});
if (seriesContentEl && isSeriesOpen) {
seriesContentEl.style.maxHeight = 'none';
} else {
hasToc = false;
}
return () => {
if (hasToc) tocbot.destroy();
tocbot.destroy();
};
});
$effect(() => {
if (hasToc && sidebarShow) {
document.body.classList.add('sidebar-show');
} else {
document.body.classList.remove('sidebar-show');
}
return () => {
document.body.classList.remove('sidebar-show');
};
});
$effect(() => {
if (seriesContentEl && isSeriesOpen) {
seriesContentEl.style.maxHeight = 'none';
}
});
</script>
<svelte:head>