Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Pass whole binding instead of just the new value
  • Loading branch information
SantosGuillamot committed Jul 5, 2024
commit f1b393f2e3500f7f9a33eb38256c2255d5b3e6f0
26 changes: 14 additions & 12 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

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

// Loop only over the updated attributes to avoid modifying the bound ones that haven't changed.
for ( const [ attributeName, newValue ] of Object.entries(
Expand All @@ -187,37 +187,39 @@ export const withBlockBindingSupport = createHigherOrderComponent(
) {
continue;
}
updatesBySource.set( source, {
...updatesBySource.get( source ),
[ attributeName ]: newValue,
bindingsBySource.set( source, {
...bindingsBySource.get( source ),
[ attributeName ]: {
args: binding.args,
newValue,
},
} );
delete keptAttributes[ attributeName ];
}

if ( updatesBySource.size ) {
if ( bindingsBySource.size ) {
for ( const [
source,
attributes,
] of updatesBySource ) {
sourceBindings,
] of bindingsBySource ) {
if ( source.setValuesInBatch ) {
source.setValuesInBatch( {
registry,
context,
clientId,
attributes,
sourceBindings,
} );
} else {
for ( const [
attributeName,
value,
] of Object.entries( attributes ) ) {
const binding = bindings[ attributeName ];
{ args, newValue: value },
] of Object.entries( sourceBindings ) ) {
source.setValue( {
registry,
context,
clientId,
attributeName,
args: binding.args,
args,
value,
} );
}
Expand Down
11 changes: 10 additions & 1 deletion packages/editor/src/bindings/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {

return overridableValue === '' ? undefined : overridableValue;
},
setValuesInBatch( { registry, clientId, attributes } ) {
setValuesInBatch( { registry, clientId, sourceBindings } ) {
const { getBlockAttributes, getBlockParentsByBlockName, getBlocks } =
registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
Expand All @@ -45,6 +45,15 @@ export default {
true
);

// Extract the updated attributes from the source bindings.
const attributes = Object.entries( sourceBindings ).reduce(
( attrs, [ key, { newValue } ] ) => {
attrs[ key ] = newValue;
return attrs;
},
{}
);

// If there is no pattern client ID, sync blocks with the same name and same attributes.
if ( ! patternClientId ) {
const syncBlocksWithSameName = ( blocks ) => {
Expand Down