通用评论 vedio

index.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 'dayjs/locale/es';
  7. import relativeTime from "dayjs/plugin/relativeTime";
  8. import intl from "react-intl-universal";
  9. import Comment from "../../Comment";
  10. import CommentInput from "../CommentInput";
  11. import avatar from "../../avatar";
  12. import { renderContent } from "../../helper";
  13. import { IMAGE_SPLIT } from "../../constant";
  14. import "./index.css";
  15. import ImagePreviewer from "../ImagePreviewer/ImagePreviewer";
  16. // dayjs.locale("zh-cn");
  17. dayjs.extend(relativeTime);
  18. const LOCALES = {
  19. "zh-CN": "zh-cn"
  20. };
  21. class CommentItem extends Component {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. showInput: false,
  26. showPreviewer: false,
  27. previewerIndex: 0
  28. };
  29. this.handleToggleInput = this.handleToggleInput.bind(this);
  30. this.renderTextWithReply = this.renderTextWithReply.bind(this);
  31. this.showPreviewer = this.showPreviewer.bind(this);
  32. this.hidePreviewer = this.hidePreviewer.bind(this);
  33. }
  34. showPreviewer(index) {
  35. this.setState({
  36. showPreviewer: true,
  37. previewerIndex: index
  38. });
  39. }
  40. hidePreviewer() {
  41. this.setState({
  42. showPreviewer: false
  43. });
  44. }
  45. handleToggleInput() {
  46. this.setState({ showInput: !this.state.showInput });
  47. }
  48. renderTextWithReply(text, content) {
  49. let newText = text;
  50. const { reply } = content;
  51. if (reply) {
  52. // newText = `${newText} //@<a href="/${reply.user_id}">${
  53. // reply.user_name
  54. // }</a> ${reply.content}`;
  55. newText = `${newText} //@${reply.user_name} ${reply.content}`;
  56. // newText = (
  57. // <span>
  58. // {newText}
  59. // @<a href={`/${reply.user_id}`}>{reply.user_name}</a>{reply.content}
  60. // </span>
  61. // )
  62. if (reply.reply) {
  63. return this.renderTextWithReply(newText, reply);
  64. }
  65. }
  66. return newText;
  67. }
  68. render() {
  69. const {
  70. commentId,
  71. replyId,
  72. content,
  73. action,
  74. showReply,
  75. onShowReply,
  76. app
  77. } = this.props;
  78. const { locale } = this.props.app;
  79. console.log("locale is", locale);
  80. const { showInput } = this.state;
  81. let newContent = content.content;
  82. let images = "";
  83. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  84. newContent = newContent.split(IMAGE_SPLIT);
  85. images = newContent.pop();
  86. newContent = newContent.join("");
  87. }
  88. const imageList = images.split(",");
  89. // 在3, 7前需要换行
  90. const needClear =
  91. imageList.length === 5 ||
  92. imageList.length === 6 ||
  93. imageList.length === 9;
  94. const imgs = [...imageList];
  95. if (needClear) {
  96. if (imgs.length > 6) {
  97. imgs.splice(3, 0, { type: "divider" });
  98. imgs.splice(7, 0, { type: "divider" });
  99. } else {
  100. imgs.splice(3, 0, { type: "divider" });
  101. }
  102. }
  103. return (
  104. <div className="comment-item-box">
  105. <div className="comment-item-left">
  106. <Avatar src={content.user_avatar || avatar} size="large" />
  107. </div>
  108. <div className="comment-item-right">
  109. <div>
  110. {/* <a href={`/${content.user_id}`}>
  111. {content.user_name || "暂无昵称"}
  112. </a> */}
  113. <strong>{content.user_name || intl.get("comment.tourist")}</strong>
  114. <span style={{ marginLeft: 10 }}>
  115. <Tooltip
  116. placement="top"
  117. title={dayjs(content.created * 1000).format(
  118. "YYYY-MM-DD HH:mm:ss"
  119. )}
  120. >
  121. {LOCALES[locale]
  122. ? dayjs(content.created * 1000)
  123. .locale(LOCALES[locale])
  124. .fromNow()
  125. : dayjs(content.created * 1000).fromNow()}
  126. </Tooltip>
  127. </span>
  128. </div>
  129. <div
  130. className="comment-item-content"
  131. dangerouslySetInnerHTML={{
  132. __html: renderContent(
  133. this.renderTextWithReply(newContent, content)
  134. )
  135. }}
  136. />
  137. {// image为空时不渲染comment-item-image
  138. imageList.length > 0 && imageList[0] !== "" && (
  139. <div className="comment-item-image">
  140. {!this.state.showPreviewer &&
  141. imgs.map((item, index) => {
  142. if (item.type === "divider") {
  143. return (
  144. <div className="comment-item-image-wrapper" key={index}>
  145. <div className="comment-img-divider" />
  146. {/* <img src={item} alt={item} className="comment-img" /> */}
  147. </div>
  148. );
  149. }
  150. return (
  151. <div
  152. className="comment-item-image-wrapper"
  153. key={index}
  154. onClick={() => {
  155. let i = index;
  156. if (needClear) {
  157. if (index > 3) {
  158. i -= 1;
  159. }
  160. if (index > 7) {
  161. i -= 1;
  162. }
  163. }
  164. this.showPreviewer(i);
  165. }}
  166. >
  167. <div
  168. style={{ backgroundImage: `url(${item})` }}
  169. className="comment-img-thumbnail"
  170. />
  171. {/* <img src={item} alt={item} className="comment-img" /> */}
  172. </div>
  173. );
  174. })}
  175. {this.state.showPreviewer && (
  176. <ImagePreviewer
  177. list={imageList}
  178. index={this.state.previewerIndex}
  179. onFold={this.hidePreviewer}
  180. />
  181. )}
  182. <div className="clearfix" />
  183. </div>
  184. )}
  185. <div className="comment-item-bottom">
  186. {content.reply_count ? (
  187. <div>
  188. <a className="comment-item-bottom-left" onClick={onShowReply}>
  189. {/* {content.reply_count} 条回复 */}
  190. {intl.get("reply.totalReply", { total: content.reply_count })}
  191. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  192. </a>
  193. </div>
  194. ) : null}
  195. {app.userId === content.user_id && (
  196. <Popconfirm
  197. // title="确定要删除吗?"
  198. title={intl.get("popConfirm.title")}
  199. onConfirm={() => {
  200. if (replyId) {
  201. app.sDeleteReply(content.id, commentId);
  202. return;
  203. }
  204. app.sDeleteComment(content.id);
  205. }}
  206. okText={intl.get("popConfirm.ok")}
  207. cancelText={intl.get("popConfirm.cancel")}
  208. >
  209. <a className="comment-item-bottom-right">
  210. &nbsp; {intl.get("popConfirm.delete")}
  211. </a>
  212. </Popconfirm>
  213. )}
  214. <a
  215. onClick={this.handleToggleInput}
  216. className="comment-item-bottom-right"
  217. >
  218. &nbsp; {intl.get("comment.reply")}
  219. </a>
  220. <div
  221. className="comment-item-bottom-right"
  222. onClick={() => {
  223. if (replyId) {
  224. // 如果有 replyId,则说明是评论的回复
  225. app.sReplyFavor(content.id, commentId, content.favored);
  226. return;
  227. }
  228. app.sCommentFavor(content.id, content.favored);
  229. }}
  230. >
  231. <Icon
  232. type="like-o"
  233. className={
  234. content.favored
  235. ? "comment-favor comment-favored"
  236. : "comment-favor"
  237. }
  238. />
  239. &nbsp;{content.favor_count}
  240. </div>
  241. </div>
  242. </div>
  243. {showInput && (
  244. <CommentInput
  245. content={app.children}
  246. action={action}
  247. replyId={replyId}
  248. commentId={commentId}
  249. callback={this.handleToggleInput}
  250. />
  251. )}
  252. </div>
  253. );
  254. }
  255. }
  256. CommentItem.propTypes = {
  257. content: PropTypes.object.isRequired,
  258. // comment 评论
  259. // reply 评论的回复
  260. // replyToReply 回复的回复
  261. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  262. onShowReply: PropTypes.func
  263. };
  264. CommentItem.defaultProps = {
  265. action: "comment",
  266. onShowReply: () => {}
  267. };
  268. export default Comment(CommentItem);