From caa59b86f3a5be8d9624cc3bf94b73801c139d9e Mon Sep 17 00:00:00 2001 From: Rad Azzouz Date: Tue, 6 Aug 2019 10:32:04 -0400 Subject: [PATCH 1/3] Implement `getAllAnnotations()` on iOS --- index.js | 19 +++++++++++++++++++ ios/RCTPSPDFKit/RCTPSPDFKitView.h | 1 + ios/RCTPSPDFKit/RCTPSPDFKitView.m | 9 +++++++++ ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m | 13 +++++++++++++ samples/Catalog/Catalog.ios.js | 20 ++++++++++++++++++++ 5 files changed, 62 insertions(+) diff --git a/index.js b/index.js index 7af9cff8..916cf09a 100644 --- a/index.js +++ b/index.js @@ -276,6 +276,25 @@ class PSPDFKitView extends React.Component { } }; + /** + * Gets all annotations of the given type. + * + * @param type The type of annotations to get (See here for types https://pspdfkit.com/guides/server/current/api/json-format/) or null to get all annotations. + * + * Returns a promise resolving an array with the following structure: + * {'annotations' : [instantJson]} + */ + getAllAnnotations = function(type) { + if (Platform.OS === "android") { + //TODO: Implement Android here. + } else if (Platform.OS === "ios") { + return NativeModules.PSPDFKitViewManager.getAllAnnotations( + type, + findNodeHandle(this.refs.pdfView) + ); + } + }; + /** * Applies the passed in document instant json. * diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.h b/ios/RCTPSPDFKit/RCTPSPDFKitView.h index 85fe2b87..5a5f40ae 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.h +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.h @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error; - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID; - (NSDictionary *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error; +- (NSDictionary *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error; - (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error; /// Forms diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index 03f4d243..ef8161c7 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -247,6 +247,15 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID { return annotationsJSON; } +- (NSDictionary *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error { + PSPDFDocument *document = self.pdfController.document; + VALIDATE_DOCUMENT(document, nil) + + NSArray *annotations = [[document allAnnotationsOfType:type].allValues valueForKeyPath:@"@unionOfArrays.self"]; + NSArray *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error]; + return @{@"annotations" : annotationsJSON}; +} + - (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error { NSData *data; if ([jsonAnnotations isKindOfClass:NSString.class]) { diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m b/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m index 2638d841..6ec6dd70 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m @@ -195,6 +195,19 @@ @implementation RCTPSPDFKitViewManager }); } +RCT_EXPORT_METHOD(getAllAnnotations:(NSString *)type reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { + dispatch_async(dispatch_get_main_queue(), ^{ + RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag]; + NSError *error; + NSDictionary *annotations = [component getAllAnnotations:[RCTConvert annotationTypeFromInstantJSONType:type] error:&error]; + if (annotations) { + resolve(annotations); + } else { + reject(@"error", @"Failed to get all annotations.", error); + } + }); +} + RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { dispatch_async(dispatch_get_main_queue(), ^{ RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag]; diff --git a/samples/Catalog/Catalog.ios.js b/samples/Catalog/Catalog.ios.js index 17d9d89e..e59477c7 100644 --- a/samples/Catalog/Catalog.ios.js +++ b/samples/Catalog/Catalog.ios.js @@ -783,6 +783,26 @@ class ProgrammaticAnnotations extends Component { title="getAllUnsavedAnnotations" /> + +