Skip to content

Commit 3f3aeae

Browse files
authored
Merge pull request #88 from PSPDFKit/rad/add-onAnnotationTapped-ios
Add onAnnotationTapped event for the UI Component
2 parents 89104ea + fe7d3a1 commit 3f3aeae

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class PSPDFKitView extends React.Component {
3030
onCloseButtonPressed={onCloseButtonPressedHandler}
3131
onStateChanged={this._onStateChanged}
3232
onDocumentSaved={this._onDocumentSaved}
33+
onAnnotationTapped={this._onAnnotationTapped}
3334
/>
3435
);
3536
} else {
@@ -48,6 +49,12 @@ class PSPDFKitView extends React.Component {
4849
this.props.onDocumentSaved(event.nativeEvent);
4950
}
5051
};
52+
53+
_onAnnotationTapped = (event) => {
54+
if (this.props.onAnnotationTapped) {
55+
this.props.onAnnotationTapped(event.nativeEvent);
56+
}
57+
};
5158

5259
/**
5360
* Enters the annotation creation mode, showing the annotation creation toolbar.
@@ -107,6 +114,12 @@ PSPDFKitView.propTypes = {
107114
* @platform ios
108115
*/
109116
showCloseButton: PropTypes.bool,
117+
/**
118+
* Controls wheter or not the default action for tapped annotations is processed. Defaults to processing the action (false).
119+
*
120+
* @platform ios
121+
*/
122+
disableDefaultActionForTappedAnnotations: PropTypes.bool,
110123
/**
111124
* Callback that is called when the user tapped the close button.
112125
* If you provide this function, you need to handle dismissal yourself.
@@ -120,7 +133,13 @@ PSPDFKitView.propTypes = {
120133
*
121134
* @platform ios
122135
*/
123-
onDocumentSaved: PropTypes.func,
136+
onDocumentSaved: PropTypes.func,
137+
/**
138+
* Callback that is called when the user taps on an annotation.
139+
*
140+
* @platform ios
141+
*/
142+
onAnnotationTapped: PropTypes.func,
124143
/**
125144
* Callback that is called when the state of the PSPDFKitView changes.
126145
* Returns an object with the following structure:
@@ -137,7 +156,6 @@ PSPDFKitView.propTypes = {
137156
* @platform android
138157
*/
139158
onStateChanged: PropTypes.func,
140-
141159
/**
142160
* fragmentTag: A tag used to identify a single PdfFragment in the view hierarchy.
143161
* This needs to be unique in the view hierarchy.

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
@property (nonatomic, readonly) PSPDFViewController *pdfController;
1919
@property (nonatomic) BOOL hideNavigationBar;
2020
@property (nonatomic, readonly) UIBarButtonItem *closeButton;
21+
@property (nonatomic) BOOL disableDefaultActionForTappedAnnotations;
2122
@property (nonatomic, copy) RCTBubblingEventBlock onCloseButtonPressed;
2223
@property (nonatomic, copy) RCTBubblingEventBlock onDocumentSaved;
24+
@property (nonatomic, copy) RCTBubblingEventBlock onAnnotationTapped;
2325

2426
@end

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "RCTPSPDFKitView.h"
1111
#import <React/RCTUtils.h>
1212

13-
@interface RCTPSPDFKitView ()<PSPDFDocumentDelegate>
13+
@interface RCTPSPDFKitView ()<PSPDFDocumentDelegate, PSPDFViewControllerDelegate>
1414

1515
@property (nonatomic, nullable) UIViewController *topController;
1616

@@ -21,6 +21,7 @@ @implementation RCTPSPDFKitView
2121
- (instancetype)initWithFrame:(CGRect)frame {
2222
if ((self = [super initWithFrame:frame])) {
2323
_pdfController = [[PSPDFViewController alloc] init];
24+
_pdfController.delegate = self;
2425
_closeButton = [[UIBarButtonItem alloc] initWithImage:[PSPDFKit imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonPressed:)];
2526
}
2627

@@ -105,4 +106,15 @@ - (void)pdfDocumentDidSave:(nonnull PSPDFDocument *)document {
105106
}
106107
}
107108

109+
#pragma mark - PSPDFViewControllerDelegate
110+
111+
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController didTapOnAnnotation:(PSPDFAnnotation *)annotation annotationPoint:(CGPoint)annotationPoint annotationView:(UIView<PSPDFAnnotationPresenting> *)annotationView pageView:(PSPDFPageView *)pageView viewPoint:(CGPoint)viewPoint {
112+
if (self.onAnnotationTapped) {
113+
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
114+
NSDictionary *annotationDictionary = [NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL];
115+
self.onAnnotationTapped(annotationDictionary);
116+
}
117+
return self.disableDefaultActionForTappedAnnotations;
118+
}
119+
108120
@end

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ @implementation RCTPSPDFKitViewManager
3838

3939
RCT_EXPORT_VIEW_PROPERTY(hideNavigationBar, BOOL)
4040

41+
RCT_EXPORT_VIEW_PROPERTY(disableDefaultActionForTappedAnnotations, BOOL)
42+
4143
RCT_REMAP_VIEW_PROPERTY(color, tintColor, UIColor)
4244

4345
RCT_CUSTOM_VIEW_PROPERTY(showCloseButton, BOOL, RCTPSPDFKitView) {
@@ -50,6 +52,8 @@ @implementation RCTPSPDFKitViewManager
5052

5153
RCT_EXPORT_VIEW_PROPERTY(onDocumentSaved, RCTBubblingEventBlock)
5254

55+
RCT_EXPORT_VIEW_PROPERTY(onAnnotationTapped, RCTBubblingEventBlock)
56+
5357
- (UIView *)view {
5458
return [[RCTPSPDFKitView alloc] init];
5559
}

0 commit comments

Comments
 (0)