@orca-fe/painter
Version:
Konva Painter
251 lines • 12.8 kB
JavaScript
var _excluded = ["className", "shape", "onShapeClick", "onShapeMouseEnter", "onShapeMouseLeave", "onShapeChange", "checked", "onCheck", "svgRoot", "renderTransformingRect", "onChangeStart"];
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; }
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 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); }
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import { TransformerBox, TransformerLine } from '@orca-fe/transformer';
import cn from 'classnames';
import useStyles from "./ShapeRenderer.style";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var ef = () => undefined;
function getNewShape(shape, originBounds, newBounds) {
// 新旧宽度和高度的比例
var ratio = {
width: newBounds.width / originBounds.width,
height: newBounds.height / originBounds.height
};
var newShape = _objectSpread({}, shape);
newShape.x = newBounds.left;
newShape.y = newBounds.top;
newShape.width = newBounds.width;
newShape.height = newBounds.height;
newShape.rotate = newBounds.rotate || 0;
// 对特殊图形进行特殊处理
switch (newShape.type) {
case 'line-path':
newShape.points = newShape.points.map(([x, y]) => [(x - originBounds.left) * ratio.width + newBounds.left, (y - originBounds.top) * ratio.height + newBounds.top]);
break;
default:
}
return newShape;
}
var ShapeRenderer = props => {
var _props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
_shape = props.shape,
_props$onShapeClick = props.onShapeClick,
onShapeClick = _props$onShapeClick === void 0 ? ef : _props$onShapeClick,
_props$onShapeMouseEn = props.onShapeMouseEnter,
onShapeMouseEnter = _props$onShapeMouseEn === void 0 ? ef : _props$onShapeMouseEn,
_props$onShapeMouseLe = props.onShapeMouseLeave,
onShapeMouseLeave = _props$onShapeMouseLe === void 0 ? ef : _props$onShapeMouseLe,
_props$onShapeChange = props.onShapeChange,
onShapeChange = _props$onShapeChange === void 0 ? ef : _props$onShapeChange,
checked = props.checked,
_props$onCheck = props.onCheck,
onCheck = _props$onCheck === void 0 ? ef : _props$onCheck,
svgRoot = props.svgRoot,
_props$renderTransfor = props.renderTransformingRect,
renderTransformingRect = _props$renderTransfor === void 0 ? () => null : _props$renderTransfor,
_props$onChangeStart = props.onChangeStart,
onChangeStart = _props$onChangeStart === void 0 ? ef : _props$onChangeStart,
otherProps = _objectWithoutProperties(props, _excluded);
var styles = useStyles();
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
tmpShape = _useState2[0],
setTmpShape = _useState2[1];
var shape = tmpShape || _shape;
return /*#__PURE__*/_jsx("div", _objectSpread(_objectSpread({
className: cn(styles.root, className)
}, otherProps), {}, {
children: svgRoot && (() => {
var element = null;
var strokeStyle = {
stroke: shape.stroke,
strokeWidth: shape.strokeWidth
};
switch (shape.type) {
case 'ellipse':
element = /*#__PURE__*/_jsx("ellipse", _objectSpread({
cx: shape.x + 0.5 * shape.width,
cy: shape.y + 0.5 * shape.height,
rx: 0.5 * shape.width,
ry: 0.5 * shape.height,
style: {
transformOrigin: `${shape.x}px ${shape.y}px`,
transform: `rotate(${shape.rotate}deg)`
}
}, strokeStyle));
break;
case 'line':
element = /*#__PURE__*/_jsx("line", {
x1: shape.point1[0],
y1: shape.point1[1],
x2: shape.point2[0],
y2: shape.point2[1]
});
break;
case 'rectangle':
element = /*#__PURE__*/_jsx("rect", _objectSpread({
x: shape.x,
y: shape.y,
width: shape.width,
height: shape.height,
rotate: `${shape.rotate}deg`,
style: {
transformOrigin: `${shape.x}px ${shape.y}px`,
transform: `rotate(${shape.rotate}deg)`
}
}, strokeStyle));
break;
case 'mark':
element = /*#__PURE__*/_jsxs("g", {
children: [/*#__PURE__*/_jsx("rect", _objectSpread({
x: shape.x,
y: shape.y,
width: shape.width,
height: shape.height,
rotate: `${shape.rotate}deg`,
style: {
fill: 'rgba(255, 77, 79, 0.05)',
transformOrigin: `${shape.x}px ${shape.y}px`,
transform: `rotate(${shape.rotate}deg)`
}
}, strokeStyle)), /*#__PURE__*/_jsx("circle", {
cx: shape.x + shape.width + 5 // 圆心在矩形右边缘外 5px
,
cy: shape.y + shape.height + 5 // 圆心在矩形下边缘外 5px
,
r: 12,
fill: "#F33B40" // 圆形背景颜色
,
stroke: "#F33B40" // 圆形边框颜色
,
strokeWidth: "1"
}), /*#__PURE__*/_jsx("text", {
x: shape.x + shape.width + 5 // 文字开始于圆心x坐标
,
y: shape.y + shape.height + 5 // 文字开始于圆心y坐标
,
textAnchor: "middle" // 文字水平居中
,
dominantBaseline: "central" // 文字垂直居中
,
fill: "#fff" // 文字填充为无色
,
stroke: "#fff" // 边框颜色
,
strokeWidth: "1" // 边框宽度
,
fontSize: 14 // 文字大小
,
children: shape.markNum
})]
});
break;
case 'line-path':
element = /*#__PURE__*/_jsx("path", _objectSpread({
d: shape.points.map(([x, y], index) => `${(index === 0 ? 'M' : 'L') + x} ${y}`).join(''),
rotate: `${shape.rotate}deg`,
style: {
transformOrigin: `${shape.x}px ${shape.y}px`,
transform: `rotate(${shape.rotate}deg)`
}
}, strokeStyle));
break;
default:
}
if (element) {
var svgElement = /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/React.cloneElement(element, {
className: cn({
[styles.disabled]: shape.disabled
})
}), /*#__PURE__*/React.cloneElement(element, {
className: cn(styles.svgHit, {
[styles.disabled]: shape.disabled
}),
onClick: () => {
onShapeClick();
onCheck();
},
onMouseEnter: () => {
onShapeMouseEnter();
},
onMouseLeave: () => {
onShapeMouseLeave();
}
})]
});
if (shape.type === 'line') {
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/ReactDOM.createPortal(svgElement, svgRoot), /*#__PURE__*/_jsx(TransformerLine, {
checked: checked,
disabled: shape.disabled,
points: [{
x: shape.point1[0],
y: shape.point1[1]
}, {
x: shape.point2[0],
y: shape.point2[1]
}],
onChangeStart: onChangeStart,
onPointsChange: newPoints => {
setTmpShape(_objectSpread(_objectSpread({}, shape), {}, {
point1: [newPoints[0].x, newPoints[0].y],
point2: [newPoints[1].x, newPoints[1].y]
}));
},
onChangeEnd: newPoints => {
onShapeChange(_objectSpread(_objectSpread({}, shape), {}, {
point1: [newPoints[0].x, newPoints[0].y],
point2: [newPoints[1].x, newPoints[1].y]
}));
setTmpShape(null);
},
children: checked && renderTransformingRect()
})]
});
}
var bounds = {
left: shape.x,
top: shape.y,
height: shape.height,
width: shape.width,
rotate: shape.rotate
};
return /*#__PURE__*/_jsxs(TransformerBox, {
controlledMode: true,
checked: checked,
disabled: shape.disabled,
bounds: bounds,
rotateEnabled: true,
onBoundsChange: newTmpBounds => {
setTmpShape(getNewShape(shape, bounds, newTmpBounds));
},
onChangeEnd: newBounds => {
onShapeChange(getNewShape(shape, bounds, newBounds));
setTmpShape(null);
},
children: [checked && renderTransformingRect(), /*#__PURE__*/ReactDOM.createPortal(svgElement, svgRoot)]
});
}
return null;
})()
}));
};
export default ShapeRenderer;