Skip to content

Commit 0d34203

Browse files
committed
Block Bindings: rely on broader context instead of requiring direct block context
1 parent 06d7e49 commit 0d34203

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

packages/block-editor/src/hooks/use-bindings-attributes.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import { store as blocksStore } from '@wordpress/blocks';
55
import { createHigherOrderComponent } from '@wordpress/compose';
66
import { useRegistry, useSelect } from '@wordpress/data';
7-
import { useCallback } from '@wordpress/element';
7+
import { useCallback, useContext } from '@wordpress/element';
88
import { addFilter } from '@wordpress/hooks';
99

1010
/**
1111
* Internal dependencies
1212
*/
1313
import { unlock } from '../lock-unlock';
14+
import BlockContext from '../components/block-context';
1415

1516
/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
1617
/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */
@@ -58,11 +59,12 @@ export function canBindAttribute( blockName, attributeName ) {
5859
export const withBlockBindingSupport = createHigherOrderComponent(
5960
( BlockEdit ) => ( props ) => {
6061
const registry = useRegistry();
62+
const blockContext = useContext( BlockContext );
6163
const sources = useSelect( ( select ) =>
6264
unlock( select( blocksStore ) ).getAllBlockBindingsSources()
6365
);
6466
const bindings = props.attributes.metadata?.bindings;
65-
const { name, clientId, context } = props;
67+
const { name, clientId } = props;
6668
const boundAttributes = useSelect( () => {
6769
if ( ! bindings ) {
6870
return;
@@ -81,6 +83,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
8183
continue;
8284
}
8385

86+
const context = {};
87+
88+
if ( source.usesContext?.length ) {
89+
for ( const key of source.usesContext ) {
90+
context[ key ] = blockContext[ key ];
91+
}
92+
}
93+
8494
const args = {
8595
registry,
8696
context,
@@ -102,7 +112,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
102112
}
103113

104114
return attributes;
105-
}, [ bindings, name, clientId, context, registry, sources ] );
115+
}, [ bindings, name, clientId, registry, sources, blockContext ] );
106116

107117
const { setAttributes } = props;
108118

@@ -127,6 +137,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
127137
continue;
128138
}
129139

140+
const context = {};
141+
142+
if ( source.usesContext?.length ) {
143+
for ( const key of source.usesContext ) {
144+
context[ key ] = blockContext[ key ];
145+
}
146+
}
147+
130148
source.setValue( {
131149
registry,
132150
context,
@@ -148,7 +166,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
148166
bindings,
149167
name,
150168
clientId,
151-
context,
169+
blockContext,
152170
setAttributes,
153171
sources,
154172
]

packages/blocks/src/store/private-actions.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function registerBlockBindingsSource( source ) {
5151
type: 'REGISTER_BLOCK_BINDINGS_SOURCE',
5252
sourceName: source.name,
5353
sourceLabel: source.label,
54+
usesContext: source.usesContext,
5455
getValue: source.getValue,
5556
setValue: source.setValue,
5657
getPlaceholder: source.getPlaceholder,

packages/blocks/src/store/reducer.js

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ export function blockBindingsSources( state = {}, action ) {
389389
...state,
390390
[ action.sourceName ]: {
391391
label: action.sourceLabel,
392+
usesContext: action.usesContext,
392393
getValue: action.getValue,
393394
setValue: action.setValue,
394395
getPlaceholder: action.getPlaceholder,

packages/editor/src/bindings/post-meta.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { store as editorStore } from '../store';
1212
export default {
1313
name: 'core/post-meta',
1414
label: _x( 'Post Meta', 'block bindings source' ),
15+
usesContext: [ 'postId', 'postType' ],
1516
getPlaceholder( { args } ) {
1617
return args.key;
1718
},

0 commit comments

Comments
 (0)