Skip to content

Commit

Permalink
Block Editor: Yield remove actions directly
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 1, 2019
1 parent 94f8c9d commit 37bcda9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -958,16 +958,6 @@ 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
33 changes: 6 additions & 27 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, dispatch } from './controls';
import { select } from './controls';

/**
* Returns an action object used in signalling that blocks state should be
Expand Down Expand Up @@ -374,23 +374,6 @@ 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 @@ -403,23 +386,19 @@ export function* removeBlocks( clientIds, selectPrevious = true ) {
clientIds = castArray( clientIds );

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

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

const count = yield select(
'core/block-editor',
'getBlockCount',
);

if ( count === 0 ) {
yield insertDefaultBlock();
}
Expand Down
23 changes: 0 additions & 23 deletions packages/block-editor/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,10 @@ 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;
58 changes: 16 additions & 42 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
updateBlockAttributes,
updateBlock,
selectBlock,
selectPreviousBlock,
startMultiSelect,
stopMultiSelect,
multiSelect,
Expand All @@ -26,9 +27,8 @@ import {
removeBlock,
toggleBlockMode,
updateBlockListSettings,
__internalRemoveBlocksPure,
} from '../actions';
import { select, dispatch } from '../controls';
import { select } from '../controls';

describe( 'actions', () => {
describe( 'resetBlocks', () => {
Expand Down Expand Up @@ -206,21 +206,6 @@ 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 @@ -229,16 +214,11 @@ describe( 'actions', () => {
const actions = Array.from( removeBlocks( clientIds ) );

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

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

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

0 comments on commit 37bcda9

Please sign in to comment.