Skip to content
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

Merged

Conversation

glendaviesnz
Copy link
Contributor

@glendaviesnz glendaviesnz commented Nov 4, 2019

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

  • Check out this branch
  • Add a Page Break block to the editor root - should work as normal
  • Try adding a Page Break block to a block that allows nesting like Columns - it should not appear as an available block to add

Screenshots

pagebreak

@glendaviesnz glendaviesnz changed the title WIP - Limit of next page block to root level only Limit of Next Page (Page Break) block to root level only Nov 11, 2019
@glendaviesnz glendaviesnz marked this pull request as ready for review November 11, 2019 08:58
@glendaviesnz
Copy link
Contributor Author

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.

@mmtr
Copy link
Contributor

mmtr commented Nov 11, 2019

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 🙂

@kwight
Copy link

kwight commented Nov 11, 2019

Works really well 👍

@@ -1036,6 +1036,9 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) =
return list;
}
if ( isArray( list ) ) {
if ( includes( list, 'root' ) && item === null ) {
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 )  )

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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'.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@glendaviesnz glendaviesnz added [Type] Bug An existing feature does not function as intended [Block] More Affects the More Block - used for displaying the 'Read More' link [Block] Page Break Affects the Page Break Block labels Dec 3, 2019
@glendaviesnz glendaviesnz added the [Feature] Block API API that allows to express the block paradigm. label Dec 3, 2019
packages/block-editor/src/store/selectors.js Outdated Show resolved Hide resolved
packages/block-editor/src/store/test/selectors.js Outdated Show resolved Hide resolved
packages/block-editor/src/store/test/selectors.js Outdated Show resolved Hide resolved
blocks: {
byClientId: {},
attributes: {
block1: {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary.

Copy link
Contributor Author

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,
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

glendaviesnz and others added 5 commits December 9, 2019 13:49
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>
@glendaviesnz
Copy link
Contributor Author

@epiqueras - apologies for the basic formatting issues, my local linting doesn't seem to be working and hadn't noticed.

Copy link
Contributor

@epiqueras epiqueras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@epiqueras epiqueras merged commit 2ebfb64 into WordPress:master Dec 9, 2019
@glendaviesnz glendaviesnz deleted the fix/page-break-as-child-bug branch December 9, 2019 23:18
@youknowriad youknowriad added this to the Gutenberg 7.2 milestone Jan 6, 2020
@CreativeDive
Copy link
Contributor

@glendaviesnz @youknowriad I tested this behavior with WP 6.2 and Gutenberg 15.5.0 and it no longer works. If the block has "core/post-content" set in the "parent" attribute in block.json, the block can be grouped. But that shouldn't be possible.

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.

@youknowriad
Copy link
Contributor

@CreativeDive would you mind creating an issue about it, so we don't forget about it.

@CreativeDive
Copy link
Contributor

Created a new issue #49662

@youknowriad
Copy link
Contributor

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] More Affects the More Block - used for displaying the 'Read More' link [Block] Page Break Affects the Page Break Block [Feature] Block API API that allows to express the block paradigm. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants