通用评论

index.js 12KB

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