-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
242 lines (218 loc) · 6.06 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
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
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { Draggable } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
import { throttle } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockDraggableChip from './draggable-chip';
import useScrollWhenDragging from './use-scroll-when-dragging';
import { store as blockEditorStore } from '../../store';
import { useBlockElement } from '../block-list/use-block-props/use-block-refs';
import { isDropTargetValid } from '../use-block-drop-zone';
const BlockDraggable = ( {
appendToOwnerDocument,
children,
clientIds,
cloneClassname,
elementId,
onDragStart,
onDragEnd,
fadeWhenDisabled = false,
dragComponent,
} ) => {
const {
srcRootClientId,
isDraggable,
icon,
visibleInserter,
getBlockType,
} = useSelect(
( select ) => {
const {
canMoveBlocks,
getBlockRootClientId,
getBlockName,
getBlockAttributes,
isBlockInsertionPointVisible,
} = select( blockEditorStore );
const { getBlockType: _getBlockType, getActiveBlockVariation } =
select( blocksStore );
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
const blockName = getBlockName( clientIds[ 0 ] );
const variation = getActiveBlockVariation(
blockName,
getBlockAttributes( clientIds[ 0 ] )
);
return {
srcRootClientId: rootClientId,
isDraggable: canMoveBlocks( clientIds ),
icon: variation?.icon || _getBlockType( blockName )?.icon,
visibleInserter: isBlockInsertionPointVisible(),
getBlockType: _getBlockType,
};
},
[ clientIds ]
);
const isDraggingRef = useRef( false );
const [ startScrolling, scrollOnDragOver, stopScrolling ] =
useScrollWhenDragging();
const { getAllowedBlocks, getBlockNamesByClientId, getBlockRootClientId } =
useSelect( blockEditorStore );
const { startDraggingBlocks, stopDraggingBlocks } =
useDispatch( blockEditorStore );
// Stop dragging blocks if the block draggable is unmounted.
useEffect( () => {
return () => {
if ( isDraggingRef.current ) {
stopDraggingBlocks();
}
};
}, [] );
// Find the root of the editor iframe.
const blockEl = useBlockElement( clientIds[ 0 ] );
const editorRoot = blockEl?.closest( 'body' );
/*
* Add a dragover event listener to the editor root to track the blocks being dragged over.
* The listener has to be inside the editor iframe otherwise the target isn't accessible.
*/
useEffect( () => {
if ( ! editorRoot || ! fadeWhenDisabled ) {
return;
}
const onDragOver = ( event ) => {
if ( ! event.target.closest( '[data-block]' ) ) {
return;
}
const draggedBlockNames = getBlockNamesByClientId( clientIds );
const targetClientId = event.target
.closest( '[data-block]' )
.getAttribute( 'data-block' );
const allowedBlocks = getAllowedBlocks( targetClientId );
const targetBlockName = getBlockNamesByClientId( [
targetClientId,
] )[ 0 ];
/*
* Check if the target is valid to drop in.
* If the target's allowedBlocks is an empty array,
* it isn't a container block, in which case we check
* its parent's validity instead.
*/
let dropTargetValid;
if ( allowedBlocks?.length === 0 ) {
const targetRootClientId =
getBlockRootClientId( targetClientId );
const targetRootBlockName = getBlockNamesByClientId( [
targetRootClientId,
] )[ 0 ];
const rootAllowedBlocks =
getAllowedBlocks( targetRootClientId );
dropTargetValid = isDropTargetValid(
getBlockType,
rootAllowedBlocks,
draggedBlockNames,
targetRootBlockName
);
} else {
dropTargetValid = isDropTargetValid(
getBlockType,
allowedBlocks,
draggedBlockNames,
targetBlockName
);
}
/*
* Update the body class to reflect if drop target is valid.
* This has to be done on the document body because the draggable
* chip is rendered outside of the editor iframe.
*/
if ( ! dropTargetValid && ! visibleInserter ) {
window?.document?.body?.classList?.add(
'block-draggable-invalid-drag-token'
);
} else {
window?.document?.body?.classList?.remove(
'block-draggable-invalid-drag-token'
);
}
};
const throttledOnDragOver = throttle( onDragOver, 200 );
editorRoot.addEventListener( 'dragover', throttledOnDragOver );
return () => {
editorRoot.removeEventListener( 'dragover', throttledOnDragOver );
};
}, [
clientIds,
editorRoot,
fadeWhenDisabled,
getAllowedBlocks,
getBlockNamesByClientId,
getBlockRootClientId,
getBlockType,
visibleInserter,
] );
if ( ! isDraggable ) {
return children( { draggable: false } );
}
const transferData = {
type: 'block',
srcClientIds: clientIds,
srcRootClientId,
};
return (
<Draggable
appendToOwnerDocument={ appendToOwnerDocument }
cloneClassname={ cloneClassname }
__experimentalTransferDataType="wp-blocks"
transferData={ transferData }
onDragStart={ ( event ) => {
// Defer hiding the dragged source element to the next
// frame to enable dragging.
window.requestAnimationFrame( () => {
startDraggingBlocks( clientIds );
isDraggingRef.current = true;
startScrolling( event );
if ( onDragStart ) {
onDragStart();
}
} );
} }
onDragOver={ scrollOnDragOver }
onDragEnd={ () => {
stopDraggingBlocks();
isDraggingRef.current = false;
stopScrolling();
if ( onDragEnd ) {
onDragEnd();
}
} }
__experimentalDragComponent={
// Check against `undefined` so that `null` can be used to disable
// the default drag component.
dragComponent !== undefined ? (
dragComponent
) : (
<BlockDraggableChip
count={ clientIds.length }
icon={ icon }
fadeWhenDisabled
/>
)
}
elementId={ elementId }
>
{ ( { onDraggableStart, onDraggableEnd } ) => {
return children( {
draggable: true,
onDragStart: onDraggableStart,
onDragEnd: onDraggableEnd,
} );
} }
</Draggable>
);
};
export default BlockDraggable;