-
Notifications
You must be signed in to change notification settings - Fork 4k
/
block-support-tools-panel.js
76 lines (67 loc) · 1.73 KB
/
block-support-tools-panel.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
/**
* WordPress dependencies
*/
import { __experimentalToolsPanel as ToolsPanel } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { cleanEmptyObject } from '../../hooks/utils';
const labels = {
border: {
label: __( 'Border options' ),
header: __( 'Border' ),
},
color: {
label: __( 'Color options' ),
header: __( 'Color' ),
},
dimensions: {
label: __( 'Dimensions options' ),
header: __( 'Dimensions' ),
},
typography: {
label: __( 'Typography options' ),
header: __( 'Typography' ),
},
};
export default function BlockSupportToolsPanel( { group, children } ) {
const { clientId, attributes } = useSelect( ( select ) => {
const { getBlockAttributes, getSelectedBlockClientId } = select(
blockEditorStore
);
const selectedBlockClientId = getSelectedBlockClientId();
return {
clientId: selectedBlockClientId,
attributes: getBlockAttributes( selectedBlockClientId ),
};
} );
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const resetAll = ( resetFilters = [] ) => {
const { style } = attributes;
let newAttributes = { style };
resetFilters.forEach( ( resetFilter ) => {
newAttributes = {
...newAttributes,
...resetFilter( newAttributes ),
};
} );
// Enforce a cleaned style object.
newAttributes = {
...newAttributes,
style: cleanEmptyObject( newAttributes.style ),
};
updateBlockAttributes( clientId, newAttributes );
};
return (
<ToolsPanel
label={ labels[ group ].label }
header={ labels[ group ].header }
resetAll={ resetAll }
>
{ children }
</ToolsPanel>
);
}