/
index.native.js
77 lines (65 loc) · 1.74 KB
/
index.native.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
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import UngroupIcon from './icon';
import { store as blockEditorStore } from '../../store';
export function UngroupButton( { onConvertFromGroup, isUngroupable = false } ) {
if ( ! isUngroupable ) {
return null;
}
return (
<ToolbarGroup>
<ToolbarButton
title={ __( 'Ungroup' ) }
icon={ UngroupIcon }
onClick={ onConvertFromGroup }
/>
</ToolbarGroup>
);
}
export default compose( [
withSelect( ( select ) => {
const { getSelectedBlockClientId, getBlock } = select(
blockEditorStore
);
const { getGroupingBlockName } = select( blocksStore );
const selectedId = getSelectedBlockClientId();
const selectedBlock = getBlock( selectedId );
const groupingBlockName = getGroupingBlockName();
const isUngroupable =
selectedBlock &&
selectedBlock.innerBlocks &&
!! selectedBlock.innerBlocks.length &&
selectedBlock.name === groupingBlockName;
const innerBlocks = isUngroupable ? selectedBlock.innerBlocks : [];
return {
isUngroupable,
clientId: selectedId,
innerBlocks,
};
} ),
withDispatch( ( dispatch, { clientId, innerBlocks, onToggle = noop } ) => {
const { replaceBlocks } = dispatch( blockEditorStore );
return {
onConvertFromGroup() {
if ( ! innerBlocks.length ) {
return;
}
replaceBlocks( clientId, innerBlocks );
onToggle();
},
};
} ),
] )( UngroupButton );