Skip to content

Commit

Permalink
Pattern overrides: use block binding editing API (#60721)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
  • Loading branch information
7 people authored May 14, 2024
1 parent 3bd64b5 commit 8d7c8fd
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 453 deletions.
64 changes: 48 additions & 16 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import { unlock } from '../lock-unlock';
const BLOCK_BINDINGS_ALLOWED_BLOCKS = {
'core/paragraph': [ 'content' ],
'core/heading': [ 'content' ],
'core/image': [ 'url', 'title', 'alt' ],
'core/button': [ 'url', 'text', 'linkTarget' ],
'core/image': [ 'id', 'url', 'title', 'alt' ],
'core/button': [ 'url', 'text', 'linkTarget', 'rel' ],
};

/**
Expand Down Expand Up @@ -110,34 +110,66 @@ export const withBlockBindingSupport = createHigherOrderComponent(
( nextAttributes ) => {
registry.batch( () => {
if ( ! bindings ) {
return setAttributes( nextAttributes );
setAttributes( nextAttributes );
return;
}

const keptAttributes = { ...nextAttributes };
const updatesBySource = new Map();

for ( const [
attributeName,
boundAttribute,
] of Object.entries( bindings ) ) {
const source = sources[ boundAttribute.source ];
// Loop only over the updated attributes to avoid modifying the bound ones that haven't changed.
for ( const [ attributeName, newValue ] of Object.entries(
keptAttributes
) ) {
if (
! source?.setValue ||
! bindings[ attributeName ] ||
! canBindAttribute( name, attributeName )
) {
continue;
}

source.setValue( {
registry,
context,
clientId,
attributeName,
args: boundAttribute.args,
value: nextAttributes[ attributeName ],
const source =
sources[ bindings[ attributeName ].source ];
if ( ! source?.setValue && ! source?.setValues ) {
continue;
}
updatesBySource.set( source, {
...updatesBySource.get( source ),
[ attributeName ]: newValue,
} );
delete keptAttributes[ attributeName ];
}

if ( updatesBySource.size ) {
for ( const [
source,
attributes,
] of updatesBySource ) {
if ( source.setValues ) {
source.setValues( {
registry,
context,
clientId,
attributes,
} );
} else {
for ( const [
attributeName,
value,
] of Object.entries( attributes ) ) {
source.setValue( {
registry,
context,
clientId,
attributeName,
args: bindings[ attributeName ].args,
value,
} );
}
}
}
}

if ( Object.keys( keptAttributes ).length ) {
setAttributes( keptAttributes );
}
Expand Down
Loading

0 comments on commit 8d7c8fd

Please sign in to comment.