Skip to content

Commit

Permalink
Editor: Track elements for more reliable drag detection
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 23, 2015
1 parent 7f09624 commit 4664e17
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions client/components/drop-zone/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* External dependencies
*/
var React = require( 'react/addons' ),
without = require( 'lodash/array/without' ),
includes = require( 'lodash/collection/includes' ),
classNames = require( 'classnames' ),
noop = require( 'lodash/utility/noop' );

Expand Down Expand Up @@ -39,7 +41,7 @@ module.exports = React.createClass( {
},

componentDidMount: function() {
this.dragEnteredCounter = 0;
this.dragEnterNodes = [];

window.addEventListener( 'dragover', this.preventDefault );
window.addEventListener( 'drop', this.onDrop );
Expand All @@ -48,8 +50,6 @@ module.exports = React.createClass( {
},

componentWillUnmount: function() {
delete this.dragEnteredCounter;

window.removeEventListener( 'dragover', this.preventDefault );
window.removeEventListener( 'drop', this.onDrop );
window.removeEventListener( 'dragenter', this.toggleDraggingOverDocument );
Expand All @@ -59,9 +59,12 @@ module.exports = React.createClass( {
toggleDraggingOverDocument: function( event ) {
var isDraggingOverDocument, detail, isValidDrag;

switch ( event.type ) {
case 'dragenter': this.dragEnteredCounter++; break;
case 'dragleave': this.dragEnteredCounter--; break;
// Track nodes that have received a drag event. So long as nodes exist
// in the set, we can assume that an item is being dragged on the page.
if ( 'dragenter' === event.type && ! includes( this.dragEnterNodes, event.target ) ) {
this.dragEnterNodes.push( event.target );
} else if ( 'dragleave' === event.type ) {
this.dragEnterNodes = without( this.dragEnterNodes, event.target );
}

// In some contexts, it may be necessary to capture and redirect the
Expand All @@ -73,7 +76,7 @@ module.exports = React.createClass( {
detail = window.CustomEvent && event instanceof window.CustomEvent ? event.detail : event;

isValidDrag = this.props.onVerifyValidTransfer( detail.dataTransfer );
isDraggingOverDocument = isValidDrag && 0 !== this.dragEnteredCounter;
isDraggingOverDocument = isValidDrag && this.dragEnterNodes.length;

this.setState( {
isDraggingOverDocument: isDraggingOverDocument,
Expand All @@ -82,10 +85,9 @@ module.exports = React.createClass( {
} );

if ( window.CustomEvent && event instanceof window.CustomEvent ) {
// For redirected CustomEvent instances, immediately decrement the
// counter following the state update, since another "real" event
// will be triggered on the `window` object immediately following.
this.dragEnteredCounter--;
// For redirected CustomEvent instances, immediately remove window
// from tracked nodes since another "real" event will be triggered.
this.dragEnterNodes = without( this.dragEnterNodes, window );
}
},

Expand Down

0 comments on commit 4664e17

Please sign in to comment.