UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

202 lines 10.8 kB
var _excluded = ["className", "children", "vertical", "horizontal", "width", "height", "style", "defaultHeight", "defaultWidth", "verticalPosition", "horizontalPosition", "triggerOnResize", "minHeight", "minWidth", "maxHeight", "maxWidth", "cover"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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 React, { useImperativeHandle, useMemo, useRef, useState } from 'react'; import { useEventListener, useMemoizedFn } from 'ahooks'; import { useControllableProps, useSizeListener } from '@orca-fe/hooks'; import { clamp } from '@orca-fe/tools'; import cn from 'classnames'; import useStyles from "./ResizableWrapper.style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; /** * 可调整尺寸的容器 * @param props * @constructor */ var ResizableWrapper = (props, pRef) => { var defaultWidth = props.defaultWidth, defaultHeight = props.defaultHeight; var _useControllableProps = useControllableProps(props, { width: defaultWidth, height: defaultHeight }), _useControllableProps2 = _slicedToArray(_useControllableProps, 2), _useControllableProps3 = _useControllableProps2[0], _useControllableProps4 = _useControllableProps3.className, className = _useControllableProps4 === void 0 ? '' : _useControllableProps4, children = _useControllableProps3.children, vertical = _useControllableProps3.vertical, horizontal = _useControllableProps3.horizontal, width = _useControllableProps3.width, height = _useControllableProps3.height, style = _useControllableProps3.style, _nouse1 = _useControllableProps3.defaultHeight, _nouse2 = _useControllableProps3.defaultWidth, _useControllableProps5 = _useControllableProps3.verticalPosition, verticalPosition = _useControllableProps5 === void 0 ? 'right' : _useControllableProps5, _useControllableProps6 = _useControllableProps3.horizontalPosition, horizontalPosition = _useControllableProps6 === void 0 ? 'bottom' : _useControllableProps6, _useControllableProps7 = _useControllableProps3.triggerOnResize, triggerOnResize = _useControllableProps7 === void 0 ? true : _useControllableProps7, minHeight = _useControllableProps3.minHeight, minWidth = _useControllableProps3.minWidth, maxHeight = _useControllableProps3.maxHeight, maxWidth = _useControllableProps3.maxWidth, cover = _useControllableProps3.cover, otherProps = _objectWithoutProperties(_useControllableProps3, _excluded), changeProps = _useControllableProps2[1]; var styles = useStyles(); var rootRef = useRef(null); useImperativeHandle(pRef, () => rootRef.current); var clampWidth = useMemo(() => value => clamp(value, minWidth, maxWidth), [minWidth, maxWidth]); var clampHeight = useMemo(() => value => clamp(value, minHeight, maxHeight), [minHeight, maxHeight]); var _useState = useState({}), _useState2 = _slicedToArray(_useState, 1), _this = _useState2[0]; var _useState3 = useState(null), _useState4 = _slicedToArray(_useState3, 2), dragging = _useState4[0], setDragging = _useState4[1]; useEventListener('mousemove', e => { if (dragging) { var initialNum = dragging.initialNum, initialMouse = dragging.initialMouse, type = dragging.type; var curMouse = type === 'vertical' ? e.clientY : e.clientX; var sign = 1; if (type === 'vertical' && verticalPosition === 'top' || type === 'horizontal' && horizontalPosition === 'left') { sign = -1; } var curNum = Math.max(1, initialNum + sign * (curMouse - initialMouse)); if (type === 'vertical') { if (height !== curNum) changeProps({ height: clampHeight(curNum) }); } else if (width !== curNum) { changeProps({ width: clampWidth(curNum) }); } } }); useEventListener('pointerup', () => { if (dragging) { setDragging(null); } // remove cover if (_this.cover) { document.body.removeChild(_this.cover); _this.cover = undefined; } if (_this.size) { if (triggerOnResize) { if (vertical && _this.size.height !== height) { changeProps({ height: clampHeight(_this.size.height) }); } if (horizontal && _this.size.width !== width) { changeProps({ width: clampWidth(_this.size.width) }); } } _this.size = undefined; } }); useEventListener('pointerdown', () => { _this.size = undefined; }); useSizeListener(() => { if (rootRef.current) { var _rootRef$current$getB = rootRef.current.getBoundingClientRect(), _width = _rootRef$current$getB.width, _height = _rootRef$current$getB.height; _this.size = { width: _width, height: _height }; } }, rootRef); var showCover = useMemoizedFn(() => { // create cover if (!_this.cover && cover) { _this.cover = document.createElement('div'); _this.cover.style.position = 'fixed'; _this.cover.style.top = '0'; _this.cover.style.left = '0'; _this.cover.style.right = '0'; _this.cover.style.bottom = '0'; _this.cover.style.zIndex = typeof cover === 'object' && cover.zIndex != null ? cover.zIndex.toString() : '99999'; if (horizontal) { _this.cover.style.cursor = 'ew-resize'; } else if (vertical) { _this.cover.style.cursor = 'ns-resize'; } document.body.appendChild(_this.cover); } }); return /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({ ref: rootRef, className: `${styles.root} ${className}`, style: _objectSpread(_objectSpread({ minWidth, minHeight, maxWidth, maxHeight }, style), {}, { width, height, flexShrink: dragging ? 1 : '' }) }, otherProps), {}, { children: [children, vertical && /*#__PURE__*/_jsx("div", { className: cn(styles.dragHandle, styles.handleVertical, { [styles.dragging]: (dragging === null || dragging === void 0 ? void 0 : dragging.type) === 'vertical', [styles.handleVerticalTop]: verticalPosition === 'top' }), draggable: true, onMouseDown: e => { e.preventDefault(); if (height != null) { setDragging({ type: 'vertical', initialNum: height, initialMouse: e.clientY }); showCover(); } } }), horizontal && /*#__PURE__*/_jsx("div", { className: cn(styles.dragHandle, styles.handleHorizontal, { [styles.dragging]: (dragging === null || dragging === void 0 ? void 0 : dragging.type) === 'horizontal', [styles.handleHorizontalLeft]: horizontalPosition === 'left' }), draggable: true, onMouseDown: e => { e.preventDefault(); if (width != null) { setDragging({ type: 'horizontal', initialNum: width, initialMouse: e.clientX }); showCover(); } } })] })); }; export default /*#__PURE__*/React.forwardRef(ResizableWrapper);