Skip to content

Commit

Permalink
Inserter: Hide child blocks from the inserter when needed (#67734)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
  • Loading branch information
7 people authored Dec 10, 2024
1 parent 601ede4 commit 8042535
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function BlockTypesTab(
continue;
}

if ( rootClientId && item.isAllowedInCurrentRoot ) {
if ( item.isAllowedInCurrentRoot ) {
itemsForCurrentRoot.push( item );
} else {
itemsRemaining.push( item );
Expand Down
50 changes: 26 additions & 24 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,14 +1586,14 @@ export function getTemplateLock( state, rootClientId ) {
* @param {string|Object} blockNameOrType The block type object, e.g., the response
* from the block directory; or a string name of
* an installed block type, e.g.' core/paragraph'.
* @param {Set} checkedBlocks Set of block names that have already been checked.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {boolean} Whether the given block type is allowed to be inserted.
*/
const isBlockVisibleInTheInserter = (
state,
blockNameOrType,
checkedBlocks = new Set()
rootClientId = null
) => {
let blockType;
let blockName;
Expand Down Expand Up @@ -1621,26 +1621,19 @@ const isBlockVisibleInTheInserter = (
return false;
}

if ( checkedBlocks.has( blockName ) ) {
return false;
}

checkedBlocks.add( blockName );

// If parent blocks are not visible, child blocks should be hidden too.
if ( Array.isArray( blockType.parent ) ) {
return blockType.parent.some(
( name ) =>
( blockName !== name &&
isBlockVisibleInTheInserter(
state,
name,
checkedBlocks
) ) ||
// Exception for blocks with post-content parent,
// the root level is often consider as "core/post-content".
// This exception should only apply to the post editor ideally though.
name === 'core/post-content'
const parents = (
Array.isArray( blockType.parent ) ? blockType.parent : []
).concat( Array.isArray( blockType.ancestor ) ? blockType.ancestor : [] );
if ( parents.length > 0 ) {
const rootBlockName = getBlockName( state, rootClientId );
// This is an exception to the rule that says that all blocks are visible in the inserter.
// Blocks that require a given parent or ancestor are only visible if we're within that parent.
return (
parents.includes( 'core/post-content' ) ||
parents.includes( rootBlockName ) ||
getBlockParentsByBlockName( state, rootClientId, parents ).length >
0
);
}

Expand All @@ -1665,7 +1658,7 @@ const canInsertBlockTypeUnmemoized = (
blockName,
rootClientId = null
) => {
if ( ! isBlockVisibleInTheInserter( state, blockName ) ) {
if ( ! isBlockVisibleInTheInserter( state, blockName, rootClientId ) ) {
return false;
}

Expand Down Expand Up @@ -2072,6 +2065,7 @@ const buildBlockTypeItem =
category: blockType.category,
keywords: blockType.keywords,
parent: blockType.parent,
ancestor: blockType.ancestor,
variations: inserterVariations,
example: blockType.example,
utility: 1, // Deprecated.
Expand Down Expand Up @@ -2169,7 +2163,11 @@ export const getInserterItems = createRegistrySelector( ( select ) =>
} else {
blockTypeInserterItems = blockTypeInserterItems
.filter( ( blockType ) =>
isBlockVisibleInTheInserter( state, blockType )
isBlockVisibleInTheInserter(
state,
blockType,
rootClientId
)
)
.map( ( blockType ) => ( {
...blockType,
Expand Down Expand Up @@ -2501,7 +2499,11 @@ export const __experimentalGetAllowedPatterns = createRegistrySelector(
name,
rootClientId
)
: isBlockVisibleInTheInserter( state, name )
: isBlockVisibleInTheInserter(
state,
name,
rootClientId
)
)
);

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3324,7 +3324,7 @@ describe( 'selectors', () => {
settings: {},
blockEditingModes: new Map(),
};
expect( canInsertBlocks( state, [ '2', '3' ], '1' ) ).toBe( true );
expect( canInsertBlocks( state, [ '2' ], '1' ) ).toBe( true );
} );

it( 'should deny blocks', () => {
Expand Down

1 comment on commit 8042535

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8042535.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12259345705
📝 Reported issues:

Please sign in to comment.