@orca-fe/painter
Version:
Konva Painter
269 lines (268 loc) • 15.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _transformer = require("@orca-fe/transformer");
var _classnames = _interopRequireDefault(require("classnames"));
var _ShapeRenderer = _interopRequireDefault(require("./ShapeRenderer.style"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["className", "shape", "onShapeClick", "onShapeMouseEnter", "onShapeMouseLeave", "onShapeChange", "checked", "onCheck", "svgRoot", "renderTransformingRect", "onChangeStart"];
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 _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; }
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); }
var ef = function ef() {
return 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(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
x = _ref2[0],
y = _ref2[1];
return [(x - originBounds.left) * ratio.width + newBounds.left, (y - originBounds.top) * ratio.height + newBounds.top];
});
break;
default:
}
return newShape;
}
var ShapeRenderer = function 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 ? function () {
return null;
} : _props$renderTransfor,
_props$onChangeStart = props.onChangeStart,
onChangeStart = _props$onChangeStart === void 0 ? ef : _props$onChangeStart,
otherProps = _objectWithoutProperties(props, _excluded);
var styles = (0, _ShapeRenderer.default)();
var _useState = (0, _react.useState)(null),
_useState2 = _slicedToArray(_useState, 2),
tmpShape = _useState2[0],
setTmpShape = _useState2[1];
var shape = tmpShape || _shape;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({
className: (0, _classnames.default)(styles.root, className)
}, otherProps), {}, {
children: svgRoot && function () {
var element = null;
var strokeStyle = {
stroke: shape.stroke,
strokeWidth: shape.strokeWidth
};
switch (shape.type) {
case 'ellipse':
element = /*#__PURE__*/(0, _jsxRuntime.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: "".concat(shape.x, "px ").concat(shape.y, "px"),
transform: "rotate(".concat(shape.rotate, "deg)")
}
}, strokeStyle));
break;
case 'line':
element = /*#__PURE__*/(0, _jsxRuntime.jsx)("line", {
x1: shape.point1[0],
y1: shape.point1[1],
x2: shape.point2[0],
y2: shape.point2[1]
});
break;
case 'rectangle':
element = /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", _objectSpread({
x: shape.x,
y: shape.y,
width: shape.width,
height: shape.height,
rotate: "".concat(shape.rotate, "deg"),
style: {
transformOrigin: "".concat(shape.x, "px ").concat(shape.y, "px"),
transform: "rotate(".concat(shape.rotate, "deg)")
}
}, strokeStyle));
break;
case 'mark':
element = /*#__PURE__*/(0, _jsxRuntime.jsxs)("g", {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("rect", _objectSpread({
x: shape.x,
y: shape.y,
width: shape.width,
height: shape.height,
rotate: "".concat(shape.rotate, "deg"),
style: {
fill: 'rgba(255, 77, 79, 0.05)',
transformOrigin: "".concat(shape.x, "px ").concat(shape.y, "px"),
transform: "rotate(".concat(shape.rotate, "deg)")
}
}, strokeStyle)), /*#__PURE__*/(0, _jsxRuntime.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__*/(0, _jsxRuntime.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__*/(0, _jsxRuntime.jsx)("path", _objectSpread({
d: shape.points.map(function (_ref3, index) {
var _ref4 = _slicedToArray(_ref3, 2),
x = _ref4[0],
y = _ref4[1];
return "".concat((index === 0 ? 'M' : 'L') + x, " ").concat(y);
}).join(''),
rotate: "".concat(shape.rotate, "deg"),
style: {
transformOrigin: "".concat(shape.x, "px ").concat(shape.y, "px"),
transform: "rotate(".concat(shape.rotate, "deg)")
}
}, strokeStyle));
break;
default:
}
if (element) {
var svgElement = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/_react.default.cloneElement(element, {
className: (0, _classnames.default)(_defineProperty({}, styles.disabled, shape.disabled))
}), /*#__PURE__*/_react.default.cloneElement(element, {
className: (0, _classnames.default)(styles.svgHit, _defineProperty({}, styles.disabled, shape.disabled)),
onClick: function onClick() {
onShapeClick();
onCheck();
},
onMouseEnter: function onMouseEnter() {
onShapeMouseEnter();
},
onMouseLeave: function onMouseLeave() {
onShapeMouseLeave();
}
})]
});
if (shape.type === 'line') {
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/_reactDom.default.createPortal(svgElement, svgRoot), /*#__PURE__*/(0, _jsxRuntime.jsx)(_transformer.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: function onPointsChange(newPoints) {
setTmpShape(_objectSpread(_objectSpread({}, shape), {}, {
point1: [newPoints[0].x, newPoints[0].y],
point2: [newPoints[1].x, newPoints[1].y]
}));
},
onChangeEnd: function 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__*/(0, _jsxRuntime.jsxs)(_transformer.TransformerBox, {
controlledMode: true,
checked: checked,
disabled: shape.disabled,
bounds: bounds,
rotateEnabled: true,
onBoundsChange: function onBoundsChange(newTmpBounds) {
setTmpShape(getNewShape(shape, bounds, newTmpBounds));
},
onChangeEnd: function onChangeEnd(newBounds) {
onShapeChange(getNewShape(shape, bounds, newBounds));
setTmpShape(null);
},
children: [checked && renderTransformingRect(), /*#__PURE__*/_reactDom.default.createPortal(svgElement, svgRoot)]
});
}
return null;
}()
}));
};
var _default = exports.default = ShapeRenderer;