-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock-bindings.js
99 lines (91 loc) · 2.42 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { store as blocksStore } from '@wordpress/blocks';
import {
BaseControl,
PanelBody,
__experimentalHStack as HStack,
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { canBindAttribute } from '../hooks/use-bindings-attributes';
import { unlock } from '../lock-unlock';
import InspectorControls from '../components/inspector-controls';
export const BlockBindingsPanel = ( { name, metadata } ) => {
const { bindings } = metadata || {};
const { sources } = useSelect( ( select ) => {
const _sources = unlock(
select( blocksStore )
).getAllBlockBindingsSources();
return {
sources: _sources,
};
}, [] );
if ( ! bindings ) {
return null;
}
// Don't show not allowed attributes.
// Don't show the bindings connected to pattern overrides in the inspectors panel.
// TODO: Explore if this should be abstracted to let other sources decide.
const filteredBindings = { ...bindings };
Object.keys( filteredBindings ).forEach( ( key ) => {
if (
! canBindAttribute( name, key ) ||
filteredBindings[ key ].source === 'core/pattern-overrides'
) {
delete filteredBindings[ key ];
}
} );
if ( Object.keys( filteredBindings ).length === 0 ) {
return null;
}
return (
<InspectorControls>
<PanelBody
title={ __( 'Attributes' ) }
className="components-panel__block-bindings-panel"
>
<BaseControl
help={ __( 'Attributes connected to various sources.' ) }
>
<ItemGroup isBordered isSeparated size="large">
{ Object.keys( filteredBindings ).map( ( key ) => {
return (
<Item key={ key }>
<HStack>
<span>{ key }</span>
<span className="components-item__block-bindings-source">
{ sources[
filteredBindings[ key ].source
]
? sources[
filteredBindings[ key ]
.source
].label
: filteredBindings[ key ]
.source }
</span>
</HStack>
</Item>
);
} ) }
</ItemGroup>
</BaseControl>
</PanelBody>
</InspectorControls>
);
};
export default {
edit: BlockBindingsPanel,
attributeKeys: [ 'metadata' ],
isMatch: ( { metadata } ) => !! metadata?.bindings,
hasSupport() {
return true;
},
};