通用评论

index.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
  349. var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
  350. var handleSubmit = this.handleSubmit;
  351. var disabledSubmit = this.checkDisabledSubmit();
  352. var inputValue = value || this.state.value;
  353. var uploadFileList = fileList || this.state.fileList;
  354. return _react2.default.createElement(
  355. "div",
  356. { className: "comment-editor-container", onPaste: this.handlePaste },
  357. _react2.default.createElement("div", {
  358. className: (0, _classnames2.default)({
  359. "comment-editor-toolbar": true,
  360. "comment-editor-toolbar-error": inputValue.length > maxLength
  361. })
  362. }),
  363. _react2.default.createElement(
  364. "div",
  365. { className: "comment-editor" },
  366. _react2.default.createElement(TextArea, {
  367. value: inputValue,
  368. onChange: function onChange(e) {
  369. return _this4.handleChange(e.target.value);
  370. },
  371. rows: rows,
  372. placeholder: placeholder,
  373. autoFocus: autoFocus
  374. }),
  375. _react2.default.createElement(
  376. "div",
  377. { className: "comment-toolbar" },
  378. _react2.default.createElement(
  379. "div",
  380. { className: "comment-toolbar-left" },
  381. showEmoji && _react2.default.createElement(
  382. _popover2.default,
  383. {
  384. trigger: "click",
  385. placement: emojiPopoverPlacement,
  386. autoAdjustOverflow: false,
  387. overlayStyle: { zIndex: 9999 },
  388. content: _react2.default.createElement(
  389. "div",
  390. {
  391. style: { width: 240, height: 205 },
  392. onWheel: this.handleEmojiScroll
  393. },
  394. _react2.default.createElement(_Emoji2.default, {
  395. onClick: this.handleClickEmoji,
  396. ref: function ref(node) {
  397. _this4.emoji = node;
  398. },
  399. emojiList: this.props.app.emojiList
  400. })
  401. ),
  402. overlayClassName: "comment-emoji-popover"
  403. },
  404. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  405. ),
  406. showUpload ? _react2.default.createElement(
  407. _popover2.default,
  408. {
  409. trigger: "click"
  410. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  411. , visible: this.state.uploadVisible,
  412. placement: uploadPopoverPlacement,
  413. overlayClassName: uploadOverlayClassName,
  414. autoAdjustOverflow: false,
  415. overlayStyle: { zIndex: 9999 },
  416. onVisibleChange: function onVisibleChange(visible) {
  417. _this4.setState({
  418. uploadVisible: visible
  419. });
  420. },
  421. content: _react2.default.createElement(
  422. "div",
  423. {
  424. style: {
  425. width: 336, // 一行显示3张
  426. minHeight: 100,
  427. margin: "0 auto"
  428. }
  429. },
  430. _react2.default.createElement(_Upload2.default, {
  431. onRef: function onRef(node) {
  432. return _this4.uploadRef = node;
  433. },
  434. multiple: multiple,
  435. onChangeFileList: this.handleChangeFileList,
  436. onUpload: this.handleUpload,
  437. maxUpload: maxUpload,
  438. fileList: uploadFileList,
  439. showError: this.props.showError,
  440. onError: this.props.onError
  441. }),
  442. _react2.default.createElement("div", { className: "clearfix" })
  443. ),
  444. title: _react2.default.createElement(
  445. "div",
  446. { style: { margin: "5px auto" } },
  447. _react2.default.createElement(
  448. "span",
  449. null,
  450. _reactIntlUniversal2.default.get("editor.uploadTip"),
  451. maxUpload >= 2 ? _react2.default.createElement(
  452. "span",
  453. { style: { color: "#666", fontWeight: 400 } },
  454. _reactIntlUniversal2.default.get("editor.uploadCount", {
  455. count: maxUpload - uploadFileList.length
  456. })
  457. ) : null
  458. )
  459. )
  460. },
  461. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  462. onClick: function onClick() {
  463. return _this4.handleShowUpload(true);
  464. }
  465. }) : _react2.default.createElement(_icon2.default, {
  466. type: "picture",
  467. className: "comment-toolbar-icon",
  468. style: { marginLeft: 20 },
  469. onClick: function onClick() {
  470. return _this4.handleShowUpload(true);
  471. }
  472. })
  473. ) : null
  474. ),
  475. _react2.default.createElement(
  476. "div",
  477. { className: "comment-toolbar-right" },
  478. button ? _react2.default.cloneElement(button, {
  479. onClick: button.props.onClick || handleSubmit
  480. }) : _react2.default.createElement(
  481. _button2.default,
  482. {
  483. onClick: function onClick() {
  484. return _this4.handleSubmit();
  485. },
  486. type: "primary",
  487. loading: btnLoading,
  488. disabled: disabledSubmit
  489. },
  490. btnSubmitText
  491. )
  492. )
  493. )
  494. )
  495. );
  496. }
  497. }]);
  498. return Editor;
  499. }(_react2.default.Component);
  500. Editor.propTypes = {
  501. rows: _propTypes2.default.number,
  502. placeholder: _propTypes2.default.string,
  503. showEmoji: _propTypes2.default.bool,
  504. emojiPopoverPlacement: _propTypes2.default.string,
  505. showUpload: _propTypes2.default.bool,
  506. uploadPopoverPlacement: _propTypes2.default.string,
  507. uploadOverlayClassName: _propTypes2.default.string,
  508. multiple: _propTypes2.default.bool,
  509. closeUploadWhenBlur: _propTypes2.default.bool,
  510. maxUpload: _propTypes2.default.number,
  511. value: _propTypes2.default.string,
  512. onChange: _propTypes2.default.func,
  513. onSubmit: _propTypes2.default.func,
  514. beforeSubmit: _propTypes2.default.func,
  515. btnSubmitText: _propTypes2.default.string,
  516. btnLoading: _propTypes2.default.bool,
  517. btnDisabled: _propTypes2.default.bool,
  518. button: _propTypes2.default.node,
  519. emojiToolIcon: _propTypes2.default.node,
  520. imageToolIcon: _propTypes2.default.node,
  521. showError: _propTypes2.default.bool,
  522. onError: _propTypes2.default.func,
  523. maxLength: _propTypes2.default.number
  524. };
  525. Editor.defaultProps = {
  526. rows: 5,
  527. // placeholder: "说点什么吧",
  528. showEmoji: true,
  529. showUpload: true,
  530. multiple: true,
  531. emojiPopoverPlacement: "bottomLeft",
  532. closeUploadWhenBlur: false,
  533. uploadPopoverPlacement: "bottomLeft",
  534. uploadOverlayClassName: "",
  535. maxUpload: 1,
  536. // btnSubmitText: "发表",
  537. btnLoading: false,
  538. btnDisabled: false,
  539. showError: true,
  540. maxLength: 5000,
  541. handleChangeFileList: function handleChangeFileList() {}
  542. };
  543. exports.default = (0, _Comment2.default)(Editor);
  544. //# sourceMappingURL=index.js.map