diff --git a/packages/block-editor/src/components/inner-blocks/README.md b/packages/block-editor/src/components/inner-blocks/README.md index 0f5d303b8c791..c5ca597311f51 100644 --- a/packages/block-editor/src/components/inner-blocks/README.md +++ b/packages/block-editor/src/components/inner-blocks/README.md @@ -194,4 +194,4 @@ For example, a button block, deeply nested in several levels of block `X` that u ### `directInsert` - **Type:** `Boolean` -- **Default:** - `undefined`. Determines whether the default block should be inserted directly into the InnerBlocks area by the block appender. +- **Default:** - `undefined`. Determines whether the default block is inserted directly into the InnerBlocks area by the block appender. Direct insertion is automatic if `defaultBlock` is defined or `allowedBlocks` specifies a single block type that has no variations. This setting can be used to override the automated behavior. diff --git a/packages/block-editor/src/components/inserter/index.js b/packages/block-editor/src/components/inserter/index.js index 8e2972fbe2bf5..c491751b55ebc 100644 --- a/packages/block-editor/src/components/inserter/index.js +++ b/packages/block-editor/src/components/inserter/index.js @@ -195,13 +195,14 @@ class PrivateInserter extends Component { const { position, hasSingleBlockType, + directInsert, directInsertBlock, insertOnlyAllowedBlock, __experimentalIsQuick: isQuick, onSelectOrClose, } = this.props; - if ( hasSingleBlockType || directInsertBlock ) { + if ( hasSingleBlockType || ( directInsertBlock && directInsert ) ) { return this.renderToggle( { onToggle: insertOnlyAllowedBlock } ); } @@ -225,51 +226,55 @@ class PrivateInserter extends Component { } export const ComposedPrivateInserter = compose( [ - withSelect( - ( select, { clientId, rootClientId, shouldDirectInsert = true } ) => { - const { - getBlockRootClientId, - hasInserterItems, - getAllowedBlocks, - getDirectInsertBlock, - getSettings, - } = select( blockEditorStore ); - - const { getBlockVariations } = select( blocksStore ); - - rootClientId = - rootClientId || getBlockRootClientId( clientId ) || undefined; - - const allowedBlocks = getAllowedBlocks( rootClientId ); - - const directInsertBlock = - shouldDirectInsert && getDirectInsertBlock( rootClientId ); - - const settings = getSettings(); - - const hasSingleBlockType = - allowedBlocks?.length === 1 && - getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' ) - ?.length === 0; - - let allowedBlockType = false; - if ( hasSingleBlockType ) { - allowedBlockType = allowedBlocks[ 0 ]; - } - - return { - hasItems: hasInserterItems( rootClientId ), - hasSingleBlockType, - blockTitle: allowedBlockType ? allowedBlockType.title : '', - allowedBlockType, - directInsertBlock, - rootClientId, - prioritizePatterns: - settings.__experimentalPreferPatternsOnRoot && - ! rootClientId, - }; + withSelect( ( select, { clientId, rootClientId, shouldDirectInsert } ) => { + const { + getBlock, + getBlockRootClientId, + hasInserterItems, + getAllowedBlocks, + getSettings, + getBlockListSettings, + } = select( blockEditorStore ); + + const { getBlockVariations } = select( blocksStore ); + + rootClientId = + rootClientId || getBlockRootClientId( clientId ) || undefined; + + const allowedBlocks = getAllowedBlocks( rootClientId ); + + const { defaultBlock, directInsert } = + getBlockListSettings( rootClientId ) ?? {}; + + const resolvedDirectInsert = + shouldDirectInsert ?? typeof directInsert === 'function' + ? directInsert( getBlock( rootClientId ) ) + : directInsert; + + const settings = getSettings(); + + const hasSingleBlockType = + allowedBlocks?.length === 1 && + getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' ) + ?.length === 0; + + let allowedBlockType = false; + if ( hasSingleBlockType ) { + allowedBlockType = allowedBlocks[ 0 ]; } - ), + + return { + hasItems: hasInserterItems( rootClientId ), + hasSingleBlockType, + blockTitle: allowedBlockType ? allowedBlockType.title : '', + allowedBlockType, + directInsert: resolvedDirectInsert, + directInsertBlock: defaultBlock, + rootClientId, + prioritizePatterns: + settings.__experimentalPreferPatternsOnRoot && ! rootClientId, + }; + } ), withDispatch( ( dispatch, ownProps, { select } ) => { return { insertOnlyAllowedBlock() { diff --git a/packages/block-library/src/buttons/edit.js b/packages/block-library/src/buttons/edit.js index dce160dfb2fea..a4545067efa59 100644 --- a/packages/block-library/src/buttons/edit.js +++ b/packages/block-library/src/buttons/edit.js @@ -12,7 +12,6 @@ import { store as blockEditorStore, } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; -import { store as blocksStore } from '@wordpress/blocks'; /** * Internal dependencies @@ -43,26 +42,16 @@ function ButtonsEdit( { attributes, className } ) { 'has-custom-font-size': fontSize || style?.typography?.fontSize, } ), } ); - const { preferredStyle, hasButtonVariations } = useSelect( ( select ) => { + const preferredStyle = useSelect( ( select ) => { const preferredStyleVariations = select( blockEditorStore ).getSettings() .__experimentalPreferredStyleVariations; - const buttonVariations = select( blocksStore ).getBlockVariations( - buttonBlockName, - 'inserter' - ); - return { - preferredStyle: - preferredStyleVariations?.value?.[ buttonBlockName ], - hasButtonVariations: buttonVariations.length > 0, - }; + return preferredStyleVariations?.value?.[ buttonBlockName ]; }, [] ); const innerBlocksProps = useInnerBlocksProps( blockProps, { allowedBlocks: ALLOWED_BLOCKS, defaultBlock: DEFAULT_BLOCK, - // This check should be handled by the `Inserter` internally to be consistent across all blocks that use it. - directInsert: ! hasButtonVariations, template: [ [ buttonBlockName,