-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
reusable-block-convert-button.js
115 lines (102 loc) · 2.88 KB
/
reusable-block-convert-button.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
/**
* External dependencies
*/
import { noop, every } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { hasBlockSupport, isReusableBlock } from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
export function ReusableBlockConvertButton( {
isVisible,
isReusable,
onConvertToStatic,
onConvertToReusable,
} ) {
if ( ! isVisible ) {
return null;
}
return (
<Fragment>
{ ! isReusable && (
<MenuItem
className="editor-block-settings-menu__control block-editor-block-settings-menu__control"
icon="controls-repeat"
onClick={ onConvertToReusable }
>
{ __( 'Add to Reusable Blocks' ) }
</MenuItem>
) }
{ isReusable && (
<MenuItem
className="editor-block-settings-menu__control block-editor-block-settings-menu__control"
icon="controls-repeat"
onClick={ onConvertToStatic }
>
{ __( 'Convert to Regular Block' ) }
</MenuItem>
) }
</Fragment>
);
}
export default compose( [
withSelect( ( select, { clientIds } ) => {
const {
getBlocksByClientId,
canInsertBlockType,
} = select( 'core/block-editor' );
const {
__experimentalGetReusableBlock: getReusableBlock,
} = select( 'core/editor' );
const { canUser } = select( 'core' );
const blocks = getBlocksByClientId( clientIds );
const isReusable = (
blocks.length === 1 &&
blocks[ 0 ] &&
isReusableBlock( blocks[ 0 ] ) &&
!! getReusableBlock( blocks[ 0 ].attributes.ref )
);
// Show 'Convert to Regular Block' when selected block is a reusable block
const isVisible = isReusable || (
// Hide 'Add to Reusable Blocks' when reusable blocks are disabled
canInsertBlockType( 'core/block' ) &&
every( blocks, ( block ) => (
// Guard against the case where a regular block has *just* been converted
!! block &&
// Hide 'Add to Reusable Blocks' on invalid blocks
block.isValid &&
// Hide 'Add to Reusable Blocks' when block doesn't support being made reusable
hasBlockSupport( block.name, 'reusable', true )
) ) &&
// Hide 'Add to Reusable Blocks' when current doesn't have permission to do that
!! canUser( 'create', 'blocks' )
);
return {
isReusable,
isVisible,
};
} ),
withDispatch( ( dispatch, { clientIds, onToggle = noop } ) => {
const {
__experimentalConvertBlockToReusable: convertBlockToReusable,
__experimentalConvertBlockToStatic: convertBlockToStatic,
} = dispatch( 'core/editor' );
return {
onConvertToStatic() {
if ( clientIds.length !== 1 ) {
return;
}
convertBlockToStatic( clientIds[ 0 ] );
onToggle();
},
onConvertToReusable() {
convertBlockToReusable( clientIds );
onToggle();
},
};
} ),
] )( ReusableBlockConvertButton );