Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add callback function in config to get pdf currentpage and totalpage count #187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ You can provide a config object, which configures parts of the component as requ
zoomJump: 0.2, // 0.1 as default,
},
pdfVerticalScrollByDefault: true, // false as default

// only applicable for pdf
getPdfData: ({ currentPage, totalPages }) => {
console.log("pdf data", totalPages, currentPage);
},
}}
/>
```
Expand Down
6 changes: 6 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { FC, ReactElement, ComponentType, PropsWithChildren } from "react";
import { IMainState } from "./store/mainStateReducer";
import { FileLoaderFunction } from "./utils/fileLoaders";

export interface IpdfDataProps {
currentPage: number;
totalPages: number;
}

export interface IConfig {
header?: IHeaderConfig;
loadingRenderer?: ILoadingRendererConfig;
noRenderer?: INoRendererConfig;
csvDelimiter?: string;
pdfZoom?: IPdfZoomConfig;
pdfVerticalScrollByDefault?: boolean;
getPdfData?: (props: IpdfDataProps) => void | null | undefined;
}

export interface ILoadingRendererConfig {
Expand Down
38 changes: 36 additions & 2 deletions src/renderers/pdf/components/pages/PDFSinglePage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useContext } from "react";
import React, { FC, useContext, useEffect, useRef } from "react";
import { Page } from "react-pdf";
import styled from "styled-components";
import { IStyledProps } from "../../../..";
Expand All @@ -14,13 +14,47 @@ const PDFSinglePage: FC<Props> = ({ pageNum }) => {
state: { mainState, paginated, zoomLevel, numPages, currentPage },
} = useContext(PDFContext);
const { t } = useTranslation();
const pdfPageRef = useRef(null);

const rendererRect = mainState?.rendererRect || null;

const _pageNum = pageNum || currentPage;
// call function to return page data for single
useEffect(() => {
if (!mainState?.config?.getPdfData) return;
if (!numPages) return;
if (!paginated) return;

mainState?.config?.getPdfData({
currentPage: _pageNum,
totalPages: numPages,
});
}, [numPages, _pageNum]);

// call function to return page data for multi page
useEffect(() => {
if (paginated) return;
if (!mainState?.config?.getPdfData) return;
if (!pdfPageRef?.current) return;
if (!numPages) return;

let observer = new IntersectionObserver((entries) => {
if (!entries?.[0]?.isIntersecting) return;

mainState?.config?.getPdfData({
currentPage: _pageNum,
totalPages: numPages,
});
});
observer.observe(pdfPageRef?.current);
}, [pdfPageRef?.current]);

return (
<PageWrapper id="pdf-page-wrapper" last={_pageNum >= numPages}>
<PageWrapper
id="pdf-page-wrapper"
last={_pageNum >= numPages}
ref={pdfPageRef}
>
{!paginated && (
<PageTag id="pdf-page-info">
{t("pdfPluginPageNumber", {
Expand Down