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 13 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
61 changes: 55 additions & 6 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, PanResponder} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
Expand Down Expand Up @@ -56,13 +56,25 @@ class AttachmentCarousel extends React.Component {
this.renderItem = this.renderItem.bind(this);
this.renderCell = this.renderCell.bind(this);
this.updatePage = this.updatePage.bind(this);
this.updateZoomState = this.updateZoomState.bind(this);
this.toggleArrowsDebounced = _.debounce(this.toggleArrowsDebounced.bind(this), 300);
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved

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

this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (event, gestureState) => {
if (gestureState.numberActiveTouches > 1 || this.state.isZoomed) {
return false;
}
this.toggleArrowsDebounced();
},
});
}

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

/**
* Toggles the visibility of the arrows (debounced)
*/
toggleArrowsDebounced() {
// Don't toggle arrows in a zoomed state
if (this.state.isZoomed) {
return;
}
this.setState(current => ({shouldShowArrow: !current.shouldShowArrow}));
}

/**
* Updates zoomed state to enable/disable panning the PDF
* @param {Number} scale current PDF scale
*/
updateZoomState(scale) {
const isZoomed = scale > 1;
if (isZoomed === this.state.isZoomed) {
return;
}
this.setState({isZoomed});
}

/**
* Map report actions to attachment items
*/
Expand Down Expand Up @@ -184,10 +219,11 @@ class AttachmentCarousel extends React.Component {
return;
}

const isZoomed = false;
const page = entry.index;
const {source, file} = this.getAttachment(entry.item);
this.props.onNavigate({source: addEncryptedAuthTokenToURL(source), file});
this.setState({page, source});
this.setState({page, source, isZoomed});
alexxxwork marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -206,10 +242,11 @@ class AttachmentCarousel extends React.Component {
}

return (
<Pressable
<View
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onPress={() => this.setState(current => ({shouldShowArrow: !current.shouldShowArrow}))}
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.panResponder.panHandlers}
style={style}
/>
);
Expand All @@ -222,7 +259,17 @@ 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.updateZoomState}
/>
);
}

render() {
Expand Down Expand Up @@ -292,7 +339,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}
// Enable scrolling FlatList only when PDF is not in a zoomed state
scrollEnabled={this.canUseTouchScreen && !this.state.isZoomed}
ref={this.scrollRef}
initialScrollIndex={this.state.page}
initialNumToRender={3}
Expand All @@ -305,6 +353,7 @@ class AttachmentCarousel extends React.Component {
keyExtractor={item => item.source}
viewabilityConfig={this.viewabilityConfig}
onViewableItemsChanged={this.updatePage}
onScrollBeginDrag={this.toggleArrowsDebounced.cancel}
/>
)}

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
30 changes: 12 additions & 18 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 @@ -124,11 +124,6 @@ class PDFView extends Component {
styles.imageModalPDF,
StyleUtils.getWidthAndHeightStyle(this.props.windowWidth, this.props.windowHeight),
];
const touchableStyles = [
styles.flex1,
this.props.style,
styles.w100,
];

// If we haven't yet successfully validated the password and loaded the PDF,
// then we need to hide the react-native-pdf/PDF component so that PDFPasswordForm
Expand All @@ -152,18 +147,17 @@ class PDFView extends Component {
</View>
)}
{this.state.shouldAttemptPDFLoad && (
<TouchableWithoutFeedback style={touchableStyles}>
<PDF
trustAllCerts={false}
renderActivityIndicator={() => <FullScreenLoadingIndicator />}
source={{uri: this.props.sourceURL}}
style={pdfStyles}
onError={this.handleFailureToLoadPDF}
password={this.state.password}
onLoadComplete={this.finishPDFLoad}
onPageSingleTap={this.props.onPress}
/>
</TouchableWithoutFeedback>
<PDF
trustAllCerts={false}
renderActivityIndicator={() => <FullScreenLoadingIndicator />}
source={{uri: this.props.sourceURL}}
style={pdfStyles}
onError={this.handleFailureToLoadPDF}
password={this.state.password}
onLoadComplete={this.finishPDFLoad}
onPageSingleTap={this.props.onPress}
onScaleChanged={this.props.onScaleChanged}
/>
)}
{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};