Skip to content

Commit

Permalink
Blocks: Use selectors to fetch details about for patterns and styles …
Browse files Browse the repository at this point in the history
…from block types
  • Loading branch information
gziolo committed Nov 26, 2019
1 parent 276dd9f commit 330abef
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ Namespace: `core/blocks`.
<a name="getBlockStyles" href="#getBlockStyles">#</a> **getBlockStyles**

Returns block styles by block name.
It combines styles registered with the block together with
the block styles registered separately.

_Parameters_

- _state_ `Object`: Data state.
- _name_ `string`: Block type name.
- _blockName_ `string`: Block type name.

_Returns_

Expand Down
21 changes: 0 additions & 21 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
isEmpty,
keyBy,
map,
mapValues,
omit,
uniqBy,
} from 'lodash';
Expand Down Expand Up @@ -66,16 +65,6 @@ export function blockTypes( state = {}, action ) {
*/
export function blockStyles( state = {}, action ) {
switch ( action.type ) {
case 'ADD_BLOCK_TYPES':
return {
...state,
...mapValues( keyBy( action.blockTypes, 'name' ), ( blockType ) => {
return uniqBy( [
...get( blockType, [ 'styles' ], [] ),
...get( state, [ blockType.name ], [] ),
], ( style ) => style.name );
} ),
};
case 'ADD_BLOCK_STYLES':
return {
...state,
Expand Down Expand Up @@ -107,16 +96,6 @@ export function blockStyles( state = {}, action ) {
*/
export function blockPatterns( state = {}, action ) {
switch ( action.type ) {
case 'ADD_BLOCK_TYPES':
return {
...state,
...mapValues( keyBy( action.blockTypes, 'name' ), ( blockType ) => {
return uniqBy( [
...get( blockType, [ 'patterns' ], [] ),
...get( state, [ blockType.name ], [] ),
], ( style ) => style.name );
} ),
};
case 'ADD_BLOCK_PATTERNS':
return {
...state,
Expand Down
42 changes: 34 additions & 8 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import createSelector from 'rememo';
import { filter, get, includes, map, some, flow, deburr } from 'lodash';
import { compact, concat, filter, get, includes, map, some, flow, deburr } from 'lodash';

/**
* Given a block name or block type object, returns the corresponding
Expand Down Expand Up @@ -47,27 +47,53 @@ export function getBlockType( state, name ) {

/**
* Returns block styles by block name.
* It combines styles registered with the block together with
* the block styles registered separately.
*
* @param {Object} state Data state.
* @param {string} name Block type name.
* @param {string} blockName Block type name.
*
* @return {Array?} Block Styles.
*/
export function getBlockStyles( state, name ) {
return state.blockStyles[ name ];
}
export const getBlockStyles = createSelector(
( state, blockName ) => {
const blockType = getBlockType( state, blockName );

if ( ! blockType ) {
return;
}

return compact(
concat( blockType.styles, state.blockStyles[ blockName ] )
);
},
( state ) => [ state.blockTypes, state.blockStyles ]
);

/**
* Returns block patterns by block name.
* It combines patterns registered with the block together with
* the block patterns registered separately.
*
* @param {Object} state Data state.
* @param {string} blockName Block type name.
*
* @return {(WPBlockPattern[]|void)} Block patterns.
*/
export function __experimentalGetBlockPatterns( state, blockName ) {
return state.blockPatterns[ blockName ];
}
export const __experimentalGetBlockPatterns = createSelector(
( state, blockName ) => {
const blockType = getBlockType( state, blockName );

if ( ! blockType ) {
return;
}

return compact(
concat( blockType.patterns, state.blockPatterns[ blockName ] )
);
},
( state ) => [ state.blockTypes, state.blockPatterns ]
);

/**
* Returns all the available categories.
Expand Down
53 changes: 1 addition & 52 deletions packages/blocks/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import deepFreeze from 'deep-freeze';
*/
import {
__experimentalAddBlockPatterns,
addBlockTypes,
__experimentalRemoveBlockPatterns,
} from '../actions';
import {
Expand Down Expand Up @@ -95,31 +94,6 @@ describe( 'blockStyles', () => {
} );
} );

it( 'should add block styles when adding a block', () => {
const original = deepFreeze( {
'core/image': [
{ name: 'fancy' },
],
} );

const state = blockStyles( original, {
type: 'ADD_BLOCK_TYPES',
blockTypes: [ {
name: 'core/image',
styles: [
{ name: 'original' },
],
} ],
} );

expect( state ).toEqual( {
'core/image': [
{ name: 'original' },
{ name: 'fancy' },
],
} );
} );

it( 'should remove block styles', () => {
const original = deepFreeze( {
'core/image': [
Expand Down Expand Up @@ -163,7 +137,7 @@ describe( 'blockPatterns', () => {
expect( state ).toEqual( {} );
} );

it( 'should add a new block pattern when no pattern register', () => {
it( 'should add a new block pattern when no pattern registered', () => {
const initialState = deepFreeze( {} );

const state = blockPatterns(
Expand Down Expand Up @@ -198,31 +172,6 @@ describe( 'blockPatterns', () => {
} );
} );

it( 'should prepend block patterns added when adding a block', () => {
const initialState = deepFreeze( {
[ blockName ]: [
secondBlockPattern,
],
} );

const state = blockPatterns(
initialState,
addBlockTypes( {
name: blockName,
patterns: [
blockPattern,
],
} )
);

expect( state ).toEqual( {
[ blockName ]: [
blockPattern,
secondBlockPattern,
],
} );
} );

it( 'should remove a block pattern', () => {
const initialState = deepFreeze( {
[ blockName ]: [
Expand Down
Loading

0 comments on commit 330abef

Please sign in to comment.