"use strict";

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

var _popconfirm = require("antd/es/popconfirm");

var _popconfirm2 = _interopRequireDefault(_popconfirm);

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

var _icon2 = _interopRequireDefault(_icon);

var _tooltip = require("antd/es/tooltip");

var _tooltip2 = _interopRequireDefault(_tooltip);

var _avatar = require("antd/es/avatar");

var _avatar2 = _interopRequireDefault(_avatar);

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/popconfirm/style/css");

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

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

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

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _dayjs = require("dayjs");

var _dayjs2 = _interopRequireDefault(_dayjs);

require("dayjs/locale/zh-cn");

var _relativeTime = require("dayjs/plugin/relativeTime");

var _relativeTime2 = _interopRequireDefault(_relativeTime);

var _Comment = require("../../Comment");

var _Comment2 = _interopRequireDefault(_Comment);

var _CommentInput = require("../CommentInput");

var _CommentInput2 = _interopRequireDefault(_CommentInput);

var _avatar3 = require("../../avatar");

var _avatar4 = _interopRequireDefault(_avatar3);

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

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

require("./index.css");

var _ImagePreviewer = require("../ImagePreviewer/ImagePreviewer");

var _ImagePreviewer2 = _interopRequireDefault(_ImagePreviewer);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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; }

_dayjs2.default.locale("zh-cn");
_dayjs2.default.extend(_relativeTime2.default);

var CommentItem = function (_Component) {
  _inherits(CommentItem, _Component);

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

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

    _this.state = {
      showInput: false,
      showPreviewer: false,
      previewerIndex: 0
    };
    _this.handleToggleInput = _this.handleToggleInput.bind(_this);
    _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
    _this.showPreviewer = _this.showPreviewer.bind(_this);
    _this.hidePreviewer = _this.hidePreviewer.bind(_this);
    return _this;
  }

  _createClass(CommentItem, [{
    key: "showPreviewer",
    value: function showPreviewer(index) {
      this.setState({
        showPreviewer: true,
        previewerIndex: index
      });
    }
  }, {
    key: "hidePreviewer",
    value: function hidePreviewer() {
      this.setState({
        showPreviewer: false
      });
    }
  }, {
    key: "handleToggleInput",
    value: function handleToggleInput() {
      this.setState({ showInput: !this.state.showInput });
    }
  }, {
    key: "renderTextWithReply",
    value: function renderTextWithReply(text, content) {
      var newText = text;
      var reply = content.reply;

      if (reply) {
        // newText = `${newText} //@<a href="/${reply.user_id}">${
        //   reply.user_name
        // }</a> ${reply.content}`;
        newText = newText + " //@" + reply.user_name + " " + reply.content;
        // newText = (
        //   <span>
        //     {newText}
        //     @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
        //   </span>
        // )
        if (reply.reply) {
          return this.renderTextWithReply(newText, reply);
        }
      }
      return newText;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          commentId = _props.commentId,
          replyId = _props.replyId,
          content = _props.content,
          action = _props.action,
          showReply = _props.showReply,
          onShowReply = _props.onShowReply,
          app = _props.app;
      var showInput = this.state.showInput;


      var newContent = content.content;
      var images = "";
      if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
        newContent = newContent.split(_constant.IMAGE_SPLIT);
        images = newContent.pop();
        newContent = newContent.join("");
      }

      var imageList = images.split(",");

      // 在3, 7前需要换行
      var needClear = imageList.length === 5 || imageList.length === 6 || imageList.length === 9;
      var imgs = [].concat(_toConsumableArray(imageList));
      if (needClear) {
        if (imgs.length > 6) {
          imgs.splice(3, 0, { type: "divider" });
          imgs.splice(7, 0, { type: "divider" });
        } else {
          imgs.splice(3, 0, { type: "divider" });
        }
      }

      return _react2.default.createElement(
        "div",
        { className: "comment-item-box" },
        _react2.default.createElement(
          "div",
          { className: "comment-item-left" },
          _react2.default.createElement(_avatar2.default, { src: content.user_avatar || _avatar4.default, size: "large" })
        ),
        _react2.default.createElement(
          "div",
          { className: "comment-item-right" },
          _react2.default.createElement(
            "div",
            null,
            _react2.default.createElement(
              "strong",
              null,
              content.user_name || "游客"
            ),
            _react2.default.createElement(
              "span",
              { style: { marginLeft: 10 } },
              _react2.default.createElement(
                _tooltip2.default,
                {
                  placement: "top",
                  title: (0, _dayjs2.default)(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")
                },
                (0, _dayjs2.default)(content.created * 1000).fromNow()
              )
            )
          ),
          _react2.default.createElement("div", {
            className: "comment-item-content",
            dangerouslySetInnerHTML: {
              __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
            }
          }),
          // image为空时不渲染comment-item-image
          imageList.length > 0 && imageList[0] !== "" && _react2.default.createElement(
            "div",
            { className: "comment-item-image" },
            !this.state.showPreviewer && imgs.map(function (item, index) {
              if (item.type === "divider") {
                return _react2.default.createElement(
                  "div",
                  { className: "comment-item-image-wrapper", key: index },
                  _react2.default.createElement("div", { className: "comment-img-divider" })
                );
              }
              return _react2.default.createElement(
                "div",
                {
                  className: "comment-item-image-wrapper",
                  key: index,
                  onClick: function onClick() {
                    var i = index;
                    if (needClear) {
                      if (index > 3) {
                        i -= 1;
                      }
                      if (index > 7) {
                        i -= 1;
                      }
                    }
                    _this2.showPreviewer(i);
                  }
                },
                _react2.default.createElement("div", {
                  style: { backgroundImage: "url(" + item + ")" },
                  className: "comment-img-thumbnail"
                })
              );
            }),
            this.state.showPreviewer && _react2.default.createElement(_ImagePreviewer2.default, {
              list: imageList,
              index: this.state.previewerIndex,
              onFold: this.hidePreviewer
            }),
            _react2.default.createElement("div", { className: "clearfix" })
          ),
          _react2.default.createElement(
            "div",
            { className: "comment-item-bottom" },
            content.reply_count ? _react2.default.createElement(
              "div",
              null,
              _react2.default.createElement(
                "a",
                { className: "comment-item-bottom-left", onClick: onShowReply },
                content.reply_count,
                " \u6761\u56DE\u590D",
                showReply ? _react2.default.createElement(_icon2.default, { type: "up" }) : _react2.default.createElement(_icon2.default, { type: "down" })
              )
            ) : null,
            app.userId === content.user_id && _react2.default.createElement(
              _popconfirm2.default,
              {
                title: "\u786E\u5B9A\u8981\u5220\u9664\u5417?",
                onConfirm: function onConfirm() {
                  if (replyId) {
                    app.sDeleteReply(content.id, commentId);
                    return;
                  }
                  app.sDeleteComment(content.id);
                },
                okText: "\u786E\u5B9A",
                cancelText: "\u53D6\u6D88"
              },
              _react2.default.createElement(
                "a",
                { className: "comment-item-bottom-right" },
                "\xA0 \u5220\u9664"
              )
            ),
            _react2.default.createElement(
              "a",
              {
                onClick: this.handleToggleInput,
                className: "comment-item-bottom-right"
              },
              "\xA0 \u56DE\u590D"
            ),
            _react2.default.createElement(
              "div",
              {
                className: "comment-item-bottom-right",
                onClick: function onClick() {
                  if (replyId) {
                    // 如果有 replyId，则说明是评论的回复
                    app.sReplyFavor(content.id, commentId, content.favored);
                    return;
                  }
                  app.sCommentFavor(content.id, content.favored);
                }
              },
              _react2.default.createElement(_icon2.default, {
                type: "like-o",
                className: content.favored ? "comment-favor comment-favored" : "comment-favor"
              }),
              "\xA0",
              content.favor_count
            )
          )
        ),
        showInput && _react2.default.createElement(_CommentInput2.default, {
          content: app.children,
          action: action,
          replyId: replyId,
          commentId: commentId,
          callback: this.handleToggleInput
        })
      );
    }
  }]);

  return CommentItem;
}(_react.Component);

CommentItem.propTypes = {
  content: _propTypes2.default.object.isRequired,
  // comment 评论
  // reply 评论的回复
  // replyToReply 回复的回复
  action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"]),
  onShowReply: _propTypes2.default.func
};

CommentItem.defaultProps = {
  action: "comment",
  onShowReply: function onShowReply() {}
};

exports.default = (0, _Comment2.default)(CommentItem);
//# sourceMappingURL=index.js.map