forked from wonday/react-native-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdfView.js
304 lines (243 loc) · 9.35 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
/**
* 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, {Component} from 'react';
import {FlatList, View} from 'react-native';
import PropTypes from 'prop-types';
import PdfManager from './PdfManager';
import PdfPageView from './PdfPageView';
import DoubleTapView from './DoubleTapView';
import PinchZoomView from './PinchZoomView';
const MAX_SCALE = 3;
const VIEWABILITYCONFIG = {minimumViewTime: 500, itemVisiblePercentThreshold: 10, waitForInteraction: false};
export default class PdfView extends Component {
static propTypes = {
...View.propTypes,
path: PropTypes.string,
password: PropTypes.string,
scale: PropTypes.number,
spacing: PropTypes.number,
fitPolicy: PropTypes.number,
horizontal: PropTypes.bool,
page: PropTypes.number,
onPageSingleTap: PropTypes.func,
onScaleChanged: PropTypes.func,
};
static defaultProps = {
path: "",
password: "",
scale: 1,
spacing: 10,
style: {},
fitPolicy: 0,
horizontal: false,
page: 1,
onPageSingleTap: (page)=>{},
onScaleChanged: (scale)=>{},
};
constructor(props) {
super(props);
this.state = {
pdfLoaded: false,
fileNo: -1,
numberOfPages: 0,
page: -1,
pageAspectRate: 0.5,
contentContainerSize: {width: 0, height: 0},
scale: 1,
contentOffset: {x:0, y:0},
scrollEnabled: true,
};
this.flatList = null;
this.scaleTimer = null;
}
componentWillMount() {
}
componentDidMount() {
PdfManager.loadFile(this.props.path, this.props.password)
.then((pdfInfo) => {
this.setState({
pdfLoaded: true,
fileNo: pdfInfo[0],
numberOfPages: pdfInfo[1],
pageAspectRate: pdfInfo[3] === 0 ? 1 : pdfInfo[2] / pdfInfo[3]
});
if (this.props.onLoadComplete) this.props.onLoadComplete(pdfInfo[1], this.props.path);
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.scale !== this.state.scale) {
this._onScaleChanged(nextProps.scale / this.state.scale);
}
}
componentWillUnmount() {
clearTimeout(this.scaleTimer);
}
_keyExtractor = (item, index) => index;
_getPageWidth = () => {
// if only one page, show whole page in center
if (this.state.numberOfPages===1) {
return this.state.contentContainerSize.width * this.state.scale;
}
switch (this.props.fitPolicy) {
case 0: //fit width
return this.state.contentContainerSize.width * this.state.scale;
case 1: //fit height
return this.state.contentContainerSize.height * this.state.pageAspectRate * this.state.scale;
case 2: //fit both
default: {
if ((this.state.contentContainerSize.width/this.state.contentContainerSize.height) > this.state.pageAspectRate) {
return this.state.contentContainerSize.height * this.state.scale * this.state.pageAspectRate;
} else {
return this.state.contentContainerSize.width * this.state.scale;
}
}
}
};
_getPageHeight = () => {
// if only one page, show whole page in center
if (this.state.numberOfPages===1) {
return this.state.contentContainerSize.height * this.state.scale;
}
switch (this.props.fitPolicy) {
case 0: //fit width
return this.state.contentContainerSize.width * (1 / this.state.pageAspectRate) * this.state.scale;
case 1: //fit height
return this.state.contentContainerSize.height * this.state.scale;
case 2: //fit both
default: {
if ((this.state.contentContainerSize.width/this.state.contentContainerSize.height) > this.state.pageAspectRate) {
return this.state.contentContainerSize.height * this.state.scale;
} else {
return this.state.contentContainerSize.width * (1 / this.state.pageAspectRate) * this.state.scale;
}
}
}
};
_renderSeparator = () => (
<View style={this.props.horizontal ? {
width: this.props.spacing*this.state.scale,
backgroundColor: 'transparent'
} : {
height: this.props.spacing*this.state.scale,
backgroundColor: 'transparent'
}}/>
);
_onItemSingleTap = (index) => {
this.props.onPageSingleTap(index+1);
};
_onItemDoubleTap = (index) => {
if (this.state.scale >= MAX_SCALE) {
this._onScaleChanged(1 / this.state.scale);
} else {
this._onScaleChanged(1.2);
}
};
_onScaleChanged = (scale, center) => {
let newScale = scale * this.state.scale;
newScale = newScale > MAX_SCALE ? MAX_SCALE : newScale;
newScale = newScale < 1 ? 1 : newScale;
if (this.flatList && this.state.contentOffset) {
this.flatList.scrollToOffset({
animated: false,
offset: (this.props.horizontal ? this.state.contentOffset.x : this.state.contentOffset.y) * scale
});
}
this.setState({scale: newScale, scrollEnabed: false});
this.props.onScaleChanged(newScale);
if (this.scaleTimer) {
clearTimeout(this.scaleTimer);
this.scaleTimer = setTimeout(() => {
this.setState({scrollEnabled: true});
}, 1000);
}
};
_renderItem = ({item, index}) => {
return (
<DoubleTapView style={{flexDirection: this.props.horizontal ? 'row' : 'column'}}
onSingleTap={()=>this._onItemSingleTap(index)}
onDoubleTap={()=>this._onItemDoubleTap(index)}
>
<PdfPageView
key={item.id}
fileNo={this.state.fileNo}
page={item.key + 1}
style={{width: this._getPageWidth(), height: this._getPageHeight()}}
/>
{(index !== this.state.numberOfPages - 1) && this._renderSeparator()}
</DoubleTapView>
);
};
_onViewableItemsChanged = (viewableInfo) => {
if (viewableInfo.viewableItems.length > 0) {
if (this.props.onPageChanged) {
this.props.onPageChanged(viewableInfo.viewableItems[0].index + 1, this.state.numberOfPages);
}
}
};
_renderList = () => {
let data = [];
for (let i = 0; i < this.state.numberOfPages; i++) {
data[i] = {key: i};
}
if (this.state.page !== this.props.page) {
this.timer = setTimeout(() => {
if (this.flatList) {
this.flatList.scrollToIndex({animated: true, index: (this.props.page - 1)});
this.state.page = this.props.page;
}
}, 200);
}
return (
<FlatList
ref={(ref) => {
this.flatList = ref;
}}
style={this.props.style}
contentContainerStyle={this.props.horizontal ? {height: this.state.contentContainerSize.height * this.state.scale} : {width: this.state.contentContainerSize.width * this.state.scale}}
horizontal={this.props.horizontal}
data={data}
renderItem={this._renderItem}
keyExtractor={this._keyExtractor}
windowSize={11}
getItemLayout={(data, index) => ({
length: this.props.horizontal ? this._getPageWidth() : this._getPageHeight(),
offset: ((this.props.horizontal ? this._getPageWidth() : this._getPageHeight()) + this.props.spacing*this.state.scale) * index,
index
})}
maxToRenderPerBatch={1}
removeClippedSubviews={true}
/*initialScrollIndex={this.props.page - 1}*/ /* not action? */
onViewableItemsChanged={this._onViewableItemsChanged}
viewabilityConfig={VIEWABILITYCONFIG}
onScroll={(e) => {
this.state.scrollEnabled && this.setState({contentOffset: e.nativeEvent.contentOffset});
}}
scrollEnabled={this.state.scrollEnabled}
/>
);
};
render() {
return (
<PinchZoomView
style={{flex: 1}}
onLayout={(event) => {
this.setState({
contentContainerSize: {
width: event.nativeEvent.layout.width,
height: event.nativeEvent.layout.height
}
});
}}
onScaleChanged={this._onScaleChanged}
>
{this.state.pdfLoaded ? this._renderList() : (null)}
</PinchZoomView>
);
}
}