-
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
Limit of Next Page (Page Break) block to root level only #18260
Limit of Next Page (Page Break) block to root level only #18260
Conversation
… to root level only
This seems to work, and doesn't break any existing tests, but I am sure I am overlooking some gotchas, or an easier existing way to limit a block to the root context. |
I think this is a valid approach! Simple and easy 🙂 |
Works really well 👍 |
@@ -1036,6 +1036,9 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) = | |||
return list; | |||
} | |||
if ( isArray( list ) ) { | |||
if ( includes( list, 'root' ) && item === null ) { |
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.
How would it work if it's not a post, in case of full site editing? Maybe not something we should fix now, but maybe good to have in mind. Cc @epiqueras.
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.
It would only let you insert it at the root level of the template.
I guess we would need to add core/post-content
to the list of allowed parents? Along with any other template layout blocks that should be able to insert a page break.
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 also add a list of parents that should be considered as a 'root' container at this point, rather than having to add them to each blocks allowed parents? eg.
const rootContainers = [ null, 'core/post-content ];
if ( includes( list, 'root' ) && includes( rootContainers , item ) )
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.
Sure, but won't the page break still break if its rendered in a Post Content block? Or does it only break in Column blocks?
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 just used Post Content block as an example because you had mentioned it - the array would just contain any parent blocks that we knew should be treated like a root element for the likes of page breaks.
It may be that it is not possible to identify what constitutes a 'root' block for a range of different child blocks, in which case it is probably best to leave 'root' purely as the editor root with if ( includes( list, 'root' ) && item === null )
, and add any other possible parent blocks to the blocks own allowed parents array.
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.
It may be that it is not possible to identify what constitutes a 'root' block for a range of different child blocks, in which case it is probably best to leave 'root' purely as the editor root with if ( includes( list, 'root' ) && item === null ), and add any other possible parent blocks to the blocks own allowed parents array.
A "root" block shouldn't be defined in relationship to a range of child blocks. A "root" block is simply a block that serves as the root of the block tree in some rendering context. For example, a Post Content block serves as the root of the block tree when rendering a single post, but doesn't when rendering an entire template. So I think we are safe with including Post Content in our definition of "root".
I just used Post Content block as an example because you had mentioned it - the array would just contain any parent blocks that we knew should be treated like a root element for the likes of page breaks.
Yeah, I understood that. I was just wondering if the Page Break block will cause issues as a child of the Post Content block. I think it should be fine.
Now that I think more about this. The Page Break block only makes sense as a top level block in post content. If we whitelist this using "root", you would be able to add them to templates and template parts which wouldn't make any sense or work.
To make this work, the selector needs to check that the block is being inserted at the root level and that we are editing a post, or that it's being inserted into a Post Content block.
that we are editing a post
We don't have a canonical way of doing this yet so I think that the way to move forward is using
parent: [ 'core/post-content' ]
and
if ( includes( list, 'core/post-content' ) && item === null ) {
for now. With a todo note to change it to
if (
includes( list, 'core/post-content' ) &&
(
item === 'core/post-content' ||
(getEditorMode() === 'post-content' && item === null)
) {
when possible.
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 approach makes sense to me - have made this change, but haven't had time to test it properly - will do that tomorrow hopefully.
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.
Looks good to me.
We'll actually only need to change it to includes( list, 'core/post-content' ) && getEditorMode() === 'post-content' && item === null
, because the following return includes( list, item );
will take care of the other case where item === 'core/post-content'
.
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.
Have updated the TODO comment. Will try and get a unit test added in the next day or so.
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.
@epiqueras, finally got back to this, have added a basic unit test to cover this.
blocks: { | ||
byClientId: {}, | ||
attributes: { | ||
block1: {}, |
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 unnecessary.
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.
fixed
@@ -121,6 +121,15 @@ describe( 'selectors', () => { | |||
}, | |||
} ); | |||
|
|||
registerBlockType( 'core/post-content-child', { | |||
save: ( props ) => props.attributes.text, |
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 block doesn't have attributes
.
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.
fixed
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
@epiqueras - apologies for the basic formatting issues, my local linting doesn't seem to be working and hadn't noticed. |
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.
Thanks!
@glendaviesnz @youknowriad I tested this behavior with WP 6.2 and Gutenberg 15.5.0 and it no longer works. If the block has " I have other custom blocks that are restricted to the root level of the editor, but the options to group these blocks are available. But that shouldn't be possible. It would be nice if it works as expected again. |
@CreativeDive would you mind creating an issue about it, so we don't forget about it. |
Created a new issue #49662 |
Thanks 👍 |
Description
Adds a new 'root' parent block type which allows a block to be limited to only insertion in the root context. This is initially as a suggested fix for a bug with the Page Break block ( #16411 ) but could be documented as a way to limit any block to the root context.
How has this been tested
Just tested manually at this stage. If it is agreed that this is a possible way forward I will look at adding some unit or e2e tests.
To test
Screenshots