通用评论 vedio

index.js 5.0KB

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. <span style={{ marginLeft: 10 }}>
  76. <Tooltip
  77. placement="top"
  78. title={dayjs(content.created * 1000).format(
  79. "YYYY-MM-DD HH:mm:ss"
  80. )}
  81. >
  82. {dayjs(content.created * 1000).fromNow()}
  83. </Tooltip>
  84. </span>
  85. </div>
  86. <div
  87. className="comment-item-content"
  88. dangerouslySetInnerHTML={{
  89. __html: renderContent(
  90. this.renderTextWithReply(newContent, content)
  91. )
  92. }}
  93. />
  94. <div className="comment-item-image">
  95. {images.split(",").map((item, index) => {
  96. return (
  97. <img
  98. key={index}
  99. src={item}
  100. alt={item}
  101. style={{ maxWidth: "100%" }}
  102. />
  103. );
  104. })}
  105. </div>
  106. <div className="comment-item-bottom">
  107. {content.reply_count ? (
  108. <div>
  109. <a className="comment-item-bottom-left" onClick={onShowReply}>
  110. {content.reply_count} 条回复
  111. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  112. </a>
  113. </div>
  114. ) : null}
  115. <a
  116. onClick={this.handleToggleInput}
  117. className="comment-item-bottom-right"
  118. >
  119. &nbsp; 回复
  120. </a>
  121. <div
  122. className="comment-item-bottom-right"
  123. onClick={() => {
  124. if (replyId) {
  125. // 如果有 replyId,则说明是评论的回复
  126. app.sReplyFavor(content.id, commentId, content.favored);
  127. return;
  128. }
  129. app.sCommentFavor(content.id, content.favored);
  130. }}
  131. >
  132. <Icon
  133. type="like-o"
  134. className={
  135. content.favored
  136. ? "comment-favor comment-favored"
  137. : "comment-favor"
  138. }
  139. />
  140. &nbsp;{content.favor_count}
  141. </div>
  142. </div>
  143. </div>
  144. {showInput && (
  145. <CommentInput
  146. content={app.children}
  147. action={action}
  148. replyId={replyId}
  149. commentId={commentId}
  150. callback={this.handleToggleInput}
  151. />
  152. )}
  153. </div>
  154. );
  155. }
  156. }
  157. CommentItem.propTypes = {
  158. content: PropTypes.object.isRequired,
  159. // comment 评论
  160. // reply 评论的回复
  161. // replyToReply 回复的回复
  162. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  163. onShowReply: PropTypes.func
  164. };
  165. CommentItem.defaultProps = {
  166. action: "comment",
  167. onShowReply: () => {}
  168. };
  169. export default Comment(CommentItem);