Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Avoid default block insertion if no default block type #15786

Merged
merged 3 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export function* selectPreviousBlock( clientId ) {
clientId
);

yield selectBlock( previousBlockClientId, -1 );
if ( previousBlockClientId ) {
yield selectBlock( previousBlockClientId, -1 );
}
}

/**
Expand All @@ -147,7 +149,9 @@ export function* selectNextBlock( clientId ) {
clientId
);

yield selectBlock( nextBlockClientId );
if ( nextBlockClientId ) {
yield selectBlock( nextBlockClientId );
}
}

/**
Expand Down Expand Up @@ -633,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 );
} );
} );