From a37b541b85f4b0e11fa2e474eccde6d2f83939c9 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 17 Jan 2023 10:07:56 +0200 Subject: [PATCH 1/2] [Block Editor]: Stabilize __experimentalGetAllowedBlocks --- .../data/data-core-block-editor.md | 13 ++++++++++ .../src/components/inserter/index.js | 5 ++-- packages/block-editor/src/store/selectors.js | 26 +++++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index 84279435606a9..37c44d3b6a893 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -159,6 +159,19 @@ _Returns_ - `?string`: Return the client ID of the block, or null if none exists. +### getAllowedBlocks + +Returns the list of allowed inserter blocks for inner blocks children. + +_Parameters_ + +- _state_ `Object`: Editor state. +- _rootClientId_ `?string`: Optional root client ID of block list. + +_Returns_ + +- `Array?`: The list of allowed block types. + ### getBlock Returns a block given its client ID. This is a parsed copy of the block, diff --git a/packages/block-editor/src/components/inserter/index.js b/packages/block-editor/src/components/inserter/index.js index ec9ae5e543145..d1613fc1f9d40 100644 --- a/packages/block-editor/src/components/inserter/index.js +++ b/packages/block-editor/src/components/inserter/index.js @@ -225,7 +225,7 @@ export default compose( [ const { getBlockRootClientId, hasInserterItems, - __experimentalGetAllowedBlocks, + getAllowedBlocks, __experimentalGetDirectInsertBlock, getSettings, } = select( blockEditorStore ); @@ -235,8 +235,7 @@ export default compose( [ rootClientId = rootClientId || getBlockRootClientId( clientId ) || undefined; - const allowedBlocks = - __experimentalGetAllowedBlocks( rootClientId ); + const allowedBlocks = getAllowedBlocks( rootClientId ); const directInsertBlock = shouldDirectInsert && diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 630240b2d2b7b..7f7d6dbc1f7cd 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2144,14 +2144,14 @@ export const hasInserterItems = createSelector( ); /** - * Returns the list of allowed inserter blocks for inner blocks children + * Returns the list of allowed inserter blocks for inner blocks children. * * @param {Object} state Editor state. * @param {?string} rootClientId Optional root client ID of block list. * * @return {Array?} The list of allowed block types. */ -export const __experimentalGetAllowedBlocks = createSelector( +export const getAllowedBlocks = createSelector( ( state, rootClientId = null ) => { if ( ! rootClientId ) { return; @@ -2170,6 +2170,28 @@ export const __experimentalGetAllowedBlocks = createSelector( ] ); +export const __experimentalGetAllowedBlocks = createSelector( + ( state, rootClientId = null ) => { + deprecated( + 'wp.data.select( "core/block-editor" ).__experimentalGetAllowedBlocks', + { + alternative: + 'wp.data.select( "core/block-editor" ).getAllowedBlocks', + since: '6.2', + version: '6.4', + } + ); + return getAllowedBlocks( state, rootClientId ); + }, + ( state, rootClientId ) => [ + state.blockListSettings[ rootClientId ], + state.blocks.byClientId, + state.settings.allowedBlockTypes, + state.settings.templateLock, + getBlockTypes(), + ] +); + /** * Returns the block to be directly inserted by the block appender. * From 88f38105d3b3a5a2207b8ce776886709311e1895 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 17 Jan 2023 11:59:45 +0200 Subject: [PATCH 2/2] feedback --- packages/block-editor/src/store/selectors.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 7f7d6dbc1f7cd..6c956c2a6d19d 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2184,11 +2184,7 @@ export const __experimentalGetAllowedBlocks = createSelector( return getAllowedBlocks( state, rootClientId ); }, ( state, rootClientId ) => [ - state.blockListSettings[ rootClientId ], - state.blocks.byClientId, - state.settings.allowedBlockTypes, - state.settings.templateLock, - getBlockTypes(), + ...getAllowedBlocks.getDependants( state, rootClientId ), ] );