Skip to content

Commit

Permalink
Data: Build the basic data controls into every store (#25362)
Browse files Browse the repository at this point in the history
* createRegistrySelector: don't bind to defaultRegistry by default

We intend to register some controls created by `createRegistryControl` as
built-ins with every store. In order to do that, we need to break a dependency
cycle where registry creation depends on store registration which depends
on creating controls which depends on default registry.

The solution is to remove the `defaultRegistry` binding, which is there
only to satisfy a typechecker anyway, and doesn't have any runtime impact.

* Data: add built-in controls for select, resolveSelect and dispatch

Instead of shipping them in a separate `data-controls` package and require
the store author to register them, build them in into every store.

* data-controls: Remove implementation of built-in data controls

The implementation and the unit test have been moved to the `@wordpress/data`
package. The `data-controls` package now exposes only legacy aliases.

* Update unit tests that depend on internal representation of data controls

Updates unit tests of actions (in `block-directory`, `edit-site` and `core-data`)
that inspect the internal properties (like `type`) of controls that the action
generator yields.

A test that verifies if the action _behaves_ correctly wouldn't need to be changed
like that.

* Block Editor: replace a home-grown SELECT control with builtin one

* Mark the deprecated controls as deprecated

* Comment out the deprecations until all Gutenberg packages are updated
  • Loading branch information
jsnajdr committed Oct 1, 2020
1 parent 1279310 commit 16e98d4
Show file tree
Hide file tree
Showing 17 changed files with 663 additions and 606 deletions.
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 @@ -386,7 +383,7 @@ export function* moveBlocksToPosition(
toRootClientId = '',
index
) {
const templateLock = yield select(
const templateLock = yield controls.select(
'core/block-editor',
'getTemplateLock',
fromRootClientId
Expand Down Expand Up @@ -419,7 +416,7 @@ export function* moveBlocksToPosition(
return;
}

const canInsertBlocks = yield select(
const canInsertBlocks = yield controls.select(
'core/block-editor',
'canInsertBlocks',
clientIds,
Expand Down Expand Up @@ -498,11 +495,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 @@ -608,12 +605,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 @@ -626,7 +623,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 @@ -951,12 +948,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 @@ -976,7 +973,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 @@ -1007,12 +1004,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 @@ -1021,7 +1018,7 @@ export function* insertBeforeBlock( clientId ) {
return;
}

const firstSelectedIndex = yield select(
const firstSelectedIndex = yield controls.select(
'core/block-editor',
'getBlockIndex',
clientId,
Expand All @@ -1039,12 +1036,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 @@ -1053,7 +1050,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

0 comments on commit 16e98d4

Please sign in to comment.