通用评论

index.js 13KB

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