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 Jul 25, 2023
1 parent c91f2a5 commit 8bc03af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
import type { EventProps } from 'make-event-props';
import type {
CustomRenderer,
CustomTextLayerRenderer,
CustomTextRenderer,
NodeOrRenderer,
OnGetAnnotationsError,
Expand Down Expand Up @@ -66,6 +67,7 @@ export type PageProps = {
children?: React.ReactNode;
className?: string;
customRenderer?: CustomRenderer;
customTextLayerRenderer?: CustomTextLayerRenderer;
customTextRenderer?: CustomTextRenderer;
devicePixelRatio?: number;
error?: NodeOrRenderer;
Expand Down Expand Up @@ -119,6 +121,7 @@ export default function Page(props: PageProps) {
children,
className,
customRenderer: CustomRenderer,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
error = 'Failed to load the page.',
Expand Down Expand Up @@ -297,6 +300,7 @@ export default function Page(props: PageProps) {
? {
_className,
canvasBackground,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
onGetAnnotationsError: onGetAnnotationsErrorProps,
Expand All @@ -323,6 +327,7 @@ export default function Page(props: PageProps) {
[
_className,
canvasBackground,
customTextLayerRenderer,
customTextRenderer,
devicePixelRatio,
onGetAnnotationsErrorProps,
Expand Down
11 changes: 10 additions & 1 deletion 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 @@ -210,7 +211,14 @@ export default function TextLayer() {
? layer.querySelectorAll('.markedContent > *:not(.markedContent')
: layer.children;

if (customTextRenderer) {
if (customTextLayerRenderer) {
customTextLayerRenderer({
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 @@ -244,6 +252,7 @@ export default function TextLayer() {
}

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

export type CustomTextLayerRenderer = (props: {
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 @@ -144,6 +151,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 8bc03af

Please sign in to comment.