@react-stately/dnd 3.7.4 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,128 +0,0 @@
1
- var $ax21c$react = require("react");
2
-
3
-
4
- function $parcel$export(e, n, v, s) {
5
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
6
- }
7
-
8
- $parcel$export(module.exports, "useDroppableCollectionState", () => $6ce4cbfbe5e354f1$export$926850f6ecef79d0);
9
- /*
10
- * Copyright 2020 Adobe. All rights reserved.
11
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
12
- * you may not use this file except in compliance with the License. You may obtain a copy
13
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software distributed under
16
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
17
- * OF ANY KIND, either express or implied. See the License for the specific language
18
- * governing permissions and limitations under the License.
19
- */
20
- function $6ce4cbfbe5e354f1$export$926850f6ecef79d0(props) {
21
- 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;
22
- let [target, setTarget] = (0, $ax21c$react.useState)(null);
23
- let targetRef = (0, $ax21c$react.useRef)(null);
24
- let getOppositeTarget = (target)=>{
25
- if (target.dropPosition === 'before') {
26
- let node = collection.getItem(target.key);
27
- return node && node.prevKey != null ? {
28
- type: 'item',
29
- key: node.prevKey,
30
- dropPosition: 'after'
31
- } : null;
32
- } else if (target.dropPosition === 'after') {
33
- let node = collection.getItem(target.key);
34
- return node && node.nextKey != null ? {
35
- type: 'item',
36
- key: node.nextKey,
37
- dropPosition: 'before'
38
- } : null;
39
- }
40
- return null;
41
- };
42
- let defaultGetDropOperation = (0, $ax21c$react.useCallback)((e)=>{
43
- let { target: target, types: types, allowedOperations: allowedOperations, isInternal: isInternal, draggingKeys: draggingKeys } = e;
44
- if (isDisabled || !target) return 'cancel';
45
- if (acceptedDragTypes === 'all' || acceptedDragTypes.some((type)=>types.has(type))) {
46
- let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');
47
- let isValidReorder = onReorder && target.type === 'item' && isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after') && $6ce4cbfbe5e354f1$var$isDraggingWithinParent(collection, target, draggingKeys);
48
- let isItemDropAllowed = target.type !== 'item' || target.dropPosition !== 'on' || !shouldAcceptItemDrop || shouldAcceptItemDrop(target, types);
49
- let isValidMove = onMove && target.type === 'item' && isInternal && isItemDropAllowed;
50
- // Feedback was that internal root drop was weird so preventing that from happening
51
- let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;
52
- // Automatically prevent items (i.e. folders) from being dropped on themselves.
53
- let isValidOnItemDrop = onItemDrop && target.type === 'item' && target.dropPosition === 'on' && !(isInternal && target.key != null && draggingKeys.has(target.key)) && isItemDropAllowed;
54
- if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {
55
- if (getDropOperation) return getDropOperation(target, types, allowedOperations);
56
- else return allowedOperations[0];
57
- }
58
- }
59
- return 'cancel';
60
- }, [
61
- isDisabled,
62
- collection,
63
- acceptedDragTypes,
64
- getDropOperation,
65
- onInsert,
66
- onRootDrop,
67
- onItemDrop,
68
- shouldAcceptItemDrop,
69
- onReorder,
70
- onMove,
71
- onDrop
72
- ]);
73
- return {
74
- collection: collection,
75
- selectionManager: selectionManager,
76
- isDisabled: isDisabled,
77
- target: target,
78
- setTarget (newTarget) {
79
- if (this.isDropTarget(newTarget)) return;
80
- let target = targetRef.current;
81
- if (target && typeof props.onDropExit === 'function') props.onDropExit({
82
- type: 'dropexit',
83
- x: 0,
84
- y: 0,
85
- target: target
86
- });
87
- if (newTarget && typeof onDropEnter === 'function') onDropEnter({
88
- type: 'dropenter',
89
- x: 0,
90
- y: 0,
91
- target: newTarget
92
- });
93
- targetRef.current = newTarget !== null && newTarget !== void 0 ? newTarget : null;
94
- setTarget(newTarget !== null && newTarget !== void 0 ? newTarget : null);
95
- },
96
- isDropTarget (dropTarget) {
97
- let target = targetRef.current;
98
- if (!target || !dropTarget) return false;
99
- if ($6ce4cbfbe5e354f1$var$isEqualDropTarget(dropTarget, target)) return true;
100
- // Check if the targets point at the same point between two items, one referring before, and the other after.
101
- if ((dropTarget === null || dropTarget === void 0 ? void 0 : dropTarget.type) === 'item' && (target === null || target === void 0 ? void 0 : target.type) === 'item' && dropTarget.key !== target.key && dropTarget.dropPosition !== target.dropPosition && dropTarget.dropPosition !== 'on' && target.dropPosition !== 'on') return $6ce4cbfbe5e354f1$var$isEqualDropTarget(getOppositeTarget(dropTarget), target) || $6ce4cbfbe5e354f1$var$isEqualDropTarget(dropTarget, getOppositeTarget(target));
102
- return false;
103
- },
104
- getDropOperation (e) {
105
- return defaultGetDropOperation(e);
106
- }
107
- };
108
- }
109
- function $6ce4cbfbe5e354f1$var$isEqualDropTarget(a, b) {
110
- if (!a) return !b;
111
- switch(a.type){
112
- case 'root':
113
- return (b === null || b === void 0 ? void 0 : b.type) === 'root';
114
- case 'item':
115
- return (b === null || b === void 0 ? void 0 : b.type) === 'item' && (b === null || b === void 0 ? void 0 : b.key) === a.key && (b === null || b === void 0 ? void 0 : b.dropPosition) === a.dropPosition;
116
- }
117
- }
118
- function $6ce4cbfbe5e354f1$var$isDraggingWithinParent(collection, target, draggingKeys) {
119
- let targetNode = collection.getItem(target.key);
120
- for (let key of draggingKeys){
121
- let node = collection.getItem(key);
122
- if ((node === null || node === void 0 ? void 0 : node.parentKey) !== (targetNode === null || targetNode === void 0 ? void 0 : targetNode.parentKey)) return false;
123
- }
124
- return true;
125
- }
126
-
127
-
128
- //# sourceMappingURL=useD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S,UAAU,OAAO;YAC9B,IAAI,CAAC,UAAU,CAAC,YACd,OAAO;YAET,IAAI,wCAAkB,YAAY,SAChC,OAAO;YAGT,6GAA6G;YAC7G,IACE,CAAA,uBAAA,iCAAA,WAAY,IAAI,MAAK,UACrB,CAAA,mBAAA,6BAAA,OAAQ,IAAI,MAAK,UACjB,WAAW,GAAG,KAAK,OAAO,GAAG,IAC7B,WAAW,YAAY,KAAK,OAAO,YAAY,IAC/C,WAAW,YAAY,KAAK,QAC5B,OAAO,YAAY,KAAK,MAExB,OAAO,wCAAkB,kBAAkB,aAAa,WACtD,wCAAkB,YAAY,kBAAkB;YAGpD,OAAO;QACT;QACA,kBAAiB,CAAC;YAChB,OAAO,wBAAwB;QACjC;IACF;AACF;AAEA,SAAS,wCAAkB,CAAqB,EAAE,CAAqB;IACrE,IAAI,CAAC,GACH,OAAO,CAAC;IAGV,OAAQ,EAAE,IAAI;QACZ,KAAK;YACH,OAAO,CAAA,cAAA,wBAAA,EAAG,IAAI,MAAK;QACrB,KAAK;YACH,OAAO,CAAA,cAAA,wBAAA,EAAG,IAAI,MAAK,UAAU,CAAA,cAAA,wBAAA,EAAG,GAAG,MAAK,EAAE,GAAG,IAAI,CAAA,cAAA,wBAAA,EAAG,YAAY,MAAK,EAAE,YAAY;IACvF;AACF;AAEA,SAAS,6CAAuB,UAAqC,EAAE,MAAsB,EAAE,YAAsB;IACnH,IAAI,aAAa,WAAW,OAAO,CAAC,OAAO,GAAG;IAE9C,KAAK,IAAI,OAAO,aAAc;QAC5B,IAAI,OAAO,WAAW,OAAO,CAAC;QAC9B,IAAI,CAAA,iBAAA,2BAAA,KAAM,SAAS,OAAK,uBAAA,iCAAA,WAAY,SAAS,GAC3C,OAAO;IAEX;IAEA,OAAO;AACT","sources":["packages/@react-stately/dnd/src/useDroppableCollectionState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, DragTypes, DropOperation, DroppableCollectionProps, DropTarget, ItemDropTarget, Key, Node} from '@react-types/shared';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {useCallback, useRef, useState} from 'react';\n\ninterface DropOperationEvent {\n target: DropTarget,\n types: DragTypes,\n allowedOperations: DropOperation[],\n isInternal: boolean,\n draggingKeys: Set<Key>\n}\n\nexport interface DroppableCollectionStateOptions extends Omit<DroppableCollectionProps, 'onDropMove' | 'onDropActivate'> {\n /** A collection of items. */\n collection: Collection<Node<unknown>>,\n /** An interface for reading and updating multiple selection state. */\n selectionManager: MultipleSelectionManager,\n /** Whether the drop events should be disabled. */\n isDisabled?: boolean\n}\n\nexport interface DroppableCollectionState {\n /** A collection of items. */\n collection: Collection<Node<unknown>>,\n /** An interface for reading and updating multiple selection state. */\n selectionManager: MultipleSelectionManager,\n /** The current drop target. */\n target: DropTarget | null,\n /** Whether drop events are disabled. */\n isDisabled?: boolean,\n /** Sets the current drop target. */\n setTarget(target: DropTarget | null): void,\n /** Returns whether the given target is equivalent to the current drop target. */\n isDropTarget(target: DropTarget | null): boolean,\n /** Returns the drop operation for the given parameters. */\n getDropOperation(e: DropOperationEvent): DropOperation\n}\n\n/**\n * Manages state for a droppable collection.\n */\nexport function useDroppableCollectionState(props: DroppableCollectionStateOptions): DroppableCollectionState {\n let {\n acceptedDragTypes = 'all',\n isDisabled,\n onInsert,\n onRootDrop,\n onItemDrop,\n onReorder,\n onMove,\n shouldAcceptItemDrop,\n collection,\n selectionManager,\n onDropEnter,\n getDropOperation,\n onDrop\n } = props;\n let [target, setTarget] = useState<DropTarget | null>(null);\n let targetRef = useRef<DropTarget | null>(null);\n\n let getOppositeTarget = (target: ItemDropTarget): ItemDropTarget | null => {\n if (target.dropPosition === 'before') {\n let node = collection.getItem(target.key);\n return node && node.prevKey != null\n ? {type: 'item', key: node.prevKey, dropPosition: 'after'} \n : null;\n } else if (target.dropPosition === 'after') {\n let node = collection.getItem(target.key);\n return node && node.nextKey != null\n ? {type: 'item', key: node.nextKey, dropPosition: 'before'} \n : null;\n }\n return null;\n };\n\n let defaultGetDropOperation = useCallback((e: DropOperationEvent) => {\n let {\n target,\n types,\n allowedOperations,\n isInternal,\n draggingKeys\n } = e;\n\n if (isDisabled || !target) {\n return 'cancel';\n }\n\n if (acceptedDragTypes === 'all' || acceptedDragTypes.some(type => types.has(type))) {\n let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');\n let isValidReorder = onReorder\n && target.type === 'item'\n && isInternal\n && (target.dropPosition === 'before' || target.dropPosition === 'after')\n && isDraggingWithinParent(collection, target, draggingKeys);\n\n let isItemDropAllowed = target.type !== 'item'\n || target.dropPosition !== 'on'\n || (!shouldAcceptItemDrop || shouldAcceptItemDrop(target, types));\n\n let isValidMove = onMove\n && target.type === 'item'\n && isInternal\n && isItemDropAllowed;\n\n // Feedback was that internal root drop was weird so preventing that from happening\n let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;\n \n // Automatically prevent items (i.e. folders) from being dropped on themselves.\n let isValidOnItemDrop = onItemDrop \n && target.type === 'item' \n && target.dropPosition === 'on' \n && !(isInternal && target.key != null && draggingKeys.has(target.key)) \n && isItemDropAllowed;\n\n if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {\n if (getDropOperation) {\n return getDropOperation(target, types, allowedOperations);\n } else {\n return allowedOperations[0];\n }\n }\n }\n\n return 'cancel';\n }, [isDisabled, collection, acceptedDragTypes, getDropOperation, onInsert, onRootDrop, onItemDrop, shouldAcceptItemDrop, onReorder, onMove, onDrop]);\n\n return {\n collection,\n selectionManager,\n isDisabled,\n target,\n setTarget(newTarget) {\n if (this.isDropTarget(newTarget)) {\n return;\n }\n\n let target = targetRef.current;\n if (target && typeof props.onDropExit === 'function') {\n props.onDropExit({\n type: 'dropexit',\n x: 0, // todo\n y: 0,\n target\n });\n }\n\n if (newTarget && typeof onDropEnter === 'function') {\n onDropEnter({\n type: 'dropenter',\n x: 0, // todo\n y: 0,\n target: newTarget\n });\n }\n\n targetRef.current = newTarget ?? null;\n setTarget(newTarget ?? null);\n },\n isDropTarget(dropTarget) {\n let target = targetRef.current;\n if (!target || !dropTarget) {\n return false;\n }\n if (isEqualDropTarget(dropTarget, target)) {\n return true;\n }\n\n // Check if the targets point at the same point between two items, one referring before, and the other after.\n if (\n dropTarget?.type === 'item' &&\n target?.type === 'item' &&\n dropTarget.key !== target.key &&\n dropTarget.dropPosition !== target.dropPosition &&\n dropTarget.dropPosition !== 'on' &&\n target.dropPosition !== 'on'\n ) {\n return isEqualDropTarget(getOppositeTarget(dropTarget), target) ||\n isEqualDropTarget(dropTarget, getOppositeTarget(target));\n }\n\n return false;\n },\n getDropOperation(e) {\n return defaultGetDropOperation(e);\n }\n };\n}\n\nfunction isEqualDropTarget(a?: DropTarget | null, b?: DropTarget | null) {\n if (!a) {\n return !b;\n }\n\n switch (a.type) {\n case 'root':\n return b?.type === 'root';\n case 'item':\n return b?.type === 'item' && b?.key === a.key && b?.dropPosition === a.dropPosition;\n }\n}\n\nfunction isDraggingWithinParent(collection: Collection<Node<unknown>>, target: ItemDropTarget, draggingKeys: Set<Key>) {\n let targetNode = collection.getItem(target.key);\n\n for (let key of draggingKeys) {\n let node = collection.getItem(key);\n if (node?.parentKey !== targetNode?.parentKey) {\n return false;\n }\n }\n\n return true;\n}\n"],"names":[],"version":3,"file":"useDroppableCollectionState.main.js.map"}
@@ -1,123 +0,0 @@
1
- import {useState as $lyPoT$useState, useRef as $lyPoT$useRef, useCallback as $lyPoT$useCallback} from "react";
2
-
3
- /*
4
- * Copyright 2020 Adobe. All rights reserved.
5
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License. You may obtain a copy
7
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software distributed under
10
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
- * OF ANY KIND, either express or implied. See the License for the specific language
12
- * governing permissions and limitations under the License.
13
- */
14
- function $e672e8bc247525d1$export$926850f6ecef79d0(props) {
15
- 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;
16
- let [target, setTarget] = (0, $lyPoT$useState)(null);
17
- let targetRef = (0, $lyPoT$useRef)(null);
18
- let getOppositeTarget = (target)=>{
19
- if (target.dropPosition === 'before') {
20
- let node = collection.getItem(target.key);
21
- return node && node.prevKey != null ? {
22
- type: 'item',
23
- key: node.prevKey,
24
- dropPosition: 'after'
25
- } : null;
26
- } else if (target.dropPosition === 'after') {
27
- let node = collection.getItem(target.key);
28
- return node && node.nextKey != null ? {
29
- type: 'item',
30
- key: node.nextKey,
31
- dropPosition: 'before'
32
- } : null;
33
- }
34
- return null;
35
- };
36
- let defaultGetDropOperation = (0, $lyPoT$useCallback)((e)=>{
37
- let { target: target, types: types, allowedOperations: allowedOperations, isInternal: isInternal, draggingKeys: draggingKeys } = e;
38
- if (isDisabled || !target) return 'cancel';
39
- if (acceptedDragTypes === 'all' || acceptedDragTypes.some((type)=>types.has(type))) {
40
- let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');
41
- let isValidReorder = onReorder && target.type === 'item' && isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after') && $e672e8bc247525d1$var$isDraggingWithinParent(collection, target, draggingKeys);
42
- let isItemDropAllowed = target.type !== 'item' || target.dropPosition !== 'on' || !shouldAcceptItemDrop || shouldAcceptItemDrop(target, types);
43
- let isValidMove = onMove && target.type === 'item' && isInternal && isItemDropAllowed;
44
- // Feedback was that internal root drop was weird so preventing that from happening
45
- let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;
46
- // Automatically prevent items (i.e. folders) from being dropped on themselves.
47
- let isValidOnItemDrop = onItemDrop && target.type === 'item' && target.dropPosition === 'on' && !(isInternal && target.key != null && draggingKeys.has(target.key)) && isItemDropAllowed;
48
- if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {
49
- if (getDropOperation) return getDropOperation(target, types, allowedOperations);
50
- else return allowedOperations[0];
51
- }
52
- }
53
- return 'cancel';
54
- }, [
55
- isDisabled,
56
- collection,
57
- acceptedDragTypes,
58
- getDropOperation,
59
- onInsert,
60
- onRootDrop,
61
- onItemDrop,
62
- shouldAcceptItemDrop,
63
- onReorder,
64
- onMove,
65
- onDrop
66
- ]);
67
- return {
68
- collection: collection,
69
- selectionManager: selectionManager,
70
- isDisabled: isDisabled,
71
- target: target,
72
- setTarget (newTarget) {
73
- if (this.isDropTarget(newTarget)) return;
74
- let target = targetRef.current;
75
- if (target && typeof props.onDropExit === 'function') props.onDropExit({
76
- type: 'dropexit',
77
- x: 0,
78
- y: 0,
79
- target: target
80
- });
81
- if (newTarget && typeof onDropEnter === 'function') onDropEnter({
82
- type: 'dropenter',
83
- x: 0,
84
- y: 0,
85
- target: newTarget
86
- });
87
- targetRef.current = newTarget !== null && newTarget !== void 0 ? newTarget : null;
88
- setTarget(newTarget !== null && newTarget !== void 0 ? newTarget : null);
89
- },
90
- isDropTarget (dropTarget) {
91
- let target = targetRef.current;
92
- if (!target || !dropTarget) return false;
93
- if ($e672e8bc247525d1$var$isEqualDropTarget(dropTarget, target)) return true;
94
- // Check if the targets point at the same point between two items, one referring before, and the other after.
95
- if ((dropTarget === null || dropTarget === void 0 ? void 0 : dropTarget.type) === 'item' && (target === null || target === void 0 ? void 0 : target.type) === 'item' && dropTarget.key !== target.key && dropTarget.dropPosition !== target.dropPosition && dropTarget.dropPosition !== 'on' && target.dropPosition !== 'on') return $e672e8bc247525d1$var$isEqualDropTarget(getOppositeTarget(dropTarget), target) || $e672e8bc247525d1$var$isEqualDropTarget(dropTarget, getOppositeTarget(target));
96
- return false;
97
- },
98
- getDropOperation (e) {
99
- return defaultGetDropOperation(e);
100
- }
101
- };
102
- }
103
- function $e672e8bc247525d1$var$isEqualDropTarget(a, b) {
104
- if (!a) return !b;
105
- switch(a.type){
106
- case 'root':
107
- return (b === null || b === void 0 ? void 0 : b.type) === 'root';
108
- case 'item':
109
- return (b === null || b === void 0 ? void 0 : b.type) === 'item' && (b === null || b === void 0 ? void 0 : b.key) === a.key && (b === null || b === void 0 ? void 0 : b.dropPosition) === a.dropPosition;
110
- }
111
- }
112
- function $e672e8bc247525d1$var$isDraggingWithinParent(collection, target, draggingKeys) {
113
- let targetNode = collection.getItem(target.key);
114
- for (let key of draggingKeys){
115
- let node = collection.getItem(key);
116
- if ((node === null || node === void 0 ? void 0 : node.parentKey) !== (targetNode === null || targetNode === void 0 ? void 0 : targetNode.parentKey)) return false;
117
- }
118
- return true;
119
- }
120
-
121
-
122
- export {$e672e8bc247525d1$export$926850f6ecef79d0 as useDroppableCollectionState};
123
- //# sourceMappingURL=useDroppableCollectionState.module.js.map
@@ -1,123 +0,0 @@
1
- import {useState as $lyPoT$useState, useRef as $lyPoT$useRef, useCallback as $lyPoT$useCallback} from "react";
2
-
3
- /*
4
- * Copyright 2020 Adobe. All rights reserved.
5
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License. You may obtain a copy
7
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software distributed under
10
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
- * OF ANY KIND, either express or implied. See the License for the specific language
12
- * governing permissions and limitations under the License.
13
- */
14
- function $e672e8bc247525d1$export$926850f6ecef79d0(props) {
15
- 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;
16
- let [target, setTarget] = (0, $lyPoT$useState)(null);
17
- let targetRef = (0, $lyPoT$useRef)(null);
18
- let getOppositeTarget = (target)=>{
19
- if (target.dropPosition === 'before') {
20
- let node = collection.getItem(target.key);
21
- return node && node.prevKey != null ? {
22
- type: 'item',
23
- key: node.prevKey,
24
- dropPosition: 'after'
25
- } : null;
26
- } else if (target.dropPosition === 'after') {
27
- let node = collection.getItem(target.key);
28
- return node && node.nextKey != null ? {
29
- type: 'item',
30
- key: node.nextKey,
31
- dropPosition: 'before'
32
- } : null;
33
- }
34
- return null;
35
- };
36
- let defaultGetDropOperation = (0, $lyPoT$useCallback)((e)=>{
37
- let { target: target, types: types, allowedOperations: allowedOperations, isInternal: isInternal, draggingKeys: draggingKeys } = e;
38
- if (isDisabled || !target) return 'cancel';
39
- if (acceptedDragTypes === 'all' || acceptedDragTypes.some((type)=>types.has(type))) {
40
- let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');
41
- let isValidReorder = onReorder && target.type === 'item' && isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after') && $e672e8bc247525d1$var$isDraggingWithinParent(collection, target, draggingKeys);
42
- let isItemDropAllowed = target.type !== 'item' || target.dropPosition !== 'on' || !shouldAcceptItemDrop || shouldAcceptItemDrop(target, types);
43
- let isValidMove = onMove && target.type === 'item' && isInternal && isItemDropAllowed;
44
- // Feedback was that internal root drop was weird so preventing that from happening
45
- let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;
46
- // Automatically prevent items (i.e. folders) from being dropped on themselves.
47
- let isValidOnItemDrop = onItemDrop && target.type === 'item' && target.dropPosition === 'on' && !(isInternal && target.key != null && draggingKeys.has(target.key)) && isItemDropAllowed;
48
- if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {
49
- if (getDropOperation) return getDropOperation(target, types, allowedOperations);
50
- else return allowedOperations[0];
51
- }
52
- }
53
- return 'cancel';
54
- }, [
55
- isDisabled,
56
- collection,
57
- acceptedDragTypes,
58
- getDropOperation,
59
- onInsert,
60
- onRootDrop,
61
- onItemDrop,
62
- shouldAcceptItemDrop,
63
- onReorder,
64
- onMove,
65
- onDrop
66
- ]);
67
- return {
68
- collection: collection,
69
- selectionManager: selectionManager,
70
- isDisabled: isDisabled,
71
- target: target,
72
- setTarget (newTarget) {
73
- if (this.isDropTarget(newTarget)) return;
74
- let target = targetRef.current;
75
- if (target && typeof props.onDropExit === 'function') props.onDropExit({
76
- type: 'dropexit',
77
- x: 0,
78
- y: 0,
79
- target: target
80
- });
81
- if (newTarget && typeof onDropEnter === 'function') onDropEnter({
82
- type: 'dropenter',
83
- x: 0,
84
- y: 0,
85
- target: newTarget
86
- });
87
- targetRef.current = newTarget !== null && newTarget !== void 0 ? newTarget : null;
88
- setTarget(newTarget !== null && newTarget !== void 0 ? newTarget : null);
89
- },
90
- isDropTarget (dropTarget) {
91
- let target = targetRef.current;
92
- if (!target || !dropTarget) return false;
93
- if ($e672e8bc247525d1$var$isEqualDropTarget(dropTarget, target)) return true;
94
- // Check if the targets point at the same point between two items, one referring before, and the other after.
95
- if ((dropTarget === null || dropTarget === void 0 ? void 0 : dropTarget.type) === 'item' && (target === null || target === void 0 ? void 0 : target.type) === 'item' && dropTarget.key !== target.key && dropTarget.dropPosition !== target.dropPosition && dropTarget.dropPosition !== 'on' && target.dropPosition !== 'on') return $e672e8bc247525d1$var$isEqualDropTarget(getOppositeTarget(dropTarget), target) || $e672e8bc247525d1$var$isEqualDropTarget(dropTarget, getOppositeTarget(target));
96
- return false;
97
- },
98
- getDropOperation (e) {
99
- return defaultGetDropOperation(e);
100
- }
101
- };
102
- }
103
- function $e672e8bc247525d1$var$isEqualDropTarget(a, b) {
104
- if (!a) return !b;
105
- switch(a.type){
106
- case 'root':
107
- return (b === null || b === void 0 ? void 0 : b.type) === 'root';
108
- case 'item':
109
- return (b === null || b === void 0 ? void 0 : b.type) === 'item' && (b === null || b === void 0 ? void 0 : b.key) === a.key && (b === null || b === void 0 ? void 0 : b.dropPosition) === a.dropPosition;
110
- }
111
- }
112
- function $e672e8bc247525d1$var$isDraggingWithinParent(collection, target, draggingKeys) {
113
- let targetNode = collection.getItem(target.key);
114
- for (let key of draggingKeys){
115
- let node = collection.getItem(key);
116
- if ((node === null || node === void 0 ? void 0 : node.parentKey) !== (targetNode === null || targetNode === void 0 ? void 0 : targetNode.parentKey)) return false;
117
- }
118
- return true;
119
- }
120
-
121
-
122
- export {$e672e8bc247525d1$export$926850f6ecef79d0 as useDroppableCollectionState};
123
- //# sourceMappingURL=useDroppableCollectionState.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC;AA2CM,SAAS,0CAA4B,KAAsC;IAChF,IAAI,qBACF,oBAAoB,mBACpB,UAAU,YACV,QAAQ,cACR,UAAU,cACV,UAAU,aACV,SAAS,UACT,MAAM,wBACN,oBAAoB,cACpB,UAAU,oBACV,gBAAgB,eAChB,WAAW,oBACX,gBAAgB,UAChB,MAAM,EACP,GAAG;IACJ,IAAI,CAAC,QAAQ,UAAU,GAAG,CAAA,GAAA,eAAO,EAAqB;IACtD,IAAI,YAAY,CAAA,GAAA,aAAK,EAAqB;IAE1C,IAAI,oBAAoB,CAAC;QACvB,IAAI,OAAO,YAAY,KAAK,UAAU;YACpC,IAAI,OAAO,WAAW,OAAO,CAAC,OAAO,GAAG;YACxC,OAAO,QAAQ,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AO,CAAC;IAGV,OAAQ,EAAE,IAAI;QACZ,KAAK;YACH,OAAO,CAAA,cAAA,wBAAA,EAAG,IAAI,MAAK;QACrB,KAAK;YACH,OAAO,CAAA,cAAA,wBAAA,EAAG,IAAI,MAAK,UAAU,CAAA,cAAA,wBAAA,EAAG,GAAG,MAAK,EAAE,GAAG,IAAI,CAAA,cAAA,wBAAA,EAAG,YAAY,MAAK,EAAE,YAAY;IACvF;AACF;AAEA,SAAS,6CAAuB,UAAqC,EAAE,MAAsB,EAAE,YAAsB;IACnH,IAAI,aAAa,WAAW,OAAO,CAAC,OAAO,GAAG;IAE9C,KAAK,IAAI,OAAO,aAAc;QAC5B,IAAI,OAAO,WAAW,OAAO,CAAC;QAC9B,IAAI,CAAA,iBAAA,2BAAA,KAAM,SAAS,OAAK,uBAAA,iCAAA,WAAY,SAAS,GAC3C,OAAO;IAEX;IAEA,OAAO;AACT","sources":["packages/@react-stately/dnd/src/useDroppableCollectionState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection, DragTypes, DropOperation, DroppableCollectionProps, DropTarget, ItemDropTarget, Key, Node} from '@react-types/shared';\nimport {MultipleSelectionManager} from '@react-stately/selection';\nimport {useCallback, useRef, useState} from 'react';\n\ninterface DropOperationEvent {\n target: DropTarget,\n types: DragTypes,\n allowedOperations: DropOperation[],\n isInternal: boolean,\n draggingKeys: Set<Key>\n}\n\nexport interface DroppableCollectionStateOptions extends Omit<DroppableCollectionProps, 'onDropMove' | 'onDropActivate'> {\n /** A collection of items. */\n collection: Collection<Node<unknown>>,\n /** An interface for reading and updating multiple selection state. */\n selectionManager: MultipleSelectionManager,\n /** Whether the drop events should be disabled. */\n isDisabled?: boolean\n}\n\nexport interface DroppableCollectionState {\n /** A collection of items. */\n collection: Collection<Node<unknown>>,\n /** An interface for reading and updating multiple selection state. */\n selectionManager: MultipleSelectionManager,\n /** The current drop target. */\n target: DropTarget | null,\n /** Whether drop events are disabled. */\n isDisabled?: boolean,\n /** Sets the current drop target. */\n setTarget(target: DropTarget | null): void,\n /** Returns whether the given target is equivalent to the current drop target. */\n isDropTarget(target: DropTarget | null): boolean,\n /** Returns the drop operation for the given parameters. */\n getDropOperation(e: DropOperationEvent): DropOperation\n}\n\n/**\n * Manages state for a droppable collection.\n */\nexport function useDroppableCollectionState(props: DroppableCollectionStateOptions): DroppableCollectionState {\n let {\n acceptedDragTypes = 'all',\n isDisabled,\n onInsert,\n onRootDrop,\n onItemDrop,\n onReorder,\n onMove,\n shouldAcceptItemDrop,\n collection,\n selectionManager,\n onDropEnter,\n getDropOperation,\n onDrop\n } = props;\n let [target, setTarget] = useState<DropTarget | null>(null);\n let targetRef = useRef<DropTarget | null>(null);\n\n let getOppositeTarget = (target: ItemDropTarget): ItemDropTarget | null => {\n if (target.dropPosition === 'before') {\n let node = collection.getItem(target.key);\n return node && node.prevKey != null\n ? {type: 'item', key: node.prevKey, dropPosition: 'after'} \n : null;\n } else if (target.dropPosition === 'after') {\n let node = collection.getItem(target.key);\n return node && node.nextKey != null\n ? {type: 'item', key: node.nextKey, dropPosition: 'before'} \n : null;\n }\n return null;\n };\n\n let defaultGetDropOperation = useCallback((e: DropOperationEvent) => {\n let {\n target,\n types,\n allowedOperations,\n isInternal,\n draggingKeys\n } = e;\n\n if (isDisabled || !target) {\n return 'cancel';\n }\n\n if (acceptedDragTypes === 'all' || acceptedDragTypes.some(type => types.has(type))) {\n let isValidInsert = onInsert && target.type === 'item' && !isInternal && (target.dropPosition === 'before' || target.dropPosition === 'after');\n let isValidReorder = onReorder\n && target.type === 'item'\n && isInternal\n && (target.dropPosition === 'before' || target.dropPosition === 'after')\n && isDraggingWithinParent(collection, target, draggingKeys);\n\n let isItemDropAllowed = target.type !== 'item'\n || target.dropPosition !== 'on'\n || (!shouldAcceptItemDrop || shouldAcceptItemDrop(target, types));\n\n let isValidMove = onMove\n && target.type === 'item'\n && isInternal\n && isItemDropAllowed;\n\n // Feedback was that internal root drop was weird so preventing that from happening\n let isValidRootDrop = onRootDrop && target.type === 'root' && !isInternal;\n \n // Automatically prevent items (i.e. folders) from being dropped on themselves.\n let isValidOnItemDrop = onItemDrop \n && target.type === 'item' \n && target.dropPosition === 'on' \n && !(isInternal && target.key != null && draggingKeys.has(target.key)) \n && isItemDropAllowed;\n\n if (onDrop || isValidInsert || isValidReorder || isValidMove || isValidRootDrop || isValidOnItemDrop) {\n if (getDropOperation) {\n return getDropOperation(target, types, allowedOperations);\n } else {\n return allowedOperations[0];\n }\n }\n }\n\n return 'cancel';\n }, [isDisabled, collection, acceptedDragTypes, getDropOperation, onInsert, onRootDrop, onItemDrop, shouldAcceptItemDrop, onReorder, onMove, onDrop]);\n\n return {\n collection,\n selectionManager,\n isDisabled,\n target,\n setTarget(newTarget) {\n if (this.isDropTarget(newTarget)) {\n return;\n }\n\n let target = targetRef.current;\n if (target && typeof props.onDropExit === 'function') {\n props.onDropExit({\n type: 'dropexit',\n x: 0, // todo\n y: 0,\n target\n });\n }\n\n if (newTarget && typeof onDropEnter === 'function') {\n onDropEnter({\n type: 'dropenter',\n x: 0, // todo\n y: 0,\n target: newTarget\n });\n }\n\n targetRef.current = newTarget ?? null;\n setTarget(newTarget ?? null);\n },\n isDropTarget(dropTarget) {\n let target = targetRef.current;\n if (!target || !dropTarget) {\n return false;\n }\n if (isEqualDropTarget(dropTarget, target)) {\n return true;\n }\n\n // Check if the targets point at the same point between two items, one referring before, and the other after.\n if (\n dropTarget?.type === 'item' &&\n target?.type === 'item' &&\n dropTarget.key !== target.key &&\n dropTarget.dropPosition !== target.dropPosition &&\n dropTarget.dropPosition !== 'on' &&\n target.dropPosition !== 'on'\n ) {\n return isEqualDropTarget(getOppositeTarget(dropTarget), target) ||\n isEqualDropTarget(dropTarget, getOppositeTarget(target));\n }\n\n return false;\n },\n getDropOperation(e) {\n return defaultGetDropOperation(e);\n }\n };\n}\n\nfunction isEqualDropTarget(a?: DropTarget | null, b?: DropTarget | null) {\n if (!a) {\n return !b;\n }\n\n switch (a.type) {\n case 'root':\n return b?.type === 'root';\n case 'item':\n return b?.type === 'item' && b?.key === a.key && b?.dropPosition === a.dropPosition;\n }\n}\n\nfunction isDraggingWithinParent(collection: Collection<Node<unknown>>, target: ItemDropTarget, draggingKeys: Set<Key>) {\n let targetNode = collection.getItem(target.key);\n\n for (let key of draggingKeys) {\n let node = collection.getItem(key);\n if (node?.parentKey !== targetNode?.parentKey) {\n return false;\n }\n }\n\n return true;\n}\n"],"names":[],"version":3,"file":"useDroppableCollectionState.module.js.map"}
@@ -1,174 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {Collection, DraggableCollectionEndEvent, DraggableCollectionProps, DragItem, DragMoveEvent, DragPreviewRenderer, DragStartEvent, DropOperation, Key, Node, RefObject} from '@react-types/shared';
14
- import {MultipleSelectionManager} from '@react-stately/selection';
15
- import {useRef, useState} from 'react';
16
-
17
- export interface DraggableCollectionStateOptions<T = object> extends DraggableCollectionProps<T> {
18
- /** A collection of items. */
19
- collection: Collection<Node<unknown>>,
20
- /** An interface for reading and updating multiple selection state. */
21
- selectionManager: MultipleSelectionManager,
22
- /** Whether the drag events should be disabled. */
23
- isDisabled?: boolean
24
- }
25
-
26
- export interface DraggableCollectionState {
27
- /** A collection of items. */
28
- collection: Collection<Node<unknown>>,
29
- /** An interface for reading and updating multiple selection state. */
30
- selectionManager: MultipleSelectionManager,
31
- /** The key of the item that initiated a drag. */
32
- draggedKey: Key | null,
33
- /** The keys of the items that are currently being dragged. */
34
- draggingKeys: Set<Key>,
35
- /** Whether drag events are disabled. */
36
- isDisabled?: boolean,
37
- /** Returns whether the given key is currently being dragged. */
38
- isDragging(key: Key): boolean,
39
- /** Returns the keys of the items that will be dragged with the given key (e.g. selected items). */
40
- getKeysForDrag(key: Key): Set<Key>,
41
- /** Returns the items to drag for the given key. */
42
- getItems(key: Key): DragItem[],
43
- /** The ref of the element that will be rendered as the drag preview while dragging. */
44
- preview?: RefObject<DragPreviewRenderer | null>,
45
- /** Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed. */
46
- getAllowedDropOperations?: () => DropOperation[],
47
- /** Begins a drag for the given key. This triggers the onDragStart event. */
48
- startDrag(key: Key, event: DragStartEvent): void,
49
- /** Triggers an onDragMove event. */
50
- moveDrag(event: DragMoveEvent): void,
51
- /** Ends the current drag, and emits an onDragEnd event. */
52
- endDrag(event: DraggableCollectionEndEvent): void
53
- }
54
-
55
- /**
56
- * Manages state for a draggable collection.
57
- */
58
- export function useDraggableCollectionState<T = object>(props: DraggableCollectionStateOptions<T>): DraggableCollectionState {
59
- let {
60
- getItems,
61
- isDisabled,
62
- collection,
63
- selectionManager,
64
- onDragStart,
65
- onDragMove,
66
- onDragEnd,
67
- preview,
68
- getAllowedDropOperations
69
- } = props;
70
- let [, setDragging] = useState(false);
71
- let draggingKeys = useRef(new Set<Key>());
72
- let draggedKey = useRef<Key | null>(null);
73
- let getKeys = (key: Key) => {
74
- // The clicked item is always added to the drag. If it is selected, then all of the
75
- // other selected items are also dragged. If it is not selected, then only the clicked
76
- // item is dragged. This matches native macOS behavior.
77
- // Additionally, we filter out any keys that are children of any of the other selected keys
78
- let keys = new Set<Key>();
79
- if (selectionManager.isSelected(key)) {
80
- for (let currentKey of selectionManager.selectedKeys) {
81
- let node = collection.getItem(currentKey);
82
- if (node) {
83
- let isChild = false;
84
- let parentKey = node.parentKey;
85
- while (parentKey != null) {
86
- // eslint-disable-next-line max-depth
87
- if (selectionManager.selectedKeys.has(parentKey)) {
88
- isChild = true;
89
- break;
90
- }
91
- let parentNode = collection.getItem(parentKey);
92
- parentKey = parentNode ? parentNode.parentKey : null;
93
- }
94
-
95
- if (!isChild) {
96
- keys.add(currentKey);
97
- }
98
- }
99
- }
100
- } else {
101
- keys.add(key);
102
- }
103
-
104
- return keys;
105
- };
106
-
107
- return {
108
- collection,
109
- selectionManager,
110
- get draggedKey() {
111
- return draggedKey.current;
112
- },
113
- get draggingKeys() {
114
- return draggingKeys.current;
115
- },
116
- isDragging(key) {
117
- return draggingKeys.current.has(key);
118
- },
119
- getKeysForDrag: getKeys,
120
- getItems(key) {
121
- let keys = getKeys(key);
122
- let items: any[] = [];
123
- for (let key of keys) {
124
- let value = collection.getItem(key)?.value;
125
- if (value != null) {
126
- items.push(value);
127
- }
128
- }
129
-
130
- return getItems(getKeys(key), items);
131
- },
132
- isDisabled,
133
- preview,
134
- getAllowedDropOperations,
135
- startDrag(key, event) {
136
- let keys = getKeys(key);
137
- draggingKeys.current = keys;
138
- draggedKey.current = key;
139
- selectionManager.setFocused(false);
140
- setDragging(true);
141
- if (typeof onDragStart === 'function') {
142
- onDragStart({
143
- ...event,
144
- keys
145
- });
146
- }
147
- },
148
- moveDrag(event) {
149
- if (typeof onDragMove === 'function') {
150
- onDragMove({
151
- ...event,
152
- keys: draggingKeys.current
153
- });
154
- }
155
- },
156
- endDrag(event) {
157
- let {
158
- isInternal
159
- } = event;
160
-
161
- if (typeof onDragEnd === 'function') {
162
- onDragEnd({
163
- ...event,
164
- keys: draggingKeys.current,
165
- isInternal
166
- });
167
- }
168
-
169
- draggingKeys.current = new Set();
170
- draggedKey.current = null;
171
- setDragging(false);
172
- }
173
- };
174
- }