通用评论

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