通用评论

index.js 7.3KB

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