通用评论 vedio

index.js 10KB

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