通用评论

index.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Avatar, Icon, Tooltip } 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. <a
  117. onClick={this.handleToggleInput}
  118. className="comment-item-bottom-right"
  119. >
  120. &nbsp; 回复
  121. </a>
  122. <div
  123. className="comment-item-bottom-right"
  124. onClick={() => {
  125. if (replyId) {
  126. // 如果有 replyId,则说明是评论的回复
  127. app.sReplyFavor(content.id, commentId, content.favored);
  128. return;
  129. }
  130. app.sCommentFavor(content.id, content.favored);
  131. }}
  132. >
  133. <Icon
  134. type="like-o"
  135. className={
  136. content.favored
  137. ? "comment-favor comment-favored"
  138. : "comment-favor"
  139. }
  140. />
  141. &nbsp;{content.favor_count}
  142. </div>
  143. </div>
  144. </div>
  145. {showInput && (
  146. <CommentInput
  147. content={app.children}
  148. action={action}
  149. replyId={replyId}
  150. commentId={commentId}
  151. callback={this.handleToggleInput}
  152. />
  153. )}
  154. </div>
  155. );
  156. }
  157. }
  158. CommentItem.propTypes = {
  159. content: PropTypes.object.isRequired,
  160. // comment 评论
  161. // reply 评论的回复
  162. // replyToReply 回复的回复
  163. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  164. onShowReply: PropTypes.func
  165. };
  166. CommentItem.defaultProps = {
  167. action: "comment",
  168. onShowReply: () => {}
  169. };
  170. export default Comment(CommentItem);