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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG5kLXN0YXRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvZG5kL3NyYy9saWIvZG5kLXN0YXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFDTCxnQkFBZ0IsRUFDaEIsWUFBWSxFQUNaLGFBQWEsRUFDYixvQkFBb0IsRUFDcEIsY0FBYyxFQUNkLGNBQWMsR0FDZixNQUFNLGFBQWEsQ0FBQztBQVNyQixNQUFNLFNBQVMsR0FBYTtJQUMxQixVQUFVLEVBQUUsS0FBSztJQUNqQixVQUFVLEVBQUUsTUFBTTtJQUNsQixhQUFhLEVBQUUsS0FBSztJQUNwQixJQUFJLEVBQUUsU0FBUztDQUNoQixDQUFDO0FBRUYsTUFBTSxVQUFVLFNBQVMsQ0FDdkIsS0FBZ0IsRUFDaEIsYUFBNEIsRUFDNUIsSUFBd0I7SUFFeEIsU0FBUyxDQUFDLFVBQVUsR0FBRyxJQUFJLENBQUM7SUFDNUIsU0FBUyxDQUFDLFVBQVUsR0FBRyxNQ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