-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock-actions-menu.native.js
363 lines (343 loc) · 9.51 KB
/
block-actions-menu.native.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* External dependencies
*/
import { Platform, findNodeHandle } from 'react-native';
import { partial, first, castArray, last, compact } from 'lodash';
/**
* WordPress dependencies
*/
import {
getClipboard,
setClipboard,
ToolbarButton,
Picker,
} from '@wordpress/components';
import {
getBlockType,
getDefaultBlockName,
serialize,
rawHandler,
createBlock,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { __, sprintf } from '@wordpress/i18n';
import { withDispatch, withSelect } from '@wordpress/data';
import { withInstanceId, compose } from '@wordpress/compose';
import { moreHorizontalMobile } from '@wordpress/icons';
import { useRef, useState } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { getMoversSetup } from '../block-mover/mover-description';
import { store as blockEditorStore } from '../../store';
import BlockTransformationsMenu from '../block-switcher/block-transformations-menu';
const BlockActionsMenu = ( {
// Select
blockTitle,
canInsertBlockType,
getBlocksByClientId,
isEmptyDefaultBlock,
isFirst,
isLast,
rootClientId,
selectedBlockClientId,
selectedBlockPossibleTransformations,
// Dispatch
createSuccessNotice,
duplicateBlock,
onMoveDown,
onMoveUp,
openGeneralSidebar,
pasteBlock,
removeBlocks,
// Passed in
anchorNodeRef,
isStackedHorizontally,
onDelete,
wrapBlockMover,
wrapBlockSettings,
} ) => {
const [ clipboard, setCurrentClipboard ] = useState( getClipboard() );
const blockActionsMenuPickerRef = useRef();
const blockTransformationMenuPickerRef = useRef();
const moversOptions = { keys: [ 'icon', 'actionTitle' ] };
const clipboardBlock = clipboard && rawHandler( { HTML: clipboard } )[ 0 ];
const isPasteEnabled =
clipboardBlock &&
canInsertBlockType( clipboardBlock.name, rootClientId );
const {
actionTitle: {
backward: backwardButtonTitle,
forward: forwardButtonTitle,
},
} = getMoversSetup( isStackedHorizontally, moversOptions );
const allOptions = {
settings: {
id: 'settingsOption',
label: __( 'Block settings' ),
value: 'settingsOption',
onSelect: openGeneralSidebar,
},
backwardButton: {
id: 'backwardButtonOption',
label: backwardButtonTitle,
value: 'backwardButtonOption',
disabled: isFirst,
onSelect: onMoveUp,
},
forwardButton: {
id: 'forwardButtonOption',
label: forwardButtonTitle,
value: 'forwardButtonOption',
disabled: isLast,
onSelect: onMoveDown,
},
delete: {
id: 'deleteOption',
label: __( 'Remove block' ),
value: 'deleteOption',
separated: true,
disabled: isEmptyDefaultBlock,
onSelect: () => {
onDelete();
createSuccessNotice(
// translators: displayed right after the block is removed.
__( 'Block removed' )
);
},
},
transformButton: {
id: 'transformButtonOption',
label: __( 'Transform block…' ),
value: 'transformButtonOption',
onSelect: () => {
if ( blockTransformationMenuPickerRef.current ) {
blockTransformationMenuPickerRef.current.presentPicker();
}
},
},
copyButton: {
id: 'copyButtonOption',
label: __( 'Copy block' ),
value: 'copyButtonOption',
onSelect: () => {
const serializedBlock = serialize(
getBlocksByClientId( selectedBlockClientId )
);
setCurrentClipboard( serializedBlock );
setClipboard( serializedBlock );
createSuccessNotice(
// translators: displayed right after the block is copied.
__( 'Block copied' )
);
},
},
cutButton: {
id: 'cutButtonOption',
label: __( 'Cut block' ),
value: 'cutButtonOption',
onSelect: () => {
setClipboard(
serialize( getBlocksByClientId( selectedBlockClientId ) )
);
removeBlocks( selectedBlockClientId );
createSuccessNotice(
// translators: displayed right after the block is cut.
__( 'Block cut' )
);
},
},
pasteButton: {
id: 'pasteButtonOption',
label: __( 'Paste block after' ),
value: 'pasteButtonOption',
onSelect: () => {
onPasteBlock();
createSuccessNotice(
// translators: displayed right after the block is pasted.
__( 'Block pasted' )
);
},
},
duplicateButton: {
id: 'duplicateButtonOption',
label: __( 'Duplicate block' ),
value: 'duplicateButtonOption',
onSelect: () => {
duplicateBlock();
createSuccessNotice(
// translators: displayed right after the block is duplicated.
__( 'Block duplicated' )
);
},
},
};
const options = compact( [
wrapBlockMover && allOptions.backwardButton,
wrapBlockMover && allOptions.forwardButton,
wrapBlockSettings && allOptions.settings,
selectedBlockPossibleTransformations.length &&
allOptions.transformButton,
allOptions.copyButton,
allOptions.cutButton,
isPasteEnabled && allOptions.pasteButton,
allOptions.duplicateButton,
allOptions.delete,
] );
function onPasteBlock() {
if ( ! clipboard ) {
return;
}
pasteBlock( rawHandler( { HTML: clipboard } )[ 0 ] );
}
function onPickerSelect( value ) {
const selectedItem = options.find( ( item ) => item.value === value );
selectedItem.onSelect();
}
function onPickerPresent() {
if ( blockActionsMenuPickerRef.current ) {
blockActionsMenuPickerRef.current.presentPicker();
}
}
const disabledButtonIndices = options
.map( ( option, index ) => option.disabled && index + 1 )
.filter( Boolean );
const accessibilityHint =
Platform.OS === 'ios'
? __( 'Double tap to open Action Sheet with available options' )
: __( 'Double tap to open Bottom Sheet with available options' );
const getAnchor = () =>
anchorNodeRef ? findNodeHandle( anchorNodeRef ) : undefined;
return (
<>
<ToolbarButton
title={ __( 'Open Block Actions Menu' ) }
onClick={ onPickerPresent }
icon={ moreHorizontalMobile }
extraProps={ {
hint: accessibilityHint,
} }
/>
<Picker
ref={ blockActionsMenuPickerRef }
options={ options }
onChange={ onPickerSelect }
destructiveButtonIndex={ options.length }
disabledButtonIndices={ disabledButtonIndices }
hideCancelButton={ Platform.OS !== 'ios' }
leftAlign={ true }
getAnchor={ getAnchor }
// translators: %s: block title e.g: "Paragraph".
title={ sprintf( __( '%s block options' ), blockTitle ) }
/>
<BlockTransformationsMenu
anchorNodeRef={ anchorNodeRef }
blockTitle={ blockTitle }
pickerRef={ blockTransformationMenuPickerRef }
possibleTransformations={ selectedBlockPossibleTransformations }
selectedBlock={ getBlocksByClientId( selectedBlockClientId ) }
selectedBlockClientId={ selectedBlockClientId }
/>
</>
);
};
export default compose(
withSelect( ( select, { clientIds } ) => {
const {
getBlockIndex,
getBlockRootClientId,
getBlockOrder,
getBlockName,
getBlockTransformItems,
getBlock,
getBlocksByClientId,
getSelectedBlockClientIds,
canInsertBlockType,
} = select( blockEditorStore );
const normalizedClientIds = castArray( clientIds );
const block = getBlock( normalizedClientIds );
const blockName = getBlockName( normalizedClientIds );
const blockType = getBlockType( blockName );
const blockTitle = blockType.title;
const firstClientId = first( normalizedClientIds );
const rootClientId = getBlockRootClientId( firstClientId );
const blockOrder = getBlockOrder( rootClientId );
const firstIndex = getBlockIndex( firstClientId, rootClientId );
const lastIndex = getBlockIndex(
last( normalizedClientIds ),
rootClientId
);
const isDefaultBlock = blockName === getDefaultBlockName();
const isEmptyContent = block.attributes.content === '';
const isExactlyOneBlock = blockOrder.length === 1;
const isEmptyDefaultBlock =
isExactlyOneBlock && isDefaultBlock && isEmptyContent;
const selectedBlockClientId = getSelectedBlockClientIds();
const selectedBlock = getBlocksByClientId( selectedBlockClientId );
const selectedBlockPossibleTransformations = getBlockTransformItems(
selectedBlock,
rootClientId
);
return {
blockTitle,
canInsertBlockType,
currentIndex: firstIndex,
getBlocksByClientId,
isEmptyDefaultBlock,
isFirst: firstIndex === 0,
isLast: lastIndex === blockOrder.length - 1,
rootClientId,
selectedBlockClientId,
selectedBlockPossibleTransformations,
};
} ),
withDispatch(
( dispatch, { clientIds, rootClientId, currentIndex }, { select } ) => {
const {
moveBlocksDown,
moveBlocksUp,
duplicateBlocks,
removeBlocks,
insertBlock,
replaceBlocks,
} = dispatch( blockEditorStore );
const { openGeneralSidebar } = dispatch( 'core/edit-post' );
const { getBlockSelectionEnd, getBlock } = select(
blockEditorStore
);
const { createSuccessNotice } = dispatch( noticesStore );
return {
createSuccessNotice,
duplicateBlock() {
return duplicateBlocks( clientIds );
},
onMoveDown: partial( moveBlocksDown, clientIds, rootClientId ),
onMoveUp: partial( moveBlocksUp, clientIds, rootClientId ),
openGeneralSidebar: () =>
openGeneralSidebar( 'edit-post/block' ),
pasteBlock: ( clipboardBlock ) => {
const canReplaceBlock = isUnmodifiedDefaultBlock(
getBlock( getBlockSelectionEnd() )
);
if ( ! canReplaceBlock ) {
const insertedBlock = createBlock(
clipboardBlock.name,
clipboardBlock.attributes,
clipboardBlock.innerBlocks
);
insertBlock(
insertedBlock,
currentIndex + 1,
rootClientId
);
} else {
replaceBlocks( clientIds, clipboardBlock );
}
},
removeBlocks,
};
}
),
withInstanceId
)( BlockActionsMenu );