import React, { Component } from "react"; import PropTypes from "prop-types"; import { Icon, Tooltip, Popconfirm, Popover } from "antd"; import dayjs from "dayjs"; import "dayjs/locale/zh-cn"; // import 'dayjs/locale/es'; import relativeTime from "dayjs/plugin/relativeTime"; import intl from "react-intl-universal"; import AvatarHoverCard from "./AvatarHoverCard"; import Comment from "../../Comment"; import CommentInput from "../CommentInput"; import avatar from "../../avatar"; import { renderContent } from "../../helper"; import { IMAGE_SPLIT } from "../../constant"; import "./index.css"; import ImagePreviewer from "../ImagePreviewer/ImagePreviewer"; import AudioPlayer from "../AudioPlayer"; // dayjs.locale("zh-cn"); dayjs.extend(relativeTime); const LOCALES = { "zh-CN": "zh-cn" }; class CommentItem extends Component { constructor(props) { super(props); this.state = { showInput: false, showPreviewer: false, previewerIndex: 0, popoverVisible: false }; this.handleToggleInput = this.handleToggleInput.bind(this); this.renderTextWithReply = this.renderTextWithReply.bind(this); this.showPreviewer = this.showPreviewer.bind(this); this.hidePreviewer = this.hidePreviewer.bind(this); this.handleVisibleChange = this.handleVisibleChange.bind(this); this.handleUserAvaClick = this.handleUserAvaClick.bind(this); } showPreviewer(index) { this.setState({ showPreviewer: true, previewerIndex: index }); } hidePreviewer() { this.setState({ showPreviewer: false }); } handleToggleInput() { this.setState({ showInput: !this.state.showInput }); } handleVisibleChange(visible) { this.setState({ popoverVisible: visible }); } handleUserAvaClick() { const { user_id } = this.props; const { userAvaClick } = this.props.app; if (userAvaClick) { userAvaClick(user_id); } } renderTextWithReply(text, content) { let newText = text; const { reply } = content; if (reply) { // newText = `${newText} //@${ // reply.user_name // } ${reply.content}`; newText = `${newText} //@${reply.user_name} ${reply.content}`; // newText = ( // // {newText} // @{reply.user_name}{reply.content} // // ) if (reply.reply) { return this.renderTextWithReply(newText, reply); } } return newText; } render() { const { commentId, replyId, content, action, showReply, onShowReply, app, user_id, page } = this.props; const { medias, is_speak: isSpeak } = content; const { locale, showHoverCard, showEdit } = this.props.app; const { showInput } = this.state; let newContent = content.content; let images = ""; if (newContent.indexOf(IMAGE_SPLIT) !== -1) { newContent = newContent.split(IMAGE_SPLIT); images = newContent.pop(); newContent = newContent.join(""); } const imageList = images.split(","); // 在3, 7前需要换行 const needClear = imageList.length === 5 || imageList.length === 6 || imageList.length === 9; const imgs = [...imageList]; if (needClear) { if (imgs.length > 6) { imgs.splice(3, 0, { type: "divider" }); imgs.splice(7, 0, { type: "divider" }); } else { imgs.splice(3, 0, { type: "divider" }); } } const IconColor = content.favor_count > 0 ? "#71C135" : "#4a90e2"; const showDivider = content.reply_count || (showEdit && app.userId === content.user_id) || app.userId === content.user_id; return (
{showHoverCard ? ( } // placement={this.props.placement} // trigger="click" visible={this.state.popoverVisible} onVisibleChange={this.handleVisibleChange} overlayClassName="avatar-hover-card-overlay" >
) : (
)}
{/* {content.user_name || "暂无昵称"} */} {content.user_name || intl.get("comment.tourist")} {LOCALES[locale] ? dayjs(content.created * 1000) .locale(LOCALES[locale]) .fromNow() : dayjs(content.created * 1000).fromNow()}
{isSpeak && (
[{intl.get("comment.speakComment")}]
)} {// image为空时不渲染comment-item-image imageList.length > 0 && imageList[0] !== "" && (
{!this.state.showPreviewer && imgs.map((item, index) => { if (item.type === "divider") { return (
{/* {item} */}
); } return (
{ let i = index; if (needClear) { if (index > 3) { i -= 1; } if (index > 7) { i -= 1; } } this.showPreviewer(i); }} >
{/* {item} */}
); })} {this.state.showPreviewer && ( )}
)}
{content.reply_count ? ( ) : null} {showEdit && !isSpeak && app.userId === content.user_id && ( this.props.app.handleEdit({ action, replyId, commentId, userId: content.user_id, content, replyPage: page }) } /> )} {app.userId === content.user_id && ( { if (replyId) { app.sDeleteReply(content.id, commentId); return; } app.sDeleteComment(content.id); }} okText={intl.get("popConfirm.ok")} cancelText={intl.get("popConfirm.cancel")} > {/*   {intl.get("popConfirm.delete")} */} )} {showDivider && }
{ if (replyId) { // 如果有 replyId,则说明是评论的回复 app.sReplyFavor(content.id, commentId, content.favored); return; } app.sCommentFavor(content.id, content.favored); }} style={{ color: `${IconColor}` }} >
  {content.favor_count}
  {intl.get("comment.reply")}
{showInput && ( )}
); } } CommentItem.propTypes = { content: PropTypes.object.isRequired, // comment 评论 // reply 评论的回复 // replyToReply 回复的回复 action: PropTypes.oneOf(["comment", "reply", "replyToReply"]), onShowReply: PropTypes.func, showEdit: PropTypes.bool }; CommentItem.defaultProps = { action: "comment", onShowReply: () => {}, showEdit: false }; export default Comment(CommentItem);