-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add __experimentalGetGlobalBlocksByName selector #39610
Conversation
Size Change: +31 B (0%) Total Size: 1.21 MB
ℹ️ View Unchanged
|
return EMPTY_ARRAY; | ||
} | ||
const clientIds = getClientIdsWithDescendants( state ); | ||
const foundBlocks = clientIds.filter( ( clientId ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just return the result of the filter? Do we need to use the EMPTY_ARRAY
const?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filter will return a new array reference. Thus we use the reference to a constant object reference EMPTY_ARRAY
so as to avoid returning a new object reference if the selector returns "nothing". It's a minor perf thing but it could make a big difference in certain components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see Kerry even wrote a test to validate that. See the final it()
in the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What is it that makes |
@getdave Thanks for adding this. It has allowed me to optimize a little bit of the logic in #29739. @draganescu The term is already used by |
What?
This PR adds a new selector:
__experimentalGetGlobalBlocksByName
.Why?
This is a sub PR spun out of #39290.
This selector returns all global blocks that match a given blockName. It also includes nested blocks.
This is required by #39290 to quickly get a list (including nested blocks) of any Navigation blocks currently within the current post/template.
How?
This selector is added to the
@wordpresss/block-editor
package.Testing Instructions
Console
drawer on any Editor page.wp.data.select('core/block-editor').__experimentalGetGlobalBlocksByName(%%YOUR BLOCK NAME HERE%%)
replacing the placeholder with a block you know is in the current post/template (egcore/paragraph
).Screenshots or screencast