-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
143 lines (136 loc) · 4 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* External dependencies
*/
import { castArray, flow } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _n } from '@wordpress/i18n';
import {
Toolbar,
DropdownMenu,
MenuGroup,
MenuItem,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import BlockActions from '../block-actions';
import BlockModeToggle from './block-mode-toggle';
import BlockHTMLConvertButton from './block-html-convert-button';
import BlockUnknownConvertButton from './block-unknown-convert-button';
import __experimentalBlockSettingsMenuFirstItem from './block-settings-menu-first-item';
import __experimentalBlockSettingsMenuPluginsExtension from './block-settings-menu-plugins-extension';
const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
position: 'bottom right',
};
export function BlockSettingsMenu( { clientIds } ) {
const blockClientIds = castArray( clientIds );
const count = blockClientIds.length;
const firstBlockClientId = blockClientIds[ 0 ];
const shortcuts = useSelect( ( select ) => {
const { getShortcutRepresentation } = select( 'core/keyboard-shortcuts' );
return {
duplicate: getShortcutRepresentation( 'core/block-editor/duplicate' ),
remove: getShortcutRepresentation( 'core/block-editor/remove' ),
insertAfter: getShortcutRepresentation( 'core/block-editor/insert-after' ),
insertBefore: getShortcutRepresentation( 'core/block-editor/insert-before' ),
};
}, [] );
return (
<BlockActions clientIds={ clientIds }>
{ ( {
canDuplicate,
canInsertDefaultBlock,
isLocked,
onDuplicate,
onInsertAfter,
onInsertBefore,
onRemove,
} ) => (
<Toolbar>
<DropdownMenu
icon="ellipsis"
label={ __( 'More options' ) }
className="block-editor-block-settings-menu"
popoverProps={ POPOVER_PROPS }
>
{ ( { onClose } ) => (
<>
<MenuGroup>
<__experimentalBlockSettingsMenuFirstItem.Slot
fillProps={ { onClose } }
/>
{ count === 1 && (
<BlockUnknownConvertButton
clientId={ firstBlockClientId }
/>
) }
{ count === 1 && (
<BlockHTMLConvertButton
clientId={ firstBlockClientId }
/>
) }
{ canDuplicate && (
<MenuItem
className="block-editor-block-settings-menu__control"
onClick={ flow( onClose, onDuplicate ) }
icon="admin-page"
shortcut={ shortcuts.duplicate }
>
{ __( 'Duplicate' ) }
</MenuItem>
) }
{ canInsertDefaultBlock && (
<>
<MenuItem
className="block-editor-block-settings-menu__control"
onClick={ flow( onClose, onInsertBefore ) }
icon="insert-before"
shortcut={ shortcuts.insertBefore }
>
{ __( 'Insert Before' ) }
</MenuItem>
<MenuItem
className="block-editor-block-settings-menu__control"
onClick={ flow( onClose, onInsertAfter ) }
icon="insert-after"
shortcut={ shortcuts.insertAfter }
>
{ __( 'Insert After' ) }
</MenuItem>
</>
) }
{ count === 1 && (
<BlockModeToggle
clientId={ firstBlockClientId }
onToggle={ onClose }
/>
) }
<__experimentalBlockSettingsMenuPluginsExtension.Slot
fillProps={ { clientIds, onClose } }
/>
</MenuGroup>
<MenuGroup>
{ ! isLocked && (
<MenuItem
className="block-editor-block-settings-menu__control"
onClick={ flow( onClose, onRemove ) }
icon="trash"
shortcut={ shortcuts.remove }
>
{ _n( 'Remove Block', 'Remove Blocks', count ) }
</MenuItem>
) }
</MenuGroup>
</>
) }
</DropdownMenu>
</Toolbar>
) }
</BlockActions>
);
}
export default BlockSettingsMenu;