-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add Modal Previews when Uploading Attachments + Copy/Paste files #685
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
7ce0794
Allow pasting files on web and desktop
marcaaron f3e7b2f
fix blunder
marcaaron e07c484
fix style
marcaaron e72c63e
fix conflicts
marcaaron 1b076e5
Remove BaseImageModal
marcaaron e63583c
Fix ModalHeader
marcaaron 67cfc73
Fix style - refactor a bit
marcaaron fc65c4b
fix comment
marcaaron 03fe193
fix style
marcaaron a3bf456
add defaulPropt
marcaaron bab7699
fix up iOS and styles
marcaaron 91f7fa9
fix conflicts
marcaaron 1ccb66f
fix gradle changes
marcaaron b713a76
add basic styles
marcaaron 5daeaa1
fix conflicts add authToken logic back. still needs to be refactored …
marcaaron 3b68595
Bump expensify-common version fix isImage issue
marcaaron 70c5708
not sure where this is going yet
marcaaron 091abdc
Refactor image components
marcaaron 8205ee5
fix styles
marcaaron 4466b15
fix conflicts
marcaaron 729f142
fix conflict
marcaaron bc63591
fix up styles
marcaaron a4c71da
fix style
marcaaron 913b0a6
add modalHeaderHeight
marcaaron 0aecd9a
use constant for modal header height
marcaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import React, {Component} from 'react'; | ||
import Modal from 'react-native-modal'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
View, Dimensions, TouchableOpacity, Text, | ||
} from 'react-native'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import AttachmentView from '../AttachmentView'; | ||
import styles, {colors} from '../../styles/StyleSheet'; | ||
import ModalView from '../ModalView'; | ||
import ModalHeader from '../ModalHeader'; | ||
import ONYXKEYS from '../../ONYXKEYS'; | ||
import addAuthTokenToURL from '../../libs/addAuthTokenToURL'; | ||
|
||
/** | ||
* Modal render prop component that exposes modal launching triggers that can be used | ||
* to display a full size image or PDF modally with optional confirmation button. | ||
*/ | ||
|
||
const propTypes = { | ||
// Should modal go full screen | ||
pinToEdges: PropTypes.bool, | ||
|
||
// Title of the modal header | ||
title: PropTypes.string, | ||
|
||
// Optional source URL for the image shown inside the . | ||
// If not passed in via props must be specified when modal is opened. | ||
sourceURL: PropTypes.string, | ||
|
||
// Optional callback to fire when we want to preview an image and approve it for use. | ||
onConfirm: PropTypes.func, | ||
|
||
// A function as a child to pass modal launching methods to | ||
children: PropTypes.func.isRequired, | ||
|
||
// Do the urls require an authToken? | ||
isAuthTokenRequired: PropTypes.bool.isRequired, | ||
|
||
// Current user session | ||
session: PropTypes.shape({ | ||
authToken: PropTypes.string.isRequired, | ||
}).isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
pinToEdges: false, | ||
title: '', | ||
sourceURL: null, | ||
onConfirm: null, | ||
}; | ||
|
||
class AttachmentModalBase extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.updateImageDimensions = this.updateImageDimensions.bind(this); | ||
|
||
// If pinToEdges is false, the default modal width and height will take up about 80% of the screen | ||
this.modalWidth = Dimensions.get('window').width * 0.8; | ||
this.modalHeight = Dimensions.get('window').height * 0.8; | ||
|
||
// The image inside the modal shouldn't span the entire width of the modal | ||
// unless it is full screen so the default is 20% smaller than the width of the modal | ||
this.modalImageWidth = Dimensions.get('window').width * 0.6; | ||
|
||
this.state = { | ||
isModalOpen: false, | ||
imageWidth: 300, | ||
imageHeight: 300, | ||
file: null, | ||
sourceURL: props.sourceURL, | ||
}; | ||
} | ||
|
||
/** | ||
* Update image dimensions once the size is fetched | ||
*/ | ||
updateImageDimensions({width, height}) { | ||
// Unlike the image width, we do allow the image to span the full modal height | ||
const modalHeight = this.props.pinToEdges | ||
? Dimensions.get('window').height | ||
: this.modalHeight - (styles.modalHeaderBar.height || 0); | ||
const modalWidth = this.props.pinToEdges ? Dimensions.get('window').width : this.modalImageWidth; | ||
let imageHeight = height; | ||
let imageWidth = width; | ||
|
||
// Resize image to fit within the modal, if necessary | ||
if (width > modalWidth || height > modalHeight) { | ||
const scaleFactor = Math.max(width / modalWidth, height / modalHeight); | ||
imageHeight = height / scaleFactor; | ||
imageWidth = width / scaleFactor; | ||
} | ||
|
||
this.setState({imageWidth, imageHeight}); | ||
} | ||
|
||
render() { | ||
const sourceURL = addAuthTokenToURL({ | ||
url: this.state.sourceURL, | ||
authToken: this.props.session.authToken, | ||
required: this.props.isAuthTokenRequired, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Modal | ||
onRequestClose={() => this.setState({isModalOpen: false})} | ||
visible={this.state.isModalOpen} | ||
transparent | ||
style={styles.m0} | ||
> | ||
<ModalView | ||
pinToEdges={this.props.pinToEdges} | ||
modalWidth={this.modalWidth} | ||
modalHeight={this.modalHeight} | ||
onCloseButtonPress={() => this.setState({isModalOpen: false})} | ||
> | ||
<ModalHeader | ||
title={this.props.title} | ||
onCloseButtonPress={() => this.setState({isModalOpen: false})} | ||
/> | ||
<View style={styles.imageModalImageCenterContainer}> | ||
{this.state.sourceURL && ( | ||
<AttachmentView | ||
sourceURL={sourceURL} | ||
height={this.state.imageHeight} | ||
width={this.state.imageWidth} | ||
onImagePrefetched={this.updateImageDimensions} | ||
file={this.state.file} | ||
/> | ||
)} | ||
</View> | ||
{/* If we have an onConfirm method show a confirmation button */} | ||
{this.props.onConfirm && ( | ||
<TouchableOpacity | ||
style={[styles.button, styles.buttonSuccess, styles.buttonConfirm]} | ||
underlayColor={colors.componentBG} | ||
onPress={() => { | ||
this.props.onConfirm(this.state.file); | ||
this.setState({isModalOpen: false}); | ||
}} | ||
> | ||
<Text | ||
style={[ | ||
styles.buttonText, | ||
styles.buttonSuccessText, | ||
styles.buttonConfirmText, | ||
]} | ||
> | ||
Upload | ||
</Text> | ||
</TouchableOpacity> | ||
)} | ||
</ModalView> | ||
</Modal> | ||
{this.props.children({ | ||
displayFileInModal: ({file}) => { | ||
if (file instanceof File) { | ||
const source = URL.createObjectURL(file); | ||
this.setState({isModalOpen: true, sourceURL: source, file}); | ||
} else { | ||
this.setState({isModalOpen: true, sourceURL: file.uri, file}); | ||
} | ||
}, | ||
show: () => { | ||
this.setState({isModalOpen: true}); | ||
}, | ||
})} | ||
</> | ||
); | ||
} | ||
} | ||
|
||
AttachmentModalBase.propTypes = propTypes; | ||
AttachmentModalBase.defaultProps = defaultProps; | ||
export default withOnyx({ | ||
session: { | ||
key: ONYXKEYS.SESSION, | ||
}, | ||
})(AttachmentModalBase); |
15 changes: 15 additions & 0 deletions
15
src/components/AttachmentModal/attachmentModalPropTypes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import PropTypes from 'prop-types'; | ||
|
||
export default { | ||
// Title of Modal | ||
title: PropTypes.string.isRequired, | ||
|
||
// Optional image or PDF source URL | ||
sourceURL: PropTypes.string, | ||
|
||
// Function as a child | ||
children: PropTypes.func.isRequired, | ||
|
||
// Do the urls require an authToken? | ||
isAuthTokenRequired: PropTypes.bool, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import AttachmentModalBase from './AttachmentModalBase'; | ||
import propTypes from './attachmentModalPropTypes'; | ||
|
||
const defaultProps = { | ||
sourceURL: null, | ||
isAuthTokenRequired: false, | ||
}; | ||
|
||
const AttachmentModal = props => ( | ||
<AttachmentModalBase | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
> | ||
{props.children} | ||
</AttachmentModalBase> | ||
); | ||
|
||
AttachmentModal.propTypes = propTypes; | ||
AttachmentModal.defaultProps = defaultProps; | ||
AttachmentModal.displayName = 'AttachmentModal'; | ||
|
||
export default AttachmentModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import AttachmentModalBase from './AttachmentModalBase'; | ||
import propTypes from './attachmentModalPropTypes'; | ||
|
||
const defaultProps = { | ||
sourceURL: null, | ||
}; | ||
|
||
const AttachmentModal = props => ( | ||
<AttachmentModalBase | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
pinToEdges | ||
> | ||
{props.children} | ||
</AttachmentModalBase> | ||
); | ||
|
||
AttachmentModal.propTypes = propTypes; | ||
AttachmentModal.defaultProps = defaultProps; | ||
AttachmentModal.displayName = 'AttachmentModal'; | ||
|
||
export default AttachmentModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, do you know why this was wrapped with a React.Fragment before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think was enabling us to use the short circuit syntax as a curly brace after the
=>
would have opened a new block and required an explicit return.