Skip to content
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

Extend updateBlockAttributes to provide for different attribute changes for each block in the clientIds array #29099

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -154,15 +154,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