通用评论

EditComment.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React from "react";
  2. import { Modal, Icon } from "antd";
  3. import intl from "react-intl-universal";
  4. import CommentInput from "../CommentInput";
  5. import Editor from "../Editor";
  6. import { IMAGE_SPLIT } from "../../constant";
  7. import "./EditComment.css";
  8. import Comment from "../../Comment";
  9. class EditComment extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.handleSubmit = this.handleSubmit.bind(this);
  13. this.state = {
  14. value: this.getInitValue(props).value,
  15. fileList: this.getInitValue(props).fileList
  16. };
  17. }
  18. handleSubmit({ text = "", files = [] }) {
  19. const { fileList } = this.state;
  20. const { app, action, commentId, replyId, replyPage } = this.props;
  21. let value = text;
  22. app.onBeforeUpdateComment();
  23. if (fileList && fileList.length) {
  24. value += IMAGE_SPLIT;
  25. fileList.forEach(file => {
  26. value += `${file.thumbUrl},`;
  27. });
  28. }
  29. if (value.substr(-1) === ",") {
  30. value = value.slice(0, -1);
  31. }
  32. if (action === "comment") {
  33. app.sUpdateComment({ commentId, content: value });
  34. } else {
  35. app.sUpdateReply({ commentId, content: value, replyId, replyPage });
  36. }
  37. this.props.handleClose();
  38. }
  39. getInitValue(props) {
  40. const { content } = props;
  41. let newContent = content.content;
  42. let images = "";
  43. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  44. newContent = newContent.split(IMAGE_SPLIT);
  45. images = newContent.pop();
  46. newContent = newContent.join("");
  47. }
  48. let fileList = [];
  49. if (images !== "") {
  50. fileList = images.split(",");
  51. fileList = fileList.map((item, index) => {
  52. return {
  53. thumbUrl: item,
  54. uid: index
  55. };
  56. });
  57. }
  58. const value = this.renderTextWithReply(newContent, content);
  59. return { value, fileList };
  60. }
  61. getEmojiToolIcon() {
  62. return (
  63. <div className="expression-btn-wrap">
  64. <Icon type="smile" className="icon" />
  65. <span className="icon-tool-text">{intl.get("bilingually.emoji")}</span>
  66. </div>
  67. );
  68. }
  69. getImageToolIcon() {
  70. return (
  71. <div className="picture-btn-wrap">
  72. <Icon type="picture" className="icon" />
  73. <span className="icon-tool-text">{intl.get("bilingually.pic")}</span>
  74. </div>
  75. );
  76. }
  77. renderTextWithReply(text, content) {
  78. let newText = text;
  79. // const { reply } = content;
  80. // if (reply) {
  81. // newText = `${newText} //@${reply.user_name} ${reply.content}`;
  82. // if (reply.reply) {
  83. // return this.renderTextWithReply(newText, reply);
  84. // }
  85. // }
  86. return newText;
  87. }
  88. render() {
  89. return (
  90. <Modal
  91. visible={true}
  92. footer={null}
  93. wrapClassName="editCommetModal"
  94. onCancel={this.props.handleClose}
  95. >
  96. <div className="title">{intl.get("comment.edit")}</div>
  97. <CommentInput
  98. content={
  99. <Editor
  100. maxUpload={9}
  101. autoFocus
  102. // {...this.props.editorProps}
  103. action={this.props.action}
  104. replyId={this.props.replyId}
  105. commentId={this.props.commentId}
  106. userId={this.props.content.user_id}
  107. fileList={this.state.fileList}
  108. value={
  109. this.props.preRenderValue &&
  110. this.props.preRenderValue(this.state.value)
  111. }
  112. onChange={value => {
  113. this.setState({
  114. value
  115. });
  116. }}
  117. handleChangeFileList={fileList => {
  118. this.setState({
  119. fileList
  120. });
  121. }}
  122. onSubmit={this.handleSubmit}
  123. emojiToolIcon={this.getEmojiToolIcon()}
  124. imageToolIcon={this.getImageToolIcon()}
  125. emojiPopoverPlacement="bottom"
  126. uploadPopoverPlacement="bottom"
  127. btnSubmitText={intl.get("comment.update")}
  128. />
  129. }
  130. />
  131. </Modal>
  132. );
  133. }
  134. }
  135. export default Comment(EditComment);