11import React , { Component } from 'react'
2+ import debounce from 'lodash/debounce'
23import { dispatch } from '../store'
34import mobxStore from '../mobxStore'
45import { observer } from 'mobx-react'
@@ -8,6 +9,7 @@ import * as PaneActions from './Pane/actions'
89import PaneState from './Pane/state'
910import * as TabActions from 'components/Tab/actions'
1011import * as FileTreeActions from './FileTree/actions'
12+ import FileTreeState from './FileTree/state'
1113
1214// Corner case: file dragging doesn't trigger 'dragend' natively
1315// so need to patch for this behavior
@@ -20,6 +22,12 @@ class DragAndDrop extends Component {
2022
2123 constructor ( props ) {
2224 super ( props )
25+ this . state = {
26+ dragPos : {
27+ x : 0 ,
28+ y : 0 ,
29+ }
30+ }
2331 }
2432
2533 render ( ) {
@@ -51,19 +59,41 @@ class DragAndDrop extends Component {
5159 }
5260
5361 onDragLeave = ( e ) => {
62+ this . resetDragPos ( )
5463 e . preventDefault ( )
5564 if ( isFileDragEnd ( e ) ) dnd . dragEnd ( )
5665 }
5766
5867 onDragOver = ( e ) => {
5968 e . preventDefault ( )
60- const { source, droppables = [ ] , meta } = dnd
61- if ( ! source ) {
62- dnd . dragStart ( {
63- type : 'EXTERNAL_FILE' ,
64- id : Date . now ( ) ,
65- } )
69+ const dx = this . state . dragPos . x - e . x
70+ const dy = this . state . dragPos . y - e . y
71+ if ( ( dx < - 5 || dx > 5 ) || ( dy < - 5 || dy > 5 ) ) {
72+ const { source } = dnd
73+ this . state . dragPos = {
74+ x : e . x ,
75+ y : e . y ,
76+ }
77+ if ( ! source || ! source . id ) {
78+ dnd . dragStart ( {
79+ type : 'EXTERNAL_FILE' ,
80+ id : Date . now ( ) ,
81+ } )
82+ }
83+ this . handleDropOver ( e )
6684 }
85+ }
86+
87+ resetDragPos = ( ) => {
88+ this . state . dragPos = {
89+ x : 0 ,
90+ y : 0 ,
91+ }
92+ }
93+
94+ handleDropOver = debounce ( ( e ) => {
95+ dnd . updateDroppables ( )
96+ const { source, droppables = [ ] , meta } = dnd
6797 const [ oX , oY ] = [ e . pageX , e . pageY ]
6898 const target = droppables . reduce ( ( result , droppable ) => {
6999 const { top, left, right, bottom} = droppable . rect
@@ -89,9 +119,10 @@ class DragAndDrop extends Component {
89119
90120 default :
91121 }
92- }
122+ } , 1000 )
93123
94124 onDrop = ( e ) => {
125+ this . resetDragPos ( )
95126 e . preventDefault ( )
96127 const { source, target, meta } = dnd
97128 if ( ! source || ! target ) return
@@ -121,6 +152,7 @@ class DragAndDrop extends Component {
121152 }
122153
123154 onDragEnd = ( e ) => {
155+ this . resetDragPos ( )
124156 e . preventDefault ( )
125157 dnd . dragEnd ( )
126158 }
@@ -135,12 +167,13 @@ class DragAndDrop extends Component {
135167 }
136168
137169 dragTabOverFileTree ( e , target ) {
138- const id = target . id
139- if ( id . split ( '_' ) [ 0 ] === 'folder' ) {
140- const DOMNode = document . getElementById ( target . id )
141- DOMNode . firstChild . click ( )
170+ const { id , DOMNode } = target
171+ const node = FileTreeState . entities . get ( id )
172+ if ( node . isDir && node . isFolded ) {
173+ FileTreeActions . openNode ( node )
142174 }
143175 }
176+
144177 dragTabOverPane ( e , target ) {
145178 if ( target . type !== 'PANE' ) return
146179 const { source, meta } = dnd
0 commit comments