通用评论

index.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. return (
  66. <div className="comment-item-box">
  67. <div className="comment-item-left">
  68. <Avatar src={content.user_avatar || avatar} size="large" />
  69. </div>
  70. <div className="comment-item-right">
  71. <div>
  72. {/* <a href={`/${content.user_id}`}>
  73. {content.user_name || "暂无昵称"}
  74. </a> */}
  75. <strong>{content.user_name || "游客"}</strong>
  76. <span style={{ marginLeft: 10 }}>
  77. <Tooltip
  78. placement="top"
  79. title={dayjs(content.created * 1000).format(
  80. "YYYY-MM-DD HH:mm:ss"
  81. )}
  82. >
  83. {dayjs(content.created * 1000).fromNow()}
  84. </Tooltip>
  85. </span>
  86. </div>
  87. <div
  88. className="comment-item-content"
  89. dangerouslySetInnerHTML={{
  90. __html: renderContent(
  91. this.renderTextWithReply(newContent, content)
  92. )
  93. }}
  94. />
  95. <div className="comment-item-image">
  96. {images.split(",").map((item, index) => {
  97. return (
  98. <img
  99. key={index}
  100. src={item}
  101. alt={item}
  102. style={{ maxWidth: "100%" }}
  103. />
  104. );
  105. })}
  106. </div>
  107. <div className="comment-item-bottom">
  108. {content.reply_count ? (
  109. <div>
  110. <a className="comment-item-bottom-left" onClick={onShowReply}>
  111. {content.reply_count} 条回复
  112. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  113. </a>
  114. </div>
  115. ) : null}
  116. {app.userId === content.user_id && (
  117. <Popconfirm
  118. title="确定要删除吗?"
  119. onConfirm={() => {
  120. if (replyId) {
  121. app.sDeleteReply(content.id, commentId);
  122. return;
  123. }
  124. app.sDeleteComment(content.id);
  125. }}
  126. okText="确定"
  127. cancelText="取消"
  128. >
  129. <a className="comment-item-bottom-right">&nbsp; 删除</a>
  130. </Popconfirm>
  131. )}
  132. <a
  133. onClick={this.handleToggleInput}
  134. className="comment-item-bottom-right"
  135. >
  136. &nbsp; 回复
  137. </a>
  138. <div
  139. className="comment-item-bottom-right"
  140. onClick={() => {
  141. if (replyId) {
  142. // 如果有 replyId,则说明是评论的回复
  143. app.sReplyFavor(content.id, commentId, content.favored);
  144. return;
  145. }
  146. app.sCommentFavor(content.id, content.favored);
  147. }}
  148. >
  149. <Icon
  150. type="like-o"
  151. className={
  152. content.favored
  153. ? "comment-favor comment-favored"
  154. : "comment-favor"
  155. }
  156. />
  157. &nbsp;{content.favor_count}
  158. </div>
  159. </div>
  160. </div>
  161. {showInput && (
  162. <CommentInput
  163. content={app.children}
  164. action={action}
  165. replyId={replyId}
  166. commentId={commentId}
  167. callback={this.handleToggleInput}
  168. />
  169. )}
  170. </div>
  171. );
  172. }
  173. }
  174. CommentItem.propTypes = {
  175. content: PropTypes.object.isRequired,
  176. // comment 评论
  177. // reply 评论的回复
  178. // replyToReply 回复的回复
  179. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  180. onShowReply: PropTypes.func
  181. };
  182. CommentItem.defaultProps = {
  183. action: "comment",
  184. onShowReply: () => {}
  185. };
  186. export default Comment(CommentItem);