From e1df80fa59e5684f58609d76d59ab1fb20e0d2f0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 4 Nov 2019 14:48:06 +1300 Subject: [PATCH 1/9] WIP - playing around with options to allow limiting of nextpage block to root level only --- packages/block-editor/src/store/selectors.js | 3 +++ packages/block-library/src/nextpage/index.js | 1 + 2 files changed, 4 insertions(+) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index d2a009b59c6399..7b76bb0d16968f 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1036,6 +1036,9 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) = return list; } if ( isArray( list ) ) { + if ( includes( list, 'root' ) && item === null ) { + return true; + } return includes( list, item ); } return defaultResult; diff --git a/packages/block-library/src/nextpage/index.js b/packages/block-library/src/nextpage/index.js index 9f39f881d83def..b281229d603b9f 100644 --- a/packages/block-library/src/nextpage/index.js +++ b/packages/block-library/src/nextpage/index.js @@ -18,6 +18,7 @@ export { metadata, name }; export const settings = { title: __( 'Page Break' ), + parent: [ 'root' ], description: __( 'Separate your content into a multi-page experience.' ), icon, keywords: [ __( 'next page' ), __( 'pagination' ) ], From c86525f3ae58b8f189dd12e877312f9b8a8f62ef Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 3 Dec 2019 14:41:09 +1300 Subject: [PATCH 2/9] Changes from code review --- packages/block-editor/src/store/selectors.js | 7 ++++++- packages/block-library/src/nextpage/index.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 7b76bb0d16968f..d62e4c08b5a6fc 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1036,7 +1036,12 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) = return list; } if ( isArray( list ) ) { - if ( includes( list, 'root' ) && item === null ) { + // TODO: when there is a canonical way to detect that we are editing a post + // the following check should be changed to something like: + // if ( includes( list, 'core/post-content' ) && + // ( item === 'core/post-content' || (getEditorMode() === 'post-content' && item === null) + // ) + if ( includes( list, 'core/post-content' ) && item === null ) { return true; } return includes( list, item ); diff --git a/packages/block-library/src/nextpage/index.js b/packages/block-library/src/nextpage/index.js index b281229d603b9f..00dedee31d7ff6 100644 --- a/packages/block-library/src/nextpage/index.js +++ b/packages/block-library/src/nextpage/index.js @@ -18,7 +18,7 @@ export { metadata, name }; export const settings = { title: __( 'Page Break' ), - parent: [ 'root' ], + parent: [ 'core/post-content' ], description: __( 'Separate your content into a multi-page experience.' ), icon, keywords: [ __( 'next page' ), __( 'pagination' ) ], From 084e2774d5dd82535ed18360b7b1b2ce24cdac4d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 5 Dec 2019 10:29:37 +1300 Subject: [PATCH 3/9] Update todo comment --- packages/block-editor/src/store/selectors.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index d62e4c08b5a6fc..6e33f6483a3051 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1038,9 +1038,7 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) = if ( isArray( list ) ) { // TODO: when there is a canonical way to detect that we are editing a post // the following check should be changed to something like: - // if ( includes( list, 'core/post-content' ) && - // ( item === 'core/post-content' || (getEditorMode() === 'post-content' && item === null) - // ) + // if (includes( list, 'core/post-content' ) && getEditorMode() === 'post-content' && item === null ) if ( includes( list, 'core/post-content' ) && item === null ) { return true; } From 16e78fca2622964460e17ccd0f1847672fed6165 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 10 Dec 2019 06:59:42 +1300 Subject: [PATCH 4/9] Add unit test --- .../block-editor/src/store/test/selectors.js | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 6000c76b824f0e..bca154d27e6b53 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -121,6 +121,15 @@ describe( 'selectors', () => { }, } ); + registerBlockType( 'core/post-content-child', { + save: ( props ) => props.attributes.text, + category: 'common', + title: 'Test Block Post Content Child', + icon: 'test', + keywords: [ 'testing' ], + parent: [ 'core/post-content' ], + } ); + setFreeformContentHandlerName( 'core/test-freeform' ); cachedSelectors.forEach( ( { clear } ) => clear() ); @@ -132,7 +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 ); } ); @@ -1933,6 +1942,36 @@ 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: { + block1: {}, + }, + }, + blockListSettings: {}, + settings: {}, + }; + expect( canInsertBlockType( state, 'core/post-content-child' ) ).toBe( true ); + } ); } ); describe( 'getInserterItems', () => { @@ -2033,6 +2072,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', From 8a2bdf55c066a718688ecc467880ac66cff48af8 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 9 Dec 2019 13:49:10 -0800 Subject: [PATCH 5/9] Update packages/block-editor/src/store/selectors.js Co-Authored-By: Enrique Piqueras --- packages/block-editor/src/store/selectors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 6e33f6483a3051..a5d5050a5b2b7d 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1038,7 +1038,7 @@ const canInsertBlockTypeUnmemoized = ( state, blockName, rootClientId = null ) = if ( isArray( list ) ) { // TODO: when there is a canonical way to detect that we are editing a post // 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' ) && getEditorMode() === 'post-content' && item === null ) if ( includes( list, 'core/post-content' ) && item === null ) { return true; } From 82e21314b6fdc3c48335a75d5699e4a8d2622d6b Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 9 Dec 2019 13:49:52 -0800 Subject: [PATCH 6/9] Fix linting issue Co-Authored-By: Enrique Piqueras --- packages/block-editor/src/store/test/selectors.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index bca154d27e6b53..4620d35f210ed7 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -142,6 +142,7 @@ describe( 'selectors', () => { unregisterBlockType( 'core/test-block-c' ); unregisterBlockType( 'core/test-freeform' ); unregisterBlockType( 'core/post-content-child' ); + setFreeformContentHandlerName( undefined ); } ); From 6a9f6566fb49278ded955a51b1aa989355579622 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 9 Dec 2019 13:50:08 -0800 Subject: [PATCH 7/9] Fix linting issue Co-Authored-By: Enrique Piqueras --- packages/block-editor/src/store/test/selectors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 4620d35f210ed7..8a10d382e81b0e 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -1944,7 +1944,7 @@ 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 ', () => { + it( 'should deny blocks that restrict parent to core/post-content when not in editor root', () => { const state = { blocks: { byClientId: { From bdc4fec537f23ffa152f23f39d6632f4b5d8482f Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 9 Dec 2019 13:50:23 -0800 Subject: [PATCH 8/9] Fix linting issue Co-Authored-By: Enrique Piqueras --- packages/block-editor/src/store/test/selectors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 8a10d382e81b0e..de0407573dc6c2 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -1960,7 +1960,7 @@ describe( 'selectors', () => { 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 ', () => { + it( 'should allow blocks that restrict parent to core/post-content when in editor root', () => { const state = { blocks: { byClientId: {}, From febb4264e7d7a1d01e5d9e855a61aca193172ccc Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 10 Dec 2019 10:55:06 +1300 Subject: [PATCH 9/9] Changes from review --- packages/block-editor/src/store/test/selectors.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index de0407573dc6c2..0d0785f6fd3522 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -122,7 +122,7 @@ describe( 'selectors', () => { } ); registerBlockType( 'core/post-content-child', { - save: ( props ) => props.attributes.text, + save: () => null, category: 'common', title: 'Test Block Post Content Child', icon: 'test', @@ -1964,9 +1964,7 @@ describe( 'selectors', () => { const state = { blocks: { byClientId: {}, - attributes: { - block1: {}, - }, + attributes: {}, }, blockListSettings: {}, settings: {},