Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Check all ancestors inside canInsertBlockTypeUnmemoized selector #39228

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ||
Expand Down
34 changes: 34 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,7 @@ describe( 'selectors', () => {
blocks: {
byClientId: {},
attributes: {},
parents: {},
},
blockListSettings: {},
settings: {},
Expand Down Expand Up @@ -2284,6 +2285,7 @@ describe( 'selectors', () => {
attributes: {
block1: {},
},
parents: {},
},
blockListSettings: {
block1: {},
Expand All @@ -2304,6 +2306,7 @@ describe( 'selectors', () => {
attributes: {
block1: {},
},
parents: {},
},
blockListSettings: {
block1: {},
Expand Down Expand Up @@ -2368,6 +2371,7 @@ describe( 'selectors', () => {
attributes: {
block1: {},
},
parents: {},
},
blockListSettings: {
block1: {
Expand Down Expand Up @@ -2404,6 +2408,7 @@ describe( 'selectors', () => {
blocks: {
byClientId: {},
attributes: {},
parents: {},
},
blockListSettings: {},
settings: {},
Expand All @@ -2428,6 +2433,7 @@ describe( 'selectors', () => {
2: {},
3: {},
},
parents: {},
},
blockListSettings: {
1: {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -2664,6 +2696,7 @@ describe( 'selectors', () => {
},
},
controlledInnerBlocks: {},
parents: {},
},
preferences: {
insertUsage: {},
Expand Down Expand Up @@ -2872,6 +2905,7 @@ describe( 'selectors', () => {
},
},
controlledInnerBlocks: {},
parents: {},
},
preferences: {
insertUsage: {},
Expand Down