|
| 1 | +// Copyright © 2018 PSPDFKit GmbH. All rights reserved. |
| 2 | +// |
| 3 | +// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW |
| 4 | +// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT. |
| 5 | +// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. |
| 6 | +// This notice may not be removed from this file. |
| 7 | +// |
| 8 | + |
| 9 | +import PropTypes from "prop-types"; |
| 10 | +import React from "react"; |
| 11 | +import { |
| 12 | + requireNativeComponent, |
| 13 | + Platform, |
| 14 | + findNodeHandle, |
| 15 | + UIManager |
| 16 | +} from "react-native"; |
| 17 | + |
| 18 | +class PSPDFKitView extends React.Component { |
| 19 | + render() { |
| 20 | + if (Platform.OS === "ios" || Platform.OS === "android") { |
| 21 | + const onCloseButtonPressedHandler = this.props.onCloseButtonPressed |
| 22 | + ? event => { |
| 23 | + this.props.onCloseButtonPressed(event.nativeEvent); |
| 24 | + } |
| 25 | + : null; |
| 26 | + return ( |
| 27 | + <RCTPSPDFKitView |
| 28 | + ref="pdfView" |
| 29 | + {...this.props} |
| 30 | + onCloseButtonPressed={onCloseButtonPressedHandler} |
| 31 | + onStateChanged={this._onStateChanged} |
| 32 | + /> |
| 33 | + ); |
| 34 | + } else { |
| 35 | + return null; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + _onStateChanged = event => { |
| 40 | + if (this.props.onStateChanged) { |
| 41 | + this.props.onStateChanged(event.nativeEvent); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * Enters the annotation creation mode, showing the annotation creation toolbar. |
| 47 | + * |
| 48 | + * @platform android |
| 49 | + */ |
| 50 | + enterAnnotationCreationMode = function () { |
| 51 | + UIManager.dispatchViewManagerCommand( |
| 52 | + findNodeHandle(this.refs.pdfView), |
| 53 | + UIManager.RCTPSPDFKitView.Commands.enterAnnotationCreationMode, |
| 54 | + [] |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + /** |
| 59 | + * Exits the currently active mode, hiding all toolbars. |
| 60 | + * |
| 61 | + * @platform android |
| 62 | + */ |
| 63 | + exitCurrentlyActiveMode = function () { |
| 64 | + UIManager.dispatchViewManagerCommand( |
| 65 | + findNodeHandle(this.refs.pdfView), |
| 66 | + UIManager.RCTPSPDFKitView.Commands.exitCurrentlyActiveMode, |
| 67 | + [] |
| 68 | + ); |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +PSPDFKitView.propTypes = { |
| 73 | + /** |
| 74 | + * Path to the PDF file that should be displayed. |
| 75 | + */ |
| 76 | + document: PropTypes.string, |
| 77 | + /** |
| 78 | + * Configuration object, to customize the appearance and behavior of PSPDFKit. |
| 79 | + * See https://pspdfkit.com/guides/ios/current/getting-started/pspdfconfiguration/ for more information. |
| 80 | + * |
| 81 | + * Note: On iOS, set `useParentNavigationBar` to `true`, to use the parent navigation bar instead of creating its own, |
| 82 | + * if the view is already contained in a navigation controller (like when using NavigatorIOS, react-native-navigation, ...). |
| 83 | + */ |
| 84 | + configuration: PropTypes.object, |
| 85 | + /** |
| 86 | + * Page index of the document that will be shown. |
| 87 | + */ |
| 88 | + pageIndex: PropTypes.number, |
| 89 | + /** |
| 90 | + * Controls wheter a navigation bar is created and shown or not. Defaults to showing a navigation bar (false). |
| 91 | + * |
| 92 | + * @platform ios |
| 93 | + */ |
| 94 | + hideNavigationBar: PropTypes.bool, |
| 95 | + /** |
| 96 | + * Whether the close button should be shown in the navigation bar. Disabled by default. |
| 97 | + * Will call `onCloseButtonPressed` if it was provided, when tapped. |
| 98 | + * If `onCloseButtonPressed` was not provided, PSPDFKitView will be automatically dismissed. |
| 99 | + * |
| 100 | + * @platform ios |
| 101 | + */ |
| 102 | + showCloseButton: PropTypes.bool, |
| 103 | + /** |
| 104 | + * Callback that is called when the user tapped the close button. |
| 105 | + * If you provide this function, you need to handle dismissal yourself. |
| 106 | + * If you don't provide this function, PSPDFKitView will be automatically dismissed. |
| 107 | + * |
| 108 | + * @platform ios |
| 109 | + */ |
| 110 | + onCloseButtonPressed: PropTypes.func, |
| 111 | + /** |
| 112 | + * Callback that is called when the state of the PSPDFKitView changes. |
| 113 | + * Returns an object with the following structure: |
| 114 | + * { |
| 115 | + * documentLoaded: bool, |
| 116 | + * currentPageIndex: int, |
| 117 | + * pageCount: int, |
| 118 | + * annotationCreationActive: bool, |
| 119 | + * annotationEditingActive: bool, |
| 120 | + * textSelectionActive: bool, |
| 121 | + * formEditingActive: bool, |
| 122 | + * } |
| 123 | + * |
| 124 | + * @platform android |
| 125 | + */ |
| 126 | + onStateChanged: PropTypes.func, |
| 127 | + |
| 128 | + /** |
| 129 | + * fragmentTag: A tag used to identify a single PdfFragment in the view hierarchy. |
| 130 | + * This needs to be unique in the view hierarchy. |
| 131 | + * |
| 132 | + * @platform android |
| 133 | + */ |
| 134 | + fragmentTag: PropTypes.string |
| 135 | +}; |
| 136 | + |
| 137 | +if (Platform.OS === "ios" || Platform.OS === "android") { |
| 138 | + var RCTPSPDFKitView = requireNativeComponent( |
| 139 | + "RCTPSPDFKitView", |
| 140 | + PSPDFKitView, |
| 141 | + { |
| 142 | + nativeOnly: { |
| 143 | + testID: true, |
| 144 | + accessibilityComponentType: true, |
| 145 | + renderToHardwareTextureAndroid: true, |
| 146 | + accessibilityLabel: true, |
| 147 | + accessibilityLiveRegion: true, |
| 148 | + importantForAccessibility: true, |
| 149 | + onLayout: true, |
| 150 | + nativeID: true |
| 151 | + } |
| 152 | + } |
| 153 | + ); |
| 154 | + module.exports = PSPDFKitView; |
| 155 | +} |
0 commit comments