"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _button = require("antd/es/button");

var _button2 = _interopRequireDefault(_button);

var _popover = require("antd/es/popover");

var _popover2 = _interopRequireDefault(_popover);

var _icon = require("antd/es/icon");

var _icon2 = _interopRequireDefault(_icon);

var _message2 = require("antd/es/message");

var _message3 = _interopRequireDefault(_message2);

var _input = require("antd/es/input");

var _input2 = _interopRequireDefault(_input);

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

require("antd/es/button/style/css");

require("antd/es/popover/style/css");

require("antd/es/icon/style/css");

require("antd/es/message/style/css");

require("antd/es/input/style/css");

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require("classnames");

var _classnames2 = _interopRequireDefault(_classnames);

var _constant = require("../../constant");

var _helper = require("../../helper");

var _Upload = require("./Upload");

var _Upload2 = _interopRequireDefault(_Upload);

var _Emoji = require("./Emoji");

var _Emoji2 = _interopRequireDefault(_Emoji);

require("./index.css");

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

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var TextArea = _input2.default.TextArea;

var Editor = function (_React$Component) {
  _inherits(Editor, _React$Component);

  function Editor(props) {
    _classCallCheck(this, Editor);

    var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));

    _this.state = {
      showUpload: false,
      value: "", // 编辑器里面的值

      fileList: [], // 图片列表
      fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
    };
    _this.handleChange = _this.handleChange.bind(_this);
    _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
    _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
    _this.handleShowUpload = _this.handleShowUpload.bind(_this);
    _this.handleUpload = _this.handleUpload.bind(_this);
    _this.handleSubmit = _this.handleSubmit.bind(_this);
    _this.resetState = _this.resetState.bind(_this);
    _this.handleEmojiScroll = _this.handleEmojiScroll.bind(_this);
    return _this;
  }

  _createClass(Editor, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if ((0, _helper.isFunction)(this.props.onRef)) {
        this.props.onRef(this);
      }
    }
  }, {
    key: "handleEmojiScroll",
    value: function handleEmojiScroll(e) {
      if (!this.emoji) {
        return;
      }
      e.preventDefault();
      if (e.deltaY > 0) {
        this.emoji.next();
      } else if (e.deltaY < 0) {
        this.emoji.prev();
      }
    }

    /**
     * 编辑器的值改变事件
     * 将最新的值存储到 state 中
     * @param {string} value 输入的值
     */

  }, {
    key: "handleChange",
    value: function handleChange(value) {
      this.setState({ value: value });
      if (this.props.onChange) {
        this.props.onChange(value);
      }
    }

    /**
     * 点击 emoji 的事件
     * 点击后，需要将改 emoji 插入到编辑器中
     * 插入的值为 [emoji chinese name]
     * 参数 emoji 即为 emoji chinese name
     * @param {string}} emoji emoji 的中文，如 微笑
     */

  }, {
    key: "handleClickEmoji",
    value: function handleClickEmoji(emoji) {
      var value = this.state.value;

      value += "[" + emoji + "]";
      this.handleChange(value);
    }

    /**
     * 监听文件列表改变事件
     * @param {Array} fileList 文件列表
     */

  }, {
    key: "handleChangeFileList",
    value: function handleChangeFileList(fileList) {
      this.setState({ fileList: fileList });
    }

    /**
     * 控制上传 Popover 的显示和隐藏
     * @param {boolean} showUpload 是否显示上传的 Popover
     */

  }, {
    key: "handleShowUpload",
    value: function handleShowUpload(showUpload) {
      if (typeof showUpload === "boolean") {
        this.setState({ showUpload: showUpload });
      } else {
        this.setState({ showUpload: !this.state.showUpload });
      }
    }

    /**
     * 上传文件
     * @param {object} param 文件对象
     */

  }, {
    key: "handleUpload",
    value: function handleUpload(_ref) {
      var uid = _ref.uid,
          path = _ref.path;
      var fileMap = this.state.fileMap;
      var fileList = this.state.fileList;

      fileMap[uid] = path;
      fileList = fileList.map(function (item) {
        if (item.uid === uid) {
          item.thumbUrl = _constant.OSS_LINK + path;
        }
        return item;
      });
      this.setState({ fileMap: fileMap, fileList: fileList });
    }

    /**
     * 提交编辑器内容
     * 提交功能，交给父组件来实现
     * 需要父组件传入 onSubmit
     */

  }, {
    key: "handleSubmit",
    value: function handleSubmit() {
      var _this2 = this;

      var maxLength = this.props.maxLength;
      var _state = this.state,
          value = _state.value,
          fileMap = _state.fileMap,
          fileList = _state.fileList;

      if (value.length > maxLength) {
        _message3.default.error("\u5B57\u6570\u4E0D\u5F97\u8D85\u8FC7" + maxLength + "\u5B57");
        return;
      }
      var files = [];
      if (fileList.length) {
        fileList.forEach(function (item) {
          if (!fileMap[item.uid]) {
            return;
          }
          files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
        });
      }
      if (this.props.beforeSubmit) {
        Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
          if (!(res === false)) {
            _this2.props.onSubmit({ text: value, files: files }, function () {
              _this2.resetState();
              if (_this2.props.onCommentSuccess) {
                _this2.props.onCommentSuccess();
              }
            });
          }
        });
      } else {
        this.props.onSubmit({ text: value, files: files }, function () {
          _this2.resetState();
          if (_this2.props.onCommentSuccess) {
            _this2.props.onCommentSuccess();
          }
        });
      }
    }
  }, {
    key: "resetState",
    value: function resetState() {
      this.setState({
        showUpload: false,
        value: "",
        fileList: [],
        fileMap: {}
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _props = this.props,
          value = _props.value,
          placeholder = _props.placeholder,
          rows = _props.rows,
          showEmoji = _props.showEmoji,
          showUpload = _props.showUpload,
          closeUploadWhenBlur = _props.closeUploadWhenBlur,
          maxUpload = _props.maxUpload,
          btnSubmitText = _props.btnSubmitText,
          btnLoading = _props.btnLoading,
          btnDisabled = _props.btnDisabled,
          button = _props.button,
          emojiToolIcon = _props.emojiToolIcon,
          imageToolIcon = _props.imageToolIcon,
          maxLength = _props.maxLength,
          autoFocus = _props.autoFocus;

      var handleSubmit = this.handleSubmit;
      var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
      var inputValue = value || this.state.value;
      return _react2.default.createElement(
        "div",
        { className: "comment-editor-container" },
        _react2.default.createElement(
          "div",
          {
            className: (0, _classnames2.default)({
              "comment-editor-toolbar": true,
              "comment-editor-toolbar-error": inputValue.length > maxLength
            })
          },
          "\u5DF2\u8F93\u5165 ",
          inputValue.length,
          " / ",
          maxLength,
          " \u5B57"
        ),
        _react2.default.createElement(
          "div",
          { className: "comment-editor" },
          _react2.default.createElement(TextArea, {
            value: inputValue,
            onChange: function onChange(e) {
              return _this3.handleChange(e.target.value);
            },
            rows: rows,
            placeholder: placeholder,
            autoFocus: autoFocus
          }),
          _react2.default.createElement(
            "div",
            { className: "comment-toolbar" },
            _react2.default.createElement(
              "div",
              { className: "comment-toolbar-left" },
              showEmoji && _react2.default.createElement(
                _popover2.default,
                {
                  trigger: "click",
                  placement: "bottomLeft",
                  autoAdjustOverflow: false,
                  content: _react2.default.createElement(
                    "div",
                    { style: { width: 240 }, onWheel: this.handleEmojiScroll },
                    _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji, ref: function ref(node) {
                        _this3.emoji = node;
                      } })
                  ),
                  overlayClassName: "comment-emoji-popover"
                },
                emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
              ),
              showUpload ? _react2.default.createElement(
                _popover2.default,
                {
                  trigger: "click",
                  visible: this.state.showUpload,
                  overlayStyle: { zIndex: 999 },
                  onVisibleChange: closeUploadWhenBlur ? function (visible) {
                    _this3.handleShowUpload(visible);
                  } : null,
                  content: _react2.default.createElement(
                    "div",
                    {
                      style: {
                        width: 336, // 一行显示3张
                        minHeight: 100,
                        margin: "0 auto"
                      }
                    },
                    _react2.default.createElement(_Upload2.default, {
                      onChangeFileList: this.handleChangeFileList,
                      onUpload: this.handleUpload,
                      maxUpload: maxUpload,
                      fileList: this.state.fileList,
                      showError: this.props.showError,
                      onError: this.props.onError
                    }),
                    _react2.default.createElement("div", { className: "clearfix" })
                  ),
                  placement: "bottomLeft",
                  title: _react2.default.createElement(
                    "div",
                    { style: { margin: "5px auto" } },
                    _react2.default.createElement(
                      "span",
                      null,
                      "\u4E0A\u4F20\u56FE\u7247",
                      maxUpload >= 2 ? _react2.default.createElement(
                        "span",
                        { style: { color: "#666", fontWeight: 400 } },
                        "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
                        maxUpload - this.state.fileList.length,
                        "\u5F20\u56FE\u7247)"
                      ) : null
                    ),
                    _react2.default.createElement(_icon2.default, {
                      type: "close",
                      onClick: function onClick() {
                        return _this3.handleShowUpload(false);
                      },
                      style: {
                        float: "right",
                        cursor: "pointer",
                        marginTop: 4
                      }
                    })
                  )
                },
                imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
                  onClick: function onClick() {
                    return _this3.handleShowUpload(true);
                  }
                }) : _react2.default.createElement(_icon2.default, {
                  type: "picture",
                  className: "comment-toolbar-icon",
                  style: { marginLeft: 10 },
                  onClick: function onClick() {
                    return _this3.handleShowUpload(true);
                  }
                })
              ) : null
            ),
            _react2.default.createElement(
              "div",
              { className: "comment-toolbar-right" },
              button ? _react2.default.cloneElement(button, {
                onClick: button.props.onClick || handleSubmit
              }) : _react2.default.createElement(
                _button2.default,
                {
                  onClick: function onClick() {
                    return _this3.handleSubmit();
                  },
                  type: "primary",
                  loading: btnLoading,
                  disabled: disabledSubmit
                },
                btnSubmitText
              )
            )
          )
        )
      );
    }
  }]);

  return Editor;
}(_react2.default.Component);

Editor.propTypes = {
  rows: _propTypes2.default.number,
  placeholder: _propTypes2.default.string,
  showEmoji: _propTypes2.default.bool,
  showUpload: _propTypes2.default.bool,
  closeUploadWhenBlur: _propTypes2.default.bool,
  maxUpload: _propTypes2.default.number,
  value: _propTypes2.default.string,
  onChange: _propTypes2.default.func,
  onSubmit: _propTypes2.default.func,
  beforeSubmit: _propTypes2.default.func,
  btnSubmitText: _propTypes2.default.string,
  btnLoading: _propTypes2.default.bool,
  btnDisabled: _propTypes2.default.bool,
  button: _propTypes2.default.node,
  emojiToolIcon: _propTypes2.default.node,
  imageToolIcon: _propTypes2.default.node,
  showError: _propTypes2.default.bool,
  onError: _propTypes2.default.func,
  maxLength: _propTypes2.default.number
};

Editor.defaultProps = {
  rows: 5,
  placeholder: "说点什么吧...",
  showEmoji: true,
  showUpload: true,
  closeUploadWhenBlur: false,
  maxUpload: 1,
  btnSubmitText: "发表",
  btnLoading: false,
  btnDisabled: false,
  showError: true,
  maxLength: 140
};

exports.default = Editor;
//# sourceMappingURL=index.js.map