funda-ui 4.5.666 → 4.5.671
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/ColorPicker/index.js +3 -1
- package/Date/index.js +14 -1
- package/DragDropList/index.css +188 -0
- package/DragDropList/index.d.ts +44 -0
- package/DragDropList/index.js +1587 -0
- package/Input/index.d.ts +2 -0
- package/Input/index.js +14 -1
- package/LICENSE +21 -0
- package/MultipleSelect/index.css +237 -144
- package/MultipleSelect/index.d.ts +24 -10
- package/MultipleSelect/index.js +2240 -1225
- package/README.md +3 -1
- package/RangeSlider/index.js +14 -1
- package/Textarea/index.d.ts +2 -0
- package/Textarea/index.js +14 -1
- package/Tree/index.d.ts +1 -0
- package/Tree/index.js +29 -0
- package/Utils/useBoundedDrag.d.ts +125 -0
- package/Utils/useBoundedDrag.js +380 -0
- package/Utils/useDragDropPosition.d.ts +169 -0
- package/Utils/useDragDropPosition.js +456 -0
- package/Utils/useIsMobile.d.ts +2 -0
- package/Utils/useIsMobile.js +168 -0
- package/all.d.ts +1 -0
- package/all.js +1 -1
- package/lib/cjs/ColorPicker/index.js +3 -1
- package/lib/cjs/Date/index.js +14 -1
- package/lib/cjs/DragDropList/index.d.ts +44 -0
- package/lib/cjs/DragDropList/index.js +1587 -0
- package/lib/cjs/Input/index.d.ts +2 -0
- package/lib/cjs/Input/index.js +14 -1
- package/lib/cjs/MultipleSelect/index.d.ts +24 -10
- package/lib/cjs/MultipleSelect/index.js +2240 -1225
- package/lib/cjs/RangeSlider/index.js +14 -1
- package/lib/cjs/Textarea/index.d.ts +2 -0
- package/lib/cjs/Textarea/index.js +14 -1
- package/lib/cjs/Tree/index.d.ts +1 -0
- package/lib/cjs/Tree/index.js +29 -0
- package/lib/cjs/Utils/useBoundedDrag.d.ts +125 -0
- package/lib/cjs/Utils/useBoundedDrag.js +380 -0
- package/lib/cjs/Utils/useDragDropPosition.d.ts +169 -0
- package/lib/cjs/Utils/useDragDropPosition.js +456 -0
- package/lib/cjs/Utils/useIsMobile.d.ts +2 -0
- package/lib/cjs/Utils/useIsMobile.js +168 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -1
- package/lib/css/DragDropList/index.css +188 -0
- package/lib/css/MultipleSelect/index.css +237 -144
- package/lib/esm/ColorPicker/index.tsx +53 -49
- package/lib/esm/DragDropList/index.scss +245 -0
- package/lib/esm/DragDropList/index.tsx +494 -0
- package/lib/esm/Input/index.tsx +17 -3
- package/lib/esm/MultipleSelect/index.scss +288 -183
- package/lib/esm/MultipleSelect/index.tsx +305 -166
- package/lib/esm/MultipleSelect/utils/func.ts +21 -1
- package/lib/esm/Tabs/Tabs.tsx +1 -1
- package/lib/esm/Textarea/index.tsx +18 -1
- package/lib/esm/Tree/TreeList.tsx +32 -0
- package/lib/esm/Tree/index.tsx +3 -0
- package/lib/esm/Utils/hooks/useBoundedDrag.tsx +301 -0
- package/lib/esm/Utils/hooks/useDragDropPosition.tsx +420 -0
- package/lib/esm/Utils/hooks/useIsMobile.tsx +56 -0
- package/lib/esm/index.js +1 -0
- package/package.json +1 -1
- package/lib/esm/MultipleSelect/ItemList.tsx +0 -323
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
2
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
3
|
+
module.exports = factory(require("react"));
|
|
4
|
+
else if(typeof define === 'function' && define.amd)
|
|
5
|
+
define(["react"], factory);
|
|
6
|
+
else if(typeof exports === 'object')
|
|
7
|
+
exports["RPB"] = factory(require("react"));
|
|
8
|
+
else
|
|
9
|
+
root["RPB"] = factory(root["React"]);
|
|
10
|
+
})(this, (__WEBPACK_EXTERNAL_MODULE__787__) => {
|
|
11
|
+
return /******/ (() => { // webpackBootstrap
|
|
12
|
+
/******/ "use strict";
|
|
13
|
+
/******/ var __webpack_modules__ = ({
|
|
14
|
+
|
|
15
|
+
/***/ 787:
|
|
16
|
+
/***/ ((module) => {
|
|
17
|
+
|
|
18
|
+
module.exports = __WEBPACK_EXTERNAL_MODULE__787__;
|
|
19
|
+
|
|
20
|
+
/***/ })
|
|
21
|
+
|
|
22
|
+
/******/ });
|
|
23
|
+
/************************************************************************/
|
|
24
|
+
/******/ // The module cache
|
|
25
|
+
/******/ var __webpack_module_cache__ = {};
|
|
26
|
+
/******/
|
|
27
|
+
/******/ // The require function
|
|
28
|
+
/******/ function __webpack_require__(moduleId) {
|
|
29
|
+
/******/ // Check if module is in cache
|
|
30
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
31
|
+
/******/ if (cachedModule !== undefined) {
|
|
32
|
+
/******/ return cachedModule.exports;
|
|
33
|
+
/******/ }
|
|
34
|
+
/******/ // Create a new module (and put it into the cache)
|
|
35
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
36
|
+
/******/ // no module.id needed
|
|
37
|
+
/******/ // no module.loaded needed
|
|
38
|
+
/******/ exports: {}
|
|
39
|
+
/******/ };
|
|
40
|
+
/******/
|
|
41
|
+
/******/ // Execute the module function
|
|
42
|
+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
43
|
+
/******/
|
|
44
|
+
/******/ // Return the exports of the module
|
|
45
|
+
/******/ return module.exports;
|
|
46
|
+
/******/ }
|
|
47
|
+
/******/
|
|
48
|
+
/************************************************************************/
|
|
49
|
+
/******/ /* webpack/runtime/compat get default export */
|
|
50
|
+
/******/ (() => {
|
|
51
|
+
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
|
52
|
+
/******/ __webpack_require__.n = (module) => {
|
|
53
|
+
/******/ var getter = module && module.__esModule ?
|
|
54
|
+
/******/ () => (module['default']) :
|
|
55
|
+
/******/ () => (module);
|
|
56
|
+
/******/ __webpack_require__.d(getter, { a: getter });
|
|
57
|
+
/******/ return getter;
|
|
58
|
+
/******/ };
|
|
59
|
+
/******/ })();
|
|
60
|
+
/******/
|
|
61
|
+
/******/ /* webpack/runtime/define property getters */
|
|
62
|
+
/******/ (() => {
|
|
63
|
+
/******/ // define getter functions for harmony exports
|
|
64
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
65
|
+
/******/ for(var key in definition) {
|
|
66
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
67
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
68
|
+
/******/ }
|
|
69
|
+
/******/ }
|
|
70
|
+
/******/ };
|
|
71
|
+
/******/ })();
|
|
72
|
+
/******/
|
|
73
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
74
|
+
/******/ (() => {
|
|
75
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
76
|
+
/******/ })();
|
|
77
|
+
/******/
|
|
78
|
+
/******/ /* webpack/runtime/make namespace object */
|
|
79
|
+
/******/ (() => {
|
|
80
|
+
/******/ // define __esModule on exports
|
|
81
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
82
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
83
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
84
|
+
/******/ }
|
|
85
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
86
|
+
/******/ };
|
|
87
|
+
/******/ })();
|
|
88
|
+
/******/
|
|
89
|
+
/************************************************************************/
|
|
90
|
+
var __webpack_exports__ = {};
|
|
91
|
+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
|
|
92
|
+
(() => {
|
|
93
|
+
__webpack_require__.r(__webpack_exports__);
|
|
94
|
+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
95
|
+
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__),
|
|
96
|
+
/* harmony export */ "useBoundedDrag": () => (/* binding */ useBoundedDrag)
|
|
97
|
+
/* harmony export */ });
|
|
98
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(787);
|
|
99
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
100
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
101
|
+
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."); }
|
|
102
|
+
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); }
|
|
103
|
+
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; }
|
|
104
|
+
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
105
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
106
|
+
/**
|
|
107
|
+
* Bounded Drag
|
|
108
|
+
*
|
|
109
|
+
* @usage:
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
const App = () => {
|
|
113
|
+
const [items, setItems] = useState<ListItem[]>([]);
|
|
114
|
+
// ... other states and refs
|
|
115
|
+
|
|
116
|
+
const deepCloneWithReactNode = (obj: any): any => {
|
|
117
|
+
if (obj === null || typeof obj !== 'object') {
|
|
118
|
+
return obj;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Handle array
|
|
122
|
+
if (Array.isArray(obj)) {
|
|
123
|
+
return obj.map(item => deepCloneWithReactNode(item));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Handle object
|
|
127
|
+
const clonedObj: any = {};
|
|
128
|
+
for (const key in obj) {
|
|
129
|
+
if (key === 'appendControl') {
|
|
130
|
+
clonedObj[key] = obj[key];
|
|
131
|
+
} else {
|
|
132
|
+
clonedObj[key] = deepCloneWithReactNode(obj[key]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return clonedObj;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
const getItemWithChildrenIndices = (items: ListItem[], startIndex: number): number[] => {
|
|
140
|
+
const indices = [startIndex];
|
|
141
|
+
const startItem = items[startIndex];
|
|
142
|
+
const startDepth = startItem.depth || 0;
|
|
143
|
+
|
|
144
|
+
// Check if subsequent items are child items
|
|
145
|
+
for (let i = startIndex + 1; i < items.length; i++) {
|
|
146
|
+
const currentItem = items[i];
|
|
147
|
+
const currentDepth = currentItem.depth || 0;
|
|
148
|
+
if (currentDepth > startDepth) {
|
|
149
|
+
indices.push(i);
|
|
150
|
+
} else {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return indices;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
const { isDragging, dragHandlers } = useBoundedDrag({
|
|
160
|
+
dragMode,
|
|
161
|
+
boundarySelector: '.custom-draggable-list',
|
|
162
|
+
itemSelector:'.custom-draggable-list__item',
|
|
163
|
+
dragHandleSelector: '.custom-draggable-list__handle',
|
|
164
|
+
onDragStart: (index: number) => {
|
|
165
|
+
// Additional drag start logic if needed
|
|
166
|
+
},
|
|
167
|
+
onDragOver: (dragIndex: number | null, dropIndex: number | null) => {
|
|
168
|
+
// Additional drag over logic if needed
|
|
169
|
+
},
|
|
170
|
+
onDragEnd: (dragIndex: number | null, dropIndex: number | null) => {
|
|
171
|
+
if (dragIndex !== null && dropIndex !== null && dragIndex !== dropIndex) {
|
|
172
|
+
// Handle item movement
|
|
173
|
+
const newItems = deepCloneWithReactNode(items);
|
|
174
|
+
const itemsToMove = getItemWithChildrenIndices(newItems, dragIndex);
|
|
175
|
+
const itemsBeingMoved = itemsToMove.map(index => newItems[index]);
|
|
176
|
+
|
|
177
|
+
// ... rest of your existing drag end logic ...
|
|
178
|
+
|
|
179
|
+
setItems(updatedItems);
|
|
180
|
+
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Update your JSX to use the new handlers
|
|
186
|
+
return (
|
|
187
|
+
<ul className="custom-draggable-list">
|
|
188
|
+
{items.map((item: any, index: number) => (
|
|
189
|
+
<li
|
|
190
|
+
// ... other props
|
|
191
|
+
draggable={!draggable ? undefined : editingItem !== item.id && "true"}
|
|
192
|
+
onDragStart={!draggable ? undefined : (e) => dragHandlers.handleDragStart(e, index)}
|
|
193
|
+
onDragOver={!draggable ? undefined : dragHandlers.handleDragOver}
|
|
194
|
+
onDragEnd={!draggable ? undefined : dragHandlers.handleDragEnd}
|
|
195
|
+
onTouchStart={!draggable ? undefined : (e) => dragHandlers.handleDragStart(e, index)}
|
|
196
|
+
onTouchMove={!draggable ? undefined : dragHandlers.handleDragOver}
|
|
197
|
+
onTouchEnd={!draggable ? undefined : dragHandlers.handleDragEnd}
|
|
198
|
+
>
|
|
199
|
+
<li className="custom-draggable-list__item">
|
|
200
|
+
<span className="custom-draggable-list__handle">☰</span>
|
|
201
|
+
<i>content {indec}<i>
|
|
202
|
+
</li>
|
|
203
|
+
</li>
|
|
204
|
+
))}
|
|
205
|
+
</ul>
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
var useBoundedDrag = function useBoundedDrag() {
|
|
212
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
213
|
+
var _options$dragMode = options.dragMode,
|
|
214
|
+
dragMode = _options$dragMode === void 0 ? 'handle' : _options$dragMode,
|
|
215
|
+
_options$boundarySele = options.boundarySelector,
|
|
216
|
+
boundarySelector = _options$boundarySele === void 0 ? '.custom-draggable-list' : _options$boundarySele,
|
|
217
|
+
_options$itemSelector = options.itemSelector,
|
|
218
|
+
itemSelector = _options$itemSelector === void 0 ? '.custom-draggable-list__item' : _options$itemSelector,
|
|
219
|
+
_options$dragHandleSe = options.dragHandleSelector,
|
|
220
|
+
dragHandleSelector = _options$dragHandleSe === void 0 ? '.custom-draggable-list__handle' : _options$dragHandleSe,
|
|
221
|
+
onDragStart = options.onDragStart,
|
|
222
|
+
onDragOver = options.onDragOver,
|
|
223
|
+
onDragEnd = options.onDragEnd;
|
|
224
|
+
var _useState = (0,react__WEBPACK_IMPORTED_MODULE_0__.useState)(false),
|
|
225
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
226
|
+
isDragging = _useState2[0],
|
|
227
|
+
setIsDragging = _useState2[1];
|
|
228
|
+
var dragItem = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)(null);
|
|
229
|
+
var dragOverItem = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)(null);
|
|
230
|
+
var dragNode = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)(null);
|
|
231
|
+
var touchOffset = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)({
|
|
232
|
+
x: 0,
|
|
233
|
+
y: 0
|
|
234
|
+
});
|
|
235
|
+
var currentHoverItem = (0,react__WEBPACK_IMPORTED_MODULE_0__.useRef)(null);
|
|
236
|
+
var handleDragStart = function handleDragStart(e, position) {
|
|
237
|
+
var isTouch = ('touches' in e);
|
|
238
|
+
var target = e.target;
|
|
239
|
+
|
|
240
|
+
// For block mode or handle mode check
|
|
241
|
+
if (dragMode === 'handle') {
|
|
242
|
+
var handle = target.closest(dragHandleSelector);
|
|
243
|
+
if (!handle) {
|
|
244
|
+
if (!isTouch) e.preventDefault();
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Find the draggable item
|
|
250
|
+
var listItem = target.closest(itemSelector);
|
|
251
|
+
if (!listItem) return;
|
|
252
|
+
|
|
253
|
+
// Check boundary
|
|
254
|
+
var boundary = listItem.closest(boundarySelector);
|
|
255
|
+
if (!boundary) return;
|
|
256
|
+
dragItem.current = position;
|
|
257
|
+
onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(position);
|
|
258
|
+
if (isTouch) {
|
|
259
|
+
e.preventDefault(); // Prevent scrolling
|
|
260
|
+
var touch = e.touches[0];
|
|
261
|
+
var rect = listItem.getBoundingClientRect();
|
|
262
|
+
var boundaryRect = boundary.getBoundingClientRect();
|
|
263
|
+
|
|
264
|
+
// Calculate offset relative to the boundary
|
|
265
|
+
touchOffset.current = {
|
|
266
|
+
x: touch.clientX - rect.left,
|
|
267
|
+
y: touch.clientY - rect.top
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// Clone the item for dragging
|
|
271
|
+
dragNode.current = listItem.cloneNode(true);
|
|
272
|
+
dragNode.current.classList.add('dragging');
|
|
273
|
+
|
|
274
|
+
// Style the clone
|
|
275
|
+
Object.assign(dragNode.current.style, {
|
|
276
|
+
position: 'fixed',
|
|
277
|
+
width: "".concat(rect.width, "px"),
|
|
278
|
+
height: "".concat(rect.height, "px"),
|
|
279
|
+
left: "".concat(rect.left, "px"),
|
|
280
|
+
top: "".concat(rect.top, "px"),
|
|
281
|
+
zIndex: '1000',
|
|
282
|
+
pointerEvents: 'none',
|
|
283
|
+
transform: 'scale(1.05)',
|
|
284
|
+
transition: 'transform 0.1s',
|
|
285
|
+
opacity: '0.9'
|
|
286
|
+
});
|
|
287
|
+
document.body.appendChild(dragNode.current);
|
|
288
|
+
setIsDragging(true);
|
|
289
|
+
listItem.classList.add('dragging-placeholder');
|
|
290
|
+
} else {
|
|
291
|
+
// ... desktop drag logic remains the same ...
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var handleDragOver = function handleDragOver(e) {
|
|
295
|
+
e.preventDefault();
|
|
296
|
+
var isTouch = ('touches' in e);
|
|
297
|
+
if (!isTouch) {
|
|
298
|
+
e.dataTransfer.dropEffect = 'move';
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Get the current pointer/touch position
|
|
302
|
+
var point = isTouch ? e.touches[0] : {
|
|
303
|
+
clientX: e.clientX,
|
|
304
|
+
clientY: e.clientY
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// Update dragged element position for touch events
|
|
308
|
+
if (isTouch && isDragging && dragNode.current) {
|
|
309
|
+
dragNode.current.style.left = "".concat(point.clientX - touchOffset.current.x, "px");
|
|
310
|
+
dragNode.current.style.top = "".concat(point.clientY - touchOffset.current.y, "px");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Find the element below the pointer/touch
|
|
314
|
+
var elemBelow = document.elementFromPoint(point.clientX, point.clientY);
|
|
315
|
+
if (!elemBelow) return;
|
|
316
|
+
|
|
317
|
+
// Find the closest list item
|
|
318
|
+
var listItem = elemBelow.closest(itemSelector);
|
|
319
|
+
if (!listItem || listItem === currentHoverItem.current) return;
|
|
320
|
+
|
|
321
|
+
// Check boundary
|
|
322
|
+
var boundary = listItem.closest(boundarySelector);
|
|
323
|
+
if (!boundary) return;
|
|
324
|
+
|
|
325
|
+
// Update hover states
|
|
326
|
+
if (currentHoverItem.current) {
|
|
327
|
+
currentHoverItem.current.classList.remove('drag-over', 'drag-over-top', 'drag-over-bottom');
|
|
328
|
+
}
|
|
329
|
+
currentHoverItem.current = listItem;
|
|
330
|
+
listItem.classList.add('drag-over');
|
|
331
|
+
|
|
332
|
+
// Calculate position in list
|
|
333
|
+
var position = Array.from(listItem.parentNode.children).indexOf(listItem);
|
|
334
|
+
dragOverItem.current = position;
|
|
335
|
+
|
|
336
|
+
// Determine drop position (top/bottom)
|
|
337
|
+
var rect = listItem.getBoundingClientRect();
|
|
338
|
+
var middleY = rect.top + rect.height / 2;
|
|
339
|
+
if (point.clientY < middleY) {
|
|
340
|
+
listItem.classList.add('drag-over-top');
|
|
341
|
+
} else {
|
|
342
|
+
listItem.classList.add('drag-over-bottom');
|
|
343
|
+
}
|
|
344
|
+
onDragOver === null || onDragOver === void 0 ? void 0 : onDragOver(dragItem.current, dragOverItem.current);
|
|
345
|
+
};
|
|
346
|
+
var handleDragEnd = function handleDragEnd(e) {
|
|
347
|
+
var isTouch = ('touches' in e);
|
|
348
|
+
if (isTouch && !isDragging) return;
|
|
349
|
+
onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd(dragItem.current, dragOverItem.current);
|
|
350
|
+
|
|
351
|
+
// Cleanup
|
|
352
|
+
if (dragNode.current) {
|
|
353
|
+
dragNode.current.remove();
|
|
354
|
+
dragNode.current = null;
|
|
355
|
+
}
|
|
356
|
+
document.querySelectorAll(itemSelector).forEach(function (item) {
|
|
357
|
+
item.style.opacity = '1';
|
|
358
|
+
item.classList.remove('dragging', 'dragging-placeholder', 'drag-over', 'drag-over-top', 'drag-over-bottom');
|
|
359
|
+
});
|
|
360
|
+
setIsDragging(false);
|
|
361
|
+
currentHoverItem.current = null;
|
|
362
|
+
dragItem.current = null;
|
|
363
|
+
dragOverItem.current = null;
|
|
364
|
+
};
|
|
365
|
+
return {
|
|
366
|
+
isDragging: isDragging,
|
|
367
|
+
dragHandlers: {
|
|
368
|
+
handleDragStart: handleDragStart,
|
|
369
|
+
handleDragOver: handleDragOver,
|
|
370
|
+
handleDragEnd: handleDragEnd
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (useBoundedDrag);
|
|
375
|
+
})();
|
|
376
|
+
|
|
377
|
+
/******/ return __webpack_exports__;
|
|
378
|
+
/******/ })()
|
|
379
|
+
;
|
|
380
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag Drop Object
|
|
3
|
+
*
|
|
4
|
+
* @usage:
|
|
5
|
+
|
|
6
|
+
import { useState, useCallback } from 'react';
|
|
7
|
+
import { useDragDropPosition } from '@/utils/hooks/useDragDropPosition';
|
|
8
|
+
|
|
9
|
+
const App = () => {
|
|
10
|
+
|
|
11
|
+
const [show, setShow] = useState<boolean>(false);
|
|
12
|
+
|
|
13
|
+
// drag & drop
|
|
14
|
+
//---------------、
|
|
15
|
+
const moveDelay = 150;
|
|
16
|
+
const pin = false;
|
|
17
|
+
const dimension = 32; // onject with dimension
|
|
18
|
+
const [objPosition, setObjPosition] = useState<{
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
}>({ x: 0, y: 0 });
|
|
22
|
+
const [isDragged, setIsDragged] = useState<boolean>(false);
|
|
23
|
+
const [isPressed, setIsPressed] = useState<boolean>(false);
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const { setup, ref } = useDragDropPosition({
|
|
27
|
+
// usePercentage: true, // Enable percentage values
|
|
28
|
+
dimension,
|
|
29
|
+
onDragEnd: ({position, hasContainer}) => {
|
|
30
|
+
const { left, top } = position;
|
|
31
|
+
setObjPosition({
|
|
32
|
+
x: left || 0,
|
|
33
|
+
y: (top || 0),
|
|
34
|
+
});
|
|
35
|
+
setIsPressed(false);
|
|
36
|
+
setIsDragged(false);
|
|
37
|
+
|
|
38
|
+
// click event here (restore)
|
|
39
|
+
setShow(false);
|
|
40
|
+
},
|
|
41
|
+
onDragStart: ({position, hasContainer}) => {
|
|
42
|
+
const { left, top } = position;
|
|
43
|
+
setObjPosition({
|
|
44
|
+
x: left || 0,
|
|
45
|
+
y: (top || 0),
|
|
46
|
+
});
|
|
47
|
+
setIsDragged(true);
|
|
48
|
+
|
|
49
|
+
// click event here (restore)
|
|
50
|
+
setShow(false);
|
|
51
|
+
},
|
|
52
|
+
onInit: ({position, hasContainer}) => {
|
|
53
|
+
const { left, top } = position;
|
|
54
|
+
setObjPosition({
|
|
55
|
+
x: left || 0,
|
|
56
|
+
y: (top || 0),
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
onPointerDown: () => {
|
|
60
|
+
setIsPressed(true);
|
|
61
|
+
},
|
|
62
|
+
onPointerUp: useCallback(() => {
|
|
63
|
+
setIsPressed(false);
|
|
64
|
+
|
|
65
|
+
// click event here
|
|
66
|
+
setShow((prev) => !prev);
|
|
67
|
+
|
|
68
|
+
}, []),
|
|
69
|
+
onMove: ({position, hasContainer}) => {
|
|
70
|
+
const { left, top } = position;
|
|
71
|
+
setObjPosition({
|
|
72
|
+
x: left || 0,
|
|
73
|
+
y: (top || 0),
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
pin,
|
|
77
|
+
moveDelay
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
<>
|
|
86
|
+
|
|
87
|
+
<div
|
|
88
|
+
ref={setup}
|
|
89
|
+
className="float-btn"
|
|
90
|
+
style={{position: 'fixed', left: '50%', top: '50%', zIndex: 1000, background: 'red'}}
|
|
91
|
+
|
|
92
|
+
>Move Here<small>{JSON.stringify(objPosition)}</small><br /><strong>{show ? 'Clicked' : 'None'}</strong></div>
|
|
93
|
+
|
|
94
|
+
</>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
const App2 = () => {
|
|
102
|
+
|
|
103
|
+
const dragdropContainerRef = useRef<HTMLDivElement>(null);
|
|
104
|
+
....
|
|
105
|
+
|
|
106
|
+
const { setup, ref } = useDragDropPosition({
|
|
107
|
+
container: dragdropContainerRef.current, // If there is a container with a drag range
|
|
108
|
+
...
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
|
|
114
|
+
<>
|
|
115
|
+
|
|
116
|
+
<div
|
|
117
|
+
ref={dragdropContainerRef}
|
|
118
|
+
style={{
|
|
119
|
+
width: '300px',
|
|
120
|
+
height: '300px',
|
|
121
|
+
border: '1px solid #ddd',
|
|
122
|
+
background: '#efefef',
|
|
123
|
+
position: 'relative'
|
|
124
|
+
}}
|
|
125
|
+
>
|
|
126
|
+
<div
|
|
127
|
+
ref={setup}
|
|
128
|
+
className="float-btn"
|
|
129
|
+
style={{ position: 'absolute', left: '50%', top: '50%', zIndex: 1000, background: 'red' }}
|
|
130
|
+
|
|
131
|
+
>Move Here<small>{JSON.stringify(objPosition)}</small><br /><strong>{show ? 'Clicked' : 'None'}</strong></div>
|
|
132
|
+
|
|
133
|
+
</div>
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
</>
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
*/
|
|
142
|
+
/// <reference types="react" />
|
|
143
|
+
interface Position {
|
|
144
|
+
left: number;
|
|
145
|
+
top: number;
|
|
146
|
+
}
|
|
147
|
+
interface PositionResult {
|
|
148
|
+
position: Position;
|
|
149
|
+
hasContainer: boolean;
|
|
150
|
+
}
|
|
151
|
+
interface DragDropSettings {
|
|
152
|
+
container?: HTMLElement | null;
|
|
153
|
+
onPointerDown?: () => void;
|
|
154
|
+
onPointerUp?: () => void;
|
|
155
|
+
onDragStart?: (position: PositionResult) => void;
|
|
156
|
+
onDragEnd?: (position: PositionResult) => void;
|
|
157
|
+
onMove?: (position: PositionResult) => void;
|
|
158
|
+
onInit?: (position: PositionResult) => void;
|
|
159
|
+
dimension?: number;
|
|
160
|
+
pin?: boolean;
|
|
161
|
+
moveDelay?: number;
|
|
162
|
+
usePercentage?: boolean;
|
|
163
|
+
}
|
|
164
|
+
declare const useDragDropPosition: (settings: DragDropSettings) => {
|
|
165
|
+
ref: import("react").MutableRefObject<HTMLElement | null>;
|
|
166
|
+
setup: (node: HTMLElement | null) => void;
|
|
167
|
+
};
|
|
168
|
+
export { useDragDropPosition };
|
|
169
|
+
export type { DragDropSettings, Position, PositionResult };
|