-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Try: Custom block insertion hooks #45098
Open
joshuatf
wants to merge
4
commits into
try/example-block-hooks
Choose a base branch
from
try/block-insertion-hooks
base: try/example-block-hooks
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−115
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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.
This needs further investigation to make sure this won't break things. I suspect this could be problematic depending on existing chunks in
innerContent
.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 think you need to prepend/append the hooked block to
innerBlocks
rather thaninnerContent
:Additionally, you also need to insert a
null
in the corresponding position ininnerContent
, so it should actually readThe way it works is that
innerContent
can have items that are either HTML strings, ornull
to mark a position where an item frominnerBlocks
needs to be interpolated. E.g.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.
This is still not the full picture BTW if you want to account for some subtleties: As the Group block example demonstrates, you might not actually want to put the
null
marker forfirst_child
in the very first position ininnerContent
(or the one forlast_child
in the very last one), as you might want to account for the wrapping HTML:Instead, you'll likely want to find the first (last) non-HTML location in
innerContent
. For the original Block Hooks implementation (in GB's WP 6.4 compat shim), we ended up with the following logic: https://github.com/WordPress/gutenberg/blob/98552b2486b52e4dd3c90775c2a029b95e64253c/lib/compat/wordpress-6.4/block-hooks.php#L157-L182