-
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
Block bindings: don't use hooks #60724
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreDataStore } from '@wordpress/core-data'; | ||
import { _x } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
@@ -12,33 +12,17 @@ import { store as editorStore } from '../store'; | |
export default { | ||
name: 'core/post-meta', | ||
label: _x( 'Post Meta', 'block bindings source' ), | ||
useSource( props, sourceAttributes ) { | ||
const { getCurrentPostType } = useSelect( editorStore ); | ||
const { context } = props; | ||
const { key: metaKey } = sourceAttributes; | ||
getPlaceholder( { args } ) { | ||
return args.key; | ||
}, | ||
getValue( { registry, context, args } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the backend, we have |
||
const postType = context.postType | ||
? context.postType | ||
: getCurrentPostType(); | ||
|
||
const [ meta, setMeta ] = useEntityProp( | ||
'postType', | ||
context.postType, | ||
'meta', | ||
context.postId | ||
); | ||
|
||
if ( postType === 'wp_template' ) { | ||
return { placeholder: metaKey }; | ||
} | ||
const metaValue = meta[ metaKey ]; | ||
const updateMetaValue = ( newValue ) => { | ||
setMeta( { ...meta, [ metaKey ]: newValue } ); | ||
}; | ||
: registry.select( editorStore ).getCurrentPostType(); | ||
|
||
return { | ||
placeholder: metaKey, | ||
value: metaValue, | ||
updateValue: updateMetaValue, | ||
}; | ||
return registry | ||
.select( coreDataStore ) | ||
.getEditedEntityRecord( 'postType', postType, context.postId ) | ||
.meta?.[ args.key ]; | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if you have already been aware of this, but one reason we initially implemented Pattern Overrides differently is because patching
setAttributes
might not work in some edge cases.There are different ways to update a block's attributes. Calling
setAttributes
from the<BlockEdit>
's props is just one of those ways. You can also callupdateBlockAttributes
directly from theblock-editor
store,updateBlock
orreplaceBlock
to change the whole block, or even areplaceInnerBlocks
andcloneBlock
combo for whatever reasons.We can make sure all core blocks favor
setAttributes
from the<BlockEdit>
's props whenever possible, but as we allow third-party blocks support, then it's probably going to be a problem.A current example of this in the core block library would be the
core/table-of-contents
block. As you update the heading block elsewhere, then the ToC block will be updated usingupdateBlockAttributes
.The way we implemented in Pattern Overrides currently is the other way around. Instead of patching anything or "capturing" anything from the block updates, we "listen" to the changes that happened in the block and reflect the changes to the pattern instance (
content
attribute.) Everything that happens in the block is kept intact, so we don't have to patch anything. Hence,syncDerivedUpdates
is used to "sync" the block's "updates" to the pattern instance. Actions wrapped in that action callback shouldn't create an undo level as it's merely syncing derived states between different blocks.(Just for the info for #60721, c.c. @SantosGuillamot)
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.
That's a good point but it's not an issue that is specific to pattern overrides. All block bindings have the same issue/impact here.
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 aware of it and this is actually something that not using hooks would allow us to fix. But I remember @SantosGuillamot saying it was not desirable to fix, I can't recall correctly. Please correct me if I'm wrong 🙂
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 agree it is a good point and something we should aim to fix.
Mmm, I don't remember exactly. Maybe we were discussing the possibility of not using the hook and moving it to the core directly, and I mentioned that it shouldn't be a blocker for WordPress 6.6. But I totally agree that should probably be the path to go.