Skip to content

Commit

Permalink
Refactor controls to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Feb 19, 2019
1 parent 424604e commit 32ea24f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
25 changes: 11 additions & 14 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { castArray } from 'lodash';
*/
import { getDefaultBlockName, createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { select } from './controls';

/**
* Returns an action object used in signalling that editor has initialized with
* the specified post object and editor settings.
Expand Down Expand Up @@ -177,29 +182,21 @@ export function selectBlock( clientId, initialPosition = null ) {
* given clientId should be selected.
*
* @param {string} clientId Block client ID.
*
* @return {Object} Action object.
*/
export function selectPreviousBlock( clientId ) {
return {
type: 'SELECT_PREVIOUS_BLOCK',
clientId,
};
export function * selectPreviousBlock( clientId ) {
const previousBlockClientId = yield select( 'core/editor', 'getPreviousBlockClientId', clientId );
yield selectBlock( previousBlockClientId );
}

/**
* Returns an action object used in signalling that the block following the
* given clientId should be selected.
*
* @param {string} clientId Block client ID.
*
* @return {Object} Action object.
*/
export function selectNextBlock( clientId ) {
return {
type: 'SELECT_NEXT_BLOCK',
clientId,
};
export function * selectNextBlock( clientId ) {
const nextBlockClientId = yield select( 'core/editor', 'getNextBlockClientId', clientId );
yield selectBlock( nextBlockClientId );
}

export function startMultiSelect() {
Expand Down
36 changes: 23 additions & 13 deletions packages/editor/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
*/
import { createRegistryControl } from '@wordpress/data';

export const SELECT_PREVIOUS_BLOCK = createRegistryControl( ( registry ) => ( action ) => {
const { clientId } = action;
const { getPreviousBlockClientId } = registry.select( 'core/editor' );
const { selectBlock } = registry.dispatch( 'core/editor' );

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

export const SELECT_NEXT_BLOCK = createRegistryControl( ( registry ) => ( action ) => {
const { clientId } = action;
const { getNextBlockClientId } = registry.select( 'core/editor' );
const { selectBlock } = registry.dispatch( 'core/editor' );
const controls = {
SELECT: createRegistryControl( ( registry ) => ( { storeKey, selectorName, args } ) => {
return registry.select( storeKey )[ selectorName ]( ...args );
} ),
};

selectBlock( getNextBlockClientId( clientId ) );
} );
export default controls;
2 changes: 1 addition & 1 deletion packages/editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import reducer from './reducer';
import applyMiddlewares from './middlewares';
import * as selectors from './selectors';
import * as actions from './actions';
import * as controls from './controls';
import controls from './controls';

/**
* Module Constants
Expand Down

0 comments on commit 32ea24f

Please sign in to comment.