通用评论 vedio

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. const { showInput } = this.state;
  80. let newContent = content.content;
  81. let images = "";
  82. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  83. newContent = newContent.split(IMAGE_SPLIT);
  84. images = newContent.pop();
  85. newContent = newContent.join("");
  86. }
  87. const imageList = images.split(",");
  88. // 在3, 7前需要换行
  89. const needClear =
  90. imageList.length === 5 ||
  91. imageList.length === 6 ||
  92. imageList.length === 9;
  93. const imgs = [...imageList];
  94. if (needClear) {
  95. if (imgs.length > 6) {
  96. imgs.splice(3, 0, { type: "divider" });
  97. imgs.splice(7, 0, { type: "divider" });
  98. } else {
  99. imgs.splice(3, 0, { type: "divider" });
  100. }
  101. }
  102. return (
  103. <div className="comment-item-box">
  104. <div className="comment-item-left">
  105. <Avatar src={content.user_avatar || avatar} size="large" />
  106. </div>
  107. <div className="comment-item-right">
  108. <div>
  109. {/* <a href={`/${content.user_id}`}>
  110. {content.user_name || "暂无昵称"}
  111. </a> */}
  112. <strong style={{ color: "#75ACFF" }}>
  113. {/* {content.user_name || intl.get("comment.tourist")} */}
  114. {content.user_name || "游客"}
  115. </strong>
  116. {/* <span style={{ marginLeft: 10 }}>
  117. <Tooltip
  118. placement="top"
  119. title={dayjs(content.created * 1000).format(
  120. "YYYY-MM-DD HH:mm:ss"
  121. )}
  122. >
  123. {LOCALES[locale]
  124. ? dayjs(content.created * 1000)
  125. .locale(LOCALES[locale])
  126. .fromNow()
  127. : dayjs(content.created * 1000).fromNow()}
  128. </Tooltip>
  129. </span> */}
  130. <span
  131. className="comment-item-icon"
  132. onClick={this.handleToggleInput}
  133. >
  134. <Icon type="message" />
  135. &nbsp; {content.reply_count}
  136. </span>
  137. <span className="comment-item-icon">
  138. <Icon
  139. type="heart"
  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. className={
  149. content.favored
  150. ? "comment-favor comment-favored"
  151. : "comment-favor"
  152. }
  153. />
  154. &nbsp; {content.favor_count}
  155. </span>
  156. </div>
  157. <div style={{ color: "#D5D5D5" }}>
  158. {dayjs(content.created * 1000).format("MM-DD HH:mm:ss")}
  159. </div>
  160. </div>
  161. <div style={{ overflow: "auto" }}>
  162. <div
  163. className="comment-item-content"
  164. dangerouslySetInnerHTML={{
  165. __html: renderContent(
  166. this.renderTextWithReply(newContent, content)
  167. )
  168. }}
  169. />
  170. {// image为空时不渲染comment-item-image
  171. imageList.length > 0 && imageList[0] !== "" && (
  172. <div className="comment-item-image">
  173. {!this.state.showPreviewer &&
  174. imgs.map((item, index) => {
  175. if (item.type === "divider") {
  176. return (
  177. <div className="comment-item-image-wrapper" key={index}>
  178. <div className="comment-img-divider" />
  179. {/* <img src={item} alt={item} className="comment-img" /> */}
  180. </div>
  181. );
  182. }
  183. return (
  184. <div
  185. className="comment-item-image-wrapper"
  186. key={index}
  187. onClick={() => {
  188. let i = index;
  189. if (needClear) {
  190. if (index > 3) {
  191. i -= 1;
  192. }
  193. if (index > 7) {
  194. i -= 1;
  195. }
  196. }
  197. this.showPreviewer(i);
  198. }}
  199. >
  200. <div
  201. style={{ backgroundImage: `url(${item})` }}
  202. className="comment-img-thumbnail"
  203. />
  204. {/* <img src={item} alt={item} className="comment-img" /> */}
  205. </div>
  206. );
  207. })}
  208. {this.state.showPreviewer && (
  209. <ImagePreviewer
  210. list={imageList}
  211. index={this.state.previewerIndex}
  212. onFold={this.hidePreviewer}
  213. />
  214. )}
  215. <div className="clearfix" />
  216. </div>
  217. )}
  218. <div className="comment-item-bottom">
  219. {content.reply_count ? (
  220. <div>
  221. <a className="comment-item-bottom-left" onClick={onShowReply}>
  222. {content.reply_count} 条回复
  223. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  224. </a>
  225. </div>
  226. ) : null}
  227. </div>
  228. {/* <div className="comment-item-bottom">
  229. {content.reply_count ? (
  230. <div>
  231. <a className="comment-item-bottom-left" onClick={onShowReply}>
  232. {content.reply_count} 条回复
  233. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  234. </a>
  235. </div>
  236. ) : null}
  237. {app.userId === content.user_id && (
  238. <Popconfirm
  239. // title="确定要删除吗?"
  240. title={intl.get("popConfirm.title")}
  241. onConfirm={() => {
  242. if (replyId) {
  243. app.sDeleteReply(content.id, commentId);
  244. return;
  245. }
  246. app.sDeleteComment(content.id);
  247. }}
  248. okText={intl.get("popConfirm.ok")}
  249. cancelText={intl.get("popConfirm.cancel")}
  250. >
  251. <a className="comment-item-bottom-right">
  252. &nbsp; {intl.get("popConfirm.delete")}
  253. </a>
  254. </Popconfirm>
  255. )}
  256. <a
  257. onClick={this.handleToggleInput}
  258. className="comment-item-bottom-right"
  259. >
  260. &nbsp; {intl.get("comment.reply")}
  261. </a>
  262. <div
  263. className="comment-item-bottom-right"
  264. onClick={() => {
  265. if (replyId) {
  266. // 如果有 replyId,则说明是评论的回复
  267. app.sReplyFavor(content.id, commentId, content.favored);
  268. return;
  269. }
  270. app.sCommentFavor(content.id, content.favored);
  271. }}
  272. >
  273. <Icon
  274. type="like-o"
  275. className={
  276. content.favored
  277. ? "comment-favor comment-favored"
  278. : "comment-favor"
  279. }
  280. />
  281. &nbsp;{content.favor_count}
  282. </div>
  283. </div> */}
  284. </div>
  285. {showInput && (
  286. <CommentInput
  287. content={app.children}
  288. action={action}
  289. replyId={replyId}
  290. commentId={commentId}
  291. callback={this.handleToggleInput}
  292. />
  293. )}
  294. </div>
  295. );
  296. }
  297. }
  298. CommentItem.propTypes = {
  299. content: PropTypes.object.isRequired,
  300. // comment 评论
  301. // reply 评论的回复
  302. // replyToReply 回复的回复
  303. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  304. onShowReply: PropTypes.func
  305. };
  306. CommentItem.defaultProps = {
  307. action: "comment",
  308. onShowReply: () => {}
  309. };
  310. export default Comment(CommentItem);