Skip to content

Commit

Permalink
RNMobile - ImageWithFocalPoint - Move container logic to component an…
Browse files Browse the repository at this point in the history
…d prettier formatting
  • Loading branch information
Gerardo Pacheco committed Feb 3, 2020
1 parent 5305373 commit e71ee48
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
19 changes: 2 additions & 17 deletions packages/block-library/src/cover/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@wordpress/block-editor';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { useEffect } from '@wordpress/element';
import { cover as icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -69,7 +69,6 @@ const Cover = ( {
minHeight,
url,
} = attributes;
const [ containerSize, setContainerSize ] = useState( null );
const CONTAINER_HEIGHT = minHeight || COVER_DEFAULT_HEIGHT;

// Used to set a default color for its InnerBlocks
Expand All @@ -96,11 +95,6 @@ const Cover = ( {
setAttributes( { dimRatio: value } );
};

const onContainerLayout = ( event ) => {
const { height, width } = event.nativeEvent.layout;
setContainerSize( { width, height } );
};

const getOpacity = () => {
// Set opacity to 1 while video support is not available
if ( VIDEO_BACKGROUND_TYPE === backgroundType ) {
Expand Down Expand Up @@ -181,10 +175,7 @@ const Cover = ( {
return (
<View style={ containerStyles }>
{ controls }
<View
onLayout={ onContainerLayout }
style={ [ styles.backgroundContainer ] }
>
<View style={ [ styles.backgroundContainer ] }>
<View
style={ [
styles.content,
Expand All @@ -198,12 +189,6 @@ const Cover = ( {

{ IMAGE_BACKGROUND_TYPE === backgroundType && (
<ImageWithFocalPoint
containerSize={ containerSize }
contentHeight={
containerSize
? containerSize.height
: CONTAINER_HEIGHT
}
focalPoint={ focalPoint }
url={ url }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { Image } from 'react-native';
import { Image, View } from 'react-native';

/**
* WordPress dependencies
Expand All @@ -13,23 +13,33 @@ import { useState, useEffect, memo } from '@wordpress/element';
*/
import styles from './style.scss';

const ImageWithFocalPoint = ( {
containerSize,
contentHeight,
focalPoint,
url,
} ) => {
const ImageWithFocalPoint = ( { focalPoint, url } ) => {
const [ originalImageData, setOriginalImageData ] = useState( null );
const [ containerSize, setContainerSize ] = useState( null );

useEffect( () => {
if ( url ) {
Image.getSize( url, ( width, height ) => {
setOriginalImageData( { width, height, aspectRatio: width / height } );
setOriginalImageData( {
width,
height,
aspectRatio: width / height,
} );
} );
}
}, [] );

const getFocalPointOffset = ( imageRatio, container, imageSize, focusPoint ) => {
const onContainerLayout = ( event ) => {
const { height, width } = event.nativeEvent.layout;
setContainerSize( { width, height } );
};

const getFocalPointOffset = (
imageRatio,
container,
imageSize,
focusPoint
) => {
const containerCenter = Math.floor( container / 2 );
const scaledImage = Math.floor( imageSize / imageRatio );
const focus = Math.floor( focusPoint * scaledImage );
Expand Down Expand Up @@ -59,11 +69,21 @@ const ImageWithFocalPoint = ( {
imageStyle.resizeMode = 'stretch';

if ( widthRatio > heightRatio ) {
horizontalOffset = getFocalPointOffset( heightRatio, containerSize.width, originalImageData.width, focalPoint.x );
horizontalOffset = getFocalPointOffset(
heightRatio,
containerSize.width,
originalImageData.width,
focalPoint.x
);
imageStyle.width = undefined;
imageStyle.left = horizontalOffset;
} else if ( widthRatio < heightRatio ) {
verticalOffset = getFocalPointOffset( widthRatio, containerSize.height, originalImageData.height, focalPoint.y );
verticalOffset = getFocalPointOffset(
widthRatio,
containerSize.height,
originalImageData.height,
focalPoint.y
);
imageStyle.height = undefined;
imageStyle.top = verticalOffset;
}
Expand All @@ -74,11 +94,27 @@ const ImageWithFocalPoint = ( {
return imageStyle;
};

return <Image
aspectRatio={ originalImageData ? originalImageData.aspectRatio : undefined }
style={ [ styles.image, { height: contentHeight }, getImageStyles() ] }
source={ { uri: url } }
/>;
return (
<View style={ styles.container } onLayout={ onContainerLayout }>
<Image
aspectRatio={
originalImageData
? originalImageData.aspectRatio
: undefined
}
style={ [
styles.image,
{
height: containerSize
? containerSize.height
: undefined,
},
getImageStyles(),
] }
source={ { uri: url } }
/>
</View>
);
};

export default memo( ImageWithFocalPoint );
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.container {
height: 100%;
position: absolute;
width: 100%;
}

.image {
position: absolute;
width: 100%;
Expand Down

0 comments on commit e71ee48

Please sign in to comment.