Skip to content

Commit

Permalink
Image block: fix cover transform and excessive re-rendering (#32102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored May 21, 2021
1 parent 65a9a15 commit 5215429
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/cover/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const transforms = {
anchor,
style: {
color: {
duotone: style.color.duotone,
duotone: style?.color?.duotone,
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function ImageEdit( {
insertBlocksAfter,
noticeOperations,
onReplace,
clientId,
} ) {
const {
url = '',
Expand Down Expand Up @@ -300,6 +301,7 @@ export function ImageEdit( {
onSelectURL={ onSelectURL }
onUploadError={ onUploadError }
containerRef={ ref }
clientId={ clientId }
/>
) }
{ ! url && (
Expand Down
33 changes: 16 additions & 17 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,25 @@ export default function Image( {
onSelectURL,
onUploadError,
containerRef,
clientId,
} ) {
const captionRef = useRef();
const prevUrl = usePrevious( url );
const { block, currentId, image, multiImageSelection } = useSelect(
const { getBlock } = useSelect( blockEditorStore );
const { image, multiImageSelection } = useSelect(
( select ) => {
const { getMedia } = select( coreStore );
const {
getMultiSelectedBlockClientIds,
getBlockName,
getSelectedBlock,
getSelectedBlockClientId,
} = select( blockEditorStore );
const { getMultiSelectedBlockClientIds, getBlockName } = select(
blockEditorStore
);
const multiSelectedClientIds = getMultiSelectedBlockClientIds();
return {
block: getSelectedBlock(),
currentId: getSelectedBlockClientId(),
image: id && isSelected ? getMedia( id ) : null,
multiImageSelection:
multiSelectedClientIds.length &&
multiSelectedClientIds.every(
( clientId ) =>
getBlockName( clientId ) === 'core/image'
( _clientId ) =>
getBlockName( _clientId ) === 'core/image'
),
};
},
Expand Down Expand Up @@ -260,6 +257,13 @@ export default function Image( {
const canEditImage = id && naturalWidth && naturalHeight && imageEditing;
const allowCrop = ! multiImageSelection && canEditImage && ! isEditingImage;

function switchToCover() {
replaceBlocks(
clientId,
switchToBlockType( getBlock( clientId ), 'core/cover' )
);
}

const controls = (
<>
<BlockControls group="block">
Expand Down Expand Up @@ -297,12 +301,7 @@ export default function Image( {
<ToolbarButton
icon={ overlayText }
label={ __( 'Add text over image' ) }
onClick={ () =>
replaceBlocks(
currentId,
switchToBlockType( block, 'core/cover' )
)
}
onClick={ switchToCover }
/>
) }
</BlockControls>
Expand Down

0 comments on commit 5215429

Please sign in to comment.