forked from wonday/react-native-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfView.js
356 lines (316 loc) · 12 KB
/
PdfView.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Copyright (c) 2017-present, Wonday (@wonday.org)
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict'
import React, { useEffect, useRef, useState } from 'react'
import { ScrollView, View, StyleSheet } from 'react-native'
import { ViewPropTypes } from 'deprecated-react-native-prop-types'
import PropTypes from 'prop-types'
import PdfManager from './PdfManager'
import PdfPageView from './PdfPageView'
import DoubleTapView from './DoubleTapView'
import PinchZoomView from './PinchZoomView'
import PdfViewFlatList from './PdfViewFlatList'
const MIN_SCALE = 1
const MAX_SCALE = 3
const VIEWABILITYCONFIG = { minimumViewTime: 500, itemVisiblePercentThreshold: 10, waitForInteraction: false }
const PdfView = (props) => {
const {
path = '',
password = '',
scale: _scale = 1,
minScale = MIN_SCALE,
maxScale = MAX_SCALE,
spacing = 10,
style = {},
fitPolicy = 2,
horizontal = false,
centerContent: _centerContent = false,
page: _page = 1,
currentPage: _currentPage = -1,
enablePaging = false,
singlePage = false,
onPageSingleTap = (page, x, y) => {},
onScaleChanged = (scale) => {}
} = props
const flatListRef = useRef()
const scaleTimerRef = useRef()
const scrollTimerRef = useRef()
const mountedRef = useRef(false)
const [pdfLoaded, setPdfLoaded] = useState(false)
const [fileNo, setFileNo] = useState(-1)
const [numberOfPages, setNumberOfPages] = useState(0)
const [page] = useState(_page)
const [currentPage, setCurrentPage] = useState(-1)
const [pageAspectRate, setPageAspectRate] = useState(0.5)
const [pdfPageSize, setPdfPageSize] = useState({ width: 0, height: 0 })
const [contentContainerSize, setContentContainerSize] = useState({ width: 0, height: 0 })
const [scale, setScale] = useState(props?.scale)
const [contentOffset, setContentOffset] = useState({ x: 0, y: 0 })
const [newContentOffset, setNewContentOffset] = useState({ x: 0, y: 0 })
const [centerContent, setCenterContent] = useState(false)
useEffect(() => {
mountedRef.current = true
PdfManager.loadFile(path, password)
.then((pdfInfo) => {
if (mountedRef.current) {
const _fileNo = pdfInfo[0]
const _numberOfPages = pdfInfo[1]
const width = pdfInfo[2]
const height = pdfInfo[3]
const _pageAspectRatio = height === 0 ? 1 : width / height
setPdfLoaded(true)
setFileNo(_fileNo)
setNumberOfPages(_numberOfPages)
setPageAspectRate(_pageAspectRatio)
setPdfPageSize({ width, height })
setCenterContent(numberOfPages > 1 ? false : true)
if (props?.onLoadComplete) {
props?.onLoadComplete(numberOfPages, props?.path, { width, height })
}
}
})
.catch((error) => {
props?.onError(error)
})
clearTimeout(scrollTimerRef.current)
scrollTimerRef.current = setTimeout(() => {
if (flatListRef?.current) {
flatListRef?.current?.scrollToIndex({ animated: false, index: page < 1 ? 0 : page - 1 })
}
}, 200)
return () => {
mountedRef.current = false
clearTimeout(scaleTimerRef?.current)
clearTimeout(scrollTimerRef?.current)
}
}, [])
useEffect(() => {
onScaleChanged({
scale: props?.scale / scale,
pageX: contentContainerSize.width / 2,
pageY: contentContainerSize.height / 2
})
}, [scale])
useEffect(() => {
let page = page < 1 ? 1 : page
page = page > numberOfPages ? numberOfPages : page
if (flatListRef?.current) {
clearTimeout(scrollTimerRef?.current)
scrollTimerRef.current = setTimeout(() => {
flatListRef?.current?.scrollToIndex({ animated: false, index: page - 1 })
}, 200)
}
}, [horizontal, page])
const onLayout = (event) => {
setContentContainerSize({
width: event.nativeEvent.layout.width,
height: event.nativeEvent.layout.height
})
}
const _onScaleChanged = (pinchInfo) => {
let newScale = pinchInfo?.scale * scale
newScale = newScale > maxScale ? maxScale : newScale
newScale = newScale < minScale ? minScale : newScale
let newContentOffset = {
x: (contentOffset?.x + pinchInfo?.pageX) * (newScale / scale) - pinchInfo?.pageX,
y: (contentOffset?.y + pinchInfo?.pageY) * (newScale / scale) - pinchInfo?.pageY
}
setScale(newScale)
setContentOffset(newContentOffset)
props?.onScaleChanged(newScale)
}
const renderSeparator = () => (
<View
style={
horizontal
? {
width: spacing * scale,
backgroundColor: 'transparent'
}
: {
height: spacing * scale,
backgroundColor: 'transparent'
}
}
/>
)
const getPageWidth = () => {
let fitPolicy = props?.fitPolicy
// if only one page, show whole page in center
if (numberOfPages === 1 || singlePage) {
fitPolicy = 2
}
switch (fitPolicy) {
case 0: //fit width
return contentContainerSize.width * scale
case 1: //fit height
return contentContainerSize.height * pageAspectRate * scale
case 2: //fit both
default: {
if (contentContainerSize.width / contentContainerSize.height < pageAspectRate) {
return contentContainerSize.width * scale
} else {
return contentContainerSize.height * pageAspectRate * scale
}
}
}
}
const getPageHeight = () => {
let fitPolicy = fitPolicy
// if only one page, show whole page in center
if (numberOfPages === 1 || singlePage) {
fitPolicy = 2
}
switch (fitPolicy) {
case 0: //fit width
return contentContainerSize.width * (1 / pageAspectRate) * scale
case 1: //fit height
return contentContainerSize.height * scale
case 2: //fit both
default: {
if (contentContainerSize.width / contentContainerSize.height < pageAspectRate) {
return contentContainerSize.width * (1 / pageAspectRate) * scale
} else {
return contentContainerSize.height * scale
}
}
}
}
const onItemSingleTap = (index, x, y) => {
onPageSingleTap(index + 1, x, y)
}
const onItemDoubleTap = (index) => {
if (scale >= maxScale) {
_onScaleChanged({
scale: 1 / scale,
pageX: contentContainerSize.width / 2,
pageY: contentContainerSize.height / 2
})
} else {
_onScaleChanged({
scale: 1.2,
pageX: contentContainerSize.width / 2,
pageY: contentContainerSize.height / 2
})
}
}
const renderItem = ({ item, index }) => {
const pageView = <PdfPageView accessible key={item.id} fileNo={fileNo} page={item.key + 1} width={getPageWidth()} height={getPageHeight()} />
if (singlePage) {
return <View style={{ flexDirection: horizontal ? 'row' : 'column' }}>{pageView}</View>
}
return (
<DoubleTapView
style={{ flexDirection: horizontal ? 'row' : 'column' }}
onSingleTap={(x, y) => {
onItemSingleTap(index, x, y)
}}
onDoubleTap={() => {
onItemDoubleTap(index)
}}>
{pageView}
{index !== numberOfPages - 1 && renderSeparator()}
</DoubleTapView>
)
}
const getItemLayout = (data, index) => ({
length: horizontal ? getPageWidth() : getPageHeight(),
offset: ((horizontal ? getPageWidth() : getPageHeight()) + spacing * scale) * index,
index
})
const onViewableItemsChanged = (viewableInfo) => {
for (let i = 0; i < viewableInfo.viewableItems.length; i++) {
onPageChanged(viewableInfo.viewableItems[i].index + 1, numberOfPages)
if (viewableInfo.viewableItems.length + viewableInfo.viewableItems[0].index < numberOfPages) break
}
}
const onPageChanged = (page, numberOfPages) => {
if (props?.onPageChanged && currentPage !== page) {
props?.onPageChanged(page, numberOfPages)
setCurrentPage(page)
}
}
const onScroll = (e) => {
setContentOffset(e.nativeEvent.contentOffset)
setNewContentOffset(e.nativeEvent.contentOffset)
}
const onListContentSizeChange = (contentWidth, contentHeight) => {
if (contentOffset.x != newContentOffset.x || contentOffset.y != newContentOffset.y) {
flatListRef?.current && flatListRef?.current?.scrollToXY(newContentOffset.x, newContentOffset.y)
}
}
const renderList = () => {
let data = []
if (singlePage) {
data[0] = { key: props?.currentPage >= 0 ? props?.currentPage : 0 }
} else {
for (let i = 0; i < numberOfPages; i++) {
data[i] = { key: i }
}
}
return (
<PdfViewFlatList
ref={flatListRef}
style={[styles.container, style]}
pagingEnabled={enablePaging}
contentContainerStyle={[
{
justifyContent: 'center',
alignItems: 'center'
},
horizontal ? { height: contentContainerSize.height * scale } : { width: contentContainerSize.width * scale }
]}
horizontal={horizontal}
data={data}
renderItem={renderItem}
keyExtractor={(_, index) => 'pdf-page-' + index}
windowSize={11}
getItemLayout={getItemLayout}
maxToRenderPerBatch={1}
renderScrollComponent={(props) => <ScrollView {...props} centerContent={centerContent} pinchGestureEnabled={false} />}
initialScrollIndex={page < 1 ? 0 : page - 1}
onViewableItemsChanged={onViewableItemsChanged}
viewabilityConfig={VIEWABILITYCONFIG}
onScroll={onScroll}
onContentSizeChange={onListContentSizeChange}
scrollEnabled={!singlePage}
/>
)
}
return singlePage ? (
<View style={styles.container} onLayout={onLayout}>
{pdfLoaded && renderList()}
</View>
) : (
<PinchZoomView style={styles.container} onLayout={onLayout} onScaleChanged={_onScaleChanged}>
{pdfLoaded && renderList()}
</PinchZoomView>
)
}
PdfView.propTypes = {
...ViewPropTypes,
path: PropTypes.string,
password: PropTypes.string,
scale: PropTypes.number,
minScale: PropTypes.number,
maxScale: PropTypes.number,
spacing: PropTypes.number,
fitPolicy: PropTypes.number,
horizontal: PropTypes.bool,
page: PropTypes.number,
currentPage: PropTypes.number,
singlePage: PropTypes.bool,
onPageSingleTap: PropTypes.func,
onScaleChanged: PropTypes.func
}
export default PdfView
const styles = StyleSheet.create({
container: {
flex: 1
}
})