通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. if (file === null) return;
  213. }
  214. this.setState({
  215. uploadVisible: true
  216. });
  217. var reader = new FileReader();
  218. reader.readAsDataURL(file);
  219. reader.onloadend = function () {
  220. // DRIVER_LICENSE_PATH oss 的存储路径位置
  221. UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
  222. var fileList = _this2.state.fileList.concat({
  223. url: _constant.OSS_LINK + data.name,
  224. thumbUrl: _constant.OSS_LINK + data.name,
  225. type: file.type,
  226. uid: new Date().valueOf()
  227. });
  228. _this2.props.handleChangeFileList(fileList);
  229. _this2.setState({
  230. fileList: fileList
  231. });
  232. }).catch(function (e) {
  233. var msg = e.message || _constant.ERROR_DEFAULT;
  234. if (_this2.props.showError) {
  235. _message3.default.error(msg);
  236. }
  237. if (_this2.props.onError) {
  238. _this2.props.onError(msg, { response: e.response });
  239. }
  240. });
  241. };
  242. }
  243. /**
  244. * 提交编辑器内容
  245. * 提交功能,交给父组件来实现
  246. * 需要父组件传入 onSubmit
  247. */
  248. }, {
  249. key: "handleSubmit",
  250. value: function handleSubmit() {
  251. var _this3 = this;
  252. var maxLength = this.props.maxLength;
  253. var _state = this.state,
  254. value = _state.value,
  255. fileMap = _state.fileMap,
  256. fileList = _state.fileList;
  257. if (value.length > maxLength) {
  258. // message.error(`字数不得超过${maxLength}字`);
  259. _message3.default.error(_reactIntlUniversal2.default.get("editor.maxLength", { maxLength: maxLength }));
  260. return;
  261. }
  262. var files = [];
  263. if (fileList.length) {
  264. fileList.forEach(function (item) {
  265. if (item.url) {
  266. files.push(item.url);
  267. return;
  268. }
  269. if (!fileMap[item.uid]) {
  270. return;
  271. }
  272. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  273. });
  274. }
  275. if (this.props.beforeSubmit) {
  276. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  277. if (!(res === false)) {
  278. _this3.props.onSubmit({ text: value, files: files }, function (res, action) {
  279. _this3.resetState();
  280. if (action === "comment" && _this3.props.onCommentSuccess) {
  281. _this3.props.onCommentSuccess(res);
  282. }
  283. });
  284. }
  285. });
  286. } else {
  287. this.props.onSubmit({ text: value, files: files }, function (res, action) {
  288. _this3.resetState();
  289. if (action === "comment" && _this3.props.onCommentSuccess) {
  290. _this3.props.onCommentSuccess(res);
  291. }
  292. });
  293. }
  294. }
  295. }, {
  296. key: "resetState",
  297. value: function resetState() {
  298. this.handleChange("");
  299. this.handleChangeFileList([]);
  300. this.setState({
  301. showUpload: false,
  302. value: "",
  303. fileList: [],
  304. fileMap: {}
  305. });
  306. }
  307. }, {
  308. key: "checkDisabledSubmit",
  309. value: function checkDisabledSubmit() {
  310. var _props2 = this.props,
  311. btnDisabled = _props2.btnDisabled,
  312. value = _props2.value,
  313. fileList = _props2.fileList;
  314. if (btnDisabled) {
  315. return true;
  316. }
  317. if (value && value !== "") {
  318. return false;
  319. }
  320. if (this.state.value && this.state.value !== "") {
  321. return false;
  322. }
  323. if (fileList && fileList.length > 0) {
  324. return false;
  325. }
  326. if (this.state.fileList.length > 0) {
  327. return false;
  328. }
  329. return true;
  330. }
  331. }, {
  332. key: "render",
  333. value: function render() {
  334. var _this4 = this;
  335. var _props3 = this.props,
  336. value = _props3.value,
  337. rows = _props3.rows,
  338. showEmoji = _props3.showEmoji,
  339. showUpload = _props3.showUpload,
  340. multiple = _props3.multiple,
  341. emojiPopoverPlacement = _props3.emojiPopoverPlacement,
  342. uploadPopoverPlacement = _props3.uploadPopoverPlacement,
  343. uploadOverlayClassName = _props3.uploadOverlayClassName,
  344. fileList = _props3.fileList,
  345. maxUpload = _props3.maxUpload,
  346. btnLoading = _props3.btnLoading,
  347. button = _props3.button,
  348. emojiToolIcon = _props3.emojiToolIcon,
  349. imageToolIcon = _props3.imageToolIcon,
  350. maxLength = _props3.maxLength,
  351. autoFocus = _props3.autoFocus,
  352. app = _props3.app;
  353. var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
  354. var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
  355. var handleSubmit = this.handleSubmit;
  356. var disabledSubmit = this.checkDisabledSubmit();
  357. var inputValue = value || this.state.value;
  358. var uploadFileList = fileList || this.state.fileList;
  359. var isLogin = app.currentUser && (app.currentUser.user_id > 0 || app.currentUser.id > 0);
  360. return _react2.default.createElement(
  361. "div",
  362. { className: "comment-editor-container", onPaste: this.handlePaste },
  363. isLogin ? _react2.default.createElement(
  364. _react.Fragment,
  365. null,
  366. _react2.default.createElement("div", {
  367. className: (0, _classnames2.default)({
  368. "comment-editor-toolbar": true,
  369. "comment-editor-toolbar-error": inputValue.length > maxLength
  370. })
  371. }),
  372. _react2.default.createElement(
  373. "div",
  374. { className: "comment-editor" },
  375. _react2.default.createElement(TextArea, {
  376. value: inputValue,
  377. onChange: function onChange(e) {
  378. return _this4.handleChange(e.target.value);
  379. },
  380. rows: rows,
  381. placeholder: placeholder,
  382. autoFocus: autoFocus
  383. }),
  384. _react2.default.createElement(
  385. "div",
  386. { className: "comment-toolbar" },
  387. _react2.default.createElement(
  388. "div",
  389. { className: "comment-toolbar-left" },
  390. showEmoji && _react2.default.createElement(
  391. _popover2.default,
  392. {
  393. trigger: "click",
  394. placement: emojiPopoverPlacement,
  395. autoAdjustOverflow: false,
  396. overlayStyle: { zIndex: 9999 },
  397. content: _react2.default.createElement(
  398. "div",
  399. {
  400. style: { width: 240, height: 205 },
  401. onWheel: this.handleEmojiScroll
  402. },
  403. _react2.default.createElement(_Emoji2.default, {
  404. onClick: this.handleClickEmoji,
  405. ref: function ref(node) {
  406. _this4.emoji = node;
  407. },
  408. emojiList: this.props.app.emojiList
  409. })
  410. ),
  411. overlayClassName: "comment-emoji-popover"
  412. },
  413. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  414. ),
  415. showUpload ? _react2.default.createElement(
  416. _popover2.default,
  417. {
  418. trigger: "click"
  419. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  420. , visible: this.state.uploadVisible,
  421. placement: uploadPopoverPlacement,
  422. overlayClassName: uploadOverlayClassName,
  423. autoAdjustOverflow: false,
  424. overlayStyle: { zIndex: 9999 },
  425. onVisibleChange: function onVisibleChange(visible) {
  426. _this4.setState({
  427. uploadVisible: visible
  428. });
  429. },
  430. content: _react2.default.createElement(
  431. "div",
  432. {
  433. style: {
  434. width: 336, // 一行显示3张
  435. minHeight: 100,
  436. margin: "0 auto"
  437. }
  438. },
  439. _react2.default.createElement(_Upload2.default, {
  440. onRef: function onRef(node) {
  441. return _this4.uploadRef = node;
  442. },
  443. multiple: multiple,
  444. onChangeFileList: this.handleChangeFileList,
  445. onUpload: this.handleUpload,
  446. maxUpload: maxUpload,
  447. fileList: uploadFileList,
  448. showError: this.props.showError,
  449. onError: this.props.onError
  450. }),
  451. _react2.default.createElement("div", { className: "clearfix" })
  452. ),
  453. title: _react2.default.createElement(
  454. "div",
  455. { style: { margin: "5px auto" } },
  456. _react2.default.createElement(
  457. "span",
  458. null,
  459. _reactIntlUniversal2.default.get("editor.uploadTip"),
  460. maxUpload >= 2 ? _react2.default.createElement(
  461. "span",
  462. { style: { color: "#666", fontWeight: 400 } },
  463. _reactIntlUniversal2.default.get("editor.uploadCount", {
  464. count: maxUpload - uploadFileList.length
  465. })
  466. ) : null
  467. )
  468. )
  469. },
  470. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  471. onClick: function onClick() {
  472. return _this4.handleShowUpload(true);
  473. }
  474. }) : _react2.default.createElement(_icon2.default, {
  475. type: "picture",
  476. className: "comment-toolbar-icon",
  477. style: { marginLeft: 20 },
  478. onClick: function onClick() {
  479. return _this4.handleShowUpload(true);
  480. }
  481. })
  482. ) : null
  483. ),
  484. _react2.default.createElement(
  485. "div",
  486. { className: "comment-toolbar-right" },
  487. button ? _react2.default.cloneElement(button, {
  488. onClick: button.props.onClick || handleSubmit
  489. }) : _react2.default.createElement(
  490. _button2.default,
  491. {
  492. onClick: function onClick() {
  493. return _this4.handleSubmit();
  494. },
  495. type: "primary",
  496. loading: btnLoading,
  497. disabled: disabledSubmit
  498. },
  499. btnSubmitText
  500. )
  501. )
  502. )
  503. )
  504. ) : _react2.default.createElement(
  505. _react.Fragment,
  506. null,
  507. _react2.default.createElement(
  508. "div",
  509. { className: "comment-unlogin-tip" },
  510. _reactIntlUniversal2.default.get("comment.unlogin")
  511. ),
  512. _react2.default.createElement(
  513. "div",
  514. { className: "comment-unlogin-button" },
  515. _react2.default.createElement(
  516. _button2.default,
  517. {
  518. type: "primary",
  519. onClick: function onClick() {
  520. window.location.href = app.LOGINLINK + "?f=" + window.location.href;
  521. }
  522. },
  523. _reactIntlUniversal2.default.get("account.login")
  524. )
  525. )
  526. )
  527. );
  528. }
  529. }]);
  530. return Editor;
  531. }(_react2.default.Component);
  532. Editor.propTypes = {
  533. rows: _propTypes2.default.number,
  534. placeholder: _propTypes2.default.string,
  535. showEmoji: _propTypes2.default.bool,
  536. emojiPopoverPlacement: _propTypes2.default.string,
  537. showUpload: _propTypes2.default.bool,
  538. uploadPopoverPlacement: _propTypes2.default.string,
  539. uploadOverlayClassName: _propTypes2.default.string,
  540. multiple: _propTypes2.default.bool,
  541. closeUploadWhenBlur: _propTypes2.default.bool,
  542. maxUpload: _propTypes2.default.number,
  543. value: _propTypes2.default.string,
  544. onChange: _propTypes2.default.func,
  545. onSubmit: _propTypes2.default.func,
  546. beforeSubmit: _propTypes2.default.func,
  547. btnSubmitText: _propTypes2.default.string,
  548. btnLoading: _propTypes2.default.bool,
  549. btnDisabled: _propTypes2.default.bool,
  550. button: _propTypes2.default.node,
  551. emojiToolIcon: _propTypes2.default.node,
  552. imageToolIcon: _propTypes2.default.node,
  553. showError: _propTypes2.default.bool,
  554. onError: _propTypes2.default.func,
  555. maxLength: _propTypes2.default.number
  556. };
  557. Editor.defaultProps = {
  558. rows: 5,
  559. // placeholder: "说点什么吧",
  560. showEmoji: true,
  561. showUpload: true,
  562. multiple: true,
  563. emojiPopoverPlacement: "bottomLeft",
  564. closeUploadWhenBlur: false,
  565. uploadPopoverPlacement: "bottomLeft",
  566. uploadOverlayClassName: "",
  567. maxUpload: 1,
  568. // btnSubmitText: "发表",
  569. btnLoading: false,
  570. btnDisabled: false,
  571. showError: true,
  572. maxLength: 5000,
  573. app: {},
  574. handleChangeFileList: function handleChangeFileList() {}
  575. };
  576. exports.default = (0, _Comment2.default)(Editor);
  577. //# sourceMappingURL=index.js.map