mirror of
https://github.com/zadam/trilium.git
synced 2026-01-01 20:24:25 +01:00
feat(client/right_pane): use intersection observer for performance
This commit is contained in:
parent
bcf72f4624
commit
cb33404122
@ -81,18 +81,43 @@ function PdfPageItem({
|
||||
pageNumber: number;
|
||||
isActive: boolean;
|
||||
thumbnail?: string;
|
||||
onRequestThumbnail: (pageNumber: number) => void;
|
||||
onPageClick: () => void;
|
||||
}) {
|
||||
const itemRef = useRef<HTMLDivElement>(null);
|
||||
const hasRequested = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!thumbnail && !hasRequested.current) {
|
||||
hasRequested.current = true;
|
||||
onRequestThumbnail(pageNumber);
|
||||
}
|
||||
const element = itemRef.current;
|
||||
if (!element) return;
|
||||
|
||||
// Create intersection observer to detect when item enters viewport
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting && !thumbnail && !hasRequested.current) {
|
||||
hasRequested.current = true;
|
||||
onRequestThumbnail(pageNumber);
|
||||
}
|
||||
});
|
||||
},
|
||||
{
|
||||
root: null, // viewport
|
||||
rootMargin: '100px', // Load thumbnails slightly before they enter viewport
|
||||
threshold: 0.01
|
||||
}
|
||||
);
|
||||
|
||||
observer.observe(element);
|
||||
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
};
|
||||
}, [pageNumber, thumbnail, onRequestThumbnail]);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={itemRef}
|
||||
className={`pdf-page-item ${isActive ? 'active' : ''}`}
|
||||
onClick={onPageClick}
|
||||
>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user