-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [rnmobile]: Convert image-block into class * [rnmobile]: Creating mobile version of `image-size`. * [rnmobile]: Adding util to image-block to share math between image-size components. * [rnmobile]: Adding newline at the end of image-size.native.js file * [rnmobile]: Replace wrong tabs characters * [rnmobile]: Clear image size when url changes. * [rnmobile]: Fixed lint issues * [rnmobile]: Removing unnecessary binding calls * [rnmobile]: Added necessary this.onLayout.bind( this ) * [rnmobile]: Adding missing bind to `onMediaLibraryPress`.
- Loading branch information
1 parent
6adc907
commit 277cb8c
Showing
4 changed files
with
185 additions
and
51 deletions.
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 ) { | ||
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 }; | ||
} |