Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zoom and slide on native for PDFView #17647

Merged
merged 32 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2515154
Initial work
alexxxwork Apr 18, 2023
bf20612
Remove redundant code
alexxxwork Apr 19, 2023
43dc6d6
Merge branch 'main' into issue-15988
alexxxwork Apr 19, 2023
4db1446
Fix comment in FlatList
alexxxwork Apr 19, 2023
879868f
Update src/components/AttachmentCarousel/index.js
alexxxwork Apr 20, 2023
e64e030
Fix toggleZoomed func name
alexxxwork Apr 20, 2023
8124997
Fix comment for updateZoomState
alexxxwork Apr 20, 2023
0f17713
Remove redundant View
alexxxwork Apr 20, 2023
925514f
Merge branch 'main' into issue-15988
alexxxwork Apr 20, 2023
e990f9a
Fix tap to show arrows
alexxxwork Apr 20, 2023
6dce5dd
Minor fixes and lint
alexxxwork Apr 20, 2023
17e1339
Merge branch 'main' into issue-15988
alexxxwork Apr 21, 2023
cbdb329
Fix reset zoom state on page update
alexxxwork Apr 21, 2023
e82e1de
Hide arrows when in a zoomed state
alexxxwork Apr 21, 2023
5a2752b
Add comments for debounce func
alexxxwork Apr 21, 2023
2fe3955
Minor style fix
alexxxwork Apr 21, 2023
c9013d0
Fix for mWeb onScrollBeginDrag to onScroll
alexxxwork Apr 21, 2023
439c667
Refactor solution to use Pressable in AttachmentView
alexxxwork Apr 21, 2023
31c1d7f
Fix lin issues, remove debounce
alexxxwork Apr 21, 2023
1f8e2d0
Fix lint issues, remove 'this' in AttachmentView
alexxxwork Apr 21, 2023
11eaff0
Refactor toggleArrowsVisibility
alexxxwork Apr 21, 2023
de07469
Fix lint issues, add missing import
alexxxwork Apr 21, 2023
a341d3e
Fix lint issue
alexxxwork Apr 21, 2023
375655f
Update src/components/AttachmentView.js
alexxxwork Apr 21, 2023
a39457f
Replace View with Pressable in AttachmentView
alexxxwork Apr 21, 2023
b49ce7c
Update src/components/AttachmentView.js
alexxxwork Apr 21, 2023
a3e6026
Fix a bug in upload pdf preview
alexxxwork Apr 21, 2023
68cb6b6
Remove Pressable from AttachmentView
alexxxwork Apr 24, 2023
fe44cee
Fix remove redundant onPress in AttachmentView
alexxxwork Apr 24, 2023
5536157
Update src/components/AttachmentView.js
alexxxwork Apr 24, 2023
83cc726
Update src/components/AttachmentView.js
alexxxwork Apr 24, 2023
f482778
Fix web pointer regression, set onPress: undefined
alexxxwork Apr 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/components/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {View, FlatList, Pressable} from 'react-native';
import {View, FlatList} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
Expand Down Expand Up @@ -56,12 +56,14 @@ class AttachmentCarousel extends React.Component {
this.renderItem = this.renderItem.bind(this);
this.renderCell = this.renderCell.bind(this);
this.updatePage = this.updatePage.bind(this);
this.toggleZoomed = this.toggleZoomed.bind(this);

this.state = {
attachments: [],
source: this.props.source,
shouldShowArrow: this.canUseTouchScreen,
containerWidth: 0,
isZoomed: false,
};
}

Expand Down Expand Up @@ -116,6 +118,17 @@ class AttachmentCarousel extends React.Component {
this.setState({shouldShowArrow});
}

/**
* Toggles zoomed state to enable panning the PDF
* @param {Number} scale current PDF scale
*/
toggleZoomed(scale) {
if ((scale <= 1 && !this.state.isZoomed) || (scale > 1 && this.state.isZoomed)) {
return;
}
this.setState(current => ({isZoomed: !current.isZoomed}));
}
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved

/**
* Map report actions to attachment items
*/
Expand Down Expand Up @@ -198,21 +211,8 @@ class AttachmentCarousel extends React.Component {
renderCell(props) {
const style = [props.style, styles.h100, {width: this.state.containerWidth}];

// Touch screen devices can toggle between showing and hiding the arrows by tapping on the image/container
// Other devices toggle the arrows through hovering (mouse) instead (see render() root element)
if (!this.canUseTouchScreen) {
// eslint-disable-next-line react/jsx-props-no-spreading
return <View {...props} style={style} />;
}

return (
<Pressable
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onPress={() => this.setState(current => ({shouldShowArrow: !current.shouldShowArrow}))}
style={style}
/>
);
// eslint-disable-next-line react/jsx-props-no-spreading
return <View {...props} style={style} />;
}

/**
Expand All @@ -222,7 +222,18 @@ class AttachmentCarousel extends React.Component {
*/
renderItem({item}) {
const authSource = addEncryptedAuthTokenToURL(item.source);
return <AttachmentView source={authSource} file={item.file} />;
if (!this.canUseTouchScreen) {
return <AttachmentView source={authSource} file={item.file} />;
}

return (
<AttachmentView
source={authSource}
file={item.file}
onScaleChanged={this.toggleZoomed}
onPress={() => this.setState(current => ({shouldShowArrow: !current.shouldShowArrow}))}
/>
);
}

render() {
Expand Down Expand Up @@ -292,7 +303,8 @@ class AttachmentCarousel extends React.Component {

// Enable scrolling by swiping on mobile (touch) devices only
// disable scroll for desktop/browsers because they add their scrollbars
scrollEnabled={this.canUseTouchScreen}
// disable scroll in PDF zoomed state
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved
scrollEnabled={this.canUseTouchScreen && !this.state.isZoomed}
ref={this.scrollRef}
initialScrollIndex={this.state.page}
initialNumToRender={3}
Expand Down
5 changes: 5 additions & 0 deletions src/components/AttachmentView.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const propTypes = {
/** Function for handle on press */
onPress: PropTypes.func,

/** Handles scale changed event in PDF component */
onScaleChanged: PropTypes.func,

/** Notify parent that the UI should be modified to accommodate keyboard */
onToggleKeyboard: PropTypes.func,

Expand All @@ -51,6 +54,7 @@ const defaultProps = {
shouldShowDownloadIcon: false,
shouldShowLoadingSpinnerIcon: false,
onPress: () => {},
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved
onScaleChanged: () => {},
onToggleKeyboard: () => {},
};

Expand All @@ -75,6 +79,7 @@ const AttachmentView = (props) => {
sourceURL={sourceURL}
style={styles.imageModalPDF}
onToggleKeyboard={props.onToggleKeyboard}
onScaleChanged={props.onScaleChanged}
/>
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/PDFView/index.native.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {Component} from 'react';
import {TouchableWithoutFeedback, View} from 'react-native';
import {View} from 'react-native';
import PDF from 'react-native-pdf';
import KeyboardAvoidingView from '../KeyboardAvoidingView';
import styles from '../../styles/styles';
Expand Down Expand Up @@ -152,7 +152,7 @@ class PDFView extends Component {
</View>
)}
{this.state.shouldAttemptPDFLoad && (
<TouchableWithoutFeedback style={touchableStyles}>
<View style={touchableStyles}>
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved
<PDF
trustAllCerts={false}
renderActivityIndicator={() => <FullScreenLoadingIndicator />}
Expand All @@ -162,8 +162,9 @@ class PDFView extends Component {
password={this.state.password}
onLoadComplete={this.finishPDFLoad}
onPageSingleTap={this.props.onPress}
onScaleChanged={this.props.onScaleChanged}
/>
</TouchableWithoutFeedback>
</View>
)}
{this.state.shouldRequestPassword && (
<KeyboardAvoidingView style={styles.flex1}>
Expand Down
4 changes: 4 additions & 0 deletions src/components/PDFView/pdfViewPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const propTypes = {
/** Handles press events like toggling attachment arrows natively */
onPress: PropTypes.func,

/** Handles scale changed event in PDF component */
onScaleChanged: PropTypes.func,

...windowDimensionsPropTypes,
};

Expand All @@ -23,6 +26,7 @@ const defaultProps = {
style: {},
onPress: () => {},
onToggleKeyboard: () => {},
onScaleChanged: () => {},
};

export {propTypes, defaultProps};