/** * features/chat/composables/useChatScroll.ts * 聊天主区域滚动:贴底、滚到最新用户问题、悬浮跳底按钮与思考态切换时的滚动策略。 */ import { computed, nextTick, onBeforeUnmount, ref, watch, type Ref } from 'vue'; import type { ChatMessage } from '../types/chat.types'; export interface UseChatScrollOptions { container: Ref; messages: Ref; } export function useChatScroll(options: UseChatScrollOptions) { const { container, messages } = options; let scrollToBottomFrame: number | null = null; let observedContainer: HTMLElement | null = null; const isNearBottom = ref(true); const updateNearBottomState = () => { const el = container.value; if (!el) { isNearBottom.value = true; return; } const remaining = el.scrollHeight - el.scrollTop - el.clientHeight; isNearBottom.value = remaining <= 96; }; /** 绑定当前滚动容器,容器引用变化时同步换绑监听器。 */ const syncScrollListener = () => { const nextContainer = container.value; if (observedContainer === nextContainer) { updateNearBottomState(); return; } observedContainer?.removeEventListener('scroll', updateNearBottomState); observedContainer = nextContainer; observedContainer?.addEventListener('scroll', updateNearBottomState, { passive: true }); updateNearBottomState(); }; const stopContainerWatch = watch(container, syncScrollListener, { immediate: true }); const showScrollToBottom = computed(() => messages.value.length >= 4 && !isNearBottom.value, ); const scrollToBottom = async () => { await nextTick(); const el = container.value; if (!el) return; el.scrollTo({ top: el.scrollHeight, behavior: 'auto', }); updateNearBottomState(); }; const scrollToBottomSmooth = async () => { await nextTick(); const el = container.value; if (!el) return; el.scrollTo({ top: el.scrollHeight, behavior: 'smooth', }); }; const scrollToTop = async () => { await nextTick(); const el = container.value; if (!el) return; el.scrollTo({ top: 0, behavior: 'auto', }); isNearBottom.value = false; }; const scrollToUserQuestion = async () => { if (scrollToBottomFrame !== null) { window.cancelAnimationFrame(scrollToBottomFrame); scrollToBottomFrame = null; } await nextTick(); await nextTick(); const el = container.value; if (!el) return; let lastUserIndex = -1; for (let i = messages.value.length - 1; i >= 0; i--) { if (messages.value[i]?.role === 'user') { lastUserIndex = i; break; } } if (lastUserIndex < 0) return; const target = el.querySelector(`[data-msg-index="${lastUserIndex}"]`) as HTMLElement | null; target?.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; const scheduleScrollToBottom = () => { if (scrollToBottomFrame !== null) { window.cancelAnimationFrame(scrollToBottomFrame); } scrollToBottomFrame = window.requestAnimationFrame(() => { scrollToBottomFrame = null; void scrollToBottom(); }); }; const dispose = () => { if (scrollToBottomFrame !== null) { window.cancelAnimationFrame(scrollToBottomFrame); scrollToBottomFrame = null; } observedContainer?.removeEventListener('scroll', updateNearBottomState); observedContainer = null; stopContainerWatch(); }; /** 消息/思考态变化时自动滚动(思考结束滚到用户问题,否则贴底) */ const setupThinkingScrollWatch = (isThinking: Ref, conversationId?: Ref) => { let wasThinking = false; watch( () => ({ messageCount: messages.value.length, lastMessageContent: messages.value[messages.value.length - 1]?.content || '', lastMessageRole: messages.value[messages.value.length - 1]?.role || '', isThinking: isThinking.value, conversationId: conversationId?.value || '', }), () => { if (wasThinking && !isThinking.value) { wasThinking = false; void scrollToUserQuestion(); } else { if (isThinking.value) wasThinking = true; if (isNearBottom.value) scheduleScrollToBottom(); else window.requestAnimationFrame(updateNearBottomState); } }, { deep: false }, ); }; onBeforeUnmount(dispose); return { isNearBottom, showScrollToBottom, scrollToBottom, scrollToBottomSmooth, scrollToTop, scrollToUserQuestion, scheduleScrollToBottom, setupThinkingScrollWatch, dispose, }; }