refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* features/chat/composables/useChatScroll.ts
|
||||
* 聊天主区域滚动:贴底、滚到最新用户问题与思考态切换时的滚动策略。
|
||||
*/
|
||||
import { nextTick, watch, type Ref } from 'vue';
|
||||
import type { ChatMessage } from '../types/chat.types';
|
||||
|
||||
export interface UseChatScrollOptions {
|
||||
container: Ref<HTMLElement | null>;
|
||||
messages: Ref<ChatMessage[]>;
|
||||
}
|
||||
|
||||
export function useChatScroll(options: UseChatScrollOptions) {
|
||||
const { container, messages } = options;
|
||||
let scrollToBottomFrame: number | null = null;
|
||||
|
||||
const scrollToBottom = async () => {
|
||||
await nextTick();
|
||||
const el = container.value;
|
||||
if (!el) return;
|
||||
el.scrollTo({
|
||||
top: el.scrollHeight,
|
||||
behavior: 'auto',
|
||||
});
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
/** 消息/思考态变化时自动滚动(思考结束滚到用户问题,否则贴底) */
|
||||
const setupThinkingScrollWatch = (isThinking: Ref<boolean>, conversationId?: Ref<string | null>) => {
|
||||
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;
|
||||
scheduleScrollToBottom();
|
||||
}
|
||||
},
|
||||
{ deep: false },
|
||||
);
|
||||
};
|
||||
|
||||
return {
|
||||
scrollToBottom,
|
||||
scrollToUserQuestion,
|
||||
scheduleScrollToBottom,
|
||||
setupThinkingScrollWatch,
|
||||
dispose,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user