AiCsMessageList.vue
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
<div ref="listEl" class="cs-message-list">
<AiCsMessageBubble
v-for="msg in messages"
:key="msg.id"
:message="msg"
/>
<!-- 滚动锚点 -->
<div ref="bottomEl"></div>
</div>
</template>
<script setup lang="ts">
import AiCsMessageBubble from "./AiCsMessageBubble.vue";
import type { CsMessage } from "@/stores/customerService";
const props = defineProps<{ messages: CsMessage[] }>();
const listEl = ref<HTMLElement | null>(null);
const bottomEl = ref<HTMLElement | null>(null);
function scrollToBottom(smooth = true) {
nextTick(() => {
bottomEl.value?.scrollIntoView({ behavior: smooth ? "smooth" : "instant" });
});
}
// 监听消息变化时自动滚底
watch(
() => props.messages.length,
() => scrollToBottom(),
);
// 监听最后一条消息内容增量(流式)时滚底
watch(
() => {
const last = props.messages[props.messages.length - 1];
return last?.content?.length ?? 0;
},
() => {
const last = props.messages[props.messages.length - 1];
if (last?.status === "streaming") scrollToBottom(false);
},
);
defineExpose({ scrollToBottom });
</script>
<style scoped>
.cs-message-list {
flex: 1;
overflow-y: auto;
padding: 16px 20px;
display: flex;
flex-direction: column;
gap: 12px;
background: #f5f7fa;
}
.cs-message-list::-webkit-scrollbar {
width: 4px;
}
.cs-message-list::-webkit-scrollbar-track {
background: transparent;
}
.cs-message-list::-webkit-scrollbar-thumb {
background: #dcdfe6;
border-radius: 2px;
}
</style>