通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _icon = require("antd/es/icon");
  6. var _icon2 = _interopRequireDefault(_icon);
  7. 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; }; }();
  8. require("antd/es/icon/style/css");
  9. var _react = require("react");
  10. var _react2 = _interopRequireDefault(_react);
  11. var _propTypes = require("prop-types");
  12. var _propTypes2 = _interopRequireDefault(_propTypes);
  13. var _reactIntlUniversal = require("react-intl-universal");
  14. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  15. var _Comment = require("../../Comment");
  16. var _Comment2 = _interopRequireDefault(_Comment);
  17. var _ContentItem = require("./../ContentItem");
  18. var _ContentItem2 = _interopRequireDefault(_ContentItem);
  19. require("./index.css");
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  22. 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; }
  23. 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; }
  24. var CommentBox = function (_Component) {
  25. _inherits(CommentBox, _Component);
  26. function CommentBox(props) {
  27. _classCallCheck(this, CommentBox);
  28. var _this = _possibleConstructorReturn(this, (CommentBox.__proto__ || Object.getPrototypeOf(CommentBox)).call(this, props));
  29. _this.state = {
  30. showReply: true,
  31. page: 1
  32. };
  33. _this.handleToggleReply = _this.handleToggleReply.bind(_this);
  34. _this.handleGetMoreReply = _this.handleGetMoreReply.bind(_this);
  35. _this.renderReplies = _this.renderReplies.bind(_this);
  36. return _this;
  37. }
  38. /**
  39. * 切换是否显示回复列表
  40. */
  41. _createClass(CommentBox, [{
  42. key: "handleToggleReply",
  43. value: function handleToggleReply() {
  44. this.setState({ showReply: !this.state.showReply });
  45. }
  46. /**
  47. * 获取更多评论
  48. * @param {string} commentId comment id
  49. */
  50. }, {
  51. key: "handleGetMoreReply",
  52. value: function handleGetMoreReply(commentId) {
  53. // 从第一页开始获取评论
  54. var page = this.state.page;
  55. this.props.app.sGetReply({ commentId: commentId, page: page });
  56. this.setState({ page: page + 1 });
  57. }
  58. /**
  59. * 渲染回复 DOM
  60. * @param {array} replies 回复列表
  61. * @param {number} replies 回复的数量
  62. * @param {boolean} isNoMoreReply 是否没有更多回复
  63. */
  64. }, {
  65. key: "renderReplies",
  66. value: function renderReplies(replies, replyCount, isNoMoreReply) {
  67. var _this2 = this;
  68. var commentId = this.props.commentId;
  69. var showReply = this.state.showReply;
  70. if (showReply && replies && replies.length) {
  71. var len = replies.length;
  72. return _react2.default.createElement(
  73. "div",
  74. { style: { marginLeft: 50 } },
  75. replies.map(function (item, index) {
  76. if (index === len - 1) {
  77. return [_react2.default.createElement(_ContentItem2.default, {
  78. commentId: commentId,
  79. replyId: item.id,
  80. key: item.id,
  81. content: item,
  82. user_id: item.user_id,
  83. action: "replyToReply" // 回复的回复
  84. }), _react2.default.createElement(
  85. "div",
  86. { className: "comment-more-box", key: "show_more_button" },
  87. !isNoMoreReply && replyCount !== len && _react2.default.createElement(
  88. "span",
  89. {
  90. className: "comment-show-more",
  91. onClick: function onClick() {
  92. return _this2.handleGetMoreReply(commentId);
  93. }
  94. },
  95. _reactIntlUniversal2.default.get("reply.moreReply")
  96. ),
  97. _react2.default.createElement(
  98. "a",
  99. {
  100. style: { float: "right" },
  101. onClick: _this2.handleToggleReply
  102. },
  103. _react2.default.createElement(_icon2.default, { type: "up" }),
  104. " ",
  105. _reactIntlUniversal2.default.get("reply.collapse")
  106. )
  107. )];
  108. }
  109. return _react2.default.createElement(_ContentItem2.default, {
  110. commentId: commentId,
  111. replyId: item.id,
  112. key: item.id,
  113. content: item,
  114. action: "replyToReply" // 评论的回复
  115. });
  116. })
  117. );
  118. }
  119. return null;
  120. }
  121. }, {
  122. key: "render",
  123. value: function render() {
  124. var _props = this.props,
  125. content = _props.content,
  126. user_id = _props.user_id;
  127. var showReply = this.state.showReply;
  128. return _react2.default.createElement(
  129. "div",
  130. null,
  131. _react2.default.createElement(_ContentItem2.default, {
  132. content: content,
  133. onShowReply: this.handleToggleReply,
  134. showReply: showReply,
  135. commentId: content.id,
  136. user_id: user_id,
  137. action: "reply" // 评论的回复
  138. }),
  139. this.renderReplies(content.replies, content.reply_count, content.isNoMoreReply)
  140. );
  141. }
  142. }]);
  143. return CommentBox;
  144. }(_react.Component);
  145. CommentBox.propTypes = {
  146. commentId: _propTypes2.default.string.isRequired
  147. };
  148. CommentBox.defaultProps = {};
  149. exports.default = (0, _Comment2.default)(CommentBox);
  150. //# sourceMappingURL=index.js.map