react-native-pdf 6.4.0 → 6.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -2
- package/android/build.gradle +3 -3
- package/index.d.ts +79 -10
- package/ios/RCTPdf/PdfManager.m +11 -1
- package/ios/RCTPdf/RCTPdfView.m +4 -1
- package/package.json +14 -8
- package/windows/RCTPdf/RCTPdfControl.xaml +0 -1
- package/DoubleTapView.js +0 -126
- package/PdfManager.js +0 -31
- package/PdfPageView.js +0 -54
- package/PdfView.js +0 -416
- package/PdfViewFlatList.js +0 -31
- package/PinchZoomView.js +0 -126
- package/index.js +0 -467
package/PdfView.js
DELETED
|
@@ -1,416 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2017-present, Wonday (@wonday.org)
|
|
3
|
-
* All rights reserved.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT-style license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
'use strict';
|
|
10
|
-
import React, {Component} from 'react';
|
|
11
|
-
import {ScrollView, FlatList, View, StyleSheet, ViewPropTypes} from 'react-native';
|
|
12
|
-
|
|
13
|
-
import PropTypes from 'prop-types';
|
|
14
|
-
|
|
15
|
-
import PdfManager from './PdfManager';
|
|
16
|
-
import PdfPageView from './PdfPageView';
|
|
17
|
-
import DoubleTapView from './DoubleTapView';
|
|
18
|
-
import PinchZoomView from './PinchZoomView';
|
|
19
|
-
import PdfViewFlatList from './PdfViewFlatList';
|
|
20
|
-
|
|
21
|
-
const MIN_SCALE = 1;
|
|
22
|
-
const MAX_SCALE = 3;
|
|
23
|
-
|
|
24
|
-
const VIEWABILITYCONFIG = {minimumViewTime: 500, itemVisiblePercentThreshold: 10, waitForInteraction: false};
|
|
25
|
-
|
|
26
|
-
export default class PdfView extends Component {
|
|
27
|
-
|
|
28
|
-
static propTypes = {
|
|
29
|
-
...ViewPropTypes,
|
|
30
|
-
path: PropTypes.string,
|
|
31
|
-
password: PropTypes.string,
|
|
32
|
-
scale: PropTypes.number,
|
|
33
|
-
minScale: PropTypes.number,
|
|
34
|
-
maxScale: PropTypes.number,
|
|
35
|
-
spacing: PropTypes.number,
|
|
36
|
-
fitPolicy: PropTypes.number,
|
|
37
|
-
horizontal: PropTypes.bool,
|
|
38
|
-
page: PropTypes.number,
|
|
39
|
-
currentPage: PropTypes.number,
|
|
40
|
-
singlePage: PropTypes.bool,
|
|
41
|
-
onPageSingleTap: PropTypes.func,
|
|
42
|
-
onScaleChanged: PropTypes.func,
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
static defaultProps = {
|
|
46
|
-
path: "",
|
|
47
|
-
password: "",
|
|
48
|
-
scale: 1,
|
|
49
|
-
minScale: MIN_SCALE,
|
|
50
|
-
maxScale: MAX_SCALE,
|
|
51
|
-
spacing: 10,
|
|
52
|
-
style: {},
|
|
53
|
-
fitPolicy: 2,
|
|
54
|
-
horizontal: false,
|
|
55
|
-
centerContent: false,
|
|
56
|
-
page: 1,
|
|
57
|
-
currentPage: -1,
|
|
58
|
-
enablePaging: false,
|
|
59
|
-
singlePage: false,
|
|
60
|
-
onPageSingleTap: (page, x, y) => {
|
|
61
|
-
},
|
|
62
|
-
onScaleChanged: (scale) => {
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
constructor(props) {
|
|
67
|
-
|
|
68
|
-
super(props);
|
|
69
|
-
this.state = {
|
|
70
|
-
pdfLoaded: false,
|
|
71
|
-
fileNo: -1,
|
|
72
|
-
numberOfPages: 0,
|
|
73
|
-
page: -1,
|
|
74
|
-
currentPage: -1,
|
|
75
|
-
pageAspectRate: 0.5,
|
|
76
|
-
pdfPageSize: {width: 0, height: 0},
|
|
77
|
-
contentContainerSize: {width: 0, height: 0},
|
|
78
|
-
scale: this.props.scale,
|
|
79
|
-
contentOffset: {x: 0, y: 0},
|
|
80
|
-
newContentOffset: {x: 0, y: 0},
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
this._flatList = null;
|
|
84
|
-
this._scaleTimer = null;
|
|
85
|
-
this._scrollTimer = null;
|
|
86
|
-
this._mounted = false;
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
componentDidMount() {
|
|
91
|
-
this._mounted = true;
|
|
92
|
-
PdfManager.loadFile(this.props.path, this.props.password)
|
|
93
|
-
.then((pdfInfo) => {
|
|
94
|
-
if (this._mounted) {
|
|
95
|
-
const fileNo = pdfInfo[0];
|
|
96
|
-
const numberOfPages = pdfInfo[1];
|
|
97
|
-
const width = pdfInfo[2];
|
|
98
|
-
const height = pdfInfo[3];
|
|
99
|
-
const pageAspectRatio = height === 0 ? 1 : width / height;
|
|
100
|
-
|
|
101
|
-
this.setState({
|
|
102
|
-
pdfLoaded: true,
|
|
103
|
-
fileNo,
|
|
104
|
-
numberOfPages,
|
|
105
|
-
pageAspectRate: pageAspectRatio,
|
|
106
|
-
pdfPageSize: {width, height},
|
|
107
|
-
centerContent: numberOfPages > 1 ? false : true
|
|
108
|
-
});
|
|
109
|
-
if (this.props.onLoadComplete) {
|
|
110
|
-
this.props.onLoadComplete(numberOfPages, this.props.path, {width, height});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
})
|
|
115
|
-
.catch((error) => {
|
|
116
|
-
this.props.onError(error);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
clearTimeout(this._scrollTimer);
|
|
120
|
-
this._scrollTimer = setTimeout(() => {
|
|
121
|
-
if (this._flatList) {
|
|
122
|
-
this._flatList.scrollToIndex({animated: false, index: this.props.page < 1 ? 0 : this.props.page - 1});
|
|
123
|
-
}
|
|
124
|
-
}, 200);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
componentDidUpdate(prevProps) {
|
|
128
|
-
|
|
129
|
-
if (this.props.scale !== this.state.scale) {
|
|
130
|
-
this._onScaleChanged({
|
|
131
|
-
scale: this.props.scale / this.state.scale,
|
|
132
|
-
pageX: this.state.contentContainerSize.width / 2,
|
|
133
|
-
pageY: this.state.contentContainerSize.height / 2
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (this.props.horizontal !== prevProps.horizontal || this.props.page !== prevProps.page) {
|
|
138
|
-
let page = (this.props.page) < 1 ? 1 : this.props.page;
|
|
139
|
-
page = page > this.state.numberOfPages ? this.state.numberOfPages : page;
|
|
140
|
-
|
|
141
|
-
if (this._flatList) {
|
|
142
|
-
clearTimeout(this._scrollTimer);
|
|
143
|
-
this._scrollTimer = setTimeout(() => {
|
|
144
|
-
this._flatList.scrollToIndex({animated: false, index: page - 1});
|
|
145
|
-
}, 200);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
componentWillUnmount() {
|
|
152
|
-
this._mounted = false;
|
|
153
|
-
clearTimeout(this._scaleTimer);
|
|
154
|
-
clearTimeout(this._scrollTimer);
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
_keyExtractor = (item, index) => "pdf-page-" + index;
|
|
159
|
-
|
|
160
|
-
_getPageWidth = () => {
|
|
161
|
-
|
|
162
|
-
let fitPolicy = this.props.fitPolicy;
|
|
163
|
-
|
|
164
|
-
// if only one page, show whole page in center
|
|
165
|
-
if (this.state.numberOfPages === 1 || this.props.singlePage) {
|
|
166
|
-
fitPolicy = 2;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
switch (fitPolicy) {
|
|
171
|
-
case 0: //fit width
|
|
172
|
-
return this.state.contentContainerSize.width * this.state.scale;
|
|
173
|
-
case 1: //fit height
|
|
174
|
-
return this.state.contentContainerSize.height * this.state.pageAspectRate * this.state.scale;
|
|
175
|
-
case 2: //fit both
|
|
176
|
-
default: {
|
|
177
|
-
if (this.state.contentContainerSize.width/this.state.contentContainerSize.height<this.state.pageAspectRate) {
|
|
178
|
-
return this.state.contentContainerSize.width * this.state.scale;
|
|
179
|
-
} else {
|
|
180
|
-
return this.state.contentContainerSize.height * this.state.pageAspectRate * this.state.scale;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
_getPageHeight = () => {
|
|
188
|
-
|
|
189
|
-
let fitPolicy = this.props.fitPolicy;
|
|
190
|
-
|
|
191
|
-
// if only one page, show whole page in center
|
|
192
|
-
if (this.state.numberOfPages === 1 || this.props.singlePage) {
|
|
193
|
-
fitPolicy = 2;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
switch (fitPolicy) {
|
|
197
|
-
case 0: //fit width
|
|
198
|
-
return this.state.contentContainerSize.width * (1 / this.state.pageAspectRate) * this.state.scale;
|
|
199
|
-
case 1: //fit height
|
|
200
|
-
return this.state.contentContainerSize.height * this.state.scale;
|
|
201
|
-
case 2: //fit both
|
|
202
|
-
default: {
|
|
203
|
-
if (this.state.contentContainerSize.width/this.state.contentContainerSize.height<this.state.pageAspectRate) {
|
|
204
|
-
return this.state.contentContainerSize.width * (1 / this.state.pageAspectRate) * this.state.scale;
|
|
205
|
-
} else {
|
|
206
|
-
return this.state.contentContainerSize.height * this.state.scale;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
_renderSeparator = () => (
|
|
214
|
-
<View style={this.props.horizontal ? {
|
|
215
|
-
width: this.props.spacing * this.state.scale,
|
|
216
|
-
backgroundColor: 'transparent'
|
|
217
|
-
} : {
|
|
218
|
-
height: this.props.spacing * this.state.scale,
|
|
219
|
-
backgroundColor: 'transparent'
|
|
220
|
-
}}/>
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
_onItemSingleTap = (index, x, y) => {
|
|
224
|
-
|
|
225
|
-
this.props.onPageSingleTap(index + 1, x, y);
|
|
226
|
-
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
_onItemDoubleTap = (index) => {
|
|
230
|
-
|
|
231
|
-
if (this.state.scale >= this.props.maxScale) {
|
|
232
|
-
this._onScaleChanged({
|
|
233
|
-
scale: 1 / this.state.scale,
|
|
234
|
-
pageX: this.state.contentContainerSize.width / 2,
|
|
235
|
-
pageY: this.state.contentContainerSize.height / 2
|
|
236
|
-
});
|
|
237
|
-
} else {
|
|
238
|
-
this._onScaleChanged({
|
|
239
|
-
scale: 1.2,
|
|
240
|
-
pageX: this.state.contentContainerSize.width / 2,
|
|
241
|
-
pageY: this.state.contentContainerSize.height / 2
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
_onScaleChanged = (pinchInfo) => {
|
|
248
|
-
|
|
249
|
-
let newScale = pinchInfo.scale * this.state.scale;
|
|
250
|
-
newScale = newScale > this.props.maxScale ? this.props.maxScale : newScale;
|
|
251
|
-
newScale = newScale < this.props.minScale ? this.props.minScale : newScale;
|
|
252
|
-
let newContentOffset = {
|
|
253
|
-
x: (this.state.contentOffset.x + pinchInfo.pageX) * (newScale / this.state.scale) - pinchInfo.pageX,
|
|
254
|
-
y: (this.state.contentOffset.y + pinchInfo.pageY) * (newScale / this.state.scale) - pinchInfo.pageY
|
|
255
|
-
}
|
|
256
|
-
this.setState({scale: newScale, newContentOffset: newContentOffset});
|
|
257
|
-
this.props.onScaleChanged(newScale);
|
|
258
|
-
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
_renderItem = ({item, index}) => {
|
|
262
|
-
const pageView = (
|
|
263
|
-
<PdfPageView
|
|
264
|
-
accessible={true}
|
|
265
|
-
key={item.id}
|
|
266
|
-
fileNo={this.state.fileNo}
|
|
267
|
-
page={item.key + 1}
|
|
268
|
-
width={this._getPageWidth()}
|
|
269
|
-
height={this._getPageHeight()}
|
|
270
|
-
/>
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
if (this.props.singlePage) {
|
|
274
|
-
return (
|
|
275
|
-
<View style={{flexDirection: this.props.horizontal ? 'row' : 'column'}} >
|
|
276
|
-
{pageView}
|
|
277
|
-
</View>
|
|
278
|
-
)
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return (
|
|
282
|
-
<DoubleTapView style={{flexDirection: this.props.horizontal ? 'row' : 'column'}}
|
|
283
|
-
onSingleTap={(x, y) => {
|
|
284
|
-
this._onItemSingleTap(index, x, y);
|
|
285
|
-
}}
|
|
286
|
-
onDoubleTap={() => {
|
|
287
|
-
this._onItemDoubleTap(index);
|
|
288
|
-
}}
|
|
289
|
-
>
|
|
290
|
-
{pageView}
|
|
291
|
-
{(index !== this.state.numberOfPages - 1) && this._renderSeparator()}
|
|
292
|
-
</DoubleTapView>
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
_onViewableItemsChanged = (viewableInfo) => {
|
|
298
|
-
|
|
299
|
-
for (let i = 0; i < viewableInfo.viewableItems.length; i++) {
|
|
300
|
-
this._onPageChanged(viewableInfo.viewableItems[i].index + 1, this.state.numberOfPages);
|
|
301
|
-
if (viewableInfo.viewableItems.length + viewableInfo.viewableItems[0].index < this.state.numberOfPages) break;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
_onPageChanged = (page, numberOfPages) => {
|
|
307
|
-
if (this.props.onPageChanged && this.state.currentPage !== page) {
|
|
308
|
-
this.props.onPageChanged(page, numberOfPages);
|
|
309
|
-
this.setState({currentPage: page});
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
_getRef = (ref) => this._flatList = ref;
|
|
315
|
-
|
|
316
|
-
_getItemLayout = (data, index) => ({
|
|
317
|
-
length: this.props.horizontal ? this._getPageWidth() : this._getPageHeight(),
|
|
318
|
-
offset: ((this.props.horizontal ? this._getPageWidth() : this._getPageHeight()) + this.props.spacing * this.state.scale) * index,
|
|
319
|
-
index
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
_onScroll = (e) => {
|
|
323
|
-
this.setState({contentOffset: e.nativeEvent.contentOffset, newContentOffset: e.nativeEvent.contentOffset});
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
_onListContentSizeChange = (contentWidth, contentHeight) => {
|
|
327
|
-
if (this.state.contentOffset.x != this.state.newContentOffset.x
|
|
328
|
-
|| this.state.contentOffset.y != this.state.newContentOffset.y) {
|
|
329
|
-
this._flatList.scrollToXY(this.state.newContentOffset.x, this.state.newContentOffset.y);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
_renderList = () => {
|
|
334
|
-
let data = [];
|
|
335
|
-
|
|
336
|
-
if (this.props.singlePage) {
|
|
337
|
-
data[0] = {key: this.props.currentPage >= 0 ? this.props.currentPage : 0}
|
|
338
|
-
} else {
|
|
339
|
-
for (let i = 0; i < this.state.numberOfPages; i++) {
|
|
340
|
-
data[i] = {key: i};
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
return (
|
|
345
|
-
<PdfViewFlatList
|
|
346
|
-
ref={this._getRef}
|
|
347
|
-
style={[styles.container, this.props.style]}
|
|
348
|
-
pagingEnabled={this.props.enablePaging}
|
|
349
|
-
contentContainerStyle={[{
|
|
350
|
-
justifyContent: 'center',
|
|
351
|
-
alignItems: 'center'
|
|
352
|
-
}, this.props.horizontal ? {height: this.state.contentContainerSize.height * this.state.scale} : {width: this.state.contentContainerSize.width * this.state.scale}]}
|
|
353
|
-
horizontal={this.props.horizontal}
|
|
354
|
-
data={data}
|
|
355
|
-
renderItem={this._renderItem}
|
|
356
|
-
keyExtractor={this._keyExtractor}
|
|
357
|
-
windowSize={11}
|
|
358
|
-
getItemLayout={this._getItemLayout}
|
|
359
|
-
maxToRenderPerBatch={1}
|
|
360
|
-
renderScrollComponent={(props) => <ScrollView
|
|
361
|
-
{...props}
|
|
362
|
-
centerContent={this.state.centerContent}
|
|
363
|
-
pinchGestureEnabled={false}
|
|
364
|
-
/>}
|
|
365
|
-
initialScrollIndex={this.props.page < 1 ? 0 : this.props.page - 1}
|
|
366
|
-
onViewableItemsChanged={this._onViewableItemsChanged}
|
|
367
|
-
viewabilityConfig={VIEWABILITYCONFIG}
|
|
368
|
-
onScroll={this._onScroll}
|
|
369
|
-
onContentSizeChange={this._onListContentSizeChange}
|
|
370
|
-
scrollEnabled={!this.props.singlePage}
|
|
371
|
-
/>
|
|
372
|
-
);
|
|
373
|
-
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
_onLayout = (event) => {
|
|
377
|
-
this.setState({
|
|
378
|
-
contentContainerSize: {
|
|
379
|
-
width: event.nativeEvent.layout.width,
|
|
380
|
-
height: event.nativeEvent.layout.height
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
};
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
render() {
|
|
387
|
-
if (this.props.singlePage) {
|
|
388
|
-
return (
|
|
389
|
-
<View
|
|
390
|
-
style={styles.container}
|
|
391
|
-
onLayout={this._onLayout}
|
|
392
|
-
>
|
|
393
|
-
{this.state.pdfLoaded && this._renderList()}
|
|
394
|
-
</View>
|
|
395
|
-
)
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
return (
|
|
399
|
-
<PinchZoomView
|
|
400
|
-
style={styles.container}
|
|
401
|
-
onLayout={this._onLayout}
|
|
402
|
-
onScaleChanged={this._onScaleChanged}
|
|
403
|
-
>
|
|
404
|
-
{this.state.pdfLoaded && this._renderList()}
|
|
405
|
-
</PinchZoomView>
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
const styles = StyleSheet.create({
|
|
413
|
-
container: {
|
|
414
|
-
flex: 1
|
|
415
|
-
}
|
|
416
|
-
});
|
package/PdfViewFlatList.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2017-present, Wonday (@wonday.org)
|
|
3
|
-
* All rights reserved.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT-style license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
'use strict';
|
|
10
|
-
import React, {Component} from 'react';
|
|
11
|
-
import {
|
|
12
|
-
FlatList,
|
|
13
|
-
} from 'react-native';
|
|
14
|
-
|
|
15
|
-
export default class PdfViewFlatList extends FlatList {
|
|
16
|
-
/**
|
|
17
|
-
* Scrolls to a given x, y offset, either immediately or with a smooth animation.
|
|
18
|
-
*
|
|
19
|
-
* Example:
|
|
20
|
-
*
|
|
21
|
-
* `scrollTo({x: 0, y: 0, animated: true})`
|
|
22
|
-
*
|
|
23
|
-
* Note: The weird function signature is due to the fact that, for historical reasons,
|
|
24
|
-
* the function also accepts separate arguments as an alternative to the options object.
|
|
25
|
-
* This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED.
|
|
26
|
-
*/
|
|
27
|
-
scrollToXY = (x, y) => {
|
|
28
|
-
this._listRef._scrollRef.scrollTo({x: x, y: y, animated: false});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
}
|
package/PinchZoomView.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2017-present, Wonday (@wonday.org)
|
|
3
|
-
* All rights reserved.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT-style license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
'use strict';
|
|
10
|
-
import React, {Component} from 'react';
|
|
11
|
-
import PropTypes from 'prop-types';
|
|
12
|
-
import {
|
|
13
|
-
View,
|
|
14
|
-
StyleSheet,
|
|
15
|
-
PanResponder,
|
|
16
|
-
ViewPropTypes,
|
|
17
|
-
} from 'react-native';
|
|
18
|
-
|
|
19
|
-
export default class PinchZoomView extends Component {
|
|
20
|
-
|
|
21
|
-
static propTypes = {
|
|
22
|
-
...ViewPropTypes,
|
|
23
|
-
scalable: PropTypes.bool,
|
|
24
|
-
onScaleChanged: PropTypes.func,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
static defaultProps = {
|
|
28
|
-
scalable: true,
|
|
29
|
-
onScaleChanged: (scale) => {
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
constructor(props) {
|
|
34
|
-
|
|
35
|
-
super(props);
|
|
36
|
-
this.state = {};
|
|
37
|
-
this.distant = 0;
|
|
38
|
-
this.gestureHandlers = PanResponder.create({
|
|
39
|
-
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
|
|
40
|
-
onMoveShouldSetResponderCapture: (evt, gestureState) => (true),
|
|
41
|
-
onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
|
|
42
|
-
onPanResponderGrant: this._handlePanResponderGrant,
|
|
43
|
-
onPanResponderMove: this._handlePanResponderMove,
|
|
44
|
-
onPanResponderRelease: this._handlePanResponderEnd,
|
|
45
|
-
onPanResponderTerminationRequest: evt => false,
|
|
46
|
-
onPanResponderTerminate: this._handlePanResponderTerminate,
|
|
47
|
-
onShouldBlockNativeResponder: evt => true
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
_handleStartShouldSetPanResponder = (e, gestureState) => {
|
|
53
|
-
|
|
54
|
-
// don't respond to single touch to avoid shielding click on child components
|
|
55
|
-
return false;
|
|
56
|
-
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
_handleMoveShouldSetPanResponder = (e, gestureState) => {
|
|
60
|
-
|
|
61
|
-
return this.props.scalable && (e.nativeEvent.changedTouches.length >= 2 || gestureState.numberActiveTouches >= 2);
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
_handlePanResponderGrant = (e, gestureState) => {
|
|
66
|
-
|
|
67
|
-
if (e.nativeEvent.changedTouches.length >= 2 || gestureState.numberActiveTouches >= 2) {
|
|
68
|
-
let dx = Math.abs(e.nativeEvent.touches[0].pageX - e.nativeEvent.touches[1].pageX);
|
|
69
|
-
let dy = Math.abs(e.nativeEvent.touches[0].pageY - e.nativeEvent.touches[1].pageY);
|
|
70
|
-
this.distant = Math.sqrt(dx * dx + dy * dy);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
_handlePanResponderEnd = (e, gestureState) => {
|
|
76
|
-
|
|
77
|
-
this.distant = 0;
|
|
78
|
-
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
_handlePanResponderTerminate = (e, gestureState) => {
|
|
82
|
-
|
|
83
|
-
this.distant = 0;
|
|
84
|
-
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
_handlePanResponderMove = (e, gestureState) => {
|
|
88
|
-
|
|
89
|
-
if ((e.nativeEvent.changedTouches.length >= 2 || gestureState.numberActiveTouches >= 2) && this.distant > 100) {
|
|
90
|
-
|
|
91
|
-
let dx = Math.abs(e.nativeEvent.touches[0].pageX - e.nativeEvent.touches[1].pageX);
|
|
92
|
-
let dy = Math.abs(e.nativeEvent.touches[0].pageY - e.nativeEvent.touches[1].pageY);
|
|
93
|
-
let distant = Math.sqrt(dx * dx + dy * dy);
|
|
94
|
-
let scale = (distant / this.distant);
|
|
95
|
-
let pageX = (e.nativeEvent.touches[0].pageX + e.nativeEvent.touches[1].pageX) / 2;
|
|
96
|
-
let pageY = (e.nativeEvent.touches[0].pageY + e.nativeEvent.touches[1].pageY) / 2;
|
|
97
|
-
let pinchInfo = {scale: scale, pageX: pageX, pageY: pageY};
|
|
98
|
-
|
|
99
|
-
this.props.onScaleChanged(pinchInfo);
|
|
100
|
-
this.distant = distant;
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
render() {
|
|
107
|
-
|
|
108
|
-
return (
|
|
109
|
-
<View
|
|
110
|
-
{...this.props}
|
|
111
|
-
{...this.gestureHandlers?.panHandlers}
|
|
112
|
-
style={[styles.container, this.props.style]}>
|
|
113
|
-
{this.props.children}
|
|
114
|
-
</View>
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const styles = StyleSheet.create({
|
|
121
|
-
container: {
|
|
122
|
-
flex: 1,
|
|
123
|
-
justifyContent: 'center',
|
|
124
|
-
alignItems: 'center'
|
|
125
|
-
}
|
|
126
|
-
});
|