From 5a911784a8a44681392915bdc00569d86044bc60 Mon Sep 17 00:00:00 2001 From: kkk Date: Mon, 21 Oct 2019 17:03:12 +0400 Subject: [PATCH 1/8] expose PSPDFImageQuality through PSPDFConfiguration --- .../Converters/RCTConvert+PSPDFConfiguration.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m b/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m index a608abeb..c59b61ec 100644 --- a/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m +++ b/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m @@ -218,6 +218,14 @@ + (PSPDFConfiguration *)PSPDFConfiguration:(id)json { PSPDFTextSelectionMenuActionNone, unsignedIntegerValue) +RCT_MULTI_ENUM_CONVERTER(PSPDFImageQuality, + (@{@"low" : @(PSPDFImageQualityLow), + @"medium" : @(PSPDFImageQualityMedium), + @"high" : @(PSPDFImageQualityHigh), + @"all" : @(PSPDFImageQualityAll), + PSPDFImageQualityAll, + unsignedIntegerValue) + RCT_MULTI_ENUM_CONVERTER(PSPDFAppearanceMode, (@{@"default" : @(PSPDFAppearanceModeDefault), @"sepia" : @(PSPDFAppearanceModeSepia), @@ -295,6 +303,7 @@ - (void)setupFromJSON:(id)json { SET(useParentNavigationBar, BOOL) SET(linkAction, PSPDFLinkAction) SET(allowedMenuActions, PSPDFTextSelectionMenuAction) + SET(allowedImageQualities, PSPDFImageQuality) SET(userInterfaceViewMode, PSPDFUserInterfaceViewMode) SET(userInterfaceViewAnimation, PSPDFUserInterfaceViewAnimation) SET_PROPERTY(showThumbnailBar, thumbnailBarMode, PSPDFThumbnailBarMode) From bb0a7f40779f10e1036d1cc5ffcaa6edcc3dfa21 Mon Sep 17 00:00:00 2001 From: kkk Date: Tue, 22 Oct 2019 11:49:56 +0400 Subject: [PATCH 2/8] implements CustomImagePickerController to always return PSPDFImageQualityLow --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index ceff59d6..cc276fbb 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -15,6 +15,17 @@ #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 RCTPSPDFKitView () @property (nonatomic, nullable) UIViewController *topController; @@ -28,6 +39,9 @@ - (instancetype)initWithFrame:(CGRect)frame { _pdfController = [[PSPDFViewController alloc] init]; _pdfController.delegate = self; _pdfController.annotationToolbarController.delegate = self; + _pdfController = [[PSPDFViewController alloc] initWithDocument:nil configuration:[PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) { + [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]; From 235967f4435390f4f6a5c9302dfb6ea44979b740 Mon Sep 17 00:00:00 2001 From: kkk Date: Tue, 22 Oct 2019 11:52:30 +0400 Subject: [PATCH 3/8] remove ImageQuality property from PSPDFCongifuration --- .../Converters/RCTConvert+PSPDFConfiguration.m | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m b/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m index c59b61ec..a608abeb 100644 --- a/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m +++ b/ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFConfiguration.m @@ -218,14 +218,6 @@ + (PSPDFConfiguration *)PSPDFConfiguration:(id)json { PSPDFTextSelectionMenuActionNone, unsignedIntegerValue) -RCT_MULTI_ENUM_CONVERTER(PSPDFImageQuality, - (@{@"low" : @(PSPDFImageQualityLow), - @"medium" : @(PSPDFImageQualityMedium), - @"high" : @(PSPDFImageQualityHigh), - @"all" : @(PSPDFImageQualityAll), - PSPDFImageQualityAll, - unsignedIntegerValue) - RCT_MULTI_ENUM_CONVERTER(PSPDFAppearanceMode, (@{@"default" : @(PSPDFAppearanceModeDefault), @"sepia" : @(PSPDFAppearanceModeSepia), @@ -303,7 +295,6 @@ - (void)setupFromJSON:(id)json { SET(useParentNavigationBar, BOOL) SET(linkAction, PSPDFLinkAction) SET(allowedMenuActions, PSPDFTextSelectionMenuAction) - SET(allowedImageQualities, PSPDFImageQuality) SET(userInterfaceViewMode, PSPDFUserInterfaceViewMode) SET(userInterfaceViewAnimation, PSPDFUserInterfaceViewAnimation) SET_PROPERTY(showThumbnailBar, thumbnailBarMode, PSPDFThumbnailBarMode) From 52a36e31de250cbb18b68cf416303b7066d14a1f Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Thu, 7 Nov 2019 09:23:50 +0400 Subject: [PATCH 4/8] add custom configuration --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 101 +++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index cc276fbb..3b4689cf 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -36,7 +36,12 @@ @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) { @@ -483,3 +488,97 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController } @end + +@implementation CustomButtonAnnotationToolbar + +/////////////////////////////////////////////////////////////////////////////////////////// +#pragma mark - Lifecycle + +- (instancetype)initWithAnnotationStateManager:(PSPDFAnnotationStateManager *)annotationStateManager { + if ((self = [super initWithAnnotationStateManager:annotationStateManager])) { + PSPDFAnnotationGroupItem *underline = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringUnderline]; + + PSPDFAnnotationGroupItem *freeText = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringFreeText]; + + PSPDFAnnotationGroupItem *note = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringNote]; + 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]; + + NSArray *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[inkPen]], [PSPDFAnnotationGroup groupWithItems:@[inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]]]; + PSPDFAnnotationToolbarConfiguration *compact = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:compactGroups]; + + NSArray *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[inkPen]], [PSPDFAnnotationGroup groupWithItems:@[inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]]]; + PSPDFAnnotationToolbarConfiguration *regular = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:regularGroups]; + + self.configurations = @[regular, compact]; + } + return self; +} + +- (void)dealloc { + [NSNotificationCenter.defaultCenter removeObserver:self]; +} + +/////////////////////////////////////////////////////////////////////////////////////////// +#pragma mark - Clear Button Action + +- (void)clearButtonPressed:(id)sender { + // Iterate over all visible pages and remove all but links and widgets (forms). + PSPDFViewController *pdfController = self.annotationStateManager.pdfController; + PSPDFDocument *document = pdfController.document; + for (PSPDFPageView *pageView in pdfController.visiblePageViews) { + NSArray *annotations = [document annotationsForPageAtIndex:pageView.pageIndex type:PSPDFAnnotationTypeAll & ~(PSPDFAnnotationTypeLink | PSPDFAnnotationTypeWidget)]; + [document removeAnnotations:annotations options:nil]; + + // Remove any annotation on the page as well (updates views) + // Alternatively, you can call `reloadData` on the pdfController as well. + for (PSPDFAnnotation *annotation in annotations) { + [pageView removeAnnotation:annotation options:nil animated:YES]; + } + } +} + +/////////////////////////////////////////////////////////////////////////////////////////// +#pragma mark - Notifications + +// If we detect annotation changes, schedule a reload. +- (void)annotationChangedNotification:(NSNotification *)notification { + // Re-evaluate toolbar button + if (self.window) { + [self updateClearAnnotationButton]; + } +} + +- (void)willShowSpreadViewNotification:(NSNotification *)notification { + [self updateClearAnnotationButton]; +} + +/////////////////////////////////////////////////////////////////////////////////////////// +#pragma mark - PSPDFAnnotationStateManagerDelegate + +- (void)annotationStateManager:(PSPDFAnnotationStateManager *)manager didChangeUndoState:(BOOL)undoEnabled redoState:(BOOL)redoEnabled { + [super annotationStateManager:manager didChangeUndoState:undoEnabled redoState:redoEnabled]; + [self updateClearAnnotationButton]; +} + +/////////////////////////////////////////////////////////////////////////////////////////// +#pragma mark - Private + +- (void)updateClearAnnotationButton { + __block BOOL annotationsFound = NO; + PSPDFViewController *pdfController = self.annotationStateManager.pdfController; + [pdfController.visiblePageIndexes enumerateIndexesUsingBlock:^(NSUInteger pageIndex, BOOL *stop) { + NSArray *annotations = [pdfController.document annotationsForPageAtIndex:pageIndex type:PSPDFAnnotationTypeAll & ~(PSPDFAnnotationTypeLink | PSPDFAnnotationTypeWidget)]; + if (annotations.count > 0) { + annotationsFound = YES; + *stop = YES; + } + }]; + self.clearAnnotationsButton.enabled = annotationsFound; +} + +@end From 27fc3e966ad4553110993397594c66927331b666 Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Thu, 7 Nov 2019 17:58:34 +0400 Subject: [PATCH 5/8] removes clear annotations button from example and joins custom classes together --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 159 +++++++++++------------------- 1 file changed, 60 insertions(+), 99 deletions(-) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index 3b4689cf..8fbf50a7 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -23,7 +23,9 @@ @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 () @@ -38,22 +40,23 @@ - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // 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 = [[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]; } - + return self; } @@ -74,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], @@ -107,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; @@ -209,7 +212,7 @@ - (void)flexibleToolbarContainerDidHide:(PSPDFFlexibleToolbarContainer *)contain - (NSDictionary *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error { PSPDFDocument *document = self.pdfController.document; VALIDATE_DOCUMENT(document, nil); - + NSArray *annotations = [document annotationsForPageAtIndex:pageIndex type:type]; NSArray *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error]; return @{@"annotations" : annotationsJSON}; @@ -225,11 +228,11 @@ - (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]; @@ -237,11 +240,11 @@ - (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error { success = [document addAnnotations:@[annotation] options:nil]; } } - + if (!success) { NSLog(@"Failed to add annotation."); } - + return success; } @@ -249,7 +252,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID { PSPDFDocument *document = self.pdfController.document; VALIDATE_DOCUMENT(document, NO) BOOL success = NO; - + NSArray *allAnnotations = [[document allAnnotationsOfType:PSPDFAnnotationTypeAll].allValues valueForKeyPath:@"@unionOfArrays.self"]; for (PSPDFAnnotation *annotation in allAnnotations) { // Remove the annotation if the uuids match. @@ -258,7 +261,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID { break; } } - + if (!success) { NSLog(@"Failed to remove annotation."); } @@ -268,7 +271,7 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID { - (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]; @@ -294,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) @@ -303,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; } @@ -315,49 +318,57 @@ - (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."}; } -- (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName { +- (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fullyQualifiedName { if (fullyQualifiedName.length == 0) { NSLog(@"Invalid fully qualified name."); - return; + return NO; } - + PSPDFDocument *document = self.pdfController.document; - VALIDATE_DOCUMENT(document) - + VALIDATE_DOCUMENT(document, NO) + + BOOL success = NO; for (PSPDFFormElement *formElement in document.formParser.forms) { if ([formElement.fullyQualifiedFieldName isEqualToString:fullyQualifiedName]) { if ([formElement isKindOfClass:PSPDFButtonFormElement.class]) { if ([value isEqualToString:@"selected"]) { [(PSPDFButtonFormElement *)formElement select]; + success = YES; } else if ([value isEqualToString:@"deselected"]) { [(PSPDFButtonFormElement *)formElement deselect]; + success = YES; } } else if ([formElement isKindOfClass:PSPDFChoiceFormElement.class]) { ((PSPDFChoiceFormElement *)formElement).selectedIndices = [NSIndexSet indexSetWithIndex:value.integerValue]; + success = YES; } else if ([formElement isKindOfClass:PSPDFTextFieldFormElement.class]) { formElement.contents = value; + success = YES; } else if ([formElement isKindOfClass:PSPDFSignatureFormElement.class]) { NSLog(@"Signature form elements are not supported."); + success = NO; } else { NSLog(@"Unsupported form element."); + success = NO; } break; } } + return success; } #pragma mark - Notifications @@ -375,7 +386,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification { } return; } - + NSString *name = notification.name; NSString *change; if ([name isEqualToString:PSPDFAnnotationChangedNotification]) { @@ -385,7 +396,7 @@ - (void)annotationChangedNotification:(NSNotification *)notification { } else if ([name isEqualToString:PSPDFAnnotationsRemovedNotification]) { change = @"removed"; } - + NSArray *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:NULL]; if (self.onAnnotationsChanged) { self.onAnnotationsChanged(@{@"change" : change, @"annotations" : annotationsJSON}); @@ -402,7 +413,7 @@ - (void)setLeftBarButtonItems:(nullable NSArray *)items forViewMode [leftItems addObject:barButtonItem]; } } - + if (viewMode.length) { [self.pdfController.navigationItem setLeftBarButtonItems:[leftItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated]; } else { @@ -418,7 +429,7 @@ - (void)setRightBarButtonItems:(nullable NSArray *)items forViewMod [rightItems addObject:barButtonItem]; } } - + if (viewMode.length) { [self.pdfController.navigationItem setRightBarButtonItems:[rightItems copy] forViewMode:[RCTConvert PSPDFViewMode:viewMode] animated:animated]; } else { @@ -433,7 +444,7 @@ - (void)setRightBarButtonItems:(nullable NSArray *)items forViewMod } else { items = [self.pdfController.navigationItem leftBarButtonItems]; } - + return [self buttonItemsStringFromUIBarButtonItems:items]; } @@ -444,7 +455,7 @@ - (void)setRightBarButtonItems:(nullable NSArray *)items forViewMod } else { items = [self.pdfController.navigationItem rightBarButtonItems]; } - + return [self buttonItemsStringFromUIBarButtonItems:items]; } @@ -464,7 +475,7 @@ - (void)onStateChangedForPDFViewController:(PSPDFViewController *)pdfController break; } } - + self.onStateChanged(@{@"documentLoaded" : @(isDocumentLoaded), @"currentPageIndex" : @(pageIndex), @"pageCount" : @(pageCount), @@ -496,22 +507,30 @@ @implementation CustomButtonAnnotationToolbar - (instancetype)initWithAnnotationStateManager:(PSPDFAnnotationStateManager *)annotationStateManager { if ((self = [super initWithAnnotationStateManager:annotationStateManager])) { - PSPDFAnnotationGroupItem *underline = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringUnderline]; - PSPDFAnnotationGroupItem *freeText = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringFreeText]; + PSPDFAnnotationGroupItem *underline = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringUnderline]; - PSPDFAnnotationGroupItem *note = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringNote]; 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]; - NSArray *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[inkPen]], [PSPDFAnnotationGroup groupWithItems:@[inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]]]; + PSPDFAnnotationGroupItem *eraser = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringEraser]; + PSPDFAnnotationGroupItem *note = [PSPDFAnnotationGroupItem itemWithType:PSPDFAnnotationStringNote]; + + + NSArray *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[eraser, note]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; PSPDFAnnotationToolbarConfiguration *compact = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:compactGroups]; - NSArray *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[inkPen]], [PSPDFAnnotationGroup groupWithItems:@[inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]]]; + NSArray *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]], [PSPDFAnnotationGroup groupWithItems:@[polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; PSPDFAnnotationToolbarConfiguration *regular = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:regularGroups]; self.configurations = @[regular, compact]; @@ -523,62 +542,4 @@ - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; } -/////////////////////////////////////////////////////////////////////////////////////////// -#pragma mark - Clear Button Action - -- (void)clearButtonPressed:(id)sender { - // Iterate over all visible pages and remove all but links and widgets (forms). - PSPDFViewController *pdfController = self.annotationStateManager.pdfController; - PSPDFDocument *document = pdfController.document; - for (PSPDFPageView *pageView in pdfController.visiblePageViews) { - NSArray *annotations = [document annotationsForPageAtIndex:pageView.pageIndex type:PSPDFAnnotationTypeAll & ~(PSPDFAnnotationTypeLink | PSPDFAnnotationTypeWidget)]; - [document removeAnnotations:annotations options:nil]; - - // Remove any annotation on the page as well (updates views) - // Alternatively, you can call `reloadData` on the pdfController as well. - for (PSPDFAnnotation *annotation in annotations) { - [pageView removeAnnotation:annotation options:nil animated:YES]; - } - } -} - -/////////////////////////////////////////////////////////////////////////////////////////// -#pragma mark - Notifications - -// If we detect annotation changes, schedule a reload. -- (void)annotationChangedNotification:(NSNotification *)notification { - // Re-evaluate toolbar button - if (self.window) { - [self updateClearAnnotationButton]; - } -} - -- (void)willShowSpreadViewNotification:(NSNotification *)notification { - [self updateClearAnnotationButton]; -} - -/////////////////////////////////////////////////////////////////////////////////////////// -#pragma mark - PSPDFAnnotationStateManagerDelegate - -- (void)annotationStateManager:(PSPDFAnnotationStateManager *)manager didChangeUndoState:(BOOL)undoEnabled redoState:(BOOL)redoEnabled { - [super annotationStateManager:manager didChangeUndoState:undoEnabled redoState:redoEnabled]; - [self updateClearAnnotationButton]; -} - -/////////////////////////////////////////////////////////////////////////////////////////// -#pragma mark - Private - -- (void)updateClearAnnotationButton { - __block BOOL annotationsFound = NO; - PSPDFViewController *pdfController = self.annotationStateManager.pdfController; - [pdfController.visiblePageIndexes enumerateIndexesUsingBlock:^(NSUInteger pageIndex, BOOL *stop) { - NSArray *annotations = [pdfController.document annotationsForPageAtIndex:pageIndex type:PSPDFAnnotationTypeAll & ~(PSPDFAnnotationTypeLink | PSPDFAnnotationTypeWidget)]; - if (annotations.count > 0) { - annotationsFound = YES; - *stop = YES; - } - }]; - self.clearAnnotationsButton.enabled = annotationsFound; -} - @end From 1ec3971f852ed095b96c971b5ed6169cb48ce2a9 Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Thu, 7 Nov 2019 18:56:28 +0400 Subject: [PATCH 6/8] shuffle annotation icons around --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index 8fbf50a7..73595c55 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -518,19 +518,19 @@ - (instancetype)initWithAnnotationStateManager:(PSPDFAnnotationStateManager *)an 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 *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[eraser, note]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; + NSArray *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; PSPDFAnnotationToolbarConfiguration *compact = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:compactGroups]; - NSArray *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line]], [PSPDFAnnotationGroup groupWithItems:@[polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; + NSArray *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; PSPDFAnnotationToolbarConfiguration *regular = [[PSPDFAnnotationToolbarConfiguration alloc] initWithAnnotationGroups:regularGroups]; self.configurations = @[regular, compact]; From fcc3115f9097a6570c4e692b8b507850cea96e5a Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Wed, 13 Nov 2019 17:29:41 +0400 Subject: [PATCH 7/8] adjust order of icons --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index 73595c55..ed9bfc74 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -518,19 +518,19 @@ - (instancetype)initWithAnnotationStateManager:(PSPDFAnnotationStateManager *)an 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 *compactGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; + NSArray *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 *regularGroups = @[[PSPDFAnnotationGroup groupWithItems:@[freeText, underline]], [PSPDFAnnotationGroup groupWithItems:@[inkPen, inkHighlighter]], [PSPDFAnnotationGroup groupWithItems:@[note]], [PSPDFAnnotationGroup groupWithItems:@[eraser]], [PSPDFAnnotationGroup groupWithItems:@[square, circle, line, polygon, polyline]], [PSPDFAnnotationGroup groupWithItems:@[image, sound, link]]]; + NSArray *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]; From 73c30517dda158c59f2fd26372ccdd5cc8b0dd12 Mon Sep 17 00:00:00 2001 From: Matthew Slight Date: Wed, 20 Nov 2019 22:28:22 +0400 Subject: [PATCH 8/8] don't subscribe to annotationChangeNotification --- ios/RCTPSPDFKit/RCTPSPDFKitView.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ios/RCTPSPDFKit/RCTPSPDFKitView.m b/ios/RCTPSPDFKit/RCTPSPDFKitView.m index ed9bfc74..e10ff7f2 100644 --- a/ios/RCTPSPDFKit/RCTPSPDFKitView.m +++ b/ios/RCTPSPDFKit/RCTPSPDFKitView.m @@ -52,9 +52,9 @@ - (instancetype)initWithFrame:(CGRect)frame { }]]; _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;