通用评论

index.js 6.3KB

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