通用评论

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _popconfirm = require("antd/es/popconfirm");
  6. var _popconfirm2 = _interopRequireDefault(_popconfirm);
  7. var _icon = require("antd/es/icon");
  8. var _icon2 = _interopRequireDefault(_icon);
  9. var _tooltip = require("antd/es/tooltip");
  10. var _tooltip2 = _interopRequireDefault(_tooltip);
  11. var _avatar = require("antd/es/avatar");
  12. var _avatar2 = _interopRequireDefault(_avatar);
  13. 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; }; }();
  14. require("antd/es/popconfirm/style/css");
  15. require("antd/es/icon/style/css");
  16. require("antd/es/tooltip/style/css");
  17. require("antd/es/avatar/style/css");
  18. var _react = require("react");
  19. var _react2 = _interopRequireDefault(_react);
  20. var _propTypes = require("prop-types");
  21. var _propTypes2 = _interopRequireDefault(_propTypes);
  22. var _dayjs = require("dayjs");
  23. var _dayjs2 = _interopRequireDefault(_dayjs);
  24. require("dayjs/locale/zh-cn");
  25. var _relativeTime = require("dayjs/plugin/relativeTime");
  26. var _relativeTime2 = _interopRequireDefault(_relativeTime);
  27. var _Comment = require("../../Comment");
  28. var _Comment2 = _interopRequireDefault(_Comment);
  29. var _CommentInput = require("../CommentInput");
  30. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  31. var _avatar3 = require("../../avatar");
  32. var _avatar4 = _interopRequireDefault(_avatar3);
  33. var _helper = require("../../helper");
  34. var _constant = require("../../constant");
  35. require("./index.css");
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  38. 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; }
  39. 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; }
  40. _dayjs2.default.locale("zh-cn");
  41. _dayjs2.default.extend(_relativeTime2.default);
  42. var CommentItem = function (_Component) {
  43. _inherits(CommentItem, _Component);
  44. function CommentItem(props) {
  45. _classCallCheck(this, CommentItem);
  46. var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));
  47. _this.state = {
  48. showInput: false
  49. };
  50. _this.handleToggleInput = _this.handleToggleInput.bind(_this);
  51. _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
  52. return _this;
  53. }
  54. _createClass(CommentItem, [{
  55. key: "handleToggleInput",
  56. value: function handleToggleInput() {
  57. this.setState({ showInput: !this.state.showInput });
  58. }
  59. }, {
  60. key: "renderTextWithReply",
  61. value: function renderTextWithReply(text, content) {
  62. var newText = text;
  63. var reply = content.reply;
  64. if (reply) {
  65. // newText = `${newText} //@<a href="/${reply.user_id}">${
  66. // reply.user_name
  67. // }</a> ${reply.content}`;
  68. newText = newText + " //@" + reply.user_name + " " + reply.content;
  69. // newText = (
  70. // <span>
  71. // {newText}
  72. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  73. // </span>
  74. // )
  75. if (reply.reply) {
  76. return this.renderTextWithReply(newText, reply);
  77. }
  78. }
  79. return newText;
  80. }
  81. }, {
  82. key: "render",
  83. value: function render() {
  84. var _props = this.props,
  85. commentId = _props.commentId,
  86. replyId = _props.replyId,
  87. content = _props.content,
  88. action = _props.action,
  89. showReply = _props.showReply,
  90. onShowReply = _props.onShowReply,
  91. app = _props.app;
  92. var showInput = this.state.showInput;
  93. var newContent = content.content;
  94. var images = "";
  95. if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
  96. newContent = newContent.split(_constant.IMAGE_SPLIT);
  97. images = newContent.pop();
  98. newContent = newContent.join("");
  99. }
  100. var imageList = images.split(",");
  101. return _react2.default.createElement(
  102. "div",
  103. { className: "comment-item-box" },
  104. _react2.default.createElement(
  105. "div",
  106. { className: "comment-item-left" },
  107. _react2.default.createElement(_avatar2.default, { src: content.user_avatar || _avatar4.default, size: "large" })
  108. ),
  109. _react2.default.createElement(
  110. "div",
  111. { className: "comment-item-right" },
  112. _react2.default.createElement(
  113. "div",
  114. null,
  115. _react2.default.createElement(
  116. "strong",
  117. null,
  118. content.user_name || "游客"
  119. ),
  120. _react2.default.createElement(
  121. "span",
  122. { style: { marginLeft: 10 } },
  123. _react2.default.createElement(
  124. _tooltip2.default,
  125. {
  126. placement: "top",
  127. title: (0, _dayjs2.default)(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")
  128. },
  129. (0, _dayjs2.default)(content.created * 1000).fromNow()
  130. )
  131. )
  132. ),
  133. _react2.default.createElement("div", {
  134. className: "comment-item-content",
  135. dangerouslySetInnerHTML: {
  136. __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
  137. }
  138. }),
  139. // image为空时不渲染comment-item-image
  140. imageList.length > 0 && imageList[0] !== "" && _react2.default.createElement(
  141. "div",
  142. { className: "comment-item-image" },
  143. imageList.map(function (item, index) {
  144. return _react2.default.createElement(
  145. "a",
  146. {
  147. href: item,
  148. target: "_blank",
  149. rel: "noopener noreferrer",
  150. key: index
  151. },
  152. _react2.default.createElement("img", { src: item, alt: item, className: "comment-img" })
  153. );
  154. })
  155. ),
  156. _react2.default.createElement(
  157. "div",
  158. { className: "comment-item-bottom" },
  159. content.reply_count ? _react2.default.createElement(
  160. "div",
  161. null,
  162. _react2.default.createElement(
  163. "a",
  164. { className: "comment-item-bottom-left", onClick: onShowReply },
  165. content.reply_count,
  166. " \u6761\u56DE\u590D",
  167. showReply ? _react2.default.createElement(_icon2.default, { type: "up" }) : _react2.default.createElement(_icon2.default, { type: "down" })
  168. )
  169. ) : null,
  170. app.userId === content.user_id && _react2.default.createElement(
  171. _popconfirm2.default,
  172. {
  173. title: "\u786E\u5B9A\u8981\u5220\u9664\u5417?",
  174. onConfirm: function onConfirm() {
  175. if (replyId) {
  176. app.sDeleteReply(content.id, commentId);
  177. return;
  178. }
  179. app.sDeleteComment(content.id);
  180. },
  181. okText: "\u786E\u5B9A",
  182. cancelText: "\u53D6\u6D88"
  183. },
  184. _react2.default.createElement(
  185. "a",
  186. { className: "comment-item-bottom-right" },
  187. "\xA0 \u5220\u9664"
  188. )
  189. ),
  190. _react2.default.createElement(
  191. "a",
  192. {
  193. onClick: this.handleToggleInput,
  194. className: "comment-item-bottom-right"
  195. },
  196. "\xA0 \u56DE\u590D"
  197. ),
  198. _react2.default.createElement(
  199. "div",
  200. {
  201. className: "comment-item-bottom-right",
  202. onClick: function onClick() {
  203. if (replyId) {
  204. // 如果有 replyId,则说明是评论的回复
  205. app.sReplyFavor(content.id, commentId, content.favored);
  206. return;
  207. }
  208. app.sCommentFavor(content.id, content.favored);
  209. }
  210. },
  211. _react2.default.createElement(_icon2.default, {
  212. type: "like-o",
  213. className: content.favored ? "comment-favor comment-favored" : "comment-favor"
  214. }),
  215. "\xA0",
  216. content.favor_count
  217. )
  218. )
  219. ),
  220. showInput && _react2.default.createElement(_CommentInput2.default, {
  221. content: app.children,
  222. action: action,
  223. replyId: replyId,
  224. commentId: commentId,
  225. callback: this.handleToggleInput
  226. })
  227. );
  228. }
  229. }]);
  230. return CommentItem;
  231. }(_react.Component);
  232. CommentItem.propTypes = {
  233. content: _propTypes2.default.object.isRequired,
  234. // comment 评论
  235. // reply 评论的回复
  236. // replyToReply 回复的回复
  237. action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"]),
  238. onShowReply: _propTypes2.default.func
  239. };
  240. CommentItem.defaultProps = {
  241. action: "comment",
  242. onShowReply: function onShowReply() {}
  243. };
  244. exports.default = (0, _Comment2.default)(CommentItem);
  245. //# sourceMappingURL=index.js.map