-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.native.js
79 lines (74 loc) · 1.94 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
78
79
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { switchToBlockType } from '@wordpress/blocks';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import useConvertToGroupButtonProps from './use-convert-to-group-button-props';
function useConvertToGroupButtons( {
clientIds,
onUngroup,
blocksSelection,
groupingBlockName,
} ) {
const { replaceBlocks } = useDispatch( blockEditorStore );
const { createSuccessNotice } = useDispatch( noticesStore );
const onConvertToGroup = () => {
// Activate the `transform` on the Grouping Block which does the conversion.
const newBlocks = switchToBlockType(
blocksSelection,
groupingBlockName
);
if ( newBlocks ) {
replaceBlocks( clientIds, newBlocks );
}
};
const onConvertFromGroup = () => {
let innerBlocks = blocksSelection[ 0 ].innerBlocks;
if ( ! innerBlocks.length ) {
return;
}
if ( onUngroup ) {
innerBlocks = onUngroup(
blocksSelection[ 0 ].attributes,
blocksSelection[ 0 ].innerBlocks
);
}
replaceBlocks( clientIds, innerBlocks );
};
return {
group: {
id: 'groupButtonOption',
label: _x( 'Group', 'verb' ),
value: 'groupButtonOption',
onSelect: () => {
onConvertToGroup();
createSuccessNotice(
// translators: displayed right after the block is grouped
__( 'Block grouped' )
);
},
},
ungroup: {
id: 'ungroupButtonOption',
label: _x(
'Ungroup',
'Ungrouping blocks from within a grouping block back into individual blocks within the Editor'
),
value: 'ungroupButtonOption',
onSelect: () => {
onConvertFromGroup();
createSuccessNotice(
// translators: displayed right after the block is ungrouped.
__( 'Block ungrouped' )
);
},
},
};
}
export { useConvertToGroupButtons, useConvertToGroupButtonProps };