通用评论 vedio

index.js 10KB

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