通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 _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/popconfirm/style/css");
  13. require("antd/es/icon/style/css");
  14. require("antd/es/tooltip/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 _reactIntlUniversal = require("react-intl-universal");
  25. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  26. var _Comment = require("../../Comment");
  27. var _Comment2 = _interopRequireDefault(_Comment);
  28. var _CommentInput = require("../CommentInput");
  29. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  30. var _avatar = require("../../avatar");
  31. var _avatar2 = _interopRequireDefault(_avatar);
  32. var _helper = require("../../helper");
  33. var _constant = require("../../constant");
  34. require("./index.css");
  35. var _ImagePreviewer = require("../ImagePreviewer/ImagePreviewer");
  36. var _ImagePreviewer2 = _interopRequireDefault(_ImagePreviewer);
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  39. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  40. 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; }
  41. 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; }
  42. // import 'dayjs/locale/es';
  43. // dayjs.locale("zh-cn");
  44. _dayjs2.default.extend(_relativeTime2.default);
  45. var LOCALES = {
  46. "zh-CN": "zh-cn"
  47. };
  48. var CommentItem = function (_Component) {
  49. _inherits(CommentItem, _Component);
  50. function CommentItem(props) {
  51. _classCallCheck(this, CommentItem);
  52. var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));
  53. _this.state = {
  54. showInput: false,
  55. showPreviewer: false,
  56. previewerIndex: 0
  57. };
  58. _this.handleToggleInput = _this.handleToggleInput.bind(_this);
  59. _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
  60. _this.showPreviewer = _this.showPreviewer.bind(_this);
  61. _this.hidePreviewer = _this.hidePreviewer.bind(_this);
  62. return _this;
  63. }
  64. _createClass(CommentItem, [{
  65. key: "showPreviewer",
  66. value: function showPreviewer(index) {
  67. this.setState({
  68. showPreviewer: true,
  69. previewerIndex: index
  70. });
  71. }
  72. }, {
  73. key: "hidePreviewer",
  74. value: function hidePreviewer() {
  75. this.setState({
  76. showPreviewer: false
  77. });
  78. }
  79. }, {
  80. key: "handleToggleInput",
  81. value: function handleToggleInput() {
  82. this.setState({ showInput: !this.state.showInput });
  83. }
  84. }, {
  85. key: "renderTextWithReply",
  86. value: function renderTextWithReply(text, content) {
  87. var newText = text;
  88. var reply = content.reply;
  89. if (reply) {
  90. // newText = `${newText} //@<a href="/${reply.user_id}">${
  91. // reply.user_name
  92. // }</a> ${reply.content}`;
  93. newText = newText + " //@" + reply.user_name + " " + reply.content;
  94. // newText = (
  95. // <span>
  96. // {newText}
  97. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  98. // </span>
  99. // )
  100. if (reply.reply) {
  101. return this.renderTextWithReply(newText, reply);
  102. }
  103. }
  104. return newText;
  105. }
  106. }, {
  107. key: "render",
  108. value: function render() {
  109. var _this2 = this;
  110. var _props = this.props,
  111. commentId = _props.commentId,
  112. replyId = _props.replyId,
  113. content = _props.content,
  114. action = _props.action,
  115. showReply = _props.showReply,
  116. onShowReply = _props.onShowReply,
  117. app = _props.app;
  118. var locale = this.props.app.locale;
  119. var showInput = this.state.showInput;
  120. var newContent = content.content;
  121. var images = "";
  122. if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
  123. newContent = newContent.split(_constant.IMAGE_SPLIT);
  124. images = newContent.pop();
  125. newContent = newContent.join("");
  126. }
  127. var imageList = images.split(",");
  128. // 在3, 7前需要换行
  129. var needClear = imageList.length === 5 || imageList.length === 6 || imageList.length === 9;
  130. var imgs = [].concat(_toConsumableArray(imageList));
  131. if (needClear) {
  132. if (imgs.length > 6) {
  133. imgs.splice(3, 0, { type: "divider" });
  134. imgs.splice(7, 0, { type: "divider" });
  135. } else {
  136. imgs.splice(3, 0, { type: "divider" });
  137. }
  138. }
  139. var IconColor = content.favor_count > 0 ? "#71C135" : "#4a90e2";
  140. return _react2.default.createElement(
  141. "div",
  142. { className: "comment-item-box" },
  143. _react2.default.createElement(
  144. "div",
  145. { className: "comment-item-left" },
  146. _react2.default.createElement("div", {
  147. className: "comment-item-avatar",
  148. style: {
  149. backgroundImage: "url(" + (content.user_avatar || _avatar2.default) + ")"
  150. }
  151. })
  152. ),
  153. _react2.default.createElement(
  154. "div",
  155. { className: "comment-item-right" },
  156. _react2.default.createElement(
  157. "div",
  158. null,
  159. _react2.default.createElement(
  160. "strong",
  161. null,
  162. content.user_name || _reactIntlUniversal2.default.get("comment.tourist")
  163. ),
  164. _react2.default.createElement(
  165. "span",
  166. { style: { marginLeft: 10 } },
  167. _react2.default.createElement(
  168. _tooltip2.default,
  169. {
  170. placement: "top",
  171. title: (0, _dayjs2.default)(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")
  172. },
  173. LOCALES[locale] ? (0, _dayjs2.default)(content.created * 1000).locale(LOCALES[locale]).fromNow() : (0, _dayjs2.default)(content.created * 1000).fromNow()
  174. )
  175. )
  176. ),
  177. _react2.default.createElement("div", {
  178. className: "comment-item-content",
  179. dangerouslySetInnerHTML: {
  180. __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
  181. }
  182. }),
  183. // image为空时不渲染comment-item-image
  184. imageList.length > 0 && imageList[0] !== "" && _react2.default.createElement(
  185. "div",
  186. { className: "comment-item-image" },
  187. !this.state.showPreviewer && imgs.map(function (item, index) {
  188. if (item.type === "divider") {
  189. return _react2.default.createElement(
  190. "div",
  191. { className: "comment-item-image-wrapper", key: index },
  192. _react2.default.createElement("div", { className: "comment-img-divider" })
  193. );
  194. }
  195. return _react2.default.createElement(
  196. "div",
  197. {
  198. className: "comment-item-image-wrapper",
  199. key: index,
  200. onClick: function onClick() {
  201. var i = index;
  202. if (needClear) {
  203. if (index > 3) {
  204. i -= 1;
  205. }
  206. if (index > 7) {
  207. i -= 1;
  208. }
  209. }
  210. _this2.showPreviewer(i);
  211. }
  212. },
  213. _react2.default.createElement("div", {
  214. style: { backgroundImage: "url(" + item + ")" },
  215. className: "comment-img-thumbnail"
  216. })
  217. );
  218. }),
  219. this.state.showPreviewer && _react2.default.createElement(_ImagePreviewer2.default, {
  220. list: imageList,
  221. index: this.state.previewerIndex,
  222. onFold: this.hidePreviewer
  223. }),
  224. _react2.default.createElement("div", { className: "clearfix" })
  225. ),
  226. _react2.default.createElement(
  227. "div",
  228. { className: "comment-item-bottom" },
  229. content.reply_count ? _react2.default.createElement(
  230. "div",
  231. null,
  232. _react2.default.createElement(
  233. "a",
  234. { className: "comment-item-bottom-left", onClick: onShowReply },
  235. _reactIntlUniversal2.default.get("reply.totalReply", { total: content.reply_count }),
  236. showReply ? _react2.default.createElement(_icon2.default, { type: "up" }) : _react2.default.createElement(_icon2.default, { type: "down" })
  237. )
  238. ) : null,
  239. app.userId === content.user_id && _react2.default.createElement(
  240. _popconfirm2.default,
  241. {
  242. // title="确定要删除吗?"
  243. title: _reactIntlUniversal2.default.get("popConfirm.title"),
  244. onConfirm: function onConfirm() {
  245. if (replyId) {
  246. app.sDeleteReply(content.id, commentId);
  247. return;
  248. }
  249. app.sDeleteComment(content.id);
  250. },
  251. okText: _reactIntlUniversal2.default.get("popConfirm.ok"),
  252. cancelText: _reactIntlUniversal2.default.get("popConfirm.cancel")
  253. },
  254. _react2.default.createElement(
  255. "a",
  256. { className: "comment-item-bottom-right" },
  257. "\xA0 ",
  258. _reactIntlUniversal2.default.get("popConfirm.delete")
  259. )
  260. ),
  261. _react2.default.createElement(
  262. "a",
  263. {
  264. onClick: this.handleToggleInput,
  265. className: "comment-item-bottom-right"
  266. },
  267. "\xA0 ",
  268. _reactIntlUniversal2.default.get("comment.reply")
  269. ),
  270. _react2.default.createElement(
  271. "div",
  272. {
  273. className: "comment-item-bottom-right",
  274. onClick: function onClick() {
  275. if (replyId) {
  276. // 如果有 replyId,则说明是评论的回复
  277. app.sReplyFavor(content.id, commentId, content.favored);
  278. return;
  279. }
  280. app.sCommentFavor(content.id, content.favored);
  281. },
  282. style: { color: "" + IconColor }
  283. },
  284. _react2.default.createElement(_icon2.default, {
  285. type: "like-o",
  286. className: content.favored ? "comment-favor comment-favored" : "comment-favor",
  287. style: { color: "" + IconColor }
  288. }),
  289. "\xA0",
  290. content.favor_count
  291. )
  292. )
  293. ),
  294. showInput && _react2.default.createElement(_CommentInput2.default, {
  295. content: app.children,
  296. action: action,
  297. replyId: replyId,
  298. commentId: commentId,
  299. userId: content.user_id,
  300. callback: this.handleToggleInput
  301. })
  302. );
  303. }
  304. }]);
  305. return CommentItem;
  306. }(_react.Component);
  307. CommentItem.propTypes = {
  308. content: _propTypes2.default.object.isRequired,
  309. // comment 评论
  310. // reply 评论的回复
  311. // replyToReply 回复的回复
  312. action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"]),
  313. onShowReply: _propTypes2.default.func
  314. };
  315. CommentItem.defaultProps = {
  316. action: "comment",
  317. onShowReply: function onShowReply() {}
  318. };
  319. exports.default = (0, _Comment2.default)(CommentItem);
  320. //# sourceMappingURL=index.js.map