Skip to content

Commit

Permalink
Extend updateBlockAttributes to provide for different attribute chang…
Browse files Browse the repository at this point in the history
…es for each block in the clientIds array (#29099)

Co-authored-by: Glen Davies <glen.davies@a8c.com>
  • Loading branch information
glendaviesnz and Glen Davies committed Feb 23, 2021
1 parent 1b65fe9 commit ec1b9da
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,8 @@ attributes with the specified client IDs have been updated.
_Parameters_

- _clientIds_ `string|string[]`: Block client IDs.
- _attributes_ `Object`: Block attributes to be merged.
- _attributes_ `Object`: Block attributes to be merged. Should be keyed by clientIds if uniqueByBlock is true.
- _uniqueByBlock_ `boolean`: true if each block in clientIds array has a unique set of attributes

_Returns_

Expand Down
12 changes: 9 additions & 3 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,21 @@ export function receiveBlocks( blocks ) {
* attributes with the specified client IDs have been updated.
*
* @param {string|string[]} clientIds Block client IDs.
* @param {Object} attributes Block attributes to be merged.
*
* @param {Object} attributes Block attributes to be merged. Should be keyed by clientIds if
* uniqueByBlock is true.
* @param {boolean} uniqueByBlock true if each block in clientIds array has a unique set of attributes
* @return {Object} Action object.
*/
export function updateBlockAttributes( clientIds, attributes ) {
export function updateBlockAttributes(
clientIds,
attributes,
uniqueByBlock = false
) {
return {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: castArray( clientIds ),
attributes,
uniqueByBlock,
};
}

Expand Down
8 changes: 6 additions & 2 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,9 @@ export const blocks = flow(
( accumulator, id ) => ( {
...accumulator,
[ id ]: reduce(
action.attributes,
action.uniqueByBlock
? action.attributes[ id ]
: action.attributes,
( result, value, key ) => {
// Consider as updates only changed values.
if ( value !== result[ key ] ) {
Expand Down Expand Up @@ -1650,7 +1652,9 @@ export function lastBlockAttributesChange( state, action ) {
return action.clientIds.reduce(
( accumulator, id ) => ( {
...accumulator,
[ id ]: action.attributes,
[ id ]: action.uniqueByBlock
? action.attributes[ id ]
: action.attributes,
} ),
{}
);
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe( 'actions', () => {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ clientId ],
attributes,
uniqueByBlock: false,
} );
} );

Expand All @@ -101,6 +102,7 @@ describe( 'actions', () => {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds,
attributes,
uniqueByBlock: false,
} );
} );
} );
Expand Down
42 changes: 42 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,31 @@ describe( 'state', () => {
expect( state.attributes.kumquat.updated ).toBe( true );
} );

it( 'should return with attribute block updates when attributes are unique by block', () => {
const original = deepFreeze(
blocks( undefined, {
type: 'RESET_BLOCKS',
blocks: [
{
clientId: 'kumquat',
attributes: {},
innerBlocks: [],
},
],
} )
);
const state = blocks( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ 'kumquat' ],
attributes: {
kumquat: { updated: true },
},
uniqueByBlock: true,
} );

expect( state.attributes.kumquat.updated ).toBe( true );
} );

it( 'should accumulate attribute block updates', () => {
const original = deepFreeze(
blocks( undefined, {
Expand Down Expand Up @@ -2890,6 +2915,23 @@ describe( 'state', () => {
} );
} );

it( 'returns updated value when explicit block attributes update are unique by block id', () => {
const original = null;

const state = lastBlockAttributesChange( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1' ],
attributes: {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
},
uniqueByBlock: true,
} );

expect( state ).toEqual( {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
} );
} );

it( 'returns null on anything other than block attributes update', () => {
const original = deepFreeze( {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
Expand Down

0 comments on commit ec1b9da

Please sign in to comment.