From 5f2b237296a7f7428bf5627077d4541b0c7da9f9 Mon Sep 17 00:00:00 2001 From: seaHi Date: Sat, 8 Aug 2026 14:45:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Markdown=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97=E5=B1=95=E5=BC=80=E5=8A=A8=E7=94=BB?= =?UTF-8?q?=E9=AB=98=E5=BA=A6=E8=AE=A1=E7=AE=97=E5=BC=82=E5=B8=B8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/components/MarkdownRenderer.svelte | 29 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib/components/MarkdownRenderer.svelte b/src/lib/components/MarkdownRenderer.svelte index 8c4f14c..24d56c6 100644 --- a/src/lib/components/MarkdownRenderer.svelte +++ b/src/lib/components/MarkdownRenderer.svelte @@ -400,14 +400,35 @@ codeBlock.classList.remove('is-open'); codeBlock.classList.add('is-closed'); } else { + // 暂时关闭 transition 以获取真实的 scrollHeight (包括 padding) + const originalTransition = codeEl.style.transition; + codeEl.style.transition = 'none'; codeBlock.classList.remove('is-closed'); codeBlock.classList.add('is-open'); - codeEl.style.maxHeight = codeEl.scrollHeight + 'px'; + codeEl.style.maxHeight = 'none'; + + const targetHeight = codeEl.scrollHeight; + + // 恢复关闭状态,准备开始动画 + codeBlock.classList.remove('is-open'); + codeBlock.classList.add('is-closed'); + codeEl.style.maxHeight = '0px'; + void codeEl.offsetHeight; // 强制 reflow + + // 恢复 transition 并开始动画 + codeEl.style.transition = originalTransition; + codeBlock.classList.remove('is-closed'); + codeBlock.classList.add('is-open'); + codeEl.style.maxHeight = targetHeight + 'px'; + const handler = (e: Event) => { - if (e.target === codeEl && codeBlock.classList.contains('is-open')) { - codeEl.style.maxHeight = 'none'; + const transEvent = e as TransitionEvent; + if (e.target === codeEl && transEvent.propertyName === 'max-height') { + if (codeBlock.classList.contains('is-open')) { + codeEl.style.maxHeight = 'none'; + } + codeEl.removeEventListener('transitionend', handler); } - codeEl.removeEventListener('transitionend', handler); }; codeEl.addEventListener('transitionend', handler); }