function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
 * 简易拖拽组件
 * @author 施阳 2024-8-2
 * @description 使用拖拽组件包裹的子组件会脱离文档流，变成fixed定位的元素
 */
import { useLayoutEffect, useRef, cloneElement } from 'react';
function Dragger(_ref) {
  var _children$props2;
  var children = _ref.children,
    disabled = _ref.disabled,
    _ref$offset = _ref.offset,
    offset = _ref$offset === void 0 ? 5 : _ref$offset,
    _ref$limit = _ref.limit,
    limit = _ref$limit === void 0 ? {
      top: 0
    } : _ref$limit;
  // 被拖拽的元素
  var floatBtnRef = useRef(null);

  // 鼠标是否按住
  var isMouseDownRef = useRef(false);

  // 记录鼠标按下时在屏幕可视区域的位置
  var mouseStartPosition = useRef({
    x: 0,
    y: 0
  });

  // 记录鼠标按下时在被拖拽元素中的位置
  var mouseMovePosition = useRef({
    x: 0,
    y: 0
  });
  useLayoutEffect(function () {
    var moveCallback = function moveCallback(event) {
      var clientX = event.clientX,
        clientY = event.clientY;
      if (!isMouseDownRef.current || !floatBtnRef.current) return;
      if (limit) {
        var element = floatBtnRef.current;
        var elementWidth = element.offsetWidth;
        var elementHeight = element.offsetHeight;
        var viewportWidth = window.innerWidth;
        var viewportHeight = window.innerHeight;

        // 计算新位置
        var newLeft = clientX - mouseMovePosition.current.x;
        var newTop = clientY - mouseMovePosition.current.y;

        // 应用边界限制
        if (limit.left !== undefined) {
          newLeft = Math.max(limit.left, newLeft);
        }
        if (limit.right !== undefined) {
          newLeft = Math.min(viewportWidth - elementWidth - limit.right, newLeft);
        }
        if (limit.top !== undefined) {
          newTop = Math.max(limit.top, newTop);
        }
        if (limit.bottom !== undefined) {
          newTop = Math.min(viewportHeight - elementHeight - limit.bottom, newTop);
        }

        // 更新元素位置
        element.style.left = "".concat(newLeft, "px");
        element.style.top = "".concat(newTop, "px");
        element.style.bottom = 'unset';
        element.style.right = 'unset';
      } else {
        floatBtnRef.current.style.left = "".concat(clientX - mouseMovePosition.current.x, "px");
        floatBtnRef.current.style.top = "".concat(clientY - mouseMovePosition.current.y, "px");
        floatBtnRef.current.style.bottom = 'unset';
        floatBtnRef.current.style.right = 'unset';
      }
    };
    document.addEventListener('mousemove', moveCallback);
    return function () {
      document.removeEventListener('mousemove', moveCallback);
    };
  }, [limit]);
  var handleMouseStart = function handleMouseStart(event) {
    if (disabled) return;
    isMouseDownRef.current = true;
    var clientX = event.clientX,
      clientY = event.clientY;
    var _ref2 = floatBtnRef.current,
      offsetLeft = _ref2.offsetLeft,
      offsetTop = _ref2.offsetTop;
    mouseStartPosition.current = {
      x: clientX,
      y: clientY
    };
    mouseMovePosition.current = {
      x: clientX - offsetLeft,
      y: clientY - offsetTop
    };
  };
  var handleMouseStop = function handleMouseStop() {
    isMouseDownRef.current = false;
  };
  var handleDragStart = function handleDragStart(event) {
    event.preventDefault(); // 阻止默认行为
    event.stopPropagation(); // 阻止事件冒泡
  };

  var onDraggerClick = function onDraggerClick(event) {
    // 计算鼠标移动的距离
    var moveDistance = Math.sqrt(Math.pow(event.clientX - mouseStartPosition.current.x, 2) + Math.pow(event.clientY - mouseStartPosition.current.y, 2));

    // 如果移动距离小于某个阈值（例如5像素），则认为是点击而非拖动
    if (moveDistance < offset) {
      var _children$props$onCli, _children$props;
      (_children$props$onCli = (_children$props = children.props).onClick) === null || _children$props$onCli === void 0 ? void 0 : _children$props$onCli.call(_children$props, event);
    }
  };
  return /*#__PURE__*/cloneElement(children, _objectSpread(_objectSpread({}, children.props), {}, {
    ref: floatBtnRef,
    style: _objectSpread(_objectSpread({}, (_children$props2 = children.props) === null || _children$props2 === void 0 ? void 0 : _children$props2.style), {}, {
      position: 'fixed'
    }),
    onClick: onDraggerClick,
    onMouseDown: handleMouseStart,
    onMouseUp: handleMouseStop,
    onDragStart: handleDragStart
  }));
}
export default Dragger;