通用评论

index.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _button = require("antd/es/button");
  6. var _button2 = _interopRequireDefault(_button);
  7. var _popover = require("antd/es/popover");
  8. var _popover2 = _interopRequireDefault(_popover);
  9. var _icon = require("antd/es/icon");
  10. var _icon2 = _interopRequireDefault(_icon);
  11. var _message2 = require("antd/es/message");
  12. var _message3 = _interopRequireDefault(_message2);
  13. var _input = require("antd/es/input");
  14. var _input2 = _interopRequireDefault(_input);
  15. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  16. require("antd/es/button/style/css");
  17. require("antd/es/popover/style/css");
  18. require("antd/es/icon/style/css");
  19. require("antd/es/message/style/css");
  20. require("antd/es/input/style/css");
  21. var _react = require("react");
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require("prop-types");
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _classnames = require("classnames");
  26. var _classnames2 = _interopRequireDefault(_classnames);
  27. var _reactIntlUniversal = require("react-intl-universal");
  28. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  29. var _dayjs = require("dayjs");
  30. var _dayjs2 = _interopRequireDefault(_dayjs);
  31. var _shortid = require("shortid");
  32. var _shortid2 = _interopRequireDefault(_shortid);
  33. var _constant = require("../../constant");
  34. var _helper = require("../../helper");
  35. var _Upload = require("./Upload");
  36. var _Upload2 = _interopRequireDefault(_Upload);
  37. var _Emoji = require("./Emoji");
  38. var _Emoji2 = _interopRequireDefault(_Emoji);
  39. var _Comment = require("../../Comment");
  40. var _Comment2 = _interopRequireDefault(_Comment);
  41. require("./index.css");
  42. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  43. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  44. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  45. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  46. var TextArea = _input2.default.TextArea;
  47. var client = function client(oss) {
  48. return new window.OSS.Wrapper({
  49. accessKeyId: oss.access_key_id,
  50. accessKeySecret: oss.access_key_secret,
  51. stsToken: oss.security_token,
  52. endpoint: _constant.OSS_ENDPOINT, //常量,你可以自己定义
  53. bucket: _constant.OSS_BUCKET
  54. });
  55. };
  56. var uploadPath = function uploadPath(path, file) {
  57. return path + "/" + (0, _dayjs2.default)().format("YYYYMMDD") + "/" + _shortid2.default.generate() + "." + file.type.split("/")[1];
  58. };
  59. var UploadToOss = function UploadToOss(oss, path, file) {
  60. var url = uploadPath(path, file);
  61. return new Promise(function (resolve, reject) {
  62. client(oss).multipartUpload(url, file).then(function (data) {
  63. resolve(data);
  64. }).catch(function (error) {
  65. reject(error);
  66. });
  67. });
  68. };
  69. var Editor = function (_React$Component) {
  70. _inherits(Editor, _React$Component);
  71. function Editor(props) {
  72. _classCallCheck(this, Editor);
  73. var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
  74. _this.state = {
  75. showUpload: false,
  76. value: props.value || "", // 编辑器里面的值
  77. fileList: props.fileList || [], // 图片列表
  78. fileMap: {}, // 已经上传的图片路径和 uid 的映射 { uid: path }
  79. uploadVisible: false
  80. };
  81. _this.handleChange = _this.handleChange.bind(_this);
  82. _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
  83. _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
  84. _this.handleShowUpload = _this.handleShowUpload.bind(_this);
  85. _this.handleUpload = _this.handleUpload.bind(_this);
  86. _this.handleSubmit = _this.handleSubmit.bind(_this);
  87. _this.handlePaste = _this.handlePaste.bind(_this);
  88. _this.resetState = _this.resetState.bind(_this);
  89. _this.handleEmojiScroll = _this.handleEmojiScroll.bind(_this);
  90. return _this;
  91. }
  92. _createClass(Editor, [{
  93. key: "componentDidMount",
  94. value: function componentDidMount() {
  95. if (this.props.app.currentUser && this.props.app.currentUser.user_id > 0) {
  96. this.props.app.sOssSts();
  97. }
  98. if ((0, _helper.isFunction)(this.props.onRef)) {
  99. this.props.onRef(this);
  100. }
  101. }
  102. }, {
  103. key: "handleEmojiScroll",
  104. value: function handleEmojiScroll(e) {
  105. if (!this.emoji) {
  106. return;
  107. }
  108. e.preventDefault();
  109. if (e.deltaY > 0) {
  110. this.emoji.next();
  111. } else if (e.deltaY < 0) {
  112. this.emoji.prev();
  113. }
  114. }
  115. /**
  116. * 编辑器的值改变事件
  117. * 将最新的值存储到 state 中
  118. * @param {string} value 输入的值
  119. */
  120. }, {
  121. key: "handleChange",
  122. value: function handleChange(value) {
  123. this.setState({ value: value });
  124. if (this.props.onChange) {
  125. this.props.onChange(value);
  126. }
  127. }
  128. /**
  129. * 点击 emoji 的事件
  130. * 点击后,需要将改 emoji 插入到编辑器中
  131. * 插入的值为 [emoji chinese name]
  132. * 参数 emoji 即为 emoji chinese name
  133. * @param {string}} emoji emoji 的中文,如 微笑
  134. */
  135. }, {
  136. key: "handleClickEmoji",
  137. value: function handleClickEmoji(emoji) {
  138. var value = this.props.value || this.state.value;
  139. value += "[" + emoji + "]";
  140. this.handleChange(value);
  141. }
  142. /**
  143. * 监听文件列表改变事件
  144. * @param {Array} fileList 文件列表
  145. */
  146. }, {
  147. key: "handleChangeFileList",
  148. value: function handleChangeFileList(fileList) {
  149. var list = fileList;
  150. if (fileList.length > this.props.maxUpload) {
  151. list = fileList.slice(0, this.props.maxUpload);
  152. }
  153. this.props.handleChangeFileList(list);
  154. this.setState({ fileList: list });
  155. }
  156. /**
  157. * 控制上传 Popover 的显示和隐藏
  158. * @param {boolean} showUpload 是否显示上传的 Popover
  159. */
  160. }, {
  161. key: "handleShowUpload",
  162. value: function handleShowUpload(showUpload) {
  163. if (typeof showUpload === "boolean") {
  164. this.setState({ showUpload: showUpload });
  165. } else {
  166. this.setState({ showUpload: !this.state.showUpload });
  167. }
  168. }
  169. /**
  170. * 上传文件
  171. * @param {object} param 文件对象
  172. */
  173. }, {
  174. key: "handleUpload",
  175. value: function handleUpload(_ref) {
  176. var uid = _ref.uid,
  177. path = _ref.path;
  178. var fileMap = this.state.fileMap;
  179. var fileList = this.state.fileList;
  180. fileMap[uid] = path;
  181. fileList = fileList.map(function (item) {
  182. if (item.uid === uid) {
  183. item.thumbUrl = _constant.OSS_LINK + path;
  184. }
  185. return item;
  186. });
  187. this.props.handleChangeFileList(fileList);
  188. this.setState({ fileMap: fileMap, fileList: fileList });
  189. }
  190. /**
  191. * 粘贴回调
  192. */
  193. }, {
  194. key: "handlePaste",
  195. value: function handlePaste(e) {
  196. var _this2 = this;
  197. if (this.state.fileList.length >= this.props.maxUpload) {
  198. return;
  199. }
  200. var items = e.clipboardData && e.clipboardData.items;
  201. var file = null;
  202. if (items && items.length) {
  203. for (var i = 0; i < items.length; i++) {
  204. if (items[i].type.indexOf("image") !== -1) {
  205. file = items[i].getAsFile();
  206. break;
  207. }
  208. }
  209. }
  210. this.setState({
  211. uploadVisible: true
  212. });
  213. var reader = new FileReader();
  214. reader.readAsDataURL(file);
  215. reader.onloadend = function () {
  216. // DRIVER_LICENSE_PATH oss 的存储路径位置
  217. UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
  218. var fileList = _this2.state.fileList.concat({
  219. url: _constant.OSS_LINK + data.name,
  220. thumbUrl: _constant.OSS_LINK + data.name,
  221. type: file.type,
  222. uid: new Date().valueOf()
  223. });
  224. _this2.props.handleChangeFileList(fileList);
  225. _this2.setState({
  226. fileList: fileList
  227. });
  228. }).catch(function (e) {
  229. var msg = e.message || _constant.ERROR_DEFAULT;
  230. if (_this2.props.showError) {
  231. _message3.default.error(msg);
  232. }
  233. if (_this2.props.onError) {
  234. _this2.props.onError(msg, { response: e.response });
  235. }
  236. });
  237. };
  238. }
  239. /**
  240. * 提交编辑器内容
  241. * 提交功能,交给父组件来实现
  242. * 需要父组件传入 onSubmit
  243. */
  244. }, {
  245. key: "handleSubmit",
  246. value: function handleSubmit() {
  247. var _this3 = this;
  248. var maxLength = this.props.maxLength;
  249. var _state = this.state,
  250. value = _state.value,
  251. fileMap = _state.fileMap,
  252. fileList = _state.fileList;
  253. if (value.length > maxLength) {
  254. // message.error(`字数不得超过${maxLength}字`);
  255. _message3.default.error(_reactIntlUniversal2.default.get("editor.maxLength", { maxLength: maxLength }));
  256. return;
  257. }
  258. var files = [];
  259. if (fileList.length) {
  260. fileList.forEach(function (item) {
  261. if (item.url) {
  262. files.push(item.url);
  263. return;
  264. }
  265. if (!fileMap[item.uid]) {
  266. return;
  267. }
  268. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  269. });
  270. }
  271. if (this.props.beforeSubmit) {
  272. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  273. if (!(res === false)) {
  274. _this3.props.onSubmit({ text: value, files: files }, function (res, action) {
  275. _this3.resetState();
  276. if (action === "comment" && _this3.props.onCommentSuccess) {
  277. _this3.props.onCommentSuccess(res);
  278. }
  279. });
  280. }
  281. });
  282. } else {
  283. this.props.onSubmit({ text: value, files: files }, function (res, action) {
  284. _this3.resetState();
  285. if (action === "comment" && _this3.props.onCommentSuccess) {
  286. _this3.props.onCommentSuccess(res);
  287. }
  288. });
  289. }
  290. }
  291. }, {
  292. key: "resetState",
  293. value: function resetState() {
  294. this.handleChange("");
  295. this.handleChangeFileList([]);
  296. this.setState({
  297. showUpload: false,
  298. value: "",
  299. fileList: [],
  300. fileMap: {}
  301. });
  302. }
  303. }, {
  304. key: "checkDisabledSubmit",
  305. value: function checkDisabledSubmit() {
  306. var _props = this.props,
  307. btnDisabled = _props.btnDisabled,
  308. value = _props.value,
  309. fileList = _props.fileList;
  310. if (btnDisabled) {
  311. return true;
  312. }
  313. if (value && value !== "") {
  314. return false;
  315. }
  316. if (this.state.value && this.state.value !== "") {
  317. return false;
  318. }
  319. if (fileList && fileList.length > 0) {
  320. return false;
  321. }
  322. if (this.state.fileList.length > 0) {
  323. return false;
  324. }
  325. return true;
  326. }
  327. }, {
  328. key: "render",
  329. value: function render() {
  330. var _this4 = this;
  331. var _props2 = this.props,
  332. value = _props2.value,
  333. rows = _props2.rows,
  334. showEmoji = _props2.showEmoji,
  335. showUpload = _props2.showUpload,
  336. multiple = _props2.multiple,
  337. emojiPopoverPlacement = _props2.emojiPopoverPlacement,
  338. uploadPopoverPlacement = _props2.uploadPopoverPlacement,
  339. uploadOverlayClassName = _props2.uploadOverlayClassName,
  340. fileList = _props2.fileList,
  341. maxUpload = _props2.maxUpload,
  342. btnLoading = _props2.btnLoading,
  343. button = _props2.button,
  344. emojiToolIcon = _props2.emojiToolIcon,
  345. imageToolIcon = _props2.imageToolIcon,
  346. maxLength = _props2.maxLength,
  347. autoFocus = _props2.autoFocus,
  348. app = _props2.app;
  349. var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
  350. var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
  351. var handleSubmit = this.handleSubmit;
  352. var disabledSubmit = this.checkDisabledSubmit();
  353. var inputValue = value || this.state.value;
  354. var uploadFileList = fileList || this.state.fileList;
  355. var isLogin = app.currentUser && app.currentUser.user_id > 0;
  356. return _react2.default.createElement(
  357. "div",
  358. { className: "comment-editor-container", onPaste: this.handlePaste },
  359. isLogin ? _react2.default.createElement(
  360. _react.Fragment,
  361. null,
  362. _react2.default.createElement("div", {
  363. className: (0, _classnames2.default)({
  364. "comment-editor-toolbar": true,
  365. "comment-editor-toolbar-error": inputValue.length > maxLength
  366. })
  367. }),
  368. _react2.default.createElement(
  369. "div",
  370. { className: "comment-editor" },
  371. _react2.default.createElement(TextArea, {
  372. value: inputValue,
  373. onChange: function onChange(e) {
  374. return _this4.handleChange(e.target.value);
  375. },
  376. rows: rows,
  377. placeholder: placeholder,
  378. autoFocus: autoFocus
  379. }),
  380. _react2.default.createElement(
  381. "div",
  382. { className: "comment-toolbar" },
  383. _react2.default.createElement(
  384. "div",
  385. { className: "comment-toolbar-left" },
  386. showEmoji && _react2.default.createElement(
  387. _popover2.default,
  388. {
  389. trigger: "click",
  390. placement: emojiPopoverPlacement,
  391. autoAdjustOverflow: false,
  392. overlayStyle: { zIndex: 9999 },
  393. content: _react2.default.createElement(
  394. "div",
  395. {
  396. style: { width: 240, height: 205 },
  397. onWheel: this.handleEmojiScroll
  398. },
  399. _react2.default.createElement(_Emoji2.default, {
  400. onClick: this.handleClickEmoji,
  401. ref: function ref(node) {
  402. _this4.emoji = node;
  403. },
  404. emojiList: this.props.app.emojiList
  405. })
  406. ),
  407. overlayClassName: "comment-emoji-popover"
  408. },
  409. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  410. ),
  411. showUpload ? _react2.default.createElement(
  412. _popover2.default,
  413. {
  414. trigger: "click"
  415. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  416. , visible: this.state.uploadVisible,
  417. placement: uploadPopoverPlacement,
  418. overlayClassName: uploadOverlayClassName,
  419. autoAdjustOverflow: false,
  420. overlayStyle: { zIndex: 9999 },
  421. onVisibleChange: function onVisibleChange(visible) {
  422. _this4.setState({
  423. uploadVisible: visible
  424. });
  425. },
  426. content: _react2.default.createElement(
  427. "div",
  428. {
  429. style: {
  430. width: 336, // 一行显示3张
  431. minHeight: 100,
  432. margin: "0 auto"
  433. }
  434. },
  435. _react2.default.createElement(_Upload2.default, {
  436. onRef: function onRef(node) {
  437. return _this4.uploadRef = node;
  438. },
  439. multiple: multiple,
  440. onChangeFileList: this.handleChangeFileList,
  441. onUpload: this.handleUpload,
  442. maxUpload: maxUpload,
  443. fileList: uploadFileList,
  444. showError: this.props.showError,
  445. onError: this.props.onError
  446. }),
  447. _react2.default.createElement("div", { className: "clearfix" })
  448. ),
  449. title: _react2.default.createElement(
  450. "div",
  451. { style: { margin: "5px auto" } },
  452. _react2.default.createElement(
  453. "span",
  454. null,
  455. _reactIntlUniversal2.default.get("editor.uploadTip"),
  456. maxUpload >= 2 ? _react2.default.createElement(
  457. "span",
  458. { style: { color: "#666", fontWeight: 400 } },
  459. _reactIntlUniversal2.default.get("editor.uploadCount", {
  460. count: maxUpload - uploadFileList.length
  461. })
  462. ) : null
  463. )
  464. )
  465. },
  466. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  467. onClick: function onClick() {
  468. return _this4.handleShowUpload(true);
  469. }
  470. }) : _react2.default.createElement(_icon2.default, {
  471. type: "picture",
  472. className: "comment-toolbar-icon",
  473. style: { marginLeft: 20 },
  474. onClick: function onClick() {
  475. return _this4.handleShowUpload(true);
  476. }
  477. })
  478. ) : null
  479. ),
  480. _react2.default.createElement(
  481. "div",
  482. { className: "comment-toolbar-right" },
  483. button ? _react2.default.cloneElement(button, {
  484. onClick: button.props.onClick || handleSubmit
  485. }) : _react2.default.createElement(
  486. _button2.default,
  487. {
  488. onClick: function onClick() {
  489. return _this4.handleSubmit();
  490. },
  491. type: "primary",
  492. loading: btnLoading,
  493. disabled: disabledSubmit
  494. },
  495. btnSubmitText
  496. )
  497. )
  498. )
  499. )
  500. ) : _react2.default.createElement(
  501. _react.Fragment,
  502. null,
  503. _react2.default.createElement(
  504. "div",
  505. { className: "comment-unlogin-tip" },
  506. _reactIntlUniversal2.default.get("comment.unlogin")
  507. ),
  508. _react2.default.createElement(
  509. "div",
  510. { className: "comment-unlogin-button" },
  511. _react2.default.createElement(
  512. _button2.default,
  513. {
  514. type: "primary",
  515. onClick: function onClick() {
  516. window.location.href = app.LOGINLINK + "?f=" + window.location.href;
  517. }
  518. },
  519. _reactIntlUniversal2.default.get("account.login")
  520. )
  521. )
  522. )
  523. );
  524. }
  525. }]);
  526. return Editor;
  527. }(_react2.default.Component);
  528. Editor.propTypes = {
  529. rows: _propTypes2.default.number,
  530. placeholder: _propTypes2.default.string,
  531. showEmoji: _propTypes2.default.bool,
  532. emojiPopoverPlacement: _propTypes2.default.string,
  533. showUpload: _propTypes2.default.bool,
  534. uploadPopoverPlacement: _propTypes2.default.string,
  535. uploadOverlayClassName: _propTypes2.default.string,
  536. multiple: _propTypes2.default.bool,
  537. closeUploadWhenBlur: _propTypes2.default.bool,
  538. maxUpload: _propTypes2.default.number,
  539. value: _propTypes2.default.string,
  540. onChange: _propTypes2.default.func,
  541. onSubmit: _propTypes2.default.func,
  542. beforeSubmit: _propTypes2.default.func,
  543. btnSubmitText: _propTypes2.default.string,
  544. btnLoading: _propTypes2.default.bool,
  545. btnDisabled: _propTypes2.default.bool,
  546. button: _propTypes2.default.node,
  547. emojiToolIcon: _propTypes2.default.node,
  548. imageToolIcon: _propTypes2.default.node,
  549. showError: _propTypes2.default.bool,
  550. onError: _propTypes2.default.func,
  551. maxLength: _propTypes2.default.number
  552. };
  553. Editor.defaultProps = {
  554. rows: 5,
  555. // placeholder: "说点什么吧",
  556. showEmoji: true,
  557. showUpload: true,
  558. multiple: true,
  559. emojiPopoverPlacement: "bottomLeft",
  560. closeUploadWhenBlur: false,
  561. uploadPopoverPlacement: "bottomLeft",
  562. uploadOverlayClassName: "",
  563. maxUpload: 1,
  564. // btnSubmitText: "发表",
  565. btnLoading: false,
  566. btnDisabled: false,
  567. showError: true,
  568. maxLength: 5000,
  569. handleChangeFileList: function handleChangeFileList() {}
  570. };
  571. exports.default = (0, _Comment2.default)(Editor);
  572. //# sourceMappingURL=index.js.map