|
1 | | -// Copyright © 2018 PSPDFKit GmbH. All rights reserved. |
| 1 | +// Copyright © 2018 PSPDFKit GmbH. All rights reserved. |
2 | 2 | // |
3 | 3 | // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW |
4 | 4 | // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT. |
5 | 5 | // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. |
6 | 6 | // This notice may not be removed from this file. |
7 | 7 | // |
8 | | -import { requireNativeComponent, ViewPropTypes } from "react-native"; |
9 | | -import PropTypes from 'prop-types'; |
10 | | - |
11 | | -var iface = { |
12 | | - name: 'PSPDFKitView', |
13 | | - propTypes: { |
14 | | - /** |
15 | | - * Path to the PDF file that should be displayed. |
16 | | - */ |
17 | | - document: PropTypes.string, |
18 | | - /** |
19 | | - * Page index of the document that will be shown. |
20 | | - */ |
21 | | - pageIndex: PropTypes.number, |
22 | | - /** |
23 | | - * Controls wheter a navigation bar is created and shown or not. Defaults to showing a navigation bar (false). |
24 | | - */ |
25 | | - hideNavigationBar: PropTypes.bool, |
26 | | - ...ViewPropTypes |
27 | | - }, |
| 8 | +import PropTypes from "prop-types"; |
| 9 | +import React from "react"; |
| 10 | +import { |
| 11 | + requireNativeComponent, |
| 12 | + ViewPropTypes, |
| 13 | + findNodeHandle, |
| 14 | + UIManager |
| 15 | +} from "react-native"; |
| 16 | + |
| 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 | + ); |
| 31 | + } |
| 32 | + |
| 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) |
| 60 | + } |
| 61 | + |
| 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 | + ); |
| 94 | + } |
| 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 | + ); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +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 |
28 | 159 | }; |
29 | 160 |
|
30 | | -module.exports = requireNativeComponent("ReactPSPDFKitView", iface); |
| 161 | +var RCTPSPDFKitView = requireNativeComponent( |
| 162 | + "RCTPSPDFKitView", |
| 163 | + PSPDFKitView, |
| 164 | + {} |
| 165 | +); |
| 166 | +module.exports = PSPDFKitView; |
0 commit comments