Skip to content

Commit dd666d0

Browse files
committed
Drag and drop dans les dossiers
1 parent 9d5a2c0 commit dd666d0

5 files changed

Lines changed: 74 additions & 25 deletions

File tree

src/ExplorerView.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ExplorerView extends Component {
1717

1818
this.createFolder = this.createFolder.bind(this);
1919
this.copyFile = this.copyFile.bind(this);
20+
this.dropFile = this.dropFile.bind(this);
2021
this.pasteFile = this.pasteFile.bind(this);
2122
this.handleContextMenu = this.handleContextMenu.bind(this);
2223
this.hideContextMenu = this.hideContextMenu.bind(this);
@@ -42,6 +43,16 @@ class ExplorerView extends Component {
4243
request('Paste', { _id: copiedFile, idParent: _id }).then(() => refresh());
4344
}
4445

46+
dropFile(event, _id) {
47+
const { refresh } = this.props;
48+
['folder', 'question', 'qcm'].forEach(type => {
49+
if (event.dataTransfer.types.includes(type)) {
50+
const file = JSON.parse(event.dataTransfer.getData(type));
51+
request('Move', { _id: file._id, idParent: _id }).then(() => refresh());
52+
}
53+
});
54+
}
55+
4556
handleContextMenu(event, items = []) {
4657
const { pageX, pageY, screenX, screenY } = event;
4758
const x = screenX - window.screenX;
@@ -71,23 +82,14 @@ class ExplorerView extends Component {
7182
}
7283

7384
buildFileItem(file) {
74-
const {
75-
handleContextMenu,
76-
props: {
77-
folder,
78-
edit,
79-
requestFolder,
80-
refresh,
81-
updateSessionView
82-
}
83-
} = this;
84-
85+
const { handleContextMenu, props: { folder, edit, requestFolder, refresh, updateSessionView } } = this;
8586
return (
8687
<File
8788
folder={folder}
8889
file={file}
8990
edit={edit}
9091
copyFile={this.copyFile}
92+
dropFile={this.dropFile}
9193
requestFolder={requestFolder}
9294
refresh={refresh}
9395
handleContextMenu={handleContextMenu}
@@ -110,13 +112,7 @@ class ExplorerView extends Component {
110112
}
111113

112114
handleDrop(event, _id) {
113-
const { refresh } = this.props;
114-
['folder', 'question', 'qcm'].forEach(type => {
115-
if (event.dataTransfer.types.includes(type)) {
116-
const file = JSON.parse(event.dataTransfer.getData(type));
117-
request('Move', { _id: file._id, idParent: _id }).then(() => refresh());
118-
}
119-
});
115+
this.dropFile(event, _id);
120116
event.target.classList.remove('dropZone');
121117
}
122118

src/File.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import request from './request';
66
class File extends Component {
77
constructor(props) {
88
super(props);
9-
this.state = { renaming: false };
9+
this.state = {
10+
renaming: false,
11+
dragging: false
12+
};
1013

1114
this.open = this.open.bind(this);
1215
this.rename = this.rename.bind(this);
@@ -15,6 +18,10 @@ class File extends Component {
1518
this.stopRenaming = this.stopRenaming.bind(this);
1619
this.handleContextMenu = this.handleContextMenu.bind(this);
1720
this.handleDragStart = this.handleDragStart.bind(this);
21+
this.handleDragEnd = this.handleDragEnd.bind(this);
22+
this.handleDragOver = this.handleDragOver.bind(this);
23+
this.handleDragLeave = this.handleDragLeave.bind(this);
24+
this.handleDrop = this.handleDrop.bind(this);
1825
}
1926

2027
open() {
@@ -102,12 +109,52 @@ class File extends Component {
102109
handleDragStart(event) {
103110
const { file } = this.props;
104111
event.dataTransfer.setData(file.type, JSON.stringify(file));
112+
this.setState({ dragging: true });
113+
}
114+
115+
handleDragEnd() {
116+
this.setState({ dragging: false });
117+
}
118+
119+
handleDragOver(event) {
120+
const { dragging } = this.state;
121+
const element = event.target;
122+
if (!dragging && element.classList.contains('folder')) {
123+
element.classList.add('grow');
124+
}
125+
event.preventDefault();
126+
}
127+
128+
handleDragLeave(event) {
129+
const element = event.target;
130+
element.classList.remove('grow');
131+
}
132+
133+
handleDrop(event) {
134+
const { props: { file: { _id }, dropFile }, state: { dragging } } = this;
135+
const element = event.target;
136+
137+
if (!dragging && element.classList.contains('folder')) {
138+
dropFile(event, _id);
139+
element.classList.remove('grow');
140+
}
105141
}
106142

107143
render() {
108144
const { props: { file: { type, name } }, state: { renaming } } = this;
145+
109146
return (
110-
<div className={`file ${renaming ? 'renaming' : ''}`} onDoubleClick={this.open} onContextMenu={this.handleContextMenu} onDragStart={this.handleDragStart} draggable>
147+
<div
148+
className={`file ${renaming ? 'renaming' : ''}`}
149+
onDoubleClick={this.open}
150+
onContextMenu={this.handleContextMenu}
151+
onDragStart={this.handleDragStart}
152+
onDragEnd={this.handleDragEnd}
153+
onDragOver={this.handleDragOver}
154+
onDragLeave={this.handleDragLeave}
155+
onDrop={this.handleDrop}
156+
draggable
157+
>
111158
<div className={type}/>
112159
<div className="fileName">
113160
{(renaming) ? <AutoFocusInput value={name} onStopEditing={this.stopRenaming}/> : name}

src/style/editor.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,4 @@
1313

1414
.stretch {
1515
flex-grow: 1;
16-
}
17-
18-
.dropZone {
19-
border: 1px blue dotted;
20-
background-color: rgba(98, 156, 201, .5);
2116
}

src/style/explorer.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@
6969
.folder, .question, .qcm {
7070
width: 100px;
7171
height: 100px;
72+
transition: transform .2s ease-in-out;
73+
}
74+
75+
.folder.grow {
76+
transform: scale(1.2);
77+
transition: transform .2s ease-in-out;
7278
}
7379

7480
.list .folder, .list .question, .list .qcm {

src/style/global.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@
4444
flex: 1 0;
4545
padding: 5px;
4646
overflow: auto;
47+
}
48+
49+
.dropZone {
50+
border: 1px blue dotted;
51+
background-color: rgba(98, 156, 201, .5);
4752
}

0 commit comments

Comments
 (0)