通用评论

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