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

Data: Build the basic data controls into every store #25362

Merged
merged 7 commits into from
Oct 1, 2020
6 changes: 3 additions & 3 deletions packages/block-directory/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe( 'actions', () => {
args: [],
selectorName: 'getBlockTypes',
storeKey: 'core/blocks',
type: 'SELECT',
type: '@@data/RESOLVE_SELECT',
} );

expect( generator.next( [ block ] ).value ).toEqual( {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe( 'actions', () => {
args: [],
selectorName: 'getBlockTypes',
storeKey: 'core/blocks',
type: 'SELECT',
type: '@@data/RESOLVE_SELECT',
} );

expect( generator.next( [ inactiveBlock ] ).value ).toEqual( {
Expand Down Expand Up @@ -274,7 +274,7 @@ describe( 'actions', () => {
data: null,
};
expect( generator.throw( apiError ).value ).toMatchObject( {
type: 'DISPATCH',
type: '@@data/DISPATCH',
actionName: 'createErrorNotice',
storeKey: 'core/notices',
} );
Expand Down
49 changes: 23 additions & 26 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
} from '@wordpress/blocks';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { select } from './controls';
import { controls } from '@wordpress/data';

/**
* Generator which will yield a default block insert action if there
Expand All @@ -26,7 +23,7 @@ import { select } from './controls';
* replacement, etc).
*/
function* ensureDefaultBlock() {
const count = yield select( 'core/block-editor', 'getBlockCount' );
const count = yield controls.select( 'core/block-editor', 'getBlockCount' );

// To avoid a focus loss when removing the last block, assure there is
// always a default block if the last of the blocks have been removed.
Expand Down Expand Up @@ -156,7 +153,7 @@ export function selectBlock( clientId, initialPosition = null ) {
* @param {string} clientId Block client ID.
*/
export function* selectPreviousBlock( clientId ) {
const previousBlockClientId = yield select(
const previousBlockClientId = yield controls.select(
'core/block-editor',
'getPreviousBlockClientId',
clientId
Expand All @@ -175,7 +172,7 @@ export function* selectPreviousBlock( clientId ) {
* @param {string} clientId Block client ID.
*/
export function* selectNextBlock( clientId ) {
const nextBlockClientId = yield select(
const nextBlockClientId = yield controls.select(
'core/block-editor',
'getNextBlockClientId',
clientId
Expand Down Expand Up @@ -303,17 +300,17 @@ export function* replaceBlocks(
clientIds = castArray( clientIds );
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select( 'core/block-editor', 'getSettings' )
yield controls.select( 'core/block-editor', 'getSettings' )
);
const rootClientId = yield select(
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
first( clientIds )
);
// Replace is valid if the new blocks can be inserted in the root block.
for ( let index = 0; index < blocks.length; index++ ) {
const block = blocks[ index ];
const canInsertBlock = yield select(
const canInsertBlock = yield controls.select(
'core/block-editor',
'canInsertBlockType',
block.name,
Expand Down Expand Up @@ -385,7 +382,7 @@ export function* moveBlocksToPosition(
toRootClientId = '',
index
) {
const templateLock = yield select(
const templateLock = yield controls.select(
'core/block-editor',
'getTemplateLock',
fromRootClientId
Expand Down Expand Up @@ -418,7 +415,7 @@ export function* moveBlocksToPosition(
return;
}

const canInsertBlocks = yield select(
const canInsertBlocks = yield controls.select(
'core/block-editor',
'canInsertBlocks',
clientIds,
Expand Down Expand Up @@ -497,11 +494,11 @@ export function* insertBlocks(
) {
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select( 'core/block-editor', 'getSettings' )
yield controls.select( 'core/block-editor', 'getSettings' )
);
const allowedBlocks = [];
for ( const block of blocks ) {
const isValid = yield select(
const isValid = yield controls.select(
'core/block-editor',
'canInsertBlockType',
block.name,
Expand Down Expand Up @@ -607,12 +604,12 @@ export function* removeBlocks( clientIds, selectPrevious = true ) {
}

clientIds = castArray( clientIds );
const rootClientId = yield select(
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientIds[ 0 ]
);
const isLocked = yield select(
const isLocked = yield controls.select(
'core/block-editor',
'getTemplateLock',
rootClientId
Expand All @@ -625,7 +622,7 @@ export function* removeBlocks( clientIds, selectPrevious = true ) {
if ( selectPrevious ) {
previousBlockId = yield selectPreviousBlock( clientIds[ 0 ] );
} else {
previousBlockId = yield select(
previousBlockId = yield controls.select(
'core/block-editor',
'getPreviousBlockClientId',
clientIds[ 0 ]
Expand Down Expand Up @@ -950,12 +947,12 @@ export function* duplicateBlocks( clientIds, updateSelection = true ) {
if ( ! clientIds && ! clientIds.length ) {
return;
}
const blocks = yield select(
const blocks = yield controls.select(
'core/block-editor',
'getBlocksByClientId',
clientIds
);
const rootClientId = yield select(
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientIds[ 0 ]
Expand All @@ -975,7 +972,7 @@ export function* duplicateBlocks( clientIds, updateSelection = true ) {
return;
}

const lastSelectedIndex = yield select(
const lastSelectedIndex = yield controls.select(
'core/block-editor',
'getBlockIndex',
last( castArray( clientIds ) ),
Expand Down Expand Up @@ -1006,12 +1003,12 @@ export function* insertBeforeBlock( clientId ) {
if ( ! clientId ) {
return;
}
const rootClientId = yield select(
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientId
);
const isLocked = yield select(
const isLocked = yield controls.select(
'core/block-editor',
'getTemplateLock',
rootClientId
Expand All @@ -1020,7 +1017,7 @@ export function* insertBeforeBlock( clientId ) {
return;
}

const firstSelectedIndex = yield select(
const firstSelectedIndex = yield controls.select(
'core/block-editor',
'getBlockIndex',
clientId,
Expand All @@ -1038,12 +1035,12 @@ export function* insertAfterBlock( clientId ) {
if ( ! clientId ) {
return;
}
const rootClientId = yield select(
const rootClientId = yield controls.select(
'core/block-editor',
'getBlockRootClientId',
clientId
);
const isLocked = yield select(
const isLocked = yield controls.select(
'core/block-editor',
'getTemplateLock',
rootClientId
Expand All @@ -1052,7 +1049,7 @@ export function* insertAfterBlock( clientId ) {
return;
}

const firstSelectedIndex = yield select(
const firstSelectedIndex = yield controls.select(
'core/block-editor',
'getBlockIndex',
clientId,
Expand Down
28 changes: 0 additions & 28 deletions packages/block-editor/src/store/controls.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
/**
* WordPress dependencies
*/
import { createRegistryControl } from '@wordpress/data';

/**
* Calls a selector using the current state.
*
* @param {string} storeName Store name.
* @param {string} selectorName Selector name.
* @param {Array} args Selector arguments.
*
* @return {Object} control descriptor.
*/
export function select( storeName, selectorName, ...args ) {
return {
type: 'SELECT',
storeName,
selectorName,
args,
};
}

const controls = {
SELECT: createRegistryControl(
( registry ) => ( { storeName, selectorName, args } ) => {
return registry.select( storeName )[ selectorName ]( ...args );
}
),
SLEEP( { duration } ) {
return new Promise( ( resolve ) => {
setTimeout( resolve, duration );
Expand Down
Loading