-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
block-bindings.js
349 lines (326 loc) · 8.6 KB
/
block-bindings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { privateApis as blocksPrivateApis } from '@wordpress/blocks';
import {
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
__experimentalText as Text,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalTruncate as Truncate,
__experimentalVStack as VStack,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { useContext, Fragment } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import {
canBindAttribute,
getBindableAttributes,
} from '../hooks/use-bindings-attributes';
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';
import InspectorControls from '../components/inspector-controls';
import BlockContext from '../components/block-context';
const {
DropdownMenuV2: DropdownMenu,
DropdownMenuGroupV2: DropdownMenuGroup,
DropdownMenuRadioItemV2: DropdownMenuRadioItem,
DropdownMenuItemLabelV2: DropdownMenuItemLabel,
DropdownMenuItemHelpTextV2: DropdownMenuItemHelpText,
DropdownMenuSeparatorV2: DropdownMenuSeparator,
} = unlock( componentsPrivateApis );
const useToolsPanelDropdownMenuProps = () => {
const isMobile = useViewportMatch( 'medium', '<' );
return ! isMobile
? {
popoverProps: {
placement: 'left-start',
// For non-mobile, inner sidebar width (248px) - button width (24px) - border (1px) + padding (16px) + spacing (20px)
offset: 259,
},
}
: {};
};
function BlockBindingsPanelDropdown( {
fieldsList,
addConnection,
attribute,
binding,
} ) {
const currentKey = binding?.args?.key;
return (
<>
{ Object.entries( fieldsList ).map( ( [ label, fields ], i ) => (
<Fragment key={ label }>
<DropdownMenuGroup>
{ Object.keys( fieldsList ).length > 1 && (
<Text
className="block-editor-bindings__source-label"
upperCase
variant="muted"
aria-hidden
>
{ label }
</Text>
) }
{ Object.entries( fields ).map( ( [ key, value ] ) => (
<DropdownMenuRadioItem
key={ key }
onChange={ () =>
addConnection( key, attribute )
}
name={ attribute + '-binding' }
value={ key }
checked={ key === currentKey }
>
<DropdownMenuItemLabel>
{ key }
</DropdownMenuItemLabel>
<DropdownMenuItemHelpText>
{ value }
</DropdownMenuItemHelpText>
</DropdownMenuRadioItem>
) ) }
</DropdownMenuGroup>
{ i !== Object.keys( fieldsList ).length - 1 && (
<DropdownMenuSeparator />
) }
</Fragment>
) ) }
</>
);
}
function BlockBindingsAttribute( { attribute, binding } ) {
const { source: sourceName, args } = binding || {};
const sourceProps =
unlock( blocksPrivateApis ).getBlockBindingsSource( sourceName );
return (
<VStack>
<Truncate>{ attribute }</Truncate>
{ !! binding && (
<Text
variant="muted"
className="block-editor-bindings__item-explanation"
>
<Truncate>
{ args?.key || sourceProps?.label || sourceName }
</Truncate>
</Text>
) }
</VStack>
);
}
function ReadOnlyBlockBindingsPanelItems( { bindings } ) {
return (
<>
{ Object.entries( bindings ).map( ( [ attribute, binding ] ) => (
<Item key={ attribute }>
<BlockBindingsAttribute
attribute={ attribute }
binding={ binding }
/>
</Item>
) ) }
</>
);
}
function EditableBlockBindingsPanelItems( {
attributes,
bindings,
fieldsList,
addConnection,
removeConnection,
} ) {
const isMobile = useViewportMatch( 'medium', '<' );
return (
<>
{ attributes.map( ( attribute ) => {
const binding = bindings[ attribute ];
return (
<ToolsPanelItem
key={ attribute }
hasValue={ () => !! binding }
label={ attribute }
onDeselect={ () => {
removeConnection( attribute );
} }
>
<DropdownMenu
placement={
isMobile ? 'bottom-start' : 'left-start'
}
gutter={ isMobile ? 8 : 36 }
className="block-editor-bindings__popover"
trigger={
<Item>
<BlockBindingsAttribute
attribute={ attribute }
binding={ binding }
/>
</Item>
}
>
<BlockBindingsPanelDropdown
fieldsList={ fieldsList }
addConnection={ addConnection }
attribute={ attribute }
binding={ binding }
/>
</DropdownMenu>
</ToolsPanelItem>
);
} ) }
</>
);
}
export const BlockBindingsPanel = ( { name, metadata } ) => {
const registry = useRegistry();
const blockContext = useContext( BlockContext );
const { bindings } = metadata || {};
const bindableAttributes = getBindableAttributes( name );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const filteredBindings = { ...bindings };
Object.keys( filteredBindings ).forEach( ( key ) => {
if (
! canBindAttribute( name, key ) ||
filteredBindings[ key ].source === 'core/pattern-overrides'
) {
delete filteredBindings[ key ];
}
} );
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { _id } = useSelect( ( select ) => {
const { getSelectedBlockClientId } = select( blockEditorStore );
return {
_id: getSelectedBlockClientId(),
};
}, [] );
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
return null;
}
const removeAllConnections = () => {
const newMetadata = { ...metadata };
delete newMetadata.bindings;
updateBlockAttributes( _id, {
metadata:
Object.keys( newMetadata ).length === 0
? undefined
: newMetadata,
} );
};
const addConnection = ( value, attribute ) => {
// Assuming the block expects a flat structure for its metadata attribute
const newMetadata = {
...metadata,
// Adjust this according to the actual structure expected by your block
bindings: {
...metadata?.bindings,
[ attribute ]: {
source: 'core/post-meta',
args: { key: value },
},
},
};
// Update the block's attributes with the new metadata
updateBlockAttributes( _id, {
metadata: newMetadata,
} );
};
const removeConnection = ( key ) => {
const newMetadata = { ...metadata };
if ( ! newMetadata.bindings ) {
return;
}
delete newMetadata.bindings[ key ];
if ( Object.keys( newMetadata.bindings ).length === 0 ) {
delete newMetadata.bindings;
}
updateBlockAttributes( _id, {
metadata:
Object.keys( newMetadata ).length === 0
? undefined
: newMetadata,
} );
};
const fieldsList = {};
const { getBlockBindingsSources } = unlock( blocksPrivateApis );
const registeredSources = getBlockBindingsSources();
Object.values( registeredSources ).forEach(
( { getFieldsList, label, usesContext } ) => {
if ( getFieldsList ) {
// Populate context.
const context = {};
if ( usesContext?.length ) {
for ( const key of usesContext ) {
context[ key ] = blockContext[ key ];
}
}
const sourceList = getFieldsList( {
registry,
context,
} );
// Only add source if the list is not empty.
if ( sourceList ) {
fieldsList[ label ] = { ...sourceList };
}
}
}
);
// Remove empty sources.
Object.entries( fieldsList ).forEach( ( [ key, value ] ) => {
if ( ! Object.keys( value ).length ) {
delete fieldsList[ key ];
}
} );
// Lock the UI when the experiment is not enabled or there are no fields to connect to.
const readOnly =
! window.__experimentalBlockBindingsUI ||
! Object.keys( fieldsList ).length;
if ( readOnly && Object.keys( filteredBindings ).length === 0 ) {
return null;
}
return (
<InspectorControls>
<ToolsPanel
label={ __( 'Attributes' ) }
resetAll={ () => {
removeAllConnections();
} }
dropdownMenuProps={ dropdownMenuProps }
className="block-editor-bindings__panel"
>
<ItemGroup isBordered isSeparated>
{ readOnly ? (
<ReadOnlyBlockBindingsPanelItems
bindings={ filteredBindings }
/>
) : (
<EditableBlockBindingsPanelItems
attributes={ bindableAttributes }
bindings={ filteredBindings }
fieldsList={ fieldsList }
addConnection={ addConnection }
removeConnection={ removeConnection }
/>
) }
</ItemGroup>
<Text variant="muted">
{ __( 'Attributes connected to various sources.' ) }
</Text>
</ToolsPanel>
</InspectorControls>
);
};
export default {
edit: BlockBindingsPanel,
attributeKeys: [ 'metadata' ],
hasSupport() {
return true;
},
};