4
4
import { store as blocksStore } from '@wordpress/blocks' ;
5
5
import { createHigherOrderComponent } from '@wordpress/compose' ;
6
6
import { useRegistry , useSelect } from '@wordpress/data' ;
7
- import { useCallback } from '@wordpress/element' ;
7
+ import { useCallback , useContext } from '@wordpress/element' ;
8
8
import { addFilter } from '@wordpress/hooks' ;
9
9
10
10
/**
11
11
* Internal dependencies
12
12
*/
13
13
import { unlock } from '../lock-unlock' ;
14
+ import BlockContext from '../components/block-context' ;
14
15
15
16
/** @typedef {import('@wordpress/compose').WPHigherOrderComponent } WPHigherOrderComponent */
16
17
/** @typedef {import('@wordpress/blocks').WPBlockSettings } WPBlockSettings */
@@ -58,11 +59,12 @@ export function canBindAttribute( blockName, attributeName ) {
58
59
export const withBlockBindingSupport = createHigherOrderComponent (
59
60
( BlockEdit ) => ( props ) => {
60
61
const registry = useRegistry ( ) ;
62
+ const blockContext = useContext ( BlockContext ) ;
61
63
const sources = useSelect ( ( select ) =>
62
64
unlock ( select ( blocksStore ) ) . getAllBlockBindingsSources ( )
63
65
) ;
64
66
const bindings = props . attributes . metadata ?. bindings ;
65
- const { name, clientId, context } = props ;
67
+ const { name, clientId } = props ;
66
68
const boundAttributes = useSelect ( ( ) => {
67
69
if ( ! bindings ) {
68
70
return ;
@@ -81,6 +83,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
81
83
continue ;
82
84
}
83
85
86
+ const context = { } ;
87
+
88
+ if ( source . usesContext ?. length ) {
89
+ for ( const key of source . usesContext ) {
90
+ context [ key ] = blockContext [ key ] ;
91
+ }
92
+ }
93
+
84
94
const args = {
85
95
registry,
86
96
context,
@@ -102,7 +112,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
102
112
}
103
113
104
114
return attributes ;
105
- } , [ bindings , name , clientId , context , registry , sources ] ) ;
115
+ } , [ bindings , name , clientId , registry , sources , blockContext ] ) ;
106
116
107
117
const { setAttributes } = props ;
108
118
@@ -127,6 +137,14 @@ export const withBlockBindingSupport = createHigherOrderComponent(
127
137
continue ;
128
138
}
129
139
140
+ const context = { } ;
141
+
142
+ if ( source . usesContext ?. length ) {
143
+ for ( const key of source . usesContext ) {
144
+ context [ key ] = blockContext [ key ] ;
145
+ }
146
+ }
147
+
130
148
source . setValue ( {
131
149
registry,
132
150
context,
@@ -148,7 +166,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
148
166
bindings ,
149
167
name ,
150
168
clientId ,
151
- context ,
169
+ blockContext ,
152
170
setAttributes ,
153
171
sources ,
154
172
]
0 commit comments