通用评论

index.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. app
  77. } = this.props;
  78. const { locale } = this.props.app;
  79. const { showInput } = this.state;
  80. let newContent = content.content;
  81. let images = "";
  82. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  83. newContent = newContent.split(IMAGE_SPLIT);
  84. images = newContent.pop();
  85. newContent = newContent.join("");
  86. }
  87. const imageList = images.split(",");
  88. // 在3, 7前需要换行
  89. const needClear =
  90. imageList.length === 5 ||
  91. imageList.length === 6 ||
  92. imageList.length === 9;
  93. const imgs = [...imageList];
  94. if (needClear) {
  95. if (imgs.length > 6) {
  96. imgs.splice(3, 0, { type: "divider" });
  97. imgs.splice(7, 0, { type: "divider" });
  98. } else {
  99. imgs.splice(3, 0, { type: "divider" });
  100. }
  101. }
  102. const IconColor = content.favor_count > 0 ? "#71C135" : "#4a90e2";
  103. return (
  104. <div className="comment-item-box">
  105. <div className="comment-item-left">
  106. {/* <Avatar src={content.user_avatar || avatar} size="large" /> */}
  107. <div
  108. className="comment-item-avatar"
  109. style={{
  110. backgroundImage: `url(${content.user_avatar || avatar})`
  111. }}
  112. />
  113. </div>
  114. <div className="comment-item-right">
  115. <div>
  116. {/* <a href={`/${content.user_id}`}>
  117. {content.user_name || "暂无昵称"}
  118. </a> */}
  119. <strong>{content.user_name || intl.get("comment.tourist")}</strong>
  120. <span style={{ marginLeft: 10 }}>
  121. <Tooltip
  122. placement="top"
  123. title={dayjs(content.created * 1000).format(
  124. "YYYY-MM-DD HH:mm:ss"
  125. )}
  126. >
  127. {LOCALES[locale]
  128. ? dayjs(content.created * 1000)
  129. .locale(LOCALES[locale])
  130. .fromNow()
  131. : dayjs(content.created * 1000).fromNow()}
  132. </Tooltip>
  133. </span>
  134. </div>
  135. <div
  136. className="comment-item-content"
  137. dangerouslySetInnerHTML={{
  138. __html: renderContent(
  139. this.renderTextWithReply(newContent, content)
  140. )
  141. }}
  142. />
  143. {// image为空时不渲染comment-item-image
  144. imageList.length > 0 && imageList[0] !== "" && (
  145. <div className="comment-item-image">
  146. {!this.state.showPreviewer &&
  147. imgs.map((item, index) => {
  148. if (item.type === "divider") {
  149. return (
  150. <div className="comment-item-image-wrapper" key={index}>
  151. <div className="comment-img-divider" />
  152. {/* <img src={item} alt={item} className="comment-img" /> */}
  153. </div>
  154. );
  155. }
  156. return (
  157. <div
  158. className="comment-item-image-wrapper"
  159. key={index}
  160. onClick={() => {
  161. let i = index;
  162. if (needClear) {
  163. if (index > 3) {
  164. i -= 1;
  165. }
  166. if (index > 7) {
  167. i -= 1;
  168. }
  169. }
  170. this.showPreviewer(i);
  171. }}
  172. >
  173. <div
  174. style={{ backgroundImage: `url(${item})` }}
  175. className="comment-img-thumbnail"
  176. />
  177. {/* <img src={item} alt={item} className="comment-img" /> */}
  178. </div>
  179. );
  180. })}
  181. {this.state.showPreviewer && (
  182. <ImagePreviewer
  183. list={imageList}
  184. index={this.state.previewerIndex}
  185. onFold={this.hidePreviewer}
  186. />
  187. )}
  188. <div className="clearfix" />
  189. </div>
  190. )}
  191. <div className="comment-item-bottom">
  192. {content.reply_count ? (
  193. <div>
  194. <a className="comment-item-bottom-left" onClick={onShowReply}>
  195. {/* {content.reply_count} 条回复 */}
  196. {intl.get("reply.totalReply", { total: content.reply_count })}
  197. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  198. </a>
  199. </div>
  200. ) : null}
  201. {app.userId === content.user_id && (
  202. <Popconfirm
  203. // title="确定要删除吗?"
  204. title={intl.get("popConfirm.title")}
  205. onConfirm={() => {
  206. if (replyId) {
  207. app.sDeleteReply(content.id, commentId);
  208. return;
  209. }
  210. app.sDeleteComment(content.id);
  211. }}
  212. okText={intl.get("popConfirm.ok")}
  213. cancelText={intl.get("popConfirm.cancel")}
  214. >
  215. <a className="comment-item-bottom-right">
  216. &nbsp; {intl.get("popConfirm.delete")}
  217. </a>
  218. </Popconfirm>
  219. )}
  220. <a
  221. onClick={this.handleToggleInput}
  222. className="comment-item-bottom-right"
  223. >
  224. &nbsp; {intl.get("comment.reply")}
  225. </a>
  226. <div
  227. className="comment-item-bottom-right"
  228. onClick={() => {
  229. if (replyId) {
  230. // 如果有 replyId,则说明是评论的回复
  231. app.sReplyFavor(content.id, commentId, content.favored);
  232. return;
  233. }
  234. app.sCommentFavor(content.id, content.favored);
  235. }}
  236. style={{ color: `${IconColor}` }}
  237. >
  238. <Icon
  239. type="like-o"
  240. className={
  241. content.favored
  242. ? "comment-favor comment-favored"
  243. : "comment-favor"
  244. }
  245. style={{ color: `${IconColor}` }}
  246. />
  247. &nbsp;{content.favor_count}
  248. </div>
  249. </div>
  250. </div>
  251. {showInput && (
  252. <CommentInput
  253. content={app.children}
  254. action={action}
  255. replyId={replyId}
  256. commentId={commentId}
  257. userId={content.user_id}
  258. callback={this.handleToggleInput}
  259. />
  260. )}
  261. </div>
  262. );
  263. }
  264. }
  265. CommentItem.propTypes = {
  266. content: PropTypes.object.isRequired,
  267. // comment 评论
  268. // reply 评论的回复
  269. // replyToReply 回复的回复
  270. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  271. onShowReply: PropTypes.func
  272. };
  273. CommentItem.defaultProps = {
  274. action: "comment",
  275. onShowReply: () => {}
  276. };
  277. export default Comment(CommentItem);