-
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
InnerBlocks: Support insert before/after block actions when using allowedBlocks #59162
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @mapk, @gwwar. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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: +132 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
Flaky tests detected in 9010ab0. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7970075184
|
@Mamaduka I think it will also close this one - Make the 'Insert Before' / 'Insert After' options work for block lists that don't allow the default block type |
@talldan, the Social Icons block will need an update to display a placeholder state when no variation is defined, similar to the Groups block. That can be done in a follow-up, so it shouldn't be a blocker for this PR. |
Thanks @Mamaduka! I tested the examples @richtabor identified in #56228. With this PR I can now see an "Add before" and "Add after" button when selecting an item in these blocks:
But not these blocks:
Are we able to handle those as well? |
@noisysocks, yes, most of those blocks just need to update the inner block setting definitions. I've prepared a PR for the Gallery block - #59168 and will update others as well. The Social Icons block will need more adjustments on the block level. |
bf7dee9
to
425e5e0
Compare
425e5e0
to
9010ab0
Compare
const canInsertDefaultBlock = canInsertBlockType( | ||
getDefaultBlockName(), | ||
rootClientId | ||
const selected = useSelect( |
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.
Why store in selected
instead of destructuring right 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.
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.
😮 omg that's life changing
); | ||
const { getBlocksByClientId, getBlocks } = useSelect( blockEditorStore ); |
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 approach is much nicer 🙂
...directInsertBlock.attributes, | ||
...copiedAttributes, | ||
} ); | ||
return dispatch.insertBlock( block, blockIndex + 1, rootClientId ); |
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.
Can we make insertBeforeBlock
and insertAfterBlock
both call into e.g. insertDefaultBlock( clientId: string, position: 'before' | 'after' )
or insertDefaultBlock( clientId: string, offset: 1 | -1 )
to reduce duplication? There's quite a lot of repetition now that both actions have more logic.
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.
So, move most of the logic into the insertDefaultBlock
action, correct?
The only issue I see here is that insertDefaultBlock
is more widely used where consumers rely on it to insert an empty paragraph. Example:
gutenberg/packages/editor/src/components/post-title/index.js
Lines 77 to 79 in 09dc127
function onEnterPress() { | |
insertDefaultBlock( undefined, undefined, 0 ); | |
} |
P.S. It's definitely doable, but I am unsure if removing 20+ lines is worth complicating the logic of the existing insertDefaultBlock
action.
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.
Sorry I meant a new selector not insertDefaultBlock
. Not sure what to to name it 😀
Up to you. I think it's worth it now that the logic isn't trivial, but respect your judgement.
@@ -75,11 +88,9 @@ export default function BlockActions( { | |||
return children( { | |||
canCopyStyles, | |||
canDuplicate, | |||
canInsertDefaultBlock, | |||
canInsertBlock, |
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.
Just confirming that BlockActions
isn't a public API, yeah?
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.
Correct. It's not a public API.
Thanks everyone for the feedback. |
It looks like some block bindings changes for 6.5 rely on the changes of this PR to work properly, so I'm going to include this in 6.5. Let me know if there's any concern with that. |
* Don't call selectors during the render; subscribe to the store instead * Get blocks on demand * Update 'insertBeforeBlock' and 'insertAfterBlock' actions * A handler 'attributesToCopy' definitions Unlinked contributors: mapk, gwwar. Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: noisysocks <noisysocks@git.wordpress.org> Co-authored-by: talldan <talldanwp@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: shaunandrews <shaunandrews@git.wordpress.org> Co-authored-by: richtabor <richtabor@git.wordpress.org>
I just cherry-picked this PR to the update/packages-6.5-rc1 branch to get it included in the next release: ebdf83d |
* Don't call selectors during the render; subscribe to the store instead * Get blocks on demand * Update 'insertBeforeBlock' and 'insertAfterBlock' actions * A handler 'attributesToCopy' definitions Unlinked contributors: mapk, gwwar. Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: noisysocks <noisysocks@git.wordpress.org> Co-authored-by: talldan <talldanwp@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: shaunandrews <shaunandrews@git.wordpress.org> Co-authored-by: richtabor <richtabor@git.wordpress.org>
What?
Resolves #23603.
Resolves #56228.
PR enables "Add before/after" block actions for InnerBlocks with
allowedBlocks
when the default block is specified.Note
Currently, this doesn't handle the cases when the
allowedBlocks
contains a single block type.Why?
See #56228.
How?
BlockActions
to use store subscription instead of calling selectors during the render to avoid stale values.blocks
value inside the callbacks. Prevents block settings from re-rendering when the Top Toolbar is used.insertBeforeBlock
andinsertAfterBlock
actions to handle thedirectInsertBlock
setting.P.S. Happy to extract general refactoring comments into a new PR if that would make reviewing easier.
Todos
directInsertBlock.attributesToCopy
setting. See bf7dee9.Testing Instructions
Testing Instructions for Keyboard
Same.
Screenshots or screencast
CleanShot.2024-02-19.at.09.09.17.mp4