-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathplugin-block-settings-menu-item.js
105 lines (99 loc) · 3.46 KB
/
plugin-block-settings-menu-item.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
/**
* External dependencies
*/
import { difference } from 'lodash';
/**
* WordPress dependencies
*/
import { MenuItem } from '@wordpress/components';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import PluginBlockSettingsMenuGroup from './plugin-block-settings-menu-group';
const isEverySelectedBlockAllowed = ( selected, allowed ) => difference( selected, allowed ).length === 0;
/**
* Plugins may want to add an item to the menu either for every block
* or only for the specific ones provided in the `allowedBlocks` component property.
*
* If there are multiple blocks selected the item will be rendered if every block
* is of one allowed type (not necessarily the same).
*
* @param {string[]} selectedBlockNames Array containing the names of the blocks selected
* @param {string[]} allowedBlockNames Array containing the names of the blocks allowed
* @return {boolean} Whether the item will be rendered or not.
*/
const shouldRenderItem = ( selectedBlockNames, allowedBlockNames ) => ! Array.isArray( allowedBlockNames ) ||
isEverySelectedBlockAllowed( selectedBlockNames, allowedBlockNames );
/**
* Renders a new item in the block settings menu.
*
* @param {Object} props Component props.
* @param {Array} [props.allowedBlockNames] An array containing a list of block names for which the item should be shown. If not present, it'll be rendered for any block. If multiple blocks are selected, it'll be shown if and only if all of them are in the whitelist.
* @param {string|Element} [props.icon] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element.
* @param {string} props.label The menu item text.
* @param {Function} props.onClick Callback function to be executed when the user click the menu item.
*
* @example <caption>ES5</caption>
* ```js
* // Using ES5 syntax
* var __ = wp.i18n.__;
* var PluginBlockSettingsMenuItem = wp.editPost.PluginBlockSettingsMenuItem;
*
* function doOnClick(){
* // To be called when the user clicks the menu item.
* }
*
* function MyPluginBlockSettingsMenuItem() {
* return wp.element.createElement(
* PluginBlockSettingsMenuItem,
* {
* allowedBlockNames: [ 'core/paragraph' ],
* icon: 'dashicon-name',
* label: __( 'Menu item text' ),
* onClick: doOnClick,
* }
* );
* }
* ```
*
* @example <caption>ESNext</caption>
* ```jsx
* // Using ESNext syntax
* import { __ } from wp.i18n;
* import { PluginBlockSettingsMenuItem } from wp.editPost;
*
* const doOnClick = ( ) => {
* // To be called when the user clicks the menu item.
* };
*
* const MyPluginBlockSettingsMenuItem = () => (
* <PluginBlockSettingsMenuItem
* allowedBlockNames=[ 'core/paragraph' ]
* icon='dashicon-name'
* label=__( 'Menu item text' )
* onClick={ doOnClick } />
* );
* ```
*
* @return {WPElement} The WPElement to be rendered.
*/
const PluginBlockSettingsMenuItem = ( { allowedBlocks, icon, label, onClick, small, role } ) => (
<PluginBlockSettingsMenuGroup>
{ ( { selectedBlocks, onClose } ) => {
if ( ! shouldRenderItem( selectedBlocks, allowedBlocks ) ) {
return null;
}
return ( <MenuItem
className="editor-block-settings-menu__control"
onClick={ compose( onClick, onClose ) }
icon={ icon || 'admin-plugins' }
label={ small ? label : undefined }
role={ role }
>
{ ! small && label }
</MenuItem> );
} }
</PluginBlockSettingsMenuGroup>
);
export default PluginBlockSettingsMenuItem;