-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathblock-support-tools-panel.js
77 lines (67 loc) · 1.91 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
77
/**
* WordPress dependencies
*/
import { __experimentalToolsPanel as ToolsPanel } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { cleanEmptyObject } from '../../hooks/utils';
export default function BlockSupportToolsPanel( { children, group, label } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const {
getBlockAttributes,
getMultiSelectedBlockClientIds,
getSelectedBlockClientId,
hasMultiSelection,
} = useSelect( blockEditorStore );
const panelId = getSelectedBlockClientId();
const resetAll = useCallback(
( resetFilters = [] ) => {
const newAttributes = {};
const clientIds = hasMultiSelection()
? getMultiSelectedBlockClientIds()
: [ panelId ];
clientIds.forEach( ( clientId ) => {
const { style } = getBlockAttributes( clientId );
let newBlockAttributes = { style };
resetFilters.forEach( ( resetFilter ) => {
newBlockAttributes = {
...newBlockAttributes,
...resetFilter( newBlockAttributes ),
};
} );
// Enforce a cleaned style object.
newBlockAttributes = {
...newBlockAttributes,
style: cleanEmptyObject( newBlockAttributes.style ),
};
newAttributes[ clientId ] = newBlockAttributes;
} );
updateBlockAttributes( clientIds, newAttributes, true );
},
[
cleanEmptyObject,
getBlockAttributes,
getMultiSelectedBlockClientIds,
hasMultiSelection,
panelId,
updateBlockAttributes,
]
);
return (
<ToolsPanel
className={ `${ group }-block-support-panel` }
label={ label }
resetAll={ resetAll }
key={ panelId }
panelId={ panelId }
hasInnerWrapper={ true }
shouldRenderPlaceholderItems={ true } // Required to maintain fills ordering.
>
{ children }
</ToolsPanel>
);
}