Skip to content

Commit

Permalink
Image block: remove 2 block editor store subs
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 23, 2024
1 parent 773d03b commit 6a91130
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
21 changes: 8 additions & 13 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function hasSize( image, size ) {
export function ImageEdit( {
attributes,
setAttributes,
isSelected,
isSelected: isSingleSelected,
className,
insertBlocksAfter,
onReplace,
Expand Down Expand Up @@ -140,14 +140,7 @@ export function ImageEdit( {
}, [ align ] );

const ref = useRef();
const { imageDefaultSize, mediaUpload } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
imageDefaultSize: settings.imageDefaultSize,
mediaUpload: settings.mediaUpload,
};
}, [] );
const { getSettings } = useSelect( blockEditorStore );
const blockEditingMode = useBlockEditingMode();

const { createErrorNotice } = useDispatch( noticesStore );
Expand Down Expand Up @@ -181,6 +174,8 @@ export function ImageEdit( {

setTemporaryURL();

const { imageDefaultSize } = getSettings();

// Try to use the previous selected image size if its available
// otherwise try the default image size or fallback to "full"
let newSize = 'full';
Expand Down Expand Up @@ -263,7 +258,7 @@ export function ImageEdit( {
setAttributes( {
url: newURL,
id: undefined,
sizeSlug: imageDefaultSize,
sizeSlug: getSettings().imageDefaultSize,
} );
}
}
Expand All @@ -279,7 +274,7 @@ export function ImageEdit( {
const file = getBlobByURL( url );

if ( file ) {
mediaUpload( {
getSettings().mediaUpload( {
filesList: [ file ],
onFileChange: ( [ img ] ) => {
onSelectImage( img );
Expand Down Expand Up @@ -337,7 +332,7 @@ export function ImageEdit( {
<Placeholder
className={ classnames( 'block-editor-media-placeholder', {
[ borderProps.className ]:
!! borderProps.className && ! isSelected,
!! borderProps.className && ! isSingleSelected,
} ) }
withIllustration={ true }
icon={ icon }
Expand Down Expand Up @@ -367,7 +362,7 @@ export function ImageEdit( {
temporaryURL={ temporaryURL }
attributes={ attributes }
setAttributes={ setAttributes }
isSelected={ isSelected }
isSelected={ isSingleSelected }
insertBlocksAfter={ insertBlocksAfter }
onReplace={ onReplace }
onSelectImage={ onSelectImage }
Expand Down
43 changes: 19 additions & 24 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function Image( {
temporaryURL,
attributes,
setAttributes,
isSelected,
isSingleSelected,
insertBlocksAfter,
onReplace,
onSelectImage,
Expand Down Expand Up @@ -134,17 +134,12 @@ export default function Image( {
const { allowResize = true } = context;
const { getBlock } = useSelect( blockEditorStore );

const { image } = useSelect(
( select ) => {
const { getMedia } = select( coreStore );
return {
image:
id && isSelected
? getMedia( id, { context: 'view' } )
: null,
};
},
[ id, isSelected ]
const image = useSelect(
( select ) =>
id && isSingleSelected
? select( coreStore ).getMedia( id, { context: 'view' } )
: null,
[ id, isSingleSelected ]
);

const {
Expand Down Expand Up @@ -219,7 +214,7 @@ export default function Image( {
useEffect( () => {
if (
! isExternalImage( id, url ) ||
! isSelected ||
! isSingleSelected ||
! canUploadMedia
) {
setExternalBlob();
Expand All @@ -235,7 +230,7 @@ export default function Image( {
.then( ( blob ) => setExternalBlob( blob ) )
// Do nothing, cannot upload.
.catch( () => {} );
}, [ id, url, isSelected, externalBlob, canUploadMedia ] );
}, [ id, url, isSingleSelected, externalBlob, canUploadMedia ] );

// Get naturalWidth and naturalHeight from image ref, and fall back to loaded natural
// width and height. This resolves an issue in Safari where the loaded natural
Expand Down Expand Up @@ -325,13 +320,13 @@ export default function Image( {
}

useEffect( () => {
if ( ! isSelected ) {
if ( ! isSingleSelected ) {
setIsEditingImage( false );
}
}, [ isSelected ] );
}, [ isSingleSelected ] );

const canEditImage = id && naturalWidth && naturalHeight && imageEditing;
const allowCrop = ! multiImageSelection && canEditImage && ! isEditingImage;
const allowCrop = isSingleSelected && canEditImage && ! isEditingImage;

function switchToCover() {
replaceBlocks(
Expand Down Expand Up @@ -413,7 +408,7 @@ export default function Image( {
const controls = (
<>
<BlockControls group="block">
{ ! multiImageSelection && ! isEditingImage && (
{ isSingleSelected && ! isEditingImage && (
<ImageURLInputUI
url={ href || '' }
onChangeUrl={ onSetHref }
Expand All @@ -432,15 +427,15 @@ export default function Image( {
label={ __( 'Crop' ) }
/>
) }
{ ! multiImageSelection && canInsertCover && (
{ isSingleSelected && canInsertCover && (
<ToolbarButton
icon={ overlayText }
label={ __( 'Add text over image' ) }
onClick={ switchToCover }
/>
) }
</BlockControls>
{ ! multiImageSelection && ! isEditingImage && (
{ isSingleSelected && ! isEditingImage && (
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ id }
Expand All @@ -453,7 +448,7 @@ export default function Image( {
/>
</BlockControls>
) }
{ ! multiImageSelection && externalBlob && (
{ isSingleSelected && externalBlob && (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
Expand All @@ -470,7 +465,7 @@ export default function Image( {
resetAll={ resetAll }
dropdownMenuProps={ TOOLSPANEL_DROPDOWNMENU_PROPS }
>
{ ! multiImageSelection && (
{ isSingleSelected && (
<ToolsPanelItem
label={ __( 'Alternative text' ) }
isShownByDefault={ true }
Expand Down Expand Up @@ -711,7 +706,7 @@ export default function Image( {
width: currentWidth ?? 'auto',
height: currentHeight ?? 'auto',
} }
showHandle={ isSelected }
showHandle={ isSingleSelected }
minWidth={ minWidth }
maxWidth={ maxWidthBuffer }
minHeight={ minHeight }
Expand Down Expand Up @@ -759,7 +754,7 @@ export default function Image( {
<Caption
attributes={ attributes }
setAttributes={ setAttributes }
isSelected={ isSelected }
isSelected={ isSingleSelected }
insertBlocksAfter={ insertBlocksAfter }
label={ __( 'Image caption text' ) }
showToolbarButton={
Expand Down

0 comments on commit 6a91130

Please sign in to comment.