Skip to content

Commit 30d9456

Browse files
committed
Sort props, improve type safety
1 parent eb5dd47 commit 30d9456

File tree

6 files changed

+61
-37
lines changed

6 files changed

+61
-37
lines changed

packages/react-pdf/src/Document.spec.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { makeAsyncCallback, loadPDF, muteConsole, restoreConsole } from '../../.
1313
import type { PDFDocumentProxy } from 'pdfjs-dist';
1414
import type { DocumentContextType, ScrollPageIntoViewArgs } from './shared/types.js';
1515
import type LinkService from './LinkService.js';
16-
import type { OptionalContentConfig } from 'pdfjs-dist/types/src/display/optional_content_config.js';
1716

1817
const pdfFile = await loadPDF('../../__mocks__/_pdf.pdf');
1918
const pdfFile2 = await loadPDF('../../__mocks__/_pdf2.pdf');
@@ -50,13 +49,13 @@ async function waitForAsync() {
5049
}
5150

5251
describe('Document', () => {
53-
// Loaded PDF file
54-
let pdf5: PDFDocumentProxy;
55-
5652
// Object with basic loaded PDF information that shall match after successful loading
5753
const desiredLoadedPdf: Partial<PDFDocumentProxy> = {};
5854
const desiredLoadedPdf2: Partial<PDFDocumentProxy> = {};
5955

56+
// Loaded PDF file
57+
let pdf5: PDFDocumentProxy;
58+
6059
beforeAll(async () => {
6160
const pdf = await pdfjs.getDocument({ data: pdfFile.arrayBuffer }).promise;
6261
desiredLoadedPdf._pdfInfo = pdf._pdfInfo;
@@ -472,13 +471,11 @@ describe('Document', () => {
472471

473472
expect(child.dataset.scale).toBe('2');
474473
});
475-
474+
476475
it('passes optionalContentConfig prop to its children', async () => {
477476
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
478-
const optionalContentConfig: OptionalContentConfig = await pdf5.getOptionalContentConfig();
479-
480-
expect(optionalContentConfig.getGroup('1R').visible).toBe(true);
481477

478+
const optionalContentConfig = await pdf5.getOptionalContentConfig();
482479
optionalContentConfig.setVisibility('1R', false);
483480

484481
let documentContext: DocumentContextType | undefined;
@@ -500,8 +497,17 @@ describe('Document', () => {
500497

501498
await onLoadSuccessPromise;
502499

503-
expect(documentContext?.optionalContentConfig).toBeDefined();
504-
expect(documentContext!.optionalContentConfig!.getGroup('1R').visible).toBe(false);
500+
if (!documentContext) {
501+
throw new Error('Document context is not set');
502+
}
503+
504+
expect(documentContext.optionalContentConfig).toBeDefined();
505+
506+
if (!documentContext.optionalContentConfig) {
507+
throw new Error('Optional content config is not set');
508+
}
509+
510+
expect(documentContext.optionalContentConfig.getGroup('1R').visible).toBe(false);
505511
});
506512
});
507513

packages/react-pdf/src/Document.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ export type DocumentProps = {
179179
* @example () => alert('Document source retrieved!')
180180
*/
181181
onSourceSuccess?: OnSourceSuccess;
182+
/**
183+
* An {@link OptionalContentConfig} created from {@link PDFDocumentProxy.getOptionalContentConfig}.
184+
* If `null`, the configuration will be fetched automatically with the default visibility states set.
185+
*
186+
* @example pdfDocument.getOptionalContentConfig().then(optionalContentConfig => optionalContentConfig.setVisibility('1R', false));
187+
*/
188+
optionalContentConfig?: OptionalContentConfig | null;
182189
/**
183190
* An object in which additional parameters to be passed to PDF.js can be defined. Most notably:
184191
* - `cMapUrl`;
@@ -205,18 +212,13 @@ export type DocumentProps = {
205212
* @example 90
206213
*/
207214
rotate?: number | null;
208-
/*
215+
/**
209216
* Document scale.
210217
*
211218
* @default 1
212219
* @example 0.5
213220
*/
214221
scale?: number;
215-
/**
216-
* An {@link OptionalContentConfig} created from {@link PDFDocumentProxy.getOptionalContentConfig}.
217-
* If `null`, the configuration will be fetched automatically with the default visibility states set.
218-
*/
219-
optionalContentConfig?: OptionalContentConfig | null;
220222
} & EventProps<DocumentCallback | false | undefined>;
221223

222224
const defaultOnPassword: OnPassword = (callback, reason) => {
@@ -272,11 +274,11 @@ const Document: React.ForwardRefExoticComponent<
272274
onPassword = defaultOnPassword,
273275
onSourceError: onSourceErrorProps,
274276
onSourceSuccess: onSourceSuccessProps,
277+
optionalContentConfig,
275278
options,
276279
renderMode,
277280
rotate,
278281
scale,
279-
optionalContentConfig,
280282
...otherProps
281283
},
282284
ref,
@@ -591,24 +593,24 @@ const Document: React.ForwardRefExoticComponent<
591593
imageResourcesPath,
592594
linkService: linkService.current,
593595
onItemClick,
596+
optionalContentConfig,
594597
pdf,
595598
registerPage,
596599
renderMode,
597600
rotate,
598601
scale,
599602
unregisterPage,
600-
optionalContentConfig,
601603
}),
602604
[
603605
imageResourcesPath,
604606
onItemClick,
607+
optionalContentConfig,
605608
pdf,
606609
registerPage,
607610
renderMode,
608611
rotate,
609612
scale,
610613
unregisterPage,
611-
optionalContentConfig,
612614
],
613615
);
614616

packages/react-pdf/src/Page.spec.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import DocumentContext from './DocumentContext.js';
1515

1616
import type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
1717
import type { DocumentContextType, PageCallback } from './shared/types.js';
18-
import type { OptionalContentConfig } from 'pdfjs-dist/types/src/display/optional_content_config.js';
1918

2019
const pdfFile = await loadPDF('../../__mocks__/_pdf.pdf');
2120
const pdfFile2 = await loadPDF('../../__mocks__/_pdf2.pdf');
@@ -739,7 +738,7 @@ describe('Page', () => {
739738
expect(annotationLayer).not.toBeInTheDocument();
740739
});
741740

742-
it('requests page to be rendered with default visibility for optionalContentConfig', async () => {
741+
it('requests page to be rendered with default visibility given no optionalContentConfig', async () => {
743742
const { func: onRenderSuccess, promise: onRenderSuccessPromise } =
744743
makeAsyncCallback<[PageCallback]>();
745744

@@ -754,36 +753,46 @@ describe('Page', () => {
754753
await onRenderSuccessPromise;
755754

756755
const pageCanvas = container.querySelector('.react-pdf__Page__canvas') as HTMLCanvasElement;
757-
const context: CanvasRenderingContext2D = pageCanvas.getContext('2d')!;
758-
const imageData: ImageData = context.getImageData(100, 100, 1, 1);
756+
const context = pageCanvas.getContext('2d');
759757

760-
// should render green pixel because the layer is visible
758+
if (!context) {
759+
throw new Error('CanvasRenderingContext2D is not available');
760+
}
761+
762+
const imageData = context.getImageData(100, 100, 1, 1);
763+
764+
// Should render green pixel because the layer is visible
761765
expect(imageData.data).toStrictEqual(new Uint8ClampedArray([191, 255, 191, 255]));
762766
});
763767

764768
it('requests page to be rendered with given optionalContentConfig', async () => {
765769
const { func: onRenderSuccess, promise: onRenderSuccessPromise } =
766770
makeAsyncCallback<[PageCallback]>();
767-
const optionalContentConfig: OptionalContentConfig = await pdf5.getOptionalContentConfig();
768771

772+
const optionalContentConfig = await pdf5.getOptionalContentConfig();
769773
optionalContentConfig.setVisibility('1R', false);
770774

771775
const { container } = renderWithContext(
772776
<Page onRenderSuccess={onRenderSuccess} pageIndex={0} />,
773777
{
774-
optionalContentConfig,
775778
linkService,
779+
optionalContentConfig,
776780
pdf: pdf5,
777781
},
778782
);
779783

780784
await onRenderSuccessPromise;
781785

782786
const pageCanvas = container.querySelector('.react-pdf__Page__canvas') as HTMLCanvasElement;
783-
const context: CanvasRenderingContext2D = pageCanvas.getContext('2d')!;
784-
const imageData: ImageData = context.getImageData(100, 100, 1, 1);
787+
const context = pageCanvas.getContext('2d');
788+
789+
if (!context) {
790+
throw new Error('CanvasRenderingContext2D is not available');
791+
}
792+
793+
const imageData = context.getImageData(100, 100, 1, 1);
785794

786-
// should render white pixel because the layer is hidden
795+
// Should render white pixel because the layer is hidden
787796
expect(imageData.data).toStrictEqual(new Uint8ClampedArray([255, 255, 255, 255]));
788797
});
789798
});

packages/react-pdf/src/Page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ export default function Page(props: PageProps): React.ReactElement {
333333
onRenderSuccess: onRenderSuccessProps,
334334
onRenderTextLayerError: onRenderTextLayerErrorProps,
335335
onRenderTextLayerSuccess: onRenderTextLayerSuccessProps,
336+
optionalContentConfig,
336337
pageIndex: pageIndexProps,
337338
pageNumber: pageNumberProps,
338339
pdf,
@@ -345,7 +346,6 @@ export default function Page(props: PageProps): React.ReactElement {
345346
scale: scaleProps = defaultScale,
346347
unregisterPage,
347348
width,
348-
optionalContentConfig,
349349
...otherProps
350350
} = mergedProps;
351351

@@ -509,14 +509,14 @@ export default function Page(props: PageProps): React.ReactElement {
509509
onRenderSuccess: onRenderSuccessProps,
510510
onRenderTextLayerError: onRenderTextLayerErrorProps,
511511
onRenderTextLayerSuccess: onRenderTextLayerSuccessProps,
512+
optionalContentConfig,
512513
page,
513514
pageIndex,
514515
pageNumber,
515516
renderForms,
516517
renderTextLayer: renderTextLayerProps,
517518
rotate,
518519
scale,
519-
optionalContentConfig,
520520
}
521521
: null,
522522
[
@@ -536,14 +536,14 @@ export default function Page(props: PageProps): React.ReactElement {
536536
onRenderSuccessProps,
537537
onRenderTextLayerErrorProps,
538538
onRenderTextLayerSuccessProps,
539+
optionalContentConfig,
539540
page,
540541
pageIndex,
541542
pageNumber,
542543
renderForms,
543544
renderTextLayerProps,
544545
rotate,
545546
scale,
546-
optionalContentConfig,
547547
],
548548
);
549549

packages/react-pdf/src/Page/Canvas.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
cancelRunningTask,
1414
getDevicePixelRatio,
1515
isCancelException,
16-
makePageCallback,
1716
isProvided,
17+
makePageCallback,
1818
} from '../shared/utils.js';
1919

2020
import type { RenderParameters } from 'pdfjs-dist/types/src/display/api.js';
@@ -37,12 +37,12 @@ export default function Canvas(props: CanvasProps): React.ReactElement {
3737
devicePixelRatio = getDevicePixelRatio(),
3838
onRenderError: onRenderErrorProps,
3939
onRenderSuccess: onRenderSuccessProps,
40+
optionalContentConfig,
4041
page,
4142
renderForms,
4243
renderTextLayer,
4344
rotate,
4445
scale,
45-
optionalContentConfig,
4646
} = mergedProps;
4747
const { canvasRef } = props;
4848

packages/react-pdf/src/shared/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ import type { AnnotationLayerParameters } from 'pdfjs-dist/types/src/display/ann
1717
import type { OptionalContentConfig } from 'pdfjs-dist/types/src/display/optional_content_config.js';
1818
import type LinkService from '../LinkService.js';
1919

20-
export type { PasswordResponses, StructTreeNode, TextContent, TextItem, TextMarkedContent };
20+
export type {
21+
OptionalContentConfig,
22+
PasswordResponses,
23+
StructTreeNode,
24+
TextContent,
25+
TextItem,
26+
TextMarkedContent,
27+
};
2128

2229
type NullableObject<T extends object> = { [P in keyof T]: T[P] | null };
2330

@@ -135,12 +142,12 @@ export type DocumentContextType = {
135142
imageResourcesPath?: ImageResourcesPath;
136143
linkService: LinkService;
137144
onItemClick?: (args: OnItemClickArgs) => void;
145+
optionalContentConfig?: OptionalContentConfig | null;
138146
pdf?: PDFDocumentProxy | false;
139147
registerPage: RegisterPage;
140148
renderMode?: RenderMode;
141149
rotate?: number | null;
142150
unregisterPage: UnregisterPage;
143-
optionalContentConfig?: OptionalContentConfig | null;
144151
} | null;
145152

146153
export type PageContextType = {
@@ -160,14 +167,14 @@ export type PageContextType = {
160167
onRenderSuccess?: OnRenderSuccess;
161168
onRenderTextLayerError?: OnRenderTextLayerError;
162169
onRenderTextLayerSuccess?: OnRenderTextLayerSuccess;
170+
optionalContentConfig?: OptionalContentConfig | null;
163171
page: PDFPageProxy | false | undefined;
164172
pageIndex: number;
165173
pageNumber: number;
166174
renderForms: boolean;
167175
renderTextLayer: boolean;
168176
rotate: number;
169177
scale: number;
170-
optionalContentConfig?: OptionalContentConfig | null;
171178
} | null;
172179

173180
export type OutlineContextType = {

0 commit comments

Comments
 (0)