From 0db4cce5774e909d25a8b8f900eddfe3fa6e4086 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 19 Feb 2024 15:43:51 +0400 Subject: [PATCH 1/3] getDirectInsertBlock: Remove 'directInsert' as a callback handler --- packages/block-editor/src/store/selectors.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 87d22e3e4727d..bfd7ae835483e 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2243,18 +2243,12 @@ export const getDirectInsertBlock = createSelector( if ( ! rootClientId ) { return; } - const defaultBlock = - state.blockListSettings[ rootClientId ]?.defaultBlock; - const directInsert = - state.blockListSettings[ rootClientId ]?.directInsert; + const { defaultBlock, directInsert } = + state.blockListSettings[ rootClientId ] ?? {}; if ( ! defaultBlock || ! directInsert ) { return; } - if ( typeof directInsert === 'function' ) { - return directInsert( getBlock( state, rootClientId ) ) - ? defaultBlock - : null; - } + return defaultBlock; }, ( state, rootClientId ) => [ From 3825ab999820c80b2d6ad26ab215c9c23e1738cc Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 19 Feb 2024 16:05:43 +0400 Subject: [PATCH 2/3] No need to memoize selectors --- .../data/data-core-block-editor.md | 2 +- packages/block-editor/src/store/selectors.js | 63 ++++++++----------- 2 files changed, 28 insertions(+), 37 deletions(-) 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/store/selectors.js b/packages/block-editor/src/store/selectors.js index bfd7ae835483e..03c44ffc7dff4 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2231,50 +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, directInsert } = - state.blockListSettings[ rootClientId ] ?? {}; - if ( ! defaultBlock || ! directInsert ) { - return; - } +export function getDirectInsertBlock( state, rootClientId = null ) { + if ( ! rootClientId ) { + return; + } + const { defaultBlock, directInsert } = + state.blockListSettings[ rootClientId ] ?? {}; + if ( ! defaultBlock || ! directInsert ) { + return; + } - return defaultBlock; - }, - ( state, rootClientId ) => [ - state.blockListSettings[ rootClientId ], - state.blocks.tree.get( rootClientId ), - ] -); + return defaultBlock; +} -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 ), - ] -); +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 ) => From bdfcd07ee977fa42defc893c40c00f3fd6a805cf Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 22 Feb 2024 15:53:37 +0400 Subject: [PATCH 3/3] Add deprecation notice --- .../inner-blocks/use-nested-settings-update.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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