通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 _popover = require("antd/es/popover");
  12. var _popover2 = _interopRequireDefault(_popover);
  13. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  14. 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; }; }();
  15. require("antd/es/popconfirm/style");
  16. require("antd/es/icon/style");
  17. require("antd/es/tooltip/style");
  18. require("antd/es/popover/style");
  19. var _react = require("react");
  20. var _react2 = _interopRequireDefault(_react);
  21. var _propTypes = require("prop-types");
  22. var _propTypes2 = _interopRequireDefault(_propTypes);
  23. var _dayjs = require("dayjs");
  24. var _dayjs2 = _interopRequireDefault(_dayjs);
  25. require("dayjs/locale/zh-cn");
  26. var _relativeTime = require("dayjs/plugin/relativeTime");
  27. var _relativeTime2 = _interopRequireDefault(_relativeTime);
  28. var _reactIntlUniversal = require("react-intl-universal");
  29. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  30. var _AvatarHoverCard = require("./AvatarHoverCard");
  31. var _AvatarHoverCard2 = _interopRequireDefault(_AvatarHoverCard);
  32. var _Comment = require("../../Comment");
  33. var _Comment2 = _interopRequireDefault(_Comment);
  34. var _CommentInput = require("../CommentInput");
  35. var _CommentInput2 = _interopRequireDefault(_CommentInput);
  36. var _avatar = require("../../avatar");
  37. var _avatar2 = _interopRequireDefault(_avatar);
  38. var _helper = require("../../helper");
  39. var _constant = require("../../constant");
  40. require("./index.css");
  41. var _ImagePreviewer = require("../ImagePreviewer/ImagePreviewer");
  42. var _ImagePreviewer2 = _interopRequireDefault(_ImagePreviewer);
  43. var _AudioPlayer = require("../AudioPlayer");
  44. var _AudioPlayer2 = _interopRequireDefault(_AudioPlayer);
  45. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  46. 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); } }
  47. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  48. 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; }
  49. 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; }
  50. // import 'dayjs/locale/es';
  51. // dayjs.locale("zh-cn");
  52. _dayjs2.default.extend(_relativeTime2.default);
  53. var LOCALES = {
  54. "zh-CN": "zh-cn"
  55. };
  56. var CommentItem = function (_Component) {
  57. _inherits(CommentItem, _Component);
  58. function CommentItem(props) {
  59. _classCallCheck(this, CommentItem);
  60. var _this = _possibleConstructorReturn(this, (CommentItem.__proto__ || Object.getPrototypeOf(CommentItem)).call(this, props));
  61. _this.state = {
  62. showInput: false,
  63. showPreviewer: false,
  64. previewerIndex: 0,
  65. popoverVisible: false
  66. };
  67. _this.handleToggleInput = _this.handleToggleInput.bind(_this);
  68. _this.renderTextWithReply = _this.renderTextWithReply.bind(_this);
  69. _this.showPreviewer = _this.showPreviewer.bind(_this);
  70. _this.hidePreviewer = _this.hidePreviewer.bind(_this);
  71. _this.handleVisibleChange = _this.handleVisibleChange.bind(_this);
  72. _this.handleUserAvaClick = _this.handleUserAvaClick.bind(_this);
  73. return _this;
  74. }
  75. _createClass(CommentItem, [{
  76. key: "showPreviewer",
  77. value: function showPreviewer(index) {
  78. this.setState({
  79. showPreviewer: true,
  80. previewerIndex: index
  81. });
  82. }
  83. }, {
  84. key: "hidePreviewer",
  85. value: function hidePreviewer() {
  86. this.setState({
  87. showPreviewer: false
  88. });
  89. }
  90. }, {
  91. key: "handleToggleInput",
  92. value: function handleToggleInput() {
  93. this.setState({ showInput: !this.state.showInput });
  94. }
  95. }, {
  96. key: "handleVisibleChange",
  97. value: function handleVisibleChange(visible) {
  98. this.setState({
  99. popoverVisible: visible
  100. });
  101. }
  102. }, {
  103. key: "handleUserAvaClick",
  104. value: function handleUserAvaClick() {
  105. var user_id = this.props.user_id;
  106. var userAvaClick = this.props.app.userAvaClick;
  107. if (userAvaClick) {
  108. userAvaClick(user_id);
  109. }
  110. }
  111. }, {
  112. key: "renderTextWithReply",
  113. value: function renderTextWithReply(text, content) {
  114. var newText = text;
  115. var reply = content.reply;
  116. if (reply) {
  117. // newText = `${newText} //@<a href="/${reply.user_id}">${
  118. // reply.user_name
  119. // }</a> ${reply.content}`;
  120. newText = newText + " //@" + reply.user_name + " " + reply.content;
  121. // newText = (
  122. // <span>
  123. // {newText}
  124. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  125. // </span>
  126. // )
  127. if (reply.reply) {
  128. return this.renderTextWithReply(newText, reply);
  129. }
  130. }
  131. return newText;
  132. }
  133. }, {
  134. key: "render",
  135. value: function render() {
  136. var _this2 = this;
  137. var _props = this.props,
  138. commentId = _props.commentId,
  139. replyId = _props.replyId,
  140. content = _props.content,
  141. action = _props.action,
  142. showReply = _props.showReply,
  143. onShowReply = _props.onShowReply,
  144. app = _props.app,
  145. user_id = _props.user_id,
  146. page = _props.page;
  147. var medias = content.medias,
  148. isSpeak = content.is_speak;
  149. var _props$app = this.props.app,
  150. locale = _props$app.locale,
  151. showHoverCard = _props$app.showHoverCard,
  152. showEdit = _props$app.showEdit;
  153. var showInput = this.state.showInput;
  154. var newContent = content.content;
  155. var images = "";
  156. if (newContent.indexOf(_constant.IMAGE_SPLIT) !== -1) {
  157. newContent = newContent.split(_constant.IMAGE_SPLIT);
  158. images = newContent.pop();
  159. newContent = newContent.join("");
  160. }
  161. var imageList = images.split(",");
  162. // 在3, 7前需要换行
  163. var needClear = imageList.length === 5 || imageList.length === 6 || imageList.length === 9;
  164. var imgs = [].concat(_toConsumableArray(imageList));
  165. if (needClear) {
  166. if (imgs.length > 6) {
  167. imgs.splice(3, 0, { type: "divider" });
  168. imgs.splice(7, 0, { type: "divider" });
  169. } else {
  170. imgs.splice(3, 0, { type: "divider" });
  171. }
  172. }
  173. var IconColor = content.favor_count > 0 ? "#71C135" : "#4a90e2";
  174. var showDivider = content.reply_count || showEdit && app.userId === content.user_id || app.userId === content.user_id;
  175. return _react2.default.createElement(
  176. "div",
  177. { className: "comment-item-box" },
  178. _react2.default.createElement(
  179. "div",
  180. { className: "comment-item-left" },
  181. showHoverCard ? _react2.default.createElement(
  182. _popover2.default,
  183. {
  184. content: _react2.default.createElement(_AvatarHoverCard2.default, _extends({}, this.props.app, {
  185. user_id: user_id,
  186. handleVisibleChange: this.handleVisibleChange,
  187. image: content.user_avatar || _avatar2.default
  188. }))
  189. // placement={this.props.placement}
  190. // trigger="click"
  191. , visible: this.state.popoverVisible,
  192. onVisibleChange: this.handleVisibleChange,
  193. overlayClassName: "avatar-hover-card-overlay"
  194. },
  195. _react2.default.createElement("div", {
  196. className: "comment-item-avatar",
  197. style: {
  198. backgroundImage: "url(" + (content.user_avatar || _avatar2.default) + ")"
  199. },
  200. onClick: this.handleUserAvaClick
  201. })
  202. ) : _react2.default.createElement("div", {
  203. className: "comment-item-avatar",
  204. style: {
  205. backgroundImage: "url(" + (content.user_avatar || _avatar2.default) + ")"
  206. },
  207. onClick: this.handleUserAvaClick
  208. })
  209. ),
  210. _react2.default.createElement(
  211. "div",
  212. { className: "comment-item-right" },
  213. _react2.default.createElement(
  214. "div",
  215. null,
  216. _react2.default.createElement(
  217. "strong",
  218. {
  219. onClick: this.handleUserAvaClick,
  220. className: "comment-item-name",
  221. style: { cursor: "pointer" }
  222. },
  223. content.user_name || _reactIntlUniversal2.default.get("comment.tourist")
  224. ),
  225. _react2.default.createElement(
  226. "span",
  227. { className: "comment-item-date", style: { marginLeft: 10 } },
  228. _react2.default.createElement(
  229. _tooltip2.default,
  230. {
  231. placement: "top",
  232. title: (0, _dayjs2.default)(content.created * 1000).format("YYYY-MM-DD HH:mm:ss")
  233. },
  234. LOCALES[locale] ? (0, _dayjs2.default)(content.created * 1000).locale(LOCALES[locale]).fromNow() : (0, _dayjs2.default)(content.created * 1000).fromNow()
  235. )
  236. )
  237. ),
  238. _react2.default.createElement("div", {
  239. className: "comment-item-content",
  240. dangerouslySetInnerHTML: {
  241. __html: (0, _helper.renderContent)(this.renderTextWithReply(newContent, content))
  242. }
  243. }),
  244. isSpeak && _react2.default.createElement(
  245. "div",
  246. { className: "comment-item-speak" },
  247. _react2.default.createElement(
  248. "span",
  249. { className: "comment-item-speak-message" },
  250. "[\u8DDF\u8BFB\u6D88\u606F]"
  251. ),
  252. _react2.default.createElement(
  253. "div",
  254. { className: "comment-item-speak-audio-wrapper" },
  255. _react2.default.createElement(_AudioPlayer2.default, { src: medias && medias[0] && medias[0].url })
  256. )
  257. ),
  258. // image为空时不渲染comment-item-image
  259. imageList.length > 0 && imageList[0] !== "" && _react2.default.createElement(
  260. "div",
  261. { className: "comment-item-image" },
  262. !this.state.showPreviewer && imgs.map(function (item, index) {
  263. if (item.type === "divider") {
  264. return _react2.default.createElement(
  265. "div",
  266. { className: "comment-item-image-wrapper", key: index },
  267. _react2.default.createElement("div", { className: "comment-img-divider" })
  268. );
  269. }
  270. return _react2.default.createElement(
  271. "div",
  272. {
  273. className: "comment-item-image-wrapper",
  274. key: index,
  275. onClick: function onClick() {
  276. var i = index;
  277. if (needClear) {
  278. if (index > 3) {
  279. i -= 1;
  280. }
  281. if (index > 7) {
  282. i -= 1;
  283. }
  284. }
  285. _this2.showPreviewer(i);
  286. }
  287. },
  288. _react2.default.createElement("div", {
  289. style: { backgroundImage: "url(" + item + ")" },
  290. className: "comment-img-thumbnail"
  291. })
  292. );
  293. }),
  294. this.state.showPreviewer && _react2.default.createElement(_ImagePreviewer2.default, {
  295. list: imageList,
  296. index: this.state.previewerIndex,
  297. onFold: this.hidePreviewer
  298. }),
  299. _react2.default.createElement("div", { className: "clearfix" })
  300. ),
  301. _react2.default.createElement(
  302. "div",
  303. { className: "comment-item-bottom" },
  304. content.reply_count ? _react2.default.createElement(
  305. "div",
  306. null,
  307. _react2.default.createElement(
  308. "a",
  309. { className: "comment-item-bottom-left", onClick: onShowReply },
  310. _reactIntlUniversal2.default.get("reply.totalReply", { total: content.reply_count }),
  311. showReply ? _react2.default.createElement(_icon2.default, { type: "up" }) : _react2.default.createElement(_icon2.default, { type: "down" })
  312. )
  313. ) : null,
  314. showEdit && !isSpeak && app.userId === content.user_id && _react2.default.createElement("i", {
  315. className: "comment-item-edit",
  316. onClick: function onClick() {
  317. return _this2.props.app.handleEdit({
  318. action: action,
  319. replyId: replyId,
  320. commentId: commentId,
  321. userId: content.user_id,
  322. content: content,
  323. replyPage: page
  324. });
  325. }
  326. }),
  327. app.userId === content.user_id && _react2.default.createElement(
  328. _popconfirm2.default,
  329. {
  330. // title="确定要删除吗?"
  331. title: _reactIntlUniversal2.default.get("popConfirm.title"),
  332. onConfirm: function onConfirm() {
  333. if (replyId) {
  334. app.sDeleteReply(content.id, commentId);
  335. return;
  336. }
  337. app.sDeleteComment(content.id);
  338. },
  339. okText: _reactIntlUniversal2.default.get("popConfirm.ok"),
  340. cancelText: _reactIntlUniversal2.default.get("popConfirm.cancel")
  341. },
  342. _react2.default.createElement("i", { className: "comment-item-delete" })
  343. ),
  344. showDivider && _react2.default.createElement("span", { className: "comment-item-divider" }),
  345. _react2.default.createElement(
  346. "div",
  347. {
  348. className: "comment-item-bottom-right",
  349. onClick: function onClick() {
  350. if (replyId) {
  351. // 如果有 replyId,则说明是评论的回复
  352. app.sReplyFavor(content.id, commentId, content.favored);
  353. return;
  354. }
  355. app.sCommentFavor(content.id, content.favored);
  356. },
  357. style: { color: "" + IconColor }
  358. },
  359. _react2.default.createElement("i", {
  360. className: content.favored ? "comment-item-like" : "comment-item-unlike"
  361. })
  362. ),
  363. _react2.default.createElement(
  364. "span",
  365. null,
  366. "\xA0",
  367. content.favor_count
  368. ),
  369. _react2.default.createElement(
  370. "div",
  371. {
  372. onClick: this.handleToggleInput,
  373. className: "comment-item-reply"
  374. },
  375. "\xA0 ",
  376. _reactIntlUniversal2.default.get("comment.reply")
  377. )
  378. )
  379. ),
  380. showInput && _react2.default.createElement(_CommentInput2.default, {
  381. content: app.children,
  382. action: action,
  383. replyId: replyId,
  384. commentId: commentId,
  385. userId: content.user_id,
  386. callback: this.handleToggleInput
  387. })
  388. );
  389. }
  390. }]);
  391. return CommentItem;
  392. }(_react.Component);
  393. CommentItem.propTypes = {
  394. content: _propTypes2.default.object.isRequired,
  395. // comment 评论
  396. // reply 评论的回复
  397. // replyToReply 回复的回复
  398. action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"]),
  399. onShowReply: _propTypes2.default.func,
  400. showEdit: _propTypes2.default.bool
  401. };
  402. CommentItem.defaultProps = {
  403. action: "comment",
  404. onShowReply: function onShowReply() {},
  405. showEdit: false
  406. };
  407. exports.default = (0, _Comment2.default)(CommentItem);
  408. //# sourceMappingURL=index.js.map