通用评论

index.js 11KB

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