fix: 智能助手与发布面板加载历史消息后滚到底部

This commit is contained in:
2026-09-21 09:56:00 +08:00
parent a140cff81a
commit fd6efdb9d2
2 changed files with 59 additions and 7 deletions
@@ -147,10 +147,33 @@ function stripLegacyToolMarkers(content: string) {
.replace(/^\s+|\s+$/g, '');
}
/** 滚到消息区底部;多拍几次等 ChatItem 异步撑高后再定住 */
function scrollChatToBottom() {
const run = () => {
const el = messagePaneRef.value;
if (!el) return;
el.scrollTop = el.scrollHeight;
};
void nextTick(() => {
run();
requestAnimationFrame(() => {
run();
window.setTimeout(run, 50);
window.setTimeout(run, 200);
});
});
}
/** 历史消息加载后:短时监听高度变化,避免气泡异步渲染后仍停在顶部 */
function scrollChatToBottomAfterLoad() {
scrollChatToBottom();
const el = messagePaneRef.value;
if (!el) return;
el.scrollTop = el.scrollHeight;
if (!el || typeof ResizeObserver === 'undefined') return;
const ro = new ResizeObserver(() => {
el.scrollTop = el.scrollHeight;
});
ro.observe(el);
window.setTimeout(() => ro.disconnect(), 800);
}
function defaultModelKey() {
@@ -230,8 +253,6 @@ async function loadMessages(threadId: string) {
messages.value = res.items || [];
streamingText.value = '';
syncSelectedFromSession();
await nextTick();
scrollChatToBottom();
} catch (e: any) {
if (seq === messagesLoadSeq && !sending.value) {
message.error(e?.message || '加载消息失败');
@@ -239,6 +260,9 @@ async function loadMessages(threadId: string) {
} finally {
if (seq === messagesLoadSeq) {
loadingMessages.value = false;
if (messages.value.length > 0) {
scrollChatToBottomAfterLoad();
}
}
}
}
@@ -148,10 +148,33 @@ function syncSelectedFromSession() {
}
}
/** 滚到消息区底部;多拍几次等 ChatItem 异步撑高后再定住 */
function scrollChatToBottom() {
const run = () => {
const el = messagePaneRef.value;
if (!el) return;
el.scrollTop = el.scrollHeight;
};
void nextTick(() => {
run();
requestAnimationFrame(() => {
run();
window.setTimeout(run, 50);
window.setTimeout(run, 200);
});
});
}
/** 历史消息加载后:短时监听高度变化,避免气泡异步渲染后仍停在顶部 */
function scrollChatToBottomAfterLoad() {
scrollChatToBottom();
const el = messagePaneRef.value;
if (!el) return;
el.scrollTop = el.scrollHeight;
if (!el || typeof ResizeObserver === 'undefined') return;
const ro = new ResizeObserver(() => {
el.scrollTop = el.scrollHeight;
});
ro.observe(el);
window.setTimeout(() => ro.disconnect(), 800);
}
async function loadEnabledModels() {
@@ -196,7 +219,12 @@ async function loadMessages(threadId: string) {
if (seq !== messagesLoadSeq) return;
message.error(e?.message || '加载消息失败');
} finally {
if (seq === messagesLoadSeq) loadingMessages.value = false;
if (seq === messagesLoadSeq) {
loadingMessages.value = false;
if (messages.value.length > 0) {
scrollChatToBottomAfterLoad();
}
}
}
}