Skip to content
Merged
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
5 changes: 3 additions & 2 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down
16 changes: 10 additions & 6 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {}
);
Expand Down Expand Up @@ -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,
} ),
Expand Down
16 changes: 14 additions & 2 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe( 'actions', () => {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ clientId ],
attributes,
uniqueByBlock: false,
options: { uniqueByBlock: false },
} );
} );

Expand All @@ -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 },
} );
} );
} );
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ describe( 'state', () => {
attributes: {
kumquat: { updated: true },
},
uniqueByBlock: true,
options: { uniqueByBlock: true },
} );

expect( state.attributes.get( 'kumquat' ).updated ).toBe(
Expand Down Expand Up @@ -3308,7 +3308,7 @@ describe( 'state', () => {
attributes: {
'afd1cb17-2c08-4e7a-91be-007ba7ddc3a1': { food: 'banana' },
},
uniqueByBlock: true,
options: { uniqueByBlock: true },
} );

expect( state ).toEqual( {
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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' );
Expand All @@ -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
);
Expand Down
Loading