通用评论 vedio

index.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. const showUpload = false;
  174. let placeholder = this.props.placeholder || "快发表评论吧!";
  175. let btnSubmitText = this.props.placeholder || "发表评论";
  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. 已输入 {inputValue.length} / {maxLength} 字
  190. </div> */}
  191. <div className="comment-editor">
  192. <TextArea
  193. value={inputValue}
  194. onChange={e => this.handleChange(e.target.value)}
  195. rows={rows}
  196. placeholder={placeholder}
  197. autoFocus={autoFocus}
  198. />
  199. <div className="comment-toolbar">
  200. <div className="comment-toolbar-left">
  201. {showEmoji && (
  202. <Popover
  203. trigger="click"
  204. placement="bottomLeft"
  205. autoAdjustOverflow={false}
  206. content={
  207. <div
  208. style={{ width: 240 }}
  209. onWheel={this.handleEmojiScroll}
  210. >
  211. <Emoji
  212. onClick={this.handleClickEmoji}
  213. ref={node => {
  214. this.emoji = node;
  215. }}
  216. />
  217. </div>
  218. }
  219. overlayClassName="comment-emoji-popover"
  220. >
  221. {emojiToolIcon || (
  222. <Icon type="smile-o" className="comment-toolbar-icon" />
  223. )}
  224. </Popover>
  225. )}
  226. {showUpload ? (
  227. <Popover
  228. trigger="click"
  229. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  230. // visible={this.state.showUpload}
  231. overlayStyle={{ zIndex: 999 }}
  232. onVisibleChange={
  233. closeUploadWhenBlur
  234. ? visible => {
  235. this.handleShowUpload(visible);
  236. }
  237. : null
  238. }
  239. content={
  240. <div
  241. style={{
  242. width: 336, // 一行显示3张
  243. minHeight: 100,
  244. margin: "0 auto"
  245. }}
  246. >
  247. <Upload
  248. onChangeFileList={this.handleChangeFileList}
  249. onUpload={this.handleUpload}
  250. maxUpload={maxUpload}
  251. fileList={this.state.fileList}
  252. showError={this.props.showError}
  253. onError={this.props.onError}
  254. />
  255. <div className="clearfix" />
  256. </div>
  257. }
  258. placement="bottomLeft"
  259. title={
  260. <div style={{ margin: "5px auto" }}>
  261. <span>
  262. {intl.get("editor.uploadTip")}
  263. {maxUpload >= 2 ? (
  264. <span style={{ color: "#666", fontWeight: 400 }}>
  265. (您还能上传
  266. {maxUpload - this.state.fileList.length}
  267. 张图片)
  268. </span>
  269. ) : null}
  270. </span>
  271. {/* 因为是点击别的区域关闭,所以不用右上角的 Icon */}
  272. {/* <Icon
  273. type="close"
  274. onClick={() => this.handleShowUpload(false)}
  275. style={{
  276. float: "right",
  277. cursor: "pointer",
  278. marginTop: 4
  279. }}
  280. /> */}
  281. </div>
  282. }
  283. >
  284. {imageToolIcon ? (
  285. React.cloneElement(imageToolIcon, {
  286. onClick: () => this.handleShowUpload(true)
  287. })
  288. ) : (
  289. <Icon
  290. type="picture"
  291. className="comment-toolbar-icon"
  292. style={{ marginLeft: 10 }}
  293. onClick={() => this.handleShowUpload(true)}
  294. />
  295. )}
  296. </Popover>
  297. ) : null}
  298. </div>
  299. <div className="comment-toolbar-right">
  300. <span style={{ marginRight: 10 }}>
  301. {inputValue.length} / {maxLength}
  302. </span>
  303. {button ? (
  304. React.cloneElement(button, {
  305. onClick: button.props.onClick || handleSubmit
  306. })
  307. ) : (
  308. <Button
  309. onClick={() => this.handleSubmit()}
  310. // type="primary"
  311. className="submitBtn"
  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: 140
  358. };
  359. export default Editor;