Skip to content

Commit

Permalink
Block Bindings: rely on broader context instead of requiring direct b…
Browse files Browse the repository at this point in the history
…lock context
  • Loading branch information
ellatrix authored and SantosGuillamot committed Apr 30, 2024
1 parent 667ba29 commit 800c96b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
26 changes: 22 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 } from '@wordpress/element';
import { useCallback, 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 @@ -58,11 +59,12 @@ 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 bindings = props.attributes.metadata?.bindings;
const { name, clientId, context } = props;
const { name, clientId } = props;
const boundAttributes = useSelect( () => {
if ( ! bindings ) {
return;
Expand All @@ -81,6 +83,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
continue;
}

const context = {};

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

const args = {
registry,
context,
Expand All @@ -102,7 +112,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}

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

const { setAttributes } = props;

Expand All @@ -127,6 +137,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
continue;
}

const context = {};

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

source.setValue( {
registry,
context,
Expand All @@ -148,7 +166,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
bindings,
name,
clientId,
context,
blockContext,
setAttributes,
sources,
]
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function registerBlockBindingsSource( source ) {
type: 'REGISTER_BLOCK_BINDINGS_SOURCE',
sourceName: source.name,
sourceLabel: source.label,
usesContext: source.usesContext,
getValue: source.getValue,
setValue: source.setValue,
getPlaceholder: source.getPlaceholder,
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export function blockBindingsSources( state = {}, action ) {
...state,
[ action.sourceName ]: {
label: action.sourceLabel,
usesContext: action.usesContext,
getValue: action.getValue,
setValue: action.setValue,
getPlaceholder: action.getPlaceholder,
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { store as editorStore } from '../store';
export default {
name: 'core/post-meta',
label: _x( 'Post Meta', 'block bindings source' ),
usesContext: [ 'postId', 'postType' ],
getPlaceholder( { args } ) {
return args.key;
},
Expand Down

0 comments on commit 800c96b

Please sign in to comment.