UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

261 lines (250 loc) 10.6 kB
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 _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; } /* eslint-disable react/no-unused-prop-types */ import React from 'react'; import { useControllableProps, useThis } from '@orca-fe/hooks'; import { useEventListener, useSetState } from 'ahooks'; import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons'; import cn from 'classnames'; import Ruler from "./Ruler"; import useStyles from "./RulerGroup.style"; import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var RulerGroup = props => { var styles = useStyles(); var _useControllableProps = useControllableProps(props, { /* 横轴辅助线 */ xLines: [], /* 纵轴辅助线 */ yLines: [], /* 是否展示辅助线 */ lineVisible: true }), _useControllableProps2 = _slicedToArray(_useControllableProps, 2), _useControllableProps3 = _useControllableProps2[0], _useControllableProps4 = _useControllableProps3.size, size = _useControllableProps4 === void 0 ? 20 : _useControllableProps4, _useControllableProps5 = _useControllableProps3.offsetX, offsetX = _useControllableProps5 === void 0 ? 0 : _useControllableProps5, _useControllableProps6 = _useControllableProps3.offsetY, offsetY = _useControllableProps6 === void 0 ? 0 : _useControllableProps6, _useControllableProps7 = _useControllableProps3.zoom, zoom = _useControllableProps7 === void 0 ? 0 : _useControllableProps7, lineVisible = _useControllableProps3.lineVisible, xLines = _useControllableProps3.xLines, yLines = _useControllableProps3.yLines, changeProps = _useControllableProps2[1]; var _useSetState = useSetState({ /* 横轴辅助线(预览) */ xPreview: null, /* 纵轴辅助线(预览) */ yPreview: null }), _useSetState2 = _slicedToArray(_useSetState, 2), _useSetState2$ = _useSetState2[0], xPreview = _useSetState2$.xPreview, yPreview = _useSetState2$.yPreview, setState = _useSetState2[1]; var _this = useThis({ lineDragging: false }); /* 横向标尺点击 */ var handleHRulerClick = value => { if (lineVisible) { changeProps({ xLines: [...xLines, value] }); } }; /* 纵向标尺点击 */ var handleVRulerClick = value => { if (lineVisible) { changeProps({ yLines: [...yLines, value] }); } }; /* 横向标尺鼠标移动 */ var handleRulerHMouseMove = value => { setState({ xPreview: value }); }; /* 纵向标尺鼠标移动 */ var handleRulerVMouseMove = value => { setState({ yPreview: value }); }; /* 删除辅助线 */ var deleteLine = (e, config) => { var index = config.index, isHorizon = config.isHorizon; var newLines = (isHorizon ? xLines : yLines).filter((_, i) => i !== index); changeProps(isHorizon ? { xLines: newLines } : { yLines: newLines }); }; /* 开始拖拽辅助线 */ var lineDragStart = (e, config) => { _this.lineDragging = _objectSpread({ x: e.pageX, y: e.pageY }, config); }; /* 全局鼠标移动 - 正在拖拽辅助线 */ useEventListener('mousemove', e => { if (_this.lineDragging) { var pageX = e.pageX, pageY = e.pageY; var _this$lineDragging = _this.lineDragging, x = _this$lineDragging.x, y = _this$lineDragging.y, isHorizon = _this$lineDragging.isHorizon, index = _this$lineDragging.index, value = _this$lineDragging.value; var _lines = (isHorizon ? xLines : yLines).slice(); var newValue = value + (isHorizon ? pageX - x : pageY - y) / Math.pow(2, zoom); _lines[index] = Math.round(newValue); changeProps(isHorizon ? { xLines: _lines } : { yLines: _lines }); } }); /* 完成拖拽辅助线 */ useEventListener('mouseup', () => { _this.lineDragging = false; }); /* 切换辅助线隐藏/显示 */ var handleLineVisibleClick = () => { changeProps({ lineVisible: !lineVisible }); }; /* 预览辅助线 */ var renderLinePreview = () => { if (xPreview != null) { return /*#__PURE__*/_jsx("div", { className: cn(styles.line, styles.preview), style: { height: '100%', left: offsetX + xPreview * Math.pow(2, zoom) }, children: /*#__PURE__*/_jsx("div", { className: styles.lineTip, children: xPreview }) }); } else if (yPreview != null) { return /*#__PURE__*/_jsx("div", { className: cn(styles.line, styles.preview), style: { width: '100%', top: offsetY + yPreview * Math.pow(2, zoom) }, children: /*#__PURE__*/_jsx("div", { className: styles.lineTip, children: yPreview }) }); } return /*#__PURE__*/_jsx("div", { className: cn(styles.line, styles.preview), style: { width: '100%', top: 0, visibility: 'hidden', pointerEvents: 'none' }, children: /*#__PURE__*/_jsx("div", { className: styles.lineTip, children: yPreview }) }); }; /* 渲染辅助线 */ var renderLines = () => { var renderLine = (isHorizon, line, index) => { /* 辅助线位置需要根据zoom缩放 */ var zoomOffset = line * Math.pow(2, zoom); if ((isHorizon ? offsetX + zoomOffset : offsetY + zoomOffset) > size) { return /*#__PURE__*/_jsx("div", { onMouseDown: e => { lineDragStart(e, { isHorizon, index, value: line }); }, onDoubleClick: e => { deleteLine(e, { isHorizon, index, value: line }); }, className: styles.line, style: _objectSpread({}, isHorizon ? { height: '100%', left: offsetX + zoomOffset, cursor: 'col-resize' } : { width: '100%', top: offsetY + zoomOffset, cursor: 'row-resize' }), children: /*#__PURE__*/_jsx("div", { className: styles.lineTip, style: _objectSpread({}, _this.lineDragging && _this.lineDragging.index === index && _this.lineDragging.isHorizon === isHorizon ? { color: '#999999', backgroundColor: 'rgba(0, 145, 255, 0.8)' } : {}), children: Math.round(line) }) }, `${isHorizon ? 'H' : 'V'}_Lines_${index}`); } return null; }; return xLines.map(renderLine.bind(null, true)).concat(yLines.map(renderLine.bind(null, false))); }; return /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(Ruler, { size: size, zoom: zoom, offsetX: offsetX, onRulerClick: handleHRulerClick, onRulerMouseMove: handleRulerHMouseMove }), /*#__PURE__*/_jsx(Ruler, { size: size, zoom: zoom, offsetY: offsetY, orientation: "vertical", onRulerClick: handleVRulerClick, onRulerMouseMove: handleRulerVMouseMove }), lineVisible && renderLines(), lineVisible && renderLinePreview(), /*#__PURE__*/_jsx("div", { className: styles.corner, style: { width: size, height: size }, onClick: handleLineVisibleClick, children: lineVisible ? /*#__PURE__*/_jsx(EyeOutlined, {}) : /*#__PURE__*/_jsx(EyeInvisibleOutlined, {}) })] }); }; RulerGroup.Ruler = Ruler; export default RulerGroup;