Skip to content

Commit

Permalink
feat: 优化打字机模式显示新内容的逻辑 (Chanzhaoyu#394)
Browse files Browse the repository at this point in the history
* 添加为打字机模式优化的滚动至底部函数

* feat: 优化打字机模式始终显示最新内容的逻辑

* feat: 内容输出结束时也根据滚动条位置判断是否滚动至底部
  • Loading branch information
acongee committed Mar 9, 2023
1 parent 5b74ac9 commit 9576edf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/views/chat/hooks/useScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ScrollReturn {
scrollRef: Ref<ScrollElement>
scrollToBottom: () => Promise<void>
scrollToTop: () => Promise<void>
scrollToBottomIfAtBottom: () => Promise<void>
}

export function useScroll(): ScrollReturn {
Expand All @@ -24,9 +25,21 @@ export function useScroll(): ScrollReturn {
scrollRef.value.scrollTop = 0
}

const scrollToBottomIfAtBottom = async () => {
await nextTick()
if (scrollRef.value) {
const threshold = 50 // 阈值,表示滚动条到底部的距离阈值
const distanceToBottom = scrollRef.value.scrollHeight - scrollRef.value.scrollTop - scrollRef.value.clientHeight
if (distanceToBottom <= threshold) {
scrollRef.value.scrollTop = scrollRef.value.scrollHeight
}
}
}

return {
scrollRef,
scrollToBottom,
scrollToTop,
scrollToBottomIfAtBottom,
}
}
6 changes: 3 additions & 3 deletions src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const chatStore = useChatStore()
useCopyCode()
const { isMobile } = useBasicLayout()
const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat()
const { scrollRef, scrollToBottom } = useScroll()
const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll()
const { uuid } = route.params as { uuid: string }
Expand Down Expand Up @@ -113,14 +113,14 @@ async function onConversation() {
requestOptions: { prompt: message, options: { ...options } },
},
)
scrollToBottom()
scrollToBottomIfAtBottom()
}
catch (error) {
//
}
},
})
scrollToBottom()
scrollToBottomIfAtBottom()
}
catch (error: any) {
const errorMessage = error?.message ?? t('common.wrong')
Expand Down

0 comments on commit 9576edf

Please sign in to comment.