72 lines
9.0 KiB
JavaScript
72 lines
9.0 KiB
JavaScript
import { CUSTOM_MIME_TYPE, DROP_EFFECTS, filterEffects, getWellKnownMimeType, JSON_MIME_TYPE, MSIE_MIME_TYPE, } from './dnd-utils';
|
|
const _dndState = {
|
|
isDragging: false,
|
|
dropEffect: 'none',
|
|
effectAllowed: 'all',
|
|
type: undefined,
|
|
};
|
|
export function startDrag(event, effectAllowed, type) {
|
|
_dndState.isDragging = true;
|
|
_dndState.dropEffect = 'none';
|
|
_dndState.effectAllowed = effectAllowed;
|
|
_dndState.type = type;
|
|
if (event.dataTransfer) {
|
|
event.dataTransfer.effectAllowed = effectAllowed;
|
|
}
|
|
}
|
|
export function endDrag() {
|
|
_dndState.isDragging = false;
|
|
_dndState.dropEffect = undefined;
|
|
_dndState.effectAllowed = undefined;
|
|
_dndState.type = undefined;
|
|
}
|
|
export function setDropEffect(event, dropEffect) {
|
|
if (_dndState.isDragging === true) {
|
|
_dndState.dropEffect = dropEffect;
|
|
}
|
|
if (event.dataTransfer) {
|
|
event.dataTransfer.dropEffect = dropEffect;
|
|
}
|
|
}
|
|
export function getDropEffect(event, effectAllowed) {
|
|
const dataTransferEffectAllowed = event.dataTransfer
|
|
? event.dataTransfer.effectAllowed
|
|
: 'uninitialized';
|
|
let effects = filterEffects(DROP_EFFECTS, dataTransferEffectAllowed);
|
|
if (_dndState.isDragging === true) {
|
|
effects = filterEffects(effects, _dndState.effectAllowed);
|
|
}
|
|
if (effectAllowed) {
|
|
effects = filterEffects(effects, effectAllowed);
|
|
}
|
|
// MacOS automatically filters dataTransfer.effectAllowed depending on the modifier keys,
|
|
// therefore the following modifier keys will only affect other operating systems.
|
|
if (effects.length === 0) {
|
|
return 'none';
|
|
}
|
|
if (event.ctrlKey && effects.indexOf('copy') !== -1) {
|
|
return 'copy';
|
|
}
|
|
if (event.altKey && effects.indexOf('link') !== -1) {
|
|
return 'link';
|
|
}
|
|
return effects[0];
|
|
}
|
|
export function getDndType(event) {
|
|
if (_dndState.isDragging === true) {
|
|
return _dndState.type;
|
|
}
|
|
const mimeType = getWellKnownMimeType(event);
|
|
if (mimeType === null) {
|
|
return undefined;
|
|
}
|
|
if (mimeType === MSIE_MIME_TYPE || mimeType === JSON_MIME_TYPE) {
|
|
return undefined;
|
|
}
|
|
return mimeType.substr(CUSTOM_MIME_TYPE.length + 1) || undefined;
|
|
}
|
|
export function isExternalDrag() {
|
|
return _dndState.isDragging === false;
|
|
}
|
|
export const dndState = _dndState;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG5kLXN0YXRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvZG5kL3NyYy9saWIvZG5kLXN0YXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFDTCxnQkFBZ0IsRUFDaEIsWUFBWSxFQUNaLGFBQWEsRUFDYixvQkFBb0IsRUFDcEIsY0FBYyxFQUNkLGNBQWMsR0FDZixNQUFNLGFBQWEsQ0FBQztBQVNyQixNQUFNLFNBQVMsR0FBYTtJQUMxQixVQUFVLEVBQUUsS0FBSztJQUNqQixVQUFVLEVBQUUsTUFBTTtJQUNsQixhQUFhLEVBQUUsS0FBSztJQUNwQixJQUFJLEVBQUUsU0FBUztDQUNoQixDQUFDO0FBRUYsTUFBTSxVQUFVLFNBQVMsQ0FDdkIsS0FBZ0IsRUFDaEIsYUFBNEIsRUFDNUIsSUFBd0I7SUFFeEIsU0FBUyxDQUFDLFVBQVUsR0FBRyxJQUFJLENBQUM7SUFDNUIsU0FBUyxDQUFDLFVBQVUsR0FBRyxNQUFNLENBQUM7SUFDOUIsU0FBUyxDQUFDLGFBQWEsR0FBRyxhQUFhLENBQUM7SUFDeEMsU0FBUyxDQUFDLElBQUksR0FBRyxJQUFJLENBQUM7SUFFdEIsSUFBSSxLQUFLLENBQUMsWUFBWSxFQUFFO1F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