通用评论

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