From 1d036172d6806f27e5386ab470575506c47de97f Mon Sep 17 00:00:00 2001 From: seaHi Date: Thu, 24 Sep 2026 11:35:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20Svelte=205=20?= =?UTF-8?q?=E7=9A=84=20$effect=20=E6=9B=BF=E6=8D=A2=20onMount=20=E4=BB=A5?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E9=87=8D=E6=96=B0=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/posts/[slug]/+page.svelte | 45 ++++++++++++++++------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/routes/posts/[slug]/+page.svelte b/src/routes/posts/[slug]/+page.svelte index 6a04c5d..f917fc4 100644 --- a/src/routes/posts/[slug]/+page.svelte +++ b/src/routes/posts/[slug]/+page.svelte @@ -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'; + } + });