通用评论

index.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. , page: _this2.state.page
  85. }), _react2.default.createElement(
  86. "div",
  87. { className: "comment-more-box", key: "show_more_button" },
  88. !isNoMoreReply && replyCount !== len && _react2.default.createElement(
  89. "span",
  90. {
  91. className: "comment-show-more",
  92. onClick: function onClick() {
  93. return _this2.handleGetMoreReply(commentId);
  94. }
  95. },
  96. _reactIntlUniversal2.default.get("reply.moreReply")
  97. ),
  98. _react2.default.createElement(
  99. "a",
  100. {
  101. style: { float: "right" },
  102. onClick: _this2.handleToggleReply
  103. },
  104. _react2.default.createElement(_icon2.default, { type: "up" }),
  105. " ",
  106. _reactIntlUniversal2.default.get("reply.collapse")
  107. )
  108. )];
  109. }
  110. return _react2.default.createElement(_ContentItem2.default, {
  111. commentId: commentId,
  112. replyId: item.id,
  113. user_id: item.user_id,
  114. key: item.id,
  115. content: item,
  116. action: "replyToReply" // 评论的回复
  117. , page: _this2.state.page
  118. });
  119. })
  120. );
  121. }
  122. return null;
  123. }
  124. }, {
  125. key: "render",
  126. value: function render() {
  127. var _props = this.props,
  128. content = _props.content,
  129. user_id = _props.user_id;
  130. var showReply = this.state.showReply;
  131. return _react2.default.createElement(
  132. "div",
  133. null,
  134. _react2.default.createElement(_ContentItem2.default, {
  135. content: content,
  136. onShowReply: this.handleToggleReply,
  137. showReply: showReply,
  138. commentId: content.id,
  139. user_id: user_id,
  140. action: "reply" // 评论的回复
  141. , page: this.state.page
  142. }),
  143. this.renderReplies(content.replies, content.reply_count, content.isNoMoreReply)
  144. );
  145. }
  146. }]);
  147. return CommentBox;
  148. }(_react.Component);
  149. CommentBox.propTypes = {
  150. commentId: _propTypes2.default.string.isRequired
  151. };
  152. CommentBox.defaultProps = {};
  153. exports.default = (0, _Comment2.default)(CommentBox);
  154. //# sourceMappingURL=index.js.map