通用评论

index.js 11KB

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