Skip to content

Commit

Permalink
Image Block: Update "can switch to cover" check
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jun 30, 2021
1 parent 79e02a2 commit 58a8253
Showing 1 changed file with 29 additions and 44 deletions.
73 changes: 29 additions & 44 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ import {
import { useEffect, useState, useRef } from '@wordpress/element';
import { __, sprintf, isRTL } from '@wordpress/i18n';
import { getPath } from '@wordpress/url';
import {
createBlock,
getBlockType,
switchToBlockType,
} from '@wordpress/blocks';
import { createBlock, switchToBlockType } from '@wordpress/blocks';
import { crop, overlayText, upload } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -61,27 +57,6 @@ function getFilename( url ) {
}
}

/**
* Checks if the given block is registered and is in the allowed blocks list.
*
* @param {string} name Block name.
* @param {boolean|Array} list Allowed block types.
*
* @return {boolean} Whether the block exists.
*/
function checkBlockExists( name, list ) {
if ( ! getBlockType( name ) ) {
return false;
}

// The allowed blocks list has a boolean value so return it.
if ( ! Array.isArray( list ) ) {
return list;
}

return list.includes( name );
}

export default function Image( {
temporaryURL,
attributes: {
Expand Down Expand Up @@ -133,21 +108,37 @@ export default function Image( {
[ id, isSelected ]
);
const {
allowedBlockTypes,
canInsertCover,
imageEditing,
imageSizes,
maxWidth,
mediaUpload,
} = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return pick( getSettings(), [
'allowedBlockTypes',
'imageEditing',
'imageSizes',
'maxWidth',
'mediaUpload',
] );
} );
} = useSelect(
( select ) => {
const {
canInsertBlockType,
getBlockRootClientId,
getSettings,
} = select( blockEditorStore );

const rootClientId = getBlockRootClientId( clientId );
const settings = pick( getSettings(), [
'imageEditing',
'imageSizes',
'maxWidth',
'mediaUpload',
] );

return {
...settings,
canInsertCover: canInsertBlockType(
'core/cover',
rootClientId
),
};
},
[ clientId ]
);
const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );
const { createErrorNotice, createSuccessNotice } = useDispatch(
noticesStore
Expand All @@ -166,12 +157,6 @@ export default function Image( {
( { name, slug } ) => ( { value: slug, label: name } )
);

// Check if the cover block is registered and in allowed block list.
const coverBlockExists = checkBlockExists(
'core/cover',
allowedBlockTypes
);

// If an image is externally hosted, try to fetch the image data. This may
// fail if the image host doesn't allow CORS with the domain. If it works,
// we can enable a button in the toolbar to upload the image.
Expand Down Expand Up @@ -326,7 +311,7 @@ export default function Image( {
label={ __( 'Upload external image' ) }
/>
) }
{ ! multiImageSelection && coverBlockExists && (
{ ! multiImageSelection && canInsertCover && (
<ToolbarButton
icon={ overlayText }
label={ __( 'Add text over image' ) }
Expand Down

0 comments on commit 58a8253

Please sign in to comment.