通用评论

index.js 10KB

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