Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image edit: avoid re-render on select #23009

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/block-library/src/rich-image/rich-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
BlockControls,
__experimentalBlock as Block,
} from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import {
rotateLeft as rotateLeftIcon,
rotateRight as rotateRightIcon,
Expand Down Expand Up @@ -162,7 +162,7 @@ function RichImage( props ) {
setAttributes,
noticeOperations,
} = props;
const [ isCrop, setIsCrop ] = useState( false );
const [ isCropping, setIsCropping ] = useState( false );
const [ inProgress, setIsProgress ] = useState( null );
const [ imageSize, setImageSize ] = useState( {
naturalHeight: 0,
Expand All @@ -172,11 +172,14 @@ function RichImage( props ) {
const [ position, setPosition ] = useState( { x: 0, y: 0 } );
const [ zoom, setZoom ] = useState( 1 );
const [ aspect, setAspect ] = useState( 4 / 3 );
const isEditing = ! isCrop && isSelected && url;
const isEditing = ! isCropping && isSelected && url;

if ( ! isSelected ) {
return <OriginalBlock { ...props } />;
}
// Cancel cropping on deselect.
useEffect( () => {
if ( ! isSelected ) {
setIsCropping( false );
}
}, [ isSelected ] );

function adjustImage( action, attrs ) {
setIsProgress( action );
Expand All @@ -185,7 +188,7 @@ function RichImage( props ) {
richImageRequest( id, action, attrs )
.then( ( response ) => {
setIsProgress( null );
setIsCrop( false );
setIsCropping( false );

if ( response.mediaID && response.mediaID !== id ) {
setAttributes( {
Expand All @@ -201,7 +204,7 @@ function RichImage( props ) {
)
);
setIsProgress( null );
setIsCrop( false );
setIsCropping( false );
} );
}

Expand All @@ -228,7 +231,7 @@ function RichImage( props ) {
<Spinner />
</div>
) }
{ isCrop ? (
{ isCropping ? (
<Block.div className="richimage__crop-controls">
<div
className="richimage__crop-area"
Expand Down Expand Up @@ -339,14 +342,14 @@ function RichImage( props ) {
icon={ cropIcon }
label={ __( 'Crop' ) }
onClick={ () => {
setIsCrop( ! isCrop );
setIsCropping( ( prev ) => ! prev );
setCrop( DEFAULT_CROP );
} }
/>
</ToolbarGroup>
</BlockControls>
) }
{ isCrop && (
{ isCropping && (
<BlockControls>
<ToolbarGroup>
<div className="richimage__crop-icon">
Expand All @@ -370,7 +373,7 @@ function RichImage( props ) {
</ToolbarButton>
<ToolbarButton
onClick={ () => {
setIsCrop( false );
setIsCropping( false );
} }
>
{ __( 'Cancel' ) }
Expand Down