-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock-settings-dropdown.js
205 lines (194 loc) · 5.12 KB
/
block-settings-dropdown.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* External dependencies
*/
import { castArray, flow, noop } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { moreVertical } from '@wordpress/icons';
import { Children, cloneElement, useCallback } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { useCopyToClipboard } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockActions from '../block-actions';
import BlockModeToggle from './block-mode-toggle';
import BlockHTMLConvertButton from './block-html-convert-button';
import __unstableBlockSettingsMenuFirstItem from './block-settings-menu-first-item';
import BlockSettingsMenuControls from '../block-settings-menu-controls';
import { store as blockEditorStore } from '../../store';
const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
position: 'bottom right',
isAlternate: true,
};
function CopyMenuItem( { blocks, onCopy } ) {
const ref = useCopyToClipboard( () => serialize( blocks ), onCopy );
return <MenuItem ref={ ref }>{ __( 'Copy' ) }</MenuItem>;
}
export function BlockSettingsDropdown( {
clientIds,
__experimentalSelectBlock,
children,
...props
} ) {
const blockClientIds = castArray( clientIds );
const count = blockClientIds.length;
const firstBlockClientId = blockClientIds[ 0 ];
const onlyBlock = useSelect(
( select ) => 1 === select( blockEditorStore ).getBlockCount(),
[]
);
const shortcuts = useSelect( ( select ) => {
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
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'
),
};
}, [] );
const updateSelection = useCallback(
__experimentalSelectBlock
? async ( clientIdsPromise ) => {
const ids = await clientIdsPromise;
if ( ids && ids[ 0 ] ) {
__experimentalSelectBlock( ids[ 0 ] );
}
}
: noop,
[ __experimentalSelectBlock ]
);
const removeBlockLabel =
count === 1 ? __( 'Remove block' ) : __( 'Remove blocks' );
return (
<BlockActions
clientIds={ clientIds }
__experimentalUpdateSelection={ ! __experimentalSelectBlock }
>
{ ( {
canDuplicate,
canInsertDefaultBlock,
isLocked,
onDuplicate,
onInsertAfter,
onInsertBefore,
onRemove,
onCopy,
onMoveTo,
blocks,
} ) => (
<DropdownMenu
icon={ moreVertical }
label={ __( 'Options' ) }
className="block-editor-block-settings-menu"
popoverProps={ POPOVER_PROPS }
noIcons
{ ...props }
>
{ ( { onClose } ) => (
<>
<MenuGroup>
<__unstableBlockSettingsMenuFirstItem.Slot
fillProps={ { onClose } }
/>
{ count === 1 && (
<BlockHTMLConvertButton
clientId={ firstBlockClientId }
/>
) }
<CopyMenuItem
blocks={ blocks }
onCopy={ onCopy }
/>
{ canDuplicate && (
<MenuItem
onClick={ flow(
onClose,
onDuplicate,
updateSelection
) }
shortcut={ shortcuts.duplicate }
>
{ __( 'Duplicate' ) }
</MenuItem>
) }
{ canInsertDefaultBlock && (
<>
<MenuItem
onClick={ flow(
onClose,
onInsertBefore
) }
shortcut={ shortcuts.insertBefore }
>
{ __( 'Insert before' ) }
</MenuItem>
<MenuItem
onClick={ flow(
onClose,
onInsertAfter
) }
shortcut={ shortcuts.insertAfter }
>
{ __( 'Insert after' ) }
</MenuItem>
</>
) }
{ ! isLocked && (
<MenuItem
onClick={ flow( onClose, onMoveTo ) }
disabled={ onlyBlock }
>
{ __( 'Move to' ) }
</MenuItem>
) }
{ count === 1 && (
<BlockModeToggle
clientId={ firstBlockClientId }
onToggle={ onClose }
/>
) }
</MenuGroup>
<BlockSettingsMenuControls.Slot
fillProps={ { onClose } }
clientIds={ clientIds }
/>
{ typeof children === 'function'
? children( { onClose } )
: Children.map( ( child ) =>
cloneElement( child, { onClose } )
) }
<MenuGroup>
{ ! isLocked && (
<MenuItem
onClick={ flow(
onClose,
onRemove,
updateSelection
) }
shortcut={ shortcuts.remove }
>
{ removeBlockLabel }
</MenuItem>
) }
</MenuGroup>
</>
) }
</DropdownMenu>
) }
</BlockActions>
);
}
export default BlockSettingsDropdown;