1
1
import * as React from "react" ;
2
- import { Map } from "immutable" ;
2
+ import { Map , List } from "immutable" ;
3
+ import { throttle } from "throttle-debounce" ;
3
4
4
5
import { ICoreState , MsgProps , UIProps } from "./core_state" ;
5
6
import { FilesPanel , FilesProps } from "./panel_files" ;
@@ -13,8 +14,10 @@ import { AdminProps } from "./pane_admin";
13
14
import { TopBar } from "./topbar" ;
14
15
import { CronJobs } from "../common/cron" ;
15
16
import { updater } from "./state_updater" ;
17
+ import { dropAreaCtrl , ctrlOn , ctrlOff } from "../common/controls" ;
16
18
17
19
export const controlName = "panelTabs" ;
20
+ const dragOverthrottlePeriod = 200 ;
18
21
export interface Props {
19
22
filesInfo : FilesProps ;
20
23
uploadingsInfo : UploadingsProps ;
@@ -26,10 +29,16 @@ export interface Props {
26
29
update ?: ( updater : ( prevState : ICoreState ) => ICoreState ) => void ;
27
30
}
28
31
29
- export interface State { }
32
+ export interface State {
33
+ lastDragOverTime : number ;
34
+ }
30
35
export class RootFrame extends React . Component < Props , State , { } > {
36
+ private filesPanelRef : FilesPanel ;
31
37
constructor ( p : Props ) {
32
38
super ( p ) ;
39
+ this . state = {
40
+ lastDragOverTime : 0 ,
41
+ } ;
33
42
}
34
43
35
44
componentDidMount ( ) : void {
@@ -38,12 +47,22 @@ export class RootFrame extends React.Component<Props, State, {}> {
38
47
args : [ ] ,
39
48
delay : 60 * 1000 ,
40
49
} ) ;
50
+
51
+ CronJobs ( ) . setInterval ( "endDrag" , {
52
+ func : this . endDrag ,
53
+ args : [ ] ,
54
+ delay : dragOverthrottlePeriod * 2 ,
55
+ } ) ;
41
56
}
42
57
43
58
componentWillUnmount ( ) {
44
59
CronJobs ( ) . clearInterval ( "autoSwitchTheme" ) ;
45
60
}
46
61
62
+ private setFilesPanelRef = ( ref : FilesPanel ) => {
63
+ this . filesPanelRef = ref ;
64
+ } ;
65
+
47
66
makeBgStyle = ( ) : Object => {
48
67
if ( this . props . ui . clientCfg . allowSetBg ) {
49
68
if (
@@ -78,6 +97,39 @@ export class RootFrame extends React.Component<Props, State, {}> {
78
97
return { } ;
79
98
} ;
80
99
100
+ onDragOver = ( ev : React . DragEvent < HTMLDivElement > ) => {
101
+ this . onDragOverImp ( ) ;
102
+ ev . preventDefault ( ) ;
103
+ } ;
104
+
105
+ onDragOverImp = throttle ( dragOverthrottlePeriod , ( ) => {
106
+ updater ( ) . setControlOption ( dropAreaCtrl , ctrlOn ) ;
107
+ this . props . update ( updater ( ) . updateUI ) ;
108
+ this . setState ( { lastDragOverTime : Date . now ( ) } ) ;
109
+ } ) ;
110
+
111
+ onDrop = ( ev : React . DragEvent < HTMLDivElement > ) => {
112
+ if ( ev . dataTransfer ?. files ?. length > 0 ) {
113
+ this . filesPanelRef . addFileList ( ev . dataTransfer . files ) ;
114
+ }
115
+ ev . preventDefault ( ) ;
116
+ } ;
117
+
118
+ endDrag = ( ) => {
119
+ const now = Date . now ( ) ;
120
+ const isDragOverOff =
121
+ this . props . ui . control . controls . get ( dropAreaCtrl ) === ctrlOff ;
122
+ if (
123
+ now - this . state . lastDragOverTime < dragOverthrottlePeriod * 1.5 ||
124
+ isDragOverOff
125
+ ) {
126
+ return ;
127
+ }
128
+
129
+ updater ( ) . setControlOption ( dropAreaCtrl , ctrlOff ) ;
130
+ this . props . update ( updater ( ) . updateUI ) ;
131
+ } ;
132
+
81
133
render ( ) {
82
134
const bgStyle = this . makeBgStyle ( ) ;
83
135
const autoTheme =
@@ -97,7 +149,12 @@ export class RootFrame extends React.Component<Props, State, {}> {
97
149
const sharingsPanelClass = displaying === "sharingsPanel" ? "" : "hidden" ;
98
150
99
151
return (
100
- < div id = "root-frame" className = { `${ theme } ${ fontSizeClass } ` } >
152
+ < div
153
+ id = "root-frame"
154
+ className = { `${ theme } ${ fontSizeClass } ` }
155
+ onDragOver = { this . onDragOver }
156
+ onDrop = { this . onDrop }
157
+ >
101
158
< div id = "bg" style = { bgStyle } >
102
159
< div id = "custom" >
103
160
< Layers
@@ -151,6 +208,7 @@ export class RootFrame extends React.Component<Props, State, {}> {
151
208
ui = { this . props . ui }
152
209
enabled = { displaying === "filesPanel" }
153
210
update = { this . props . update }
211
+ ref = { this . setFilesPanelRef }
154
212
/>
155
213
</ span >
156
214
0 commit comments