"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _richEditor = _interopRequireDefault(require("@titaui/rich-editor"));

var _style = require("../style");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var MblogContent = /*#__PURE__*/function (_PureComponent) {
  _inherits(MblogContent, _PureComponent);

  var _super = _createSuper(MblogContent);

  function MblogContent(props) {
    var _this;

    _classCallCheck(this, MblogContent);

    _this = _super.call(this, props);

    _this._handlerTipsLabelClick = function () {// @ts-ignore
      // this.refs.textareaInput.focus(); // 点击 输入框获得焦点
    };

    _this._handlerFocus = function (event) {
      _this.setState({
        isFocus: true
      });

      _this.toEnd(event);
    };

    _this.toEnd = function (event) {
      var obj = event.srcElement ? event.srcElement : event.target;

      if (obj.createTextRange) {
        // IE浏览器
        var range = obj.createTextRange();
        range.moveStart("character", event.target.value.length);
        range.collapse(true); // range.select()
      } else {
        // 非IE浏览器
        obj.setSelectionRange(event.target.value.length, event.target.value.length);
        obj.focus();
      }
    };

    _this._handlerBlur = function (event) {
      _this.props.onContentChange(event.target.value);

      _this.setState({
        isFocus: false
      });
    };

    _this._handlerKeyDown = function (event) {
      event.persist(); // console.log(event)
      // console.log(event.keyCode)
      // let isInputAt = (event.shiftKey && event.keyCode == 50) || event.key == '@'

      var isSubmit = event.ctrlKey && event.keyCode == 13; // if (isInputAt) {
      //   this.props.onContentChange('showUsersSelector') // 输入@弹出选人组件
      // }

      if (isSubmit) {
        _this.props.onActionSubmit("submit"); // ctrl + enter发送回复

      }
    };

    _this._handlerKeyUp = function (event) {// event.persist()
      // console.log(event)
      // console.log(event.keyCode)
      // let isInputAt = (event.shiftKey && event.keyCode == 50) || event.key == '@'
      // // let isSubmit = event.ctrlKey && event.keyCode == 13
      // if (isInputAt) {
      //   this.props.onContentChange('showUsersSelector') // 输入@弹出选人组件
      // }
      // if (isSubmit) {
      //   this.props.onContentChange('isInputEnterSubmit') // ctrl + enter发送回复
      // }
      // if (
      //   (keyCode == 50 || keyCode == 8) &&
      //   val.charAt(caret_pos - 1) == "@"
      // ) {
      //   this._showUserselectorSdk()
      // }
    };

    _this._handlerPaste = function (event) {
      var clipData = event.clipboardData && event.clipboardData.items;
      if (!clipData) return;

      for (var i = 0, len = clipData.length; i < len; i++) {
        var item = clipData[i];

        if (item.kind === "string") {
          item.getAsString(function (str) {});
        } else if (item.kind === "file") {
          var _file = item.getAsFile && item.getAsFile();

          if (_file) {
            var newFileName = "".concat(new Date().getTime() + parseInt("".concat(Math.random() * 100)), ".png");
            var dfl = new File([_file], newFileName, {
              type: _file.type
            }); // @ts-ignore

            _this.props.onPasteSubmit(dfl);
          }
        }
      }
    };

    _this._handleInputChange = function (event) {
      _this.props.onContentChange(event.target.value);

      _this.setTextareaHeight(); // @ts-ignore
      // this.refs.textareaInput.focus();

    };

    _this.onChangeEditor = function (value) {
      _this.props.onContentChange(value);
    };

    _this.setTextareaHeight = function () {
      var _this$props = _this.props,
          initialTextHeight = _this$props.initialTextHeight,
          autoResizing = _this$props.autoResizing;
      if (!autoResizing) return; // const { textareaSetHeight } = this.refs;
      // const { textareaInput } = this.refs;
      // @ts-ignore
      // // textareaInput.style.height = autoResizing && textareaSetHeight.scrollHeight > initialTextHeight ? `${textareaSetHeight.scrollHeight}px` : `${initialTextHeight}px`;
    };

    _this.state = {
      isFocus: false
    };
    return _this;
  } // @ts-ignore


  _createClass(MblogContent, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.autoFocus) {// @ts-ignore
        // this.refs.textareaInput.focus();
      } // this.props.setTextareaRef && this.props.setTextareaRef(this.refs.textareaInput);

    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      if (this.props.textareaValue != prevProps.textareaValue) {
        this.setTextareaHeight();
      }
    } // label提示信息点击事件

  }, {
    key: "render",
    value: function render() {
      var isFocus = this.state.isFocus;
      var _this$props2 = this.props,
          className = _this$props2.className,
          isHasPlaceholder = _this$props2.isHasPlaceholder,
          placeholderText = _this$props2.placeholderText,
          maxLength = _this$props2.maxLength,
          visibility = _this$props2.visibility,
          textareaValue = _this$props2.textareaValue;
      var textareaValueLen = textareaValue.trim().length;
      return /*#__PURE__*/_react["default"].createElement(_style.DivMblogContent, {
        className: [className, isFocus ? "input_default-active" : ""].join(" ")
      }, isHasPlaceholder && /*#__PURE__*/_react["default"].createElement(_style.LabelWrapper, {
        visibility: textareaValueLen > 0 ? "hidden" : "visible"
      }, /*#__PURE__*/_react["default"].createElement(_style.Label, {
        onMouseDown: this._handlerTipsLabelClick,
        style: {
          opacity: isFocus ? "0.8" : "1"
        }
      }, placeholderText)), /*#__PURE__*/_react["default"].createElement(_style.TextareaWrapper, {
        className: "textarea-wrap"
      }, /*#__PURE__*/_react["default"].createElement(_richEditor["default"], {
        ref: "editorRef",
        editable: true,
        showToolbar: false,
        withMentions: true,
        onKeyUp: this._handlerKeyUp,
        onKeyDown: this._handlerKeyDown,
        initialState: {},
        onDocChange: this.onChangeEditor,
        placeholder: placeholderText
      })), /*#__PURE__*/_react["default"].createElement(_style.Tips, {
        className: "textarea-tips",
        onMouseDown: this._handlerTipsLabelClick,
        color: maxLength && textareaValueLen > maxLength ? "#EB4447" : textareaValueLen > 0 ? "#2879ff" : "#BFC3C7"
      }, textareaValueLen, "/", maxLength));
    }
  }]);

  return MblogContent;
}(_react.PureComponent);

exports["default"] = MblogContent;