Skip to content

Commit

Permalink
Override toolbar button press CPM-23 (#572)
Browse files Browse the repository at this point in the history
* boilerplate code

* android implementation

* ios implementation

* updated API.md

* Updating package version

* revised API.md, added null check

* Updating package.json

* Updating package version

* revised ios implementation

* Updating package version

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
terryyoon-pdftron and github-actions[bot] authored Jun 30, 2022
1 parent 48eb963 commit 68fd438
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 1 deletion.
43 changes: 43 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,49 @@ Defines whether the viewer will add padding to take account of the system status
/>
```

#### overrideToolbarButtonBehavior
array of [`Config.Buttons`](./src/Config/Config.ts) constants, optional, defaults to none

Defines the option buttons in the top app nav bar or the bottom toolbar that will skip default behavior when pressed.
They will still be displayed in the toolbar, and the function [`onToolbarButtonPress`](#ontoolbarbuttonpress) will be called where custom behavior can be implemented.

```js
<DocumentView
overrideToolbarButtonBehavior={[Config.Buttons.shareButton, Config.Buttons.searchButton]}
onToolbarButtonPress={({id}) => {
if (id === Config.Buttons.shareButton) {
console.log('Share button pressed');
} else if (id === Config.Buttons.searchButton) {
console.log('Search button pressed');
}
}}
/>
```

#### onToolbarButtonPress
function, optional

This function is called when a toolbar item passed in to [`overrideToolbarButtonBehavior`](#overridetoolbarbuttonbehavior) is pressed.

Parameters:

Name | Type | Description
--- | --- | ---
id | string | one of [`Config.Buttons`](./src/Config/Config.ts) constants representing the item that has been pressed

```js
<DocumentView
overrideToolbarButtonBehavior={[Config.Buttons.shareButton, Config.Buttons.searchButton]}
onToolbarButtonPress={({id}) => {
if (id === Config.Buttons.shareButton) {
console.log('Share button pressed');
} else if (id === Config.Buttons.searchButton) {
console.log('Search button pressed');
}
}}
/>
```

### Layout

#### fitMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class Constants {
public static final String ON_ANNOTATION_FLATTENED = "onAnnotationFlattened";
public static final String ON_ANNOTATION_TOOLBAR_ITEM_PRESS = "onAnnotationToolbarItemPress";
public static final String ON_SAVED_SIGNATURES_CHANGED = "onSavedSignaturesChanged";
public static final String ON_TOOLBAR_BUTTON_PRESS = "onToolbarButtonPress";

// BUTTONS
public static final String BUTTON_TOOLS = "toolsButton";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,11 @@ public void setMaxSignatureCount(DocumentView documentView, int maxSignatureCoun
SignatureDialogFragment.MAX_SIGNATURES = maxSignatureCount;
}

@ReactProp(name = "overrideToolbarButtonBehavior")
public void setOverrideToolbarButtonBehavior(DocumentView documentView, @NonNull ReadableArray items) {
documentView.setOverrideToolbarButtonBehavior(items);
}

public void importBookmarkJson(int tag, String bookmarkJson) throws PDFNetException {
DocumentView documentView = mDocumentViews.get(tag);
if (documentView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public class DocumentView extends com.pdftron.pdf.controls.DocumentView2 {
private PDFViewCtrl.AnnotationManagerMode mAnnotationManagerUndoMode = PDFViewCtrl.AnnotationManagerMode.ADMIN_UNDO_OWN;
private File mCollabTempFile;

// toolbar buttons
private ArrayList<Object> mToolbarOverrideButtons;

// quick menu
private ArrayList<Object> mAnnotMenuItems;
private ArrayList<Object> mAnnotMenuOverrideItems;
Expand Down Expand Up @@ -713,6 +716,10 @@ public void setOverrideBehavior(@NonNull ReadableArray items) {
}
}

public void setOverrideToolbarButtonBehavior(ReadableArray items) {
mToolbarOverrideButtons = items != null ? items.toArrayList() : null;
}

public void setSignSignatureFieldsWithStamps(boolean signWithStamps) {
mSignWithStamps = signWithStamps;
}
Expand Down Expand Up @@ -1674,6 +1681,44 @@ private TopToolbarMenuId convButtonIdToMenuId(String item) {
return null;
}

@Nullable
private String convItemIdToString(int id) {
String buttonId = null;
if (id == R.id.action_tabs) {
buttonId = BUTTON_TABS;
} else if (id == R.id.action_search) {
buttonId = BUTTON_SEARCH;
} else if (id == R.id.action_viewmode) {
buttonId = BUTTON_VIEW_CONTROLS;
} else if (id == R.id.action_thumbnails) {
buttonId = BUTTON_THUMBNAILS;
} else if (id == R.id.action_outline) {
buttonId = BUTTON_OUTLINE_LIST;
} else if (id == R.id.undo) {
buttonId = BUTTON_UNDO;
} else if (id == R.id.action_share) {
buttonId = BUTTON_SHARE;
} else if (id == R.id.action_reflow_mode) {
buttonId = BUTTON_REFLOW;
} else if (id == R.id.action_editpages) {
buttonId = BUTTON_EDIT_PAGES;
} else if (id == R.id.action_export_options) {
buttonId = BUTTON_SAVE_COPY;
} else if (id == R.id.action_print) {
buttonId = BUTTON_PRINT;
} else if (id == R.id.action_file_attachment) {
buttonId = BUTTON_FILE_ATTACHMENT;
} else if (id == R.id.action_pdf_layers) {
buttonId = BUTTON_VIEW_LAYERS;
} else if (id == R.id.action_digital_signatures) {
buttonId = BUTTON_DIGITAL_SIGNATURE;
} else if (id == R.id.action_close_tab) {
buttonId = BUTTON_CLOSE;
}

return buttonId;
}

@Nullable
private ToolbarButtonType convStringToToolbarType(String item) {
ToolbarButtonType buttonType = null;
Expand Down Expand Up @@ -2161,6 +2206,16 @@ public boolean onToolbarOptionsItemSelected(MenuItem item) {
params.putString(TOOLBAR_ITEM_KEY_ID, itemKey);
onReceiveNativeEvent(params);
return true;
} else {
itemKey = convItemIdToString(itemId);
// check if the item's behavior should be overridden
if (itemKey != null && mToolbarOverrideButtons != null && mToolbarOverrideButtons.contains(itemKey)) {
WritableMap params = Arguments.createMap();
params.putString(ON_TOOLBAR_BUTTON_PRESS, ON_TOOLBAR_BUTTON_PRESS);
params.putString(TOOLBAR_ITEM_KEY_ID, itemKey);
onReceiveNativeEvent(params);
return true;
}
}

return super.onToolbarOptionsItemSelected(item);
Expand Down
2 changes: 2 additions & 0 deletions ios/DocumentViewController/RNTPTDocumentViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)rnt_documentViewControllerDidRotatePages:(PTDocumentBaseViewController *)documentViewController forPageNumbers:(NSIndexSet *)pageNumbers;

- (void)rnt_documentViewControllerToolbarButtonPressed:(PTDocumentBaseViewController *)documentViewController buttonString:(NSString *)buttonString;

@end

@class RNTPTDocumentViewController;
Expand Down
4 changes: 4 additions & 0 deletions ios/RNTPTDocumentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ static NSString * const PTSignaturesManager_signatureDirectory = @"PTSignaturesM

- (void)annotationToolbarItemPressed:(RNTPTDocumentView *)sender withKey:(NSString *)itemKey;

- (void)toolbarButtonPressed:(RNTPTDocumentView *)sender withKey:(NSString *)itemKey;

@end

@interface RNTPTDocumentView : UIView
Expand Down Expand Up @@ -528,6 +530,8 @@ static NSString * const PTSignaturesManager_signatureDirectory = @"PTSignaturesM

@property (nonatomic, copy, nullable) NSString *defaultEraserType;

@property (nonatomic, copy, nullable) NSArray<NSString *> *overrideToolbarButtonBehavior;

#pragma mark - Methods

- (void)setToolMode:(NSString *)toolMode;
Expand Down
40 changes: 40 additions & 0 deletions ios/RNTPTDocumentView.m
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,38 @@ - (void)applyDocumentControllerSettings:(PTDocumentController *)documentControll
}
documentController.toolbarItems = [bottomToolbarItems copy];
}

// Override action of overridden toolbar button items
if (self.overrideToolbarButtonBehavior) {
for (NSString *buttonString in self.overrideToolbarButtonBehavior) {
UIBarButtonItem *toolbarItem = [self itemForButton:buttonString
inViewController:documentController];

NSString *actionName = [NSString stringWithFormat:@"overriddenPressed_%@",
buttonString];
const SEL selector = NSSelectorFromString(actionName);

RNTPT_addMethod([documentController class], selector, ^(id documentController) {
if ([documentController isKindOfClass:[RNTPTDocumentController class]]) {
RNTPTDocumentController *controller = documentController;

if ([controller.delegate respondsToSelector:@selector(rnt_documentViewControllerToolbarButtonPressed:buttonString:)]) {
[controller.delegate rnt_documentViewControllerToolbarButtonPressed:controller
buttonString:buttonString];
}
} else if ([documentController isKindOfClass:[RNTPTCollaborationDocumentController class]]) {
RNTPTCollaborationDocumentController *controller = documentController;

if ([controller.delegate respondsToSelector:@selector(rnt_documentViewControllerToolbarButtonPressed:buttonString:)]) {
[controller.delegate rnt_documentViewControllerToolbarButtonPressed:controller
buttonString:buttonString];
}
}
});

toolbarItem.action = selector;
}
}
}

- (PTToolGroup *)toolGroupForKey:(PTDefaultAnnotationToolbarKey)key toolGroupManager:(PTToolGroupManager *)toolGroupManager
Expand Down Expand Up @@ -3507,6 +3539,14 @@ - (void)rnt_documentViewControllerSavedSignaturesChanged:(PTDocumentBaseViewCont
}
}

- (void)rnt_documentViewControllerToolbarButtonPressed:(PTDocumentBaseViewController *)documentViewController
buttonString:(NSString *)buttonString
{
if ([self.delegate respondsToSelector:@selector(toolbarButtonPressed:withKey:)]) {
[self.delegate toolbarButtonPressed:self withKey:buttonString];
}
}

- (NSDictionary<NSString *, id> *)getAnnotationData:(PTAnnot *)annot pageNumber:(int)pageNumber pdfViewCtrl:(PTPDFViewCtrl *)pdfViewCtrl {
if (![annot IsValid]) {
return nil;
Expand Down
17 changes: 17 additions & 0 deletions ios/RNTPTDocumentViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,13 @@ - (instancetype)init
}
}

RCT_CUSTOM_VIEW_PROPERTY(overrideToolbarButtonBehavior, NSArray, RNTPTDocumentView)
{
if (json) {
view.overrideToolbarButtonBehavior = [RCTConvert NSArray:json];
}
}


- (UIView *)view
{
Expand Down Expand Up @@ -960,6 +967,16 @@ - (void)annotationToolbarItemPressed:(RNTPTDocumentView *)sender withKey:(NSStri
}
}

- (void)toolbarButtonPressed:(RNTPTDocumentView *)sender withKey:(NSString *)itemKey
{
if (sender.onChange) {
sender.onChange(@{
@"onToolbarButtonPress": @"onToolbarButtonPress",
PTAnnotationToolbarItemKeyId: (itemKey ?: @"")
});
}
}

#pragma mark - Methods

- (void)setToolModeForDocumentViewTag:(NSNumber *)tag toolMode:(NSString *)toolMode
Expand Down
9 changes: 9 additions & 0 deletions lib/src/DocumentView/DocumentView.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ const propTypes = {
rememberLastUsedTool: PropTypes.bool,
overflowMenuButtonIcon: PropTypes.string,
maxSignatureCount: PropTypes.number,
overrideToolbarButtonBehavior: arrayOf(Config.Buttons),
onToolbarButtonPress: func(),
...ViewPropTypes,
};
/**
Expand Down Expand Up @@ -416,6 +418,13 @@ export class DocumentView extends PureComponent {
});
}
}
else if (event.nativeEvent.onToolbarButtonPress) {
if (this.props.onToolbarButtonPress) {
this.props.onToolbarButtonPress({
'id': event.nativeEvent.id,
});
}
}
};
// Methods
getDocumentPath = () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-pdftron",
"title": "React Native Pdftron",
"version": "3.0.2-beta.111",
"version": "3.0.2-beta.112",
"description": "React Native Pdftron",
"main": "./lib/index.js",
"typings": "index.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/DocumentView/DocumentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ const propTypes = {
rememberLastUsedTool: PropTypes.bool,
overflowMenuButtonIcon: PropTypes.string,
maxSignatureCount: PropTypes.number,
overrideToolbarButtonBehavior: arrayOf<Config.Buttons>(Config.Buttons),
onToolbarButtonPress: func<(event: {id: string}) => void>(),
...ViewPropTypes,
};

Expand Down Expand Up @@ -412,6 +414,12 @@ export class DocumentView extends PureComponent<DocumentViewProps, any> {
'currentTab': event.nativeEvent.currentTab
});
}
} else if (event.nativeEvent.onToolbarButtonPress) {
if (this.props.onToolbarButtonPress) {
this.props.onToolbarButtonPress({
'id': event.nativeEvent.id,
});
}
}
}

Expand Down

0 comments on commit 68fd438

Please sign in to comment.