通用评论

index.js 8.6KB

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