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 block: fix cover transform and excessive re-rendering #32102

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost feel like we should deprecate this selector or have some kind of warning in all selectors that returns block objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two other usage we have of that selectors seem safe.

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