-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
provider.js
236 lines (199 loc) · 6.87 KB
/
provider.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
/**
* External dependencies
*/
import { isEqual, find, some, filter, throttle, includes } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, createContext } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';
const { Provider, Consumer } = createContext( {
addDropZone: () => {},
removeDropZone: () => {},
} );
const getDragEventType = ( { dataTransfer } ) => {
if ( dataTransfer ) {
// Use lodash `includes` here as in the Edge browser `types` is implemented
// as a DomStringList, whereas in other browsers it's an array. `includes`
// happily works with both types.
if ( includes( dataTransfer.types, 'Files' ) ) {
return 'file';
}
if ( includes( dataTransfer.types, 'text/html' ) ) {
return 'html';
}
}
return 'default';
};
const isTypeSupportedByDropZone = ( type, dropZone ) => {
return ( type === 'file' && dropZone.onFilesDrop ) ||
( type === 'html' && dropZone.onHTMLDrop ) ||
( type === 'default' && dropZone.onDrop );
};
const isWithinElementBounds = ( element, x, y ) => {
const rect = element.getBoundingClientRect();
/// make sure the rect is a valid rect
if ( rect.bottom === rect.top || rect.left === rect.right ) {
return false;
}
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
};
class DropZoneProvider extends Component {
constructor() {
super( ...arguments );
// Event listeners
this.onDragOver = this.onDragOver.bind( this );
this.onDrop = this.onDrop.bind( this );
// Context methods so this component can receive data from consumers
this.addDropZone = this.addDropZone.bind( this );
this.removeDropZone = this.removeDropZone.bind( this );
// Utility methods
this.resetDragState = this.resetDragState.bind( this );
this.toggleDraggingOverDocument = throttle( this.toggleDraggingOverDocument.bind( this ), 200 );
this.dropZones = [];
this.dropZoneCallbacks = {
addDropZone: this.addDropZone,
removeDropZone: this.removeDropZone,
};
this.state = {
hoveredDropZone: -1,
isDraggingOverDocument: false,
position: null,
};
}
componentDidMount() {
window.addEventListener( 'dragover', this.onDragOver );
window.addEventListener( 'mouseup', this.resetDragState );
}
componentWillUnmount() {
window.removeEventListener( 'dragover', this.onDragOver );
window.removeEventListener( 'mouseup', this.resetDragState );
}
addDropZone( dropZone ) {
this.dropZones.push( dropZone );
}
removeDropZone( dropZone ) {
this.dropZones = filter( this.dropZones, ( dz ) => dz !== dropZone );
}
resetDragState() {
// Avoid throttled drag over handler calls
this.toggleDraggingOverDocument.cancel();
const { isDraggingOverDocument, hoveredDropZone } = this.state;
if ( ! isDraggingOverDocument && hoveredDropZone === -1 ) {
return;
}
this.setState( {
hoveredDropZone: -1,
isDraggingOverDocument: false,
position: null,
} );
this.dropZones.forEach( ( dropZone ) => dropZone.setState( {
isDraggingOverDocument: false,
isDraggingOverElement: false,
position: null,
type: null,
} ) );
}
toggleDraggingOverDocument( event, dragEventType ) {
// In some contexts, it may be necessary to capture and redirect the
// drag event (e.g. atop an `iframe`). To accommodate this, you can
// create an instance of CustomEvent with the original event specified
// as the `detail` property.
//
// See: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
const detail = window.CustomEvent && event instanceof window.CustomEvent ? event.detail : event;
// Index of hovered dropzone.
const hoveredDropZones = filter( this.dropZones, ( dropZone ) =>
isTypeSupportedByDropZone( dragEventType, dropZone ) &&
isWithinElementBounds( dropZone.element, detail.clientX, detail.clientY )
);
// Find the leaf dropzone not containing another dropzone
const hoveredDropZone = find( hoveredDropZones, ( zone ) => (
! some( hoveredDropZones, ( subZone ) => subZone !== zone && zone.element.parentElement.contains( subZone.element ) )
) );
const hoveredDropZoneIndex = this.dropZones.indexOf( hoveredDropZone );
let position = null;
if ( hoveredDropZone ) {
const rect = hoveredDropZone.element.getBoundingClientRect();
position = {
x: detail.clientX - rect.left < rect.right - detail.clientX ? 'left' : 'right',
y: detail.clientY - rect.top < rect.bottom - detail.clientY ? 'top' : 'bottom',
};
}
// Optimisation: Only update the changed dropzones
let toUpdate = [];
if ( ! this.state.isDraggingOverDocument ) {
toUpdate = this.dropZones;
} else if ( hoveredDropZoneIndex !== this.state.hoveredDropZone ) {
if ( this.state.hoveredDropZone !== -1 ) {
toUpdate.push( this.dropZones[ this.state.hoveredDropZone ] );
}
if ( hoveredDropZone ) {
toUpdate.push( hoveredDropZone );
}
} else if (
hoveredDropZone &&
hoveredDropZoneIndex === this.state.hoveredDropZone &&
! isEqual( position, this.state.position )
) {
toUpdate.push( hoveredDropZone );
}
// Notifying the dropzones
toUpdate.forEach( ( dropZone ) => {
const index = this.dropZones.indexOf( dropZone );
const isDraggingOverDropZone = index === hoveredDropZoneIndex;
dropZone.setState( {
isDraggingOverDocument: isTypeSupportedByDropZone( dragEventType, dropZone ),
isDraggingOverElement: isDraggingOverDropZone,
position: isDraggingOverDropZone ? position : null,
type: isDraggingOverDropZone ? dragEventType : null,
} );
} );
const newState = {
isDraggingOverDocument: true,
hoveredDropZone: hoveredDropZoneIndex,
position,
};
if ( ! isShallowEqual( newState, this.state ) ) {
this.setState( newState );
}
}
onDragOver( event ) {
this.toggleDraggingOverDocument( event, getDragEventType( event ) );
event.preventDefault();
}
onDrop( event ) {
// This seemingly useless line has been shown to resolve a Safari issue
// where files dragged directly from the dock are not recognized
event.dataTransfer && event.dataTransfer.files.length; // eslint-disable-line no-unused-expressions
const { position, hoveredDropZone } = this.state;
const dragEventType = getDragEventType( event );
const dropZone = this.dropZones[ hoveredDropZone ];
this.resetDragState();
if ( dropZone ) {
switch ( dragEventType ) {
case 'file':
dropZone.onFilesDrop( [ ...event.dataTransfer.files ], position );
break;
case 'html':
dropZone.onHTMLDrop( event.dataTransfer.getData( 'text/html' ), position );
break;
case 'default':
dropZone.onDrop( event, position );
}
}
event.stopPropagation();
event.preventDefault();
}
render() {
return (
<div onDrop={ this.onDrop } className="components-drop-zone__provider">
<Provider value={ this.dropZoneCallbacks }>
{ this.props.children }
</Provider>
</div>
);
}
}
export default DropZoneProvider;
export { Consumer as DropZoneConsumer };