通用评论

index.js 10KB

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