Skip to content

Commit

Permalink
Fix: deleting the last block triggers a focus loss.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 1, 2019
1 parent 77a945c commit b4bbd1c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,16 @@ Returns an action object used in signalling that two blocks should be merged
* firstBlockClientId: Client ID of the first block to merge.
* secondBlockClientId: Client ID of the second block to merge.

### __internalRemoveBlocksPure

Returns action objects used in signalling that the blocks corresponding to
the set of specified client IDs are to be removed.
This action does not trigger any required side effects and it is not recommended for public usage.

*Parameters*

* clientIds: Client IDs of blocks to remove.

### removeBlocks

Yields action objects used in signalling that the blocks corresponding to
Expand Down
40 changes: 35 additions & 5 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getDefaultBlockName, createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { select } from './controls';
import { select, dispatch } from './controls';

/**
* Returns an action object used in signalling that blocks state should be
Expand Down Expand Up @@ -374,6 +374,23 @@ export function mergeBlocks( firstBlockClientId, secondBlockClientId ) {
};
}

/**
* Returns action objects used in signalling that the blocks corresponding to
* the set of specified client IDs are to be removed.
* This action does not trigger any required side effects and it is not recommended for public usage.
*
* @param {string|string[]} clientIds Client IDs of blocks to remove.
*
* @return {Object} Action object.
*
*/
export function __internalRemoveBlocksPure( clientIds ) {
return {
type: 'REMOVE_BLOCKS',
clientIds,
};
}

/**
* Yields action objects used in signalling that the blocks corresponding to
* the set of specified client IDs are to be removed.
Expand All @@ -386,13 +403,26 @@ export function* removeBlocks( clientIds, selectPrevious = true ) {
clientIds = castArray( clientIds );

if ( selectPrevious ) {
yield selectPreviousBlock( clientIds[ 0 ] );
yield dispatch(
'core/block-editor',
'selectPreviousBlock',
clientIds[ 0 ]
);
}

yield {
type: 'REMOVE_BLOCKS',
yield dispatch(
'core/block-editor',
'__internalRemoveBlocksPure',
clientIds,
};
);

const count = yield select(
'core/block-editor',
'getBlockCount',
);
if ( count === 0 ) {
yield insertDefaultBlock();
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/block-editor/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,33 @@ export function select( storeName, selectorName, ...args ) {
};
}

/**
* Dispatches a control action for triggering a registry dispatch.
*
* @param {string} storeKey
* @param {string} actionName
* @param {Array} args Arguments for the dispatch action.
*
* @return {Object} control descriptor.
*/
export function dispatch( storeKey, actionName, ...args ) {
return {
type: 'DISPATCH',
storeKey,
actionName,
args,
};
}

const controls = {
SELECT: createRegistryControl( ( registry ) => ( { storeName, selectorName, args } ) => {
return registry.select( storeName )[ selectorName ]( ...args );
} ),
DISPATCH: createRegistryControl(
( registry ) => ( { storeKey, actionName, args } ) => {
return registry.dispatch( storeKey )[ actionName ]( ...args );
}
),
};

export default controls;
3 changes: 0 additions & 3 deletions packages/block-editor/src/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ export default {
RESET_BLOCKS: [
validateBlocksToTemplate,
],
REMOVE_BLOCKS: [
ensureDefaultBlock,
],
REPLACE_BLOCKS: [
ensureDefaultBlock,
],
Expand Down
72 changes: 56 additions & 16 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
updateBlockAttributes,
updateBlock,
selectBlock,
selectPreviousBlock,
startMultiSelect,
stopMultiSelect,
multiSelect,
Expand All @@ -27,8 +26,11 @@ import {
removeBlock,
toggleBlockMode,
updateBlockListSettings,
__internalRemoveBlocksPure,
} from '../actions';

import { select, dispatch } from '../controls';

describe( 'actions', () => {
describe( 'resetBlocks', () => {
it( 'should return the RESET_BLOCKS actions', () => {
Expand Down Expand Up @@ -205,6 +207,21 @@ describe( 'actions', () => {
} );
} );

describe( '__internalRemoveBlocksPure', () => {
it( 'should return REMOVE_BLOCKS action', () => {
const clientIds = [ 'myclientid' ];

const action = __internalRemoveBlocksPure( clientIds );

expect( action ).toEqual(
{
type: 'REMOVE_BLOCKS',
clientIds,
},
);
} );
} );

describe( 'removeBlocks', () => {
it( 'should return REMOVE_BLOCKS action', () => {
const clientId = 'clientId';
Expand All @@ -213,11 +230,20 @@ describe( 'actions', () => {
const actions = Array.from( removeBlocks( clientIds ) );

expect( actions ).toEqual( [
selectPreviousBlock( clientId ),
{
type: 'REMOVE_BLOCKS',
clientIds,
},
dispatch(
'core/block-editor',
'selectPreviousBlock',
clientId
),
dispatch(
'core/block-editor',
'__internalRemoveBlocksPure',
[ clientId ],
),
select(
'core/block-editor',
'getBlockCount',
),
] );
} );
} );
Expand All @@ -229,24 +255,38 @@ describe( 'actions', () => {
const actions = Array.from( removeBlock( clientId ) );

expect( actions ).toEqual( [
selectPreviousBlock( clientId ),
{
type: 'REMOVE_BLOCKS',
clientIds: [ clientId ],
},
dispatch(
'core/block-editor',
'selectPreviousBlock',
clientId
),
dispatch(
'core/block-editor',
'__internalRemoveBlocksPure',
[ clientId ],
),
select(
'core/block-editor',
'getBlockCount',
),
] );
} );

it( 'should return REMOVE_BLOCKS action, opting out of remove previous', () => {
it( 'should return REMOVE_BLOCKS action, opting out of select previous', () => {
const clientId = 'myclientid';

const actions = Array.from( removeBlock( clientId, false ) );

expect( actions ).toEqual( [
{
type: 'REMOVE_BLOCKS',
clientIds: [ clientId ],
},
dispatch(
'core/block-editor',
'__internalRemoveBlocksPure',
[ clientId ],
),
select(
'core/block-editor',
'getBlockCount',
),
] );
} );
} );
Expand Down

0 comments on commit b4bbd1c

Please sign in to comment.