通用评论

index.js 6.9KB

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