react-img-editor-en
Version:
Image Annotation Tool for React
182 lines (181 loc) • 9.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _konva = _interopRequireDefault(require("konva"));
var _Plugin2 = _interopRequireDefault(require("./Plugin"));
var _constants = require("../common/constants");
var _utils = require("../common/utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
var Arrow = exports["default"] = /*#__PURE__*/function (_Plugin) {
function Arrow() {
var _this;
_classCallCheck(this, Arrow);
_this = _callSuper(this, Arrow, arguments);
_this.name = "arrow";
_this.iconfont = "iconfont icon-arrow";
_this.title = "Arrow";
_this.params = ["strokeWidth", "color"];
_this.defaultParamValue = {
strokeWidth: 2,
lineType: "solid",
color: "#F5222D"
};
_this.shapeName = "arrow";
_this.lastArrow = null;
_this.transformer = null;
_this.selectedNode = null;
_this.isPaint = false;
_this.started = false;
_this.startPoints = [0, 0];
_this.enableTransform = function (drawEventParams, node) {
var stage = drawEventParams.stage,
drawLayer = drawEventParams.drawLayer;
if (!_this.transformer) {
_this.transformer = new _konva["default"].Transformer(_extends(_extends({}, _constants.transformerStyle), {
rotateEnabled: true
}));
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() === "arrow") {
// 之前没有选中节点或者在相同节点之间切换点击
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 && pos) {
_this.startPoints = [pos.x, pos.y];
var strokeColor = paramValue && paramValue.color ? paramValue.color : _this.defaultParamValue.color;
_this.lastArrow = new _konva["default"].Arrow({
id: (0, _utils.uuid)(),
name: "arrow",
stroke: strokeColor,
strokeWidth: paramValue && paramValue.strokeWidth ? paramValue.strokeWidth : _this.defaultParamValue.strokeWidth,
globalCompositeOperation: "source-over",
points: _this.startPoints,
dashEnabled: !!(paramValue && paramValue.lineType && paramValue.lineType === "dash"),
dash: [8],
fill: strokeColor,
strokeScaleEnabled: false
});
_this.lastArrow.on("transformend", function () {
pubSub.pub("PUSH_HISTORY", this);
});
_this.lastArrow.on("dragend", function () {
pubSub.pub("PUSH_HISTORY", this);
});
drawLayer.add(_this.lastArrow);
_this.started = true;
}
_this.lastArrow.points([_this.startPoints[0], _this.startPoints[1], pos.x, pos.y]);
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.lastArrow) {
pubSub.pub("PUSH_HISTORY", _this.lastArrow);
}
}
_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(Arrow, _Plugin);
return _createClass(Arrow);
}(_Plugin2["default"]);