Files
Enterprise-Knowledge-Base/frontend/src/composables/useStickyBottomOnResize.ts
T
wizardchen 80bcf200b6 feat(frontend): enhance chat image handling and typewriter effect
- Introduce a stable skeleton box for protected images during loading to prevent flickering.
- Implement a new composable `useStickyBottomOnResize` to maintain chat scroll position as content loads.
- Add `useTypewriter` for smoother text rendering in chat, improving user experience during streaming.
- Create a directive `vStableHtml` to efficiently update HTML content without losing image states.
- Enhance chat markdown rendering to handle incomplete citation tags and trailing horizontal rules during streaming.
- Add tests for new typewriter functionality and citation handling.
2026-06-18 18:42:16 +08:00

45 lines
1.4 KiB
TypeScript

import { onMounted, onUnmounted, type Ref } from 'vue'
/**
* Keep a chat pinned to the bottom when asynchronously rendered content grows.
*
* Streaming text already requests scrolling when chunks arrive, but images,
* Mermaid diagrams, and other rich content can change height later. Observe the
* message list itself so all delayed layout changes share the same behavior.
* A user who has intentionally scrolled up is never pulled back down.
*/
export function useStickyBottomOnResize(
scrollContainer: Ref<HTMLElement | null>,
userHasScrolledUp: Ref<boolean>,
scrollToBottom: () => void,
): void {
let resizeObserver: ResizeObserver | null = null
let scrollFrame: number | null = null
const scheduleFollow = () => {
if (userHasScrolledUp.value || scrollFrame !== null) return
scrollFrame = requestAnimationFrame(() => {
scrollFrame = null
if (!userHasScrolledUp.value) scrollToBottom()
})
}
onMounted(() => {
const messageList = scrollContainer.value?.firstElementChild
if (!messageList || typeof ResizeObserver === 'undefined') return
resizeObserver = new ResizeObserver(scheduleFollow)
resizeObserver.observe(messageList)
})
onUnmounted(() => {
resizeObserver?.disconnect()
resizeObserver = null
if (scrollFrame !== null) {
cancelAnimationFrame(scrollFrame)
scrollFrame = null
}
})
}