diff --git a/index.js b/index.js index bd65d7e5..f31a68db 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,7 @@ class PSPDFKitView extends React.Component { onCloseButtonPressed={onCloseButtonPressedHandler} onStateChanged={this._onStateChanged} onDocumentSaved={this._onDocumentSaved} + onAnnotationTapped={this._onAnnotationTapped} /> ); } else { @@ -48,6 +49,12 @@ class PSPDFKitView extends React.Component { this.props.onDocumentSaved(event.nativeEvent); } }; + + _onAnnotationTapped = (event) => { + if (this.props.onAnnotationTapped) { + this.props.onAnnotationTapped(event.nativeEvent); + } + }; /** * Enters the annotation creation mode, showing the annotation creation toolbar. @@ -107,6 +114,12 @@ PSPDFKitView.propTypes = { * @platform ios */ showCloseButton: PropTypes.bool, + /** + * Controls wheter or not the default action for tapped annotations is processed. Defaults to processing the action (false). + * + * @platform ios + */ + disableDefaultActionForTappedAnnotations: PropTypes.bool, /** * Callback that is called when the user tapped the close button. * If you provide this function, you need to handle dismissal yourself. @@ -120,7 +133,13 @@ PSPDFKitView.propTypes = { * * @platform ios */ - onDocumentSaved: PropTypes.func, + onDocumentSaved: PropTypes.func, + /** + * Callback that is called when the user taps on an annotation. + * + * @platform ios + */ + onAnnotationTapped: PropTypes.func, /** * Callback that is called when the state of the PSPDFKitView changes. * Returns an object with the following structure: @@ -137,7 +156,6 @@ PSPDFKitView.propTypes = { * @platform android */ onStateChanged: PropTypes.func, - /** * fragmentTag: A tag used to identify a single PdfFragment in the view hierarchy. * This needs to be unique in the view hierarchy. diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.h b/ios/RCTPSPDFKit/RCTPSPDFKitView.h index c0c5ac65..604aa750 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.h +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.h @@ -18,7 +18,9 @@ @property (nonatomic, readonly) PSPDFViewController *pdfController; @property (nonatomic) BOOL hideNavigationBar; @property (nonatomic, readonly) UIBarButtonItem *closeButton; +@property (nonatomic) BOOL disableDefaultActionForTappedAnnotations; @property (nonatomic, copy) RCTBubblingEventBlock onCloseButtonPressed; @property (nonatomic, copy) RCTBubblingEventBlock onDocumentSaved; +@property (nonatomic, copy) RCTBubblingEventBlock onAnnotationTapped; @end diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index caf9cc6d..628f01ab 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -10,7 +10,7 @@ #import "RCTPSPDFKitView.h" #import -@interface RCTPSPDFKitView () +@interface RCTPSPDFKitView () @property (nonatomic, nullable) UIViewController *topController; @@ -21,6 +21,7 @@ @implementation RCTPSPDFKitView - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { _pdfController = [[PSPDFViewController alloc] init]; + _pdfController.delegate = self; _closeButton = [[UIBarButtonItem alloc] initWithImage:[PSPDFKit imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonPressed:)]; } @@ -105,4 +106,15 @@ - (void)pdfDocumentDidSave:(nonnull PSPDFDocument *)document { } } +#pragma mark - PSPDFViewControllerDelegate + +- (BOOL)pdfViewController:(PSPDFViewController *)pdfController didTapOnAnnotation:(PSPDFAnnotation *)annotation annotationPoint:(CGPoint)annotationPoint annotationView:(UIView *)annotationView pageView:(PSPDFPageView *)pageView viewPoint:(CGPoint)viewPoint { + if (self.onAnnotationTapped) { + NSData *annotationData = [annotation generateInstantJSONWithError:NULL]; + NSDictionary *annotationDictionary = [NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL]; + self.onAnnotationTapped(annotationDictionary); + } + return self.disableDefaultActionForTappedAnnotations; +} + @end diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m b/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m index eef0093a..eec49058 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m @@ -38,6 +38,8 @@ @implementation RCTPSPDFKitViewManager RCT_EXPORT_VIEW_PROPERTY(hideNavigationBar, BOOL) +RCT_EXPORT_VIEW_PROPERTY(disableDefaultActionForTappedAnnotations, BOOL) + RCT_REMAP_VIEW_PROPERTY(color, tintColor, UIColor) RCT_CUSTOM_VIEW_PROPERTY(showCloseButton, BOOL, RCTPSPDFKitView) { @@ -50,6 +52,8 @@ @implementation RCTPSPDFKitViewManager RCT_EXPORT_VIEW_PROPERTY(onDocumentSaved, RCTBubblingEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onAnnotationTapped, RCTBubblingEventBlock) + - (UIView *)view { return [[RCTPSPDFKitView alloc] init]; }