通用评论

index.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // const { type, commentId, replyId, handleToggleInput } = this.props;
  101. // if (type === "normal") {
  102. // this.props.app.sCreateComment({
  103. // content: value,
  104. // });
  105. // } else if (type === "comment") {
  106. // this.props.app.sCreateReply(
  107. // {
  108. // comment_id: commentId,
  109. // content: value,
  110. // },
  111. // () => handleToggleInput()
  112. // );
  113. // } else if (type === "reply") {
  114. // this.props.app.sCreateReply(
  115. // {
  116. // comment_id: commentId,
  117. // content: value,
  118. // reply_id: replyId,
  119. // },
  120. // () => handleToggleInput()
  121. // );
  122. // }
  123. }
  124. render() {
  125. const props = { ...EditorDefaultProps, ...this.props };
  126. const {
  127. value,
  128. onChange,
  129. onSubmit,
  130. loading,
  131. placeholder,
  132. fileList,
  133. onChangeFileList,
  134. rows,
  135. onUpload,
  136. showEmoji,
  137. showUpload,
  138. submitText
  139. } = props;
  140. return (
  141. <div className="editor">
  142. <TextArea
  143. value={this.state.value}
  144. onChange={e => this.handleChange(e.target.value)}
  145. rows={rows}
  146. placeholder={placeholder}
  147. />
  148. <div className="toolbar">
  149. <div style={{ float: "left", margin: "8px 11px" }}>
  150. {showEmoji && (
  151. <Popover
  152. trigger="click"
  153. placement="bottomLeft"
  154. autoAdjustOverflow={false}
  155. content={
  156. <div style={{ width: 200 }}>
  157. <Emoji onClick={this.handleClickEmoji} />
  158. </div>
  159. }
  160. overlayClassName="feed"
  161. >
  162. <Icon type="smile-o" className="icon" />
  163. </Popover>
  164. )}
  165. {showUpload && (
  166. <Popover
  167. visible={this.state.showUpload}
  168. overlayStyle={{ zIndex: 999 }}
  169. content={
  170. <div
  171. style={{ width: 112 * MAX_UPLOAD_NUMBER, minHeight: 100 }}
  172. >
  173. <Upload
  174. onChangeFileList={this.handleChangeFileList}
  175. onUpload={this.handleUpload}
  176. fileList={this.state.fileList}
  177. />
  178. </div>
  179. }
  180. placement="bottomLeft"
  181. title={
  182. <div style={{ margin: "5px auto" }}>
  183. <span>
  184. 上传图片
  185. <span style={{ color: "#666", fontWeight: 400 }}>
  186. (您还能上传{MAX_UPLOAD_NUMBER -
  187. this.state.fileList.length}张图片)
  188. </span>
  189. </span>
  190. <Icon
  191. type="close"
  192. onClick={() => this.handleShowUpload(false)}
  193. style={{
  194. float: "right",
  195. cursor: "pointer",
  196. marginTop: 4
  197. }}
  198. />
  199. </div>
  200. }
  201. >
  202. <Icon
  203. type="picture"
  204. className="icon"
  205. style={{ marginLeft: 10 }}
  206. onClick={() => this.handleShowUpload(true)}
  207. />
  208. </Popover>
  209. )}
  210. </div>
  211. <div style={{ float: "right", margin: "5px 11px" }}>
  212. <Button
  213. onClick={() => this.handleSubmit()}
  214. type="primary"
  215. loading={loading}
  216. >
  217. {submitText}
  218. </Button>
  219. </div>
  220. </div>
  221. </div>
  222. );
  223. }
  224. }
  225. Editor.propTypes = {
  226. rows: PropTypes.number,
  227. placeholder: PropTypes.string,
  228. showEmoji: PropTypes.bool,
  229. showUpload: PropTypes.bool,
  230. submitText: PropTypes.string,
  231. onChange: PropTypes.func
  232. };
  233. export default Editor;