通用评论

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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: "", // 编辑器里面的值
  18. 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.state;
  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.setState({ fileList: list });
  79. }
  80. /**
  81. * 控制上传 Popover 的显示和隐藏
  82. * @param {boolean} showUpload 是否显示上传的 Popover
  83. */
  84. handleShowUpload(showUpload) {
  85. if (typeof showUpload === "boolean") {
  86. this.setState({ showUpload: showUpload });
  87. } else {
  88. this.setState({ showUpload: !this.state.showUpload });
  89. }
  90. }
  91. /**
  92. * 上传文件
  93. * @param {object} param 文件对象
  94. */
  95. handleUpload({ uid, path }) {
  96. const { fileMap } = this.state;
  97. let { fileList } = this.state;
  98. fileMap[uid] = path;
  99. fileList = fileList.map(item => {
  100. if (item.uid === uid) {
  101. item.thumbUrl = OSS_LINK + path;
  102. }
  103. return item;
  104. });
  105. this.setState({ fileMap, fileList });
  106. }
  107. /**
  108. * 提交编辑器内容
  109. * 提交功能,交给父组件来实现
  110. * 需要父组件传入 onSubmit
  111. */
  112. handleSubmit() {
  113. const { maxLength } = this.props;
  114. let { value, fileMap, fileList } = this.state;
  115. if (value.length > maxLength) {
  116. // message.error(`字数不得超过${maxLength}字`);
  117. message.error(intl.get("editor.maxLength", { maxLength }));
  118. return;
  119. }
  120. const files = [];
  121. if (fileList.length) {
  122. fileList.forEach(item => {
  123. if (!fileMap[item.uid]) {
  124. return;
  125. }
  126. files.push(`${OSS_LINK}${fileMap[item.uid]}`);
  127. });
  128. }
  129. if (this.props.beforeSubmit) {
  130. Promise.resolve(this.props.beforeSubmit({ text: value, files })).then(
  131. res => {
  132. if (!(res === false)) {
  133. this.props.onSubmit({ text: value, files }, () => {
  134. this.resetState();
  135. if (this.props.onCommentSuccess) {
  136. this.props.onCommentSuccess();
  137. }
  138. });
  139. }
  140. }
  141. );
  142. } else {
  143. this.props.onSubmit({ text: value, files }, () => {
  144. this.resetState();
  145. if (this.props.onCommentSuccess) {
  146. this.props.onCommentSuccess();
  147. }
  148. });
  149. }
  150. }
  151. resetState() {
  152. this.setState({
  153. showUpload: false,
  154. value: "",
  155. fileList: [],
  156. fileMap: {}
  157. });
  158. }
  159. render() {
  160. const {
  161. value,
  162. // placeholder,
  163. rows,
  164. showEmoji,
  165. showUpload,
  166. multiple,
  167. emojiPopoverPlacement,
  168. uploadPopoverPlacement,
  169. uploadOverlayClassName,
  170. closeUploadWhenBlur,
  171. maxUpload,
  172. // btnSubmitText,
  173. btnLoading,
  174. btnDisabled,
  175. button,
  176. emojiToolIcon,
  177. imageToolIcon,
  178. maxLength,
  179. autoFocus
  180. } = this.props;
  181. let placeholder = this.props.placeholder || intl.get("editor.placeholder");
  182. let btnSubmitText =
  183. this.props.btnSubmitText || intl.get("editor.SubmitBtn");
  184. const handleSubmit = this.handleSubmit;
  185. const disabledSubmit =
  186. btnDisabled ||
  187. (!this.props.value && !this.state.value && !this.state.fileList.length);
  188. const inputValue = value || this.state.value;
  189. return (
  190. <div className="comment-editor-container">
  191. <div
  192. className={classnames({
  193. "comment-editor-toolbar": true,
  194. "comment-editor-toolbar-error": inputValue.length > maxLength
  195. })}
  196. >
  197. {/* {intl.get("editor.alreadyEntered", {
  198. count: inputValue.length,
  199. maxLength
  200. })} */}
  201. </div>
  202. <div className="comment-editor">
  203. <TextArea
  204. value={inputValue}
  205. onChange={e => this.handleChange(e.target.value)}
  206. rows={rows}
  207. placeholder={placeholder}
  208. autoFocus={autoFocus}
  209. />
  210. <div className="comment-toolbar">
  211. <div className="comment-toolbar-left">
  212. {showEmoji && (
  213. <Popover
  214. trigger="click"
  215. placement={emojiPopoverPlacement}
  216. autoAdjustOverflow={false}
  217. overlayStyle={{ zIndex: 999 }}
  218. content={
  219. <div
  220. style={{ width: 240, height: 205 }}
  221. onWheel={this.handleEmojiScroll}
  222. >
  223. <Emoji
  224. onClick={this.handleClickEmoji}
  225. ref={node => {
  226. this.emoji = node;
  227. }}
  228. />
  229. </div>
  230. }
  231. overlayClassName="comment-emoji-popover"
  232. >
  233. {emojiToolIcon || (
  234. <Icon type="smile-o" className="comment-toolbar-icon" />
  235. )}
  236. </Popover>
  237. )}
  238. {showUpload ? (
  239. <Popover
  240. trigger="click"
  241. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  242. // visible={this.state.showUpload}
  243. placement={uploadPopoverPlacement}
  244. overlayClassName={uploadOverlayClassName}
  245. autoAdjustOverflow={false}
  246. overlayStyle={{ zIndex: 999 }}
  247. onVisibleChange={
  248. closeUploadWhenBlur
  249. ? visible => {
  250. this.handleShowUpload(visible);
  251. }
  252. : null
  253. }
  254. content={
  255. <div
  256. style={{
  257. width: 336, // 一行显示3张
  258. minHeight: 100,
  259. margin: "0 auto"
  260. }}
  261. >
  262. <Upload
  263. multiple={multiple}
  264. onChangeFileList={this.handleChangeFileList}
  265. onUpload={this.handleUpload}
  266. maxUpload={maxUpload}
  267. fileList={this.state.fileList}
  268. showError={this.props.showError}
  269. onError={this.props.onError}
  270. />
  271. <div className="clearfix" />
  272. </div>
  273. }
  274. title={
  275. <div style={{ margin: "5px auto" }}>
  276. <span>
  277. {intl.get("editor.uploadTip")}
  278. {maxUpload >= 2 ? (
  279. <span style={{ color: "#666", fontWeight: 400 }}>
  280. {intl.get("editor.uploadCount", {
  281. count: maxUpload - this.state.fileList.length
  282. })}
  283. </span>
  284. ) : null}
  285. </span>
  286. {/* 因为是点击别的区域关闭,所以不用右上角的 Icon */}
  287. {/* <Icon
  288. type="close"
  289. onClick={() => this.handleShowUpload(false)}
  290. style={{
  291. float: "right",
  292. cursor: "pointer",
  293. marginTop: 4
  294. }}
  295. /> */}
  296. </div>
  297. }
  298. >
  299. {imageToolIcon ? (
  300. React.cloneElement(imageToolIcon, {
  301. onClick: () => this.handleShowUpload(true)
  302. })
  303. ) : (
  304. <Icon
  305. type="picture"
  306. className="comment-toolbar-icon"
  307. style={{ marginLeft: 10 }}
  308. onClick={() => this.handleShowUpload(true)}
  309. />
  310. )}
  311. </Popover>
  312. ) : null}
  313. </div>
  314. <div className="comment-toolbar-right">
  315. {button ? (
  316. React.cloneElement(button, {
  317. onClick: button.props.onClick || handleSubmit
  318. })
  319. ) : (
  320. <Button
  321. onClick={() => this.handleSubmit()}
  322. type="primary"
  323. loading={btnLoading}
  324. disabled={disabledSubmit}
  325. >
  326. {btnSubmitText}
  327. </Button>
  328. )}
  329. </div>
  330. </div>
  331. </div>
  332. </div>
  333. );
  334. }
  335. }
  336. Editor.propTypes = {
  337. rows: PropTypes.number,
  338. placeholder: PropTypes.string,
  339. showEmoji: PropTypes.bool,
  340. emojiPopoverPlacement: PropTypes.string,
  341. showUpload: PropTypes.bool,
  342. uploadPopoverPlacement: PropTypes.string,
  343. uploadOverlayClassName: PropTypes.string,
  344. multiple: PropTypes.bool,
  345. closeUploadWhenBlur: PropTypes.bool,
  346. maxUpload: PropTypes.number,
  347. value: PropTypes.string,
  348. onChange: PropTypes.func,
  349. onSubmit: PropTypes.func,
  350. beforeSubmit: PropTypes.func,
  351. btnSubmitText: PropTypes.string,
  352. btnLoading: PropTypes.bool,
  353. btnDisabled: PropTypes.bool,
  354. button: PropTypes.node,
  355. emojiToolIcon: PropTypes.node,
  356. imageToolIcon: PropTypes.node,
  357. showError: PropTypes.bool,
  358. onError: PropTypes.func,
  359. maxLength: PropTypes.number
  360. };
  361. Editor.defaultProps = {
  362. rows: 5,
  363. // placeholder: "说点什么吧",
  364. showEmoji: true,
  365. showUpload: true,
  366. multiple: true,
  367. emojiPopoverPlacement: "bottomLeft",
  368. closeUploadWhenBlur: false,
  369. uploadPopoverPlacement: "bottomLeft",
  370. uploadOverlayClassName: "",
  371. maxUpload: 1,
  372. // btnSubmitText: "发表",
  373. btnLoading: false,
  374. btnDisabled: false,
  375. showError: true,
  376. maxLength: 5000
  377. };
  378. export default Editor;