1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2024-10-31 07:11:47 +09:00

Improve drag and drop

This commit is contained in:
syuilo 2018-02-27 06:25:17 +09:00
parent 56bef13850
commit c6118c7bc8
7 changed files with 148 additions and 154 deletions

View File

@ -1,7 +1,7 @@
<template>
<div class="mk-messaging-form"
@dragover.prevent.stop="onDragover"
@drop.prevent.stop="onDrop"
@dragover.stop="onDragover"
@drop.stop="onDrop"
>
<textarea
v-model="text"
@ -89,34 +89,33 @@ export default Vue.extend({
},
onDragover(e) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
if (isFile || isDriveFile) {
e.preventDefault();
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
}
},
onDrop(e): void {
//
if (e.dataTransfer.files.length == 1) {
e.preventDefault();
this.upload(e.dataTransfer.files[0]);
return;
} else if (e.dataTransfer.files.length > 1) {
e.preventDefault();
alert('メッセージに添付できるのはひとつのファイルのみです');
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return;
try {
//
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
this.file = obj.file;
}
} catch (e) {
// not a json, so noop
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
this.file = JSON.parse(driveFile);
e.preventDefault();
}
//#endregion
},
onKeypress(e) {

View File

@ -88,7 +88,14 @@ export default Vue.extend({
methods: {
onDragover(e) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
if (isFile || isDriveFile) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
} else {
e.dataTransfer.dropEffect = 'none';
}
},
onDrop(e): void {
@ -101,21 +108,13 @@ export default Vue.extend({
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return;
try {
//
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
this.form.file = obj.file;
}
} catch (e) {
// not a json, so noop
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
this.form.file = file;
}
//#endregion
},
fetchMessages() {

View File

@ -115,11 +115,7 @@ export default Vue.extend({
onDragstart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text', JSON.stringify({
type: 'file',
id: this.file.id,
file: this.file
}));
e.dataTransfer.setData('mk_drive_file', JSON.stringify(this.file));
this.isDragging = true;
//

View File

@ -92,19 +92,22 @@ export default Vue.extend({
},
onDragover(e) {
//
if (!this.isDragging) {
//
if (e.dataTransfer.effectAllowed === 'all') {
e.dataTransfer.dropEffect = 'copy';
} else {
e.dataTransfer.dropEffect = 'move';
}
} else {
//
if (this.isDragging) {
//
e.dataTransfer.dropEffect = 'none';
return;
}
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
const isDriveFolder = e.dataTransfer.types[0] == 'mk_drive_folder';
if (isFile || isDriveFile || isDriveFolder) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
} else {
e.dataTransfer.dropEffect = 'none';
}
return false;
},
onDragenter() {
@ -123,36 +126,35 @@ export default Vue.extend({
Array.from(e.dataTransfer.files).forEach(file => {
this.browser.upload(file, this.folder);
});
return false;
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return false;
//
// TODO: Validate JSON
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
const file = obj.id;
this.browser.removeFile(file);
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
this.browser.removeFile(file.id);
(this as any).api('drive/files/update', {
file_id: file,
file_id: file.id,
folder_id: this.folder.id
});
// ()
} else if (obj.type == 'folder') {
const folder = obj.id;
}
//#endregion
//#region
const driveFolder = e.dataTransfer.getData('mk_drive_folder');
if (driveFolder != null && driveFolder != '') {
const folder = JSON.parse(driveFolder);
// reject
if (folder == this.folder.id) return false;
this.browser.removeFolder(folder);
if (folder.id == this.folder.id) return;
this.browser.removeFolder(folder.id);
(this as any).api('drive/folders/update', {
folder_id: folder,
folder_id: folder.id,
parent_id: this.folder.id
}).then(() => {
// something
// noop
}).catch(err => {
switch (err) {
case 'detected-circular-definition':
@ -169,16 +171,12 @@ export default Vue.extend({
}
});
}
return false;
//#endregion
},
onDragstart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text', JSON.stringify({
type: 'folder',
id: this.folder.id
}));
e.dataTransfer.setData('mk_drive_folder', JSON.stringify(this.folder));
this.isDragging = true;
//

View File

@ -41,13 +41,17 @@ export default Vue.extend({
//
if (this.folder == null && this.browser.folder == null) {
e.dataTransfer.dropEffect = 'none';
//
} else if (e.dataTransfer.effectAllowed == 'all') {
e.dataTransfer.dropEffect = 'copy';
} else {
e.dataTransfer.dropEffect = 'move';
}
return false;
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
const isDriveFolder = e.dataTransfer.types[0] == 'mk_drive_folder';
if (isFile || isDriveFile || isDriveFolder) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
} else {
e.dataTransfer.dropEffect = 'none';
}
},
onDragenter() {
if (this.folder || this.browser.folder) this.draghover = true;
@ -63,38 +67,34 @@ export default Vue.extend({
Array.from(e.dataTransfer.files).forEach(file => {
this.browser.upload(file, this.folder);
});
return false;
};
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return false;
//
// TODO: Validate JSON
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
const file = obj.id;
this.browser.removeFile(file);
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
this.browser.removeFile(file.id);
(this as any).api('drive/files/update', {
file_id: file,
file_id: file.id,
folder_id: this.folder ? this.folder.id : null
});
// ()
} else if (obj.type == 'folder') {
const folder = obj.id;
}
//#endregion
//#region
const driveFolder = e.dataTransfer.getData('mk_drive_folder');
if (driveFolder != null && driveFolder != '') {
const folder = JSON.parse(driveFolder);
// reject
if (this.folder && folder == this.folder.id) return false;
this.browser.removeFolder(folder);
if (this.folder && folder.id == this.folder.id) return;
this.browser.removeFolder(folder.id);
(this as any).api('drive/folders/update', {
folder_id: folder,
folder_id: folder.id,
parent_id: this.folder ? this.folder.id : null
});
}
return false;
//#endregion
}
}
});

View File

@ -235,15 +235,21 @@ export default Vue.extend({
},
onDragover(e): any {
//
if (!this.isDragSource) {
//
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
this.draghover = true;
} else {
//
if (this.isDragSource) {
//
e.dataTransfer.dropEffect = 'none';
return false;
return;
}
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
const isDriveFolder = e.dataTransfer.types[0] == 'mk_drive_folder';
if (isFile || isDriveFile || isDriveFolder) {
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
} else {
e.dataTransfer.dropEffect = 'none';
}
},
@ -263,38 +269,36 @@ export default Vue.extend({
Array.from(e.dataTransfer.files).forEach(file => {
this.upload(file, this.folder);
});
return false;
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return false;
//
// TODO: JSON
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
const file = obj.id;
if (this.files.some(f => f.id == file)) return false;
this.removeFile(file);
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
if (this.files.some(f => f.id == file.id)) return;
this.removeFile(file.id);
(this as any).api('drive/files/update', {
file_id: file,
file_id: file.id,
folder_id: this.folder ? this.folder.id : null
});
// ()
} else if (obj.type == 'folder') {
const folder = obj.id;
}
//#endregion
//#region
const driveFolder = e.dataTransfer.getData('mk_drive_folder');
if (driveFolder != null && driveFolder != '') {
const folder = JSON.parse(driveFolder);
// reject
if (this.folder && folder == this.folder.id) return false;
if (this.folders.some(f => f.id == folder)) return false;
this.removeFolder(folder);
if (this.folder && folder.id == this.folder.id) return false;
if (this.folders.some(f => f.id == folder.id)) return false;
this.removeFolder(folder.id);
(this as any).api('drive/folders/update', {
folder_id: folder,
folder_id: folder.id,
parent_id: this.folder ? this.folder.id : null
}).then(() => {
// something
// noop
}).catch(err => {
switch (err) {
case 'detected-circular-definition':
@ -311,8 +315,7 @@ export default Vue.extend({
}
});
}
return false;
//#endregion
},
selectLocalFile() {

View File

@ -1,9 +1,9 @@
<template>
<div class="mk-post-form"
@dragover.prevent.stop="onDragover"
@dragover.stop="onDragover"
@dragenter="onDragenter"
@dragleave="onDragleave"
@drop.prevent.stop="onDrop"
@drop.stop="onDrop"
>
<div class="content">
<textarea :class="{ with: (files.length != 0 || poll) }"
@ -159,8 +159,13 @@ export default Vue.extend({
});
},
onDragover(e) {
this.draghover = true;
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
const isFile = e.dataTransfer.items[0].kind == 'file';
const isDriveFile = e.dataTransfer.types[0] == 'mk_drive_file';
if (isFile || isDriveFile) {
e.preventDefault();
this.draghover = true;
e.dataTransfer.dropEffect = e.dataTransfer.effectAllowed == 'all' ? 'copy' : 'move';
}
},
onDragenter(e) {
this.draghover = true;
@ -173,26 +178,20 @@ export default Vue.extend({
//
if (e.dataTransfer.files.length > 0) {
e.preventDefault();
Array.from(e.dataTransfer.files).forEach(this.upload);
return;
}
//
const data = e.dataTransfer.getData('text');
if (data == null) return;
try {
//
const obj = JSON.parse(data);
// ()
if (obj.type == 'file') {
this.files.push(obj.file);
this.$emit('change-attached-media', this.files);
}
} catch (e) {
// not a json, so noop
//#region
const driveFile = e.dataTransfer.getData('mk_drive_file');
if (driveFile != null && driveFile != '') {
const file = JSON.parse(driveFile);
this.files.push(file);
this.$emit('change-attached-media', this.files);
e.preventDefault();
}
//#endregion
},
post() {
this.posting = true;