通用评论

index.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Avatar, Icon, Tooltip, Popconfirm } from "antd";
  4. import dayjs from "dayjs";
  5. import "dayjs/locale/zh-cn";
  6. // import 'dayjs/locale/es';
  7. import relativeTime from "dayjs/plugin/relativeTime";
  8. import intl from "react-intl-universal";
  9. import Comment from "../../Comment";
  10. import CommentInput from "../CommentInput";
  11. import avatar from "../../avatar";
  12. import { renderContent } from "../../helper";
  13. import { IMAGE_SPLIT } from "../../constant";
  14. import "./index.css";
  15. import ImagePreviewer from "../ImagePreviewer/ImagePreviewer";
  16. // dayjs.locale("zh-cn");
  17. dayjs.extend(relativeTime);
  18. const LOCALES = {
  19. "zh-CN": "zh-cn"
  20. };
  21. class CommentItem extends Component {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. showInput: false,
  26. showPreviewer: false,
  27. previewerIndex: 0
  28. };
  29. this.handleToggleInput = this.handleToggleInput.bind(this);
  30. this.renderTextWithReply = this.renderTextWithReply.bind(this);
  31. this.showPreviewer = this.showPreviewer.bind(this);
  32. this.hidePreviewer = this.hidePreviewer.bind(this);
  33. }
  34. showPreviewer(index) {
  35. this.setState({
  36. showPreviewer: true,
  37. previewerIndex: index
  38. });
  39. }
  40. hidePreviewer() {
  41. this.setState({
  42. showPreviewer: false
  43. });
  44. }
  45. handleToggleInput() {
  46. this.setState({ showInput: !this.state.showInput });
  47. }
  48. renderTextWithReply(text, content) {
  49. let newText = text;
  50. const { reply } = content;
  51. if (reply) {
  52. // newText = `${newText} //@<a href="/${reply.user_id}">${
  53. // reply.user_name
  54. // }</a> ${reply.content}`;
  55. newText = `${newText} //@${reply.user_name} ${reply.content}`;
  56. // newText = (
  57. // <span>
  58. // {newText}
  59. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  60. // </span>
  61. // )
  62. if (reply.reply) {
  63. return this.renderTextWithReply(newText, reply);
  64. }
  65. }
  66. return newText;
  67. }
  68. render() {
  69. const {
  70. commentId,
  71. replyId,
  72. content,
  73. action,
  74. showReply,
  75. onShowReply,
  76. isMobile,
  77. app
  78. } = this.props;
  79. const { locale } = this.props.app;
  80. const { showInput } = this.state;
  81. let newContent = content.content;
  82. let images = "";
  83. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  84. newContent = newContent.split(IMAGE_SPLIT);
  85. images = newContent.pop();
  86. newContent = newContent.join("");
  87. }
  88. const imageList = images.split(",");
  89. // 在3, 7前需要换行
  90. const needClear =
  91. imageList.length === 5 ||
  92. imageList.length === 6 ||
  93. imageList.length === 9;
  94. const imgs = [...imageList];
  95. if (needClear) {
  96. if (imgs.length > 6) {
  97. imgs.splice(3, 0, { type: "divider" });
  98. imgs.splice(7, 0, { type: "divider" });
  99. } else {
  100. imgs.splice(3, 0, { type: "divider" });
  101. }
  102. }
  103. if (isMobile) {
  104. return (
  105. <div className="comment-item-box">
  106. <div className="comment-item-left">
  107. <Avatar src={content.user_avatar || avatar} size="large" />
  108. </div>
  109. <div className="comment-item-middle">
  110. <div>
  111. <strong>{content.user_name || "游客"}</strong>
  112. </div>
  113. <div
  114. className="comment-item-content"
  115. dangerouslySetInnerHTML={{
  116. __html: renderContent(
  117. this.renderTextWithReply(newContent, content)
  118. )
  119. }}
  120. />
  121. {// image为空时不渲染comment-item-image
  122. imageList.length > 0 && imageList[0] !== "" && (
  123. <div className="comment-item-image">
  124. {!this.state.showPreviewer &&
  125. imgs.map((item, index) => {
  126. if (item.type === "divider") {
  127. return (
  128. <div className="comment-item-image-wrapper" key={index}>
  129. <div className="comment-img-divider" />
  130. {/* <img src={item} alt={item} className="comment-img" /> */}
  131. </div>
  132. );
  133. }
  134. return (
  135. <div className="comment-item-image-wrapper" key={index}>
  136. <div className="comment-img-divider" />
  137. {/* <img src={item} alt={item} className="comment-img" /> */}
  138. </div>
  139. );
  140. })}
  141. {this.state.showPreviewer && (
  142. <ImagePreviewer
  143. list={imageList}
  144. index={this.state.previewerIndex}
  145. onFold={this.hidePreviewer}
  146. />
  147. )}
  148. <div className="clearfix" />
  149. </div>
  150. )}
  151. <div>
  152. <Tooltip
  153. placement="top"
  154. title={dayjs(content.created * 1000).format(
  155. "YYYY-MM-DD HH:mm:ss"
  156. )}
  157. >
  158. {dayjs(content.created * 1000).fromNow()}
  159. </Tooltip>
  160. </div>
  161. <div className="comment-item-bottom">
  162. {content.reply_count ? (
  163. <div>
  164. <a className="comment-item-bottom-left" onClick={onShowReply}>
  165. {content.reply_count} 条回复
  166. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  167. </a>
  168. </div>
  169. ) : null}
  170. {app.userId === content.user_id && (
  171. <Popconfirm
  172. title="确定要删除吗?"
  173. onConfirm={() => {
  174. if (replyId) {
  175. app.sDeleteReply(content.id, commentId);
  176. return;
  177. }
  178. app.sDeleteComment(content.id);
  179. }}
  180. okText="确定"
  181. cancelText="取消"
  182. >
  183. <a className="comment-item-bottom-right">&nbsp; 删除</a>
  184. </Popconfirm>
  185. )}
  186. </div>
  187. </div>
  188. <div className="comment-item-right">
  189. <div
  190. className="comment-item-top-right"
  191. onClick={() => {
  192. if (replyId) {
  193. // 如果有 replyId,则说明是评论的回复
  194. app.sReplyFavor(content.id, commentId, content.favored);
  195. return;
  196. }
  197. app.sCommentFavor(content.id, content.favored);
  198. }}
  199. >
  200. <div className="text-center">
  201. <Icon
  202. type="heart"
  203. className={
  204. content.favored
  205. ? "comment-favor comment-favored"
  206. : "comment-favor"
  207. }
  208. />
  209. </div>
  210. <div className="text-center">{content.favor_count}</div>
  211. </div>
  212. </div>
  213. {showInput && (
  214. <CommentInput
  215. content={app.children}
  216. action={action}
  217. replyId={replyId}
  218. commentId={commentId}
  219. callback={this.handleToggleInput}
  220. />
  221. )}
  222. </div>
  223. );
  224. }
  225. return (
  226. <div className="comment-item-box">
  227. <div className="comment-item-left">
  228. <Avatar src={content.user_avatar || avatar} size="large" />
  229. </div>
  230. <div className="comment-item-right">
  231. <div>
  232. {/* <a href={`/${content.user_id}`}>
  233. {content.user_name || "暂无昵称"}
  234. </a> */}
  235. <strong>{content.user_name || intl.get("comment.tourist")}</strong>
  236. <span style={{ marginLeft: 10 }}>
  237. <Tooltip
  238. placement="top"
  239. title={dayjs(content.created * 1000).format(
  240. "YYYY-MM-DD HH:mm:ss"
  241. )}
  242. >
  243. {LOCALES[locale]
  244. ? dayjs(content.created * 1000)
  245. .locale(LOCALES[locale])
  246. .fromNow()
  247. : dayjs(content.created * 1000).fromNow()}
  248. </Tooltip>
  249. </span>
  250. </div>
  251. <div
  252. className="comment-item-content"
  253. dangerouslySetInnerHTML={{
  254. __html: renderContent(
  255. this.renderTextWithReply(newContent, content)
  256. )
  257. }}
  258. />
  259. {// image为空时不渲染comment-item-image
  260. imageList.length > 0 && imageList[0] !== "" && (
  261. <div className="comment-item-image">
  262. {!this.state.showPreviewer &&
  263. imgs.map((item, index) => {
  264. if (item.type === "divider") {
  265. return (
  266. <div className="comment-item-image-wrapper" key={index}>
  267. <div className="comment-img-divider" />
  268. {/* <img src={item} alt={item} className="comment-img" /> */}
  269. </div>
  270. );
  271. }
  272. return (
  273. <div
  274. className="comment-item-image-wrapper"
  275. key={index}
  276. onClick={() => {
  277. let i = index;
  278. if (needClear) {
  279. if (index > 3) {
  280. i -= 1;
  281. }
  282. if (index > 7) {
  283. i -= 1;
  284. }
  285. }
  286. this.showPreviewer(i);
  287. }}
  288. >
  289. <div
  290. style={{ backgroundImage: `url(${item})` }}
  291. className="comment-img-thumbnail"
  292. />
  293. {/* <img src={item} alt={item} className="comment-img" /> */}
  294. </div>
  295. );
  296. })}
  297. {this.state.showPreviewer && (
  298. <ImagePreviewer
  299. list={imageList}
  300. index={this.state.previewerIndex}
  301. onFold={this.hidePreviewer}
  302. />
  303. )}
  304. <div className="clearfix" />
  305. </div>
  306. )}
  307. <div className="comment-item-bottom">
  308. {content.reply_count ? (
  309. <div>
  310. <a className="comment-item-bottom-left" onClick={onShowReply}>
  311. {/* {content.reply_count} 条回复 */}
  312. {intl.get("reply.totalReply", { total: content.reply_count })}
  313. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  314. </a>
  315. </div>
  316. ) : null}
  317. {app.userId === content.user_id && (
  318. <Popconfirm
  319. // title="确定要删除吗?"
  320. title={intl.get("popConfirm.title")}
  321. onConfirm={() => {
  322. if (replyId) {
  323. app.sDeleteReply(content.id, commentId);
  324. return;
  325. }
  326. app.sDeleteComment(content.id);
  327. }}
  328. okText={intl.get("popConfirm.ok")}
  329. cancelText={intl.get("popConfirm.cancel")}
  330. >
  331. <a className="comment-item-bottom-right">
  332. &nbsp; {intl.get("popConfirm.delete")}
  333. </a>
  334. </Popconfirm>
  335. )}
  336. <a
  337. onClick={this.handleToggleInput}
  338. className="comment-item-bottom-right"
  339. >
  340. &nbsp; {intl.get("comment.reply")}
  341. </a>
  342. <div
  343. className="comment-item-bottom-right"
  344. onClick={() => {
  345. if (replyId) {
  346. // 如果有 replyId,则说明是评论的回复
  347. app.sReplyFavor(content.id, commentId, content.favored);
  348. return;
  349. }
  350. app.sCommentFavor(content.id, content.favored);
  351. }}
  352. >
  353. <Icon
  354. type="like-o"
  355. className={
  356. content.favored
  357. ? "comment-favor comment-favored"
  358. : "comment-favor"
  359. }
  360. />
  361. &nbsp;{content.favor_count}
  362. </div>
  363. </div>
  364. </div>
  365. {showInput && (
  366. <CommentInput
  367. content={app.children}
  368. action={action}
  369. replyId={replyId}
  370. commentId={commentId}
  371. userId={content.user_id}
  372. callback={this.handleToggleInput}
  373. />
  374. )}
  375. </div>
  376. );
  377. }
  378. }
  379. CommentItem.propTypes = {
  380. content: PropTypes.object.isRequired,
  381. // comment 评论
  382. // reply 评论的回复
  383. // replyToReply 回复的回复
  384. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  385. onShowReply: PropTypes.func
  386. };
  387. CommentItem.defaultProps = {
  388. action: "comment",
  389. onShowReply: () => {}
  390. };
  391. export default Comment(CommentItem);