From 0d6755f7493083318aa98f79848ef18e46241b81 Mon Sep 17 00:00:00 2001 From: Binks Riverton Date: Wed, 12 Aug 2026 12:44:32 +0800 Subject: [PATCH] feat: paginate document chunks --- frontend/src/components/doc-content.vue | 133 +++++++----------------- frontend/src/hooks/useKnowledgeBase.ts | 17 +-- 2 files changed, 47 insertions(+), 103 deletions(-) diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vue index 22f71fe8..ab12dfc8 100644 --- a/frontend/src/components/doc-content.vue +++ b/frontend/src/components/doc-content.vue @@ -496,13 +496,10 @@ const preprocessMathDelimiters = (rawText: string): string => { .replace(/\\\(([\s\S]*?)\\\)/g, '$$$1$$'); }; const renderer = new marked.Renderer(); -let page = 1; -let loadingChunks = false; -let pendingRequestedPage: number | null = null; -let pendingChunksBeforeLoad = 0; const CHUNK_PAGE_SIZE = 25; -/** Scroll container for the main doc drawer (not the first .t-drawer__body on the page). */ -let docScrollEl: HTMLElement | null = null; +const chunkPage = ref(1); +let loadedChunkPage = 1; +let pendingChunkPage: number | null = null; let mdContentWrap = ref() // Drawer uses attach="body", so markdown nodes live outside mdContentWrap in the DOM. const docMarkdownRoot = ref(null) @@ -589,62 +586,26 @@ const mergeChunks = (chunks: any[]): string => { return merged; }; -const findDocDrawerScrollEl = (): HTMLElement | null => - document.querySelector('.doc-main-drawer .t-drawer__body') as HTMLElement | null; - -const unbindDrawerScroll = () => { - if (docScrollEl) { - docScrollEl.removeEventListener('scroll', handleDetailsScroll); - docScrollEl = null; - } -}; - -const bindDrawerScroll = () => { - unbindDrawerScroll(); - docScrollEl = findDocDrawerScrollEl(); - if (docScrollEl) { - docScrollEl.addEventListener('scroll', handleDetailsScroll, { passive: true }); - } -}; - onMounted(() => { loadTraceDrawerWidth(); loadMainDrawerWidth(); window.addEventListener('resize', onTraceDrawerWindowResize, { passive: true }); }); -watch(() => props.visible, (visible) => { - if (visible) { - nextTick(() => { - bindDrawerScroll(); - maybeLoadMoreChunks(); - }); - } else { - unbindDrawerScroll(); - } -}); watch(() => props.details?.id, () => { - page = 1; - loadingChunks = false; - pendingRequestedPage = null; - pendingChunksBeforeLoad = 0; + chunkPage.value = 1; + loadedChunkPage = 1; + pendingChunkPage = null; }); watch(() => props.details?.chunkLoading, (val) => { - if (val === false) { - if (pendingRequestedPage !== null) { - const currentLength = props.details?.md?.length || 0; - const hasError = Boolean(props.details?.chunkLoadError); - if (hasError && currentLength <= pendingChunksBeforeLoad) { - page = Math.max(1, pendingRequestedPage - 1); - MessagePlugin.warning(props.details?.chunkLoadError); - } - } - pendingRequestedPage = null; - pendingChunksBeforeLoad = 0; - loadingChunks = false; - if (props.visible) { - nextTick(() => maybeLoadMoreChunks()); + if (val === false && pendingChunkPage !== null) { + if (props.details?.chunkLoadError) { + chunkPage.value = loadedChunkPage; + MessagePlugin.warning(props.details.chunkLoadError); + } else { + loadedChunkPage = pendingChunkPage; } + pendingChunkPage = null; } }); onUnmounted(() => { @@ -652,7 +613,6 @@ onUnmounted(() => { window.removeEventListener('resize', onTraceDrawerWindowResize); cleanupTraceDrawerResize(); cleanupMainDrawerResize(); - unbindDrawerScroll(); if (audioBlobUrl.value) { URL.revokeObjectURL(audioBlobUrl.value); } @@ -837,9 +797,6 @@ watch(() => props.details.md, () => { watch(() => viewMode.value, (mode) => { if ((mode === 'chunks' || mode === 'merged') && props.visible) { runMarkdownPostRenderPipeline(); - if (mode === 'chunks') { - nextTick(() => maybeLoadMoreChunks()); - } } }, { flush: 'post' }); @@ -944,7 +901,7 @@ const processMarkdown = (markdownText) => { }; const handleClose = () => { emit("closeDoc", false); - const scrollEl = docScrollEl || findDocDrawerScrollEl(); + const scrollEl = document.querySelector('.doc-main-drawer .t-drawer__body') as HTMLElement | null; if (scrollEl) scrollEl.scrollTop = 0; viewMode.value = 'merged'; }; @@ -1134,10 +1091,8 @@ const notifyChunkMutationOutcome = (item: any, result: any, successMessage?: str }; const reloadChunksFromStart = () => { - page = 1; - loadingChunks = true; - pendingRequestedPage = 1; - pendingChunksBeforeLoad = 0; + chunkPage.value = 1; + pendingChunkPage = 1; editingChunkId.value = ''; chunkDraft.value = ''; emit('getDoc', 1); @@ -1564,43 +1519,12 @@ const downloadFile = () => { MessagePlugin.error(t('file.downloadFailed')); }); }; -const requestNextChunkPage = () => { - if (loadingChunks || props.details?.chunkLoading) return; - const total = props.details?.total ?? 0; - const loaded = props.details?.md?.length ?? 0; - if (loaded >= total || total === 0) return; - const pageNum = Math.ceil(total / CHUNK_PAGE_SIZE); - if (page + 1 > pageNum) return; - page++; - loadingChunks = true; - pendingRequestedPage = page; - pendingChunksBeforeLoad = loaded; - emit('getDoc', page); +const handleChunkPageChange = (pageInfo: { current: number }) => { + if (props.details?.chunkLoading || pageInfo.current === loadedChunkPage) return; + pendingChunkPage = pageInfo.current; + emit('getDoc', pageInfo.current); }; -/** When the list is shorter than the drawer, scroll never fires — prefetch until scrollable or done. */ -const maybeLoadMoreChunks = () => { - if (!props.visible || loadingChunks || props.details?.chunkLoading) return; - const el = docScrollEl || findDocDrawerScrollEl(); - if (!el) return; - const loaded = props.details?.md?.length ?? 0; - const total = props.details?.total ?? 0; - if (loaded >= total) return; - const { scrollHeight, clientHeight } = el; - if (scrollHeight <= clientHeight + 8) { - requestNextChunkPage(); - } -}; - -const handleDetailsScroll = () => { - if (loadingChunks || props.details?.chunkLoading) return; - const el = docScrollEl || findDocDrawerScrollEl(); - if (!el) return; - const { scrollTop, scrollHeight, clientHeight } = el; - if (scrollTop + clientHeight >= scrollHeight - 8) { - requestNextChunkPage(); - } -};