Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dnd): add configurable dataTransferProperty to droppable #213

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ export type WindowAttributes = {
/**
* If window should have the default drop action
*/
droppable?: boolean;
droppable?: boolean | {
dataTransferProperty?: 'files' | 'items';
};
/**
* Minimum dimension
*/
Expand Down
11 changes: 6 additions & 5 deletions src/utils/dnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ const retval = (fn, ...args) => {
return true;
};

const getDataTransfer = (ev, type) => {
const getDataTransfer = (ev, type, dataTransferProperty) => {
let files = [];
let data;

if (ev.dataTransfer) {
files = ev.dataTransfer.files
? Array.from(ev.dataTransfer.files)
files = ev.dataTransfer[dataTransferProperty]
? Array.from(ev.dataTransfer[dataTransferProperty])
: [];

try {
Expand Down Expand Up @@ -200,9 +200,10 @@ export const draggable = (el, options = {}) => {
* @return {DroppableInstance}
*/
export const droppable = (el, options = {}) => {
const {strict, type, effect, ondragenter, ondragover, ondragleave, ondrop} = {
const {strict, type, effect, dataTransferProperty, ondragenter, ondragover, ondragleave, ondrop} = {
type: 'application/json',
effect: 'move',
dataTransferProperty: 'files',
ondragenter: () => true,
ondragover: () => true,
ondragleave: () => true,
Expand Down Expand Up @@ -241,7 +242,7 @@ export const droppable = (el, options = {}) => {
return false;
}

const {files, data} = getDataTransfer(ev, type);
const {files, data} = getDataTransfer(ev, type, dataTransferProperty);

ev.stopPropagation();
ev.preventDefault();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const createAttributes = attrs => ({
visibility: 'global',
shadowDOM: false,
clamp: true,
droppable: true,
droppable: {},
andersevenrud marked this conversation as resolved.
Show resolved Hide resolved
mediaQueries: {
small: 'screen and (max-width: 640px)',
medium: 'screen and (min-width: 640px) and (max-width: 1024px)',
Expand Down
15 changes: 13 additions & 2 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import logger from './logger';
* @property {boolean} [controls=true] Show controls
* @property {string} [visibility=global] Global visibility, 'restricted' to hide from window lists etc.
* @property {boolean} [clamp=true] Clamp the window position upon creation
* @property {boolean} [droppable=true] If window should have the default drop action
* @property {boolean | {dataTransferProperty?: 'files' | 'items'}} [droppable={}] If window should have the default drop action
* @property {WindowDimension} [minDimension] Minimum dimension
* @property {WindowDimension} [maxDimension] Maximum dimension
* @property {{name: string}} [mediaQueries] A map of matchMedia to name
Expand Down Expand Up @@ -454,11 +454,22 @@ export default class Window extends EventEmitter {

// DnD functionality
if (this.attributes.droppable) {
/*
`this.attributes.droppable` should probably always be an object
and never be set to `true`.
The following code is only to update the setting for legacy applications.
*/
if (this.attributes.droppable === true) {
this.attributes.droppable = {};
}

const {dataTransferProperty = 'files'} = this.attributes.droppable;
const d = droppable(this.$element, {
andersevenrud marked this conversation as resolved.
Show resolved Hide resolved
ondragenter: (...args) => this.emit('dragenter', ...args, this),
ondragover: (...args) => this.emit('dragover', ...args, this),
ondragleave: (...args) => this.emit('dragleave', ...args, this),
ondrop: (...args) => this.emit('drop', ...args, this)
ondrop: (...args) => this.emit('drop', ...args, this),
dataTransferProperty,
});

this.on('destroy', () => d.destroy());
Expand Down
Loading