通用评论

index.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon, Button, Popover, Input } from "antd";
  4. import { OSS_LINK } from "../../constant";
  5. import { MAX_UPLOAD_NUMBER } from "../../constant";
  6. import Upload from "./Upload";
  7. import Emoji from "./Emoji";
  8. import "./index.css";
  9. const { TextArea } = Input;
  10. // // 设置 Editor 组件的默认值
  11. // // 不能在 Editor.defaultProps 中设置
  12. // // 因为 Editor 在 ComponentInput 中调用
  13. // // 在 ComponentInput 中,需要使用 Editor 的 props 覆盖 ComponentInput 传入的 props
  14. // const EditorDefaultProps = {
  15. // rows: 5,
  16. // placeholder: "说点什么吧...",
  17. // showEmoji: true,
  18. // showUpload: true,
  19. // submitText: "发表",
  20. // onChange: () => {},
  21. // };
  22. class Editor extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. showUpload: false,
  27. value: "", // 编辑器里面的值
  28. fileList: [], // 图片列表
  29. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  30. };
  31. this.handleChange = this.handleChange.bind(this);
  32. this.handleClickEmoji = this.handleClickEmoji.bind(this);
  33. this.handleChangeFileList = this.handleChangeFileList.bind(this);
  34. this.handleShowUpload = this.handleShowUpload.bind(this);
  35. this.handleUpload = this.handleUpload.bind(this);
  36. this.handleSubmit = this.handleSubmit.bind(this);
  37. }
  38. componentDidMount() {}
  39. /**
  40. * 编辑器的值改变事件
  41. * 将最新的值存储到 state 中
  42. * @param {string} value 输入的值
  43. */
  44. handleChange(value) {
  45. this.setState({ value });
  46. }
  47. /**
  48. * 点击 emoji 的事件
  49. * 点击后,需要将改 emoji 插入到编辑器中
  50. * 插入的值为 [emoji chinese name]
  51. * 参数 emoji 即为 emoji chinese name
  52. * @param {string}} emoji emoji 的中文,如 微笑
  53. */
  54. handleClickEmoji(emoji) {
  55. let { value } = this.state;
  56. value += `[${emoji}]`;
  57. this.handleChange(value);
  58. }
  59. /**
  60. * 监听文件列表改变事件
  61. * @param {Array} fileList 文件列表
  62. */
  63. handleChangeFileList(fileList) {
  64. this.setState({ fileList });
  65. }
  66. /**
  67. * 控制上传 Popover 的显示和隐藏
  68. * @param {boolean} showUpload 是否显示上传的 Popover
  69. */
  70. handleShowUpload(showUpload) {
  71. if (typeof showUpload === "boolean") {
  72. this.setState({ showUpload: showUpload });
  73. } else {
  74. this.setState({ showUpload: !this.state.showUpload });
  75. }
  76. }
  77. /**
  78. * 上传文件 TODO:
  79. * @param {object} param 文件对象
  80. */
  81. handleUpload({ uid, path }) {
  82. const { fileMap } = this.state;
  83. fileMap[uid] = path;
  84. this.setState({ fileMap });
  85. }
  86. /**
  87. * 提交编辑器内容
  88. * 提交功能,交给父组件来实现
  89. * 需要父组件传入 onSubmit
  90. */
  91. handleSubmit() {
  92. let { value, fileMap, fileList } = this.state;
  93. if (fileList.length) {
  94. value += "<br/>";
  95. fileList.forEach(item => {
  96. value += `[${OSS_LINK}${fileMap[item.uid]}]`;
  97. });
  98. }
  99. this.props.onSubmit(value);
  100. }
  101. render() {
  102. // const props = { ...EditorDefaultProps, ...this.props };
  103. const {
  104. value,
  105. onChange,
  106. onSubmit,
  107. loading,
  108. placeholder,
  109. fileList,
  110. onChangeFileList,
  111. rows,
  112. onUpload,
  113. showEmoji,
  114. showUpload,
  115. submitText
  116. } = this.props;
  117. // console.log("editor: ", this.props);
  118. return (
  119. <div className="editor">
  120. <TextArea
  121. value={this.state.value}
  122. onChange={e => this.handleChange(e.target.value)}
  123. rows={rows}
  124. placeholder={placeholder}
  125. />
  126. <div className="toolbar">
  127. <div style={{ float: "left", margin: "8px 11px" }}>
  128. {showEmoji && (
  129. <Popover
  130. trigger="click"
  131. placement="bottomLeft"
  132. autoAdjustOverflow={false}
  133. content={
  134. <div style={{ width: 200 }}>
  135. <Emoji onClick={this.handleClickEmoji} />
  136. </div>
  137. }
  138. overlayClassName="feed"
  139. >
  140. <Icon type="smile-o" className="icon" />
  141. </Popover>
  142. )}
  143. {showUpload && (
  144. <Popover
  145. visible={this.state.showUpload}
  146. overlayStyle={{ zIndex: 999 }}
  147. content={
  148. <div
  149. style={{ width: 112 * MAX_UPLOAD_NUMBER, minHeight: 100 }}
  150. >
  151. <Upload
  152. onChangeFileList={this.handleChangeFileList}
  153. onUpload={this.handleUpload}
  154. fileList={this.state.fileList}
  155. />
  156. </div>
  157. }
  158. placement="bottomLeft"
  159. title={
  160. <div style={{ margin: "5px auto" }}>
  161. <span>
  162. 上传图片
  163. <span style={{ color: "#666", fontWeight: 400 }}>
  164. (您还能上传{MAX_UPLOAD_NUMBER -
  165. this.state.fileList.length}张图片)
  166. </span>
  167. </span>
  168. <Icon
  169. type="close"
  170. onClick={() => this.handleShowUpload(false)}
  171. style={{
  172. float: "right",
  173. cursor: "pointer",
  174. marginTop: 4
  175. }}
  176. />
  177. </div>
  178. }
  179. >
  180. <Icon
  181. type="picture"
  182. className="icon"
  183. style={{ marginLeft: 10 }}
  184. onClick={() => this.handleShowUpload(true)}
  185. />
  186. </Popover>
  187. )}
  188. </div>
  189. <div style={{ float: "right", margin: "5px 11px" }}>
  190. <Button
  191. onClick={() => this.handleSubmit()}
  192. type="primary"
  193. loading={loading}
  194. >
  195. {submitText}
  196. </Button>
  197. </div>
  198. </div>
  199. </div>
  200. );
  201. }
  202. }
  203. Editor.propTypes = {
  204. rows: PropTypes.number,
  205. placeholder: PropTypes.string,
  206. showEmoji: PropTypes.bool,
  207. showUpload: PropTypes.bool,
  208. submitText: PropTypes.string,
  209. onChange: PropTypes.func
  210. };
  211. Editor.defaultProps = {
  212. rows: 5,
  213. placeholder: "说点什么吧...",
  214. showEmoji: true,
  215. showUpload: true,
  216. submitText: "发表"
  217. };
  218. export default Editor;