@os-design/drag-sort 1.0.13 → 1.0.15
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.
- package/dist/cjs/DragAndDrop.js +50 -72
- package/dist/cjs/DragAndDrop.js.map +1 -1
- package/dist/cjs/Draggable.js +19 -36
- package/dist/cjs/Draggable.js.map +1 -1
- package/dist/cjs/Droppable.js +24 -33
- package/dist/cjs/Droppable.js.map +1 -1
- package/dist/cjs/index.js +0 -9
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/ListStore.js +4 -13
- package/dist/cjs/utils/ListStore.js.map +1 -1
- package/dist/cjs/utils/NodeList.js +25 -56
- package/dist/cjs/utils/NodeList.js.map +1 -1
- package/dist/cjs/utils/getElementOffset.js +0 -4
- package/dist/cjs/utils/getElementOffset.js.map +1 -1
- package/dist/cjs/utils/getElementScroll.js +0 -4
- package/dist/cjs/utils/getElementScroll.js.map +1 -1
- package/dist/cjs/utils/getNodeRect.js +9 -18
- package/dist/cjs/utils/getNodeRect.js.map +1 -1
- package/dist/cjs/utils/useAppendClassName.js +0 -3
- package/dist/cjs/utils/useAppendClassName.js.map +1 -1
- package/dist/cjs/utils/useBlankNode.js +17 -26
- package/dist/cjs/utils/useBlankNode.js.map +1 -1
- package/dist/cjs/utils/useDragAndDrop.js +0 -8
- package/dist/cjs/utils/useDragAndDrop.js.map +1 -1
- package/dist/cjs/utils/useDragEffect.js +104 -163
- package/dist/cjs/utils/useDragEffect.js.map +1 -1
- package/dist/cjs/utils/useDroppable.js +0 -8
- package/dist/cjs/utils/useDroppable.js.map +1 -1
- package/dist/cjs/utils/useGeneratedId.js +0 -3
- package/dist/cjs/utils/useGeneratedId.js.map +1 -1
- package/dist/cjs/utils/useGetNodeStyle.js +0 -3
- package/dist/cjs/utils/useGetNodeStyle.js.map +1 -1
- package/dist/cjs/utils/useInitRect.js +0 -3
- package/dist/cjs/utils/useInitRect.js.map +1 -1
- package/dist/cjs/utils/useInitScrollOffset.js +0 -5
- package/dist/cjs/utils/useInitScrollOffset.js.map +1 -1
- package/dist/cjs/utils/useMoveNode.js +23 -48
- package/dist/cjs/utils/useMoveNode.js.map +1 -1
- package/dist/cjs/utils/useScrollEventByPoint.js +2 -10
- package/dist/cjs/utils/useScrollEventByPoint.js.map +1 -1
- package/dist/cjs/utils/useTargetList.js +8 -21
- package/dist/cjs/utils/useTargetList.js.map +1 -1
- package/dist/cjs/utils/useTransitionStyle.js +2 -5
- package/dist/cjs/utils/useTransitionStyle.js.map +1 -1
- package/dist/esm/DragAndDrop.js +19 -12
- package/dist/esm/DragAndDrop.js.map +1 -1
- package/dist/esm/Draggable.js +10 -9
- package/dist/esm/Draggable.js.map +1 -1
- package/dist/esm/Droppable.js +17 -14
- package/dist/esm/Droppable.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/ListStore.js +3 -8
- package/dist/esm/utils/ListStore.js.map +1 -1
- package/dist/esm/utils/NodeList.js +17 -36
- package/dist/esm/utils/NodeList.js.map +1 -1
- package/dist/esm/utils/getElementOffset.js +0 -3
- package/dist/esm/utils/getElementOffset.js.map +1 -1
- package/dist/esm/utils/getElementScroll.js +0 -3
- package/dist/esm/utils/getElementScroll.js.map +1 -1
- package/dist/esm/utils/getNodeRect.js +1 -2
- package/dist/esm/utils/getNodeRect.js.map +1 -1
- package/dist/esm/utils/useAppendClassName.js +1 -2
- package/dist/esm/utils/useAppendClassName.js.map +1 -1
- package/dist/esm/utils/useBlankNode.js +10 -9
- package/dist/esm/utils/useBlankNode.js.map +1 -1
- package/dist/esm/utils/useDragAndDrop.js +0 -2
- package/dist/esm/utils/useDragAndDrop.js.map +1 -1
- package/dist/esm/utils/useDragEffect.js +57 -60
- package/dist/esm/utils/useDragEffect.js.map +1 -1
- package/dist/esm/utils/useDroppable.js +0 -2
- package/dist/esm/utils/useDroppable.js.map +1 -1
- package/dist/esm/utils/useGeneratedId.js +0 -2
- package/dist/esm/utils/useGeneratedId.js.map +1 -1
- package/dist/esm/utils/useGetNodeStyle.js +0 -2
- package/dist/esm/utils/useGetNodeStyle.js.map +1 -1
- package/dist/esm/utils/useInitRect.js +0 -2
- package/dist/esm/utils/useInitRect.js.map +1 -1
- package/dist/esm/utils/useInitScrollOffset.js +0 -2
- package/dist/esm/utils/useInitScrollOffset.js.map +1 -1
- package/dist/esm/utils/useMoveNode.js +1 -11
- package/dist/esm/utils/useMoveNode.js.map +1 -1
- package/dist/esm/utils/useScrollEventByPoint.js +0 -5
- package/dist/esm/utils/useScrollEventByPoint.js.map +1 -1
- package/dist/esm/utils/useTargetList.js +2 -3
- package/dist/esm/utils/useTargetList.js.map +1 -1
- package/dist/esm/utils/useTransitionStyle.js +0 -2
- package/dist/esm/utils/useTransitionStyle.js.map +1 -1
- package/package.json +11 -11
|
@@ -4,37 +4,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _react = require("react");
|
|
9
|
-
|
|
10
8
|
var _useThrottle3 = _interopRequireDefault(require("@os-design/use-throttle"));
|
|
11
|
-
|
|
12
9
|
var _useScrollEventByPoint = _interopRequireDefault(require("./useScrollEventByPoint"));
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
-
|
|
16
11
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
17
|
-
|
|
18
12
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
19
|
-
|
|
20
13
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
21
|
-
|
|
22
14
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
23
|
-
|
|
24
15
|
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
25
|
-
|
|
26
16
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
27
|
-
|
|
28
17
|
var useTargetList = function useTargetList(position, listStoreRef) {
|
|
29
18
|
var _useState = (0, _react.useState)(0),
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
20
|
+
number = _useState2[0],
|
|
21
|
+
setNumber = _useState2[1];
|
|
34
22
|
var targetList = (0, _react.useMemo)(function () {
|
|
35
23
|
if (!position || !listStoreRef.current) return null;
|
|
36
24
|
var x = position.x,
|
|
37
|
-
|
|
25
|
+
y = position.y;
|
|
38
26
|
var list = listStoreRef.current.findByPosition(x, y);
|
|
39
27
|
return list || null;
|
|
40
28
|
}, [listStoreRef, position, number]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
@@ -43,16 +31,15 @@ var useTargetList = function useTargetList(position, listStoreRef) {
|
|
|
43
31
|
setNumber(function (n) {
|
|
44
32
|
return n + 1;
|
|
45
33
|
});
|
|
46
|
-
}, []);
|
|
34
|
+
}, []);
|
|
47
35
|
|
|
36
|
+
// Update the target list if one of the elements located under the specified point has been scrolled
|
|
48
37
|
var _useThrottle = (0, _useThrottle3["default"])(forceUpdate, 100),
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
38
|
+
_useThrottle2 = _slicedToArray(_useThrottle, 1),
|
|
39
|
+
throttledForceUpdate = _useThrottle2[0];
|
|
52
40
|
(0, _useScrollEventByPoint["default"])(position, throttledForceUpdate);
|
|
53
41
|
return targetList;
|
|
54
42
|
};
|
|
55
|
-
|
|
56
43
|
var _default = useTargetList;
|
|
57
44
|
exports["default"] = _default;
|
|
58
45
|
//# sourceMappingURL=useTargetList.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTargetList.js","names":["useTargetList","position","listStoreRef","useState","number","setNumber","targetList","useMemo","current","x","y","list","findByPosition","forceUpdate","useCallback","n","useThrottle","throttledForceUpdate","useScrollEventByPoint"],"sources":["../../../src/utils/useTargetList.ts"],"sourcesContent":["import { RefObject, useCallback, useMemo, useState } from 'react';\nimport useThrottle from '@os-design/use-throttle';\nimport { Position } from '@os-design/use-drag';\nimport ListStore from './ListStore';\nimport useScrollEventByPoint from './useScrollEventByPoint';\n\nconst useTargetList = (\n position: Position | null,\n listStoreRef: RefObject<ListStore>\n) => {\n const [number, setNumber] = useState(0);\n\n const targetList = useMemo(() => {\n if (!position || !listStoreRef.current) return null;\n const { x, y } = position;\n const list = listStoreRef.current.findByPosition(x, y);\n return list || null;\n }, [listStoreRef, position, number]); // eslint-disable-line react-hooks/exhaustive-deps\n\n const forceUpdate = useCallback(() => {\n setNumber((n) => n + 1);\n }, []);\n\n // Update the target list if one of the elements located under the specified point has been scrolled\n const [throttledForceUpdate] = useThrottle(forceUpdate, 100);\n useScrollEventByPoint(position, throttledForceUpdate);\n\n return targetList;\n};\n\nexport default useTargetList;\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTargetList.js","names":["useTargetList","position","listStoreRef","useState","number","setNumber","targetList","useMemo","current","x","y","list","findByPosition","forceUpdate","useCallback","n","useThrottle","throttledForceUpdate","useScrollEventByPoint"],"sources":["../../../src/utils/useTargetList.ts"],"sourcesContent":["import { RefObject, useCallback, useMemo, useState } from 'react';\nimport useThrottle from '@os-design/use-throttle';\nimport { Position } from '@os-design/use-drag';\nimport ListStore from './ListStore';\nimport useScrollEventByPoint from './useScrollEventByPoint';\n\nconst useTargetList = (\n position: Position | null,\n listStoreRef: RefObject<ListStore>\n) => {\n const [number, setNumber] = useState(0);\n\n const targetList = useMemo(() => {\n if (!position || !listStoreRef.current) return null;\n const { x, y } = position;\n const list = listStoreRef.current.findByPosition(x, y);\n return list || null;\n }, [listStoreRef, position, number]); // eslint-disable-line react-hooks/exhaustive-deps\n\n const forceUpdate = useCallback(() => {\n setNumber((n) => n + 1);\n }, []);\n\n // Update the target list if one of the elements located under the specified point has been scrolled\n const [throttledForceUpdate] = useThrottle(forceUpdate, 100);\n useScrollEventByPoint(position, throttledForceUpdate);\n\n return targetList;\n};\n\nexport default useTargetList;\n"],"mappings":";;;;;;AAAA;AACA;AAGA;AAA4D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAE5D,IAAMA,aAAa,GAAG,SAAhBA,aAAa,CACjBC,QAAyB,EACzBC,YAAkC,EAC/B;EACH,gBAA4B,IAAAC,eAAQ,EAAC,CAAC,CAAC;IAAA;IAAhCC,MAAM;IAAEC,SAAS;EAExB,IAAMC,UAAU,GAAG,IAAAC,cAAO,EAAC,YAAM;IAC/B,IAAI,CAACN,QAAQ,IAAI,CAACC,YAAY,CAACM,OAAO,EAAE,OAAO,IAAI;IACnD,IAAQC,CAAC,GAAQR,QAAQ,CAAjBQ,CAAC;MAAEC,CAAC,GAAKT,QAAQ,CAAdS,CAAC;IACZ,IAAMC,IAAI,GAAGT,YAAY,CAACM,OAAO,CAACI,cAAc,CAACH,CAAC,EAAEC,CAAC,CAAC;IACtD,OAAOC,IAAI,IAAI,IAAI;EACrB,CAAC,EAAE,CAACT,YAAY,EAAED,QAAQ,EAAEG,MAAM,CAAC,CAAC,CAAC,CAAC;;EAEtC,IAAMS,WAAW,GAAG,IAAAC,kBAAW,EAAC,YAAM;IACpCT,SAAS,CAAC,UAACU,CAAC;MAAA,OAAKA,CAAC,GAAG,CAAC;IAAA,EAAC;EACzB,CAAC,EAAE,EAAE,CAAC;;EAEN;EACA,mBAA+B,IAAAC,wBAAW,EAACH,WAAW,EAAE,GAAG,CAAC;IAAA;IAArDI,oBAAoB;EAC3B,IAAAC,iCAAqB,EAACjB,QAAQ,EAAEgB,oBAAoB,CAAC;EAErD,OAAOX,UAAU;AACnB,CAAC;AAAC,eAEaN,aAAa;AAAA"}
|
|
@@ -4,16 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _react = require("react");
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
9
|
* Creates the global style for setting the transition delay.
|
|
12
10
|
*/
|
|
13
11
|
var useTransitionStyle = function useTransitionStyle(props) {
|
|
14
12
|
var className = props.className,
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
ms = props.ms,
|
|
14
|
+
enabled = props.enabled;
|
|
17
15
|
(0, _react.useEffect)(function () {
|
|
18
16
|
if (!enabled) return function () {};
|
|
19
17
|
var style = document.createElement('style');
|
|
@@ -24,7 +22,6 @@ var useTransitionStyle = function useTransitionStyle(props) {
|
|
|
24
22
|
};
|
|
25
23
|
}, [className, enabled, ms]);
|
|
26
24
|
};
|
|
27
|
-
|
|
28
25
|
var _default = useTransitionStyle;
|
|
29
26
|
exports["default"] = _default;
|
|
30
27
|
//# sourceMappingURL=useTransitionStyle.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTransitionStyle.js","names":["useTransitionStyle","props","className","ms","enabled","useEffect","style","document","createElement","innerHTML","child","head","appendChild","removeChild"],"sources":["../../../src/utils/useTransitionStyle.ts"],"sourcesContent":["import { useEffect } from 'react';\n\ninterface UseTransitionStyleProps {\n className: string;\n ms: number;\n enabled: boolean;\n}\n\n/**\n * Creates the global style for setting the transition delay.\n */\nconst useTransitionStyle = (props: UseTransitionStyleProps) => {\n const { className, ms, enabled } = props;\n\n useEffect(() => {\n if (!enabled) return () => {};\n const style = document.createElement('style');\n style.innerHTML = `.${className}{transition:transform ${ms}ms;}`;\n const child = document.head.appendChild(style);\n return () => {\n document.head.removeChild(child);\n };\n }, [className, enabled, ms]);\n};\n\nexport default useTransitionStyle;\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTransitionStyle.js","names":["useTransitionStyle","props","className","ms","enabled","useEffect","style","document","createElement","innerHTML","child","head","appendChild","removeChild"],"sources":["../../../src/utils/useTransitionStyle.ts"],"sourcesContent":["import { useEffect } from 'react';\n\ninterface UseTransitionStyleProps {\n className: string;\n ms: number;\n enabled: boolean;\n}\n\n/**\n * Creates the global style for setting the transition delay.\n */\nconst useTransitionStyle = (props: UseTransitionStyleProps) => {\n const { className, ms, enabled } = props;\n\n useEffect(() => {\n if (!enabled) return () => {};\n const style = document.createElement('style');\n style.innerHTML = `.${className}{transition:transform ${ms}ms;}`;\n const child = document.head.appendChild(style);\n return () => {\n document.head.removeChild(child);\n };\n }, [className, enabled, ms]);\n};\n\nexport default useTransitionStyle;\n"],"mappings":";;;;;;AAAA;AAQA;AACA;AACA;AACA,IAAMA,kBAAkB,GAAG,SAArBA,kBAAkB,CAAIC,KAA8B,EAAK;EAC7D,IAAQC,SAAS,GAAkBD,KAAK,CAAhCC,SAAS;IAAEC,EAAE,GAAcF,KAAK,CAArBE,EAAE;IAAEC,OAAO,GAAKH,KAAK,CAAjBG,OAAO;EAE9B,IAAAC,gBAAS,EAAC,YAAM;IACd,IAAI,CAACD,OAAO,EAAE,OAAO,YAAM,CAAC,CAAC;IAC7B,IAAME,KAAK,GAAGC,QAAQ,CAACC,aAAa,CAAC,OAAO,CAAC;IAC7CF,KAAK,CAACG,SAAS,cAAOP,SAAS,mCAAyBC,EAAE,SAAM;IAChE,IAAMO,KAAK,GAAGH,QAAQ,CAACI,IAAI,CAACC,WAAW,CAACN,KAAK,CAAC;IAC9C,OAAO,YAAM;MACXC,QAAQ,CAACI,IAAI,CAACE,WAAW,CAACH,KAAK,CAAC;IAClC,CAAC;EACH,CAAC,EAAE,CAACR,SAAS,EAAEE,OAAO,EAAED,EAAE,CAAC,CAAC;AAC9B,CAAC;AAAC,eAEaH,kBAAkB;AAAA"}
|
package/dist/esm/DragAndDrop.js
CHANGED
|
@@ -10,7 +10,6 @@ import useDragEffect from './utils/useDragEffect';
|
|
|
10
10
|
import useGeneratedId from './utils/useGeneratedId';
|
|
11
11
|
import useTransitionStyle from './utils/useTransitionStyle';
|
|
12
12
|
import useInitRect from './utils/useInitRect';
|
|
13
|
-
|
|
14
13
|
/**
|
|
15
14
|
* The container containing one or more lists with draggable nodes.
|
|
16
15
|
*/
|
|
@@ -26,26 +25,31 @@ const DragAndDrop = ({
|
|
|
26
25
|
}) => {
|
|
27
26
|
// The user can drag a node between the lists (the Droppable components).
|
|
28
27
|
// To determine which list a node should be dropped in, we need to store refs to all the lists.
|
|
29
|
-
const listStoreRef = useRef(new ListStore());
|
|
28
|
+
const listStoreRef = useRef(new ListStore());
|
|
30
29
|
|
|
30
|
+
// The class name for a node used to set the transition style
|
|
31
31
|
const generatedId = useGeneratedId();
|
|
32
|
-
const nodeClassName = useMemo(() => `n${generatedId}`, [generatedId]);
|
|
32
|
+
const nodeClassName = useMemo(() => `n${generatedId}`, [generatedId]);
|
|
33
33
|
|
|
34
|
+
// The node that is currently being dragged
|
|
34
35
|
const [draggedNode, setDraggedNode] = useState(null);
|
|
35
36
|
const startNodeRef = useRef(null);
|
|
36
37
|
const [cursorPosition, setCursorPosition] = useState({
|
|
37
38
|
x: 0,
|
|
38
39
|
y: 0
|
|
39
|
-
});
|
|
40
|
+
});
|
|
40
41
|
|
|
42
|
+
// Add a new list to the store
|
|
41
43
|
const registerList = useCallback(list => {
|
|
42
44
|
listStoreRef.current.add(list);
|
|
43
|
-
}, []);
|
|
45
|
+
}, []);
|
|
44
46
|
|
|
47
|
+
// Remove the existing list from the store
|
|
45
48
|
const deregisterList = useCallback(id => {
|
|
46
49
|
listStoreRef.current.remove(id);
|
|
47
|
-
}, []);
|
|
50
|
+
}, []);
|
|
48
51
|
|
|
52
|
+
// Set the style to delay transitions when the node is dragging
|
|
49
53
|
useTransitionStyle({
|
|
50
54
|
className: nodeClassName,
|
|
51
55
|
ms: transitionDelayMs,
|
|
@@ -83,8 +87,9 @@ const DragAndDrop = ({
|
|
|
83
87
|
onDragEnd: dragEndHandler,
|
|
84
88
|
minMouseDistPx,
|
|
85
89
|
longPressMs
|
|
86
|
-
});
|
|
90
|
+
});
|
|
87
91
|
|
|
92
|
+
// Handlers that determine whether a user clicks on the node
|
|
88
93
|
const mouseDownHandler = useCallback((e, list, node) => {
|
|
89
94
|
startNodeRef.current = {
|
|
90
95
|
list,
|
|
@@ -98,18 +103,21 @@ const DragAndDrop = ({
|
|
|
98
103
|
node
|
|
99
104
|
};
|
|
100
105
|
onTouchStart(e);
|
|
101
|
-
}, [onTouchStart]);
|
|
102
|
-
// It's important to attach the event to the body (not to the document). Otherwise, it won't work in mobile chrome.
|
|
106
|
+
}, [onTouchStart]);
|
|
103
107
|
|
|
104
|
-
|
|
108
|
+
// Prevent body scrolling when the node is dragging.
|
|
109
|
+
// It's important to attach the event to the body (not to the document). Otherwise, it won't work in mobile chrome.
|
|
110
|
+
usePreventDefaultEvent(document.body, 'touchmove', !!draggedNode);
|
|
105
111
|
|
|
112
|
+
// Implement the drag animation
|
|
106
113
|
useDragEffect({
|
|
107
114
|
draggedNode,
|
|
108
115
|
cursorPosition,
|
|
109
116
|
listStoreRef,
|
|
110
117
|
onDragEnd
|
|
111
|
-
});
|
|
118
|
+
});
|
|
112
119
|
|
|
120
|
+
// Auto scroll if the cursor position is located near the border
|
|
113
121
|
useAutoScroll({
|
|
114
122
|
enabled: !!draggedNode,
|
|
115
123
|
distPercent: autoScrollDistPercent,
|
|
@@ -140,6 +148,5 @@ const DragAndDrop = ({
|
|
|
140
148
|
}
|
|
141
149
|
})));
|
|
142
150
|
};
|
|
143
|
-
|
|
144
151
|
export default DragAndDrop;
|
|
145
152
|
//# sourceMappingURL=DragAndDrop.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DragAndDrop.js","names":["React","useCallback","useMemo","useRef","useState","usePreventDefaultEvent","useMemoObject","useDrag","Portal","useAutoScroll","DragAndDropContext","ListStore","useDragEffect","useGeneratedId","useTransitionStyle","useInitRect","DragAndDrop","draggedNodeContainer","minMouseDistPx","longPressMs","autoScrollDistPercent","autoScrollMaxSpeedPx","transitionDelayMs","onDragEnd","children","listStoreRef","generatedId","nodeClassName","draggedNode","setDraggedNode","startNodeRef","cursorPosition","setCursorPosition","x","y","registerList","list","current","add","deregisterList","id","remove","className","ms","enabled","onDragStart","pos","startPos","startNode","nodeRef","node","rect","getBoundingClientRect","position","onDragMove","dragEndHandler","onMouseDown","onTouchStart","mouseDownHandler","e","touchStartHandler","document","body","distPercent","maxSpeedPx","initRect","undefined","dragAndDropContext","renderDraggedNode","index","style","left","top","width","initWidth","height","initHeight","zIndex"],"sources":["../../src/DragAndDrop.tsx"],"sourcesContent":["import React, {\n useCallback,\n useMemo,\n useRef,\n useState,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport usePreventDefaultEvent from '@os-design/use-prevent-default-event';\nimport useMemoObject from '@os-design/use-memo-object';\nimport useDrag, {\n Position,\n OnDragEnd,\n OnDragMove,\n OnDragStart,\n} from '@os-design/use-drag';\nimport Portal, { PortalProps } from '@os-design/portal';\nimport useAutoScroll from '@os-design/use-auto-scroll';\nimport { DragAndDropContext } from './utils/useDragAndDrop';\nimport ListStore from './utils/ListStore';\nimport NodeList, { ExistingNode } from './utils/NodeList';\nimport useDragEffect, {\n DragEndHandler,\n DraggedNode,\n} from './utils/useDragEffect';\nimport useGeneratedId from './utils/useGeneratedId';\nimport useTransitionStyle from './utils/useTransitionStyle';\nimport useInitRect from './utils/useInitRect';\n\nexport interface DragAndDropProps {\n /**\n * The container in which the dragged node will be rendered.\n * @default document.body\n */\n draggedNodeContainer?: PortalProps['container'];\n /**\n * The min distance required to start dragging a node (in px).\n * @default 10\n */\n minMouseDistPx?: number;\n /**\n * The delay of the long press event required to start dragging a node on the touch device (in ms).\n * @default 500\n */\n longPressMs?: number;\n /**\n * The distance to the bound at which the list starts to scroll automatically (in percent).\n * @default 20\n */\n autoScrollDistPercent?: number;\n /**\n * The max auto scroll speed (in px).\n * @default 100\n */\n autoScrollMaxSpeedPx?: number;\n /**\n * The animation duration (in ms).\n * @default 250\n */\n transitionDelayMs?: number;\n /**\n * The callback that is called when the drag is completed.\n * @default undefined\n */\n onDragEnd?: DragEndHandler;\n /**\n * The children.\n * @default undefined\n */\n children?: React.ReactNode;\n}\n\ninterface StartNode {\n list: NodeList;\n node: ExistingNode;\n}\n\n/**\n * The container containing one or more lists with draggable nodes.\n */\nconst DragAndDrop: React.FC<DragAndDropProps> = ({\n draggedNodeContainer,\n minMouseDistPx = 10,\n longPressMs = 500,\n autoScrollDistPercent = 20,\n autoScrollMaxSpeedPx = 100,\n transitionDelayMs = 250,\n onDragEnd = () => {},\n children,\n}) => {\n // The user can drag a node between the lists (the Droppable components).\n // To determine which list a node should be dropped in, we need to store refs to all the lists.\n const listStoreRef = useRef<ListStore>(new ListStore());\n\n // The class name for a node used to set the transition style\n const generatedId = useGeneratedId();\n const nodeClassName = useMemo(() => `n${generatedId}`, [generatedId]);\n\n // The node that is currently being dragged\n const [draggedNode, setDraggedNode] = useState<DraggedNode | null>(null);\n const startNodeRef = useRef<StartNode | null>(null);\n const [cursorPosition, setCursorPosition] = useState<Position>({\n x: 0,\n y: 0,\n });\n\n // Add a new list to the store\n const registerList = useCallback((list: NodeList) => {\n listStoreRef.current.add(list);\n }, []);\n\n // Remove the existing list from the store\n const deregisterList = useCallback((id: string) => {\n listStoreRef.current.remove(id);\n }, []);\n\n // Set the style to delay transitions when the node is dragging\n useTransitionStyle({\n className: nodeClassName,\n ms: transitionDelayMs,\n enabled: !!draggedNode,\n });\n\n const onDragStart = useCallback<OnDragStart>(\n (pos: Position, startPos: Position) => {\n const startNode = startNodeRef.current;\n if (!startNode) return;\n const [, , nodeRef] = startNode.node;\n if (!nodeRef.current) return;\n const rect = nodeRef.current.getBoundingClientRect();\n setCursorPosition(pos);\n setDraggedNode({\n list: startNode.list,\n node: startNode.node,\n position: {\n x: startPos.x - rect.x,\n y: startPos.y - rect.y,\n },\n });\n startNodeRef.current = null;\n },\n []\n );\n\n const onDragMove = useCallback<OnDragMove>((pos: Position) => {\n setCursorPosition(pos);\n }, []);\n\n const dragEndHandler = useCallback<OnDragEnd>(() => {\n setDraggedNode(null);\n }, []);\n\n const { onMouseDown, onTouchStart } = useDrag({\n onDragStart,\n onDragMove,\n onDragEnd: dragEndHandler,\n minMouseDistPx,\n longPressMs,\n });\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent, list: NodeList, node: ExistingNode) => {\n startNodeRef.current = { list, node };\n onMouseDown(e);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent, list: NodeList, node: ExistingNode) => {\n startNodeRef.current = { list, node };\n onTouchStart(e);\n },\n [onTouchStart]\n );\n\n // Prevent body scrolling when the node is dragging.\n // It's important to attach the event to the body (not to the document). Otherwise, it won't work in mobile chrome.\n usePreventDefaultEvent(document.body, 'touchmove', !!draggedNode);\n\n // Implement the drag animation\n useDragEffect({\n draggedNode,\n cursorPosition,\n listStoreRef,\n onDragEnd,\n });\n\n // Auto scroll if the cursor position is located near the border\n useAutoScroll({\n enabled: !!draggedNode,\n distPercent: autoScrollDistPercent,\n maxSpeedPx: autoScrollMaxSpeedPx,\n });\n\n const initRect = useInitRect(draggedNode ? draggedNode.node[2] : undefined);\n\n const dragAndDropContext = useMemoObject({\n registerList,\n deregisterList,\n onMouseDown: mouseDownHandler,\n onTouchStart: touchStartHandler,\n nodeClassName,\n });\n\n return (\n <DragAndDropContext.Provider value={dragAndDropContext}>\n {children}\n\n {draggedNode && (\n <Portal container={draggedNodeContainer}>\n {draggedNode.list.renderDraggedNode({\n index: draggedNode.node[4],\n id: draggedNode.node[5],\n style: {\n position: 'fixed',\n left: cursorPosition.x - draggedNode.position.x,\n top: cursorPosition.y - draggedNode.position.y,\n width: initRect ? initRect.initWidth : undefined,\n height: initRect ? initRect.initHeight : undefined,\n zIndex: 1001,\n },\n })}\n </Portal>\n )}\n </DragAndDropContext.Provider>\n );\n};\n\nexport type { DragEndHandler } from './utils/useDragEffect';\n\nexport default DragAndDrop;\n"],"mappings":"AAAA,OAAOA,KAAP,IACEC,WADF,EAEEC,OAFF,EAGEC,MAHF,EAIEC,QAJF,QAOO,OAPP;AAQA,OAAOC,sBAAP,MAAmC,sCAAnC;AACA,OAAOC,aAAP,MAA0B,4BAA1B;AACA,OAAOC,OAAP,MAKO,qBALP;AAMA,OAAOC,MAAP,MAAoC,mBAApC;AACA,OAAOC,aAAP,MAA0B,4BAA1B;AACA,SAASC,kBAAT,QAAmC,wBAAnC;AACA,OAAOC,SAAP,MAAsB,mBAAtB;AAEA,OAAOC,aAAP,MAGO,uBAHP;AAIA,OAAOC,cAAP,MAA2B,wBAA3B;AACA,OAAOC,kBAAP,MAA+B,4BAA/B;AACA,OAAOC,WAAP,MAAwB,qBAAxB;;AAkDA;AACA;AACA;AACA,MAAMC,WAAuC,GAAG,CAAC;EAC/CC,oBAD+C;EAE/CC,cAAc,GAAG,EAF8B;EAG/CC,WAAW,GAAG,GAHiC;EAI/CC,qBAAqB,GAAG,EAJuB;EAK/CC,oBAAoB,GAAG,GALwB;EAM/CC,iBAAiB,GAAG,GAN2B;EAO/CC,SAAS,GAAG,MAAM,CAAE,CAP2B;EAQ/CC;AAR+C,CAAD,KAS1C;EACJ;EACA;E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BN,WAAW,CAACyB,QAAZ,CAAqBnB,CAHxC;MAILuC,KAAK,EAAER,QAAQ,GAAGA,QAAQ,CAACS,SAAZ,GAAwBR,SAJlC;MAKLS,MAAM,EAAEV,QAAQ,GAAGA,QAAQ,CAACW,UAAZ,GAAyBV,SALpC;MAMLW,MAAM,EAAE;IANH;EAH2B,CAAnC,CADH,CAJJ,CADF;AAsBD,CAnJD;;AAuJA,eAAe7D,WAAf"}
|
|
1
|
+
{"version":3,"file":"DragAndDrop.js","names":["React","useCallback","useMemo","useRef","useState","usePreventDefaultEvent","useMemoObject","useDrag","Portal","useAutoScroll","DragAndDropContext","ListStore","useDragEffect","useGeneratedId","useTransitionStyle","useInitRect","DragAndDrop","draggedNodeContainer","minMouseDistPx","longPressMs","autoScrollDistPercent","autoScrollMaxSpeedPx","transitionDelayMs","onDragEnd","children","listStoreRef","generatedId","nodeClassName","draggedNode","setDraggedNode","startNodeRef","cursorPosition","setCursorPosition","x","y","registerList","list","current","add","deregisterList","id","remove","className","ms","enabled","onDragStart","pos","startPos","startNode","nodeRef","node","rect","getBoundingClientRect","position","onDragMove","dragEndHandler","onMouseDown","onTouchStart","mouseDownHandler","e","touchStartHandler","document","body","distPercent","maxSpeedPx","initRect","undefined","dragAndDropContext","renderDraggedNode","index","style","left","top","width","initWidth","height","initHeight","zIndex"],"sources":["../../src/DragAndDrop.tsx"],"sourcesContent":["import React, {\n useCallback,\n useMemo,\n useRef,\n useState,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport usePreventDefaultEvent from '@os-design/use-prevent-default-event';\nimport useMemoObject from '@os-design/use-memo-object';\nimport useDrag, {\n Position,\n OnDragEnd,\n OnDragMove,\n OnDragStart,\n} from '@os-design/use-drag';\nimport Portal, { PortalProps } from '@os-design/portal';\nimport useAutoScroll from '@os-design/use-auto-scroll';\nimport { DragAndDropContext } from './utils/useDragAndDrop';\nimport ListStore from './utils/ListStore';\nimport NodeList, { ExistingNode } from './utils/NodeList';\nimport useDragEffect, {\n DragEndHandler,\n DraggedNode,\n} from './utils/useDragEffect';\nimport useGeneratedId from './utils/useGeneratedId';\nimport useTransitionStyle from './utils/useTransitionStyle';\nimport useInitRect from './utils/useInitRect';\n\nexport interface DragAndDropProps {\n /**\n * The container in which the dragged node will be rendered.\n * @default document.body\n */\n draggedNodeContainer?: PortalProps['container'];\n /**\n * The min distance required to start dragging a node (in px).\n * @default 10\n */\n minMouseDistPx?: number;\n /**\n * The delay of the long press event required to start dragging a node on the touch device (in ms).\n * @default 500\n */\n longPressMs?: number;\n /**\n * The distance to the bound at which the list starts to scroll automatically (in percent).\n * @default 20\n */\n autoScrollDistPercent?: number;\n /**\n * The max auto scroll speed (in px).\n * @default 100\n */\n autoScrollMaxSpeedPx?: number;\n /**\n * The animation duration (in ms).\n * @default 250\n */\n transitionDelayMs?: number;\n /**\n * The callback that is called when the drag is completed.\n * @default undefined\n */\n onDragEnd?: DragEndHandler;\n /**\n * The children.\n * @default undefined\n */\n children?: React.ReactNode;\n}\n\ninterface StartNode {\n list: NodeList;\n node: ExistingNode;\n}\n\n/**\n * The container containing one or more lists with draggable nodes.\n */\nconst DragAndDrop: React.FC<DragAndDropProps> = ({\n draggedNodeContainer,\n minMouseDistPx = 10,\n longPressMs = 500,\n autoScrollDistPercent = 20,\n autoScrollMaxSpeedPx = 100,\n transitionDelayMs = 250,\n onDragEnd = () => {},\n children,\n}) => {\n // The user can drag a node between the lists (the Droppable components).\n // To determine which list a node should be dropped in, we need to store refs to all the lists.\n const listStoreRef = useRef<ListStore>(new ListStore());\n\n // The class name for a node used to set the transition style\n const generatedId = useGeneratedId();\n const nodeClassName = useMemo(() => `n${generatedId}`, [generatedId]);\n\n // The node that is currently being dragged\n const [draggedNode, setDraggedNode] = useState<DraggedNode | null>(null);\n const startNodeRef = useRef<StartNode | null>(null);\n const [cursorPosition, setCursorPosition] = useState<Position>({\n x: 0,\n y: 0,\n });\n\n // Add a new list to the store\n const registerList = useCallback((list: NodeList) => {\n listStoreRef.current.add(list);\n }, []);\n\n // Remove the existing list from the store\n const deregisterList = useCallback((id: string) => {\n listStoreRef.current.remove(id);\n }, []);\n\n // Set the style to delay transitions when the node is dragging\n useTransitionStyle({\n className: nodeClassName,\n ms: transitionDelayMs,\n enabled: !!draggedNode,\n });\n\n const onDragStart = useCallback<OnDragStart>(\n (pos: Position, startPos: Position) => {\n const startNode = startNodeRef.current;\n if (!startNode) return;\n const [, , nodeRef] = startNode.node;\n if (!nodeRef.current) return;\n const rect = nodeRef.current.getBoundingClientRect();\n setCursorPosition(pos);\n setDraggedNode({\n list: startNode.list,\n node: startNode.node,\n position: {\n x: startPos.x - rect.x,\n y: startPos.y - rect.y,\n },\n });\n startNodeRef.current = null;\n },\n []\n );\n\n const onDragMove = useCallback<OnDragMove>((pos: Position) => {\n setCursorPosition(pos);\n }, []);\n\n const dragEndHandler = useCallback<OnDragEnd>(() => {\n setDraggedNode(null);\n }, []);\n\n const { onMouseDown, onTouchStart } = useDrag({\n onDragStart,\n onDragMove,\n onDragEnd: dragEndHandler,\n minMouseDistPx,\n longPressMs,\n });\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent, list: NodeList, node: ExistingNode) => {\n startNodeRef.current = { list, node };\n onMouseDown(e);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent, list: NodeList, node: ExistingNode) => {\n startNodeRef.current = { list, node };\n onTouchStart(e);\n },\n [onTouchStart]\n );\n\n // Prevent body scrolling when the node is dragging.\n // It's important to attach the event to the body (not to the document). Otherwise, it won't work in mobile chrome.\n usePreventDefaultEvent(document.body, 'touchmove', !!draggedNode);\n\n // Implement the drag animation\n useDragEffect({\n draggedNode,\n cursorPosition,\n listStoreRef,\n onDragEnd,\n });\n\n // Auto scroll if the cursor position is located near the border\n useAutoScroll({\n enabled: !!draggedNode,\n distPercent: autoScrollDistPercent,\n maxSpeedPx: autoScrollMaxSpeedPx,\n });\n\n const initRect = useInitRect(draggedNode ? draggedNode.node[2] : undefined);\n\n const dragAndDropContext = useMemoObject({\n registerList,\n deregisterList,\n onMouseDown: mouseDownHandler,\n onTouchStart: touchStartHandler,\n nodeClassName,\n });\n\n return (\n <DragAndDropContext.Provider value={dragAndDropContext}>\n {children}\n\n {draggedNode && (\n <Portal container={draggedNodeContainer}>\n {draggedNode.list.renderDraggedNode({\n index: draggedNode.node[4],\n id: draggedNode.node[5],\n style: {\n position: 'fixed',\n left: cursorPosition.x - draggedNode.position.x,\n top: cursorPosition.y - draggedNode.position.y,\n width: initRect ? initRect.initWidth : undefined,\n height: initRect ? initRect.initHeight : undefined,\n zIndex: 1001,\n },\n })}\n </Portal>\n )}\n </DragAndDropContext.Provider>\n );\n};\n\nexport type { DragEndHandler } from './utils/useDragEffect';\n\nexport default DragAndDrop;\n"],"mappings":"AAAA,OAAOA,KAAK,IACVC,WAAW,EACXC,OAAO,EACPC,MAAM,EACNC,QAAQ,QAGH,OAAO;AACd,OAAOC,sBAAsB,MAAM,sCAAsC;AACzE,OAAOC,aAAa,MAAM,4BAA4B;AACtD,OAAOC,OAAO,MAKP,qBAAqB;AAC5B,OAAOC,MAAM,MAAuB,mBAAmB;AACvD,OAAOC,aAAa,MAAM,4BAA4B;AACtD,SAASC,kBAAkB,QAAQ,wBAAwB;AAC3D,OAAOC,SAAS,MAAM,mBAAmB;AAEzC,OAAOC,aAAa,MAGb,uBAAuB;AAC9B,OAAOC,cAAc,MAAM,wBAAwB;AACnD,OAAOC,kBAAkB,MAAM,4BAA4B;AAC3D,OAAOC,WAAW,MAAM,qBAAqB;AAkD7C;AACA;AACA;AACA,MAAMC,WAAuC,GAAG,CAAC;EAC/CC,oBAAoB;EACpBC,cAAc,GAAG,EAAE;EACnBC,WAAW,GAAG,GAAG;EACjBC,qBAAqB,GAAG,EAAE;EAC1BC,oBAAoB,GAAG,GAAG;EAC1BC,iBAAiB,GAAG,GAAG;EACvBC,SAAS,GAAG,MAAM,CAAC,CAAC;EACpBC;AACF,CAAC,KAA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MAClDW,MAAM,EAAE;IACV;EACF,CAAC,CAAC,CAEL,CAC2B;AAElC,CAAC;AAID,eAAe7D,WAAW"}
|
package/dist/esm/Draggable.js
CHANGED
|
@@ -3,17 +3,16 @@ import useMemoObject from '@os-design/use-memo-object';
|
|
|
3
3
|
import useDroppable from './utils/useDroppable';
|
|
4
4
|
import useDragAndDrop from './utils/useDragAndDrop';
|
|
5
5
|
import useAppendClassName from './utils/useAppendClassName';
|
|
6
|
-
|
|
7
6
|
const Draggable = ({
|
|
8
7
|
index,
|
|
9
8
|
id,
|
|
10
9
|
children
|
|
11
10
|
}) => {
|
|
12
11
|
// The reference to the list item
|
|
13
|
-
const ref = useRef(null);
|
|
14
|
-
|
|
15
|
-
const nodeRef = useRef(null);
|
|
16
|
-
|
|
12
|
+
const ref = useRef(null);
|
|
13
|
+
// The reference to the node containing the refs to the prev and next nodes
|
|
14
|
+
const nodeRef = useRef(null);
|
|
15
|
+
// Additional styles for moving the list item
|
|
17
16
|
const [style, setStyle] = useState({});
|
|
18
17
|
const {
|
|
19
18
|
nodeClassName
|
|
@@ -23,8 +22,9 @@ const Draggable = ({
|
|
|
23
22
|
deregisterNode,
|
|
24
23
|
onMouseDown,
|
|
25
24
|
onTouchStart
|
|
26
|
-
} = useDroppable();
|
|
25
|
+
} = useDroppable();
|
|
27
26
|
|
|
27
|
+
// Register the node in the list
|
|
28
28
|
useEffect(() => {
|
|
29
29
|
nodeRef.current = registerNode({
|
|
30
30
|
ref,
|
|
@@ -37,10 +37,12 @@ const Draggable = ({
|
|
|
37
37
|
deregisterNode(nodeRef.current);
|
|
38
38
|
nodeRef.current = null;
|
|
39
39
|
};
|
|
40
|
-
}, [deregisterNode, id, index, registerNode]);
|
|
40
|
+
}, [deregisterNode, id, index, registerNode]);
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
// Set the class name for the node to apply the transition style (see the DragAndDrop container)
|
|
43
|
+
useAppendClassName(ref, nodeClassName);
|
|
43
44
|
|
|
45
|
+
// Handlers that determine whether a user clicks on the node
|
|
44
46
|
const mouseDownHandler = useCallback(e => {
|
|
45
47
|
if (!nodeRef.current) return;
|
|
46
48
|
onMouseDown(e, nodeRef.current);
|
|
@@ -59,6 +61,5 @@ const Draggable = ({
|
|
|
59
61
|
handlers
|
|
60
62
|
}));
|
|
61
63
|
};
|
|
62
|
-
|
|
63
64
|
export default Draggable;
|
|
64
65
|
//# sourceMappingURL=Draggable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Draggable.js","names":["React","useCallback","useEffect","useRef","useState","useMemoObject","useDroppable","useDragAndDrop","useAppendClassName","Draggable","index","id","children","ref","nodeRef","style","setStyle","nodeClassName","registerNode","deregisterNode","onMouseDown","onTouchStart","current","mouseDownHandler","e","touchStartHandler","handlers"],"sources":["../../src/Draggable.tsx"],"sourcesContent":["import React, {\n CSSProperties,\n RefObject,\n useCallback,\n useEffect,\n useRef,\n useState,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport useMemoObject from '@os-design/use-memo-object';\nimport { Node } from './utils/NodeList';\nimport useDroppable from './utils/useDroppable';\nimport useDragAndDrop from './utils/useDragAndDrop';\nimport useAppendClassName from './utils/useAppendClassName';\n\nexport interface DraggableHandlers {\n /**\n * The handler that should be called when the mouse down event occurs.\n */\n onMouseDown: (e: MouseEvent) => void;\n /**\n * The handler that should be called when the touch start event occurs.\n */\n onTouchStart: (e: TouchEvent) => void;\n}\n\nexport interface DraggableChildrenProps {\n /**\n * The reference to the draggable list item.\n */\n ref: RefObject<any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n /**\n * Additional styles for moving the list item.\n */\n style: CSSProperties;\n /**\n * The handlers to catch events when the user starts dragging the list item.\n */\n handlers: DraggableHandlers;\n}\n\nexport interface DraggableProps {\n /**\n * The index of the draggable node.\n */\n index: number;\n /**\n * The ID of the draggable node.\n */\n id: string;\n /**\n * The function that renders the draggable node.\n */\n children: (props: DraggableChildrenProps) => React.ReactNode;\n}\n\nconst Draggable: React.FC<DraggableProps> = ({ index, id, children }) => {\n // The reference to the list item\n const ref = useRef<HTMLDivElement | null>(null);\n // The reference to the node containing the refs to the prev and next nodes\n const nodeRef = useRef<Node>(null);\n // Additional styles for moving the list item\n const [style, setStyle] = useState<CSSProperties>({});\n\n const { nodeClassName } = useDragAndDrop();\n const { registerNode, deregisterNode, onMouseDown, onTouchStart } =\n useDroppable();\n\n // Register the node in the list\n useEffect(() => {\n nodeRef.current = registerNode({ ref, setStyle, index, id });\n return () => {\n if (!nodeRef.current) return;\n deregisterNode(nodeRef.current);\n nodeRef.current = null;\n };\n }, [deregisterNode, id, index, registerNode]);\n\n // Set the class name for the node to apply the transition style (see the DragAndDrop container)\n useAppendClassName(ref, nodeClassName);\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent) => {\n if (!nodeRef.current) return;\n onMouseDown(e, nodeRef.current);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent) => {\n if (!nodeRef.current) return;\n onTouchStart(e, nodeRef.current);\n },\n [onTouchStart]\n );\n\n const handlers = useMemoObject({\n onMouseDown: mouseDownHandler,\n onTouchStart: touchStartHandler,\n });\n\n return <>{children({ ref, style, handlers })}</>;\n};\n\nexport default Draggable;\n"],"mappings":"AAAA,OAAOA,
|
|
1
|
+
{"version":3,"file":"Draggable.js","names":["React","useCallback","useEffect","useRef","useState","useMemoObject","useDroppable","useDragAndDrop","useAppendClassName","Draggable","index","id","children","ref","nodeRef","style","setStyle","nodeClassName","registerNode","deregisterNode","onMouseDown","onTouchStart","current","mouseDownHandler","e","touchStartHandler","handlers"],"sources":["../../src/Draggable.tsx"],"sourcesContent":["import React, {\n CSSProperties,\n RefObject,\n useCallback,\n useEffect,\n useRef,\n useState,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport useMemoObject from '@os-design/use-memo-object';\nimport { Node } from './utils/NodeList';\nimport useDroppable from './utils/useDroppable';\nimport useDragAndDrop from './utils/useDragAndDrop';\nimport useAppendClassName from './utils/useAppendClassName';\n\nexport interface DraggableHandlers {\n /**\n * The handler that should be called when the mouse down event occurs.\n */\n onMouseDown: (e: MouseEvent) => void;\n /**\n * The handler that should be called when the touch start event occurs.\n */\n onTouchStart: (e: TouchEvent) => void;\n}\n\nexport interface DraggableChildrenProps {\n /**\n * The reference to the draggable list item.\n */\n ref: RefObject<any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n /**\n * Additional styles for moving the list item.\n */\n style: CSSProperties;\n /**\n * The handlers to catch events when the user starts dragging the list item.\n */\n handlers: DraggableHandlers;\n}\n\nexport interface DraggableProps {\n /**\n * The index of the draggable node.\n */\n index: number;\n /**\n * The ID of the draggable node.\n */\n id: string;\n /**\n * The function that renders the draggable node.\n */\n children: (props: DraggableChildrenProps) => React.ReactNode;\n}\n\nconst Draggable: React.FC<DraggableProps> = ({ index, id, children }) => {\n // The reference to the list item\n const ref = useRef<HTMLDivElement | null>(null);\n // The reference to the node containing the refs to the prev and next nodes\n const nodeRef = useRef<Node>(null);\n // Additional styles for moving the list item\n const [style, setStyle] = useState<CSSProperties>({});\n\n const { nodeClassName } = useDragAndDrop();\n const { registerNode, deregisterNode, onMouseDown, onTouchStart } =\n useDroppable();\n\n // Register the node in the list\n useEffect(() => {\n nodeRef.current = registerNode({ ref, setStyle, index, id });\n return () => {\n if (!nodeRef.current) return;\n deregisterNode(nodeRef.current);\n nodeRef.current = null;\n };\n }, [deregisterNode, id, index, registerNode]);\n\n // Set the class name for the node to apply the transition style (see the DragAndDrop container)\n useAppendClassName(ref, nodeClassName);\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent) => {\n if (!nodeRef.current) return;\n onMouseDown(e, nodeRef.current);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent) => {\n if (!nodeRef.current) return;\n onTouchStart(e, nodeRef.current);\n },\n [onTouchStart]\n );\n\n const handlers = useMemoObject({\n onMouseDown: mouse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CAAa,IAAK;IACjB,IAAI,CAACV,OAAO,CAACQ,OAAO,EAAE;IACtBF,WAAW,CAACI,CAAC,EAAEV,OAAO,CAACQ,OAAO,CAAC;EACjC,CAAC,EACD,CAACF,WAAW,CAAC,CACd;EACD,MAAMK,iBAAiB,GAAGxB,WAAW,CAClCuB,CAAa,IAAK;IACjB,IAAI,CAACV,OAAO,CAACQ,OAAO,EAAE;IACtBD,YAAY,CAACG,CAAC,EAAEV,OAAO,CAACQ,OAAO,CAAC;EAClC,CAAC,EACD,CAACD,YAAY,CAAC,CACf;EAED,MAAMK,QAAQ,GAAGrB,aAAa,CAAC;IAC7Be,WAAW,EAAEG,gBAAgB;IAC7BF,YAAY,EAAEI;EAChB,CAAC,CAAC;EAEF,oBAAO,0CAAGb,QAAQ,CAAC;IAAEC,GAAG;IAAEE,KAAK;IAAEW;EAAS,CAAC,CAAC,CAAI;AAClD,CAAC;AAED,eAAejB,SAAS"}
|
package/dist/esm/Droppable.js
CHANGED
|
@@ -4,7 +4,6 @@ import NodeList from './utils/NodeList';
|
|
|
4
4
|
import useDragAndDrop from './utils/useDragAndDrop';
|
|
5
5
|
import { DroppableContext } from './utils/useDroppable';
|
|
6
6
|
import useGeneratedId from './utils/useGeneratedId';
|
|
7
|
-
|
|
8
7
|
const Droppable = ({
|
|
9
8
|
renderDraggedNode,
|
|
10
9
|
id,
|
|
@@ -12,30 +11,33 @@ const Droppable = ({
|
|
|
12
11
|
children
|
|
13
12
|
}) => {
|
|
14
13
|
// The reference to the list
|
|
15
|
-
const ref = useRef(null);
|
|
16
|
-
|
|
17
|
-
const innerRef = useRef(null);
|
|
18
|
-
|
|
14
|
+
const ref = useRef(null);
|
|
15
|
+
// The reference to the container inside the virtual list
|
|
16
|
+
const innerRef = useRef(null);
|
|
17
|
+
// The unique ID of the list. If no ID was specified, the generated one is used.
|
|
19
18
|
const generatedId = useGeneratedId();
|
|
20
|
-
const droppableId = useMemo(() => id || generatedId, [generatedId, id]);
|
|
21
|
-
|
|
19
|
+
const droppableId = useMemo(() => id || generatedId, [generatedId, id]);
|
|
20
|
+
// The reference to the list of nodes
|
|
22
21
|
const listRef = useRef(new NodeList({
|
|
23
22
|
id: droppableId,
|
|
24
23
|
ref,
|
|
25
24
|
innerRef,
|
|
26
25
|
horizontal,
|
|
27
26
|
renderDraggedNode
|
|
28
|
-
}));
|
|
27
|
+
}));
|
|
29
28
|
|
|
29
|
+
// Update the ID of the list if it changes
|
|
30
30
|
useEffect(() => {
|
|
31
31
|
listRef.current.id = droppableId;
|
|
32
|
-
}, [droppableId]);
|
|
33
|
-
// There is no need to check the order of the nodes in the list because it will be the same.
|
|
32
|
+
}, [droppableId]);
|
|
34
33
|
|
|
34
|
+
// Update the list orientation if it changes.
|
|
35
|
+
// There is no need to check the order of the nodes in the list because it will be the same.
|
|
35
36
|
useEffect(() => {
|
|
36
37
|
listRef.current.horizontal = horizontal;
|
|
37
|
-
}, [horizontal]);
|
|
38
|
+
}, [horizontal]);
|
|
38
39
|
|
|
40
|
+
// Update the callback that renders the dragged node if it changes
|
|
39
41
|
useEffect(() => {
|
|
40
42
|
listRef.current.renderDraggedNode = renderDraggedNode;
|
|
41
43
|
}, [renderDraggedNode]);
|
|
@@ -44,8 +46,9 @@ const Droppable = ({
|
|
|
44
46
|
deregisterList,
|
|
45
47
|
onMouseDown,
|
|
46
48
|
onTouchStart
|
|
47
|
-
} = useDragAndDrop();
|
|
49
|
+
} = useDragAndDrop();
|
|
48
50
|
|
|
51
|
+
// Register the list in the store
|
|
49
52
|
useEffect(() => {
|
|
50
53
|
registerList(listRef.current);
|
|
51
54
|
const listId = listRef.current.id;
|
|
@@ -54,8 +57,9 @@ const Droppable = ({
|
|
|
54
57
|
const registerNode = useCallback(props => listRef.current.add(props), []);
|
|
55
58
|
const deregisterNode = useCallback(node => {
|
|
56
59
|
listRef.current.remove(node);
|
|
57
|
-
}, []);
|
|
60
|
+
}, []);
|
|
58
61
|
|
|
62
|
+
// Handlers that determine whether a user clicks on the node
|
|
59
63
|
const mouseDownHandler = useCallback((e, node) => {
|
|
60
64
|
onMouseDown(e, listRef.current, node);
|
|
61
65
|
}, [onMouseDown]);
|
|
@@ -75,6 +79,5 @@ const Droppable = ({
|
|
|
75
79
|
innerRef
|
|
76
80
|
}));
|
|
77
81
|
};
|
|
78
|
-
|
|
79
82
|
export default Droppable;
|
|
80
83
|
//# sourceMappingURL=Droppable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Droppable.js","names":["React","useCallback","useEffect","useMemo","useRef","useMemoObject","NodeList","useDragAndDrop","DroppableContext","useGeneratedId","Droppable","renderDraggedNode","id","horizontal","children","ref","innerRef","generatedId","droppableId","listRef","current","registerList","deregisterList","onMouseDown","onTouchStart","listId","registerNode","props","add","deregisterNode","node","remove","mouseDownHandler","e","touchStartHandler","droppableContext"],"sources":["../../src/Droppable.tsx"],"sourcesContent":["import React, {\n RefObject,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport useMemoObject from '@os-design/use-memo-object';\nimport NodeList, {\n ExistingNode,\n NodeProps,\n RenderDraggedNode,\n} from './utils/NodeList';\nimport useDragAndDrop from './utils/useDragAndDrop';\nimport { DroppableContext } from './utils/useDroppable';\nimport useGeneratedId from './utils/useGeneratedId';\n\nexport interface DroppableChildrenProps {\n /**\n * The reference to the list.\n * If a virtual list is used, pass it to the outerRef prop.\n */\n ref: RefObject<HTMLDivElement>;\n /**\n * The reference to the container inside the virtual list.\n * Pass it to the innerRef prop.\n */\n innerRef: RefObject<HTMLDivElement>;\n}\n\nexport interface DroppableProps {\n /**\n * The function that renders the dragged node.\n */\n renderDraggedNode: RenderDraggedNode;\n /**\n * The ID of the list with draggable nodes.\n * Used to determine in which list the dragged node was dropped.\n * @default undefined\n */\n id?: string;\n /**\n * Whether the list is horizontal.\n * @default false\n */\n horizontal?: boolean;\n /**\n * The function that renders the list with draggable nodes.\n */\n children: (props: DroppableChildrenProps) => React.ReactNode;\n}\n\nconst Droppable: React.FC<DroppableProps> = ({\n renderDraggedNode,\n id,\n horizontal = false,\n children,\n}) => {\n // The reference to the list\n const ref = useRef<HTMLDivElement>(null);\n // The reference to the container inside the virtual list\n const innerRef = useRef<HTMLDivElement>(null);\n // The unique ID of the list. If no ID was specified, the generated one is used.\n const generatedId = useGeneratedId();\n const droppableId = useMemo(() => id || generatedId, [generatedId, id]);\n // The reference to the list of nodes\n const listRef = useRef(\n new NodeList({\n id: droppableId,\n ref,\n innerRef,\n horizontal,\n renderDraggedNode,\n })\n );\n\n // Update the ID of the list if it changes\n useEffect(() => {\n listRef.current.id = droppableId;\n }, [droppableId]);\n\n // Update the list orientation if it changes.\n // There is no need to check the order of the nodes in the list because it will be the same.\n useEffect(() => {\n listRef.current.horizontal = horizontal;\n }, [horizontal]);\n\n // Update the callback that renders the dragged node if it changes\n useEffect(() => {\n listRef.current.renderDraggedNode = renderDraggedNode;\n }, [renderDraggedNode]);\n\n const { registerList, deregisterList, onMouseDown, onTouchStart } =\n useDragAndDrop();\n\n // Register the list in the store\n useEffect(() => {\n registerList(listRef.current);\n const listId = listRef.current.id;\n return () => deregisterList(listId);\n }, [deregisterList, registerList]);\n\n const registerNode = useCallback(\n (props: NodeProps) => listRef.current.add(props),\n []\n );\n const deregisterNode = useCallback((node: ExistingNode) => {\n listRef.current.remove(node);\n }, []);\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent, node: ExistingNode) => {\n onMouseDown(e, listRef.current, node);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent, node: ExistingNode) => {\n onTouchStart(e, listRef.current, node);\n },\n [onTouchStart]\n );\n\n const droppableContext = useMemoObject({\n registerNode,\n deregisterNode,\n onMouseDown: mouseDownHandler,\n onTouchStart: touchStartHandler,\n });\n\n return (\n <DroppableContext.Provider value={droppableContext}>\n {(children as Function)({ ref, innerRef })}\n </DroppableContext.Provider>\n );\n};\n\nexport default Droppable;\n"],"mappings":"AAAA,OAAOA,
|
|
1
|
+
{"version":3,"file":"Droppable.js","names":["React","useCallback","useEffect","useMemo","useRef","useMemoObject","NodeList","useDragAndDrop","DroppableContext","useGeneratedId","Droppable","renderDraggedNode","id","horizontal","children","ref","innerRef","generatedId","droppableId","listRef","current","registerList","deregisterList","onMouseDown","onTouchStart","listId","registerNode","props","add","deregisterNode","node","remove","mouseDownHandler","e","touchStartHandler","droppableContext"],"sources":["../../src/Droppable.tsx"],"sourcesContent":["import React, {\n RefObject,\n useCallback,\n useEffect,\n useMemo,\n useRef,\n MouseEvent,\n TouchEvent,\n} from 'react';\nimport useMemoObject from '@os-design/use-memo-object';\nimport NodeList, {\n ExistingNode,\n NodeProps,\n RenderDraggedNode,\n} from './utils/NodeList';\nimport useDragAndDrop from './utils/useDragAndDrop';\nimport { DroppableContext } from './utils/useDroppable';\nimport useGeneratedId from './utils/useGeneratedId';\n\nexport interface DroppableChildrenProps {\n /**\n * The reference to the list.\n * If a virtual list is used, pass it to the outerRef prop.\n */\n ref: RefObject<HTMLDivElement>;\n /**\n * The reference to the container inside the virtual list.\n * Pass it to the innerRef prop.\n */\n innerRef: RefObject<HTMLDivElement>;\n}\n\nexport interface DroppableProps {\n /**\n * The function that renders the dragged node.\n */\n renderDraggedNode: RenderDraggedNode;\n /**\n * The ID of the list with draggable nodes.\n * Used to determine in which list the dragged node was dropped.\n * @default undefined\n */\n id?: string;\n /**\n * Whether the list is horizontal.\n * @default false\n */\n horizontal?: boolean;\n /**\n * The function that renders the list with draggable nodes.\n */\n children: (props: DroppableChildrenProps) => React.ReactNode;\n}\n\nconst Droppable: React.FC<DroppableProps> = ({\n renderDraggedNode,\n id,\n horizontal = false,\n children,\n}) => {\n // The reference to the list\n const ref = useRef<HTMLDivElement>(null);\n // The reference to the container inside the virtual list\n const innerRef = useRef<HTMLDivElement>(null);\n // The unique ID of the list. If no ID was specified, the generated one is used.\n const generatedId = useGeneratedId();\n const droppableId = useMemo(() => id || generatedId, [generatedId, id]);\n // The reference to the list of nodes\n const listRef = useRef(\n new NodeList({\n id: droppableId,\n ref,\n innerRef,\n horizontal,\n renderDraggedNode,\n })\n );\n\n // Update the ID of the list if it changes\n useEffect(() => {\n listRef.current.id = droppableId;\n }, [droppableId]);\n\n // Update the list orientation if it changes.\n // There is no need to check the order of the nodes in the list because it will be the same.\n useEffect(() => {\n listRef.current.horizontal = horizontal;\n }, [horizontal]);\n\n // Update the callback that renders the dragged node if it changes\n useEffect(() => {\n listRef.current.renderDraggedNode = renderDraggedNode;\n }, [renderDraggedNode]);\n\n const { registerList, deregisterList, onMouseDown, onTouchStart } =\n useDragAndDrop();\n\n // Register the list in the store\n useEffect(() => {\n registerList(listRef.current);\n const listId = listRef.current.id;\n return () => deregisterList(listId);\n }, [deregisterList, registerList]);\n\n const registerNode = useCallback(\n (props: NodeProps) => listRef.current.add(props),\n []\n );\n const deregisterNode = useCallback((node: ExistingNode) => {\n listRef.current.remove(node);\n }, []);\n\n // Handlers that determine whether a user clicks on the node\n const mouseDownHandler = useCallback(\n (e: MouseEvent, node: ExistingNode) => {\n onMouseDown(e, listRef.current, node);\n },\n [onMouseDown]\n );\n const touchStartHandler = useCallback(\n (e: TouchEvent, node: ExistingNode) => {\n onTouchStart(e, listRef.current, node);\n },\n [onTouchStart]\n );\n\n const droppableContext = useMemoObject({\n registerNode,\n deregisterNode,\n onMouseDown: mouseDownHandler,\n onTouchStart: touchStartHandler,\n });\n\n return (\n <DroppableContext.Provider value={droppableContext}>\n {(children as Function)({ ref, innerRef })}\n </DroppableContext.Provider>\n );\n};\n\nexport default Droppable;\n"],"mappings":"AAAA,OAAOA,KAAK,IAEVC,WAAW,EACXC,SAAS,EACTC,OAAO,EACPC,MAAM,QAGD,OAAO;AACd,OAAOC,aAAa,MAAM,4BAA4B;AACtD,OAAOC,QAAQ,MAIR,kBAAkB;AACzB,OAAOC,cAAc,MAAM,wBAAwB;AACnD,SAASC,gBAAgB,QAAQ,sBAAsB;AAC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C,EAAE,CAACH,cAAc,EAAED,YAAY,CAAC,CAAC;EAElC,MAAMK,YAAY,GAAGzB,WAAW,CAC7B0B,KAAgB,IAAKR,OAAO,CAACC,OAAO,CAACQ,GAAG,CAACD,KAAK,CAAC,EAChD,EAAE,CACH;EACD,MAAME,cAAc,GAAG5B,WAAW,CAAE6B,IAAkB,IAAK;IACzDX,OAAO,CAACC,OAAO,CAACW,MAAM,CAACD,IAAI,CAAC;EAC9B,CAAC,EAAE,EAAE,CAAC;;EAEN;EACA,MAAME,gBAAgB,GAAG/B,WAAW,CAClC,CAACgC,CAAa,EAAEH,IAAkB,KAAK;IACrCP,WAAW,CAACU,CAAC,EAAEd,OAAO,CAACC,OAAO,EAAEU,IAAI,CAAC;EACvC,CAAC,EACD,CAACP,WAAW,CAAC,CACd;EACD,MAAMW,iBAAiB,GAAGjC,WAAW,CACnC,CAACgC,CAAa,EAAEH,IAAkB,KAAK;IACrCN,YAAY,CAACS,CAAC,EAAEd,OAAO,CAACC,OAAO,EAAEU,IAAI,CAAC;EACxC,CAAC,EACD,CAACN,YAAY,CAAC,CACf;EAED,MAAMW,gBAAgB,GAAG9B,aAAa,CAAC;IACrCqB,YAAY;IACZG,cAAc;IACdN,WAAW,EAAES,gBAAgB;IAC7BR,YAAY,EAAEU;EAChB,CAAC,CAAC;EAEF,oBACE,oBAAC,gBAAgB,CAAC,QAAQ;IAAC,KAAK,EAAEC;EAAiB,GAC/CrB,QAAQ,CAAc;IAAEC,GAAG;IAAEC;EAAS,CAAC,CAAC,CAChB;AAEhC,CAAC;AAED,eAAeN,SAAS"}
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["default","DragAndDrop","Droppable","Draggable"],"sources":["../../src/index.ts"],"sourcesContent":["export { default as DragAndDrop } from './DragAndDrop';\nexport { default as Droppable } from './Droppable';\nexport { default as Draggable } from './Draggable';\n\nexport * from './DragAndDrop';\nexport * from './Droppable';\nexport * from './Draggable';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,
|
|
1
|
+
{"version":3,"file":"index.js","names":["default","DragAndDrop","Droppable","Draggable"],"sources":["../../src/index.ts"],"sourcesContent":["export { default as DragAndDrop } from './DragAndDrop';\nexport { default as Droppable } from './Droppable';\nexport { default as Draggable } from './Draggable';\n\nexport * from './DragAndDrop';\nexport * from './Droppable';\nexport * from './Draggable';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,WAAW,QAAQ,eAAe;AACtD,SAASD,OAAO,IAAIE,SAAS,QAAQ,aAAa;AAClD,SAASF,OAAO,IAAIG,SAAS,QAAQ,aAAa;AAElD,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,aAAa"}
|
|
@@ -6,29 +6,26 @@ class ListStore {
|
|
|
6
6
|
this.lists = void 0;
|
|
7
7
|
this.lists = [];
|
|
8
8
|
}
|
|
9
|
+
|
|
9
10
|
/**
|
|
10
11
|
* Adds a new list to the store.
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
13
|
add(list) {
|
|
15
14
|
this.lists.push(list);
|
|
16
15
|
}
|
|
16
|
+
|
|
17
17
|
/**
|
|
18
18
|
* Removes the list from the store.
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
20
|
remove(id) {
|
|
23
21
|
const index = this.lists.findIndex(item => item.id === id);
|
|
24
22
|
if (index === -1) return;
|
|
25
23
|
this.lists.splice(index, 1);
|
|
26
24
|
}
|
|
25
|
+
|
|
27
26
|
/**
|
|
28
27
|
* Returns the list in which the position is located.
|
|
29
28
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
29
|
findByPosition(x, y) {
|
|
33
30
|
return this.lists.find(list => {
|
|
34
31
|
const {
|
|
@@ -39,8 +36,6 @@ class ListStore {
|
|
|
39
36
|
return x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height;
|
|
40
37
|
});
|
|
41
38
|
}
|
|
42
|
-
|
|
43
39
|
}
|
|
44
|
-
|
|
45
40
|
export default ListStore;
|
|
46
41
|
//# sourceMappingURL=ListStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ListStore.js","names":["ListStore","constructor","lists","add","list","push","remove","id","index","findIndex","item","splice","findByPosition","x","y","find","current","ref","rect","getBoundingClientRect","width","height"],"sources":["../../../src/utils/ListStore.ts"],"sourcesContent":["import NodeList from './NodeList';\n\n/**\n * Stores all the lists where the draggable items is located.\n */\nclass ListStore {\n private lists: NodeList[];\n\n public constructor() {\n this.lists = [];\n }\n\n /**\n * Adds a new list to the store.\n */\n public add(list: NodeList) {\n this.lists.push(list);\n }\n\n /**\n * Removes the list from the store.\n */\n public remove(id: string) {\n const index = this.lists.findIndex((item) => item.id === id);\n if (index === -1) return;\n this.lists.splice(index, 1);\n }\n\n /**\n * Returns the list in which the position is located.\n */\n public findByPosition(x: number, y: number) {\n return this.lists.find((list) => {\n const { current } = list.ref;\n if (!current) return false;\n const rect = current.getBoundingClientRect();\n return (\n x >= rect.x &&\n x <= rect.x + rect.width &&\n y >= rect.y &&\n y <= rect.y + rect.height\n );\n });\n }\n}\n\nexport default ListStore;\n"],"mappings":"AAEA;AACA;AACA;AACA,MAAMA,
|
|
1
|
+
{"version":3,"file":"ListStore.js","names":["ListStore","constructor","lists","add","list","push","remove","id","index","findIndex","item","splice","findByPosition","x","y","find","current","ref","rect","getBoundingClientRect","width","height"],"sources":["../../../src/utils/ListStore.ts"],"sourcesContent":["import NodeList from './NodeList';\n\n/**\n * Stores all the lists where the draggable items is located.\n */\nclass ListStore {\n private lists: NodeList[];\n\n public constructor() {\n this.lists = [];\n }\n\n /**\n * Adds a new list to the store.\n */\n public add(list: NodeList) {\n this.lists.push(list);\n }\n\n /**\n * Removes the list from the store.\n */\n public remove(id: string) {\n const index = this.lists.findIndex((item) => item.id === id);\n if (index === -1) return;\n this.lists.splice(index, 1);\n }\n\n /**\n * Returns the list in which the position is located.\n */\n public findByPosition(x: number, y: number) {\n return this.lists.find((list) => {\n const { current } = list.ref;\n if (!current) return false;\n const rect = current.getBoundingClientRect();\n return (\n x >= rect.x &&\n x <= rect.x + rect.width &&\n y >= rect.y &&\n y <= rect.y + rect.height\n );\n });\n }\n}\n\nexport default ListStore;\n"],"mappings":"AAEA;AACA;AACA;AACA,MAAMA,SAAS,CAAC;EAGPC,WAAW,GAAG;IAAA,KAFbC,KAAK;IAGX,IAAI,CAACA,KAAK,GAAG,EAAE;EACjB;;EAEA;AACF;AACA;EACSC,GAAG,CAACC,IAAc,EAAE;IACzB,IAAI,CAACF,KAAK,CAACG,IAAI,CAACD,IAAI,CAAC;EACvB;;EAEA;AACF;AACA;EACSE,MAAM,CAACC,EAAU,EAAE;IACxB,MAAMC,KAAK,GAAG,IAAI,CAACN,KAAK,CAACO,SAAS,CAAEC,IAAI,IAAKA,IAAI,CAACH,EAAE,KAAKA,EAAE,CAAC;IAC5D,IAAIC,KAAK,KAAK,CAAC,CAAC,EAAE;IAClB,IAAI,CAACN,KAAK,CAACS,MAAM,CAACH,KAAK,EAAE,CAAC,CAAC;EAC7B;;EAEA;AACF;AACA;EACSI,cAAc,CAACC,CAAS,EAAEC,CAAS,EAAE;IAC1C,OAAO,IAAI,CAACZ,KAAK,CAACa,IAAI,CAAEX,IAAI,IAAK;MAC/B,MAAM;QAAEY;MAAQ,CAAC,GAAGZ,IAAI,CAACa,GAAG;MAC5B,IAAI,CAACD,OAAO,EAAE,OAAO,KAAK;MAC1B,MAAME,IAAI,GAAGF,OAAO,CAACG,qBAAqB,EAAE;MAC5C,OACEN,CAAC,IAAIK,IAAI,CAACL,CAAC,IACXA,CAAC,IAAIK,IAAI,CAACL,CAAC,GAAGK,IAAI,CAACE,KAAK,IACxBN,CAAC,IAAII,IAAI,CAACJ,CAAC,IACXA,CAAC,IAAII,IAAI,CAACJ,CAAC,GAAGI,IAAI,CAACG,MAAM;IAE7B,CAAC,CAAC;EACJ;AACF;AAEA,eAAerB,SAAS"}
|