Skip to content

Commit

Permalink
added custom renderer for entire layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan Kaul authored and taneliang committed Nov 1, 2023
1 parent f944b85 commit bf88495
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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

0 comments on commit bf88495

Please sign in to comment.