通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 _constant = require("../../constant");
  28. var _helper = require("../../helper");
  29. var _Upload = require("./Upload");
  30. var _Upload2 = _interopRequireDefault(_Upload);
  31. var _Emoji = require("./Emoji");
  32. var _Emoji2 = _interopRequireDefault(_Emoji);
  33. require("./index.css");
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  35. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  36. 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; }
  37. 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; }
  38. var TextArea = _input2.default.TextArea;
  39. var Editor = function (_React$Component) {
  40. _inherits(Editor, _React$Component);
  41. function Editor(props) {
  42. _classCallCheck(this, Editor);
  43. var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
  44. _this.state = {
  45. showUpload: false,
  46. value: "", // 编辑器里面的值
  47. fileList: [], // 图片列表
  48. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  49. };
  50. _this.handleChange = _this.handleChange.bind(_this);
  51. _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
  52. _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
  53. _this.handleShowUpload = _this.handleShowUpload.bind(_this);
  54. _this.handleUpload = _this.handleUpload.bind(_this);
  55. _this.handleSubmit = _this.handleSubmit.bind(_this);
  56. _this.resetState = _this.resetState.bind(_this);
  57. _this.handleEmojiScroll = _this.handleEmojiScroll.bind(_this);
  58. return _this;
  59. }
  60. _createClass(Editor, [{
  61. key: "componentDidMount",
  62. value: function componentDidMount() {
  63. if ((0, _helper.isFunction)(this.props.onRef)) {
  64. this.props.onRef(this);
  65. }
  66. }
  67. }, {
  68. key: "handleEmojiScroll",
  69. value: function handleEmojiScroll(e) {
  70. if (!this.emoji) {
  71. return;
  72. }
  73. e.preventDefault();
  74. if (e.deltaY > 0) {
  75. this.emoji.next();
  76. } else if (e.deltaY < 0) {
  77. this.emoji.prev();
  78. }
  79. }
  80. /**
  81. * 编辑器的值改变事件
  82. * 将最新的值存储到 state 中
  83. * @param {string} value 输入的值
  84. */
  85. }, {
  86. key: "handleChange",
  87. value: function handleChange(value) {
  88. this.setState({ value: value });
  89. if (this.props.onChange) {
  90. this.props.onChange(value);
  91. }
  92. }
  93. /**
  94. * 点击 emoji 的事件
  95. * 点击后,需要将改 emoji 插入到编辑器中
  96. * 插入的值为 [emoji chinese name]
  97. * 参数 emoji 即为 emoji chinese name
  98. * @param {string}} emoji emoji 的中文,如 微笑
  99. */
  100. }, {
  101. key: "handleClickEmoji",
  102. value: function handleClickEmoji(emoji) {
  103. var value = this.state.value;
  104. value += "[" + emoji + "]";
  105. this.handleChange(value);
  106. }
  107. /**
  108. * 监听文件列表改变事件
  109. * @param {Array} fileList 文件列表
  110. */
  111. }, {
  112. key: "handleChangeFileList",
  113. value: function handleChangeFileList(fileList) {
  114. this.setState({ fileList: fileList });
  115. }
  116. /**
  117. * 控制上传 Popover 的显示和隐藏
  118. * @param {boolean} showUpload 是否显示上传的 Popover
  119. */
  120. }, {
  121. key: "handleShowUpload",
  122. value: function handleShowUpload(showUpload) {
  123. if (typeof showUpload === "boolean") {
  124. this.setState({ showUpload: showUpload });
  125. } else {
  126. this.setState({ showUpload: !this.state.showUpload });
  127. }
  128. }
  129. /**
  130. * 上传文件
  131. * @param {object} param 文件对象
  132. */
  133. }, {
  134. key: "handleUpload",
  135. value: function handleUpload(_ref) {
  136. var uid = _ref.uid,
  137. path = _ref.path;
  138. var fileMap = this.state.fileMap;
  139. var fileList = this.state.fileList;
  140. fileMap[uid] = path;
  141. fileList = fileList.map(function (item) {
  142. if (item.uid === uid) {
  143. item.thumbUrl = _constant.OSS_LINK + path;
  144. }
  145. return item;
  146. });
  147. this.setState({ fileMap: fileMap, fileList: fileList });
  148. }
  149. /**
  150. * 提交编辑器内容
  151. * 提交功能,交给父组件来实现
  152. * 需要父组件传入 onSubmit
  153. */
  154. }, {
  155. key: "handleSubmit",
  156. value: function handleSubmit() {
  157. var _this2 = this;
  158. var maxLength = this.props.maxLength;
  159. var _state = this.state,
  160. value = _state.value,
  161. fileMap = _state.fileMap,
  162. fileList = _state.fileList;
  163. if (value.length > maxLength) {
  164. _message3.default.error("\u5B57\u6570\u4E0D\u5F97\u8D85\u8FC7" + maxLength + "\u5B57");
  165. return;
  166. }
  167. var files = [];
  168. if (fileList.length) {
  169. fileList.forEach(function (item) {
  170. if (!fileMap[item.uid]) {
  171. return;
  172. }
  173. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  174. });
  175. }
  176. if (this.props.beforeSubmit) {
  177. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  178. if (!(res === false)) {
  179. _this2.props.onSubmit({ text: value, files: files }, function () {
  180. _this2.resetState();
  181. if (_this2.props.onCommentSuccess) {
  182. _this2.props.onCommentSuccess();
  183. }
  184. });
  185. }
  186. });
  187. } else {
  188. this.props.onSubmit({ text: value, files: files }, function () {
  189. _this2.resetState();
  190. if (_this2.props.onCommentSuccess) {
  191. _this2.props.onCommentSuccess();
  192. }
  193. });
  194. }
  195. }
  196. }, {
  197. key: "resetState",
  198. value: function resetState() {
  199. this.setState({
  200. showUpload: false,
  201. value: "",
  202. fileList: [],
  203. fileMap: {}
  204. });
  205. }
  206. }, {
  207. key: "render",
  208. value: function render() {
  209. var _this3 = this;
  210. var _props = this.props,
  211. value = _props.value,
  212. placeholder = _props.placeholder,
  213. rows = _props.rows,
  214. showEmoji = _props.showEmoji,
  215. showUpload = _props.showUpload,
  216. closeUploadWhenBlur = _props.closeUploadWhenBlur,
  217. maxUpload = _props.maxUpload,
  218. btnSubmitText = _props.btnSubmitText,
  219. btnLoading = _props.btnLoading,
  220. btnDisabled = _props.btnDisabled,
  221. button = _props.button,
  222. emojiToolIcon = _props.emojiToolIcon,
  223. imageToolIcon = _props.imageToolIcon,
  224. maxLength = _props.maxLength,
  225. autoFocus = _props.autoFocus;
  226. var handleSubmit = this.handleSubmit;
  227. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
  228. var inputValue = value || this.state.value;
  229. return _react2.default.createElement(
  230. "div",
  231. { className: "comment-editor-container" },
  232. _react2.default.createElement(
  233. "div",
  234. {
  235. className: (0, _classnames2.default)({
  236. "comment-editor-toolbar": true,
  237. "comment-editor-toolbar-error": inputValue.length > maxLength
  238. })
  239. },
  240. "\u5DF2\u8F93\u5165 ",
  241. inputValue.length,
  242. " / ",
  243. maxLength,
  244. " \u5B57"
  245. ),
  246. _react2.default.createElement(
  247. "div",
  248. { className: "comment-editor" },
  249. _react2.default.createElement(TextArea, {
  250. value: inputValue,
  251. onChange: function onChange(e) {
  252. return _this3.handleChange(e.target.value);
  253. },
  254. rows: rows,
  255. placeholder: placeholder,
  256. autoFocus: autoFocus
  257. }),
  258. _react2.default.createElement(
  259. "div",
  260. { className: "comment-toolbar" },
  261. _react2.default.createElement(
  262. "div",
  263. { className: "comment-toolbar-left" },
  264. showEmoji && _react2.default.createElement(
  265. _popover2.default,
  266. {
  267. trigger: "click",
  268. placement: "bottomLeft",
  269. autoAdjustOverflow: false,
  270. content: _react2.default.createElement(
  271. "div",
  272. { style: { width: 240 }, onWheel: this.handleEmojiScroll },
  273. _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji, ref: function ref(node) {
  274. _this3.emoji = node;
  275. } })
  276. ),
  277. overlayClassName: "comment-emoji-popover"
  278. },
  279. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  280. ),
  281. showUpload ? _react2.default.createElement(
  282. _popover2.default,
  283. {
  284. trigger: "click",
  285. visible: this.state.showUpload,
  286. overlayStyle: { zIndex: 999 },
  287. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  288. _this3.handleShowUpload(visible);
  289. } : null,
  290. content: _react2.default.createElement(
  291. "div",
  292. {
  293. style: {
  294. width: 336, // 一行显示3张
  295. minHeight: 100,
  296. margin: "0 auto"
  297. }
  298. },
  299. _react2.default.createElement(_Upload2.default, {
  300. onChangeFileList: this.handleChangeFileList,
  301. onUpload: this.handleUpload,
  302. maxUpload: maxUpload,
  303. fileList: this.state.fileList,
  304. showError: this.props.showError,
  305. onError: this.props.onError
  306. }),
  307. _react2.default.createElement("div", { className: "clearfix" })
  308. ),
  309. placement: "bottomLeft",
  310. title: _react2.default.createElement(
  311. "div",
  312. { style: { margin: "5px auto" } },
  313. _react2.default.createElement(
  314. "span",
  315. null,
  316. "\u4E0A\u4F20\u56FE\u7247",
  317. maxUpload >= 2 ? _react2.default.createElement(
  318. "span",
  319. { style: { color: "#666", fontWeight: 400 } },
  320. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  321. maxUpload - this.state.fileList.length,
  322. "\u5F20\u56FE\u7247)"
  323. ) : null
  324. ),
  325. _react2.default.createElement(_icon2.default, {
  326. type: "close",
  327. onClick: function onClick() {
  328. return _this3.handleShowUpload(false);
  329. },
  330. style: {
  331. float: "right",
  332. cursor: "pointer",
  333. marginTop: 4
  334. }
  335. })
  336. )
  337. },
  338. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  339. onClick: function onClick() {
  340. return _this3.handleShowUpload(true);
  341. }
  342. }) : _react2.default.createElement(_icon2.default, {
  343. type: "picture",
  344. className: "comment-toolbar-icon",
  345. style: { marginLeft: 10 },
  346. onClick: function onClick() {
  347. return _this3.handleShowUpload(true);
  348. }
  349. })
  350. ) : null
  351. ),
  352. _react2.default.createElement(
  353. "div",
  354. { className: "comment-toolbar-right" },
  355. button ? _react2.default.cloneElement(button, {
  356. onClick: button.props.onClick || handleSubmit
  357. }) : _react2.default.createElement(
  358. _button2.default,
  359. {
  360. onClick: function onClick() {
  361. return _this3.handleSubmit();
  362. },
  363. type: "primary",
  364. loading: btnLoading,
  365. disabled: disabledSubmit
  366. },
  367. btnSubmitText
  368. )
  369. )
  370. )
  371. )
  372. );
  373. }
  374. }]);
  375. return Editor;
  376. }(_react2.default.Component);
  377. Editor.propTypes = {
  378. rows: _propTypes2.default.number,
  379. placeholder: _propTypes2.default.string,
  380. showEmoji: _propTypes2.default.bool,
  381. showUpload: _propTypes2.default.bool,
  382. closeUploadWhenBlur: _propTypes2.default.bool,
  383. maxUpload: _propTypes2.default.number,
  384. value: _propTypes2.default.string,
  385. onChange: _propTypes2.default.func,
  386. onSubmit: _propTypes2.default.func,
  387. beforeSubmit: _propTypes2.default.func,
  388. btnSubmitText: _propTypes2.default.string,
  389. btnLoading: _propTypes2.default.bool,
  390. btnDisabled: _propTypes2.default.bool,
  391. button: _propTypes2.default.node,
  392. emojiToolIcon: _propTypes2.default.node,
  393. imageToolIcon: _propTypes2.default.node,
  394. showError: _propTypes2.default.bool,
  395. onError: _propTypes2.default.func,
  396. maxLength: _propTypes2.default.number
  397. };
  398. Editor.defaultProps = {
  399. rows: 5,
  400. placeholder: "说点什么吧...",
  401. showEmoji: true,
  402. showUpload: true,
  403. closeUploadWhenBlur: false,
  404. maxUpload: 1,
  405. btnSubmitText: "发表",
  406. btnLoading: false,
  407. btnDisabled: false,
  408. showError: true,
  409. maxLength: 140
  410. };
  411. exports.default = Editor;
  412. //# sourceMappingURL=index.js.map