Skip to content

Commit

Permalink
Merge branch 'fix/annotation-select-box' into version/1.26.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mattslight committed Nov 26, 2019
2 parents e4c2f7e + 73c3051 commit ae93813
Showing 1 changed file with 99 additions and 33 deletions.
132 changes: 99 additions & 33 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@

#define VALIDATE_DOCUMENT(document, ...) { if (!document.isValid) { NSLog(@"Document is invalid."); if (self.onDocumentLoadFailed) { self.onDocumentLoadFailed(@{@"error": @"Document is invalid."}); } return __VA_ARGS__; }}

@interface CustomImagePickerController : PSPDFImagePickerController
@end

@implementation CustomImagePickerController

- (PSPDFImageQuality)allowedImageQualities {
return PSPDFImageQualityLow; // This forces low. You can also return `PSPDFImageQualityAll` to allow the user to select the image quality.
}
@end

@interface CustomButtonAnnotationToolbar : PSPDFAnnotationToolbar
@end

@interface RCTPSPDFKitView ()<PSPDFDocumentDelegate, PSPDFViewControllerDelegate, PSPDFFlexibleToolbarContainerDelegate>

@property (nonatomic, nullable) UIViewController *topController;
Expand All @@ -25,16 +38,25 @@ @implementation RCTPSPDFKitView

- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_pdfController = [[PSPDFViewController alloc] init];
// Set configuration to use the custom annotation tool bar when initializing the PSPDFViewController.
// For more details, see `PSCCustomizeAnnotationToolbarExample.m` from PSPDFCatalog and our documentation here: https://pspdfkit.com/guides/ios/current/customizing-the-interface/customize-the-annotation-toolbar/
// _pdfController = [[PSPDFViewController alloc] initWithDocument:nil configuration:[PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
// [builder overrideClass:PSPDFAnnotationToolbar.class withClass:CustomButtonAnnotationToolbar.class];
// }]];

_pdfController.delegate = self;
_pdfController.annotationToolbarController.delegate = self;
_pdfController = [[PSPDFViewController alloc] initWithDocument:nil configuration:[PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
[builder overrideClass:PSPDFAnnotationToolbar.class withClass:CustomButtonAnnotationToolbar.class];
[builder overrideClass:PSPDFImagePickerController.class withClass:CustomImagePickerController.class];
}]];
_closeButton = [[UIBarButtonItem alloc] initWithImage:[PSPDFKitGlobal imageNamed:@"x"] style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonPressed:)];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationChangedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsAddedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsRemovedNotification object:nil];

//[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationChangedNotification object:nil];
//[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsAddedNotification object:nil];
//[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(annotationChangedNotification:) name:PSPDFAnnotationsRemovedNotification object:nil];
}

return self;
}

Expand All @@ -55,21 +77,21 @@ - (void)didMoveToWindow {
if (controller == nil || self.window == nil || self.topController != nil) {
return;
}

if (self.pdfController.configuration.useParentNavigationBar || self.hideNavigationBar) {
self.topController = self.pdfController;

} else {
self.topController = [[PSPDFNavigationController alloc] initWithRootViewController:self.pdfController];;
}

UIView *topControllerView = self.topController.view;
topControllerView.translatesAutoresizingMaskIntoConstraints = NO;

[self addSubview:topControllerView];
[controller addChildViewController:self.topController];
[self.topController didMoveToParentViewController:controller];

[NSLayoutConstraint activateConstraints:
@[[topControllerView.topAnchor constraintEqualToAnchor:self.topAnchor],
[topControllerView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
Expand All @@ -88,7 +110,7 @@ - (void)destroyViewControllerRelationship {
- (void)closeButtonPressed:(nullable id)sender {
if (self.onCloseButtonPressed) {
self.onCloseButtonPressed(@{});

} else {
// try to be smart and pop if we are not displayed modally.
BOOL shouldDismiss = YES;
Expand Down Expand Up @@ -190,7 +212,7 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil);

NSArray <PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex:pageIndex type:type];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
return @{@"annotations" : annotationsJSON};
Expand All @@ -206,31 +228,31 @@ - (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error {
NSLog(@"Invalid JSON Annotation.");
return NO;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;

BOOL success = NO;
if (data) {
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:error];
if (annotation) {
success = [document addAnnotations:@[annotation] options:nil];
}
}

if (!success) {
NSLog(@"Failed to add annotation.");
}

return success;
}

- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
BOOL success = NO;

NSArray<PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"];
for (PSPDFAnnotation *annotation in allAnnotations) {
// Remove the annotation if the uuids match.
Expand All @@ -239,7 +261,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
break;
}
}

if (!success) {
NSLog(@"Failed to remove annotation.");
}
Expand All @@ -249,7 +271,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)

PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
NSData *data = [document generateInstantJSONFromDocumentProvider:documentProvider error:error];
NSDictionary *annotationsJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:error];
Expand All @@ -275,7 +297,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
NSLog(@"Invalid JSON Annotations.");
return NO;
}

PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc] initWithData:data];
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)
Expand All @@ -284,7 +306,7 @@ - (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
if (!success) {
NSLog(@"Failed to add annotations.");
}

[self.pdfController reloadPageAtIndex:self.pdfController.pageIndex animated:NO];
return success;
}
Expand All @@ -296,17 +318,17 @@ - (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
NSLog(@"Invalid fully qualified name.");
return nil;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)

for (PSPDFFormElement *formElement in document.formParser.forms) {
if ([formElement.fullyQualifiedFieldName isEqualToString:fullyQualifiedName]) {
id formFieldValue = formElement.value;
return @{@"value": formFieldValue ?: [NSNull new]};
}
}

return @{@"error": @"Failed to get the form field value."};
}

Expand All @@ -315,7 +337,7 @@ - (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
NSLog(@"Invalid fully qualified name.");
return NO;
}

PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)

Expand Down Expand Up @@ -364,7 +386,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
}
return;
}

NSString *name = notification.name;
NSString *change;
if ([name isEqualToString:PSPDFAnnotationChangedNotification]) {
Expand All @@ -374,7 +396,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification {
} else if ([name isEqualToString:PSPDFAnnotationsRemovedNotification]) {
change = @"removed";
}

NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:NULL];
if (self.onAnnotationsChanged) {
self.onAnnotationsChanged(@{@"change" : change, @"annotations" : annotationsJSON});
Expand All @@ -391,7 +413,7 @@ - (void)setLeftBarButtonItems:(nullable NSArray <NSString *> *)items forViewMode
[leftItems addObject:barButtonItem];
}
}

if (viewMode.length) {
[self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
Expand All @@ -407,7 +429,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
[rightItems addObject:barButtonItem];
}
}

if (viewMode.length) {
[self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated];
} else {
Expand All @@ -422,7 +444,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
} else {
items = [self.pdfController.navigationItem leftBarButtonItems];
}

return [self buttonItemsStringFromUIBarButtonItems:items];
}

Expand All @@ -433,7 +455,7 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items forViewMod
} else {
items = [self.pdfController.navigationItem rightBarButtonItems];
}

return [self buttonItemsStringFromUIBarButtonItems:items];
}

Expand All @@ -453,7 +475,7 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController
break;
}
}

self.onStateChanged(@{@"documentLoaded" : @(isDocumentLoaded),
@"currentPageIndex" : @(pageIndex),
@"pageCount" : @(pageCount),
Expand All @@ -477,3 +499,47 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController
}

@end

@implementation CustomButtonAnnotationToolbar

///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Lifecycle

- (instancetype)initWithAnnotationStateManager:(PSPDFAnnotationStateManager *)annotationStateManager {
if ((self = [super initWithAnnotationStateManager:annotationStateManager])) {
PSPDFAnnotationGroupItem *freeText = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringFreeText];
PSPDFAnnotationGroupItem *underline = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringUnderline];

PSPDFAnnotationGroupItem *inkPen = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringInk variant:PSPDFAnnotationVariantStringInkPen];
PSPDFAnnotationGroupItem *inkHighlighter = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringInk variant:PSPDFAnnotationVariantStringInkHighlighter];

PSPDFAnnotationGroupItem *square = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringSquare];
PSPDFAnnotationGroupItem *circle = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringCircle];
PSPDFAnnotationGroupItem *line = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringLine];
PSPDFAnnotationGroupItem *polygon = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringPolygon];
PSPDFAnnotationGroupItem *polyline = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringPolyLine];

PSPDFAnnotationGroupItem *sound = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringSound];
PSPDFAnnotationGroupItem *image = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringImage];
PSPDFAnnotationGroupItem *link = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringLink];

PSPDFAnnotationGroupItem *eraser = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringEraser];
PSPDFAnnotationGroupItem *note = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringNote];


NSArray<PSPDFAnnotationGroup *> *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note, eraser]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]]];
PSPDFAnnotationToolbarConfiguration *compact = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:compactGroups];

NSArray<PSPDFAnnotationGroup *> *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]]];
PSPDFAnnotationToolbarConfiguration *regular = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:regularGroups];

self.configurations = @[regular, compact];
}
return self;
}

- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}

@end

0 comments on commit ae93813

Please sign in to comment.