wujingjing
2024-07-22 c123adf7cb8caa3a92f1143755092371ef4a0658
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
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
    };
};