-
Notifications
You must be signed in to change notification settings - Fork 4.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
[Mobile] Image height #13096
Merged
Merged
[Mobile] Image height #13096
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8890a4a
[rnmobile]: Convert image-block into class
etoledom 4f8cbab
[rnmobile]: Creating mobile version of `image-size`.
etoledom 8d792a1
[rnmobile]: Adding util to image-block to share math between image-si…
etoledom b8afe45
[rnmobile]: Adding newline at the end of image-size.native.js file
etoledom 82e74bf
[rnmobile]: Replace wrong tabs characters
etoledom 5b696e6
[rnmobile]: Clear image size when url changes.
etoledom 7654521
[rnmobile]: Fixed lint issues
etoledom 5664ae7
[rnmobile]: Removing unnecessary binding calls
etoledom b72c99b
[rnmobile]: Added necessary this.onLayout.bind( this )
etoledom a90eb0d
[rnmobile]: Adding missing bind to `onMediaLibraryPress`.
etoledom 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
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
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,87 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { View, Image } from 'react-native'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { calculatePreferedImageSize } from './utils'; | ||
|
||
class ImageSize extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
width: undefined, | ||
height: undefined, | ||
}; | ||
this.onLayout = this.onLayout.bind( this ); | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( this.props.src !== prevProps.src ) { | ||
this.image = {}; | ||
|
||
this.setState( { | ||
width: undefined, | ||
height: undefined, | ||
} ); | ||
this.fetchImageSize(); | ||
} | ||
|
||
if ( this.props.dirtynessTrigger !== prevProps.dirtynessTrigger ) { | ||
this.calculateSize(); | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchImageSize(); | ||
} | ||
|
||
fetchImageSize() { | ||
Image.getSize( this.props.src, ( width, height ) => { | ||
this.image = { width, height }; | ||
this.calculateSize(); | ||
} ); | ||
} | ||
|
||
calculateSize() { | ||
if ( this.image === undefined || this.container === undefined ) { | ||
return; | ||
} | ||
const { width, height } = calculatePreferedImageSize( this.image, this.container ); | ||
this.setState( { width, height } ); | ||
} | ||
|
||
onLayout( event ) { | ||
const { width, height } = event.nativeEvent.layout; | ||
this.container = { | ||
clientWidth: width, | ||
clientHeight: height, | ||
}; | ||
this.calculateSize(); | ||
} | ||
|
||
render() { | ||
const sizes = { | ||
imageWidth: this.image && this.image.width, | ||
imageHeight: this.image && this.image.height, | ||
containerWidth: this.container && this.container.width, | ||
containerHeight: this.container && this.container.height, | ||
imageWidthWithinContainer: this.state.width, | ||
imageHeightWithinContainer: this.state.height, | ||
}; | ||
return ( | ||
<View onLayout={ this.onLayout }> | ||
{ this.props.children( sizes ) } | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
export default ImageSize; |
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,9 @@ | ||
|
||
export function calculatePreferedImageSize( image, container ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to add some JSDocs :) |
||
const maxWidth = container.clientWidth; | ||
const exceedMaxWidth = image.width > maxWidth; | ||
const ratio = image.height / image.width; | ||
const width = exceedMaxWidth ? maxWidth : image.width; | ||
const height = exceedMaxWidth ? maxWidth * ratio : image.height; | ||
return { width, height }; | ||
} |
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.
Hey @Tug ! I had to add this bind back since it was giving an error accessing
this.props.setAttributes
.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.
Yep, usually
onSomething
methods need to be bound tothis
👍