MCPcopy Create free account
hub / github.com/adobe/react-spectrum / getAllowedOperations

Function getAllowedOperations

packages/react-aria/src/dnd/useDrop.ts:430–487  ·  view source on GitHub ↗
(e: DragEvent)

Source from the content-addressed store, hash-verified

428}
429
430function getAllowedOperations(e: DragEvent) {
431 let allowedOperations = DROP_OPERATION_ALLOWED[e.dataTransfer.effectAllowed];
432
433 // WebKit always sets effectAllowed to "copyMove" on macOS, and "all" on iOS, regardless of what was
434 // set during the dragstart event: https://bugs.webkit.org/show_bug.cgi?id=178058
435 //
436 // Android Chrome also sets effectAllowed to "copyMove" in all cases: https://bugs.chromium.org/p/chromium/issues/detail?id=1359182
437 //
438 // If the drag started within the page, we can use a global variable to get the real allowed operations.
439 // This needs to be intersected with the actual effectAllowed, which may have been filtered based on modifier keys.
440 // Unfortunately, this means that link operations do not work at all in Safari.
441 if (globalAllowedDropOperations) {
442 allowedOperations &= globalAllowedDropOperations;
443 }
444
445 // Chrome and Safari on macOS will automatically filter effectAllowed when pressing modifier keys,
446 // allowing the user to switch between move, link, and copy operations. Firefox on macOS and all
447 // Windows browsers do not do this, so do it ourselves instead. The exact keys are platform dependent.
448 // https://ux.stackexchange.com/questions/83748/what-are-the-most-common-modifier-keys-for-dragging-objects-with-a-mouse
449 //
450 // Note that none of these modifiers are ever set in WebKit due to a bug: https://bugs.webkit.org/show_bug.cgi?id=77465
451 // However, Safari does update effectAllowed correctly, so we can just rely on that.
452 let allowedModifiers = DROP_OPERATION.none;
453 if (isMac()) {
454 if (e.altKey) {
455 allowedModifiers |= DROP_OPERATION.copy;
456 }
457
458 // Chrome and Safari both use the Control key for link, even though Finder uses Command + Option.
459 // iPadOS doesn't support link operations and will not fire the drop event at all if dropEffect is set to link.
460 // https://bugs.webkit.org/show_bug.cgi?id=244701
461 if (e.ctrlKey && !isIPad()) {
462 allowedModifiers |= DROP_OPERATION.link;
463 }
464
465 if (e.metaKey) {
466 allowedModifiers |= DROP_OPERATION.move;
467 }
468 } else {
469 if (e.altKey) {
470 allowedModifiers |= DROP_OPERATION.link;
471 }
472
473 if (e.shiftKey) {
474 allowedModifiers |= DROP_OPERATION.move;
475 }
476
477 if (e.ctrlKey) {
478 allowedModifiers |= DROP_OPERATION.copy;
479 }
480 }
481
482 if (allowedModifiers) {
483 return allowedOperations & allowedModifiers;
484 }
485
486 return allowedOperations;
487}

Callers 2

onDragOverFunction · 0.85
onDragEnterFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected