Skip to content

Commit caa59b8

Browse files
author
Rad Azzouz
committed
Implement getAllAnnotations() on iOS
1 parent 17e895f commit caa59b8

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,25 @@ class PSPDFKitView extends React.Component {
276276
}
277277
};
278278

279+
/**
280+
* Gets all annotations of the given type.
281+
*
282+
* @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.
283+
*
284+
* Returns a promise resolving an array with the following structure:
285+
* {'annotations' : [instantJson]}
286+
*/
287+
getAllAnnotations = function(type) {
288+
if (Platform.OS === "android") {
289+
//TODO: Implement Android here.
290+
} else if (Platform.OS === "ios") {
291+
return NativeModules.PSPDFKitViewManager.getAllAnnotations(
292+
type,
293+
findNodeHandle(this.refs.pdfView)
294+
);
295+
}
296+
};
297+
279298
/**
280299
* Applies the passed in document instant json.
281300
*

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error;
4444
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
4545
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error;
46+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error;
4647
- (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error;
4748

4849
/// Forms

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
247247
return annotationsJSON;
248248
}
249249

250+
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
251+
PSPDFDocument *document = self.pdfController.document;
252+
VALIDATE_DOCUMENT(document, nil)
253+
254+
NSArray<PSPDFAnnotation *> *annotations = [[document allAnnotationsOfType:type].allValues valueForKeyPath:@"@unionOfArrays.self"];
255+
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
256+
return @{@"annotations" : annotationsJSON};
257+
}
258+
250259
- (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
251260
NSData *data;
252261
if ([jsonAnnotations isKindOfClass:NSString.class]) {

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ @implementation RCTPSPDFKitViewManager
195195
});
196196
}
197197

198+
RCT_EXPORT_METHOD(getAllAnnotations:(NSString *)type reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199+
dispatch_async(dispatch_get_main_queue(), ^{
200+
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
201+
NSError *error;
202+
NSDictionary *annotations = [component getAllAnnotations:[RCTConvert annotationTypeFromInstantJSONType:type] error:&error];
203+
if (annotations) {
204+
resolve(annotations);
205+
} else {
206+
reject(@"error", @"Failed to get all annotations.", error);
207+
}
208+
});
209+
}
210+
198211
RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199212
dispatch_async(dispatch_get_main_queue(), ^{
200213
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];

samples/Catalog/Catalog.ios.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,26 @@ class ProgrammaticAnnotations extends Component {
783783
title="getAllUnsavedAnnotations"
784784
/>
785785
</View>
786+
<View>
787+
<Button
788+
onPress={async () => {
789+
// Get all annotations annotations from the document.
790+
await this.refs.pdfView
791+
.getAllAnnotations()
792+
.then(result => {
793+
if (result) {
794+
alert(JSON.stringify(result));
795+
} else {
796+
alert("Failed to get all annotations.");
797+
}
798+
})
799+
.catch(error => {
800+
alert(JSON.stringify(error));
801+
});
802+
}}
803+
title="getAllAnnotations"
804+
/>
805+
</View>
786806
</View>
787807
</View>
788808
);

0 commit comments

Comments
 (0)