From 32ea24fc95dfc425f6eaaa9637f2cd836f12c50a Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 19 Feb 2019 09:05:26 +0100 Subject: [PATCH] Refactor controls to actions --- packages/editor/src/store/actions.js | 25 ++++++++----------- packages/editor/src/store/controls.js | 36 +++++++++++++++++---------- packages/editor/src/store/index.js | 2 +- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 90c98480187b7e..0c206cedf0a9fe 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -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. @@ -177,14 +182,10 @@ 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 ); } /** @@ -192,14 +193,10 @@ export function selectPreviousBlock( clientId ) { * 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() { diff --git a/packages/editor/src/store/controls.js b/packages/editor/src/store/controls.js index 198ce554a958ef..2af5632f9df57c 100644 --- a/packages/editor/src/store/controls.js +++ b/packages/editor/src/store/controls.js @@ -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; diff --git a/packages/editor/src/store/index.js b/packages/editor/src/store/index.js index bee18a50604f90..bc7b51a604fad5 100644 --- a/packages/editor/src/store/index.js +++ b/packages/editor/src/store/index.js @@ -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