通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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");
  17. require("antd/es/popover/style");
  18. require("antd/es/icon/style");
  19. require("antd/es/message/style");
  20. require("antd/es/input/style");
  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. var _props = this.props,
  96. app = _props.app,
  97. onRef = _props.onRef;
  98. if (app.currentUser && (app.currentUser.user_id > 0 || app.currentUser.id > 0)) {
  99. app.sOssSts();
  100. }
  101. if ((0, _helper.isFunction)(onRef)) {
  102. onRef(this);
  103. }
  104. }
  105. }, {
  106. key: "handleEmojiScroll",
  107. value: function handleEmojiScroll(e) {
  108. if (!this.emoji) {
  109. return;
  110. }
  111. e.preventDefault();
  112. if (e.deltaY > 0) {
  113. this.emoji.next();
  114. } else if (e.deltaY < 0) {
  115. this.emoji.prev();
  116. }
  117. }
  118. /**
  119. * 编辑器的值改变事件
  120. * 将最新的值存储到 state 中
  121. * @param {string} value 输入的值
  122. */
  123. }, {
  124. key: "handleChange",
  125. value: function handleChange(value) {
  126. this.setState({ value: value });
  127. if (this.props.onChange) {
  128. this.props.onChange(value);
  129. }
  130. }
  131. /**
  132. * 点击 emoji 的事件
  133. * 点击后,需要将改 emoji 插入到编辑器中
  134. * 插入的值为 [emoji chinese name]
  135. * 参数 emoji 即为 emoji chinese name
  136. * @param {string}} emoji emoji 的中文,如 微笑
  137. */
  138. }, {
  139. key: "handleClickEmoji",
  140. value: function handleClickEmoji(emoji) {
  141. var value = this.props.value || this.state.value;
  142. value += "[" + emoji + "]";
  143. this.handleChange(value);
  144. }
  145. /**
  146. * 监听文件列表改变事件
  147. * @param {Array} fileList 文件列表
  148. */
  149. }, {
  150. key: "handleChangeFileList",
  151. value: function handleChangeFileList(fileList) {
  152. var list = fileList;
  153. if (fileList.length > this.props.maxUpload) {
  154. list = fileList.slice(0, this.props.maxUpload);
  155. }
  156. this.props.handleChangeFileList(list);
  157. this.setState({ fileList: list });
  158. }
  159. /**
  160. * 控制上传 Popover 的显示和隐藏
  161. * @param {boolean} showUpload 是否显示上传的 Popover
  162. */
  163. }, {
  164. key: "handleShowUpload",
  165. value: function handleShowUpload(showUpload) {
  166. if (typeof showUpload === "boolean") {
  167. this.setState({ showUpload: showUpload });
  168. } else {
  169. this.setState({ showUpload: !this.state.showUpload });
  170. }
  171. }
  172. /**
  173. * 上传文件
  174. * @param {object} param 文件对象
  175. */
  176. }, {
  177. key: "handleUpload",
  178. value: function handleUpload(_ref) {
  179. var uid = _ref.uid,
  180. path = _ref.path;
  181. var fileMap = this.state.fileMap;
  182. var fileList = this.state.fileList;
  183. fileMap[uid] = path;
  184. fileList = fileList.map(function (item) {
  185. if (item.uid === uid) {
  186. item.thumbUrl = _constant.OSS_LINK + path;
  187. }
  188. return item;
  189. });
  190. this.props.handleChangeFileList(fileList);
  191. this.setState({ fileMap: fileMap, fileList: fileList });
  192. }
  193. /**
  194. * 粘贴回调
  195. */
  196. }, {
  197. key: "handlePaste",
  198. value: function handlePaste(e) {
  199. var _this2 = this;
  200. if (this.state.fileList.length >= this.props.maxUpload) {
  201. return;
  202. }
  203. var items = e.clipboardData && e.clipboardData.items;
  204. var file = null;
  205. if (items && items.length) {
  206. for (var i = 0; i < items.length; i++) {
  207. if (items[i].type.indexOf("image") !== -1) {
  208. file = items[i].getAsFile();
  209. break;
  210. }
  211. }
  212. }
  213. this.setState({
  214. uploadVisible: true
  215. });
  216. var reader = new FileReader();
  217. reader.readAsDataURL(file);
  218. reader.onloadend = function () {
  219. // DRIVER_LICENSE_PATH oss 的存储路径位置
  220. UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
  221. var fileList = _this2.state.fileList.concat({
  222. url: _constant.OSS_LINK + data.name,
  223. thumbUrl: _constant.OSS_LINK + data.name,
  224. type: file.type,
  225. uid: new Date().valueOf()
  226. });
  227. _this2.props.handleChangeFileList(fileList);
  228. _this2.setState({
  229. fileList: fileList
  230. });
  231. }).catch(function (e) {
  232. var msg = e.message || _constant.ERROR_DEFAULT;
  233. if (_this2.props.showError) {
  234. _message3.default.error(msg);
  235. }
  236. if (_this2.props.onError) {
  237. _this2.props.onError(msg, { response: e.response });
  238. }
  239. });
  240. };
  241. }
  242. /**
  243. * 提交编辑器内容
  244. * 提交功能,交给父组件来实现
  245. * 需要父组件传入 onSubmit
  246. */
  247. }, {
  248. key: "handleSubmit",
  249. value: function handleSubmit() {
  250. var _this3 = this;
  251. var maxLength = this.props.maxLength;
  252. var _state = this.state,
  253. value = _state.value,
  254. fileMap = _state.fileMap,
  255. fileList = _state.fileList;
  256. if (value.length > maxLength) {
  257. // message.error(`字数不得超过${maxLength}字`);
  258. _message3.default.error(_reactIntlUniversal2.default.get("editor.maxLength", { maxLength: maxLength }));
  259. return;
  260. }
  261. var files = [];
  262. if (fileList.length) {
  263. fileList.forEach(function (item) {
  264. if (item.url) {
  265. files.push(item.url);
  266. return;
  267. }
  268. if (!fileMap[item.uid]) {
  269. return;
  270. }
  271. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  272. });
  273. }
  274. if (this.props.beforeSubmit) {
  275. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  276. if (!(res === false)) {
  277. _this3.props.onSubmit({ text: value, files: files }, function (res, action) {
  278. _this3.resetState();
  279. if (action === "comment" && _this3.props.onCommentSuccess) {
  280. _this3.props.onCommentSuccess(res);
  281. }
  282. });
  283. }
  284. });
  285. } else {
  286. this.props.onSubmit({ text: value, files: files }, function (res, action) {
  287. _this3.resetState();
  288. if (action === "comment" && _this3.props.onCommentSuccess) {
  289. _this3.props.onCommentSuccess(res);
  290. }
  291. });
  292. }
  293. }
  294. }, {
  295. key: "resetState",
  296. value: function resetState() {
  297. this.handleChange("");
  298. this.handleChangeFileList([]);
  299. this.setState({
  300. showUpload: false,
  301. value: "",
  302. fileList: [],
  303. fileMap: {}
  304. });
  305. }
  306. }, {
  307. key: "checkDisabledSubmit",
  308. value: function checkDisabledSubmit() {
  309. var _props2 = this.props,
  310. btnDisabled = _props2.btnDisabled,
  311. value = _props2.value,
  312. fileList = _props2.fileList;
  313. if (btnDisabled) {
  314. return true;
  315. }
  316. if (value && value !== "") {
  317. return false;
  318. }
  319. if (this.state.value && this.state.value !== "") {
  320. return false;
  321. }
  322. if (fileList && fileList.length > 0) {
  323. return false;
  324. }
  325. if (this.state.fileList.length > 0) {
  326. return false;
  327. }
  328. return true;
  329. }
  330. }, {
  331. key: "render",
  332. value: function render() {
  333. var _this4 = this;
  334. var _props3 = this.props,
  335. value = _props3.value,
  336. rows = _props3.rows,
  337. showEmoji = _props3.showEmoji,
  338. showUpload = _props3.showUpload,
  339. multiple = _props3.multiple,
  340. emojiPopoverPlacement = _props3.emojiPopoverPlacement,
  341. uploadPopoverPlacement = _props3.uploadPopoverPlacement,
  342. uploadOverlayClassName = _props3.uploadOverlayClassName,
  343. fileList = _props3.fileList,
  344. maxUpload = _props3.maxUpload,
  345. btnLoading = _props3.btnLoading,
  346. button = _props3.button,
  347. emojiToolIcon = _props3.emojiToolIcon,
  348. imageToolIcon = _props3.imageToolIcon,
  349. maxLength = _props3.maxLength,
  350. autoFocus = _props3.autoFocus,
  351. app = _props3.app;
  352. var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
  353. var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
  354. var handleSubmit = this.handleSubmit;
  355. var disabledSubmit = this.checkDisabledSubmit();
  356. var inputValue = value || this.state.value;
  357. var uploadFileList = fileList || this.state.fileList;
  358. var isLogin = app.currentUser && (app.currentUser.user_id > 0 || app.currentUser.id > 0);
  359. return _react2.default.createElement(
  360. "div",
  361. { className: "comment-editor-container", onPaste: this.handlePaste },
  362. isLogin ? _react2.default.createElement(
  363. _react.Fragment,
  364. null,
  365. _react2.default.createElement("div", {
  366. className: (0, _classnames2.default)({
  367. "comment-editor-toolbar": true,
  368. "comment-editor-toolbar-error": inputValue.length > maxLength
  369. })
  370. }),
  371. _react2.default.createElement(
  372. "div",
  373. { className: "comment-editor" },
  374. _react2.default.createElement(TextArea, {
  375. value: inputValue,
  376. onChange: function onChange(e) {
  377. return _this4.handleChange(e.target.value);
  378. },
  379. rows: rows,
  380. placeholder: placeholder,
  381. autoFocus: autoFocus
  382. }),
  383. _react2.default.createElement(
  384. "div",
  385. { className: "comment-toolbar" },
  386. _react2.default.createElement(
  387. "div",
  388. { className: "comment-toolbar-left" },
  389. showEmoji && _react2.default.createElement(
  390. _popover2.default,
  391. {
  392. trigger: "click",
  393. placement: emojiPopoverPlacement,
  394. autoAdjustOverflow: false,
  395. overlayStyle: { zIndex: 9999 },
  396. content: _react2.default.createElement(
  397. "div",
  398. {
  399. style: { width: 240, height: 205 },
  400. onWheel: this.handleEmojiScroll
  401. },
  402. _react2.default.createElement(_Emoji2.default, {
  403. onClick: this.handleClickEmoji,
  404. ref: function ref(node) {
  405. _this4.emoji = node;
  406. },
  407. emojiList: this.props.app.emojiList
  408. })
  409. ),
  410. overlayClassName: "comment-emoji-popover"
  411. },
  412. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  413. ),
  414. showUpload ? _react2.default.createElement(
  415. _popover2.default,
  416. {
  417. trigger: "click"
  418. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  419. , visible: this.state.uploadVisible,
  420. placement: uploadPopoverPlacement,
  421. overlayClassName: uploadOverlayClassName,
  422. autoAdjustOverflow: false,
  423. overlayStyle: { zIndex: 9999 },
  424. onVisibleChange: function onVisibleChange(visible) {
  425. _this4.setState({
  426. uploadVisible: visible
  427. });
  428. },
  429. content: _react2.default.createElement(
  430. "div",
  431. {
  432. style: {
  433. width: 336, // 一行显示3张
  434. minHeight: 100,
  435. margin: "0 auto"
  436. }
  437. },
  438. _react2.default.createElement(_Upload2.default, {
  439. onRef: function onRef(node) {
  440. return _this4.uploadRef = node;
  441. },
  442. multiple: multiple,
  443. onChangeFileList: this.handleChangeFileList,
  444. onUpload: this.handleUpload,
  445. maxUpload: maxUpload,
  446. fileList: uploadFileList,
  447. showError: this.props.showError,
  448. onError: this.props.onError
  449. }),
  450. _react2.default.createElement("div", { className: "clearfix" })
  451. ),
  452. title: _react2.default.createElement(
  453. "div",
  454. { style: { margin: "5px auto" } },
  455. _react2.default.createElement(
  456. "span",
  457. null,
  458. _reactIntlUniversal2.default.get("editor.uploadTip"),
  459. maxUpload >= 2 ? _react2.default.createElement(
  460. "span",
  461. { style: { color: "#666", fontWeight: 400 } },
  462. _reactIntlUniversal2.default.get("editor.uploadCount", {
  463. count: maxUpload - uploadFileList.length
  464. })
  465. ) : null
  466. )
  467. )
  468. },
  469. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  470. onClick: function onClick() {
  471. return _this4.handleShowUpload(true);
  472. }
  473. }) : _react2.default.createElement(_icon2.default, {
  474. type: "picture",
  475. className: "comment-toolbar-icon",
  476. style: { marginLeft: 20 },
  477. onClick: function onClick() {
  478. return _this4.handleShowUpload(true);
  479. }
  480. })
  481. ) : null
  482. ),
  483. _react2.default.createElement(
  484. "div",
  485. { className: "comment-toolbar-right" },
  486. button ? _react2.default.cloneElement(button, {
  487. onClick: button.props.onClick || handleSubmit
  488. }) : _react2.default.createElement(
  489. _button2.default,
  490. {
  491. onClick: function onClick() {
  492. return _this4.handleSubmit();
  493. },
  494. type: "primary",
  495. loading: btnLoading,
  496. disabled: disabledSubmit
  497. },
  498. btnSubmitText
  499. )
  500. )
  501. )
  502. )
  503. ) : _react2.default.createElement(
  504. _react.Fragment,
  505. null,
  506. _react2.default.createElement(
  507. "div",
  508. { className: "comment-unlogin-tip" },
  509. _reactIntlUniversal2.default.get("comment.unlogin")
  510. ),
  511. _react2.default.createElement(
  512. "div",
  513. { className: "comment-unlogin-button" },
  514. _react2.default.createElement(
  515. _button2.default,
  516. {
  517. type: "primary",
  518. onClick: function onClick() {
  519. window.location.href = app.LOGINLINK + "?f=" + window.location.href;
  520. }
  521. },
  522. _reactIntlUniversal2.default.get("account.login")
  523. )
  524. )
  525. )
  526. );
  527. }
  528. }]);
  529. return Editor;
  530. }(_react2.default.Component);
  531. Editor.propTypes = {
  532. rows: _propTypes2.default.number,
  533. placeholder: _propTypes2.default.string,
  534. showEmoji: _propTypes2.default.bool,
  535. emojiPopoverPlacement: _propTypes2.default.string,
  536. showUpload: _propTypes2.default.bool,
  537. uploadPopoverPlacement: _propTypes2.default.string,
  538. uploadOverlayClassName: _propTypes2.default.string,
  539. multiple: _propTypes2.default.bool,
  540. closeUploadWhenBlur: _propTypes2.default.bool,
  541. maxUpload: _propTypes2.default.number,
  542. value: _propTypes2.default.string,
  543. onChange: _propTypes2.default.func,
  544. onSubmit: _propTypes2.default.func,
  545. beforeSubmit: _propTypes2.default.func,
  546. btnSubmitText: _propTypes2.default.string,
  547. btnLoading: _propTypes2.default.bool,
  548. btnDisabled: _propTypes2.default.bool,
  549. button: _propTypes2.default.node,
  550. emojiToolIcon: _propTypes2.default.node,
  551. imageToolIcon: _propTypes2.default.node,
  552. showError: _propTypes2.default.bool,
  553. onError: _propTypes2.default.func,
  554. maxLength: _propTypes2.default.number
  555. };
  556. Editor.defaultProps = {
  557. rows: 5,
  558. // placeholder: "说点什么吧",
  559. showEmoji: true,
  560. showUpload: true,
  561. multiple: true,
  562. emojiPopoverPlacement: "bottomLeft",
  563. closeUploadWhenBlur: false,
  564. uploadPopoverPlacement: "bottomLeft",
  565. uploadOverlayClassName: "",
  566. maxUpload: 1,
  567. // btnSubmitText: "发表",
  568. btnLoading: false,
  569. btnDisabled: false,
  570. showError: true,
  571. maxLength: 5000,
  572. app: {},
  573. handleChangeFileList: function handleChangeFileList() {}
  574. };
  575. exports.default = (0, _Comment2.default)(Editor);
  576. //# sourceMappingURL=index.js.map