通用评论

EditComment.js 3.9KB

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