通用评论

index.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 _tooltip = require('antd/es/tooltip');
  8. var _tooltip2 = _interopRequireDefault(_tooltip);
  9. var _avatar = require('antd/es/avatar');
  10. var _avatar2 = _interopRequireDefault(_avatar);
  11. 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; }; }();
  12. require('antd/es/icon/style/css');
  13. require('antd/es/tooltip/style/css');
  14. require('antd/es/avatar/style/css');
  15. var _react = require('react');
  16. var _react2 = _interopRequireDefault(_react);
  17. var _propTypes = require('prop-types');
  18. var _propTypes2 = _interopRequireDefault(_propTypes);
  19. var _dayjs = require('dayjs');
  20. var _dayjs2 = _interopRequireDefault(_dayjs);
  21. require('dayjs/locale/zh-cn');
  22. var _relativeTime = require('dayjs/plugin/relativeTime');
  23. var _relativeTime2 = _interopRequireDefault(_relativeTime);
  24. var _Comment = require('../../Comment');
  25. var _Comment2 = _interopRequireDefault(_Comment);
  26. var _CommentInput = require('../CommentInput');
  27. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  28. var _avatar3 = require('../../avatar');
  29. var _avatar4 = _interopRequireDefault(_avatar3);
  30. var _helper = require('../../helper');
  31. require('./index.css');
  32. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  33. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  34. 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; }
  35. 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; }
  36. _dayjs2.default.locale('zh-cn');
  37. _dayjs2.default.extend(_relativeTime2.default);
  38. var CommentItem = function (_Component) {
  39. _inherits(CommentItem, _Component);
  40. function CommentItem(props) {
  41. _classCallCheck(this, CommentItem);
  42. var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));
  43. _this.state = {
  44. showInput: false
  45. };
  46. _this.handleToggleInput = _this.handleToggleInput.bind(_this);
  47. _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
  48. return _this;
  49. }
  50. _createClass(CommentItem, [{
  51. key: 'handleToggleInput',
  52. value: function handleToggleInput() {
  53. this.setState({ showInput: !this.state.showInput });
  54. }
  55. }, {
  56. key: 'renderTextWithReply',
  57. value: function renderTextWithReply(text, content) {
  58. var newText = text;
  59. var reply = content.reply;
  60. if (reply) {
  61. newText = newText + ' //@<a href="/' + reply.user_id + '">' + reply.user_name + '</a> ' + reply.content;
  62. if (reply.reply) {
  63. return this.renderTextWithReply(newText, reply);
  64. }
  65. }
  66. return newText;
  67. }
  68. }, {
  69. key: 'render',
  70. value: function render() {
  71. var _props = this.props,
  72. commentId = _props.commentId,
  73. replyId = _props.replyId,
  74. content = _props.content,
  75. action = _props.action,
  76. showReply = _props.showReply,
  77. onShowReply = _props.onShowReply,
  78. app = _props.app;
  79. var showInput = this.state.showInput;
  80. return _react2.default.createElement(
  81. 'div',
  82. { className: 'comment-item-box' },
  83. _react2.default.createElement(
  84. 'div',
  85. { className: 'comment-item-left' },
  86. _react2.default.createElement(_avatar2.default, { src: content.user_avatar || _avatar4.default, size: 'large' })
  87. ),
  88. _react2.default.createElement(
  89. 'div',
  90. { className: 'comment-item-right' },
  91. _react2.default.createElement(
  92. 'div',
  93. null,
  94. _react2.default.createElement(
  95. 'a',
  96. { href: '/' + content.user_id },
  97. content.user_name || '暂无昵称'
  98. ),
  99. _react2.default.createElement(
  100. 'span',
  101. { style: { marginLeft: 10 } },
  102. _react2.default.createElement(
  103. _tooltip2.default,
  104. {
  105. placement: 'top',
  106. title: (0, _dayjs2.default)(content.created * 1000).format('YYYY-MM-DD HH:mm:ss')
  107. },
  108. (0, _dayjs2.default)(content.created * 1000).fromNow()
  109. )
  110. )
  111. ),
  112. _react2.default.createElement('div', {
  113. className: 'comment-item-content',
  114. dangerouslySetInnerHTML: {
  115. __html: (0, _helper.renderContent)(this.renderTextWithReply(content.content, content))
  116. }
  117. }),
  118. _react2.default.createElement(
  119. 'div',
  120. { className: 'comment-item-bottom' },
  121. content.reply_count ? _react2.default.createElement(
  122. 'div',
  123. null,
  124. _react2.default.createElement(
  125. 'a',
  126. { className: 'comment-item-bottom-left', onClick: onShowReply },
  127. content.reply_count,
  128. ' \u6761\u56DE\u590D',
  129. showReply ? _react2.default.createElement(_icon2.default, { type: 'up' }) : _react2.default.createElement(_icon2.default, { type: 'down' })
  130. )
  131. ) : null,
  132. _react2.default.createElement(
  133. 'a',
  134. {
  135. onClick: this.handleToggleInput,
  136. className: 'comment-item-bottom-right'
  137. },
  138. '\xA0 \u56DE\u590D'
  139. ),
  140. _react2.default.createElement(
  141. 'div',
  142. {
  143. className: 'comment-item-bottom-right',
  144. onClick: function onClick() {
  145. if (replyId) {
  146. // 如果有 replyId,则说明是评论的回复
  147. app.sReplyFavor(content.id, commentId, content.favored);
  148. return;
  149. }
  150. app.sCommentFavor(content.id, content.favored);
  151. }
  152. },
  153. _react2.default.createElement(_icon2.default, {
  154. type: 'like-o',
  155. className: content.favored ? 'comment-favor comment-favored' : 'comment-favor'
  156. }),
  157. '\xA0',
  158. content.favor_count
  159. )
  160. )
  161. ),
  162. showInput && _react2.default.createElement(_CommentInput2.default, {
  163. content: app.children,
  164. action: action,
  165. replyId: replyId,
  166. commentId: commentId,
  167. callback: this.handleToggleInput
  168. })
  169. );
  170. }
  171. }]);
  172. return CommentItem;
  173. }(_react.Component);
  174. CommentItem.propTypes = {
  175. content: _propTypes2.default.object.isRequired,
  176. // comment 评论
  177. // reply 评论的回复
  178. // replyToReply 回复的回复
  179. action: _propTypes2.default.oneOf(['comment', 'reply', 'replyToReply']),
  180. onShowReply: _propTypes2.default.func
  181. };
  182. CommentItem.defaultProps = {
  183. action: 'comment',
  184. onShowReply: function onShowReply() {}
  185. };
  186. exports.default = (0, _Comment2.default)(CommentItem);
  187. //# sourceMappingURL=index.js.map