通用评论

index.js 13KB

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