mjs • Lines: 135import {useState as $5gqtb$useState, useRef as $5gqtb$useRef, useCallback as $5gqtb$useCallback} from "react";
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $e279703d7ed26e75$export$926850f6ecef79d0(props) {
let { acceptedDragTypes: acceptedDragTypes = 'all', isDisabled: isDisabled, onInsert: onInsert, onRootDrop: onRootDrop, onItemDrop: onItemDrop, onReorder: onReorder, onMove: onMove, shouldAcceptItemDrop: shouldAcceptItemDrop, collection: collection, selectionManager: selectionManager, onDropEnter: onDropEnter, getDropOperation: getDropOperation, onDrop: onDrop } = props;
let [target, setTarget] = (0, $5gqtb$useState)(null);
let targetRef = (0, $5gqtb$useRef)(null);
let getOppositeTarget = (target)=>{
if (target.dropPosition === 'before') {
let node = collection.getItem(target.key);
return node && node.prevKey != null ? {
type: 'item',
key: node.prevKey,
dropPosition: 'after'
} : null;
} else if (target.dropPosition === 'after') {
let node = collection.getItem(target.key);
return node && node.nextKey != null ? {
type: 'item',
key: node.nextKey,
dropPosition: 'before'
} : null;
}
return null;
};
let defaultGetDropOperation = (0, $5gqtb$useCallback)((e)=>{
let { target: target, types: types, allowedOperations: allowedOperations, isInternal: isInternal, draggingKeys: draggingKeys } = e;
if (isDisabled || !target) return 'cancel';
if (acceptedDragTypes === 'all' || acceptedDragTypes.some((type)=>types.has(type))) {
let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');
let isValidReorder = onReorder && target.type === 'item' && isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after') && $e279703d7ed26e75$var$isDraggingWithinParent(collection, target, draggingKeys);
let isItemDropAllowed = target.type !== 'item' || target.dropPosition !== 'on' || !shouldAcceptItemDrop || shouldAcceptItemDrop(target, types);
let isValidMove = onMove && target.type === 'item' && isInternal && isItemDropAllowed;
// Feedback was that internal root drop was weird so preventing that from happening
let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;
// Automatically prevent items (i.e. folders) from being dropped on themselves.
let isValidOnItemDrop = onItemDrop && target.type === 'item' && target.dropPosition === 'on' && !(isInternal && target.key != null && draggingKeys.has(target.key)) && isItemDropAllowed;
if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {
if (getDropOperation) return getDropOperation(target, types, allowedOperations);
else return allowedOperations[0];
}
}
return 'cancel';
}, [
isDisabled,
collection,
acceptedDragTypes,
getDropOperation,
onInsert,
onRootDrop,
onItemDrop,
shouldAcceptItemDrop,
onReorder,
onMove,
onDrop
]);
return {
collection: collection,
selectionManager: selectionManager,
isDisabled: isDisabled,
target: target,
setTarget (newTarget) {
if (this.isDropTarget(newTarget)) return;
let target = targetRef.current;
if (target && typeof props.onDropExit === 'function') props.onDropExit({
type: 'dropexit',
x: 0,
y: 0,
target: target
});
if (newTarget && typeof onDropEnter === 'function') onDropEnter({
type: 'dropenter',
x: 0,
y: 0,
target: newTarget
});
targetRef.current = newTarget ?? null;
setTarget(newTarget ?? null);
},
isDropTarget (dropTarget) {
let target = targetRef.current;
if (!target || !dropTarget) return false;
if ($e279703d7ed26e75$var$isEqualDropTarget(dropTarget, target)) return true;
// Check if the targets point at the same point between two items, one referring before, and the other after.
if (dropTarget?.type === 'item' && target?.type === 'item' && dropTarget.key !== target.key && dropTarget.dropPosition !== target.dropPosition && dropTarget.dropPosition !== 'on' && target.dropPosition !== 'on') return $e279703d7ed26e75$var$isEqualDropTarget(getOppositeTarget(dropTarget), target) || $e279703d7ed26e75$var$isEqualDropTarget(dropTarget, getOppositeTarget(target));
return false;
},
getDropOperation (e) {
let { target: target, isInternal: isInternal, draggingKeys: draggingKeys } = e;
// Prevent dropping items onto themselves or their descendants
if (isInternal && target.type === 'item' && draggingKeys.size > 0) {
if (draggingKeys.has(target.key) && target.dropPosition === 'on') return 'cancel';
let currentKey = target.key;
while(currentKey != null){
let item = collection.getItem(currentKey);
let parentKey = item?.parentKey;
if (parentKey != null && draggingKeys.has(parentKey)) return 'cancel';
currentKey = parentKey ?? null;
}
}
return defaultGetDropOperation(e);
}
};
}
function $e279703d7ed26e75$var$isEqualDropTarget(a, b) {
if (!a) return !b;
switch(a.type){
case 'root':
return b?.type === 'root';
case 'item':
return b?.type === 'item' && b?.key === a.key && b?.dropPosition === a.dropPosition;
}
}
function $e279703d7ed26e75$var$isDraggingWithinParent(collection, target, draggingKeys) {
let targetNode = collection.getItem(target.key);
for (let key of draggingKeys){
let node = collection.getItem(key);
if (node?.parentKey !== targetNode?.parentKey) return false;
}
return true;
}
export {$e279703d7ed26e75$export$926850f6ecef79d0 as useDroppableCollectionState};
//# sourceMappingURL=useDroppableCollectionState.mjs.map