通用评论 vedio

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. <img
  103. key={index}
  104. src={item}
  105. alt={item}
  106. style={{ maxWidth: "100%" }}
  107. />
  108. );
  109. })}
  110. </div>
  111. )}
  112. <div className="comment-item-bottom">
  113. {content.reply_count ? (
  114. <div>
  115. <a className="comment-item-bottom-left" onClick={onShowReply}>
  116. {content.reply_count} 条回复
  117. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  118. </a>
  119. </div>
  120. ) : null}
  121. {app.userId === content.user_id && (
  122. <Popconfirm
  123. title="确定要删除吗?"
  124. onConfirm={() => {
  125. if (replyId) {
  126. app.sDeleteReply(content.id, commentId);
  127. return;
  128. }
  129. app.sDeleteComment(content.id);
  130. }}
  131. okText="确定"
  132. cancelText="取消"
  133. >
  134. <a className="comment-item-bottom-right">&nbsp; 删除</a>
  135. </Popconfirm>
  136. )}
  137. <a
  138. onClick={this.handleToggleInput}
  139. className="comment-item-bottom-right"
  140. >
  141. &nbsp; 回复
  142. </a>
  143. <div
  144. className="comment-item-bottom-right"
  145. onClick={() => {
  146. if (replyId) {
  147. // 如果有 replyId,则说明是评论的回复
  148. app.sReplyFavor(content.id, commentId, content.favored);
  149. return;
  150. }
  151. app.sCommentFavor(content.id, content.favored);
  152. }}
  153. >
  154. <Icon
  155. type="like-o"
  156. className={
  157. content.favored
  158. ? "comment-favor comment-favored"
  159. : "comment-favor"
  160. }
  161. />
  162. &nbsp;{content.favor_count}
  163. </div>
  164. </div>
  165. </div>
  166. {showInput && (
  167. <CommentInput
  168. content={app.children}
  169. action={action}
  170. replyId={replyId}
  171. commentId={commentId}
  172. callback={this.handleToggleInput}
  173. />
  174. )}
  175. </div>
  176. );
  177. }
  178. }
  179. CommentItem.propTypes = {
  180. content: PropTypes.object.isRequired,
  181. // comment 评论
  182. // reply 评论的回复
  183. // replyToReply 回复的回复
  184. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  185. onShowReply: PropTypes.func
  186. };
  187. CommentItem.defaultProps = {
  188. action: "comment",
  189. onShowReply: () => {}
  190. };
  191. export default Comment(CommentItem);