通用评论

index.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. {
  273. style: { width: 240 },
  274. onWheel: this.handleEmojiScroll
  275. },
  276. _react2.default.createElement(_Emoji2.default, {
  277. onClick: this.handleClickEmoji,
  278. ref: function ref(node) {
  279. _this3.emoji = node;
  280. }
  281. })
  282. ),
  283. overlayClassName: "comment-emoji-popover"
  284. },
  285. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  286. ),
  287. showUpload ? _react2.default.createElement(
  288. _popover2.default,
  289. {
  290. trigger: "click",
  291. visible: this.state.showUpload,
  292. overlayStyle: { zIndex: 999 },
  293. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  294. _this3.handleShowUpload(visible);
  295. } : null,
  296. content: _react2.default.createElement(
  297. "div",
  298. {
  299. style: {
  300. width: 336, // 一行显示3张
  301. minHeight: 100,
  302. margin: "0 auto"
  303. }
  304. },
  305. _react2.default.createElement(_Upload2.default, {
  306. onChangeFileList: this.handleChangeFileList,
  307. onUpload: this.handleUpload,
  308. maxUpload: maxUpload,
  309. fileList: this.state.fileList,
  310. showError: this.props.showError,
  311. onError: this.props.onError
  312. }),
  313. _react2.default.createElement("div", { className: "clearfix" })
  314. ),
  315. placement: "bottomLeft",
  316. title: _react2.default.createElement(
  317. "div",
  318. { style: { margin: "5px auto" } },
  319. _react2.default.createElement(
  320. "span",
  321. null,
  322. "\u4E0A\u4F20\u56FE\u7247",
  323. maxUpload >= 2 ? _react2.default.createElement(
  324. "span",
  325. { style: { color: "#666", fontWeight: 400 } },
  326. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  327. maxUpload - this.state.fileList.length,
  328. "\u5F20\u56FE\u7247)"
  329. ) : null
  330. ),
  331. _react2.default.createElement(_icon2.default, {
  332. type: "close",
  333. onClick: function onClick() {
  334. return _this3.handleShowUpload(false);
  335. },
  336. style: {
  337. float: "right",
  338. cursor: "pointer",
  339. marginTop: 4
  340. }
  341. })
  342. )
  343. },
  344. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  345. onClick: function onClick() {
  346. return _this3.handleShowUpload(true);
  347. }
  348. }) : _react2.default.createElement(_icon2.default, {
  349. type: "picture",
  350. className: "comment-toolbar-icon",
  351. style: { marginLeft: 10 },
  352. onClick: function onClick() {
  353. return _this3.handleShowUpload(true);
  354. }
  355. })
  356. ) : null
  357. ),
  358. _react2.default.createElement(
  359. "div",
  360. { className: "comment-toolbar-right" },
  361. button ? _react2.default.cloneElement(button, {
  362. onClick: button.props.onClick || handleSubmit
  363. }) : _react2.default.createElement(
  364. _button2.default,
  365. {
  366. onClick: function onClick() {
  367. return _this3.handleSubmit();
  368. },
  369. type: "primary",
  370. loading: btnLoading,
  371. disabled: disabledSubmit
  372. },
  373. btnSubmitText
  374. )
  375. )
  376. )
  377. )
  378. );
  379. }
  380. }]);
  381. return Editor;
  382. }(_react2.default.Component);
  383. Editor.propTypes = {
  384. rows: _propTypes2.default.number,
  385. placeholder: _propTypes2.default.string,
  386. showEmoji: _propTypes2.default.bool,
  387. showUpload: _propTypes2.default.bool,
  388. closeUploadWhenBlur: _propTypes2.default.bool,
  389. maxUpload: _propTypes2.default.number,
  390. value: _propTypes2.default.string,
  391. onChange: _propTypes2.default.func,
  392. onSubmit: _propTypes2.default.func,
  393. beforeSubmit: _propTypes2.default.func,
  394. btnSubmitText: _propTypes2.default.string,
  395. btnLoading: _propTypes2.default.bool,
  396. btnDisabled: _propTypes2.default.bool,
  397. button: _propTypes2.default.node,
  398. emojiToolIcon: _propTypes2.default.node,
  399. imageToolIcon: _propTypes2.default.node,
  400. showError: _propTypes2.default.bool,
  401. onError: _propTypes2.default.func,
  402. maxLength: _propTypes2.default.number
  403. };
  404. Editor.defaultProps = {
  405. rows: 5,
  406. placeholder: "说点什么吧...",
  407. showEmoji: true,
  408. showUpload: true,
  409. closeUploadWhenBlur: false,
  410. maxUpload: 1,
  411. btnSubmitText: "发表",
  412. btnLoading: false,
  413. btnDisabled: false,
  414. showError: true,
  415. maxLength: 140
  416. };
  417. exports.default = Editor;
  418. //# sourceMappingURL=index.js.map