通用评论

index.js 11KB

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