Skip to content

Commit

Permalink
Simplify useImageSizes
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jun 11, 2020
1 parent a63375c commit 645290d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 73 deletions.
41 changes: 29 additions & 12 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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 @@ -397,12 +397,8 @@ export function ImageEdit( {
/>
);

const {
imageWidthWithinContainer,
imageHeightWithinContainer,
imageWidth,
imageHeight,
} = useImageSize( ref, url, [ align ] );
const [ { naturalWidth, naturalHeight }, setNaturalSize ] = useState( {} );
const clientWidth = useClientWidth( ref, [ align, url ] );

if ( ! url ) {
return (
Expand Down Expand Up @@ -452,8 +448,8 @@ export function ImageEdit( {
height={ height }
imageSizeOptions={ imageSizeOptions }
isResizable={ isResizable }
imageWidth={ imageWidth }
imageHeight={ imageHeight }
imageWidth={ naturalWidth }
imageHeight={ naturalHeight }
/>
</PanelBody>
</InspectorControls>
Expand Down Expand Up @@ -504,23 +500,44 @@ export function ImageEdit( {
src={ url }
alt={ defaultedAlt }
onClick={ onImageClick }
onLoad={ ( event ) => {
setNaturalSize(
pick( event.target, [
'naturalWidth',
'naturalHeight',
] )
);
} }
onError={ () => onImageError() }
/>
{ 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
52 changes: 0 additions & 52 deletions packages/block-library/src/image/image-size.js

This file was deleted.

33 changes: 33 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,33 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

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

function calculateClientWidth() {
if ( ! ref.current ) {
return;
}

setClientWidth( ref.current.clientWidth );
}

useEffect( calculateClientWidth, dependencies );
useEffect( () => {
if ( ! ref.current ) {
return;
}

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 645290d

Please sign in to comment.