Skip to content

Commit 12717e2

Browse files
authored
feat(dnd): add configurable dataTransferProperty to droppable (#213)
1 parent f4343b8 commit 12717e2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ export type WindowAttributes = {
577577
/**
578578
* If window should have the default drop action
579579
*/
580-
droppable?: boolean;
580+
droppable?: boolean | {
581+
dataTransferProperty?: 'files' | 'items';
582+
};
581583
/**
582584
* Minimum dimension
583585
*/

src/utils/dnd.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ const retval = (fn, ...args) => {
9090
return true;
9191
};
9292

93-
const getDataTransfer = (ev, type) => {
93+
const getDataTransfer = (ev, type, dataTransferProperty) => {
9494
let files = [];
9595
let data;
9696

9797
if (ev.dataTransfer) {
98-
files = ev.dataTransfer.files
99-
? Array.from(ev.dataTransfer.files)
98+
files = ev.dataTransfer[dataTransferProperty]
99+
? Array.from(ev.dataTransfer[dataTransferProperty])
100100
: [];
101101

102102
try {
@@ -200,9 +200,10 @@ export const draggable = (el, options = {}) => {
200200
* @return {DroppableInstance}
201201
*/
202202
export const droppable = (el, options = {}) => {
203-
const {strict, type, effect, ondragenter, ondragover, ondragleave, ondrop} = {
203+
const {strict, type, effect, dataTransferProperty, ondragenter, ondragover, ondragleave, ondrop} = {
204204
type: 'application/json',
205205
effect: 'move',
206+
dataTransferProperty: 'files',
206207
ondragenter: () => true,
207208
ondragover: () => true,
208209
ondragleave: () => true,
@@ -241,7 +242,7 @@ export const droppable = (el, options = {}) => {
241242
return false;
242243
}
243244

244-
const {files, data} = getDataTransfer(ev, type);
245+
const {files, data} = getDataTransfer(ev, type, dataTransferProperty);
245246

246247
ev.stopPropagation();
247248
ev.preventDefault();

src/window.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import logger from './logger';
8484
* @property {boolean} [controls=true] Show controls
8585
* @property {string} [visibility=global] Global visibility, 'restricted' to hide from window lists etc.
8686
* @property {boolean} [clamp=true] Clamp the window position upon creation
87-
* @property {boolean} [droppable=true] If window should have the default drop action
87+
* @property {boolean | {dataTransferProperty?: 'files' | 'items'}} [droppable=true] If window should have the default drop action
8888
* @property {WindowDimension} [minDimension] Minimum dimension
8989
* @property {WindowDimension} [maxDimension] Maximum dimension
9090
* @property {{name: string}} [mediaQueries] A map of matchMedia to name
@@ -454,11 +454,16 @@ export default class Window extends EventEmitter {
454454

455455
// DnD functionality
456456
if (this.attributes.droppable) {
457+
const {dataTransferProperty = 'files'} = this.attributes.droppable === true
458+
? {}
459+
: this.attributes.droppable;
460+
457461
const d = droppable(this.$element, {
458462
ondragenter: (...args) => this.emit('dragenter', ...args, this),
459463
ondragover: (...args) => this.emit('dragover', ...args, this),
460464
ondragleave: (...args) => this.emit('dragleave', ...args, this),
461-
ondrop: (...args) => this.emit('drop', ...args, this)
465+
ondrop: (...args) => this.emit('drop', ...args, this),
466+
dataTransferProperty,
462467
});
463468

464469
this.on('destroy', () => d.destroy());

0 commit comments

Comments
 (0)