通用评论

index.js 16KB

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