From 62bbd3876a81bc80e142140ee197562349829758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Thu, 23 Jul 2026 13:41:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20textarea=20fallback=20=E9=AB=98=E5=BA=A6?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=20offsetHeight=20-=20clientHeight=20?= =?UTF-8?q?=E7=9A=84=E8=BE=B9=E6=A1=86=E5=B7=AE=EF=BC=8C=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E4=B9=9F=E6=94=B9=E7=94=A8=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=20border-box=20=E9=AB=98=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EditorAgentDraftTextarea.test.tsx | 21 ++++++++++++------- .../EditorAgentDraftTextarea.tsx | 9 ++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.test.tsx b/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.test.tsx index 75f3cf433..dc2a572c2 100644 --- a/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.test.tsx +++ b/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.test.tsx @@ -18,12 +18,7 @@ describe('EditorAgentDraftTextarea', () => { vi.stubGlobal('CSS', { supports }); vi.stubGlobal('ResizeObserver', resizeObserver); - render( - , - ); + render(); const input = screen.getByLabelText( '发送给画布 Agent', @@ -65,6 +60,17 @@ describe('EditorAgentDraftTextarea', () => { ) as HTMLTextAreaElement; expect(observe).toHaveBeenCalledWith(input); let contentHeight = 92; + Object.defineProperty(input, 'offsetHeight', { + configurable: true, + get: () => { + const styleHeight = Number.parseFloat(input.style.height); + return Number.isNaN(styleHeight) ? 42 : styleHeight; + }, + }); + Object.defineProperty(input, 'clientHeight', { + configurable: true, + get: () => input.offsetHeight - 2, + }); Object.defineProperty(input, 'scrollHeight', { configurable: true, get: () => contentHeight, @@ -76,8 +82,9 @@ describe('EditorAgentDraftTextarea', () => { onChange={vi.fn()} />, ); - expect(input.style.height).toBe('92px'); + expect(input.style.height).toBe('94px'); expect(input.style.overflowY).toBe('hidden'); + expect(input.scrollHeight).toBeLessThanOrEqual(input.clientHeight); contentHeight = 180; act(() => { diff --git a/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.tsx b/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.tsx index 80e601846..e2be04ffe 100644 --- a/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.tsx +++ b/src/components/image-editor/EditorAgentConversation/EditorAgentDraftTextarea.tsx @@ -20,13 +20,18 @@ function supportsNativeFieldSizing() { function resizeFallbackTextarea(textarea: HTMLTextAreaElement) { textarea.style.height = 'auto'; const contentHeight = textarea.scrollHeight; + const borderHeight = Math.max( + 0, + textarea.offsetHeight - textarea.clientHeight, + ); + const borderBoxContentHeight = contentHeight + borderHeight; const nextHeight = Math.min( - Math.max(contentHeight, DRAFT_MIN_HEIGHT_PX), + Math.max(borderBoxContentHeight, DRAFT_MIN_HEIGHT_PX), DRAFT_MAX_HEIGHT_PX, ); textarea.style.height = `${nextHeight}px`; textarea.style.overflowY = - contentHeight > DRAFT_MAX_HEIGHT_PX ? 'auto' : 'hidden'; + borderBoxContentHeight > DRAFT_MAX_HEIGHT_PX ? 'auto' : 'hidden'; } export function EditorAgentDraftTextarea({