import type { ComputedRef, Ref } from 'vue';
|
import { nextTick, onActivated, ref, watch } from 'vue';
|
import type { ChatMessage } from '../model/types';
|
|
export type UseScrollToBottomOption = {
|
chatListDom: Ref<HTMLDivElement>;
|
displayMessageList: ComputedRef<ChatMessage[]>;
|
};
|
|
export const useScrollToBottom = (option:UseScrollToBottomOption) => {
|
const {chatListDom,displayMessageList} = option;
|
|
const scrollToBottom = () => {
|
if (!chatListDom.value) return;
|
chatListDom.value.lastElementChild?.scrollIntoView();
|
};
|
const forbidScroll = ref(false);
|
watch(
|
displayMessageList,
|
() => {
|
if (forbidScroll.value) return;
|
nextTick(() => scrollToBottom());
|
},
|
{
|
deep: true,
|
}
|
);
|
|
onActivated(() => {
|
if (forbidScroll.value) return;
|
nextTick(() => scrollToBottom());
|
});
|
|
return {
|
forbidScroll
|
};
|
};
|