通用评论

index.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. dayjs.locale("zh-cn");
  14. dayjs.extend(relativeTime);
  15. class CommentItem extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. showInput: false
  20. };
  21. this.handleToggleInput = this.handleToggleInput.bind(this);
  22. this.renderTextWithReply = this.renderTextWithReply.bind(this);
  23. }
  24. handleToggleInput() {
  25. this.setState({ showInput: !this.state.showInput });
  26. }
  27. renderTextWithReply(text, content) {
  28. let newText = text;
  29. const { reply } = content;
  30. if (reply) {
  31. // newText = `${newText} //@<a href="/${reply.user_id}">${
  32. // reply.user_name
  33. // }</a> ${reply.content}`;
  34. newText = `${newText} //@${reply.user_name} ${reply.content}`;
  35. // newText = (
  36. // <span>
  37. // {newText}
  38. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  39. // </span>
  40. // )
  41. if (reply.reply) {
  42. return this.renderTextWithReply(newText, reply);
  43. }
  44. }
  45. return newText;
  46. }
  47. render() {
  48. const {
  49. commentId,
  50. replyId,
  51. content,
  52. action,
  53. showReply,
  54. onShowReply,
  55. app
  56. } = this.props;
  57. const { showInput } = this.state;
  58. let newContent = content.content;
  59. let images = "";
  60. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  61. newContent = newContent.split(IMAGE_SPLIT);
  62. images = newContent.pop();
  63. newContent = newContent.join("");
  64. }
  65. const imageList = images.split(",");
  66. return (
  67. <div className="comment-item-box">
  68. <div className="comment-item-left">
  69. <Avatar src={content.user_avatar || avatar} size="large" />
  70. </div>
  71. <div className="comment-item-right">
  72. <div>
  73. {/* <a href={`/${content.user_id}`}>
  74. {content.user_name || "暂无昵称"}
  75. </a> */}
  76. <strong>{content.user_name || "游客"}</strong>
  77. <span style={{ marginLeft: 10 }}>
  78. <Tooltip
  79. placement="top"
  80. title={dayjs(content.created * 1000).format(
  81. "YYYY-MM-DD HH:mm:ss"
  82. )}
  83. >
  84. {dayjs(content.created * 1000).fromNow()}
  85. </Tooltip>
  86. </span>
  87. </div>
  88. <div
  89. className="comment-item-content"
  90. dangerouslySetInnerHTML={{
  91. __html: renderContent(
  92. this.renderTextWithReply(newContent, content)
  93. )
  94. }}
  95. />
  96. {// image为空时不渲染comment-item-image
  97. imageList.length > 0 &&
  98. imageList[0] !== "" && (
  99. <div className="comment-item-image">
  100. {imageList.map((item, index) => {
  101. return (
  102. <a
  103. href={item}
  104. target="_blank"
  105. rel="noopener noreferrer"
  106. key={index}
  107. >
  108. <img src={item} alt={item} className="comment-img" />
  109. </a>
  110. );
  111. })}
  112. </div>
  113. )}
  114. <div className="comment-item-bottom">
  115. {content.reply_count ? (
  116. <div>
  117. <a className="comment-item-bottom-left" onClick={onShowReply}>
  118. {content.reply_count} 条回复
  119. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  120. </a>
  121. </div>
  122. ) : null}
  123. {app.userId === content.user_id && (
  124. <Popconfirm
  125. title="确定要删除吗?"
  126. onConfirm={() => {
  127. if (replyId) {
  128. app.sDeleteReply(content.id, commentId);
  129. return;
  130. }
  131. app.sDeleteComment(content.id);
  132. }}
  133. okText="确定"
  134. cancelText="取消"
  135. >
  136. <a className="comment-item-bottom-right">&nbsp; 删除</a>
  137. </Popconfirm>
  138. )}
  139. <a
  140. onClick={this.handleToggleInput}
  141. className="comment-item-bottom-right"
  142. >
  143. &nbsp; 回复
  144. </a>
  145. <div
  146. className="comment-item-bottom-right"
  147. onClick={() => {
  148. if (replyId) {
  149. // 如果有 replyId,则说明是评论的回复
  150. app.sReplyFavor(content.id, commentId, content.favored);
  151. return;
  152. }
  153. app.sCommentFavor(content.id, content.favored);
  154. }}
  155. >
  156. <Icon
  157. type="like-o"
  158. className={
  159. content.favored
  160. ? "comment-favor comment-favored"
  161. : "comment-favor"
  162. }
  163. />
  164. &nbsp;{content.favor_count}
  165. </div>
  166. </div>
  167. </div>
  168. {showInput && (
  169. <CommentInput
  170. content={app.children}
  171. action={action}
  172. replyId={replyId}
  173. commentId={commentId}
  174. callback={this.handleToggleInput}
  175. />
  176. )}
  177. </div>
  178. );
  179. }
  180. }
  181. CommentItem.propTypes = {
  182. content: PropTypes.object.isRequired,
  183. // comment 评论
  184. // reply 评论的回复
  185. // replyToReply 回复的回复
  186. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  187. onShowReply: PropTypes.func
  188. };
  189. CommentItem.defaultProps = {
  190. action: "comment",
  191. onShowReply: () => {}
  192. };
  193. export default Comment(CommentItem);