Skip to content

Commit 2e38992

Browse files
committed
fix: totalPageNumber
1 parent 017d7d1 commit 2e38992

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

src/components/newPdfViewer.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createPluginRegistration } from '@embedpdf/core';
33
import { EmbedPDF } from '@embedpdf/core/react';
44
import { usePdfiumEngine } from '@embedpdf/engines/react';
55
import { Viewport, ViewportPluginPackage } from '@embedpdf/plugin-viewport/react';
6-
import { useScroll, Scroller, ScrollPluginPackage } from '@embedpdf/plugin-scroll/react';
6+
import { useScroll, useScrollCapability, Scroller, ScrollPluginPackage } from '@embedpdf/plugin-scroll/react';
77
import { DocumentContent, DocumentManagerPluginPackage } from '@embedpdf/plugin-document-manager/react';
88
import { useZoom, ZoomPluginPackage, ZoomMode } from '@embedpdf/plugin-zoom/react';
99
import { RenderLayer, RenderPluginPackage } from '@embedpdf/plugin-render/react';
@@ -62,34 +62,57 @@ const Controls = memo(function Controls({documentId, toggleFullscreen, isFullscr
6262

6363
const { provides: zoomProv, state: zoomState } = useZoom(documentId);
6464
const { provides: scrollProv, state: scrollState } = useScroll(documentId);
65+
const { provides: scrollCapability } = useScrollCapability();
6566
const [pageNo, setPageNo] = useState("1");
67+
const [totalPages, setTotalPages] = useState(0);
68+
const editingRef = useRef(false);
69+
70+
useEffect(() => {
71+
if (!scrollCapability) return;
72+
const unsub = scrollCapability.onLayoutReady((event) => {
73+
if (event.documentId === documentId) {
74+
setTotalPages(event.totalPages);
75+
}
76+
});
77+
return () => {
78+
unsub();
79+
setTotalPages(0);
80+
};
81+
}, [scrollCapability, documentId]);
6682

6783
useEffect(() => {
6884
if (!scrollProv) return;
69-
const unsub = scrollProv.onPageChange(() =>
70-
setPageNo(String(scrollProv.getCurrentPage()))
71-
);
85+
const unsub = scrollProv.onPageChange((event) => {
86+
setTotalPages(event.totalPages);
87+
if (!editingRef.current) {
88+
setPageNo(String(event.pageNumber));
89+
}
90+
});
7291
return () => unsub();
7392
}, [scrollProv]);
74-
75-
const pageChange = useCallback(
93+
94+
const effectiveTotalPages =
95+
totalPages > 0 ? totalPages : (scrollState?.totalPages ?? 0);
96+
97+
const pageChange = useCallback(
7698
(e: React.KeyboardEvent<HTMLInputElement>) => {
7799
if (e.key !== "Enter") return;
78100
const page = parseInt(pageNo, 10);
79-
if (!isNaN(page) && page >= 1 && page <= (scrollState?.totalPages ?? 1)) {
101+
if (isNaN(page)) return;
102+
if (page >= 1 && page <= effectiveTotalPages) {
80103
scrollProv?.scrollToPage({ pageNumber: page, behavior: "smooth" });
104+
} else {
105+
setPageNo(String(scrollProv?.getCurrentPage() ?? 1));
81106
}
82107
},
83-
[pageNo, scrollState?.totalPages, scrollProv]
108+
[pageNo, effectiveTotalPages, scrollProv]
84109
);
85110

86111
if (!zoomProv || !scrollProv) return null;
87112

88113
const zoomIn = () => zoomProv.zoomIn();
89114
const zoomOut = () => zoomProv.zoomOut();
90115
const { zoomLevel } = zoomState;
91-
const { totalPages } = scrollState;
92-
93116
const fullScreenStyle = {
94117
bottom: 20,
95118
left: "50%",

0 commit comments

Comments
 (0)