通用评论

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 { isFunction } from "../../helper";
  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. this.resetState = this.resetState.bind(this);
  26. }
  27. componentDidMount() {
  28. if (isFunction(this.props.onRef)) {
  29. this.props.onRef(this);
  30. }
  31. }
  32. /**
  33. * 编辑器的值改变事件
  34. * 将最新的值存储到 state 中
  35. * @param {string} value 输入的值
  36. */
  37. handleChange(value) {
  38. this.setState({ value });
  39. if (this.props.onChange) {
  40. this.props.onChange(value);
  41. }
  42. }
  43. /**
  44. * 点击 emoji 的事件
  45. * 点击后,需要将改 emoji 插入到编辑器中
  46. * 插入的值为 [emoji chinese name]
  47. * 参数 emoji 即为 emoji chinese name
  48. * @param {string}} emoji emoji 的中文,如 微笑
  49. */
  50. handleClickEmoji(emoji) {
  51. let { value } = this.state;
  52. value += `[${emoji}]`;
  53. this.handleChange(value);
  54. }
  55. /**
  56. * 监听文件列表改变事件
  57. * @param {Array} fileList 文件列表
  58. */
  59. handleChangeFileList(fileList) {
  60. this.setState({ fileList });
  61. }
  62. /**
  63. * 控制上传 Popover 的显示和隐藏
  64. * @param {boolean} showUpload 是否显示上传的 Popover
  65. */
  66. handleShowUpload(showUpload) {
  67. if (typeof showUpload === "boolean") {
  68. this.setState({ showUpload: showUpload });
  69. } else {
  70. this.setState({ showUpload: !this.state.showUpload });
  71. }
  72. }
  73. /**
  74. * 上传文件
  75. * @param {object} param 文件对象
  76. */
  77. handleUpload({ uid, path }) {
  78. const { fileMap } = this.state;
  79. fileMap[uid] = path;
  80. this.setState({ fileMap });
  81. }
  82. /**
  83. * 提交编辑器内容
  84. * 提交功能,交给父组件来实现
  85. * 需要父组件传入 onSubmit
  86. */
  87. handleSubmit() {
  88. let { value, fileMap, fileList } = this.state;
  89. const files = [];
  90. if (fileList.length) {
  91. fileList.forEach(item => {
  92. files.push(`${OSS_LINK}${fileMap[item.uid]}`);
  93. });
  94. }
  95. if (this.props.beforeSubmit) {
  96. Promise.resolve(this.props.beforeSubmit({ text: value, files })).then(
  97. res => {
  98. if (!(res === false)) {
  99. this.props.onSubmit({ text: value, files }, () => {
  100. this.resetState();
  101. });
  102. }
  103. }
  104. );
  105. } else {
  106. this.props.onSubmit({ text: value, files }, () => {
  107. this.resetState();
  108. });
  109. }
  110. }
  111. resetState() {
  112. this.setState({
  113. showUpload: false,
  114. value: "",
  115. fileList: [],
  116. fileMap: {}
  117. });
  118. }
  119. render() {
  120. const {
  121. value,
  122. placeholder,
  123. rows,
  124. showEmoji,
  125. showUpload,
  126. closeUploadWhenBlur,
  127. maxUpload,
  128. btnSubmitText,
  129. btnLoading,
  130. btnDisabled,
  131. button,
  132. emojiToolIcon,
  133. imageToolIcon
  134. } = this.props;
  135. const handleSubmit = this.handleSubmit;
  136. const disabledSubmit =
  137. btnDisabled ||
  138. (!this.props.value && !this.state.value && !this.state.fileList.length);
  139. return (
  140. <div className="comment-editor">
  141. <TextArea
  142. value={value || this.state.value}
  143. onChange={e => this.handleChange(e.target.value)}
  144. rows={rows}
  145. placeholder={placeholder}
  146. />
  147. <div className="comment-toolbar">
  148. <div className="comment-toolbar-left">
  149. {showEmoji && (
  150. <Popover
  151. trigger="click"
  152. placement="bottomLeft"
  153. autoAdjustOverflow={false}
  154. content={
  155. <div style={{ width: 200 }}>
  156. <Emoji onClick={this.handleClickEmoji} />
  157. </div>
  158. }
  159. overlayClassName="comment-emoji-popover"
  160. >
  161. {emojiToolIcon || (
  162. <Icon type="smile-o" className="comment-toolbar-icon" />
  163. )}
  164. </Popover>
  165. )}
  166. {showUpload ? (
  167. closeUploadWhenBlur ? (
  168. <Popover
  169. trigger="click"
  170. overlayStyle={{ zIndex: 999 }}
  171. content={
  172. <div
  173. style={{
  174. width: 112 * maxUpload,
  175. minHeight: 100,
  176. margin: "0 auto"
  177. }}
  178. >
  179. <Upload
  180. onChangeFileList={this.handleChangeFileList}
  181. onUpload={this.handleUpload}
  182. maxUpload={maxUpload}
  183. fileList={this.state.fileList}
  184. />
  185. </div>
  186. }
  187. placement="bottomLeft"
  188. title={
  189. <div style={{ margin: "5px auto" }}>
  190. <span>
  191. 上传图片
  192. {maxUpload >= 2 ? (
  193. <span style={{ color: "#666", fontWeight: 400 }}>
  194. (您还能上传{maxUpload - this.state.fileList.length}张图片)
  195. </span>
  196. ) : null}
  197. </span>
  198. <Icon
  199. type="close"
  200. onClick={() => this.handleShowUpload(false)}
  201. style={{
  202. float: "right",
  203. cursor: "pointer",
  204. marginTop: 4
  205. }}
  206. />
  207. </div>
  208. }
  209. >
  210. {imageToolIcon ? (
  211. React.cloneElement(imageToolIcon, {
  212. onClick: () => this.handleShowUpload(true)
  213. })
  214. ) : (
  215. <Icon
  216. type="picture"
  217. className="comment-toolbar-icon"
  218. style={{ marginLeft: 10 }}
  219. onClick={() => this.handleShowUpload(true)}
  220. />
  221. )}
  222. </Popover>
  223. ) : (
  224. <Popover
  225. visible={this.state.showUpload}
  226. overlayStyle={{ zIndex: 999 }}
  227. content={
  228. <div
  229. style={{
  230. width: 112 * maxUpload,
  231. minHeight: 100,
  232. margin: "0 auto"
  233. }}
  234. >
  235. <Upload
  236. onChangeFileList={this.handleChangeFileList}
  237. onUpload={this.handleUpload}
  238. maxUpload={maxUpload}
  239. fileList={this.state.fileList}
  240. showError={this.props.showError}
  241. onError={this.props.onError}
  242. />
  243. </div>
  244. }
  245. placement="bottomLeft"
  246. title={
  247. <div style={{ margin: "5px auto" }}>
  248. <span>
  249. 上传图片
  250. {maxUpload >= 2 ? (
  251. <span style={{ color: "#666", fontWeight: 400 }}>
  252. (您还能上传{maxUpload - this.state.fileList.length}张图片)
  253. </span>
  254. ) : null}
  255. </span>
  256. <Icon
  257. type="close"
  258. onClick={() => this.handleShowUpload(false)}
  259. style={{
  260. float: "right",
  261. cursor: "pointer",
  262. marginTop: 4
  263. }}
  264. />
  265. </div>
  266. }
  267. >
  268. {imageToolIcon ? (
  269. React.cloneElement(imageToolIcon, {
  270. onClick: () => this.handleShowUpload(true)
  271. })
  272. ) : (
  273. <Icon
  274. type="picture"
  275. className="comment-toolbar-icon"
  276. style={{ marginLeft: 10 }}
  277. onClick={() => this.handleShowUpload(true)}
  278. />
  279. )}
  280. </Popover>
  281. )
  282. ) : null}
  283. </div>
  284. <div className="comment-toolbar-right">
  285. {button ? (
  286. React.cloneElement(button, {
  287. onClick: button.props.onClick || handleSubmit
  288. })
  289. ) : (
  290. <Button
  291. onClick={() => this.handleSubmit()}
  292. type="primary"
  293. loading={btnLoading}
  294. disabled={disabledSubmit}
  295. >
  296. {btnSubmitText}
  297. </Button>
  298. )}
  299. </div>
  300. </div>
  301. </div>
  302. );
  303. }
  304. }
  305. Editor.propTypes = {
  306. rows: PropTypes.number,
  307. placeholder: PropTypes.string,
  308. showEmoji: PropTypes.bool,
  309. showUpload: PropTypes.bool,
  310. closeUploadWhenBlur: PropTypes.bool,
  311. maxUpload: PropTypes.number,
  312. value: PropTypes.string,
  313. onChange: PropTypes.func,
  314. onSubmit: PropTypes.func,
  315. beforeSubmit: PropTypes.func,
  316. btnSubmitText: PropTypes.string,
  317. btnLoading: PropTypes.bool,
  318. btnDisabled: PropTypes.bool,
  319. button: PropTypes.node,
  320. emojiToolIcon: PropTypes.node,
  321. imageToolIcon: PropTypes.node,
  322. showError: PropTypes.bool,
  323. onError: PropTypes.func
  324. };
  325. Editor.defaultProps = {
  326. rows: 5,
  327. placeholder: "说点什么吧...",
  328. showEmoji: true,
  329. showUpload: true,
  330. closeUploadWhenBlur: false,
  331. maxUpload: 1,
  332. btnSubmitText: "发表",
  333. btnLoading: false,
  334. btnDisabled: false,
  335. showError: true
  336. };
  337. export default Editor;