通用评论

index.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. var _constant = require('../../constant');
  32. require('./index.css');
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  35. 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; }
  36. 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; }
  37. _dayjs2.default.locale('zh-cn');
  38. _dayjs2.default.extend(_relativeTime2.default);
  39. var CommentItem = function (_Component) {
  40. _inherits(CommentItem, _Component);
  41. function CommentItem(props) {
  42. _classCallCheck(this, CommentItem);
  43. var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));
  44. _this.state = {
  45. showInput: false
  46. };
  47. _this.handleToggleInput = _this.handleToggleInput.bind(_this);
  48. _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
  49. return _this;
  50. }
  51. _createClass(CommentItem, [{
  52. key: 'handleToggleInput',
  53. value: function handleToggleInput() {
  54. this.setState({ showInput: !this.state.showInput });
  55. }
  56. }, {
  57. key: 'renderTextWithReply',
  58. value: function renderTextWithReply(text, content) {
  59. var newText = text;
  60. var reply = content.reply;
  61. if (reply) {
  62. // newText = `${newText} //@<a href="/${reply.user_id}">${
  63. // reply.user_name
  64. // }</a> ${reply.content}`;
  65. newText = newText + ' //@' + reply.user_name + ' ' + reply.content;
  66. // newText = (
  67. // <span>
  68. // {newText}
  69. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  70. // </span>
  71. // )
  72. if (reply.reply) {
  73. return this.renderTextWithReply(newText, reply);
  74. }
  75. }
  76. return newText;
  77. }
  78. }, {
  79. key: 'render',
  80. value: function render() {
  81. var _props = this.props,
  82. commentId = _props.commentId,
  83. replyId = _props.replyId,
  84. content = _props.content,
  85. action = _props.action,
  86. showReply = _props.showReply,
  87. onShowReply = _props.onShowReply,
  88. app = _props.app;
  89. var showInput = this.state.showInput;
  90. var newContent = content.content;
  91. var images = '';
  92. if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
  93. newContent = newContent.split(_constant.IMAGE_SPLIT);
  94. images = newContent.pop();
  95. newContent = newContent.join('');
  96. }
  97. return _react2.default.createElement(
  98. 'div',
  99. { className: 'comment-item-box' },
  100. _react2.default.createElement(
  101. 'div',
  102. { className: 'comment-item-left' },
  103. _react2.default.createElement(_avatar2.default, { src: content.user_avatar || _avatar4.default, size: 'large' })
  104. ),
  105. _react2.default.createElement(
  106. 'div',
  107. { className: 'comment-item-right' },
  108. _react2.default.createElement(
  109. 'div',
  110. null,
  111. _react2.default.createElement(
  112. 'a',
  113. { href: '/' + content.user_id },
  114. content.user_name || '暂无昵称'
  115. ),
  116. _react2.default.createElement(
  117. 'span',
  118. { style: { marginLeft: 10 } },
  119. _react2.default.createElement(
  120. _tooltip2.default,
  121. {
  122. placement: 'top',
  123. title: (0, _dayjs2.default)(content.created * 1000).format('YYYY-MM-DD HH:mm:ss')
  124. },
  125. (0, _dayjs2.default)(content.created * 1000).fromNow()
  126. )
  127. )
  128. ),
  129. _react2.default.createElement('div', {
  130. className: 'comment-item-content',
  131. dangerouslySetInnerHTML: {
  132. __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
  133. }
  134. }),
  135. _react2.default.createElement(
  136. 'div',
  137. { className: 'comment-item-image' },
  138. images.split(',').map(function (item, index) {
  139. return _react2.default.createElement('img', {
  140. key: index,
  141. src: item,
  142. alt: item,
  143. style: { maxWidth: '100%' }
  144. });
  145. })
  146. ),
  147. _react2.default.createElement(
  148. 'div',
  149. { className: 'comment-item-bottom' },
  150. content.reply_count ? _react2.default.createElement(
  151. 'div',
  152. null,
  153. _react2.default.createElement(
  154. 'a',
  155. { className: 'comment-item-bottom-left', onClick: onShowReply },
  156. content.reply_count,
  157. ' \u6761\u56DE\u590D',
  158. showReply ? _react2.default.createElement(_icon2.default, { type: 'up' }) : _react2.default.createElement(_icon2.default, { type: 'down' })
  159. )
  160. ) : null,
  161. _react2.default.createElement(
  162. 'a',
  163. {
  164. onClick: this.handleToggleInput,
  165. className: 'comment-item-bottom-right'
  166. },
  167. '\xA0 \u56DE\u590D'
  168. ),
  169. _react2.default.createElement(
  170. 'div',
  171. {
  172. className: 'comment-item-bottom-right',
  173. onClick: function onClick() {
  174. if (replyId) {
  175. // 如果有 replyId,则说明是评论的回复
  176. app.sReplyFavor(content.id, commentId, content.favored);
  177. return;
  178. }
  179. app.sCommentFavor(content.id, content.favored);
  180. }
  181. },
  182. _react2.default.createElement(_icon2.default, {
  183. type: 'like-o',
  184. className: content.favored ? 'comment-favor comment-favored' : 'comment-favor'
  185. }),
  186. '\xA0',
  187. content.favor_count
  188. )
  189. )
  190. ),
  191. showInput && _react2.default.createElement(_CommentInput2.default, {
  192. content: app.children,
  193. action: action,
  194. replyId: replyId,
  195. commentId: commentId,
  196. callback: this.handleToggleInput
  197. })
  198. );
  199. }
  200. }]);
  201. return CommentItem;
  202. }(_react.Component);
  203. CommentItem.propTypes = {
  204. content: _propTypes2.default.object.isRequired,
  205. // comment 评论
  206. // reply 评论的回复
  207. // replyToReply 回复的回复
  208. action: _propTypes2.default.oneOf(['comment', 'reply', 'replyToReply']),
  209. onShowReply: _propTypes2.default.func
  210. };
  211. CommentItem.defaultProps = {
  212. action: 'comment',
  213. onShowReply: function onShowReply() {}
  214. };
  215. exports.default = (0, _Comment2.default)(CommentItem);
  216. //# sourceMappingURL=index.js.map