Skip to content

Commit

Permalink
Block Editor: Avoid default block insertion if no default block type (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
aduth authored and gziolo committed May 23, 2019
1 parent 6b024af commit 9f04d78
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
31 changes: 31 additions & 0 deletions packages/e2e-tests/specs/block-deletion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createNewPost,
isInDefaultBlock,
pressKeyWithModifier,
insertBlock,
} from '@wordpress/e2e-test-utils';

const addThreeParagraphsToNewPost = async () => {
Expand Down Expand Up @@ -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 );
} );
} );

0 comments on commit 9f04d78

Please sign in to comment.