Skip to content

Commit

Permalink
Image Block: Improve "can switch to cover" check (#33095)
Browse files Browse the repository at this point in the history
Check using possible block transformations
  • Loading branch information
Mamaduka authored and sarayourfriend committed Jul 15, 2021
1 parent a43881a commit 500d8c0
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 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 @@ -112,7 +87,6 @@ export default function Image( {
} ) {
const captionRef = useRef();
const prevUrl = usePrevious( url );
const { getBlock } = useSelect( blockEditorStore );
const { image, multiImageSelection } = useSelect(
( select ) => {
const { getMedia } = select( coreStore );
Expand All @@ -133,21 +107,46 @@ export default function Image( {
[ id, isSelected ]
);
const {
allowedBlockTypes,
canInsertCover,
getBlock,
imageEditing,
imageSizes,
maxWidth,
mediaUpload,
} = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return pick( getSettings(), [
'allowedBlockTypes',
'imageEditing',
'imageSizes',
'maxWidth',
'mediaUpload',
] );
} );
} = useSelect(
( select ) => {
const {
getBlock: _getBlock,
getBlockRootClientId,
getBlockTransformItems,
getSettings,
} = select( blockEditorStore );

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

return {
...settings,
getBlock: _getBlock,
canInsertCover:
transformations?.length &&
!! transformations.find(
( { name } ) => name === 'core/cover'
),
};
},
[ clientId ]
);
const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );
const { createErrorNotice, createSuccessNotice } = useDispatch(
noticesStore
Expand All @@ -166,12 +165,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 +319,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 500d8c0

Please sign in to comment.