From a3f1884560a17db8c2fb1114c737bd803aec2e44 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Tue, 21 May 2019 18:10:02 -0400 Subject: [PATCH 1/3] Block Editor: Select previous / next only when exists --- packages/block-editor/src/store/actions.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 466a9cbeb96c2..876083194ef8b 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -131,7 +131,9 @@ export function* selectPreviousBlock( clientId ) { clientId ); - yield selectBlock( previousBlockClientId, -1 ); + if ( previousBlockClientId ) { + yield selectBlock( previousBlockClientId, -1 ); + } } /** @@ -147,7 +149,9 @@ export function* selectNextBlock( clientId ) { clientId ); - yield selectBlock( nextBlockClientId ); + if ( nextBlockClientId ) { + yield selectBlock( nextBlockClientId ); + } } /** From c389300c2c45d2d9ba8be61cd8f841f548901faf Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 22 May 2019 16:59:03 -0400 Subject: [PATCH 2/3] Block Editor: Avoid default block insertion if no default block type --- packages/block-editor/src/store/actions.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 876083194ef8b..4dafaa291fcf3 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -637,7 +637,13 @@ export function selectionChange( clientId, attributeKey, startOffset, endOffset * @return {Object} Action object */ export function insertDefaultBlock( attributes, rootClientId, index ) { - const block = createBlock( getDefaultBlockName(), attributes ); + // Abort if there is no default block type (if it has been unregistered). + const defaultBlockName = getDefaultBlockName(); + if ( ! defaultBlockName ) { + return; + } + + const block = createBlock( defaultBlockName, attributes ); return insertBlock( block, index, rootClientId ); } From d9abf6c5d524c1bc1724bbd3cdbcde3300f28c6a Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 22 May 2019 16:59:29 -0400 Subject: [PATCH 3/3] Testing: Verify graceful behavior of no-default block deletion --- .../e2e-tests/specs/block-deletion.test.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/e2e-tests/specs/block-deletion.test.js b/packages/e2e-tests/specs/block-deletion.test.js index fb975fb61e88c..6adcb01681bb2 100644 --- a/packages/e2e-tests/specs/block-deletion.test.js +++ b/packages/e2e-tests/specs/block-deletion.test.js @@ -7,6 +7,7 @@ import { createNewPost, isInDefaultBlock, pressKeyWithModifier, + insertBlock, } from '@wordpress/e2e-test-utils'; const addThreeParagraphsToNewPost = async () => { @@ -130,4 +131,34 @@ describe( 'deleting all blocks', () => { // And focus is retained: expect( await isInDefaultBlock() ).toBe( true ); } ); + + it( 'gracefully removes blocks when the default block is not available', async () => { + // Regression Test: Previously, removing a block would not clear + // selection state when there were no other blocks to select. + // + // See: https://github.com/WordPress/gutenberg/issues/15458 + // See: https://github.com/WordPress/gutenberg/pull/15543 + await createNewPost(); + + // Unregister default block type. This may happen if the editor is + // configured to not allow the default (paragraph) block type, either + // by plugin editor settings filtering or user block preferences. + await page.evaluate( () => { + const defaultBlockName = wp.data.select( 'core/blocks' ).getDefaultBlockName(); + wp.data.dispatch( 'core/blocks' ).removeBlockTypes( defaultBlockName ); + } ); + + // Add and remove a block. + await insertBlock( 'Image' ); + await page.keyboard.press( 'Backspace' ); + + // Verify there is no selected block. + // TODO: There should be expectations around where focus is placed in + // this scenario. Currently, a focus loss occurs (not acceptable). + const selectedBlocksCount = await page.evaluate( () => { + return wp.data.select( 'core/block-editor' ).getSelectedBlockClientIds().length; + } ); + + expect( selectedBlocksCount ).toBe( 0 ); + } ); } );