-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathbehaviors.js
87 lines (79 loc) · 2.24 KB
/
behaviors.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
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createHigherOrderComponent } from '@wordpress/compose';
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { InspectorControls } from '../components';
import { store as blockEditorStore } from '../store';
/**
* TODO: Add description.
*
* @param {WPComponent} BlockEdit Original component.
*
* @return {WPComponent} Wrapped component.
*/
export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
// Only add behaviors to the core/image block.
if ( props.name !== 'core/image' ) {
return <BlockEdit { ...props } />;
}
const { behaviors: blockBehaviors } = props.attributes;
const settings = select( blockEditorStore ).getSettings();
const themeBehaviors =
settings?.__experimentalFeatures?.blocks?.[ 'core/image' ]
.behaviors;
if ( ! blockBehaviors && ! themeBehaviors ) {
return <BlockEdit { ...props } />;
}
// By default, use the block behaviors.
let behaviors = blockBehaviors;
// If the theme has behaviors, but the block does not, use the theme behaviors.
if ( ! blockBehaviors && themeBehaviors ) {
behaviors = themeBehaviors;
}
return (
<>
<BlockEdit { ...props } />
<InspectorControls group="advanced">
<SelectControl
__nextHasNoMarginBottom
label={ __( 'Behaviors' ) }
// At the moment we are only supporting one behavior (lightbox)
value={ behaviors?.lightbox || '' }
options={ Object.keys( behaviors )
.map( ( behavior ) => ( {
value: behavior,
label: behavior.toUpperCase(),
} ) )
.concat( {
value: '',
label: __( 'None' ),
} ) }
onChange={ ( nextValue ) => {
props.setAttributes( {
behaviors: {
lightbox: nextValue === '' ? false : true,
},
} );
} }
hideCancelButton={ true }
help={ __( 'Add behaviors' ) }
size="__unstable-large"
/>
</InspectorControls>
</>
);
};
}, 'withBehaviors' );
addFilter(
'editor.BlockEdit',
'core/behaviors/with-inspector-control',
withBehaviors
);