通用评论

index.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. className="comment-item-name"
  165. style={{ cursor: "pointer" }}
  166. >
  167. {content.user_name || intl.get("comment.tourist")}
  168. </strong>
  169. <span className="comment-item-date" style={{ marginLeft: 10 }}>
  170. <Tooltip
  171. placement="top"
  172. title={dayjs(content.created * 1000).format(
  173. "YYYY-MM-DD HH:mm:ss"
  174. )}
  175. >
  176. {LOCALES[locale]
  177. ? dayjs(content.created * 1000)
  178. .locale(LOCALES[locale])
  179. .fromNow()
  180. : dayjs(content.created * 1000).fromNow()}
  181. </Tooltip>
  182. </span>
  183. </div>
  184. <div
  185. className="comment-item-content"
  186. dangerouslySetInnerHTML={{
  187. __html: renderContent(
  188. this.renderTextWithReply(newContent, content)
  189. )
  190. }}
  191. />
  192. {// image为空时不渲染comment-item-image
  193. imageList.length > 0 && imageList[0] !== "" && (
  194. <div className="comment-item-image">
  195. {!this.state.showPreviewer &&
  196. imgs.map((item, index) => {
  197. if (item.type === "divider") {
  198. return (
  199. <div className="comment-item-image-wrapper" key={index}>
  200. <div className="comment-img-divider" />
  201. {/* <img src={item} alt={item} className="comment-img" /> */}
  202. </div>
  203. );
  204. }
  205. return (
  206. <div
  207. className="comment-item-image-wrapper"
  208. key={index}
  209. onClick={() => {
  210. let i = index;
  211. if (needClear) {
  212. if (index > 3) {
  213. i -= 1;
  214. }
  215. if (index > 7) {
  216. i -= 1;
  217. }
  218. }
  219. this.showPreviewer(i);
  220. }}
  221. >
  222. <div
  223. style={{ backgroundImage: `url(${item})` }}
  224. className="comment-img-thumbnail"
  225. />
  226. {/* <img src={item} alt={item} className="comment-img" /> */}
  227. </div>
  228. );
  229. })}
  230. {this.state.showPreviewer && (
  231. <ImagePreviewer
  232. list={imageList}
  233. index={this.state.previewerIndex}
  234. onFold={this.hidePreviewer}
  235. />
  236. )}
  237. <div className="clearfix" />
  238. </div>
  239. )}
  240. <div className="comment-item-bottom">
  241. {content.reply_count ? (
  242. <div>
  243. <a className="comment-item-bottom-left" onClick={onShowReply}>
  244. {/* {content.reply_count} 条回复 */}
  245. {intl.get("reply.totalReply", { total: content.reply_count })}
  246. {showReply ? <Icon type="up" /> : <Icon type="down" />}
  247. </a>
  248. </div>
  249. ) : null}
  250. {app.userId === content.user_id && (
  251. <Popconfirm
  252. // title="确定要删除吗?"
  253. title={intl.get("popConfirm.title")}
  254. onConfirm={() => {
  255. if (replyId) {
  256. app.sDeleteReply(content.id, commentId);
  257. return;
  258. }
  259. app.sDeleteComment(content.id);
  260. }}
  261. okText={intl.get("popConfirm.ok")}
  262. cancelText={intl.get("popConfirm.cancel")}
  263. >
  264. <a className="comment-item-bottom-right">
  265. &nbsp; {intl.get("popConfirm.delete")}
  266. </a>
  267. </Popconfirm>
  268. )}
  269. <a
  270. onClick={this.handleToggleInput}
  271. className="comment-item-bottom-right"
  272. >
  273. &nbsp; {intl.get("comment.reply")}
  274. </a>
  275. <div
  276. className="comment-item-bottom-right"
  277. onClick={() => {
  278. if (replyId) {
  279. // 如果有 replyId,则说明是评论的回复
  280. app.sReplyFavor(content.id, commentId, content.favored);
  281. return;
  282. }
  283. app.sCommentFavor(content.id, content.favored);
  284. }}
  285. style={{ color: `${IconColor}` }}
  286. >
  287. <Icon
  288. type="like-o"
  289. className={
  290. content.favored
  291. ? "comment-favor comment-favored"
  292. : "comment-favor"
  293. }
  294. style={{ color: `${IconColor}` }}
  295. />
  296. &nbsp;{content.favor_count}
  297. </div>
  298. </div>
  299. </div>
  300. {showInput && (
  301. <CommentInput
  302. content={app.children}
  303. action={action}
  304. replyId={replyId}
  305. commentId={commentId}
  306. userId={content.user_id}
  307. callback={this.handleToggleInput}
  308. />
  309. )}
  310. </div>
  311. );
  312. }
  313. }
  314. CommentItem.propTypes = {
  315. content: PropTypes.object.isRequired,
  316. // comment 评论
  317. // reply 评论的回复
  318. // replyToReply 回复的回复
  319. action: PropTypes.oneOf(["comment", "reply", "replyToReply"]),
  320. onShowReply: PropTypes.func
  321. };
  322. CommentItem.defaultProps = {
  323. action: "comment",
  324. onShowReply: () => {}
  325. };
  326. export default Comment(CommentItem);