Skip to content

Commit 817629a

Browse files
author
RadAzzouz
committed
Address feedback
1 parent 641cbd6 commit 817629a

File tree

5 files changed

+46
-52
lines changed

5 files changed

+46
-52
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class PSPDFKitView extends React.Component {
185185
[annotation]
186186
);
187187
} else if (Platform.OS === "ios") {
188-
NativeModules.PSPDFKitViewManager.addAnnotation(
188+
return NativeModules.PSPDFKitViewManager.addAnnotation(
189189
annotation,
190190
findNodeHandle(this.refs.pdfView)
191191
);
@@ -197,7 +197,7 @@ class PSPDFKitView extends React.Component {
197197
*
198198
* @param annotation InstantJson of the annotation to remove.
199199
*/
200-
removeAnnotation = function(annotationName) {
200+
removeAnnotation = function(annotation) {
201201
if (Platform.OS === "android") {
202202
// TODO: Uncomment once the Android implementaion is ready.
203203
// UIManager.dispatchViewManagerCommand(
@@ -206,7 +206,7 @@ class PSPDFKitView extends React.Component {
206206
// [annotation]
207207
// );
208208
} else if (Platform.OS === "ios") {
209-
NativeModules.PSPDFKitViewManager.removeAnnotation(
209+
return NativeModules.PSPDFKitViewManager.removeAnnotation(
210210
annotation,
211211
findNodeHandle(this.refs.pdfView)
212212
);
@@ -255,7 +255,7 @@ class PSPDFKitView extends React.Component {
255255
[annotations]
256256
);
257257
} else if (Platform.OS === "ios") {
258-
NativeModules.PSPDFKitViewManager.addAnnotations(
258+
return NativeModules.PSPDFKitViewManager.addAnnotations(
259259
annotations,
260260
findNodeHandle(this.refs.pdfView)
261261
);

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737

3838
/// Anotations
3939
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
40-
- (void)addAnnotation:(id)jsonAnnotation;
41-
- (void)removeAnnotation:(id)jsonAnnotation;
40+
- (BOOL)addAnnotation:(id)jsonAnnotation;
41+
- (BOOL)removeAnnotation:(id)jsonAnnotation;
4242
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
43-
- (void)addAnnotations:(NSString *)jsonAnnotations;
43+
- (BOOL)addAnnotations:(NSString *)jsonAnnotations;
4444

4545
/// Forms
4646
- (NSDictionary<NSString *, NSString *> *)getFormFieldValue:(NSString *)fullyQualifiedName;

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain
178178
return @{@"annotations" : annotationsJSON};
179179
}
180180

181-
- (void)addAnnotation:(id)jsonAnnotation {
181+
- (BOOL)addAnnotation:(id)jsonAnnotation {
182182
NSData *data;
183183
if ([jsonAnnotation isKindOfClass:NSString.class]) {
184184
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
185185
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
186186
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
187187
} else {
188188
NSLog(@"Invalid JSON Annotation.");
189-
return;
189+
return NO;
190190
}
191191

192192
PSPDFDocument *document = self.pdfController.document;
@@ -201,17 +201,19 @@ - (void)addAnnotation:(id)jsonAnnotation {
201201
if (!success) {
202202
NSLog(@"Failed to add annotation.");
203203
}
204+
205+
return success;
204206
}
205207

206-
- (void)removeAnnotation:(id)jsonAnnotation {
208+
- (BOOL)removeAnnotation:(id)jsonAnnotation {
207209
NSData *data;
208210
if ([jsonAnnotation isKindOfClass:NSString.class]) {
209211
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
210212
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
211213
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
212214
} else {
213215
NSLog(@"Invalid JSON Annotation.");
214-
return;
216+
return NO;
215217
}
216218

217219
PSPDFDocument *document = self.pdfController.document;
@@ -232,6 +234,7 @@ - (void)removeAnnotation:(id)jsonAnnotation {
232234
if (!success) {
233235
NSLog(@"Failed to remove annotation.");
234236
}
237+
return success;
235238
}
236239

237240
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
@@ -241,15 +244,15 @@ - (void)removeAnnotation:(id)jsonAnnotation {
241244
return annotationsJSON;
242245
}
243246

244-
- (void)addAnnotations:(id)jsonAnnotations {
247+
- (BOOL)addAnnotations:(id)jsonAnnotations {
245248
NSData *data;
246249
if ([jsonAnnotations isKindOfClass:NSString.class]) {
247250
data = [jsonAnnotations dataUsingEncoding:NSUTF8StringEncoding];
248251
} else if ([jsonAnnotations isKindOfClass:NSDictionary.class]) {
249252
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotations options:0 error:nil];;
250253
} else {
251254
NSLog(@"Invalid JSON Annotations.");
252-
return;
255+
return NO;
253256
}
254257

255258
PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc] initWithData:data];
@@ -259,6 +262,8 @@ - (void)addAnnotations:(id)jsonAnnotations {
259262
if (!success) {
260263
NSLog(@"Failed to add annotations.");
261264
}
265+
266+
return success;
262267
}
263268

264269
#pragma mark - Forms

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,27 @@ @implementation RCTPSPDFKitViewManager
109109
});
110110
}
111111

112-
RCT_EXPORT_METHOD(addAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag) {
112+
RCT_EXPORT_METHOD(addAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
113113
dispatch_async(dispatch_get_main_queue(), ^{
114114
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
115-
[component addAnnotation:jsonAnnotation];
115+
BOOL success = [component addAnnotation:jsonAnnotation];
116+
if (success) {
117+
resolve(@(success));
118+
} else {
119+
reject(@"error", @"Failed to add annotation.", nil);
120+
}
116121
});
117122
}
118123

119-
RCT_EXPORT_METHOD(removeAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag) {
124+
RCT_EXPORT_METHOD(removeAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
120125
dispatch_async(dispatch_get_main_queue(), ^{
121126
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
122-
[component removeAnnotation:jsonAnnotation];
127+
BOOL success = [component removeAnnotation:jsonAnnotation];
128+
if (success) {
129+
resolve(@(success));
130+
} else {
131+
reject(@"error", @"Failed to remove annotation.", nil);
132+
}
123133
});
124134
}
125135

@@ -135,10 +145,15 @@ @implementation RCTPSPDFKitViewManager
135145
});
136146
}
137147

138-
RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag) {
148+
RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
139149
dispatch_async(dispatch_get_main_queue(), ^{
140150
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
141-
[component addAnnotations:jsonAnnotations];
151+
BOOL success = [component addAnnotations:jsonAnnotations];
152+
if (success) {
153+
resolve(@(success));
154+
} else {
155+
reject(@"error", @"Failed to add annotations.", nil);
156+
}
142157
});
143158
}
144159

samples/Catalog/Catalog.ios.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -605,40 +605,14 @@ class ProgrammaticAnnotations extends Component {
605605
</View>
606606
<View>
607607
<Button
608-
onPress={() => {
608+
onPress={async () => {
609609
// Programmatically remove an existing ink annotation.
610-
const annotationJSON = {
611-
bbox: [
612-
89.586334228515625,
613-
98.5791015625,
614-
143.12948608398438,
615-
207.1583251953125
616-
],
617-
isDrawnNaturally: false,
618-
lineWidth: 5,
619-
lines: {
620-
intensities: [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
621-
points: [
622-
[
623-
[92.086334228515625, 101.07916259765625],
624-
[92.086334228515625, 202.15826416015625],
625-
[138.12950134277344, 303.2374267578125]
626-
],
627-
[
628-
[184.17266845703125, 101.07916259765625],
629-
[184.17266845703125, 202.15826416015625],
630-
[230.2158203125, 303.2374267578125]
631-
]
632-
]
633-
},
634-
opacity: 1,
635-
pageIndex: 0,
636-
name: "A167811E-6D10-4546-A147-B7AD775FE8AC",
637-
strokeColor: "#AA47BE",
638-
type: "pspdfkit/ink",
639-
v: 1
640-
};
641-
this.refs.pdfView.removeAnnotation(annotationJSON);
610+
const inkAnnotations = await this.refs.pdfView.getAnnotations(
611+
this.state.currentPageIndex,
612+
"pspdfkit/ink"
613+
);
614+
const firstInkAnnotation = inkAnnotations["annotations"][0];
615+
this.refs.pdfView.removeAnnotation(firstInkAnnotation);
642616
}}
643617
title="removeAnnotation"
644618
/>

0 commit comments

Comments
 (0)