优化对话自动滚动到底部

This commit is contained in:
aixianling
2023-05-18 15:38:25 +08:00
parent 1e4be83499
commit 3ba394123e
2 changed files with 10 additions and 10 deletions

View File

@@ -9,7 +9,7 @@
</el-row> </el-row>
</el-row> </el-row>
<thinking-bar v-show="loading"/> <thinking-bar v-show="loading"/>
<chat-content class="fill" :list="chatHistory"/> <chat-content class="fill" :list="chatHistory" ref="chatContent"/>
<el-row class="mar-t8" align="middle"> <el-row class="mar-t8" align="middle">
<chat-input class="fill mar-r8" v-model="inputText" @enter="handleSend"/> <chat-input class="fill mar-r8" v-model="inputText" @enter="handleSend"/>
<el-button type="primary" @click="handleSend">发送</el-button> <el-button type="primary" @click="handleSend">发送</el-button>
@@ -65,16 +65,20 @@ export default {
} }
this.chatHistory.push(aiMsg) this.chatHistory.push(aiMsg)
if (this.config.stream) { if (this.config.stream) {
ai.chatStream(this.chatHistory).then(reader => ai.streamOutput(reader, this.chatHistory.at(-1))).finally(() => this.loading = false) ai.chatStream(this.chatHistory).then(reader => ai.streamOutput(reader, this.chatHistory.at(-1))).finally(this.handleAfterSend)
} else { } else {
ai.chat(this.chatHistory).then(reply => { ai.chat(this.chatHistory).then(reply => {
const decodeArr = reply.split("") const decodeArr = reply.split("")
decodeArr.forEach(e => this.chatHistory.at(-1).msg += e) decodeArr.forEach(e => this.chatHistory.at(-1).msg += e)
}).finally(() => this.loading = false) }).finally(this.handleAfterSend)
} }
} else { } else {
this.$message.error("请不要发送空消息!") this.$message.error("请不要发送空消息!")
} }
},
handleAfterSend() {
this.loading = false
this.$refs.chatContent.scrollBottom()
} }
} }
} }

View File

@@ -1,12 +1,5 @@
<template> <template>
<section class="chatContent" @scroll="onScroll"> <section class="chatContent" @scroll="onScroll">
<div class="chat-wrapper">
<div class="chat-friend">
<div class="chat-text">
<v-md-preview :text="demo"/>
</div>
</div>
</div>
<div class="chat-wrapper" v-for="item in list" :key="item.id"> <div class="chat-wrapper" v-for="item in list" :key="item.id">
<div class="chat-friend" v-if="item.uid !== 'me'"> <div class="chat-friend" v-if="item.uid !== 'me'">
<div class="chat-text" v-if="item.chatType == 0"> <div class="chat-text" v-if="item.chatType == 0">
@@ -74,6 +67,9 @@ export default {
this.isAutoScroll = scrollTop + offsetHeight === scrollHeight; this.isAutoScroll = scrollTop + offsetHeight === scrollHeight;
scrollDom.scrollTop = scrollHeight - scrollDom.clientHeight scrollDom.scrollTop = scrollHeight - scrollDom.clientHeight
}, },
scrollBottom() {
this.$el.scrollTop = this.$el.scrollHeight - this.$el.clientHeight
},
copy(msg) { copy(msg) {
if (copyToClipboard(msg)) { if (copyToClipboard(msg)) {
this.$message.success("已复制") this.$message.success("已复制")