通用评论 vedio

index.js 8.8KB

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