Skip to content

Commit

Permalink
Simplify useImageSizes (#23091)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Jun 11, 2020
1 parent 0a2ab46 commit e0453df
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 73 deletions.
52 changes: 0 additions & 52 deletions packages/block-library/src/image/image-size.js

This file was deleted.

41 changes: 29 additions & 12 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { createBlock } from '@wordpress/blocks';
* Internal dependencies
*/
import { createUpgradedEmbedBlock } from '../embed/util';
import useImageSize from './image-size';
import useClientWidth from './use-client-width';

/**
* Module constants
Expand Down Expand Up @@ -87,12 +87,8 @@ export default function Image( {
const isLargeViewport = useViewportMatch( 'medium' );
const [ captionFocused, setCaptionFocused ] = useState( false );
const isWideAligned = includes( [ 'wide', 'full' ], align );
const {
imageWidthWithinContainer,
imageHeightWithinContainer,
imageWidth,
imageHeight,
} = useImageSize( containerRef, url, [ align ] );
const [ { naturalWidth, naturalHeight }, setNaturalSize ] = useState( {} );
const clientWidth = useClientWidth( containerRef, [ align ] );
const isResizable = ! isWideAligned && isLargeViewport;
const imageSizeOptions = map(
filter( imageSizes, ( { slug } ) =>
Expand Down Expand Up @@ -213,8 +209,8 @@ export default function Image( {
height={ height }
imageSizeOptions={ imageSizeOptions }
isResizable={ isResizable }
imageWidth={ imageWidth }
imageHeight={ imageHeight }
imageWidth={ naturalWidth }
imageHeight={ naturalHeight }
/>
</PanelBody>
</InspectorControls>
Expand Down Expand Up @@ -266,22 +262,43 @@ export default function Image( {
alt={ defaultedAlt }
onClick={ onImageClick }
onError={ () => onImageError() }
onLoad={ ( event ) => {
setNaturalSize(
pick( event.target, [
'naturalWidth',
'naturalHeight',
] )
);
} }
/>
{ isBlobURL( url ) && <Spinner /> }
</>
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
);

let imageWidthWithinContainer;
let imageHeightWithinContainer;

if ( clientWidth && naturalWidth && naturalHeight ) {
const exceedMaxWidth = naturalWidth > clientWidth;
const ratio = naturalHeight / naturalWidth;
imageWidthWithinContainer = exceedMaxWidth ? clientWidth : naturalWidth;
imageHeightWithinContainer = exceedMaxWidth
? clientWidth * ratio
: naturalHeight;
}

if ( ! isResizable || ! imageWidthWithinContainer ) {
img = <div style={ { width, height } }>{ img }</div>;
} else {
const currentWidth = width || imageWidthWithinContainer;
const currentHeight = height || imageHeightWithinContainer;

const ratio = imageWidth / imageHeight;
const minWidth = imageWidth < imageHeight ? MIN_SIZE : MIN_SIZE * ratio;
const ratio = naturalWidth / naturalHeight;
const minWidth =
naturalWidth < naturalHeight ? MIN_SIZE : MIN_SIZE * ratio;
const minHeight =
imageHeight < imageWidth ? MIN_SIZE : MIN_SIZE / ratio;
naturalHeight < naturalWidth ? MIN_SIZE : MIN_SIZE / ratio;

// With the current implementation of ResizableBox, an image needs an
// explicit pixel value for the max-width. In absence of being able to
Expand Down
25 changes: 25 additions & 0 deletions packages/block-library/src/image/use-client-width.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

export default function useClientWidth( ref, dependencies ) {
const [ clientWidth, setClientWidth ] = useState();

function calculateClientWidth() {
setClientWidth( ref.current.clientWidth );
}

useEffect( calculateClientWidth, dependencies );
useEffect( () => {
const { defaultView } = ref.current.ownerDocument;

defaultView.addEventListener( 'resize', calculateClientWidth );

return () => {
defaultView.removeEventListener( 'resize', calculateClientWidth );
};
}, [] );

return clientWidth;
}
9 changes: 0 additions & 9 deletions packages/block-library/src/image/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import { isEmpty, each } from 'lodash';
*/
import { NEW_TAB_REL } from './constants';

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 };
}

export function removeNewTabRel( currentRel ) {
let newRel = currentRel;

Expand Down

0 comments on commit e0453df

Please sign in to comment.