wujingjing
2024-12-31 7357a3709ebeb22f28c17a21f103d0c715213b34
src/components/chat/hooks/useScrollToBottom.ts
@@ -1,6 +1,7 @@
import type { ComputedRef, Ref } from 'vue';
import { nextTick, onActivated, ref, watch } from 'vue';
import { nextTick, onActivated, onUnmounted, ref, watch } from 'vue';
import type { ChatMessage } from '../model/types';
import emitter from '/@/utils/mitt';
export type UseScrollToBottomOption = {
   chatListDom: Ref<HTMLDivElement>;
@@ -12,8 +13,18 @@
    const scrollToBottom = () => {
        if (!chatListDom.value) return;
        chatListDom.value.lastElementChild?.scrollIntoView();
      const parent = chatListDom.value.parentElement;
      if(!parent)return;
      if(parent.scrollHeight>parent.clientHeight){
         parent.scrollTop = parent.scrollHeight - parent.clientHeight;
      }
    };
   emitter.on('amis.page.ready',({instance})=>{
      nextTick(()=>{
         scrollToBottom();
      })
   })
    const forbidScroll = ref(false);
   watch(
      displayMessageList,
@@ -25,6 +36,9 @@
         deep: true,
      }
   );
   onUnmounted(()=>{
      emitter.off('amis.page.ready');
   })
   onActivated(() => {
      if (forbidScroll.value) return;