通用评论

index.js 8.1KB

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