From 9f04d785bc2055897d186e9287dfc9f6458c3819 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 23 May 2019 07:40:27 -0400 Subject: [PATCH] Block Editor: Avoid default block insertion if no default block type (#15786) * Block Editor: Select previous / next only when exists * Block Editor: Avoid default block insertion if no default block type * Testing: Verify graceful behavior of no-default block deletion --- packages/block-editor/src/store/actions.js | 8 ++++- .../e2e-tests/specs/block-deletion.test.js | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 876083194ef8b7..4dafaa291fcf31 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 ); } diff --git a/packages/e2e-tests/specs/block-deletion.test.js b/packages/e2e-tests/specs/block-deletion.test.js index fb975fb61e88ce..6adcb01681bb2a 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 ); + } ); } );