通用评论

EditComment.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // 成功回调,失败不自动关闭
  33. const successCallback = () => {
  34. this.props.handleClose();
  35. };
  36. if (action === "comment") {
  37. app.sUpdateComment({ commentId, content: value, successCallback });
  38. } else {
  39. app.sUpdateReply({
  40. commentId,
  41. content: value,
  42. replyId,
  43. replyPage,
  44. successCallback
  45. });
  46. }
  47. }
  48. getInitValue(props) {
  49. const { content } = props;
  50. let newContent = content.content;
  51. let images = "";
  52. if (newContent.indexOf(IMAGE_SPLIT) !== -1) {
  53. newContent = newContent.split(IMAGE_SPLIT);
  54. images = newContent.pop();
  55. newContent = newContent.join("");
  56. }
  57. let fileList = [];
  58. if (images !== "") {
  59. fileList = images.split(",");
  60. fileList = fileList.map((item, index) => {
  61. return {
  62. thumbUrl: item,
  63. uid: index
  64. };
  65. });
  66. }
  67. const value = this.renderTextWithReply(newContent, content);
  68. return { value, fileList };
  69. }
  70. getEmojiToolIcon() {
  71. return (
  72. <div className="expression-btn-wrap">
  73. <Icon type="smile" className="icon" />
  74. <span className="icon-tool-text">{intl.get("bilingually.emoji")}</span>
  75. </div>
  76. );
  77. }
  78. getImageToolIcon() {
  79. return (
  80. <div className="picture-btn-wrap">
  81. <Icon type="picture" className="icon" />
  82. <span className="icon-tool-text">{intl.get("bilingually.pic")}</span>
  83. </div>
  84. );
  85. }
  86. renderTextWithReply(text, content) {
  87. let newText = text;
  88. // const { reply } = content;
  89. // if (reply) {
  90. // newText = `${newText} //@${reply.user_name} ${reply.content}`;
  91. // if (reply.reply) {
  92. // return this.renderTextWithReply(newText, reply);
  93. // }
  94. // }
  95. return newText;
  96. }
  97. render() {
  98. return (
  99. <Modal
  100. visible={true}
  101. footer={null}
  102. wrapClassName="editCommetModal"
  103. onCancel={this.props.handleClose}
  104. >
  105. <div className="title">{intl.get("comment.edit")}</div>
  106. <CommentInput
  107. content={
  108. <Editor
  109. maxUpload={9}
  110. autoFocus
  111. // {...this.props.editorProps}
  112. action={this.props.action}
  113. replyId={this.props.replyId}
  114. commentId={this.props.commentId}
  115. userId={this.props.content.user_id}
  116. fileList={this.state.fileList}
  117. value={
  118. this.props.preRenderValue &&
  119. this.props.preRenderValue(this.state.value)
  120. }
  121. onChange={value => {
  122. this.setState({
  123. value
  124. });
  125. }}
  126. handleChangeFileList={fileList => {
  127. this.setState({
  128. fileList
  129. });
  130. }}
  131. onSubmit={this.handleSubmit}
  132. emojiToolIcon={this.getEmojiToolIcon()}
  133. imageToolIcon={this.getImageToolIcon()}
  134. emojiPopoverPlacement="bottom"
  135. uploadPopoverPlacement="bottom"
  136. btnSubmitText={intl.get("comment.update")}
  137. />
  138. }
  139. />
  140. </Modal>
  141. );
  142. }
  143. }
  144. export default Comment(EditComment);