Skip to content

Commit

Permalink
Read usesContext during bindings processing
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 22, 2024
1 parent 9a4d951 commit 0cc1812
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import { store as blocksStore } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useCallback, useMemo } from '@wordpress/element';
import { useCallback, useMemo, useContext } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';
import BlockContext from '../components/block-context';

/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */
Expand Down Expand Up @@ -93,10 +94,11 @@ export function canBindAttribute( blockName, attributeName ) {
export const withBlockBindingSupport = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const registry = useRegistry();
const blockContext = useContext( BlockContext );
const sources = useSelect( ( select ) =>
unlock( select( blocksStore ) ).getAllBlockBindingsSources()
);
const { name, clientId, context } = props;
const { name, clientId } = props;
const hasParentPattern = !! props.context[ 'pattern/overrides' ];
const hasPatternOverridesDefaultBinding =
props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ]
Expand Down Expand Up @@ -145,6 +147,15 @@ export const withBlockBindingSupport = createHigherOrderComponent(

if ( blockBindingsBySource.size ) {
for ( const [ source, bindings ] of blockBindingsBySource ) {
// Populate context.
const context = {};

if ( source.usesContext?.length ) {
for ( const key of source.usesContext ) {
context[ key ] = blockContext[ key ];
}
}

// Get values in batch if the source supports it.
const values = source.getValues( {
registry,
Expand Down Expand Up @@ -177,7 +188,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

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

const { setAttributes } = props;

Expand Down Expand Up @@ -223,6 +234,15 @@ export const withBlockBindingSupport = createHigherOrderComponent(
source,
bindings,
] of blockBindingsBySource ) {
// Populate context.
const context = {};

if ( source.usesContext?.length ) {
for ( const key of source.usesContext ) {
context[ key ] = blockContext[ key ];
}
}

source.setValues( {
registry,
context,
Expand Down Expand Up @@ -255,7 +275,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
blockBindings,
name,
clientId,
context,
blockContext,
setAttributes,
sources,
hasPatternOverridesDefaultBinding,
Expand Down

0 comments on commit 0cc1812

Please sign in to comment.