通用评论 vedio

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