-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
144 lines (126 loc) · 4.26 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
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback, useState } from '@wordpress/element';
import {
useThrottle,
__experimentalUseDropZone as useDropZone,
} from '@wordpress/compose';
/**
* Internal dependencies
*/
import useOnBlockDrop from '../use-on-block-drop';
import { getDistanceToNearestEdge } from '../../utils/math';
import { store as blockEditorStore } from '../../store';
/** @typedef {import('../../utils/math').WPPoint} WPPoint */
/**
* The orientation of a block list.
*
* @typedef {'horizontal'|'vertical'|undefined} WPBlockListOrientation
*/
/**
* Given a list of block DOM elements finds the index that a block should be dropped
* at.
*
* @param {Element[]} elements Array of DOM elements that represent each block in a block list.
* @param {WPPoint} position The position of the item being dragged.
* @param {WPBlockListOrientation} orientation The orientation of a block list.
*
* @return {number|undefined} The block index that's closest to the drag position.
*/
export function getNearestBlockIndex( elements, position, orientation ) {
const allowedEdges =
orientation === 'horizontal'
? [ 'left', 'right' ]
: [ 'top', 'bottom' ];
let candidateIndex;
let candidateDistance;
elements.forEach( ( element, index ) => {
// Ensure the element is a block. It should have the `wp-block` class.
if ( ! element.classList.contains( 'wp-block' ) ) {
return;
}
const rect = element.getBoundingClientRect();
const [ distance, edge ] = getDistanceToNearestEdge(
position,
rect,
allowedEdges
);
if ( candidateDistance === undefined || distance < candidateDistance ) {
// If the user is dropping to the trailing edge of the block
// add 1 to the index to represent dragging after.
const isTrailingEdge = edge === 'bottom' || edge === 'right';
const offset = isTrailingEdge ? 1 : 0;
// Update the currently known best candidate.
candidateDistance = distance;
candidateIndex = index + offset;
}
} );
return candidateIndex;
}
/**
* @typedef {Object} WPBlockDropZoneConfig
* @property {string} rootClientId The root client id for the block list.
*/
/**
* A React hook that can be used to make a block list handle drag and drop.
*
* @param {WPBlockDropZoneConfig} dropZoneConfig configuration data for the drop zone.
*/
export default function useBlockDropZone( {
// An undefined value represents a top-level block. Default to an empty
// string for this so that `targetRootClientId` can be easily compared to
// values returned by the `getRootBlockClientId` selector, which also uses
// an empty string to represent top-level blocks.
rootClientId: targetRootClientId = '',
} = {} ) {
const [ targetBlockIndex, setTargetBlockIndex ] = useState( null );
const isLockedAll = useSelect(
( select ) => {
const { getTemplateLock } = select( blockEditorStore );
return getTemplateLock( targetRootClientId ) === 'all';
},
[ targetRootClientId ]
);
const { getBlockListSettings } = useSelect( blockEditorStore );
const { showInsertionPoint, hideInsertionPoint } = useDispatch(
blockEditorStore
);
const onBlockDrop = useOnBlockDrop( targetRootClientId, targetBlockIndex );
const throttled = useThrottle(
useCallback( ( event, currentTarget ) => {
const blockElements = Array.from( currentTarget.children );
const targetIndex = getNearestBlockIndex(
blockElements,
{ x: event.clientX, y: event.clientY },
getBlockListSettings( targetRootClientId )?.orientation
);
setTargetBlockIndex( targetIndex === undefined ? 0 : targetIndex );
if ( targetIndex !== null ) {
showInsertionPoint( targetRootClientId, targetIndex );
}
}, [] ),
200
);
return useDropZone( {
isDisabled: isLockedAll,
onDrop: onBlockDrop,
onDragOver( event ) {
// `currentTarget` is only available while the event is being
// handled, so get it now and pass it to the thottled function.
// https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget
throttled( event, event.currentTarget );
},
onDragLeave() {
throttled.cancel();
hideInsertionPoint();
setTargetBlockIndex( null );
},
onDragEnd() {
throttled.cancel();
hideInsertionPoint();
setTargetBlockIndex( null );
},
} );
}