diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 5a88b6fb09b0a..5adbb72d542cb 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1255,11 +1255,20 @@ const canInsertBlockTypeUnmemoized = ( ); const blockAllowedParentBlocks = blockType.parent; - const parentName = getBlockName( state, rootClientId ); - const hasBlockAllowedParent = checkAllowList( - blockAllowedParentBlocks, - parentName - ); + let hasBlockAllowedParent = null; + if ( blockAllowedParentBlocks ) { + const parents = [ + rootClientId, + ...getBlockParents( state, rootClientId ), + ]; + + hasBlockAllowedParent = some( parents, ( parentClientId ) => + checkAllowList( + blockAllowedParentBlocks, + getBlockName( state, parentClientId ) + ) + ); + } const canInsert = ( hasParentAllowedBlock === null && hasBlockAllowedParent === null ) || diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index a9baaec53d346..db6562dfb8784 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -2248,6 +2248,7 @@ describe( 'selectors', () => { blocks: { byClientId: {}, attributes: {}, + parents: {}, }, blockListSettings: {}, settings: {}, @@ -2284,6 +2285,7 @@ describe( 'selectors', () => { attributes: { block1: {}, }, + parents: {}, }, blockListSettings: { block1: {}, @@ -2304,6 +2306,7 @@ describe( 'selectors', () => { attributes: { block1: {}, }, + parents: {}, }, blockListSettings: { block1: {}, @@ -2368,6 +2371,7 @@ describe( 'selectors', () => { attributes: { block1: {}, }, + parents: {}, }, blockListSettings: { block1: { @@ -2404,6 +2408,7 @@ describe( 'selectors', () => { blocks: { byClientId: {}, attributes: {}, + parents: {}, }, blockListSettings: {}, settings: {}, @@ -2428,6 +2433,7 @@ describe( 'selectors', () => { 2: {}, 3: {}, }, + parents: {}, }, blockListSettings: { 1: { @@ -2465,6 +2471,32 @@ describe( 'selectors', () => { }; expect( canInsertBlocks( state, [ '2', '3' ], '1' ) ).toBe( false ); } ); + + it( 'should allow blocks to be inserted into any parent if an ancestor allows it', () => { + const state = { + blocks: { + byClientId: { + block1: { name: 'core/test-block-b' }, + block2: { name: 'core/test-block-a' }, + }, + attributes: { + block1: {}, + block2: {}, + }, + parents: { + block2: 'block1', + }, + }, + blockListSettings: { + block1: {}, + block2: {}, + }, + settings: {}, + }; + expect( + canInsertBlockType( state, 'core/test-block-c', 'block2' ) + ).toBe( true ); + } ); } ); describe( 'getInserterItems', () => { @@ -2664,6 +2696,7 @@ describe( 'selectors', () => { }, }, controlledInnerBlocks: {}, + parents: {}, }, preferences: { insertUsage: {}, @@ -2872,6 +2905,7 @@ describe( 'selectors', () => { }, }, controlledInnerBlocks: {}, + parents: {}, }, preferences: { insertUsage: {},