UNPKG

react-img-editor-en

Version:
175 lines 8.91 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } import Konva from "konva"; import Plugin from "./Plugin"; import { transformerStyle } from "../common/constants"; import { uuid } from "../common/utils"; var Circle = /*#__PURE__*/function (_Plugin) { function Circle() { var _this; _classCallCheck(this, Circle); _this = _callSuper(this, Circle, arguments); _this.name = "circle"; _this.iconfont = "iconfont icon-circle"; _this.title = "Circle"; _this.params = ["strokeWidth", "lineType", "color"]; _this.defaultParamValue = { strokeWidth: 2, lineType: "solid", color: "#F5222D" }; _this.shapeName = "circle"; _this.lastCircle = null; _this.transformer = null; _this.selectedNode = null; _this.isPaint = false; // 防止切换插件时,onDraw 没有释放 _this.started = false; // start draw 的标志 _this.startPoint = [0, 0]; _this.enableTransform = function (drawEventParams, node) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer; if (!_this.transformer) { _this.transformer = new Konva.Transformer(_extends({}, transformerStyle)); drawLayer.add(_this.transformer); _this.transformer.attachTo(node); node.on("mouseenter", function () { stage.container().style.cursor = "move"; }); node.on("mouseleave", function () { stage.container().style.cursor = "default"; }); stage.container().style.cursor = "move"; } node && node.draggable(true); drawLayer.draw(); }; _this.disableTransform = function (drawEventParams, node, remove) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer, pubSub = drawEventParams.pubSub; if (_this.transformer) { _this.transformer.remove(); _this.transformer = null; } if (node) { node.draggable(false); node.off("mouseenter"); node.off("mouseleave"); stage.container().style.cursor = "default"; if (remove) { node.hide(); // 使用隐藏节点占位并覆盖堆栈中已有节点 pubSub.pub("PUSH_HISTORY", node); node.remove(); } } _this.selectedNode = null; drawLayer.draw(); }; _this.onEnter = function (drawEventParams) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer; var container = stage.container(); container.tabIndex = 1; // make it focusable container.focus(); container.addEventListener("keyup", function (e) { if (e.key === "Backspace" && _this.selectedNode) { _this.disableTransform(drawEventParams, _this.selectedNode, true); drawLayer.draw(); } }); }; _this.onClick = function (drawEventParams) { var event = drawEventParams.event; if (event.target.name && event.target.name() === "circle") { // 之前没有选中节点或者在相同节点之间切换点击 if (!_this.selectedNode || _this.selectedNode._id !== event.target._id) { _this.selectedNode && _this.disableTransform(drawEventParams, _this.selectedNode); _this.enableTransform(drawEventParams, event.target); _this.selectedNode = event.target; } } else { _this.disableTransform(drawEventParams, _this.selectedNode); } }; _this.onDrawStart = function () { _this.isPaint = true; }; _this.onDraw = function (drawEventParams) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer, paramValue = drawEventParams.paramValue, pubSub = drawEventParams.pubSub; var pos = stage.getPointerPosition(); if (!_this.isPaint || _this.transformer || !pos) return; if (!_this.started) { _this.startPoint = [pos.x, pos.y]; _this.lastCircle = new Konva.Circle({ id: uuid(), name: "circle", stroke: paramValue && paramValue.color ? paramValue.color : _this.defaultParamValue.color, strokeWidth: paramValue && paramValue.strokeWidth ? paramValue.strokeWidth : _this.defaultParamValue.strokeWidth, globalCompositeOperation: "source-over", radius: 0, dashEnabled: !!(paramValue && paramValue.lineType && paramValue.lineType === "dash"), dash: [8], strokeScaleEnabled: false }); _this.lastCircle.on("transformend", function () { pubSub.pub("PUSH_HISTORY", this); }); _this.lastCircle.on("dragend", function () { pubSub.pub("PUSH_HISTORY", this); }); drawLayer.add(_this.lastCircle); _this.started = true; } var radius = Math.sqrt(Math.pow(pos.x - _this.startPoint[0], 2) + Math.pow(pos.y - _this.startPoint[1], 2)) / 2; _this.lastCircle.x((pos.x + _this.startPoint[0]) / 2); _this.lastCircle.y((pos.y + _this.startPoint[1]) / 2); _this.lastCircle.setAttr("radius", radius); drawLayer.batchDraw(); }; _this.onDrawEnd = function (drawEventParams) { var pubSub = drawEventParams.pubSub; // mouseup event is triggered by move event but click event if (_this.started) { _this.disableTransform(drawEventParams, _this.selectedNode); if (_this.lastCircle) { pubSub.pub("PUSH_HISTORY", _this.lastCircle); } } _this.isPaint = false; _this.started = false; }; _this.onLeave = function (drawEventParams) { _this.isPaint = false; _this.started = false; _this.disableTransform(drawEventParams, _this.selectedNode); }; _this.onNodeRecreate = function (drawEventParams, node) { var pubSub = drawEventParams.pubSub; node.on("transformend", function () { pubSub.pub("PUSH_HISTORY", this); }); node.on("dragend", function () { pubSub.pub("PUSH_HISTORY", this); }); }; return _this; } _inherits(Circle, _Plugin); return _createClass(Circle); }(Plugin); export { Circle as default };