聊天输入框高度自适应

This commit is contained in:
xiaoxiao 2025-08-15 17:17:37 +08:00
parent bf8f2c90e6
commit 7035dbcdfb

View File

@ -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 })