UNPKG

react-img-editor-en

Version:
238 lines 11.7 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 Text = /*#__PURE__*/function (_Plugin) { function Text() { var _this; _classCallCheck(this, Text); _this = _callSuper(this, Text, arguments); _this.name = "text"; _this.iconfont = "iconfont icon-text"; _this.title = "Text"; _this.params = ["fontSize", "color"]; _this.defaultParamValue = { fontSize: 12, color: "#F5222D" }; _this.shapeName = "text"; _this.transformer = null; _this.selectedNode = null; _this.removeTextareaBlurModal = function () { var textareaBlurModal = document.getElementById("textareaBlurModal"); if (textareaBlurModal) { textareaBlurModal.removeEventListener("click", _this.removeTextareaBlurModal); document.body.removeChild(textareaBlurModal); } }; // 防止 textarea blur 时触发 stage click 事件 _this.addTextareaBlurModal = function (stage) { if (document.getElementById("textareaBlurModal")) return; var container = stage.container().getBoundingClientRect(); var textareaBlurModal = document.createElement("div"); textareaBlurModal.id = "textareaBlurModal"; textareaBlurModal.style.position = "fixed"; textareaBlurModal.style.left = container.left + "px"; textareaBlurModal.style.top = container.top + "px"; textareaBlurModal.style.width = container.width + "px"; textareaBlurModal.style.height = container.height + "px"; textareaBlurModal.style.zIndex = "999"; document.body.appendChild(textareaBlurModal); textareaBlurModal.addEventListener("click", _this.removeTextareaBlurModal); }; _this.createTextarea = function (stage, drawLayer, transformer, textNode, pubSub) { var textarea = document.createElement("textarea"); textarea.value = textNode.text(); textarea.style.position = "absolute"; textarea.style.left = textNode.x() + "px"; textarea.style.top = textNode.y() + "px"; textarea.style.width = textNode.width() + "px"; textarea.style.height = textNode.height() + "px"; textarea.style.lineHeight = String(textNode.lineHeight()); textarea.style.padding = textNode.padding() + "px"; textarea.style.margin = "0px"; textarea.style.fontSize = textNode.fontSize() + "px"; textarea.style.color = textNode.fill(); textarea.style.fontFamily = textNode.fontFamily(); textarea.style.border = "none"; textarea.style.outline = "none"; textarea.style.overflow = "hidden"; textarea.style.background = "none"; textarea.style.resize = "none"; textarea.style.zIndex = "1000"; textarea.style.boxSizing = "content-box"; textarea.addEventListener("keyup", function (e) { textNode.text(e.target.value); drawLayer.draw(); textarea.style.width = textNode.width() + "px"; textarea.style.height = textNode.height() + "px"; }); textarea.addEventListener("blur", function () { if (textarea.value !== "") { textNode.text(textarea.value); transformer.hide(); textNode.show(); } else { textNode.destroy(); transformer.destroy(); } textarea.parentNode.removeChild(textarea); drawLayer.draw(); _this.removeTextareaBlurModal(); pubSub.pub("PUSH_HISTORY", textNode); }); return textarea; }; _this.enableTransform = function (drawEventParams, node) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer; if (!_this.transformer) { _this.transformer = new Konva.Transformer(_extends(_extends({}, transformerStyle), { enabledAnchors: [], padding: 2 })); 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 = "text"; }); 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 = "text"; if (remove) { node.hide(); // 使用隐藏节点占位并覆盖堆栈中已有节点 pubSub.pub("PUSH_HISTORY", node); node.remove(); node.remove(); } } _this.selectedNode = null; drawLayer.draw(); }; _this.onEnter = function (drawEventParams) { var stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer; var container = stage.container(); container.style.cursor = "text"; 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, stage = drawEventParams.stage, drawLayer = drawEventParams.drawLayer, paramValue = drawEventParams.paramValue, pubSub = drawEventParams.pubSub; if (event.target.name && event.target.name() === "text") { // 之前没有选中节点或者在相同节点之间切换点击 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; } return; } else if (_this.selectedNode) { _this.disableTransform(drawEventParams, _this.selectedNode); return; } var fontSize = paramValue && paramValue.fontSize ? paramValue.fontSize : _this.defaultParamValue.fontSize; var color = paramValue && paramValue.color ? paramValue.color : _this.defaultParamValue.color; var startPos = stage.getPointerPosition(); if (!startPos) return; var textNode = new Konva.Text({ id: uuid(), name: "text", x: startPos.x, y: startPos.y - 10, fontSize: fontSize, fill: color, padding: 3, lineHeight: 1.1 }); textNode.on("dragend", function () { pubSub.pub("PUSH_HISTORY", this); }); // 由于 konvajs 的文本渲染和浏览器渲染的样式不一致,所以使用 Transformer 的边框来代替 textarea 自身的边框 var textareaTransformer = new Konva.Transformer({ node: textNode, enabledAnchors: [], rotateEnabled: false, borderStroke: color }); drawLayer.add(textNode); drawLayer.add(textareaTransformer); textNode.hide(); drawLayer.draw(); var textarea = _this.createTextarea(stage, drawLayer, textareaTransformer, textNode, pubSub); stage.container().appendChild(textarea); textarea.focus(); _this.addTextareaBlurModal(stage); textNode.on("dblclick dbltap", function (e) { // dblclick 前会触发两次 onClick 事件,因此要清楚 onClick 事件里的状态 _this.disableTransform(drawEventParams, _this.selectedNode); e.cancelBubble = true; var textarea = _this.createTextarea(stage, drawLayer, textareaTransformer, textNode, pubSub); stage.container().appendChild(textarea); textarea.focus(); textNode.hide(); textareaTransformer.show(); drawLayer.draw(); _this.addTextareaBlurModal(stage); }); }; _this.onLeave = function (drawEventParams) { var stage = drawEventParams.stage; stage.container().style.cursor = "default"; _this.removeTextareaBlurModal(); _this.disableTransform(drawEventParams, _this.selectedNode); }; _this.onNodeRecreate = function (drawEventParams, node) { var pubSub = drawEventParams.pubSub; node.on("dragend", function () { pubSub.pub("PUSH_HISTORY", this); }); }; return _this; } _inherits(Text, _Plugin); return _createClass(Text); }(Plugin); export { Text as default };