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

Blocks: Use selectors to fetch details about for patterns and styles … #18398

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
} ),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know people used to disable styles by removing them from this reducer, this won't be possible anymore with this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Thanks for sharing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know people used to disable styles by removing them from this reducer, this won't be possible anymore with this change.

Can you elaborate on what you mean with this? Any links you can share with more information?

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