@orca-fe/pocket
Version:
UI components by orca-team
215 lines (214 loc) • 12.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ahooks = require("ahooks");
var _hooks = require("@orca-fe/hooks");
var _tools = require("@orca-fe/tools");
var _classnames = _interopRequireDefault(require("classnames"));
var _ResizableWrapper = _interopRequireDefault(require("./ResizableWrapper.style"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["className", "children", "vertical", "horizontal", "width", "height", "style", "defaultHeight", "defaultWidth", "verticalPosition", "horizontalPosition", "triggerOnResize", "minHeight", "minWidth", "maxHeight", "maxWidth", "cover"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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; }
/**
* 可调整尺寸的容器
* @param props
* @constructor
*/
var ResizableWrapper = function ResizableWrapper(props, pRef) {
var defaultWidth = props.defaultWidth,
defaultHeight = props.defaultHeight;
var _useControllableProps = (0, _hooks.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 = (0, _ResizableWrapper.default)();
var rootRef = (0, _react.useRef)(null);
(0, _react.useImperativeHandle)(pRef, function () {
return rootRef.current;
});
var clampWidth = (0, _react.useMemo)(function () {
return function (value) {
return (0, _tools.clamp)(value, minWidth, maxWidth);
};
}, [minWidth, maxWidth]);
var clampHeight = (0, _react.useMemo)(function () {
return function (value) {
return (0, _tools.clamp)(value, minHeight, maxHeight);
};
}, [minHeight, maxHeight]);
var _useState = (0, _react.useState)({}),
_useState2 = _slicedToArray(_useState, 1),
_this = _useState2[0];
var _useState3 = (0, _react.useState)(null),
_useState4 = _slicedToArray(_useState3, 2),
dragging = _useState4[0],
setDragging = _useState4[1];
(0, _ahooks.useEventListener)('mousemove', function (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)
});
}
}
});
(0, _ahooks.useEventListener)('pointerup', function () {
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;
}
});
(0, _ahooks.useEventListener)('pointerdown', function () {
_this.size = undefined;
});
(0, _hooks.useSizeListener)(function () {
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 = (0, _ahooks.useMemoizedFn)(function () {
// 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__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
ref: rootRef,
className: "".concat(styles.root, " ").concat(className),
style: _objectSpread(_objectSpread({
minWidth: minWidth,
minHeight: minHeight,
maxWidth: maxWidth,
maxHeight: maxHeight
}, style), {}, {
width: width,
height: height,
flexShrink: dragging ? 1 : ''
})
}, otherProps), {}, {
children: [children, vertical && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: (0, _classnames.default)(styles.dragHandle, styles.handleVertical, _defineProperty(_defineProperty({}, styles.dragging, (dragging === null || dragging === void 0 ? void 0 : dragging.type) === 'vertical'), styles.handleVerticalTop, verticalPosition === 'top')),
draggable: true,
onMouseDown: function onMouseDown(e) {
e.preventDefault();
if (height != null) {
setDragging({
type: 'vertical',
initialNum: height,
initialMouse: e.clientY
});
showCover();
}
}
}), horizontal && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: (0, _classnames.default)(styles.dragHandle, styles.handleHorizontal, _defineProperty(_defineProperty({}, styles.dragging, (dragging === null || dragging === void 0 ? void 0 : dragging.type) === 'horizontal'), styles.handleHorizontalLeft, horizontalPosition === 'left')),
draggable: true,
onMouseDown: function onMouseDown(e) {
e.preventDefault();
if (width != null) {
setDragging({
type: 'horizontal',
initialNum: width,
initialMouse: e.clientX
});
showCover();
}
}
})]
}));
};
var _default = exports.default = /*#__PURE__*/_react.default.forwardRef(ResizableWrapper);