Skip to content

Commit

Permalink
Image editing: fix image size on crop (#23173)
Browse files Browse the repository at this point in the history
* Image editing: fix image size on crop

* Fix dimensions for unresized images
  • Loading branch information
ellatrix authored Jun 18, 2020
1 parent 69acbcf commit 8e185c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/block-library/src/image/image-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,16 @@ export default function ImageEditor( {
url,
setAttributes,
isSelected,
naturalWidth,
naturalHeight,
width,
height,
clientWidth,
children,
} ) {
const { createErrorNotice } = useDispatch( 'core/notices' );
const [ isCropping, setIsCropping ] = useState( false );
const [ inProgress, setIsProgress ] = useState( null );
const [ imageSize, setImageSize ] = useState( {
naturalHeight: 0,
naturalWidth: 0,
} );
const [ crop, setCrop ] = useState( null );
const [ position, setPosition ] = useState( { x: 0, y: 0 } );
const [ zoom, setZoom ] = useState( 1 );
Expand Down Expand Up @@ -235,14 +236,15 @@ export default function ImageEditor( {
</div>
) }
{ isCropping ? (
<div className="richimage__crop-controls">
<>
<div
className="richimage__crop-area"
style={ {
paddingBottom: `${
( 100 * imageSize.naturalHeight ) /
imageSize.naturalWidth
}%`,
width,
height:
height ||
( clientWidth * naturalHeight ) /
naturalWidth,
} }
>
<Cropper
Expand All @@ -256,7 +258,6 @@ export default function ImageEditor( {
onCropChange={ setPosition }
onCropComplete={ setCrop }
onZoomChange={ setZoom }
onMediaLoaded={ setImageSize }
/>
</div>
<RangeControl
Expand All @@ -268,7 +269,7 @@ export default function ImageEditor( {
value={ zoom }
onChange={ setZoom }
/>
</div>
</>
) : (
children
) }
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ export default function Image( {
url={ url }
setAttributes={ setAttributes }
isSelected={ isSelected }
naturalWidth={ naturalWidth }
naturalHeight={ naturalHeight }
width={ width }
height={ height }
clientWidth={ clientWidth }
>
{ img }
</ImageEditor>
Expand Down

0 comments on commit 8e185c9

Please sign in to comment.