From 983aacfb0e35c4ff2597b927fd66521a558bcadc Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Thu, 30 Jan 2020 16:59:12 +0100 Subject: [PATCH] Blocks: Rename patterns to variations in the Block API --- packages/blocks/src/api/registration.js | 98 ++++++++++---------- packages/blocks/src/api/test/registration.js | 6 +- packages/blocks/src/store/actions.js | 20 ++-- packages/blocks/src/store/reducer.js | 20 ++-- packages/blocks/src/store/selectors.js | 48 +++++----- packages/blocks/src/store/test/actions.js | 34 +++---- packages/blocks/src/store/test/reducer.js | 72 +++++++------- packages/blocks/src/store/test/selectors.js | 72 +++++++------- 8 files changed, 185 insertions(+), 185 deletions(-) diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 0fd01c1d4ff11..eaae3b1f14b68 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -65,31 +65,31 @@ import { DEPRECATED_ENTRY_KEYS } from './constants'; */ /** - * Named block pattern scopes. + * Named block variation scopes. * - * @typedef {'block'|'inserter'} WPBlockPatternScope + * @typedef {'block'|'inserter'} WPBlockVariationScope */ /** - * An object describing a pattern defined for the block type. + * An object describing a variation defined for the block type. * - * @typedef {Object} WPBlockPattern + * @typedef {Object} WPBlockVariation * - * @property {string} name The unique and machine-readable name. - * @property {string} title A human-readable pattern title. - * @property {string} description A detailed pattern description. - * @property {WPIcon} [icon] An icon helping to visualize the pattern. - * @property {boolean} [isDefault] Indicates whether the current pattern is - * the default one. Defaults to `false`. - * @property {Object} [attributes] Values which override block attributes. - * @property {Array[]} [innerBlocks] Initial configuration of nested blocks. - * @property {Object} [example] Example provides structured data for - * the block preview. You can set to - * `undefined` to disable the preview shown - * for the block type. - * @property {WPBlockPatternScope[]} [scope] The list of scopes where the pattern - * is applicable. When not provided, it - * assumes all available scopes. + * @property {string} name The unique and machine-readable name. + * @property {string} title A human-readable variation title. + * @property {string} description A detailed variation description. + * @property {WPIcon} [icon] An icon helping to visualize the variation. + * @property {boolean} [isDefault] Indicates whether the current variation is + * the default one. Defaults to `false`. + * @property {Object} [attributes] Values which override block attributes. + * @property {Array[]} [innerBlocks] Initial configuration of nested blocks. + * @property {Object} [example] Example provides structured data for + * the block preview. You can set to + * `undefined` to disable the preview shown + * for the block type. + * @property {WPBlockVariationScope[]} [scope] The list of scopes where the variation + * is applicable. When not provided, it + * assumes all available scopes. */ /** @@ -97,26 +97,26 @@ import { DEPRECATED_ENTRY_KEYS } from './constants'; * * @typedef {Object} WPBlock * - * @property {string} name Block type's namespaced name. - * @property {string} title Human-readable block type label. - * @property {string} description A detailed block type description. - * @property {string} category Block type category classification, - * used in search interfaces to arrange - * block types by category. - * @property {WPBlockTypeIcon} [icon] Block type icon. - * @property {string[]} [keywords] Additional keywords to produce block - * type as result in search interfaces. - * @property {Object} [attributes] Block type attributes. - * @property {WPComponent} [save] Optional component describing - * serialized markup structure of a - * block type. - * @property {WPComponent} edit Component rendering an element to - * manipulate the attributes of a block - * in the context of an editor. - * @property {WPBlockPattern[]} [patterns] The list of block patterns. - * @property {Object} [example] Example provides structured data for - * the block preview. When not defined - * then no preview is shown. + * @property {string} name Block type's namespaced name. + * @property {string} title Human-readable block type label. + * @property {string} description A detailed block type description. + * @property {string} category Block type category classification, + * used in search interfaces to arrange + * block types by category. + * @property {WPBlockTypeIcon} [icon] Block type icon. + * @property {string[]} [keywords] Additional keywords to produce block + * type as result in search interfaces. + * @property {Object} [attributes] Block type attributes. + * @property {WPComponent} [save] Optional component describing + * serialized markup structure of a + * block type. + * @property {WPComponent} edit Component rendering an element to + * manipulate the attributes of a block + * in the context of an editor. + * @property {WPBlockVariation[]} [variations] The list of block variations. + * @property {Object} [example] Example provides structured data for + * the block preview. When not defined + * then no preview is shown. */ /** @@ -499,21 +499,21 @@ export const unregisterBlockStyle = ( blockName, styleVariationName ) => { }; /** - * Registers a new block pattern for the given block. + * Registers a new block variation for the given block. * - * @param {string} blockName Name of the block (example: “core/columns”). - * @param {WPBlockPattern} pattern Object describing a block pattern. + * @param {string} blockName Name of the block (example: “core/columns”). + * @param {WPBlockVariation} variation Object describing a block variation. */ -export const __experimentalRegisterBlockPattern = ( blockName, pattern ) => { - dispatch( 'core/blocks' ).__experimentalAddBlockPatterns( blockName, pattern ); +export const __experimentalRegisterBlockVariation = ( blockName, variation ) => { + dispatch( 'core/blocks' ).__experimentalAddBlockVariations( blockName, variation ); }; /** - * Unregisters a block pattern defined for the given block. + * Unregisters a block variation defined for the given block. * - * @param {string} blockName Name of the block (example: “core/columns”). - * @param {string} patternName Name of the pattern defined for the block. + * @param {string} blockName Name of the block (example: “core/columns”). + * @param {string} variationName Name of the variation defined for the block. */ -export const __experimentalUnregisterBlockPattern = ( blockName, patternName ) => { - dispatch( 'core/blocks' ).__experimentalRemoveBlockPatterns( blockName, patternName ); +export const __experimentalUnregisterBlockVariation = ( blockName, variationName ) => { + dispatch( 'core/blocks' ).__experimentalRemoveBlockVariations( blockName, variationName ); }; diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 92aeab35ea649..a16f8f70235cc 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -477,7 +477,7 @@ describe( 'blocks', () => { }, attributes: {}, keywords: [], - patterns: [], + variations: [], }, ] ); const oldBlock = unregisterBlockType( 'core/test-block' ); @@ -607,7 +607,7 @@ describe( 'blocks', () => { }, attributes: {}, keywords: [], - patterns: [], + variations: [], }, { name: 'core/test-block-with-settings', @@ -620,7 +620,7 @@ describe( 'blocks', () => { }, attributes: {}, keywords: [], - patterns: [], + variations: [], }, ] ); } ); diff --git a/packages/blocks/src/store/actions.js b/packages/blocks/src/store/actions.js index 6d8521f5f8eee..c84117adaa147 100644 --- a/packages/blocks/src/store/actions.js +++ b/packages/blocks/src/store/actions.js @@ -64,33 +64,33 @@ export function removeBlockStyles( blockName, styleNames ) { } /** - * Returns an action object used in signalling that new block patterns have been added. + * Returns an action object used in signalling that new block variations have been added. * * @param {string} blockName Block name. - * @param {WPBlockPattern|WPBlockPattern[]} patterns Block patterns. + * @param {WPBlockVariation|WPBlockVariation[]} variations Block variations. * * @return {Object} Action object. */ -export function __experimentalAddBlockPatterns( blockName, patterns ) { +export function __experimentalAddBlockVariations( blockName, variations ) { return { - type: 'ADD_BLOCK_PATTERNS', - patterns: castArray( patterns ), + type: 'ADD_BLOCK_VARIATIONS', + variations: castArray( variations ), blockName, }; } /** - * Returns an action object used in signalling that block patterns have been removed. + * Returns an action object used in signalling that block variations have been removed. * * @param {string} blockName Block name. - * @param {string|string[]} patternNames Block pattern names. + * @param {string|string[]} variationNames Block variation names. * * @return {Object} Action object. */ -export function __experimentalRemoveBlockPatterns( blockName, patternNames ) { +export function __experimentalRemoveBlockVariations( blockName, variationNames ) { return { - type: 'REMOVE_BLOCK_PATTERNS', - patternNames: castArray( patternNames ), + type: 'REMOVE_BLOCK_VARIATIONS', + variationNames: castArray( variationNames ), blockName, }; } diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 027748ba70003..6196059d5a132 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -98,39 +98,39 @@ export function blockStyles( state = {}, action ) { } /** - * Reducer managing the block patterns. + * Reducer managing the block variations. * * @param {Object} state Current state. * @param {Object} action Dispatched action. * * @return {Object} Updated state. */ -export function blockPatterns( state = {}, action ) { +export function blockVariations( state = {}, action ) { switch ( action.type ) { case 'ADD_BLOCK_TYPES': return { ...state, ...mapValues( keyBy( action.blockTypes, 'name' ), ( blockType ) => { return uniqBy( [ - ...get( blockType, [ 'patterns' ], [] ), + ...get( blockType, [ 'variations' ], [] ), ...get( state, [ blockType.name ], [] ), - ], ( pattern ) => pattern.name ); + ], ( variation ) => variation.name ); } ), }; - case 'ADD_BLOCK_PATTERNS': + case 'ADD_BLOCK_VARIATIONS': return { ...state, [ action.blockName ]: uniqBy( [ ...get( state, [ action.blockName ], [] ), - ...( action.patterns ), - ], ( pattern ) => pattern.name ), + ...( action.variations ), + ], ( variation ) => variation.name ), }; - case 'REMOVE_BLOCK_PATTERNS': + case 'REMOVE_BLOCK_VARIATIONS': return { ...state, [ action.blockName ]: filter( get( state, [ action.blockName ], [] ), - ( pattern ) => action.patternNames.indexOf( pattern.name ) === -1, + ( variation ) => action.variationNames.indexOf( variation.name ) === -1, ), }; } @@ -219,7 +219,7 @@ export function collections( state = {}, action ) { export default combineReducers( { blockTypes, blockStyles, - blockPatterns, + blockVariations, defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index 2e81931d6ef29..3cb9306d60c4c 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -14,7 +14,7 @@ import { some, } from 'lodash'; -/** @typedef {import('../api/registration').WPBlockPatternScope} WPBlockPatternScope */ +/** @typedef {import('../api/registration').WPBlockVariationScope} WPBlockVariationScope */ /** * Given a block name or block type object, returns the corresponding @@ -43,13 +43,13 @@ export const getBlockTypes = createSelector( return Object.values( state.blockTypes ).map( ( blockType ) => { return { ...blockType, - patterns: __experimentalGetBlockPatterns( state, blockType.name ), + variations: __experimentalGetBlockVariations( state, blockType.name ), }; } ); }, ( state ) => [ state.blockTypes, - state.blockPatterns, + state.blockVariations, ] ); @@ -78,40 +78,40 @@ export function getBlockStyles( state, name ) { } /** - * Returns block patterns by block name. + * Returns block variations by block name. * - * @param {Object} state Data state. - * @param {string} blockName Block type name. - * @param {WPBlockPatternScope} [scope] Block pattern scope name. + * @param {Object} state Data state. + * @param {string} blockName Block type name. + * @param {WPBlockVariationScope} [scope] Block variation scope name. * - * @return {(WPBlockPattern[]|void)} Block patterns. + * @return {(WPBlockPattern[]|void)} Block variations. */ -export function __experimentalGetBlockPatterns( state, blockName, scope ) { - const patterns = state.blockPatterns[ blockName ]; - if ( ! patterns || ! scope ) { - return patterns; +export function __experimentalGetBlockVariations( state, blockName, scope ) { + const variations = state.blockVariations[ blockName ]; + if ( ! variations || ! scope ) { + return variations; } - return patterns.filter( ( pattern ) => { - return ! pattern.scope || pattern.scope.includes( scope ); + return variations.filter( ( variation ) => { + return ! variation.scope || variation.scope.includes( scope ); } ); } /** - * Returns the default block pattern for the given block type. - * When there are multiple patterns annotated as the default one, + * Returns the default block variation for the given block type. + * When there are multiple variations annotated as the default one, * the last added item is picked. This simplifies registering overrides. - * When there is no default pattern set, it returns the first item. + * When there is no default variation set, it returns the first item. * - * @param {Object} state Data state. - * @param {string} blockName Block type name. - * @param {WPBlockPatternScope} [scope] Block pattern scope name. + * @param {Object} state Data state. + * @param {string} blockName Block type name. + * @param {WPBlockVariationScope} [scope] Block variation scope name. * - * @return {?WPBlockPattern} The default block pattern. + * @return {?WPBlockPattern} The default block variation. */ -export function __experimentalGetDefaultBlockPattern( state, blockName, scope ) { - const patterns = __experimentalGetBlockPatterns( state, blockName, scope ); +export function __experimentalGetDefaultBlockVariation( state, blockName, scope ) { + const variations = __experimentalGetBlockVariations( state, blockName, scope ); - return findLast( patterns, 'isDefault' ) || first( patterns ); + return findLast( variations, 'isDefault' ) || first( variations ); } /** diff --git a/packages/blocks/src/store/test/actions.js b/packages/blocks/src/store/test/actions.js index 35ece735f4486..acf194b01b586 100644 --- a/packages/blocks/src/store/test/actions.js +++ b/packages/blocks/src/store/test/actions.js @@ -2,39 +2,39 @@ * Internal dependencies */ import { - __experimentalAddBlockPatterns, - __experimentalRemoveBlockPatterns, + __experimentalAddBlockVariations, + __experimentalRemoveBlockVariations, } from '../actions'; describe( 'actions', () => { - describe( 'addBlockPatterns', () => { + describe( 'addBlockVariations', () => { const blockName = 'block/name'; - const patternName = 'my-pattern'; + const variationName = 'my-variation'; - it( 'should return the ADD_BLOCK_PATTERNS action', () => { - const pattern = { - name: patternName, - title: 'My Pattern', + it( 'should return the ADD_BLOCK_VARIATIONS action', () => { + const variation = { + name: variationName, + title: 'My Variation', attributes: { example: 'foo', }, }; - const result = __experimentalAddBlockPatterns( blockName, pattern ); + const result = __experimentalAddBlockVariations( blockName, variation ); expect( result ).toEqual( { - type: 'ADD_BLOCK_PATTERNS', - patterns: [ - pattern, + type: 'ADD_BLOCK_VARIATIONS', + variations: [ + variation, ], blockName, } ); } ); - it( 'should return the REMOVE_BLOCK_PATTERNS action', () => { - const result = __experimentalRemoveBlockPatterns( blockName, patternName ); + it( 'should return the REMOVE_BLOCK_VARIATIONS action', () => { + const result = __experimentalRemoveBlockVariations( blockName, variationName ); expect( result ).toEqual( { - type: 'REMOVE_BLOCK_PATTERNS', - patternNames: [ - patternName, + type: 'REMOVE_BLOCK_VARIATIONS', + variationNames: [ + variationName, ], blockName, } ); diff --git a/packages/blocks/src/store/test/reducer.js b/packages/blocks/src/store/test/reducer.js index 2567903962d14..b423cdc25caa1 100644 --- a/packages/blocks/src/store/test/reducer.js +++ b/packages/blocks/src/store/test/reducer.js @@ -7,12 +7,12 @@ import deepFreeze from 'deep-freeze'; * Internal dependencies */ import { - __experimentalAddBlockPatterns, + __experimentalAddBlockVariations, addBlockTypes, - __experimentalRemoveBlockPatterns, + __experimentalRemoveBlockVariations, } from '../actions'; import { - blockPatterns, + blockVariations, blockStyles, blockTypes, categories, @@ -142,103 +142,103 @@ describe( 'blockStyles', () => { } ); } ); -describe( 'blockPatterns', () => { +describe( 'blockVariations', () => { const blockName = 'block/name'; - const blockPatternName = 'pattern-name'; - const blockPattern = { - name: blockPatternName, - label: 'My pattern', + const blockVariationName = 'variation-name'; + const blockVariation = { + name: blockVariationName, + label: 'My variation', }; - const secondBlockPatternName = 'second-pattern-name'; - const secondBlockPattern = { - name: secondBlockPatternName, - label: 'My Second Pattern', + const secondBlockVariationName = 'second-variation-name'; + const secondBlockVariation = { + name: secondBlockVariationName, + label: 'My Second Variation', }; it( 'should return an empty object as default state', () => { - const state = blockPatterns( undefined, {} ); + const state = blockVariations( undefined, {} ); expect( state ).toEqual( {} ); } ); - it( 'should add a new block pattern when no pattern register', () => { + it( 'should add a new block variation when no variation register', () => { const initialState = deepFreeze( {} ); - const state = blockPatterns( + const state = blockVariations( initialState, - __experimentalAddBlockPatterns( blockName, blockPattern ) + __experimentalAddBlockVariations( blockName, blockVariation ) ); expect( state ).toEqual( { [ blockName ]: [ - blockPattern, + blockVariation, ], } ); } ); - it( 'should add another pattern when a block pattern already present for the block', () => { + it( 'should add another variation when a block variation already present for the block', () => { const initialState = deepFreeze( { [ blockName ]: [ - blockPattern, + blockVariation, ], } ); - const state = blockPatterns( + const state = blockVariations( initialState, - __experimentalAddBlockPatterns( blockName, secondBlockPattern ), + __experimentalAddBlockVariations( blockName, secondBlockVariation ), ); expect( state ).toEqual( { [ blockName ]: [ - blockPattern, - secondBlockPattern, + blockVariation, + secondBlockVariation, ], } ); } ); - it( 'should prepend block patterns added when adding a block', () => { + it( 'should prepend block variations added when adding a block', () => { const initialState = deepFreeze( { [ blockName ]: [ - secondBlockPattern, + secondBlockVariation, ], } ); - const state = blockPatterns( + const state = blockVariations( initialState, addBlockTypes( { name: blockName, - patterns: [ - blockPattern, + variations: [ + blockVariation, ], } ) ); expect( state ).toEqual( { [ blockName ]: [ - blockPattern, - secondBlockPattern, + blockVariation, + secondBlockVariation, ], } ); } ); - it( 'should remove a block pattern', () => { + it( 'should remove a block variation', () => { const initialState = deepFreeze( { [ blockName ]: [ - blockPattern, - secondBlockPattern, + blockVariation, + secondBlockVariation, ], } ); - const state = blockPatterns( + const state = blockVariations( initialState, - __experimentalRemoveBlockPatterns( blockName, blockPatternName ) + __experimentalRemoveBlockVariations( blockName, blockVariationName ) ); expect( state ).toEqual( { [ blockName ]: [ - secondBlockPattern, + secondBlockVariation, ], } ); } ); diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index a7833648bda51..fbe568162c18d 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -8,7 +8,7 @@ import deepFreeze from 'deep-freeze'; */ import { getChildBlockNames, - __experimentalGetDefaultBlockPattern, + __experimentalGetDefaultBlockVariation, getGroupingBlockName, isMatchingSearchTerm, } from '../selectors'; @@ -145,73 +145,73 @@ describe( 'selectors', () => { } ); } ); - describe( '__experimentalGetDefaultBlockPattern', () => { + describe( '__experimentalGetDefaultBlockVariation', () => { const blockName = 'block/name'; - const createBlockPatternsState = ( patterns ) => { + const createBlockVariationsState = ( variations ) => { return deepFreeze( { - blockPatterns: { - [ blockName ]: patterns, + blockVariations: { + [ blockName ]: variations, }, } ); }; - const firstBlockPattern = { - name: 'first-block-pattern', + const firstBlockVariation = { + name: 'first-block-variation', }; - const secondBlockPattern = { - name: 'second-block-pattern', + const secondBlockVariation = { + name: 'second-block-variation', }; - const thirdBlockPattern = { - name: 'third-block-pattern', + const thirdBlockVariation = { + name: 'third-block-variation', }; - it( 'should return the default pattern when set', () => { - const defaultBlockPattern = { - ...secondBlockPattern, + it( 'should return the default variation when set', () => { + const defaultBlockVariation = { + ...secondBlockVariation, isDefault: true, }; - const state = createBlockPatternsState( [ - firstBlockPattern, - defaultBlockPattern, - thirdBlockPattern, + const state = createBlockVariationsState( [ + firstBlockVariation, + defaultBlockVariation, + thirdBlockVariation, ] ); - const result = __experimentalGetDefaultBlockPattern( state, blockName ); + const result = __experimentalGetDefaultBlockVariation( state, blockName ); - expect( result ).toEqual( defaultBlockPattern ); + expect( result ).toEqual( defaultBlockVariation ); } ); - it( 'should return the last pattern when multiple default patterns added', () => { - const defaultBlockPattern = { - ...thirdBlockPattern, + it( 'should return the last variation when multiple default variations added', () => { + const defaultBlockVariation = { + ...thirdBlockVariation, isDefault: true, }; - const state = createBlockPatternsState( [ + const state = createBlockVariationsState( [ { - ...firstBlockPattern, + ...firstBlockVariation, isDefault: true, }, { - ...secondBlockPattern, + ...secondBlockVariation, isDefault: true, }, - defaultBlockPattern, + defaultBlockVariation, ] ); - const result = __experimentalGetDefaultBlockPattern( state, blockName ); + const result = __experimentalGetDefaultBlockVariation( state, blockName ); - expect( result ).toEqual( defaultBlockPattern ); + expect( result ).toEqual( defaultBlockVariation ); } ); - it( 'should return the first pattern when no default pattern set', () => { - const state = createBlockPatternsState( [ - firstBlockPattern, - secondBlockPattern, - thirdBlockPattern, + it( 'should return the first variation when no default variation set', () => { + const state = createBlockVariationsState( [ + firstBlockVariation, + secondBlockVariation, + thirdBlockVariation, ] ); - const result = __experimentalGetDefaultBlockPattern( state, blockName ); + const result = __experimentalGetDefaultBlockVariation( state, blockName ); - expect( result ).toEqual( firstBlockPattern ); + expect( result ).toEqual( firstBlockVariation ); } ); } );