|
8 | 8 | import PropTypes from "prop-types"; |
9 | 9 | import React from "react"; |
10 | 10 | import { |
11 | | - requireNativeComponent, |
12 | | - ViewPropTypes, |
13 | | - findNodeHandle, |
14 | | - UIManager |
| 11 | + requireNativeComponent, |
| 12 | + ViewPropTypes, |
| 13 | + findNodeHandle, |
| 14 | + UIManager |
15 | 15 | } from "react-native"; |
16 | 16 |
|
17 | 17 | class PSPDFKitView extends React.Component { |
18 | | - _nextRequestId = 1; |
19 | | - _requestMap = new Map(); |
20 | | - |
21 | | - render() { |
22 | | - return ( |
23 | | - <RCTPSPDFKitView |
24 | | - ref="pdfView" |
25 | | - {...this.props} |
26 | | - onAnnotationsChanged={this._onAnnotationsChanged} |
27 | | - onDocumentSaved={this._onDocumentSaved} |
28 | | - onDocumentSaveFailed={this._onDocumentSaveFailed} |
29 | | - onDataReturned={this._onDataReturned}/> |
30 | | - ); |
| 18 | + _nextRequestId = 1; |
| 19 | + _requestMap = new Map(); |
| 20 | + |
| 21 | + render() { |
| 22 | + return ( |
| 23 | + <RCTPSPDFKitView |
| 24 | + ref="pdfView" |
| 25 | + {...this.props} |
| 26 | + onAnnotationsChanged={this._onAnnotationsChanged} |
| 27 | + onDocumentSaved={this._onDocumentSaved} |
| 28 | + onDocumentSaveFailed={this._onDocumentSaveFailed} |
| 29 | + onDataReturned={this._onDataReturned} |
| 30 | + /> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + _onAnnotationsChanged = event => { |
| 35 | + if (this.props.onAnnotationsChanged) { |
| 36 | + this.props.onAnnotationsChanged(event.nativeEvent); |
31 | 37 | } |
| 38 | + }; |
32 | 39 |
|
33 | | - _onAnnotationsChanged = (event) => { |
34 | | - if (this.props.onAnnotationsChanged) { |
35 | | - this.props.onAnnotationsChanged(event.nativeEvent); |
36 | | - } |
37 | | - }; |
38 | | - |
39 | | - _onDocumentSaved = (event) => { |
40 | | - if (this.props.onDocumentSaved) { |
41 | | - this.props.onDocumentSaved(event.nativeEvent); |
42 | | - } |
43 | | - }; |
44 | | - |
45 | | - _onDocumentSaveFailed = (event) => { |
46 | | - if (this.props.onDocumentSaveFailed) { |
47 | | - this.props.onDocumentSaveFailed(event.nativeEvent); |
48 | | - } |
49 | | - }; |
50 | | - |
51 | | - _onDataReturned = (event) => { |
52 | | - let {requestId, result, error} = event.nativeEvent |
53 | | - let promise = this._requestMap[requestId] |
54 | | - if (result) { |
55 | | - promise.resolve(result) |
56 | | - } else { |
57 | | - promise.reject(error) |
58 | | - } |
59 | | - this._requestMap.delete(requestId) |
| 40 | + _onDocumentSaved = event => { |
| 41 | + if (this.props.onDocumentSaved) { |
| 42 | + this.props.onDocumentSaved(event.nativeEvent); |
60 | 43 | } |
| 44 | + }; |
61 | 45 |
|
62 | | - /** |
63 | | - * Enters the annotation creation mode, showing the annotation creation toolbar. |
64 | | - * |
65 | | - */ |
66 | | - enterAnnotationCreationMode = function () { |
67 | | - UIManager.dispatchViewManagerCommand( |
68 | | - findNodeHandle(this.refs.pdfView), |
69 | | - UIManager.RCTPSPDFKitView.Commands.enterAnnotationCreationMode, |
70 | | - [] |
71 | | - ); |
72 | | - }; |
73 | | - |
74 | | - /** |
75 | | - * Exits the currently active mode, hiding all toolbars. |
76 | | - */ |
77 | | - exitCurrentlyActiveMode = function () { |
78 | | - UIManager.dispatchViewManagerCommand( |
79 | | - findNodeHandle(this.refs.pdfView), |
80 | | - UIManager.RCTPSPDFKitView.Commands.exitCurrentlyActiveMode, |
81 | | - [] |
82 | | - ); |
83 | | - }; |
84 | | - |
85 | | - /** |
86 | | - * Saves the currently opened document. |
87 | | - */ |
88 | | - saveCurrentDocument = function () { |
89 | | - UIManager.dispatchViewManagerCommand( |
90 | | - findNodeHandle(this.refs.pdfView), |
91 | | - UIManager.RCTPSPDFKitView.Commands.saveCurrentDocument, |
92 | | - [] |
93 | | - ); |
| 46 | + _onDocumentSaveFailed = event => { |
| 47 | + if (this.props.onDocumentSaveFailed) { |
| 48 | + this.props.onDocumentSaveFailed(event.nativeEvent); |
94 | 49 | } |
95 | | - |
96 | | - /** |
97 | | - * Gets all annotations of the given type from the page. |
98 | | - * |
99 | | - * @param pageIndex The page to get the annotations for. |
100 | | - * |
101 | | - * Returns a promise resolving an array with the following structure: |
102 | | - * {'annotations' : [instantJson]} |
103 | | - */ |
104 | | - getAnnotations = function (pageIndex) { |
105 | | - let requestId = this._nextRequestId++ |
106 | | - let requestMap = this._requestMap; |
107 | | - |
108 | | - // We create a promise here that will be resolved once onDataReturned is called. |
109 | | - let promise = new Promise(function (resolve, reject) { |
110 | | - requestMap[requestId] = {'resolve': resolve, 'reject': reject} |
111 | | - }) |
112 | | - |
113 | | - UIManager.dispatchViewManagerCommand( |
114 | | - findNodeHandle(this.refs.pdfView), |
115 | | - UIManager.RCTPSPDFKitView.Commands.getAnnotations, |
116 | | - [requestId, pageIndex] |
117 | | - ); |
118 | | - |
119 | | - return promise |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Adds a new annotation to the current document. |
124 | | - * |
125 | | - * @param annotation InstantJson of the annotation to add. |
126 | | - */ |
127 | | - addAnnotation = function (annotation) { |
128 | | - UIManager.dispatchViewManagerCommand( |
129 | | - findNodeHandle(this.refs.pdfView), |
130 | | - UIManager.RCTPSPDFKitView.Commands.addAnnotation, |
131 | | - [annotation] |
132 | | - ); |
| 50 | + }; |
| 51 | + |
| 52 | + _onDataReturned = event => { |
| 53 | + let { requestId, result, error } = event.nativeEvent; |
| 54 | + let promise = this._requestMap[requestId]; |
| 55 | + if (result) { |
| 56 | + promise.resolve(result); |
| 57 | + } else { |
| 58 | + promise.reject(error); |
133 | 59 | } |
| 60 | + this._requestMap.delete(requestId); |
| 61 | + }; |
| 62 | + |
| 63 | + /** |
| 64 | + * Enters the annotation creation mode, showing the annotation creation toolbar. |
| 65 | + * |
| 66 | + */ |
| 67 | + enterAnnotationCreationMode = function() { |
| 68 | + UIManager.dispatchViewManagerCommand( |
| 69 | + findNodeHandle(this.refs.pdfView), |
| 70 | + UIManager.RCTPSPDFKitView.Commands.enterAnnotationCreationMode, |
| 71 | + [] |
| 72 | + ); |
| 73 | + }; |
| 74 | + |
| 75 | + /** |
| 76 | + * Exits the currently active mode, hiding all toolbars. |
| 77 | + */ |
| 78 | + exitCurrentlyActiveMode = function() { |
| 79 | + UIManager.dispatchViewManagerCommand( |
| 80 | + findNodeHandle(this.refs.pdfView), |
| 81 | + UIManager.RCTPSPDFKitView.Commands.exitCurrentlyActiveMode, |
| 82 | + [] |
| 83 | + ); |
| 84 | + }; |
| 85 | + |
| 86 | + /** |
| 87 | + * Saves the currently opened document. |
| 88 | + */ |
| 89 | + saveCurrentDocument = function() { |
| 90 | + UIManager.dispatchViewManagerCommand( |
| 91 | + findNodeHandle(this.refs.pdfView), |
| 92 | + UIManager.RCTPSPDFKitView.Commands.saveCurrentDocument, |
| 93 | + [] |
| 94 | + ); |
| 95 | + }; |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets all annotations of the given type from the page. |
| 99 | + * |
| 100 | + * @param pageIndex The page to get the annotations for. |
| 101 | + * |
| 102 | + * Returns a promise resolving an array with the following structure: |
| 103 | + * {'annotations' : [instantJson]} |
| 104 | + */ |
| 105 | + getAnnotations = function(pageIndex) { |
| 106 | + let requestId = this._nextRequestId++; |
| 107 | + let requestMap = this._requestMap; |
| 108 | + |
| 109 | + // We create a promise here that will be resolved once onDataReturned is called. |
| 110 | + let promise = new Promise(function(resolve, reject) { |
| 111 | + requestMap[requestId] = { resolve: resolve, reject: reject }; |
| 112 | + }); |
| 113 | + |
| 114 | + UIManager.dispatchViewManagerCommand( |
| 115 | + findNodeHandle(this.refs.pdfView), |
| 116 | + UIManager.RCTPSPDFKitView.Commands.getAnnotations, |
| 117 | + [requestId, pageIndex] |
| 118 | + ); |
| 119 | + |
| 120 | + return promise; |
| 121 | + }; |
| 122 | + |
| 123 | + /** |
| 124 | + * Adds a new annotation to the current document. |
| 125 | + * |
| 126 | + * @param annotation InstantJson of the annotation to add. |
| 127 | + */ |
| 128 | + addAnnotation = function(annotation) { |
| 129 | + UIManager.dispatchViewManagerCommand( |
| 130 | + findNodeHandle(this.refs.pdfView), |
| 131 | + UIManager.RCTPSPDFKitView.Commands.addAnnotation, |
| 132 | + [annotation] |
| 133 | + ); |
| 134 | + }; |
134 | 135 | } |
135 | 136 |
|
136 | 137 | PSPDFKitView.propTypes = { |
137 | | - /** |
138 | | - * Path to the PDF file that should be displayed. |
139 | | - */ |
140 | | - document: PropTypes.string, |
141 | | - /** |
142 | | - * Page index of the document that will be shown. |
143 | | - */ |
144 | | - pageIndex: PropTypes.number, |
145 | | - /** |
146 | | - * Controls whether a navigation bar is created and shown or not. Defaults to showing a navigation bar (false). |
147 | | - */ |
148 | | - hideNavigationBar: PropTypes.bool, |
149 | | - /** |
150 | | - * Callback that is called when an annotation is added, changed, or removed. |
151 | | - * Returns an object with the following structure: |
152 | | - * { |
153 | | - * change: "changed"|"added"|"removed", |
154 | | - * annotations: [instantJson] |
155 | | - * } |
156 | | - */ |
157 | | - onAnnotationsChanged: PropTypes.func, |
158 | | - ...ViewPropTypes |
| 138 | + /** |
| 139 | + * Path to the PDF file that should be displayed. |
| 140 | + */ |
| 141 | + document: PropTypes.string, |
| 142 | + /** |
| 143 | + * Page index of the document that will be shown. |
| 144 | + */ |
| 145 | + pageIndex: PropTypes.number, |
| 146 | + /** |
| 147 | + * Controls whether a navigation bar is created and shown or not. Defaults to showing a navigation bar (false). |
| 148 | + */ |
| 149 | + hideNavigationBar: PropTypes.bool, |
| 150 | + /** |
| 151 | + * Callback that is called when an annotation is added, changed, or removed. |
| 152 | + * Returns an object with the following structure: |
| 153 | + * { |
| 154 | + * change: "changed"|"added"|"removed", |
| 155 | + * annotations: [instantJson] |
| 156 | + * } |
| 157 | + */ |
| 158 | + onAnnotationsChanged: PropTypes.func, |
| 159 | + ...ViewPropTypes |
159 | 160 | }; |
160 | 161 |
|
161 | 162 | var RCTPSPDFKitView = requireNativeComponent( |
162 | | - "RCTPSPDFKitView", |
163 | | - PSPDFKitView, |
164 | | - {} |
| 163 | + "RCTPSPDFKitView", |
| 164 | + PSPDFKitView, |
| 165 | + {} |
165 | 166 | ); |
166 | 167 | module.exports = PSPDFKitView; |
0 commit comments