UNPKG

@orca-fe/antd-plus

Version:
81 lines (80 loc) 4.14 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 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."); } 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); } 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; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { useRef, useState } from 'react'; import { useEventListener } from 'ahooks'; import { useEffectWithTarget } from '@orca-fe/hooks'; var ef = () => {}; export default function useDraggable(options = {}) { var _options$defaultY = options.defaultY, defaultY = _options$defaultY === void 0 ? 0 : _options$defaultY, _options$defaultX = options.defaultX, defaultX = _options$defaultX === void 0 ? 0 : _options$defaultX, customHandler = options.customHandler, refFromOptions = options.ref, _options$onDragEnd = options.onDragEnd, onDragEnd = _options$onDragEnd === void 0 ? ef : _options$onDragEnd, _options$onDragStart = options.onDragStart, onDragStart = _options$onDragStart === void 0 ? ef : _options$onDragStart; var _ref = useRef(null); var ref = refFromOptions !== null && refFromOptions !== void 0 ? refFromOptions : _ref; var dragHandlerRef = useRef(null); if (!customHandler) { dragHandlerRef = ref; } var _useState = useState(() => ({ mouse: null, startMouse: null, position: [defaultX, defaultY], startPosition: null })), _useState2 = _slicedToArray(_useState, 1), _this = _useState2[0]; // 拖拽开始 useEventListener('dragstart', e => { e.preventDefault(); _this.startMouse = _this.mouse; _this.startPosition = _this.position; onDragStart(); }, { target: dragHandlerRef }); useEventListener('mousemove', e => { _this.mouse = [e.clientX, e.clientY]; if (_this.startMouse && ref.current && _this.startPosition) { var position = [_this.startPosition[0] + _this.mouse[0] - _this.startMouse[0], _this.startPosition[1] + _this.mouse[1] - _this.startMouse[1]]; _this.position = position; ref.current.style.transform = `translate(${position[0]}px, ${position[1]}px)`; } }); useEventListener('mouseup', () => { if (_this.startMouse) { _this.startPosition = null; _this.startMouse = null; if (_this.position && ref.current && _this.position[1] < 0) { _this.position[1] = 0; ref.current.style.transform = `translate(${_this.position[0]}px, ${_this.position[1]}px)`; } onDragEnd(); } }); useEffectWithTarget(() => { if (ref.current && _this.position) { ref.current.style.transform = `translate(${_this.position[0]}px, ${_this.position[1]}px)`; } }, [], ref); var props = { rootProps: { ref, draggable: !customHandler }, handleProps: { ref: dragHandlerRef, draggable: !!customHandler } }; return [props]; }