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

Blocks: directly check blockType vs hasBlockSupport #58335

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
__unstableUseAutocompleteProps as useAutocompleteProps,
} from '@wordpress/components';
import { useMemo } from '@wordpress/element';
import { getDefaultBlockName, getBlockSupport } from '@wordpress/blocks';
import { getDefaultBlockName, getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -31,7 +31,7 @@ function useCompleters( { completers = EMPTY_ARRAY } ) {

if (
name === getDefaultBlockName() ||
getBlockSupport( name, '__experimentalSlashInserter', false )
getBlockType( name ).supports?.__experimentalSlashInserter
) {
filteredCompleters = [ ...filteredCompleters, blockAutocompleter ];
}
Expand Down
8 changes: 2 additions & 6 deletions packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { withFilters } from '@wordpress/components';
import {
getBlockDefaultClassName,
hasBlockSupport,
getBlockType,
} from '@wordpress/blocks';
import { getBlockDefaultClassName, getBlockType } from '@wordpress/blocks';
import { useContext, useMemo } from '@wordpress/element';

/**
Expand Down Expand Up @@ -72,7 +68,7 @@ const EditWithGeneratedProps = ( props ) => {
}

// Generate a class name for the block's editable form.
const generatedClassName = hasBlockSupport( blockType, 'className', true )
const generatedClassName = blockType.supports?.className
? getBlockDefaultClassName( name )
: null;
const className = classnames(
Expand Down
6 changes: 3 additions & 3 deletions packages/block-editor/src/components/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { useMemo } from '@wordpress/element';

import { hasBlockSupport } from '@wordpress/blocks';
/**
* Internal dependencies
*/
Expand All @@ -30,6 +29,7 @@ export default function BlockEdit( {
mayDisplayControls,
mayDisplayParentControls,
blockEditingMode,
blockType,
// The remaining props are passed through the BlockEdit filters and are thus
// public API!
...props
Expand All @@ -43,8 +43,8 @@ export default function BlockEdit( {
} = props;
const { layout = null } = attributes;
const layoutSupport =
hasBlockSupport( name, 'layout', false ) ||
hasBlockSupport( name, '__experimentalLayout', false );
blockType?.supports?.layout ||
blockType?.supports?.__experimentalLayout;
return (
<BlockEditContextProvider
// It is important to return the same object if props haven't
Expand Down
17 changes: 6 additions & 11 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function BlockListBlock( {
const onRemove = useCallback( () => removeBlock( clientId ), [ clientId ] );

const parentLayout = useLayout() || {};
const blockType = getBlockType( name );

// We wrap the BlockEdit component in a div that hides it when editing in
// HTML mode. This allows us to render all of the ancillary pieces
Expand All @@ -134,11 +135,10 @@ function BlockListBlock( {
mayDisplayControls={ mayDisplayControls }
mayDisplayParentControls={ mayDisplayParentControls }
blockEditingMode={ context.blockEditingMode }
blockType={ blockType }
/>
);

const blockType = getBlockType( name );

// Determine whether the block has props to apply to the wrapper.
if ( blockType?.getEditWrapperProps ) {
wrapperProps = mergeWrapperProps(
Expand Down Expand Up @@ -534,10 +534,7 @@ function BlockListBlockProvider( props ) {
return;
}

const {
hasBlockSupport: _hasBlockSupport,
getActiveBlockVariation,
} = select( blocksStore );
const { getActiveBlockVariation } = select( blocksStore );
const _isSelected = isBlockSelected( clientId );
const templateLock = getTemplateLock( rootClientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
Expand Down Expand Up @@ -582,11 +579,9 @@ function BlockListBlockProvider( props ) {
( id ) => getBlockName( id ) === blockName
) ),
mayDisplayParentControls:
_hasBlockSupport(
getBlockName( clientId ),
'__experimentalExposeControlsToChildren',
false
) && hasSelectedInnerBlock( clientId ),
!! blockType?.supports
?.__experimentalExposeControlsToChildren &&
hasSelectedInnerBlock( clientId ),
index: getBlockIndex( clientId ),
blockApiVersion: blockType?.apiVersion || 1,
blockTitle: match?.title || blockType?.title,
Expand Down
22 changes: 8 additions & 14 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
getBlockSupport,
store as blocksStore,
__unstableGetInnerBlocksProps as getInnerBlocksProps,
} from '@wordpress/blocks';
Expand Down Expand Up @@ -69,7 +68,6 @@ function UncontrolledInnerBlocks( props ) {
orientation,
placeholder,
layout,
name,
blockType,
innerBlocks,
parentLock,
Expand Down Expand Up @@ -99,8 +97,8 @@ function UncontrolledInnerBlocks( props ) {
);

const defaultLayoutBlockSupport =
getBlockSupport( name, 'layout' ) ||
getBlockSupport( name, '__experimentalLayout' ) ||
blockType?.supports?.layout ||
blockType?.supports?.__experimentalLayout ||
EMPTY_OBJECT;

const { allowSizingOnChildren = false } = defaultLayoutBlockSupport;
Expand Down Expand Up @@ -191,7 +189,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const {
__experimentalCaptureToolbars,
hasOverlay,
name,
blockType,
innerBlocks,
parentLock,
Expand All @@ -215,25 +212,23 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
__unstableHasActiveBlockOverlayActive,
getBlockEditingMode,
} = select( blockEditorStore );
const { hasBlockSupport, getBlockType } = select( blocksStore );
const { getBlockType } = select( blocksStore );
const blockName = getBlockName( clientId );
const enableClickThrough =
__unstableGetEditorMode() === 'navigation';
const blockEditingMode = getBlockEditingMode( clientId );
const _parentClientId = getBlockRootClientId( clientId );
const _blockType = getBlockType( blockName );
return {
__experimentalCaptureToolbars: hasBlockSupport(
blockName,
'__experimentalExposeControlsToChildren',
false
),
__experimentalCaptureToolbars:
!! _blockType?.supports
?.__experimentalExposeControlsToChildren,
hasOverlay:
blockName !== 'core/template' &&
! isBlockSelected( clientId ) &&
! hasSelectedInnerBlock( clientId, true ) &&
enableClickThrough,
name: blockName,
blockType: getBlockType( blockName ),
blockType: _blockType,
innerBlocks: getBlocks( clientId ),
parentLock: getTemplateLock( _parentClientId ),
parentClientId: _parentClientId,
Expand Down Expand Up @@ -261,7 +256,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const innerBlocksProps = {
__experimentalCaptureToolbars,
layout,
name,
blockType,
innerBlocks,
parentLock,
Expand Down
9 changes: 3 additions & 6 deletions packages/block-editor/src/components/use-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import {
__EXPERIMENTAL_PATHS_WITH_MERGE as PATHS_WITH_MERGE,
hasBlockSupport,
getBlockType,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
Expand Down Expand Up @@ -162,11 +162,8 @@ export function useSettings( ...paths ) {
select( blockEditorStore ).getBlockName(
candidateClientId
);
return hasBlockSupport(
candidateBlockName,
'__experimentalSettings',
false
);
return !! getBlockType( candidateBlockName )?.supports
?.__experimentalSettings;
} )
: [];

Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function addAttribute( settings ) {
if ( 'type' in ( settings.attributes?.align ?? {} ) ) {
return settings;
}
if ( hasBlockSupport( settings, 'align' ) ) {
if ( settings.supports?.align ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
Expand Down Expand Up @@ -157,8 +157,8 @@ export default {
useBlockProps,
addSaveProps: addAssignedAlign,
attributeKeys: [ 'align' ],
hasSupport( name ) {
return hasBlockSupport( name, 'align', false );
hasSupport( supports ) {
return !! supports.align;
},
};

Expand Down Expand Up @@ -188,8 +188,8 @@ function useBlockProps( { name, align } ) {
*/
export function addAssignedAlign( props, blockType, attributes ) {
const { align } = attributes;
const blockAlign = getBlockSupport( blockType, 'align' );
const hasWideBlockSupport = hasBlockSupport( blockType, 'alignWide', true );
const blockAlign = blockType.supports?.align;
const hasWideBlockSupport = blockType.supports?.alignWide ?? true;

// Compute valid alignments without taking into account if
// the theme supports wide alignments or not.
Expand Down
9 changes: 4 additions & 5 deletions packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { addFilter } from '@wordpress/hooks';
import { PanelBody, TextControl, ExternalLink } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { hasBlockSupport } from '@wordpress/blocks';
import { Platform } from '@wordpress/element';

/**
Expand Down Expand Up @@ -40,7 +39,7 @@ export function addAttribute( settings ) {
if ( 'type' in ( settings.attributes?.anchor ?? {} ) ) {
return settings;
}
if ( hasBlockSupport( settings, 'anchor' ) ) {
if ( settings.supports?.anchor ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
Expand Down Expand Up @@ -123,8 +122,8 @@ export default {
addSaveProps,
edit: BlockEditAnchorControlPure,
attributeKeys: [ 'anchor' ],
hasSupport( name ) {
return hasBlockSupport( name, 'anchor' );
hasSupport( supports ) {
return !! supports.anchor;
},
};

Expand All @@ -140,7 +139,7 @@ export default {
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockSupport( blockType, 'anchor' ) ) {
if ( blockType.supports?.anchor ) {
extraProps.id = attributes.anchor === '' ? null : attributes.anchor;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/block-editor/src/hooks/aria-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';

const ARIA_LABEL_SCHEMA = {
type: 'string',
Expand All @@ -24,7 +23,7 @@ export function addAttribute( settings ) {
if ( settings?.attributes?.ariaLabel?.type ) {
return settings;
}
if ( hasBlockSupport( settings, 'ariaLabel' ) ) {
if ( settings.supports?.ariaLabel ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
Expand All @@ -47,7 +46,7 @@ export function addAttribute( settings ) {
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockSupport( blockType, 'ariaLabel' ) ) {
if ( blockType.supports?.ariaLabel ) {
extraProps[ 'aria-label' ] =
attributes.ariaLabel === '' ? null : attributes.ariaLabel;
}
Expand All @@ -58,8 +57,8 @@ export function addSaveProps( extraProps, blockType, attributes ) {
export default {
addSaveProps,
attributeKeys: [ 'ariaLabel' ],
hasSupport( name ) {
return hasBlockSupport( name, 'ariaLabel' );
hasSupport( supports ) {
return !! supports.ariaLabel;
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/block-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function BlockHooksControlPure( { name, clientId } ) {

export default {
edit: BlockHooksControlPure,
enableForAllBlocks: true,
hasSupport() {
return true;
},
Expand Down
12 changes: 4 additions & 8 deletions packages/block-editor/src/hooks/block-renaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';

Expand All @@ -24,11 +23,8 @@ export function addLabelCallback( settings ) {
return settings;
}

const supportsBlockNaming = hasBlockSupport(
settings,
'renaming',
true // default value
);
// Defaults to true.
const supportsBlockNaming = settings?.supports?.renaming ?? true;

// Check whether block metadata is supported before using it.
if ( supportsBlockNaming ) {
Expand Down Expand Up @@ -66,8 +62,8 @@ function BlockRenameControlPure( { metadata, setAttributes } ) {
export default {
edit: BlockRenameControlPure,
attributeKeys: [ 'metadata' ],
hasSupport( name ) {
return hasBlockSupport( name, 'renaming', true );
hasSupport( supports ) {
return !! supports.renaming;
},
};

Expand Down
Loading
Loading