From 7035dbcdfbaa2e52688dc40464d918e8b0876ce8 Mon Sep 17 00:00:00 2001 From: xiaoxiao Date: Fri, 15 Aug 2025 17:17:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E8=BE=93=E5=85=A5=E6=A1=86?= =?UTF-8?q?=E9=AB=98=E5=BA=A6=E8=87=AA=E9=80=82=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/view/ChatInputView.ets | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/chatkit_ui/src/main/ets/view/ChatInputView.ets b/chatkit_ui/src/main/ets/view/ChatInputView.ets index e48048a..e58e11d 100644 --- a/chatkit_ui/src/main/ets/view/ChatInputView.ets +++ b/chatkit_ui/src/main/ets/view/ChatInputView.ets @@ -47,6 +47,8 @@ export struct NEChatInputView { onDidClickMore?: () => void; @Param @Require onDidClickCloseReply?: () => void; + @Param @Require + onChangeInputHeight?: (height:number) => void; @Param inputStyle: InputStyleType = InputStyleType.None; @Param @Require controller?: RichEditorController; @@ -91,6 +93,24 @@ export struct NEChatInputView { width: '100%', customStyle: true }) + @Local editorHeight: number = 40; // 新增:动态高度状态 + /** + * 计算编辑器高度 + */ + calculateEditorHeight(): number { + const baseHeight = 40; + const maxHeight = 100; + const lineHeight = 20; // 每行大约20vp高度 + if (this.inputContentLength === 0) { + return baseHeight; + } + // 根据内容长度估算行数,假设每行约20个字符 + const estimatedLines = Math.ceil(this.inputContentLength / 20); + const calculatedHeight = baseHeight + (estimatedLines - 1) * lineHeight; + // 限制在最小和最大高度之间 + return Math.max(baseHeight, Math.min(calculatedHeight, maxHeight)); + } + aboutToAppear(): void { this.initData(); @@ -328,6 +348,7 @@ export struct NEChatInputView { RichEditor({ controller: this.controller }) .backgroundColor(this.mute ? $r('app.color.color_chat_mute_bg') : $r('app.color.color_chat_page_bg')) .height(40) + .height(this.editorHeight) // 修改:使用动态高度 .layoutWeight(1) .placeholder(this.mute ? $r('app.string.chat_team_all_mute') : $r('app.string.chat_send_tips')) @@ -343,6 +364,10 @@ export struct NEChatInputView { this.inputContentLength = spans .map(span => (span as RichEditorTextSpanResult).value?.length ?? 0) .reduce((a, b) => a + b, 0); + + // 新增:动态计算并更新编辑器高度 + this.editorHeight = this.calculateEditorHeight(); + this.onChangeInputHeight?.(this.editorHeight) }) .onSelectionChange((range: RichEditorRange) => { console.debug(`Response ChatInputView onSelectionChange ${range.start} ~ ${range.end}`) @@ -429,7 +454,8 @@ export struct NEChatInputView { } } .width('100%') - .height(60) + // .height(60) + .height(this.editorHeight+20) .id("action_button") }.padding({ left: 8, right: 8 })