diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index c6b498bf160fa1..6820b4df1ed241 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -1869,8 +1869,9 @@ Action that updates attributes of multiple blocks with the specified client IDs. _Parameters_ - _clientIds_ `string|string[]`: Block client IDs. -- _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 +- _attributes_ `Object`: Block attributes to be merged. Should be keyed by clientIds if `options.uniqueByBlock` is true. +- _options_ `Object`: Updating options. +- _options.uniqueByBlock_ `[boolean]`: Whether each block in clientIds array has a unique set of attributes. _Returns_ diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 56924ed6abe3fe..8b7f8cf2542f35 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -155,22 +155,26 @@ export function receiveBlocks( blocks ) { /** * Action that updates attributes of multiple blocks with the specified client IDs. * - * @param {string|string[]} clientIds Block client IDs. - * @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 + * @param {string|string[]} clientIds Block client IDs. + * @param {Object} attributes Block attributes to be merged. Should be keyed by clientIds if `options.uniqueByBlock` is true. + * @param {Object} options Updating options. + * @param {boolean} [options.uniqueByBlock=false] Whether each block in clientIds array has a unique set of attributes. * @return {Object} Action object. */ export function updateBlockAttributes( clientIds, attributes, - uniqueByBlock = false + options = { uniqueByBlock: false } ) { + if ( typeof options === 'boolean' ) { + options = { uniqueByBlock: options }; + } + return { type: 'UPDATE_BLOCK_ATTRIBUTES', clientIds: castArray( clientIds ), attributes, - uniqueByBlock, + options, }; } diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 6e2c78a47f5fd7..5fc08ce7a37b89 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -882,7 +882,7 @@ export const blocks = pipe( const newState = new Map( state ); for ( const clientId of action.clientIds ) { const updatedAttributeEntries = Object.entries( - action.uniqueByBlock + !! action.options?.uniqueByBlock ? action.attributes[ clientId ] : action.attributes ?? {} ); @@ -1833,7 +1833,7 @@ export function lastBlockAttributesChange( state = null, action ) { return action.clientIds.reduce( ( accumulator, id ) => ( { ...accumulator, - [ id ]: action.uniqueByBlock + [ id ]: !! action.options?.uniqueByBlock ? action.attributes[ id ] : action.attributes, } ), diff --git a/packages/block-editor/src/store/test/actions.js b/packages/block-editor/src/store/test/actions.js index 843a8ce0571408..a8df7ff300ddee 100644 --- a/packages/block-editor/src/store/test/actions.js +++ b/packages/block-editor/src/store/test/actions.js @@ -89,7 +89,7 @@ describe( 'actions', () => { type: 'UPDATE_BLOCK_ATTRIBUTES', clientIds: [ clientId ], attributes, - uniqueByBlock: false, + options: { uniqueByBlock: false }, } ); } ); @@ -101,7 +101,19 @@ describe( 'actions', () => { type: 'UPDATE_BLOCK_ATTRIBUTES', clientIds, attributes, - uniqueByBlock: false, + options: { uniqueByBlock: false }, + } ); + } ); + + it( 'should fold boolean uniqueByBlock option into an object', () => { + const clientId = 'myclientid'; + const attributes = {}; + const result = updateBlockAttributes( clientId, attributes, true ); + expect( result ).toEqual( { + type: 'UPDATE_BLOCK_ATTRIBUTES', + clientIds: [ clientId ], + attributes, + options: { uniqueByBlock: true }, } ); } ); } ); diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 6706ff2fbb59e2..b0e67a5de53169 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -1963,7 +1963,7 @@ describe( 'state', () => { attributes: { kumquat: { updated: true }, }, - uniqueByBlock: true, + options: { uniqueByBlock: true }, } ); expect( state.attributes.get( 'kumquat' ).updated ).toBe( @@ -3308,7 +3308,7 @@ describe( 'state', () => { attributes: { 'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' }, }, - uniqueByBlock: true, + options: { uniqueByBlock: true }, } ); expect( state ).toEqual( { diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 0c224f26993312..ec605185675c96 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -392,7 +392,9 @@ export default function GalleryEdit( props ) { lightboxSetting ); } ); - updateBlockAttributes( blocks, changedAttributes, true ); + updateBlockAttributes( blocks, changedAttributes, { + uniqueByBlock: true, + } ); const linkToText = [ ...linkOptions ].find( ( linkType ) => linkType.value === value ); @@ -434,7 +436,9 @@ export default function GalleryEdit( props ) { block.attributes ); } ); - updateBlockAttributes( blocks, changedAttributes, true ); + updateBlockAttributes( blocks, changedAttributes, { + uniqueByBlock: true, + } ); const noticeText = openInNewTab ? __( 'All gallery images updated to open in new tab' ) : __( 'All gallery images updated to not open in new tab' ); @@ -458,7 +462,9 @@ export default function GalleryEdit( props ) { newSizeSlug ); } ); - updateBlockAttributes( blocks, changedAttributes, true ); + updateBlockAttributes( blocks, changedAttributes, { + uniqueByBlock: true, + } ); const imageSize = imageSizeOptions.find( ( size ) => size.value === newSizeSlug );