Skip to content

Commit 11d7003

Browse files
committed
Use uuid instead of name for removing annotation
1 parent 8ecba78 commit 11d7003

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotation.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ @implementation RCTConvert (PSPDFAnnotation)
1414
+ (NSArray <NSDictionary *> *)instantJSONFromAnnotations:(NSArray <PSPDFAnnotation *> *) annotations {
1515
NSMutableArray <NSDictionary *> *annotationsJSON = [NSMutableArray new];
1616
for (PSPDFAnnotation *annotation in annotations) {
17+
NSDictionary <NSString *, NSString *> *uuidDict = @{@"uuid" : [annotation valueForKey:@"uuid"]};
1718
NSData *annotationData = [annotation generateInstantJSONWithError:NULL];
1819
if (annotationData) {
19-
NSDictionary *annotationDictionary = [NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL];
20+
NSMutableDictionary *annotationDictionary = [[NSJSONSerialization JSONObjectWithData:annotationData options:kNilOptions error:NULL] mutableCopy];
21+
[annotationDictionary addEntriesFromDictionary:uuidDict];
2022
if (annotationDictionary) {
2123
[annotationsJSON addObject:annotationDictionary];
2224
}
23-
} else if (annotation.name) {
24-
// We only generate Instant JSON data for attached annotations. When an annotation is deleted, we only send the annotation name.
25-
[annotationsJSON addObject:@{@"name" : annotation.name}];
25+
} else {
26+
// We only generate Instant JSON data for attached annotations. When an annotation is deleted, we only set the annotation uuid.
27+
[annotationsJSON addObject:uuidDict];
2628
}
2729
}
2830

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/// Anotations
3939
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
4040
- (BOOL)addAnnotation:(id)jsonAnnotation;
41-
- (BOOL)removeAnnotation:(id)jsonAnnotation;
41+
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
4242
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
4343
- (BOOL)addAnnotations:(NSString *)jsonAnnotations;
4444

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,31 +205,19 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
205205
return success;
206206
}
207207

208-
- (BOOL)removeAnnotation:(id)jsonAnnotation {
209-
NSData *data;
210-
if ([jsonAnnotation isKindOfClass:NSString.class]) {
211-
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
212-
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
213-
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
214-
} else {
215-
NSLog(@"Invalid JSON Annotation.");
216-
return NO;
217-
}
218-
208+
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
219209
PSPDFDocument *document = self.pdfController.document;
220-
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
221210

222211
BOOL success = NO;
223-
if (data) {
224-
PSPDFAnnotation *annotationToRemove = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:NULL];
225-
for (PSPDFAnnotation *annotation in [document annotationsForPageAtIndex:annotationToRemove.pageIndex type:annotationToRemove.type]) {
212+
213+
NSArray<PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"];
214+
for (PSPDFAnnotation *annotation in allAnnotations) {
226215
// Remove the annotation if the name matches.
227-
if ([annotation.name isEqualToString:annotationToRemove.name]) {
216+
if ([[annotation valueForKey:@"uuid"] isEqualToString:annotationUUID]) {
228217
success = [document removeAnnotations:@[annotation] options:nil];
229218
break;
230219
}
231220
}
232-
}
233221

234222
if (!success) {
235223
NSLog(@"Failed to remove annotation.");

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ @implementation RCTPSPDFKitViewManager
147147
RCT_EXPORT_METHOD(removeAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
148148
dispatch_async(dispatch_get_main_queue(), ^{
149149
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
150-
BOOL success = [component removeAnnotation:jsonAnnotation];
150+
BOOL success = [component removeAnnotationWithUUID:jsonAnnotation[@"uuid"]];
151151
if (success) {
152152
resolve(@(success));
153153
} else {

0 commit comments

Comments
 (0)