Skip to content

Commit

Permalink
Refactor image block control rendering logic for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitraj2203 committed May 30, 2024
1 parent f44dfae commit 43fa143
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,43 +499,55 @@ export default function Image( {
[ clientId, isSingleSelected, metadata?.bindings ]
);

const shouldRenderOtherControls =
( isSingleSelected &&
! isEditingImage &&
! lockHrefControls &&
! lockUrlControls ) ||
allowCrop ||
( isSingleSelected && canInsertCover );

const controls = (
<>
<BlockControls group="block">
{ isSingleSelected &&
! isEditingImage &&
! lockHrefControls &&
! lockUrlControls && (
<ImageURLInputUI
url={ href || '' }
onChangeUrl={ onSetHref }
linkDestination={ linkDestination }
mediaUrl={ ( image && image.source_url ) || url }
mediaLink={ image && image.link }
linkTarget={ linkTarget }
linkClass={ linkClass }
rel={ rel }
showLightboxSetting={ showLightboxSetting }
lightboxEnabled={ lightboxChecked }
onSetLightbox={ onSetLightbox }
resetLightbox={ resetLightbox }
{ shouldRenderOtherControls && (
<BlockControls group="block">
{ isSingleSelected &&
! isEditingImage &&
! lockHrefControls &&
! lockUrlControls && (
<ImageURLInputUI
url={ href || '' }
onChangeUrl={ onSetHref }
linkDestination={ linkDestination }
mediaUrl={
( image && image.source_url ) || url
}
mediaLink={ image && image.link }
linkTarget={ linkTarget }
linkClass={ linkClass }
rel={ rel }
showLightboxSetting={ showLightboxSetting }
lightboxEnabled={ lightboxChecked }
onSetLightbox={ onSetLightbox }
resetLightbox={ resetLightbox }
/>
) }
{ allowCrop && (
<ToolbarButton
onClick={ () => setIsEditingImage( true ) }
icon={ crop }
label={ __( 'Crop' ) }
/>
) }
{ allowCrop && (
<ToolbarButton
onClick={ () => setIsEditingImage( true ) }
icon={ crop }
label={ __( 'Crop' ) }
/>
) }
{ isSingleSelected && canInsertCover && (
<ToolbarButton
icon={ overlayText }
label={ __( 'Add text over image' ) }
onClick={ switchToCover }
/>
) }
</BlockControls>
{ isSingleSelected && canInsertCover && (
<ToolbarButton
icon={ overlayText }
label={ __( 'Add text over image' ) }
onClick={ switchToCover }
/>
) }
</BlockControls>
) }
{ isSingleSelected && ! isEditingImage && ! lockUrlControls && (
<BlockControls group="other">
<MediaReplaceFlow
Expand Down

0 comments on commit 43fa143

Please sign in to comment.