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

Performance: Improve opening inserter in post editor #57006

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,19 @@ _Returns_

- `string|false`: Block Template Lock

### hasAllowedPatterns

Returns whether there is at least one allowed pattern for inner blocks children. This is useful for deferring the parsing of all patterns until needed.

_Parameters_

- _state_ `Object`: Editor state.
- _rootClientId_ `?string`: Optional target root client ID.
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved

_Returns_

- `boolean`: If there is at least one allowed pattern.

### hasBlockMovingClientId

Returns whether block moving mode is enabled.
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ function InserterMenu(
} );
const { showPatterns, inserterItems } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getInserterItems } =
const { hasAllowedPatterns, getInserterItems } =
select( blockEditorStore );
return {
showPatterns: !! __experimentalGetAllowedPatterns(
destinationRootClientId
).length,
showPatterns: hasAllowedPatterns( destinationRootClientId ),
inserterItems: getInserterItems( destinationRootClientId ),
};
},
Expand Down
40 changes: 40 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,46 @@ const getAllAllowedPatterns = createSelector(
]
);

/**
* Returns whether there is at least one allowed pattern for inner blocks children.
* This is useful for deferring the parsing of all patterns until needed.
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional target root client ID.
*
* @return {boolean} If there is at least one allowed pattern.
*/
export const hasAllowedPatterns = createSelector(
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
( state, rootClientId = null ) => {
const patterns = state.settings.__experimentalBlockPatterns;
const userPatterns = getUserPatterns( state );
const { allowedBlockTypes } = getSettings( state );
return [ ...userPatterns, ...patterns ].some(
( { name, inserter = true } ) => {
if ( ! inserter ) {
return false;
}
const { blocks } = __experimentalGetParsedPattern(
Copy link
Member

Choose a reason for hiding this comment

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

I see we're still parsing patterns, but not all of them, but only until .some detects one of them as allowed. Is that the optimization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's it and in most cases we parse just the first pattern.

state,
name
);
return (
checkAllowListRecursive( blocks, allowedBlockTypes ) &&
blocks.every( ( { name: blockName } ) =>
canInsertBlockType( state, blockName, rootClientId )
)
);
}
);
},
( state, rootClientId ) => [
...__experimentalGetAllowedPatterns.getDependants(
state,
rootClientId
),
]
);

/**
* Returns the list of allowed patterns for inner blocks children.
*
Expand Down
Loading