@react-native-ohos/react-native-pdf 6.7.7-rc.1 → 6.8.0-rc.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.
Files changed (68) hide show
  1. package/PdfView.js +417 -417
  2. package/README.md +1 -1
  3. package/fabric/RNPDFPdfNativeComponent.js +48 -48
  4. package/harmony/pdfview/BuildProfile.ets +16 -16
  5. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/.ts_checker_cache +4426 -0
  6. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/.tsbuildinfo +1 -0
  7. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/.tsbuildinfo.linter +1 -0
  8. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/compileInfo.json +1 -0
  9. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/modules/Index.ets-5eef5c87aa27b233ef53cb620fed1f5d.msgpack +0 -0
  10. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/modules/Logger.ets-c555840d2a4b574cf40c57fc8b91bf97.msgpack +0 -0
  11. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/modules/RTNPdfView.ets-030d7d4eb9347bd497689e1ef58937f0.msgpack +1711 -0
  12. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/modules/types.ts-c405c8f7d848bd5a548ec85c97e839bd.msgpack +0 -0
  13. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/other/pkgName2SourceRoots.msgpack +1 -0
  14. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/compiler.cache/plugins/plugins.msgpack +0 -0
  15. package/harmony/pdfview/build/default/cache/default/default@HarCompileArkTS/esmodule/debug/dep_info.json +1 -0
  16. package/harmony/pdfview/build/default/cache/default/default@PackageHar/BuildProfile.ets +17 -0
  17. package/harmony/pdfview/build/default/cache/default/default@PackageHar/Index.ets +7 -0
  18. package/harmony/pdfview/build/default/cache/default/default@PackageHar/ResourceTable.txt +1 -0
  19. package/harmony/pdfview/build/default/cache/default/default@PackageHar/build-profile.json5 +28 -0
  20. package/harmony/pdfview/build/default/cache/default/default@PackageHar/consumer-rules.txt +0 -0
  21. package/harmony/pdfview/build/default/cache/default/default@PackageHar/hvigorfile.ts +6 -0
  22. package/harmony/pdfview/build/default/cache/default/default@PackageHar/obfuscation-rules.txt +18 -0
  23. package/harmony/pdfview/build/default/cache/default/default@PackageHar/oh-package-lock.json5 +21 -0
  24. package/harmony/pdfview/build/default/cache/default/default@PackageHar/oh-package.json5 +1 -0
  25. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/CMakeLists.txt +8 -0
  26. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/ComponentDescriptors.h +36 -0
  27. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/EventEmitters.cpp +39 -0
  28. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/EventEmitters.h +47 -0
  29. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/PdfEventEmitRequestHandler.h +50 -0
  30. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/PdfViewJSIBinder.h +71 -0
  31. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/PdfViewPackage.h +55 -0
  32. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/Props.cpp +58 -0
  33. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/Props.h +61 -0
  34. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/RTNPdfViewSpecsJSI-generated.cpp +34 -0
  35. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/RTNPdfViewSpecsJSI.h +36 -0
  36. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/ShadowNodes.cpp +33 -0
  37. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/ShadowNodes.h +47 -0
  38. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/States.cpp +33 -0
  39. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/cpp/States.h +52 -0
  40. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/ets/Logger.ets +64 -0
  41. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/ets/components/mainpage/RTNPdfView.ets +513 -0
  42. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/ets/components/mainpage/types.ts +15 -0
  43. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/module.json +32 -0
  44. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/resources/base/element/string.json +8 -0
  45. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/resources/en_US/element/string.json +8 -0
  46. package/harmony/pdfview/build/default/cache/default/default@PackageHar/src/main/resources/zh_CN/element/string.json +8 -0
  47. package/harmony/pdfview/build/default/generated/pm/default/oh-package.json5 +1 -0
  48. package/harmony/pdfview/build/default/generated/r/default/ResourceTable.h +24 -0
  49. package/harmony/pdfview/build/default/intermediates/loader/default/loader.json +25 -0
  50. package/harmony/pdfview/build/default/intermediates/merge_profile/default/module.json +29 -0
  51. package/harmony/pdfview/build/default/intermediates/patch/default/base_native_libs.json +1 -0
  52. package/harmony/pdfview/build/default/intermediates/process_profile/default/module.json +32 -0
  53. package/harmony/pdfview/build/default/intermediates/res/default/ResourceTable.txt +1 -0
  54. package/harmony/pdfview/build/default/intermediates/res/default/ark_module.json +1 -0
  55. package/harmony/pdfview/build/default/intermediates/res/default/ids_map/id_defined.json +7 -0
  56. package/harmony/pdfview/build/default/intermediates/res/default/module.json +28 -0
  57. package/harmony/pdfview/build/default/intermediates/res/default/opt-compression.json +1 -0
  58. package/harmony/pdfview/build/default/intermediates/res/default/resConfig.json +1 -0
  59. package/harmony/pdfview/build/default/intermediates/res/default/resources/rawfile/rnoh.profdata +0 -0
  60. package/harmony/pdfview/build/default/intermediates/res/default/resources.index +0 -0
  61. package/harmony/pdfview/build/default/outputs/default/pdfview.har +0 -0
  62. package/harmony/pdfview/oh-package-lock.json5 +9 -6
  63. package/harmony/pdfview/oh-package.json5 +1 -1
  64. package/harmony/pdfview.har +0 -0
  65. package/index.d.ts +72 -72
  66. package/index.js +491 -491
  67. package/index.js.flow +67 -67
  68. package/package.json +57 -61
package/index.js CHANGED
@@ -1,491 +1,491 @@
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
- Platform,
15
- StyleSheet,
16
- Image,
17
- Text,
18
- requireNativeComponent
19
- } from 'react-native';
20
- import PdfViewNativeComponent, {
21
- Commands as PdfViewCommands,
22
- } from './fabric/RNPDFPdfNativeComponent';
23
- // import ReactNativeBlobUtil from 'react-native-blob-util';
24
- import RNPDFPdfView, { Commands as RNPDFCommands } from "./fabric/RTNPdfViewNativeComponent";
25
- import { ViewPropTypes } from 'deprecated-react-native-prop-types';
26
- const SHA1 = require('crypto-js/sha1');
27
- import PdfView from './PdfView';
28
-
29
- export default class Pdf extends Component {
30
-
31
- static propTypes = {
32
- ...ViewPropTypes,
33
- source: PropTypes.oneOfType([
34
- PropTypes.shape({
35
- uri: PropTypes.string,
36
- cache: PropTypes.bool,
37
- cacheFileName: PropTypes.string,
38
- expiration: PropTypes.number,
39
- }),
40
- // Opaque type returned by require('./test.pdf')
41
- PropTypes.number,
42
- ]).isRequired,
43
- page: PropTypes.number,
44
- scale: PropTypes.number,
45
- minScale: PropTypes.number,
46
- maxScale: PropTypes.number,
47
- horizontal: PropTypes.bool,
48
- spacing: PropTypes.number,
49
- password: PropTypes.string,
50
- renderActivityIndicator: PropTypes.func,
51
- enableAntialiasing: PropTypes.bool,
52
- enableAnnotationRendering: PropTypes.bool,
53
- showsHorizontalScrollIndicator: PropTypes.bool,
54
- showsVerticalScrollIndicator: PropTypes.bool,
55
- scrollEnabled: PropTypes.bool,
56
- enablePaging: PropTypes.bool,
57
- enableRTL: PropTypes.bool,
58
- fitPolicy: PropTypes.number,
59
- trustAllCerts: PropTypes.bool,
60
- singlePage: PropTypes.bool,
61
- onLoadComplete: PropTypes.func,
62
- onPageChanged: PropTypes.func,
63
- onError: PropTypes.func,
64
- onPageSingleTap: PropTypes.func,
65
- onScaleChanged: PropTypes.func,
66
- onPressLink: PropTypes.func,
67
-
68
- // Props that are not available in the earlier react native version, added to prevent crashed on android
69
- accessibilityLabel: PropTypes.string,
70
- importantForAccessibility: PropTypes.string,
71
- renderToHardwareTextureAndroid: PropTypes.string,
72
- testID: PropTypes.string,
73
- onLayout: PropTypes.bool,
74
- accessibilityLiveRegion: PropTypes.string,
75
- accessibilityComponentType: PropTypes.string,
76
- };
77
-
78
- static defaultProps = {
79
- password: "",
80
- scale: 1,
81
- minScale: 1,
82
- maxScale: 3,
83
- spacing: 10,
84
- fitPolicy: 2, //fit both
85
- horizontal: false,
86
- page: 1,
87
- enableAntialiasing: true,
88
- enableAnnotationRendering: true,
89
- showsHorizontalScrollIndicator: true,
90
- showsVerticalScrollIndicator: true,
91
- scrollEnabled: true,
92
- enablePaging: false,
93
- enableRTL: false,
94
- trustAllCerts: true,
95
- usePDFKit: true,
96
- singlePage: false,
97
- onLoadProgress: (percent) => {
98
- },
99
- onLoadComplete: (numberOfPages, path) => {
100
- },
101
- onPageChanged: (page, numberOfPages) => {
102
- },
103
- onError: (error) => {
104
- },
105
- onPageSingleTap: (page, x, y) => {
106
- },
107
- onScaleChanged: (scale) => {
108
- },
109
- onPressLink: (url) => {
110
- },
111
- };
112
-
113
- constructor(props) {
114
-
115
- super(props);
116
- this.state = {
117
- path: '',
118
- isDownloaded: false,
119
- progress: 0,
120
- };
121
-
122
- this.lastRNBFTask = null;
123
-
124
- }
125
-
126
- componentDidUpdate(prevProps) {
127
-
128
- const nextSource = Image.resolveAssetSource(this.props.source);
129
- const curSource = Image.resolveAssetSource(prevProps.source);
130
-
131
- if ((nextSource.uri !== curSource.uri)) {
132
- // if has download task, then cancel it.
133
- if (this.lastRNBFTask && this.lastRNBFTask.cancel) {
134
- this.lastRNBFTask.cancel(err => {
135
- this._loadFromSource(this.props.source);
136
- });
137
- this.lastRNBFTask = null;
138
- } else {
139
- this._loadFromSource(this.props.source);
140
- }
141
- }
142
- }
143
-
144
- componentDidMount() {
145
- this._mounted = true;
146
- this._loadFromSource(this.props.source);
147
- }
148
-
149
- componentWillUnmount() {
150
- this._mounted = false;
151
- if (this.lastRNBFTask) {
152
- // this.lastRNBFTask.cancel(err => {
153
- // });
154
- this.lastRNBFTask = null;
155
- }
156
-
157
- }
158
-
159
- _loadFromSource = (newSource) => {
160
-
161
- const source = Image.resolveAssetSource(newSource) || {};
162
-
163
- let uri = source.uri || '';
164
- // first set to initial state
165
- if (this._mounted) {
166
- this.setState({ isDownloaded: false, path: '', progress: 0 });
167
- }
168
- const filename = source.cacheFileName || SHA1(uri) + '.pdf';
169
- console.log('=== _loadFromSource uri: ' + uri);
170
- // const cacheFile = ReactNativeBlobUtil.fs.dirs.CacheDir + '/' + filename;
171
-
172
- this.setState({ path: uri});
173
-
174
- if (source.cache) {
175
- // ReactNativeBlobUtil.fs
176
- // .stat(cacheFile)
177
- // .then(stats => {
178
- // if (!Boolean(source.expiration) || (source.expiration * 1000 + stats.lastModified) > (new Date().getTime())) {
179
- // if (this._mounted) {
180
- // this.setState({ path: cacheFile, isDownloaded: true });
181
- // }
182
- // } else {
183
- // // cache expirated then reload it
184
- // this._prepareFile(source);
185
- // }
186
- // })
187
- // .catch(() => {
188
- // this._prepareFile(source);
189
- // })
190
-
191
- } else {
192
- this._prepareFile(source);
193
- }
194
- };
195
-
196
- _prepareFile = async (source) => {
197
-
198
- try {
199
- if (source.uri) {
200
- let uri = source.uri || '';
201
-
202
- const isNetwork = !!(uri && uri.match(/^https?:\/\//));
203
- const isAsset = !!(uri && uri.match(/^bundle-assets:\/\//));
204
- const isBase64 = !!(uri && uri.match(/^data:application\/pdf;base64/));
205
-
206
- const filename = source.cacheFileName || SHA1(uri) + '.pdf';
207
- console.log('=== _prepareFile filename: ' + filename);
208
- const cacheFile = ''
209
-
210
- // delete old cache file
211
- this._unlinkFile(cacheFile);
212
-
213
- if (isNetwork) {
214
- this._downloadFile(source, cacheFile);
215
- } else if (isAsset) {
216
- } else if (isBase64) {
217
- let data = uri.replace(/data:application\/pdf;base64,/i, '');
218
- } else {
219
- if (this._mounted) {
220
- this.setState({
221
- path: decodeURIComponent(uri.replace(/file:\/\//i, '')),
222
- isDownloaded: true,
223
- });
224
- }
225
- }
226
- } else {
227
- this._onError(new Error('no pdf source!'));
228
- }
229
- } catch (e) {
230
- this._onError(e)
231
- }
232
- };
233
-
234
- _downloadFile = async (source, cacheFile) => {
235
-
236
- if (this.lastRNBFTask) {
237
- this.lastRNBFTask.cancel(err => {
238
- });
239
- this.lastRNBFTask = null;
240
- }
241
-
242
- const tempCacheFile = cacheFile + '.tmp';
243
- this._unlinkFile(tempCacheFile);
244
-
245
- // this.lastRNBFTask = ReactNativeBlobUtil.config({
246
- // // response data will be saved to this path if it has access right.
247
- // path: tempCacheFile,
248
- // trusty: this.props.trustAllCerts,
249
- // })
250
- // .fetch(
251
- // source.method ? source.method : 'GET',
252
- // source.uri,
253
- // source.headers ? source.headers : {},
254
- // source.body ? source.body : ""
255
- // )
256
- // // listen to download progress event
257
- // .progress((received, total) => {
258
- // this.props.onLoadProgress && this.props.onLoadProgress(received / total);
259
- // if (this._mounted) {
260
- // this.setState({ progress: received / total });
261
- // }
262
- // })
263
- // .catch(async (error) => {
264
- // this._onError(error);
265
- // });
266
-
267
- // this.lastRNBFTask
268
- // .then(async (res) => {
269
-
270
- // this.lastRNBFTask = null;
271
-
272
- // if (res && res.respInfo && res.respInfo.headers && !res.respInfo.headers["Content-Encoding"] && !res.respInfo.headers["Transfer-Encoding"] && res.respInfo.headers["Content-Length"]) {
273
- // const expectedContentLength = res.respInfo.headers["Content-Length"];
274
- // let actualContentLength;
275
-
276
- // try {
277
- // const fileStats = await ReactNativeBlobUtil.fs.stat(res.path());
278
-
279
- // if (!fileStats || !fileStats.size) {
280
- // throw new Error("FileNotFound:" + source.uri);
281
- // }
282
-
283
- // actualContentLength = fileStats.size;
284
- // } catch (error) {
285
- // throw new Error("DownloadFailed:" + source.uri);
286
- // }
287
-
288
- // if (expectedContentLength != actualContentLength) {
289
- // throw new Error("DownloadFailed:" + source.uri);
290
- // }
291
- // }
292
-
293
- // this._unlinkFile(cacheFile);
294
- // ReactNativeBlobUtil.fs
295
- // .cp(tempCacheFile, cacheFile)
296
- // .then(() => {
297
- // if (this._mounted) {
298
- // this.setState({ path: cacheFile, isDownloaded: true, progress: 1 });
299
- // }
300
- // this._unlinkFile(tempCacheFile);
301
- // })
302
- // .catch(async (error) => {
303
- // throw error;
304
- // });
305
- // })
306
- // .catch(async (error) => {
307
- // this._unlinkFile(tempCacheFile);
308
- // this._unlinkFile(cacheFile);
309
- // this._onError(error);
310
- // });
311
-
312
- };
313
-
314
- _unlinkFile = async (file) => {
315
- try {
316
- // await ReactNativeBlobUtil.fs.unlink(file);
317
- } catch (e) {
318
-
319
- }
320
- }
321
-
322
- setNativeProps = nativeProps => {
323
- if (this._root) {
324
- this._root.setNativeProps(nativeProps);
325
- }
326
- };
327
-
328
- setPage(pageNumber) {
329
- if ((pageNumber === null) || (isNaN(pageNumber))) {
330
- throw new Error('Specified pageNumber is not a number');
331
- }
332
- if (!!global?.nativeFabricUIManager) {
333
- if (this._root) {
334
- PdfViewCommands.setNativePage(
335
- this._root,
336
- pageNumber,
337
- );
338
- }
339
- } else {
340
- this.setNativeProps({
341
- page: pageNumber
342
- });
343
- }
344
-
345
- }
346
-
347
- _onChange = (event) => {
348
-
349
- let message = event.nativeEvent.message.split('|');
350
- //__DEV__ && console.log("onChange: " + message);
351
- if (message.length > 0) {
352
- if (message.length > 5) {
353
- message[4] = message.splice(4).join('|');
354
- }
355
- if (message[0] === 'loadComplete') {
356
- let tableContents;
357
- try {
358
- tableContents = message[4] && JSON.parse(message[4]);
359
- } catch (e) {
360
- tableContents = message[4];
361
- }
362
- this.props.onLoadComplete && this.props.onLoadComplete(Number(message[1]), this.state.path, {
363
- width: Number(message[2]),
364
- height: Number(message[3]),
365
- },
366
- tableContents
367
- );
368
- } else if (message[0] === 'loadProgress') {
369
- this.props.onLoadProgress && this.props.onLoadProgress(Number(message[1]));
370
- } else if (message[0] === 'pageChanged') {
371
- this.props.onPageChanged && this.props.onPageChanged(Number(message[1]), Number(message[2]));
372
- } else if (message[0] === 'error') {
373
- this._onError(new Error(message[1]));
374
- } else if (message[0] === 'pageSingleTap') {
375
- this.props.onPageSingleTap && this.props.onPageSingleTap(Number(message[1]), Number(message[2]), Number(message[3]));
376
- } else if (message[0] === 'scaleChanged') {
377
- this.props.onScaleChanged && this.props.onScaleChanged(Number(message[1]));
378
- } else if (message[0] === 'linkPressed') {
379
- this.props.onPressLink && this.props.onPressLink(message[1]);
380
- }
381
- }
382
-
383
- };
384
-
385
- _onError = (error) => {
386
-
387
- this.props.onError && this.props.onError(error);
388
-
389
- };
390
-
391
- normalizeSource = (source) => {
392
- if (!source) return source;
393
- if (typeof source === 'number') return source;
394
- let { headers, ...rest } = source;
395
- let normalizedHeaders = undefined;
396
- if (headers && typeof headers === 'object' && !Array.isArray(headers)) {
397
- normalizedHeaders = Object.entries(headers).map(([key, value]) => ({
398
- key,
399
- value
400
- }));
401
- } else if (Array.isArray(headers)) {
402
- normalizedHeaders = headers;
403
- }
404
- return {
405
- ...rest,
406
- uri: source.uri || '',
407
- headers: normalizedHeaders
408
- };
409
- }
410
-
411
- render() {
412
- if (Platform.OS === "android" || Platform.OS === "ios" || Platform.OS === "windows") {
413
- return (
414
- <View style={[this.props.style, { overflow: 'hidden' }]}>
415
- {!this.state.isDownloaded ?
416
- (<View
417
- style={[styles.progressContainer, this.props.progressContainerStyle]}
418
- >
419
- {this.props.renderActivityIndicator
420
- ? this.props.renderActivityIndicator(this.state.progress)
421
- : <Text>{`${(this.state.progress * 100).toFixed(2)}%`}</Text>}
422
- </View>) : (
423
- Platform.OS === "android" || Platform.OS === "windows" ? (
424
- <PdfCustom
425
- ref={component => (this._root = component)}
426
- {...this.props}
427
- style={[{ flex: 1, backgroundColor: '#EEE' }, this.props.style]}
428
- path={this.state.path}
429
- onChange={this._onChange}
430
- />
431
- ) : (
432
- this.props.usePDFKit ? (
433
- <PdfCustom
434
- ref={component => (this._root = component)}
435
- {...this.props}
436
- style={[{ backgroundColor: '#EEE', overflow: 'hidden' }, this.props.style]}
437
- path={this.state.path}
438
- onChange={this._onChange}
439
- />
440
- ) : (<PdfView
441
- {...this.props}
442
- style={[{ backgroundColor: '#EEE', overflow: 'hidden' }, this.props.style]}
443
- path={this.state.path}
444
- onLoadComplete={this.props.onLoadComplete}
445
- onPageChanged={this.props.onPageChanged}
446
- onError={this._onError}
447
- onPageSingleTap={this.props.onPageSingleTap}
448
- onScaleChanged={this.props.onScaleChanged}
449
- onPressLink={this.props.onPressLink}
450
- />)
451
- )
452
- )}
453
- </View>);
454
- } else if (Platform.OS === "harmony") {
455
- console.log("===react-native-pdf style: " + JSON.stringify(this.props.style));
456
- return (
457
- <View style={[{ overflow: 'hidden' }, this.props.style]}>
458
- <RNPDFPdfView
459
- key={this.state.path}
460
- {...this.props}
461
- source={this.normalizeSource(this.props.source)}
462
- path={this.state.path}
463
- onChange={this._onChange}
464
- />
465
- </View>
466
- );
467
- } else {
468
- return (null);
469
- }
470
- }
471
- }
472
-
473
- if (Platform.OS === "android" || Platform.OS === "ios") {
474
- var PdfCustom = PdfViewNativeComponent;
475
- } else if (Platform.OS === "windows") {
476
- var PdfCustom = requireNativeComponent('RCTPdf', Pdf, {
477
- nativeOnly: { path: true, onChange: true },
478
- })
479
- }
480
-
481
- const styles = StyleSheet.create({
482
- progressContainer: {
483
- flex: 1,
484
- justifyContent: 'center',
485
- alignItems: 'center'
486
- },
487
- progressBar: {
488
- width: 200,
489
- height: 2
490
- }
491
- });
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
+ Platform,
15
+ StyleSheet,
16
+ Image,
17
+ Text,
18
+ requireNativeComponent
19
+ } from 'react-native';
20
+ import PdfViewNativeComponent, {
21
+ Commands as PdfViewCommands,
22
+ } from './fabric/RNPDFPdfNativeComponent';
23
+ // import ReactNativeBlobUtil from 'react-native-blob-util';
24
+ import RNPDFPdfView, { Commands as RNPDFCommands } from "./fabric/RTNPdfViewNativeComponent";
25
+ import { ViewPropTypes } from 'deprecated-react-native-prop-types';
26
+ const SHA1 = require('crypto-js/sha1');
27
+ import PdfView from './PdfView';
28
+
29
+ export default class Pdf extends Component {
30
+
31
+ static propTypes = {
32
+ ...ViewPropTypes,
33
+ source: PropTypes.oneOfType([
34
+ PropTypes.shape({
35
+ uri: PropTypes.string,
36
+ cache: PropTypes.bool,
37
+ cacheFileName: PropTypes.string,
38
+ expiration: PropTypes.number,
39
+ }),
40
+ // Opaque type returned by require('./test.pdf')
41
+ PropTypes.number,
42
+ ]).isRequired,
43
+ page: PropTypes.number,
44
+ scale: PropTypes.number,
45
+ minScale: PropTypes.number,
46
+ maxScale: PropTypes.number,
47
+ horizontal: PropTypes.bool,
48
+ spacing: PropTypes.number,
49
+ password: PropTypes.string,
50
+ renderActivityIndicator: PropTypes.func,
51
+ enableAntialiasing: PropTypes.bool,
52
+ enableAnnotationRendering: PropTypes.bool,
53
+ showsHorizontalScrollIndicator: PropTypes.bool,
54
+ showsVerticalScrollIndicator: PropTypes.bool,
55
+ scrollEnabled: PropTypes.bool,
56
+ enablePaging: PropTypes.bool,
57
+ enableRTL: PropTypes.bool,
58
+ fitPolicy: PropTypes.number,
59
+ trustAllCerts: PropTypes.bool,
60
+ singlePage: PropTypes.bool,
61
+ onLoadComplete: PropTypes.func,
62
+ onPageChanged: PropTypes.func,
63
+ onError: PropTypes.func,
64
+ onPageSingleTap: PropTypes.func,
65
+ onScaleChanged: PropTypes.func,
66
+ onPressLink: PropTypes.func,
67
+
68
+ // Props that are not available in the earlier react native version, added to prevent crashed on android
69
+ accessibilityLabel: PropTypes.string,
70
+ importantForAccessibility: PropTypes.string,
71
+ renderToHardwareTextureAndroid: PropTypes.string,
72
+ testID: PropTypes.string,
73
+ onLayout: PropTypes.bool,
74
+ accessibilityLiveRegion: PropTypes.string,
75
+ accessibilityComponentType: PropTypes.string,
76
+ };
77
+
78
+ static defaultProps = {
79
+ password: "",
80
+ scale: 1,
81
+ minScale: 1,
82
+ maxScale: 3,
83
+ spacing: 10,
84
+ fitPolicy: 2, //fit both
85
+ horizontal: false,
86
+ page: 1,
87
+ enableAntialiasing: true,
88
+ enableAnnotationRendering: true,
89
+ showsHorizontalScrollIndicator: true,
90
+ showsVerticalScrollIndicator: true,
91
+ scrollEnabled: true,
92
+ enablePaging: false,
93
+ enableRTL: false,
94
+ trustAllCerts: true,
95
+ usePDFKit: true,
96
+ singlePage: false,
97
+ onLoadProgress: (percent) => {
98
+ },
99
+ onLoadComplete: (numberOfPages, path) => {
100
+ },
101
+ onPageChanged: (page, numberOfPages) => {
102
+ },
103
+ onError: (error) => {
104
+ },
105
+ onPageSingleTap: (page, x, y) => {
106
+ },
107
+ onScaleChanged: (scale) => {
108
+ },
109
+ onPressLink: (url) => {
110
+ },
111
+ };
112
+
113
+ constructor(props) {
114
+
115
+ super(props);
116
+ this.state = {
117
+ path: '',
118
+ isDownloaded: false,
119
+ progress: 0,
120
+ };
121
+
122
+ this.lastRNBFTask = null;
123
+
124
+ }
125
+
126
+ componentDidUpdate(prevProps) {
127
+
128
+ const nextSource = Image.resolveAssetSource(this.props.source);
129
+ const curSource = Image.resolveAssetSource(prevProps.source);
130
+
131
+ if ((nextSource.uri !== curSource.uri)) {
132
+ // if has download task, then cancel it.
133
+ if (this.lastRNBFTask && this.lastRNBFTask.cancel) {
134
+ this.lastRNBFTask.cancel(err => {
135
+ this._loadFromSource(this.props.source);
136
+ });
137
+ this.lastRNBFTask = null;
138
+ } else {
139
+ this._loadFromSource(this.props.source);
140
+ }
141
+ }
142
+ }
143
+
144
+ componentDidMount() {
145
+ this._mounted = true;
146
+ this._loadFromSource(this.props.source);
147
+ }
148
+
149
+ componentWillUnmount() {
150
+ this._mounted = false;
151
+ if (this.lastRNBFTask) {
152
+ // this.lastRNBFTask.cancel(err => {
153
+ // });
154
+ this.lastRNBFTask = null;
155
+ }
156
+
157
+ }
158
+
159
+ _loadFromSource = (newSource) => {
160
+
161
+ const source = Image.resolveAssetSource(newSource) || {};
162
+
163
+ let uri = source.uri || '';
164
+ // first set to initial state
165
+ if (this._mounted) {
166
+ this.setState({ isDownloaded: false, path: '', progress: 0 });
167
+ }
168
+ const filename = source.cacheFileName || SHA1(uri) + '.pdf';
169
+ console.log('=== _loadFromSource uri: ' + uri);
170
+ // const cacheFile = ReactNativeBlobUtil.fs.dirs.CacheDir + '/' + filename;
171
+
172
+ this.setState({ path: uri});
173
+
174
+ if (source.cache) {
175
+ // ReactNativeBlobUtil.fs
176
+ // .stat(cacheFile)
177
+ // .then(stats => {
178
+ // if (!Boolean(source.expiration) || (source.expiration * 1000 + stats.lastModified) > (new Date().getTime())) {
179
+ // if (this._mounted) {
180
+ // this.setState({ path: cacheFile, isDownloaded: true });
181
+ // }
182
+ // } else {
183
+ // // cache expirated then reload it
184
+ // this._prepareFile(source);
185
+ // }
186
+ // })
187
+ // .catch(() => {
188
+ // this._prepareFile(source);
189
+ // })
190
+
191
+ } else {
192
+ this._prepareFile(source);
193
+ }
194
+ };
195
+
196
+ _prepareFile = async (source) => {
197
+
198
+ try {
199
+ if (source.uri) {
200
+ let uri = source.uri || '';
201
+
202
+ const isNetwork = !!(uri && uri.match(/^https?:\/\//));
203
+ const isAsset = !!(uri && uri.match(/^bundle-assets:\/\//));
204
+ const isBase64 = !!(uri && uri.match(/^data:application\/pdf;base64/));
205
+
206
+ const filename = source.cacheFileName || SHA1(uri) + '.pdf';
207
+ console.log('=== _prepareFile filename: ' + filename);
208
+ const cacheFile = ''
209
+
210
+ // delete old cache file
211
+ this._unlinkFile(cacheFile);
212
+
213
+ if (isNetwork) {
214
+ this._downloadFile(source, cacheFile);
215
+ } else if (isAsset) {
216
+ } else if (isBase64) {
217
+ let data = uri.replace(/data:application\/pdf;base64,/i, '');
218
+ } else {
219
+ if (this._mounted) {
220
+ this.setState({
221
+ path: decodeURIComponent(uri.replace(/file:\/\//i, '')),
222
+ isDownloaded: true,
223
+ });
224
+ }
225
+ }
226
+ } else {
227
+ this._onError(new Error('no pdf source!'));
228
+ }
229
+ } catch (e) {
230
+ this._onError(e)
231
+ }
232
+ };
233
+
234
+ _downloadFile = async (source, cacheFile) => {
235
+
236
+ if (this.lastRNBFTask) {
237
+ this.lastRNBFTask.cancel(err => {
238
+ });
239
+ this.lastRNBFTask = null;
240
+ }
241
+
242
+ const tempCacheFile = cacheFile + '.tmp';
243
+ this._unlinkFile(tempCacheFile);
244
+
245
+ // this.lastRNBFTask = ReactNativeBlobUtil.config({
246
+ // // response data will be saved to this path if it has access right.
247
+ // path: tempCacheFile,
248
+ // trusty: this.props.trustAllCerts,
249
+ // })
250
+ // .fetch(
251
+ // source.method ? source.method : 'GET',
252
+ // source.uri,
253
+ // source.headers ? source.headers : {},
254
+ // source.body ? source.body : ""
255
+ // )
256
+ // // listen to download progress event
257
+ // .progress((received, total) => {
258
+ // this.props.onLoadProgress && this.props.onLoadProgress(received / total);
259
+ // if (this._mounted) {
260
+ // this.setState({ progress: received / total });
261
+ // }
262
+ // })
263
+ // .catch(async (error) => {
264
+ // this._onError(error);
265
+ // });
266
+
267
+ // this.lastRNBFTask
268
+ // .then(async (res) => {
269
+
270
+ // this.lastRNBFTask = null;
271
+
272
+ // if (res && res.respInfo && res.respInfo.headers && !res.respInfo.headers["Content-Encoding"] && !res.respInfo.headers["Transfer-Encoding"] && res.respInfo.headers["Content-Length"]) {
273
+ // const expectedContentLength = res.respInfo.headers["Content-Length"];
274
+ // let actualContentLength;
275
+
276
+ // try {
277
+ // const fileStats = await ReactNativeBlobUtil.fs.stat(res.path());
278
+
279
+ // if (!fileStats || !fileStats.size) {
280
+ // throw new Error("FileNotFound:" + source.uri);
281
+ // }
282
+
283
+ // actualContentLength = fileStats.size;
284
+ // } catch (error) {
285
+ // throw new Error("DownloadFailed:" + source.uri);
286
+ // }
287
+
288
+ // if (expectedContentLength != actualContentLength) {
289
+ // throw new Error("DownloadFailed:" + source.uri);
290
+ // }
291
+ // }
292
+
293
+ // this._unlinkFile(cacheFile);
294
+ // ReactNativeBlobUtil.fs
295
+ // .cp(tempCacheFile, cacheFile)
296
+ // .then(() => {
297
+ // if (this._mounted) {
298
+ // this.setState({ path: cacheFile, isDownloaded: true, progress: 1 });
299
+ // }
300
+ // this._unlinkFile(tempCacheFile);
301
+ // })
302
+ // .catch(async (error) => {
303
+ // throw error;
304
+ // });
305
+ // })
306
+ // .catch(async (error) => {
307
+ // this._unlinkFile(tempCacheFile);
308
+ // this._unlinkFile(cacheFile);
309
+ // this._onError(error);
310
+ // });
311
+
312
+ };
313
+
314
+ _unlinkFile = async (file) => {
315
+ try {
316
+ // await ReactNativeBlobUtil.fs.unlink(file);
317
+ } catch (e) {
318
+
319
+ }
320
+ }
321
+
322
+ setNativeProps = nativeProps => {
323
+ if (this._root) {
324
+ this._root.setNativeProps(nativeProps);
325
+ }
326
+ };
327
+
328
+ setPage(pageNumber) {
329
+ if ((pageNumber === null) || (isNaN(pageNumber))) {
330
+ throw new Error('Specified pageNumber is not a number');
331
+ }
332
+ if (!!global?.nativeFabricUIManager) {
333
+ if (this._root) {
334
+ PdfViewCommands.setNativePage(
335
+ this._root,
336
+ pageNumber,
337
+ );
338
+ }
339
+ } else {
340
+ this.setNativeProps({
341
+ page: pageNumber
342
+ });
343
+ }
344
+
345
+ }
346
+
347
+ _onChange = (event) => {
348
+
349
+ let message = event.nativeEvent.message.split('|');
350
+ //__DEV__ && console.log("onChange: " + message);
351
+ if (message.length > 0) {
352
+ if (message.length > 5) {
353
+ message[4] = message.splice(4).join('|');
354
+ }
355
+ if (message[0] === 'loadComplete') {
356
+ let tableContents;
357
+ try {
358
+ tableContents = message[4] && JSON.parse(message[4]);
359
+ } catch (e) {
360
+ tableContents = message[4];
361
+ }
362
+ this.props.onLoadComplete && this.props.onLoadComplete(Number(message[1]), this.state.path, {
363
+ width: Number(message[2]),
364
+ height: Number(message[3]),
365
+ },
366
+ tableContents
367
+ );
368
+ } else if (message[0] === 'loadProgress') {
369
+ this.props.onLoadProgress && this.props.onLoadProgress(Number(message[1]));
370
+ } else if (message[0] === 'pageChanged') {
371
+ this.props.onPageChanged && this.props.onPageChanged(Number(message[1]), Number(message[2]));
372
+ } else if (message[0] === 'error') {
373
+ this._onError(new Error(message[1]));
374
+ } else if (message[0] === 'pageSingleTap') {
375
+ this.props.onPageSingleTap && this.props.onPageSingleTap(Number(message[1]), Number(message[2]), Number(message[3]));
376
+ } else if (message[0] === 'scaleChanged') {
377
+ this.props.onScaleChanged && this.props.onScaleChanged(Number(message[1]));
378
+ } else if (message[0] === 'linkPressed') {
379
+ this.props.onPressLink && this.props.onPressLink(message[1]);
380
+ }
381
+ }
382
+
383
+ };
384
+
385
+ _onError = (error) => {
386
+
387
+ this.props.onError && this.props.onError(error);
388
+
389
+ };
390
+
391
+ normalizeSource = (source) => {
392
+ if (!source) return source;
393
+ if (typeof source === 'number') return source;
394
+ let { headers, ...rest } = source;
395
+ let normalizedHeaders = undefined;
396
+ if (headers && typeof headers === 'object' && !Array.isArray(headers)) {
397
+ normalizedHeaders = Object.entries(headers).map(([key, value]) => ({
398
+ key,
399
+ value
400
+ }));
401
+ } else if (Array.isArray(headers)) {
402
+ normalizedHeaders = headers;
403
+ }
404
+ return {
405
+ ...rest,
406
+ uri: source.uri || '',
407
+ headers: normalizedHeaders
408
+ };
409
+ }
410
+
411
+ render() {
412
+ if (Platform.OS === "android" || Platform.OS === "ios" || Platform.OS === "windows") {
413
+ return (
414
+ <View style={[this.props.style, { overflow: 'hidden' }]}>
415
+ {!this.state.isDownloaded ?
416
+ (<View
417
+ style={[styles.progressContainer, this.props.progressContainerStyle]}
418
+ >
419
+ {this.props.renderActivityIndicator
420
+ ? this.props.renderActivityIndicator(this.state.progress)
421
+ : <Text>{`${(this.state.progress * 100).toFixed(2)}%`}</Text>}
422
+ </View>) : (
423
+ Platform.OS === "android" || Platform.OS === "windows" ? (
424
+ <PdfCustom
425
+ ref={component => (this._root = component)}
426
+ {...this.props}
427
+ style={[{ flex: 1, backgroundColor: '#EEE' }, this.props.style]}
428
+ path={this.state.path}
429
+ onChange={this._onChange}
430
+ />
431
+ ) : (
432
+ this.props.usePDFKit ? (
433
+ <PdfCustom
434
+ ref={component => (this._root = component)}
435
+ {...this.props}
436
+ style={[{ backgroundColor: '#EEE', overflow: 'hidden' }, this.props.style]}
437
+ path={this.state.path}
438
+ onChange={this._onChange}
439
+ />
440
+ ) : (<PdfView
441
+ {...this.props}
442
+ style={[{ backgroundColor: '#EEE', overflow: 'hidden' }, this.props.style]}
443
+ path={this.state.path}
444
+ onLoadComplete={this.props.onLoadComplete}
445
+ onPageChanged={this.props.onPageChanged}
446
+ onError={this._onError}
447
+ onPageSingleTap={this.props.onPageSingleTap}
448
+ onScaleChanged={this.props.onScaleChanged}
449
+ onPressLink={this.props.onPressLink}
450
+ />)
451
+ )
452
+ )}
453
+ </View>);
454
+ } else if (Platform.OS === "harmony") {
455
+ console.log("===react-native-pdf style: " + JSON.stringify(this.props.style));
456
+ return (
457
+ <View style={[{ overflow: 'hidden' }, this.props.style]}>
458
+ <RNPDFPdfView
459
+ key={this.state.path}
460
+ {...this.props}
461
+ source={this.normalizeSource(this.props.source)}
462
+ path={this.state.path}
463
+ onChange={this._onChange}
464
+ />
465
+ </View>
466
+ );
467
+ } else {
468
+ return (null);
469
+ }
470
+ }
471
+ }
472
+
473
+ if (Platform.OS === "android" || Platform.OS === "ios") {
474
+ var PdfCustom = PdfViewNativeComponent;
475
+ } else if (Platform.OS === "windows") {
476
+ var PdfCustom = requireNativeComponent('RCTPdf', Pdf, {
477
+ nativeOnly: { path: true, onChange: true },
478
+ })
479
+ }
480
+
481
+ const styles = StyleSheet.create({
482
+ progressContainer: {
483
+ flex: 1,
484
+ justifyContent: 'center',
485
+ alignItems: 'center'
486
+ },
487
+ progressBar: {
488
+ width: 200,
489
+ height: 2
490
+ }
491
+ });