通用评论

index.js 10KB

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