Skip to content

Commit

Permalink
Image: Reduce 'block-editor' store subscriptions (#57358)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Dec 26, 2023
1 parent 41e8276 commit 87ec413
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,49 @@ export default function Image( {
const { allowResize = true } = context;
const { getBlock } = useSelect( blockEditorStore );

const { image, multiImageSelection } = useSelect(
const { image } = useSelect(
( select ) => {
const { getMedia } = select( coreStore );
const { getMultiSelectedBlockClientIds, getBlockName } =
select( blockEditorStore );
const multiSelectedClientIds = getMultiSelectedBlockClientIds();
return {
image:
id && isSelected
? getMedia( id, { context: 'view' } )
: null,
};
},
[ id, isSelected ]
);

const {
canInsertCover,
imageEditing,
imageSizes,
maxWidth,
mediaUpload,
multiImageSelection,
} = useSelect(
( select ) => {
const {
getBlockRootClientId,
getMultiSelectedBlockClientIds,
getBlockName,
getSettings,
canInsertBlockType,
} = select( blockEditorStore );

const rootClientId = getBlockRootClientId( clientId );
const settings = getSettings();
const multiSelectedClientIds = getMultiSelectedBlockClientIds();

return {
imageEditing: settings.imageEditing,
imageSizes: settings.imageSizes,
maxWidth: settings.maxWidth,
mediaUpload: settings.mediaUpload,
canInsertCover: canInsertBlockType(
'core/cover',
rootClientId
),
multiImageSelection:
multiSelectedClientIds.length &&
multiSelectedClientIds.every(
Expand All @@ -153,33 +185,8 @@ export default function Image( {
),
};
},
[ id, isSelected ]
[ clientId ]
);
const { canInsertCover, imageEditing, imageSizes, maxWidth, mediaUpload } =
useSelect(
( select ) => {
const {
getBlockRootClientId,
getSettings,
canInsertBlockType,
} = select( blockEditorStore );

const rootClientId = getBlockRootClientId( clientId );
const settings = getSettings();

return {
imageEditing: settings.imageEditing,
imageSizes: settings.imageSizes,
maxWidth: settings.maxWidth,
mediaUpload: settings.mediaUpload,
canInsertCover: canInsertBlockType(
'core/cover',
rootClientId
),
};
},
[ clientId ]
);

const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );
const { createErrorNotice, createSuccessNotice } =
Expand Down

0 comments on commit 87ec413

Please sign in to comment.