Skip to content

Commit

Permalink
Limit of Next Page (Page Break) block to root level only (#18260)
Browse files Browse the repository at this point in the history
* WIP - playing around with options to allow limiting of nextpage block to root level only

* Changes from code review

* Update todo comment

* Add unit test

* Update packages/block-editor/src/store/selectors.js

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>

* Fix linting issue

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>

* Fix linting issue

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>

* Fix linting issue

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>

* Changes from review
  • Loading branch information
glendaviesnz authored and epiqueras committed Dec 9, 2019
1 parent 383248a commit 2ebfb64
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,12 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) =
return list;
}
if ( isArray( list ) ) {
// TODO: when there is a canonical way to detect that we are editing a post

This comment has been minimized.

Copy link
@gziolo

gziolo Dec 9, 2019

Member

I guess it will be possible to enforce the parent through the block definition at that point.

This comment has been minimized.

Copy link
@epiqueras

epiqueras Dec 9, 2019

Contributor

Isn't it already possible?

// if ( includes( list, 'core/post-content' ) && getEditorMode() === 'post-content' && item === null )

getEditorMode() === 'post-content', basically says, only treat item === null as core/post-content if we are in post content mode.

// the following check should be changed to something like:
// if ( includes( list, 'core/post-content' ) && getEditorMode() === 'post-content' && item === null )
if ( includes( list, 'core/post-content' ) && item === null ) {
return true;
}
return includes( list, item );
}
return defaultResult;
Expand Down
39 changes: 39 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ describe( 'selectors', () => {
},
} );

registerBlockType( 'core/post-content-child', {
save: () => null,
category: 'common',
title: 'Test Block Post Content Child',
icon: 'test',
keywords: [ 'testing' ],
parent: [ 'core/post-content' ],
} );

setFreeformContentHandlerName( 'core/test-freeform' );

cachedSelectors.forEach( ( { clear } ) => clear() );
Expand All @@ -132,6 +141,7 @@ describe( 'selectors', () => {
unregisterBlockType( 'core/test-block-b' );
unregisterBlockType( 'core/test-block-c' );
unregisterBlockType( 'core/test-freeform' );
unregisterBlockType( 'core/post-content-child' );

setFreeformContentHandlerName( undefined );
} );
Expand Down Expand Up @@ -1933,6 +1943,34 @@ describe( 'selectors', () => {
};
expect( canInsertBlockType( state, 'core/test-block-c', 'block1' ) ).toBe( true );
} );

it( 'should deny blocks that restrict parent to core/post-content when not in editor root', () => {
const state = {
blocks: {
byClientId: {
block1: { name: 'core/test-block-c' },
},
attributes: {
block1: {},
},
},
blockListSettings: {},
settings: {},
};
expect( canInsertBlockType( state, 'core/post-content-child', 'block1' ) ).toBe( false );
} );

it( 'should allow blocks that restrict parent to core/post-content when in editor root', () => {
const state = {
blocks: {
byClientId: {},
attributes: {},
},
blockListSettings: {},
settings: {},
};
expect( canInsertBlockType( state, 'core/post-content-child' ) ).toBe( true );
} );
} );

describe( 'getInserterItems', () => {
Expand Down Expand Up @@ -2033,6 +2071,7 @@ describe( 'selectors', () => {
};
const itemIDs = getInserterItems( state ).map( ( item ) => item.id );
expect( itemIDs ).toEqual( [
'core/post-content-child',
'core/block/2',
'core/block/1',
'core/test-block-b',
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/nextpage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { metadata, name };

export const settings = {
title: __( 'Page Break' ),
parent: [ 'core/post-content' ],
description: __( 'Separate your content into a multi-page experience.' ),
icon,
keywords: [ __( 'next page' ), __( 'pagination' ) ],
Expand Down

0 comments on commit 2ebfb64

Please sign in to comment.