优化消息对话框和消息视图组件:增加消息对话框和滚动容器的引用,调整滚动逻辑以确保更流畅的用户体验,更新样式以改善消息显示效果

This commit is contained in:
jxxghp
2025-04-29 17:28:13 +08:00
parent 6fd5e30fdc
commit b6bb3691f0
2 changed files with 32 additions and 8 deletions

View File

@@ -46,8 +46,11 @@ const user_message = ref('')
// 发送按钮是否可用
const sendButtonDisabled = ref(false)
// 聊天容器
const chatContainer = ref<HTMLElement>()
// 消息对话框引用
const messageDialogRef = ref<any>(null)
// 滚动容器引用
const messageContentRef = ref<any>()
// 定义捷径列表
const shortcuts = [
@@ -102,11 +105,17 @@ function openDialog(dialogRef: any) {
// 滚动到底部
function scrollMessageToEnd() {
nextTick(() => {
if (chatContainer.value) {
chatContainer.value.scrollTop = chatContainer.value.scrollHeight
// 使用更长的延迟确保DOM已更新
setTimeout(() => {
try {
const cardText = document.querySelector('.v-dialog .v-card-text')
if (cardText) {
cardText.scrollTop = cardText.scrollHeight
}
} catch (error) {
console.error(error)
}
})
}, 500) // 增加延迟时间
}
// 拼接全部日志url

View File

@@ -39,7 +39,9 @@ function startSSEMessager() {
const object = JSON.parse(message)
if (compareTime(object.date, lastTime.value) <= 0) return
messages.value.push(object)
emit('scroll')
nextTick(() => {
emit('scroll')
})
}
})
}
@@ -95,6 +97,13 @@ function compareTime(time1: string, time2: string) {
return new Date(time1.replaceAll(/-/g, '/')).getTime() - new Date(time2.replaceAll(/-/g, '/')).getTime()
}
onMounted(() => {
// 组件挂载后触发一次滚动事件
nextTick(() => {
emit('scroll')
})
})
onBeforeUnmount(() => {
if (eventSource) eventSource.close()
})
@@ -105,7 +114,7 @@ onBeforeUnmount(() => {
:mode="!isLoaded ? 'intersect' : 'manual'"
side="start"
:items="messages"
class="overflow-visible"
class="overflow-visible message-scroll h-full"
@load="loadMessages"
:load-more-text="t('message.loadMore') + ' ...'"
>
@@ -127,3 +136,9 @@ onBeforeUnmount(() => {
</div>
</VInfiniteScroll>
</template>
<style scoped>
.message-scroll {
overflow-y: auto !important;
}
</style>