-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Add initial API for Block Variations (formerly Patterns) #18270
Changes from 3 commits
73ff4e5
1f1e2c7
2cc172b
5593de0
f6feeef
61701db
0f296bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,47 @@ export function blockStyles( state = {}, action ) { | |
return state; | ||
} | ||
|
||
/** | ||
* Reducer managing the block patterns. | ||
* | ||
* @param {Object} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* | ||
* @return {Object} Updated state. | ||
*/ | ||
export function blockPatterns( state = {}, action ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nearly identical to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since we're inheriting the logic, I'm not overly concerned with addressing it here, but a point for both this and the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, indeed. I guess this would force us to use memoization for the selector, but it seems like a good trade-off given that the implementation would get much simpler in the reducer. I'll open a follow-up shortly after I merge this one which is going to explore this idea for both reducers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #18398 to keep track of it. |
||
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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooops 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor planned in #18398 is going to fix it. |
||
} ), | ||
}; | ||
case 'ADD_BLOCK_PATTERNS': | ||
return { | ||
...state, | ||
[ action.blockName ]: uniqBy( [ | ||
...get( state, [ action.blockName ], [] ), | ||
...( action.patterns ), | ||
], ( pattern ) => pattern.name ), | ||
}; | ||
case 'REMOVE_BLOCK_PATTERNS': | ||
return { | ||
...state, | ||
[ action.blockName ]: filter( | ||
get( state, [ action.blockName ], [] ), | ||
( pattern ) => action.patternNames.indexOf( pattern.name ) === -1, | ||
), | ||
}; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
/** | ||
* Higher-order Reducer creating a reducer keeping track of given block name. | ||
* | ||
|
@@ -162,6 +203,7 @@ export function categories( state = DEFAULT_CATEGORIES, action ) { | |
export default combineReducers( { | ||
blockTypes, | ||
blockStyles, | ||
blockPatterns, | ||
defaultBlockName, | ||
freeformFallbackBlockName, | ||
unregisteredFallbackBlockName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
__experimentalAddBlockPatterns, | ||
__experimentalRemoveBlockPatterns, | ||
} from '../actions'; | ||
|
||
describe( 'actions', () => { | ||
describe( 'addBlockPatterns', () => { | ||
const blockName = 'block/name'; | ||
const patternName = 'my-pattern'; | ||
|
||
it( 'should return the ADD_BLOCK_PATTERNS action', () => { | ||
const pattern = { | ||
name: patternName, | ||
label: 'My Pattern', | ||
attributes: { | ||
example: 'foo', | ||
}, | ||
}; | ||
const result = __experimentalAddBlockPatterns( blockName, pattern ); | ||
expect( result ).toEqual( { | ||
type: 'ADD_BLOCK_PATTERNS', | ||
patterns: [ | ||
pattern, | ||
], | ||
blockName, | ||
} ); | ||
} ); | ||
|
||
it( 'should return the REMOVE_BLOCK_PATTERNS action', () => { | ||
const result = __experimentalRemoveBlockPatterns( blockName, patternName ); | ||
expect( result ).toEqual( { | ||
type: 'REMOVE_BLOCK_PATTERNS', | ||
patternNames: [ | ||
patternName, | ||
], | ||
blockName, | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad - do we even need those APIs in the first place? I followed
registerBlockStyle
andunregisterBlockStyle
but we could also use data API directly. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, I think we should have them:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this
wp.blocks.*
API in the examples presenting how you can tweak the Columns block with custom patterns so maybe it's indeed more convenient. The benefit is also that you don't have to worry whether thecore/blocks
store is initialized as it's embedded in the module. Wheres using the data module directly is prone to error in that regard.