From ad9064fa4652f26b92a446cd465bce05e52fcd3d Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Mon, 13 Oct 2025 21:29:51 +0800 Subject: [PATCH] fix(toc): fixed issues of scroll to active items in unpinned sidebar, closes #2213 (#2216) --- .../src/app/reader/components/sidebar/TOCView.tsx | 15 ++++++++++++--- .../src/components/settings/ColorPanel.tsx | 5 ----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/readest-app/src/app/reader/components/sidebar/TOCView.tsx b/apps/readest-app/src/app/reader/components/sidebar/TOCView.tsx index ece14acf..87fb8915 100644 --- a/apps/readest-app/src/app/reader/components/sidebar/TOCView.tsx +++ b/apps/readest-app/src/app/reader/components/sidebar/TOCView.tsx @@ -51,6 +51,7 @@ const TOCView: React.FC<{ const [expandedItems, setExpandedItems] = useState>(new Set()); const [containerHeight, setContainerHeight] = useState(400); + const hasToggleExpandRef = useRef(false); const containerRef = useRef(null); const listOuterRef = useRef(null); const vitualListRef = useRef(null); @@ -133,6 +134,7 @@ const TOCView: React.FC<{ const handleToggleExpand = useCallback((item: TOCItem) => { const itemId = getItemIdentifier(item); + hasToggleExpandRef.current = true; setExpandedItems((prev) => { const newSet = new Set(prev); if (newSet.has(itemId)) { @@ -175,8 +177,11 @@ const TOCView: React.FC<{ const hrefMd5 = activeHref ? getContentMd5(activeHref) : ''; const activeItem = staticListRef.current?.querySelector(`[data-href="${hrefMd5}"]`); if (activeItem) { - const rect = activeItem.getBoundingClientRect(); - const isVisible = rect.top >= 0 && rect.bottom <= window.innerHeight; + const container = staticListRef.current.parentElement!; + const containerRect = container.getBoundingClientRect(); + const itemRect = activeItem.getBoundingClientRect(); + const isVisible = + itemRect.top >= containerRect.top && itemRect.bottom <= containerRect.bottom; if (!isVisible) { (activeItem as HTMLElement).scrollIntoView({ behavior: 'instant', block: 'center' }); } @@ -213,11 +218,15 @@ const TOCView: React.FC<{ }, [toc, progress, viewState, sideBarBookKey, isSideBarVisible, bookKey, expandParents]); useEffect(() => { + if (hasToggleExpandRef.current) { + hasToggleExpandRef.current = false; + return; + } if (flatItems.length > 0) { setTimeout(scrollToActiveItem, appService?.isAndroidApp ? 300 : 100); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [progress]); + }, [progress, scrollToActiveItem]); return sections && sections.length > 256 ? (
= ({ bookKey, onRegisterReset minHeight: '80px', }} > - {texture.path && ( -
- {_(texture.name)} -
- )} {selectedTextureId === texture.id && (