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

Added custom renderer for entire layer #1215

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 packages/react-pdf/src/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type { EventProps } from 'make-event-props';
import type {
ClassName,
CustomRenderer,
CustomTextLayerRenderer,
CustomTextRenderer,
NodeOrRenderer,
OnGetAnnotationsError,
Expand Down Expand Up @@ -67,6 +68,7 @@ export type PageProps = {
children?: React.ReactNode;
className?: ClassName;
customRenderer?: CustomRenderer;
customTextLayerRenderer?: CustomTextLayerRenderer;
customTextRenderer?: CustomTextRenderer;
devicePixelRatio?: number;
error?: NodeOrRenderer;
Expand Down Expand Up @@ -120,6 +122,7 @@ const Page: React.FC<PageProps> = function Page(props) {
children,
className,
customRenderer: CustomRenderer,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
error = 'Failed to load the page.',
Expand Down Expand Up @@ -298,6 +301,7 @@ const Page: React.FC<PageProps> = function Page(props) {
? {
_className,
canvasBackground,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
onGetAnnotationsError: onGetAnnotationsErrorProps,
Expand All @@ -324,6 +328,7 @@ const Page: React.FC<PageProps> = function Page(props) {
[
_className,
canvasBackground,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
onGetAnnotationsErrorProps,
Expand Down
12 changes: 11 additions & 1 deletion packages/react-pdf/src/Page/TextLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function TextLayer() {
invariant(pageContext, 'Unable to find Page context.');

const {
customTextLayerRenderer,
customTextRenderer,
onGetTextError,
onGetTextSuccess,
Expand Down Expand Up @@ -206,7 +207,15 @@ export default function TextLayer() {

const layerChildren = layer.querySelectorAll('[role="presentation"]');

if (customTextRenderer) {
if (customTextLayerRenderer) {
customTextLayerRenderer({
layerChildren,
layerElement: layer,
pageIndex,
pageNumber,
textContentItems: textContent.items,
});
} else if (customTextRenderer) {
let index = 0;
textContent.items.forEach((item, itemIndex) => {
if (!isTextItem(item)) {
Expand Down Expand Up @@ -240,6 +249,7 @@ export default function TextLayer() {
}

useLayoutEffect(renderTextLayer, [
customTextLayerRenderer,
customTextRenderer,
onRenderError,
onRenderSuccess,
Expand Down
9 changes: 9 additions & 0 deletions packages/react-pdf/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export type UnregisterPage = (pageIndex: number) => void;
/* Complex types */
export type CustomRenderer = React.FunctionComponent | React.ComponentClass;

export type CustomTextLayerRenderer = (props: {
layerChildren: NodeListOf<Element>;
layerElement: HTMLDivElement;
pageIndex: number;
pageNumber: number;
textContentItems: TextContent['items'];
}) => void;

export type CustomTextRenderer = (
props: { pageIndex: number; pageNumber: number; itemIndex: number } & TextItem,
) => string;
Expand Down Expand Up @@ -139,6 +147,7 @@ export type DocumentContextType = {
export type PageContextType = {
_className?: string;
canvasBackground?: string;
customTextLayerRenderer?: CustomTextLayerRenderer;
customTextRenderer?: CustomTextRenderer;
devicePixelRatio?: number;
onGetAnnotationsError?: OnGetAnnotationsError;
Expand Down