通用评论

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon, Button, Popover, Input, message } from "antd";
  4. import classnames from "classnames";
  5. import intl from "react-intl-universal";
  6. import { OSS_LINK } from "../../constant";
  7. import { isFunction } from "../../helper";
  8. import Upload from "./Upload";
  9. import Emoji from "./Emoji";
  10. import "./index.css";
  11. const { TextArea } = Input;
  12. class Editor extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. showUpload: false,
  17. value: props.value, // 编辑器里面的值
  18. fileList: props.fileList, // 图片列表
  19. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  20. };
  21. this.handleChange = this.handleChange.bind(this);
  22. this.handleClickEmoji = this.handleClickEmoji.bind(this);
  23. this.handleChangeFileList = this.handleChangeFileList.bind(this);
  24. this.handleShowUpload = this.handleShowUpload.bind(this);
  25. this.handleUpload = this.handleUpload.bind(this);
  26. this.handleSubmit = this.handleSubmit.bind(this);
  27. this.resetState = this.resetState.bind(this);
  28. this.handleEmojiScroll = this.handleEmojiScroll.bind(this);
  29. }
  30. componentDidMount() {
  31. if (isFunction(this.props.onRef)) {
  32. this.props.onRef(this);
  33. }
  34. }
  35. handleEmojiScroll(e) {
  36. if (!this.emoji) {
  37. return;
  38. }
  39. e.preventDefault();
  40. if (e.deltaY > 0) {
  41. this.emoji.next();
  42. } else if (e.deltaY < 0) {
  43. this.emoji.prev();
  44. }
  45. }
  46. /**
  47. * 编辑器的值改变事件
  48. * 将最新的值存储到 state 中
  49. * @param {string} value 输入的值
  50. */
  51. handleChange(value) {
  52. this.setState({ value });
  53. if (this.props.onChange) {
  54. this.props.onChange(value);
  55. }
  56. }
  57. /**
  58. * 点击 emoji 的事件
  59. * 点击后,需要将改 emoji 插入到编辑器中
  60. * 插入的值为 [emoji chinese name]
  61. * 参数 emoji 即为 emoji chinese name
  62. * @param {string}} emoji emoji 的中文,如 微笑
  63. */
  64. handleClickEmoji(emoji) {
  65. let value = this.props.value || this.state.value;
  66. value += `[${emoji}]`;
  67. this.handleChange(value);
  68. }
  69. /**
  70. * 监听文件列表改变事件
  71. * @param {Array} fileList 文件列表
  72. */
  73. handleChangeFileList(fileList) {
  74. let list = fileList;
  75. if (fileList.length > this.props.maxUpload) {
  76. list = fileList.slice(0, this.props.maxUpload);
  77. }
  78. this.props.handleChangeFileList(list);
  79. this.setState({ fileList: list });
  80. }
  81. /**
  82. * 控制上传 Popover 的显示和隐藏
  83. * @param {boolean} showUpload 是否显示上传的 Popover
  84. */
  85. handleShowUpload(showUpload) {
  86. if (typeof showUpload === "boolean") {
  87. this.setState({ showUpload: showUpload });
  88. } else {
  89. this.setState({ showUpload: !this.state.showUpload });
  90. }
  91. }
  92. /**
  93. * 上传文件
  94. * @param {object} param 文件对象
  95. */
  96. handleUpload({ uid, path }) {
  97. const { fileMap } = this.state;
  98. let { fileList } = this.state;
  99. fileMap[uid] = path;
  100. fileList = fileList.map(item => {
  101. if (item.uid === uid) {
  102. item.thumbUrl = OSS_LINK + path;
  103. }
  104. return item;
  105. });
  106. this.props.handleChangeFileList(fileList);
  107. this.setState({ fileMap, fileList });
  108. }
  109. /**
  110. * 提交编辑器内容
  111. * 提交功能,交给父组件来实现
  112. * 需要父组件传入 onSubmit
  113. */
  114. handleSubmit() {
  115. const { maxLength } = this.props;
  116. let { value, fileMap, fileList } = this.state;
  117. if (value.length > maxLength) {
  118. // message.error(`字数不得超过${maxLength}字`);
  119. message.error(intl.get("editor.maxLength", { maxLength }));
  120. return;
  121. }
  122. const files = [];
  123. if (fileList.length) {
  124. fileList.forEach(item => {
  125. if (item.url) {
  126. files.push(item.url);
  127. return;
  128. }
  129. if (!fileMap[item.uid]) {
  130. return;
  131. }
  132. files.push(`${OSS_LINK}${fileMap[item.uid]}`);
  133. });
  134. }
  135. if (this.props.beforeSubmit) {
  136. Promise.resolve(this.props.beforeSubmit({ text: value, files })).then(
  137. res => {
  138. if (!(res === false)) {
  139. this.props.onSubmit({ text: value, files }, (res, action) => {
  140. this.resetState();
  141. if (action === "comment" && this.props.onCommentSuccess) {
  142. this.props.onCommentSuccess(res);
  143. }
  144. });
  145. }
  146. }
  147. );
  148. } else {
  149. this.props.onSubmit({ text: value, files }, (res, action) => {
  150. this.resetState();
  151. if (action === "comment" && this.props.onCommentSuccess) {
  152. this.props.onCommentSuccess(res);
  153. }
  154. });
  155. }
  156. }
  157. resetState() {
  158. this.handleChange("");
  159. this.setState({
  160. showUpload: false,
  161. value: "",
  162. fileList: [],
  163. fileMap: {}
  164. });
  165. }
  166. render() {
  167. const {
  168. value,
  169. // placeholder,
  170. rows,
  171. showEmoji,
  172. showUpload,
  173. multiple,
  174. emojiPopoverPlacement,
  175. uploadPopoverPlacement,
  176. uploadOverlayClassName,
  177. fileList,
  178. closeUploadWhenBlur,
  179. maxUpload,
  180. // btnSubmitText,
  181. btnLoading,
  182. btnDisabled,
  183. button,
  184. emojiToolIcon,
  185. imageToolIcon,
  186. maxLength,
  187. autoFocus
  188. } = this.props;
  189. let placeholder = this.props.placeholder || intl.get("editor.placeholder");
  190. let btnSubmitText =
  191. this.props.btnSubmitText || intl.get("editor.SubmitBtn");
  192. const handleSubmit = this.handleSubmit;
  193. const disabledSubmit =
  194. btnDisabled ||
  195. (!this.props.value &&
  196. !this.state.value &&
  197. !this.props.fileList.length &&
  198. !this.state.fileList.length);
  199. const inputValue = value || this.state.value;
  200. const uploadFileList = fileList || this.state.fileList;
  201. return (
  202. <div className="comment-editor-container">
  203. <div
  204. className={classnames({
  205. "comment-editor-toolbar": true,
  206. "comment-editor-toolbar-error": inputValue.length > maxLength
  207. })}
  208. >
  209. {/* {intl.get("editor.alreadyEntered", {
  210. count: inputValue.length,
  211. maxLength
  212. })} */}
  213. </div>
  214. <div className="comment-editor">
  215. <TextArea
  216. value={inputValue}
  217. onChange={e => this.handleChange(e.target.value)}
  218. rows={rows}
  219. placeholder={placeholder}
  220. autoFocus={autoFocus}
  221. />
  222. <div className="comment-toolbar">
  223. <div className="comment-toolbar-left">
  224. {showEmoji && (
  225. <Popover
  226. trigger="click"
  227. placement={emojiPopoverPlacement}
  228. autoAdjustOverflow={false}
  229. overlayStyle={{ zIndex: 9999 }}
  230. content={
  231. <div
  232. style={{ width: 240, height: 205 }}
  233. onWheel={this.handleEmojiScroll}
  234. >
  235. <Emoji
  236. onClick={this.handleClickEmoji}
  237. ref={node => {
  238. this.emoji = node;
  239. }}
  240. />
  241. </div>
  242. }
  243. overlayClassName="comment-emoji-popover"
  244. >
  245. {emojiToolIcon || (
  246. <Icon type="smile-o" className="comment-toolbar-icon" />
  247. )}
  248. </Popover>
  249. )}
  250. {showUpload ? (
  251. <Popover
  252. trigger="click"
  253. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  254. // visible={this.state.showUpload}
  255. placement={uploadPopoverPlacement}
  256. overlayClassName={uploadOverlayClassName}
  257. autoAdjustOverflow={false}
  258. overlayStyle={{ zIndex: 9999 }}
  259. onVisibleChange={
  260. closeUploadWhenBlur
  261. ? visible => {
  262. this.handleShowUpload(visible);
  263. }
  264. : null
  265. }
  266. content={
  267. <div
  268. style={{
  269. width: 336, // 一行显示3张
  270. minHeight: 100,
  271. margin: "0 auto"
  272. }}
  273. >
  274. <Upload
  275. multiple={multiple}
  276. onChangeFileList={this.handleChangeFileList}
  277. onUpload={this.handleUpload}
  278. maxUpload={maxUpload}
  279. fileList={uploadFileList}
  280. showError={this.props.showError}
  281. onError={this.props.onError}
  282. />
  283. <div className="clearfix" />
  284. </div>
  285. }
  286. title={
  287. <div style={{ margin: "5px auto" }}>
  288. <span>
  289. {intl.get("editor.uploadTip")}
  290. {maxUpload >= 2 ? (
  291. <span style={{ color: "#666", fontWeight: 400 }}>
  292. {intl.get("editor.uploadCount", {
  293. count: maxUpload - uploadFileList.length
  294. })}
  295. </span>
  296. ) : null}
  297. </span>
  298. {/* 因为是点击别的区域关闭,所以不用右上角的 Icon */}
  299. {/* <Icon
  300. type="close"
  301. onClick={() => this.handleShowUpload(false)}
  302. style={{
  303. float: "right",
  304. cursor: "pointer",
  305. marginTop: 4
  306. }}
  307. /> */}
  308. </div>
  309. }
  310. >
  311. {imageToolIcon ? (
  312. React.cloneElement(imageToolIcon, {
  313. onClick: () => this.handleShowUpload(true)
  314. })
  315. ) : (
  316. <Icon
  317. type="picture"
  318. className="comment-toolbar-icon"
  319. style={{ marginLeft: 20 }}
  320. onClick={() => this.handleShowUpload(true)}
  321. />
  322. )}
  323. </Popover>
  324. ) : null}
  325. </div>
  326. <div className="comment-toolbar-right">
  327. {button ? (
  328. React.cloneElement(button, {
  329. onClick: button.props.onClick || handleSubmit
  330. })
  331. ) : (
  332. <Button
  333. onClick={() => this.handleSubmit()}
  334. type="primary"
  335. loading={btnLoading}
  336. disabled={disabledSubmit}
  337. >
  338. {btnSubmitText}
  339. </Button>
  340. )}
  341. </div>
  342. </div>
  343. </div>
  344. </div>
  345. );
  346. }
  347. }
  348. Editor.propTypes = {
  349. rows: PropTypes.number,
  350. placeholder: PropTypes.string,
  351. showEmoji: PropTypes.bool,
  352. emojiPopoverPlacement: PropTypes.string,
  353. showUpload: PropTypes.bool,
  354. uploadPopoverPlacement: PropTypes.string,
  355. uploadOverlayClassName: PropTypes.string,
  356. multiple: PropTypes.bool,
  357. closeUploadWhenBlur: PropTypes.bool,
  358. maxUpload: PropTypes.number,
  359. value: PropTypes.string,
  360. onChange: PropTypes.func,
  361. onSubmit: PropTypes.func,
  362. beforeSubmit: PropTypes.func,
  363. btnSubmitText: PropTypes.string,
  364. btnLoading: PropTypes.bool,
  365. btnDisabled: PropTypes.bool,
  366. button: PropTypes.node,
  367. emojiToolIcon: PropTypes.node,
  368. imageToolIcon: PropTypes.node,
  369. showError: PropTypes.bool,
  370. onError: PropTypes.func,
  371. maxLength: PropTypes.number
  372. };
  373. Editor.defaultProps = {
  374. rows: 5,
  375. // placeholder: "说点什么吧",
  376. showEmoji: true,
  377. showUpload: true,
  378. multiple: true,
  379. emojiPopoverPlacement: "bottomLeft",
  380. closeUploadWhenBlur: false,
  381. uploadPopoverPlacement: "bottomLeft",
  382. uploadOverlayClassName: "",
  383. maxUpload: 1,
  384. value: "",
  385. fileList: [],
  386. // btnSubmitText: "发表",
  387. btnLoading: false,
  388. btnDisabled: false,
  389. showError: true,
  390. maxLength: 5000
  391. };
  392. export default Editor;