Skip to content

Commit 1fc2bc2

Browse files
committed
fix(messages): handle the problem of failure to scroll to the position of custom message item
1 parent 9d55510 commit 1fc2bc2

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/runtime/components/ChatMessages.vue

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type ChatSlotMessageProps = ChatMessageProps & {
6464
slots?: {
6565
[K in keyof ChatMessageSlots]?: ExtendSlotWithVersion<K>
6666
}
67+
[key: string]: any
6768
}
6869
6970
type ExtendSlotWithVersion<K extends keyof ChatMessageSlots>
@@ -99,7 +100,7 @@ const props = withDefaults(defineProps<ChatMessagesProps>(), {
99100
})
100101
const slots = defineSlots<ChatMessagesSlots>()
101102
102-
const getProxySlots = () => omit(slots, ['default', 'indicator', 'viewport'])
103+
const getProxySlots = () => omit(slots, ['default', 'indicator', 'viewport', 'message'])
103104
104105
const appConfig = useAppConfig() as ChatMessages['AppConfig']
105106
@@ -120,8 +121,8 @@ const lastMessageSubmitted = ref(false)
120121
const lastScrollTop = ref(0)
121122
const userScrolledUp = ref(false)
122123
123-
function registerMessageRef(id: string, element: ComponentPublicInstance | null) {
124-
const elInstance = element?.$el
124+
function registerMessageRef(id: string, element: ComponentPublicInstance | HTMLElement | null) {
125+
const elInstance = element instanceof HTMLElement ? element : element?.$el
125126
if (elInstance) {
126127
messagesRefs.value.set(id, elInstance)
127128
}
@@ -299,16 +300,33 @@ onMounted(() => {
299300
:style="{ '--last-message-height': `${lastMessageHeight}px` }"
300301
>
301302
<slot>
302-
<template v-for="message in messages" :key="message.id">
303-
<component
304-
:is="$slots.message ? $slots.message : UChatMessage"
305-
v-bind="{ ...message, ...(message.role === 'user' ? userProps : assistantProps), compact, message, slots: getProxySlots() }"
306-
:ref="(el: ComponentPublicInstance) => registerMessageRef(message.id, el)"
303+
<template
304+
v-for="messageData in messages"
305+
:key="messageData.id"
306+
>
307+
<slot
308+
v-if="$slots.message"
309+
:ref="(el: HTMLElement) => registerMessageRef(messageData.id, el)"
310+
name="message"
311+
:compact="compact"
312+
v-bind="{
313+
...messageData,
314+
...(messageData.role === 'user' ? userProps : assistantProps)
315+
}"
316+
/>
317+
<UChatMessage
318+
v-else
319+
:ref="(el) => registerMessageRef(messageData.id, el as ComponentPublicInstance)"
320+
:compact="compact"
321+
v-bind="{
322+
...messageData,
323+
...(messageData.role === 'user' ? userProps : assistantProps)
324+
}"
307325
>
308326
<template v-for="(_, name) in getProxySlots()" #[name]="slotData">
309-
<slot :name="name" v-bind="slotData" :message="message" />
327+
<slot :name="name" v-bind="(slotData as any)" :message="messageData" />
310328
</template>
311-
</component>
329+
</UChatMessage>
312330
</template>
313331
</slot>
314332
<UChatMessage

0 commit comments

Comments
 (0)