Skip to content

Commit 0d3d30c

Browse files
committed
refactor: replace ROTATION_DEGREES array with ROTATION object and update related imports
1 parent 35b5e36 commit 0d3d30c

4 files changed

Lines changed: 30 additions & 10 deletions

File tree

src/PDFPreviewer.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {PDFDocument, PageViewport, RotationDegrees} from './types.js';
1111
import {pdfPreviewerStyles as styles} from './styles.js';
1212
import PDFPasswordForm, {type PDFPasswordFormProps} from './PDFPasswordForm.js';
1313
import PageRenderer from './PageRenderer.js';
14-
import {PAGE_BORDER, LARGE_SCREEN_SIDE_SPACING, DEFAULT_DOCUMENT_OPTIONS, DEFAULT_EXTERNAL_LINK_TARGET, PDF_PASSWORD_FORM_RESPONSES} from './constants.js';
14+
import {PAGE_BORDER, LARGE_SCREEN_SIDE_SPACING, DEFAULT_DOCUMENT_OPTIONS, DEFAULT_EXTERNAL_LINK_TARGET, PDF_PASSWORD_FORM_RESPONSES, ROTATION} from './constants.js';
1515
import {setListAttributes} from './helpers.js';
1616

1717
type Props = {
@@ -53,7 +53,7 @@ function PDFPreviewer({
5353
contentContainerStyle,
5454
shouldShowErrorComponent = true,
5555
onLoadError,
56-
rotation = 0,
56+
rotation = ROTATION.DEG_0,
5757
}: Props): JSX.Element {
5858
const [pageViewports, setPageViewports] = useState<PageViewport[]>([]);
5959
const [numPages, setNumPages] = useState(0);
@@ -119,7 +119,7 @@ function PDFPreviewer({
119119
const {width: originalWidth, height: originalHeight} = pageViewports[pageIndex];
120120

121121
// Swap dimensions when rotated 90 or 270 degrees
122-
const isRotated90or270 = rotation === 90 || rotation === 270;
122+
const isRotated90or270 = rotation === ROTATION.DEG_90 || rotation === ROTATION.DEG_270;
123123
const pageViewportWidth = isRotated90or270 ? originalHeight : originalWidth;
124124
const pageViewportHeight = isRotated90or270 ? originalWidth : originalHeight;
125125

@@ -240,7 +240,12 @@ function PDFPreviewer({
240240
ref={containerRef}
241241
style={{...styles.container, ...containerStyle}}
242242
>
243-
<div style={{...styles.innerContainer, ...(shouldRequestPassword ? styles.invisibleContainer : {})}}>
243+
<div
244+
style={{
245+
...styles.innerContainer,
246+
...(shouldRequestPassword ? styles.invisibleContainer : {}),
247+
}}
248+
>
244249
<Document
245250
file={file}
246251
options={DEFAULT_DOCUMENT_OPTIONS}
@@ -262,7 +267,14 @@ function PDFPreviewer({
262267
itemCount={numPages}
263268
itemSize={calculatePageHeight}
264269
estimatedItemSize={calculatePageHeight(0)}
265-
itemData={{pageWidth, estimatedPageHeight, calculatePageHeight, getDevicePixelRatio, containerHeight, numPages}}
270+
itemData={{
271+
pageWidth,
272+
estimatedPageHeight,
273+
calculatePageHeight,
274+
getDevicePixelRatio,
275+
containerHeight,
276+
numPages,
277+
}}
266278
>
267279
{PageRenderer}
268280
</List>

src/constants.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ const PDF_PASSWORD_FORM_RESPONSES = {
3636
/**
3737
* Valid rotation angles for PDF pages, in degrees clockwise.
3838
*/
39-
const ROTATION_DEGREES = [0, 90, 180, 270] as const;
39+
const ROTATION = {
40+
DEG_0: 0,
41+
DEG_90: 90,
42+
DEG_180: 180,
43+
DEG_270: 270,
44+
} as const;
4045

41-
export {PAGE_BORDER, LARGE_SCREEN_SIDE_SPACING, DEFAULT_DOCUMENT_OPTIONS, DEFAULT_EXTERNAL_LINK_TARGET, PDF_PASSWORD_FORM_RESPONSES, ROTATION_DEGREES};
46+
const ROTATION_DEGREES_STEPS = [ROTATION.DEG_0, ROTATION.DEG_90, ROTATION.DEG_180, ROTATION.DEG_270] as const;
47+
48+
export {PAGE_BORDER, LARGE_SCREEN_SIDE_SPACING, DEFAULT_DOCUMENT_OPTIONS, DEFAULT_EXTERNAL_LINK_TARGET, PDF_PASSWORD_FORM_RESPONSES, ROTATION, ROTATION_DEGREES_STEPS};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import PDFPreviewer from './PDFPreviewer.js';
2-
import {ROTATION_DEGREES} from './constants.js';
2+
import {ROTATION, ROTATION_DEGREES_STEPS} from './constants.js';
33
import type {RotationDegrees} from './types.js';
44

55
const PACKAGE_NAME = 'react-fast-pdf';
66

7-
export {PDFPreviewer, ROTATION_DEGREES};
7+
export {PDFPreviewer, ROTATION, ROTATION_DEGREES_STEPS};
88
export type {RotationDegrees};
99

1010
export default {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {CSSProperties} from 'react';
2+
import {ROTATION} from './constants.js';
23

34
type Page = {
45
getViewport: ({scale}: {scale: number}) => PageViewport;
@@ -21,6 +22,6 @@ type ComponentStyles = {
2122
/**
2223
* Valid rotation angles for PDF pages (in degrees clockwise)
2324
*/
24-
type RotationDegrees = 0 | 90 | 180 | 270;
25+
type RotationDegrees = typeof ROTATION.DEG_0 | typeof ROTATION.DEG_90 | typeof ROTATION.DEG_180 | typeof ROTATION.DEG_270;
2526

2627
export type {PDFDocument, PageViewport, ComponentStyles, RotationDegrees};

0 commit comments

Comments
 (0)