diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index 9e9c1ef140c76..486fcddfe04ac 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -515,7 +515,7 @@ _Parameters_ _Returns_ -- `?WPDirectInsertBlock`: The block type to be directly inserted. +- `WPDirectInsertBlock|undefined`: The block type to be directly inserted. _Type Definition_ diff --git a/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js b/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js index 80acf0e639f9e..25a3dc9dadfa9 100644 --- a/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js +++ b/packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js @@ -37,11 +37,11 @@ function useShallowMemo( value ) { * in inner blocks. * @param {string[]} prioritizedInserterBlocks Block names and/or block variations to be prioritized in the inserter, in the format {blockName}/{variationName}. * @param {?WPDirectInsertBlock} defaultBlock The default block to insert: [ blockName, { blockAttributes } ]. - * @param {?Function|boolean} directInsert If a default block should be inserted directly by the appender. + * @param {?boolean} directInsert If a default block should be inserted directly by the appender. * * @param {?WPDirectInsertBlock} __experimentalDefaultBlock A deprecated prop for the default block to insert: [ blockName, { blockAttributes } ]. Use `defaultBlock` instead. * - * @param {?Function|boolean} __experimentalDirectInsert A deprecated prop for whether a default block should be inserted directly by the appender. Use `directInsert` instead. + * @param {?boolean} __experimentalDirectInsert A deprecated prop for whether a default block should be inserted directly by the appender. Use `directInsert` instead. * * @param {string} [templateLock] The template lock specified for the inner * blocks component. (e.g. "all") @@ -138,6 +138,16 @@ export default function useNestedSettingsUpdate( newSettings.directInsert = directInsert; } + if ( + newSettings.directInsert !== undefined && + typeof newSettings.directInsert !== 'boolean' + ) { + deprecated( 'Using `Function` as a `directInsert` argument', { + alternative: '`boolean` values', + since: '6.5', + } ); + } + // Batch updates to block list settings to avoid triggering cascading renders // for each container block included in a tree and optimize initial render. // To avoid triggering updateBlockListSettings for each container block diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 87d22e3e4727d..03c44ffc7dff4 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2231,56 +2231,41 @@ export const __experimentalGetAllowedBlocks = createSelector( * @param {Object} state Editor state. * @param {?string} rootClientId Optional root client ID of block list. * - * @return {?WPDirectInsertBlock} The block type to be directly inserted. + * @return {WPDirectInsertBlock|undefined} The block type to be directly inserted. * * @typedef {Object} WPDirectInsertBlock * @property {string} name The type of block. * @property {?Object} attributes Attributes to pass to the newly created block. * @property {?Array} attributesToCopy Attributes to be copied from adjecent blocks when inserted. */ -export const getDirectInsertBlock = createSelector( - ( state, rootClientId = null ) => { - if ( ! rootClientId ) { - return; - } - const defaultBlock = - state.blockListSettings[ rootClientId ]?.defaultBlock; - const directInsert = - state.blockListSettings[ rootClientId ]?.directInsert; - if ( ! defaultBlock || ! directInsert ) { - return; - } - if ( typeof directInsert === 'function' ) { - return directInsert( getBlock( state, rootClientId ) ) - ? defaultBlock - : null; - } - return defaultBlock; - }, - ( state, rootClientId ) => [ - state.blockListSettings[ rootClientId ], - state.blocks.tree.get( rootClientId ), - ] -); +export function getDirectInsertBlock( state, rootClientId = null ) { + if ( ! rootClientId ) { + return; + } + const { defaultBlock, directInsert } = + state.blockListSettings[ rootClientId ] ?? {}; + if ( ! defaultBlock || ! directInsert ) { + return; + } -export const __experimentalGetDirectInsertBlock = createSelector( - ( state, rootClientId = null ) => { - deprecated( - 'wp.data.select( "core/block-editor" ).__experimentalGetDirectInsertBlock', - { - alternative: - 'wp.data.select( "core/block-editor" ).getDirectInsertBlock', - since: '6.3', - version: '6.4', - } - ); - return getDirectInsertBlock( state, rootClientId ); - }, - ( state, rootClientId ) => [ - state.blockListSettings[ rootClientId ], - state.blocks.tree.get( rootClientId ), - ] -); + return defaultBlock; +} + +export function __experimentalGetDirectInsertBlock( + state, + rootClientId = null +) { + deprecated( + 'wp.data.select( "core/block-editor" ).__experimentalGetDirectInsertBlock', + { + alternative: + 'wp.data.select( "core/block-editor" ).getDirectInsertBlock', + since: '6.3', + version: '6.4', + } + ); + return getDirectInsertBlock( state, rootClientId ); +} export const __experimentalGetParsedPattern = createRegistrySelector( ( select ) =>