-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWebPDFDocument.js
132 lines (126 loc) · 5.14 KB
/
WebPDFDocument.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'core-js/features/array/at';
import PropTypes from 'prop-types';
import React, {memo, useCallback} from 'react';
import {Document} from 'react-pdf';
import {VariableSizeList as List} from 'react-window';
import _ from 'underscore';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Text from '@components/Text';
import stylePropTypes from '@styles/stylePropTypes';
import CONST from '@src/CONST';
import PageRenderer from './WebPDFPageRenderer';
const propTypes = {
/** Index of the PDF page to be displayed passed by VariableSizeList */
errorLabelStyles: stylePropTypes,
/** Returns translated string for given locale and phrase */
translate: PropTypes.func.isRequired,
/** The source URL from which to load PDF file to be displayed */
sourceURL: PropTypes.string.isRequired,
/** Callback invoked when the PDF document is loaded successfully */
onDocumentLoadSuccess: PropTypes.func.isRequired,
/** Viewport info of all PDF pages */
pageViewportsLength: PropTypes.number.isRequired,
/** Sets attributes to list container */
setListAttributes: PropTypes.func.isRequired,
/** Indicates, whether the screen is of small width */
isSmallScreenWidth: PropTypes.bool.isRequired,
/** Height of PDF document container view */
containerHeight: PropTypes.number.isRequired,
/** Width of PDF document container view */
containerWidth: PropTypes.number.isRequired,
/** The number of pages of the PDF file to be rendered */
numPages: PropTypes.number,
/** Function that calculates the height of a page of the PDF document */
calculatePageHeight: PropTypes.func.isRequired,
/** Function that calculates the devicePixelRatio the page should be rendered with */
getDevicePixelRatio: PropTypes.func.isRequired,
/** The estimated height of a single PDF page for virtualized rendering purposes */
estimatedItemSize: PropTypes.number.isRequired,
/** The width of a page in the PDF file */
pageWidth: PropTypes.number.isRequired,
/** The style applied to the list component */
listStyle: stylePropTypes,
/** Function that should initiate that the user should be prompted for password to the PDF file */
initiatePasswordChallenge: PropTypes.func.isRequired,
/** Either:
* - `string` - the password provided by the user to unlock the PDF file
* - `undefined` if password isn't needed to view the PDF file
* - `null` if the password is required but hasn't been provided yet */
password: PropTypes.string,
};
const defaultProps = {
errorLabelStyles: [],
numPages: null,
listStyle: undefined,
password: undefined,
};
const WebPDFDocument = memo(
({
errorLabelStyles,
translate,
sourceURL,
onDocumentLoadSuccess,
pageViewportsLength,
setListAttributes,
isSmallScreenWidth,
containerHeight,
containerWidth,
numPages,
calculatePageHeight,
getDevicePixelRatio,
estimatedItemSize,
pageWidth,
listStyle,
initiatePasswordChallenge,
password,
}) => {
const onPassword = useCallback(
(callback, reason) => {
if (reason === CONST.PDF_PASSWORD_FORM.REACT_PDF_PASSWORD_RESPONSES.NEED_PASSWORD) {
if (password) {
callback(password);
} else {
initiatePasswordChallenge(reason);
}
} else if (reason === CONST.PDF_PASSWORD_FORM.REACT_PDF_PASSWORD_RESPONSES.INCORRECT_PASSWORD) {
initiatePasswordChallenge(reason);
}
},
[password, initiatePasswordChallenge],
);
return (
<Document
loading={<FullScreenLoadingIndicator />}
error={<Text style={errorLabelStyles}>{translate('attachmentView.failedToLoadPDF')}</Text>}
file={sourceURL}
options={{
cMapUrl: 'cmaps/',
cMapPacked: true,
}}
externalLinkTarget="_blank"
onLoadSuccess={onDocumentLoadSuccess}
onPassword={onPassword}
>
{!!pageViewportsLength && (
<List
outerRef={setListAttributes}
style={listStyle}
width={isSmallScreenWidth ? pageWidth : containerWidth}
height={containerHeight}
estimatedItemSize={estimatedItemSize}
itemCount={numPages}
itemSize={calculatePageHeight}
itemData={{pageWidth, calculatePageHeight, getDevicePixelRatio, estimatedItemSize}}
>
{PageRenderer}
</List>
)}
</Document>
);
},
(prevProps, nextProps) => _.isEqual(prevProps, nextProps),
);
WebPDFDocument.displayName = 'WebPDFDocument';
WebPDFDocument.propTypes = propTypes;
WebPDFDocument.defaultProps = defaultProps;
export default WebPDFDocument;