-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 Bindings: Only pass context included in usesContext
from rich text component
#65618
Block Bindings: Only pass context included in usesContext
from rich text component
#65618
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +30 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
It's better to start this way. |
I was considering three options for the rich text:
|
@@ -179,11 +179,17 @@ export function RichTextWrapper( | |||
const blockBindingsSource = getBlockBindingsSource( | |||
relatedBinding.source | |||
); | |||
const blockBindingsContext = {}; |
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 wanted to highlight the differences I discovered with how the code feeds other API methods with the context.
const updatedContext = { ...context }; |
gutenberg/packages/block-editor/src/hooks/use-bindings-attributes.js
Lines 139 to 142 in b681b50
// Populate context. | |
for ( const key of source.usesContext || [] ) { | |
updatedContext[ key ] = blockContext[ key ]; | |
} |
gutenberg/packages/block-editor/src/hooks/use-bindings-attributes.js
Lines 162 to 167 in b681b50
values = source.getValues( { | |
registry, | |
context: updatedContext, | |
clientId, | |
bindings, | |
} ); |
gutenberg/packages/block-editor/src/hooks/use-bindings-attributes.js
Lines 238 to 248 in b681b50
for ( const [ | |
source, | |
bindings, | |
] of blockBindingsBySource ) { | |
source.setValues( { | |
registry, | |
context: updatedContext, | |
clientId, | |
bindings, | |
} ); | |
} |
While updatedContext
is correct in that code as it eventually gets passed to the <Edit />
component, I don't think we should use updatedContext
when passing context to setValues
and getValues
. Instead, we should replicate the handling proposed in this PR. In effect, useSelect
defined for the source on the server and/or client would be the ultimate contract of what these API methods can get as params.
I would proceed with this PR as planned. If we agree the same changes should get applied in other places seperately, then there should be two subsets of the context:
blockBindingsContext
like in this PR that gets passed to the methods registered for the sourceupdatedContext
would be refactored to:const updatedContext = { ...context, ... blockBindingsContext };
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.
Unifying and using only the context defined in usesContext
for setValues
and getValues
makes sense to me. I can work on that in a follow-up.
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'm exploring changing this in this pull request. I believe just changing how updatedContext
is initialized should be enough.
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 the BlockContext to pass only the properties included in source.usesContext: This is what this PR is doing.
I arrived at the same conclusion. The API support usesContext
on both the server and the client so we should treat that as a source of truth and align with the same behavior for block types. In effect, only the context defined during registration gets exposed. More about that in https://github.com/WordPress/gutenberg/pull/65618/files#r1775077099.
- Only pass context included in `usesContext` Co-authored-by: SantosGuillamot <[email protected]> Co-authored-by: gziolo <[email protected]>
I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: 31de263 |
What?
It passes only the context properties included in
usesContext
block bindings definition instead of the wholeblockContext
.Why?
It seems safer to limit the context provided to the block bindings editor APIs to only the values defined through
usesContext
property.How?
I just added a similar check to what we have in other places like this.
I tried getting the BlockEdit context from the rich text, but I don't think that's possible.
Testing Instructions
Everything should keep working as expected. e2e tests should pass.