通用评论

index.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: 'box' },
  83. _react2.default.createElement(
  84. 'div',
  85. { className: 'left' },
  86. _react2.default.createElement(_avatar2.default, { src: content.user_avatar || _avatar4.default, size: 'large' })
  87. ),
  88. _react2.default.createElement(
  89. 'div',
  90. { className: 'right' },
  91. _react2.default.createElement(
  92. 'div',
  93. { className: 'name' },
  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: 'content',
  114. dangerouslySetInnerHTML: {
  115. __html: (0, _helper.renderContent)(this.renderTextWithReply(content.content, content))
  116. }
  117. }),
  118. _react2.default.createElement(
  119. 'div',
  120. { className: 'bottom' },
  121. content.reply_count ? _react2.default.createElement(
  122. 'div',
  123. null,
  124. _react2.default.createElement(
  125. 'a',
  126. {
  127. className: 'itemLeft',
  128. onClick: onShowReply,
  129. style: { userSelect: 'none' }
  130. },
  131. content.reply_count,
  132. ' \u6761\u56DE\u590D',
  133. showReply ? _react2.default.createElement(_icon2.default, { type: 'up' }) : _react2.default.createElement(_icon2.default, { type: 'down' })
  134. )
  135. ) : null,
  136. _react2.default.createElement(
  137. 'a',
  138. { onClick: this.handleToggleInput, className: 'itemRight' },
  139. '\xA0 \u56DE\u590D'
  140. ),
  141. _react2.default.createElement(
  142. 'div',
  143. {
  144. className: 'itemRight',
  145. style: { cursor: 'pointer' },
  146. onClick: function onClick() {
  147. if (replyId) {
  148. // 如果有 replyId,则说明是评论的回复
  149. app.sReplyFavor(content.id, commentId, content.favored);
  150. return;
  151. }
  152. app.sCommentFavor(content.id, content.favored);
  153. }
  154. },
  155. _react2.default.createElement(_icon2.default, {
  156. type: 'like-o',
  157. className: content.favored ? 'favored' : ''
  158. }),
  159. '\xA0',
  160. content.favor_count
  161. )
  162. )
  163. ),
  164. showInput && _react2.default.createElement(_CommentInput2.default, {
  165. content: app.children,
  166. action: action,
  167. replyId: replyId,
  168. commentId: commentId,
  169. callback: this.handleToggleInput
  170. })
  171. );
  172. }
  173. }]);
  174. return CommentItem;
  175. }(_react.Component);
  176. CommentItem.propTypes = {
  177. content: _propTypes2.default.object.isRequired,
  178. // comment 评论
  179. // reply 评论的回复
  180. // replyToReply 回复的回复
  181. action: _propTypes2.default.oneOf(['comment', 'reply', 'replyToReply']),
  182. onShowReply: _propTypes2.default.func
  183. };
  184. CommentItem.defaultProps = {
  185. action: 'comment',
  186. onShowReply: function onShowReply() {}
  187. };
  188. exports.default = (0, _Comment2.default)(CommentItem);
  189. //# sourceMappingURL=index.js.map