Skip to content
Merged
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
Use bindings param for the API
  • Loading branch information
SantosGuillamot committed Jul 19, 2024
commit c4bf3dd682c20229610db94adcba0a38d7b69095
30 changes: 13 additions & 17 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
const hasPatternOverridesDefaultBinding =
props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ]
?.source === 'core/pattern-overrides';
const bindings = useMemo(
const blockBindings = useMemo(
() =>
replacePatternOverrideDefaultBindings(
name,
Expand All @@ -115,7 +115,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
// there are attribute updates.
// `source.getValues` may also call a selector via `registry.select`.
const boundAttributes = useSelect( () => {
if ( ! bindings ) {
if ( ! blockBindings ) {
return;
}

Expand All @@ -124,7 +124,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
const bindingsBySource = new Map();

for ( const [ attributeName, binding ] of Object.entries(
bindings
blockBindings
) ) {
const { source: sourceName, args: sourceArgs } = binding;
const source = sources[ sourceName ];
Expand All @@ -144,13 +144,13 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

if ( bindingsBySource.size ) {
for ( const [ source, sourceBindings ] of bindingsBySource ) {
for ( const [ source, bindings ] of bindingsBySource ) {
// Get values in batch if the source supports it.
const values = source.getValues( {
registry,
context,
clientId,
sourceBindings,
bindings,
} );
for ( const [ attributeName, value ] of Object.entries(
values
Expand All @@ -166,8 +166,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
context,
clientId,
attributeName,
args: sourceBindings[ attributeName ]
.args,
args: bindings[ attributeName ].args,
} );
}
} else {
Expand All @@ -178,14 +177,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

return attributes;
}, [ bindings, name, clientId, context, registry, sources ] );
}, [ blockBindings, name, clientId, context, registry, sources ] );

const { setAttributes } = props;

const _setAttributes = useCallback(
( nextAttributes ) => {
registry.batch( () => {
if ( ! bindings ) {
if ( ! blockBindings ) {
setAttributes( nextAttributes );
return;
}
Expand All @@ -198,13 +197,13 @@ export const withBlockBindingSupport = createHigherOrderComponent(
keptAttributes
) ) {
if (
! bindings[ attributeName ] ||
! blockBindings[ attributeName ] ||
! canBindAttribute( name, attributeName )
) {
continue;
}

const binding = bindings[ attributeName ];
const binding = blockBindings[ attributeName ];
const source = sources[ binding?.source ];
if ( ! source?.setValues ) {
continue;
Expand All @@ -220,15 +219,12 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

if ( bindingsBySource.size ) {
for ( const [
source,
sourceBindings,
] of bindingsBySource ) {
for ( const [ source, bindings ] of bindingsBySource ) {
source.setValues( {
registry,
context,
clientId,
sourceBindings,
bindings,
} );
}
}
Expand All @@ -253,7 +249,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
},
[
registry,
bindings,
blockBindings,
name,
clientId,
context,
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/bindings/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const CONTENT = 'content';
export default {
name: 'core/pattern-overrides',
label: _x( 'Pattern Overrides', 'block bindings source' ),
getValues( { registry, clientId, context, sourceBindings } ) {
getValues( { registry, clientId, context, bindings } ) {
const patternOverridesContent = context[ 'pattern/overrides' ];
const { getBlockAttributes } = registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );

const overridesValues = {};
for ( const attributeName of Object.keys( sourceBindings ) ) {
for ( const attributeName of Object.keys( bindings ) ) {
const overridableValue =
patternOverridesContent?.[
currentBlockAttributes?.metadata?.name
Expand All @@ -34,7 +34,7 @@ export default {
}
return overridesValues;
},
setValues( { registry, clientId, sourceBindings } ) {
setValues( { registry, clientId, bindings } ) {
const { getBlockAttributes, getBlockParentsByBlockName, getBlocks } =
registry.select( blockEditorStore );
const currentBlockAttributes = getBlockAttributes( clientId );
Expand All @@ -50,7 +50,7 @@ export default {
);

// Extract the updated attributes from the source bindings.
const attributes = Object.entries( sourceBindings ).reduce(
const attributes = Object.entries( bindings ).reduce(
( attrs, [ key, { newValue } ] ) => {
attrs[ key ] = newValue;
return attrs;
Expand Down
10 changes: 4 additions & 6 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
getPlaceholder( { args } ) {
return args.key;
},
getValues( { registry, context, sourceBindings } ) {
getValues( { registry, context, bindings } ) {
const meta = registry
.select( coreDataStore )
.getEditedEntityRecord(
Expand All @@ -24,16 +24,14 @@ export default {
context?.postId
)?.meta;
const newValues = {};
for ( const [ attributeName, source ] of Object.entries(
sourceBindings
) ) {
for ( const [ attributeName, source ] of Object.entries( bindings ) ) {
newValues[ attributeName ] = meta?.[ source.args.key ];
}
return newValues;
},
setValues( { registry, context, sourceBindings } ) {
setValues( { registry, context, bindings } ) {
const newMeta = {};
Object.values( sourceBindings ).forEach( ( { args, newValue } ) => {
Object.values( bindings ).forEach( ( { args, newValue } ) => {
newMeta[ args.key ] = newValue;
} );
registry
Expand Down