-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
131 lines (123 loc) · 3.23 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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { dragHandle } from '@wordpress/icons';
import { ToolbarGroup, ToolbarItem, Button } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockDraggable from '../block-draggable';
import { BlockMoverUpButton, BlockMoverDownButton } from './button';
import { store as blockEditorStore } from '../../store';
function BlockMover( {
clientIds,
hideDragHandle,
isBlockMoverUpButtonDisabled,
isBlockMoverDownButtonDisabled,
} ) {
const {
canMove,
rootClientId,
isFirst,
isLast,
orientation,
isManualGrid,
} = useSelect(
( select ) => {
const {
getBlockIndex,
getBlockListSettings,
canMoveBlocks,
getBlockOrder,
getBlockRootClientId,
getBlockAttributes,
} = select( blockEditorStore );
const normalizedClientIds = Array.isArray( clientIds )
? clientIds
: [ clientIds ];
const firstClientId = normalizedClientIds[ 0 ];
const _rootClientId = getBlockRootClientId( firstClientId );
const firstIndex = getBlockIndex( firstClientId );
const lastIndex = getBlockIndex(
normalizedClientIds[ normalizedClientIds.length - 1 ]
);
const blockOrder = getBlockOrder( _rootClientId );
const { layout = {} } = getBlockAttributes( _rootClientId ) ?? {};
return {
canMove: canMoveBlocks( clientIds ),
rootClientId: _rootClientId,
isFirst: firstIndex === 0,
isLast: lastIndex === blockOrder.length - 1,
orientation: getBlockListSettings( _rootClientId )?.orientation,
isManualGrid:
layout.type === 'grid' &&
layout.isManualPlacement &&
window.__experimentalEnableGridInteractivity,
};
},
[ clientIds ]
);
if (
! canMove ||
( isFirst && isLast && ! rootClientId ) ||
( hideDragHandle && isManualGrid )
) {
return null;
}
return (
<ToolbarGroup
className={ clsx( 'block-editor-block-mover', {
'is-horizontal': orientation === 'horizontal',
} ) }
>
{ ! hideDragHandle && (
<BlockDraggable clientIds={ clientIds } fadeWhenDisabled>
{ ( draggableProps ) => (
<Button
__next40pxDefaultSize
icon={ dragHandle }
className="block-editor-block-mover__drag-handle"
label={ __( 'Drag' ) }
// Should not be able to tab to drag handle as this
// button can only be used with a pointer device.
tabIndex="-1"
{ ...draggableProps }
/>
) }
</BlockDraggable>
) }
{ ! isManualGrid && (
<div className="block-editor-block-mover__move-button-container">
<ToolbarItem>
{ ( itemProps ) => (
<BlockMoverUpButton
disabled={ isBlockMoverUpButtonDisabled }
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
<ToolbarItem>
{ ( itemProps ) => (
<BlockMoverDownButton
disabled={ isBlockMoverDownButtonDisabled }
clientIds={ clientIds }
{ ...itemProps }
/>
) }
</ToolbarItem>
</div>
) }
</ToolbarGroup>
);
}
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/block-mover/README.md
*/
export default BlockMover;