通用评论

index.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  292. // visible={this.state.showUpload}
  293. , overlayStyle: { zIndex: 999 },
  294. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  295. _this3.handleShowUpload(visible);
  296. } : null,
  297. content: _react2.default.createElement(
  298. "div",
  299. {
  300. style: {
  301. width: 336, // 一行显示3张
  302. minHeight: 100,
  303. margin: "0 auto"
  304. }
  305. },
  306. _react2.default.createElement(_Upload2.default, {
  307. onChangeFileList: this.handleChangeFileList,
  308. onUpload: this.handleUpload,
  309. maxUpload: maxUpload,
  310. fileList: this.state.fileList,
  311. showError: this.props.showError,
  312. onError: this.props.onError
  313. }),
  314. _react2.default.createElement("div", { className: "clearfix" })
  315. ),
  316. placement: "bottomLeft",
  317. title: _react2.default.createElement(
  318. "div",
  319. { style: { margin: "5px auto" } },
  320. _react2.default.createElement(
  321. "span",
  322. null,
  323. "\u4E0A\u4F20\u56FE\u7247",
  324. maxUpload >= 2 ? _react2.default.createElement(
  325. "span",
  326. { style: { color: "#666", fontWeight: 400 } },
  327. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  328. maxUpload - this.state.fileList.length,
  329. "\u5F20\u56FE\u7247)"
  330. ) : null
  331. )
  332. )
  333. },
  334. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  335. onClick: function onClick() {
  336. return _this3.handleShowUpload(true);
  337. }
  338. }) : _react2.default.createElement(_icon2.default, {
  339. type: "picture",
  340. className: "comment-toolbar-icon",
  341. style: { marginLeft: 10 },
  342. onClick: function onClick() {
  343. return _this3.handleShowUpload(true);
  344. }
  345. })
  346. ) : null
  347. ),
  348. _react2.default.createElement(
  349. "div",
  350. { className: "comment-toolbar-right" },
  351. button ? _react2.default.cloneElement(button, {
  352. onClick: button.props.onClick || handleSubmit
  353. }) : _react2.default.createElement(
  354. _button2.default,
  355. {
  356. onClick: function onClick() {
  357. return _this3.handleSubmit();
  358. },
  359. type: "primary",
  360. loading: btnLoading,
  361. disabled: disabledSubmit
  362. },
  363. btnSubmitText
  364. )
  365. )
  366. )
  367. )
  368. );
  369. }
  370. }]);
  371. return Editor;
  372. }(_react2.default.Component);
  373. Editor.propTypes = {
  374. rows: _propTypes2.default.number,
  375. placeholder: _propTypes2.default.string,
  376. showEmoji: _propTypes2.default.bool,
  377. showUpload: _propTypes2.default.bool,
  378. closeUploadWhenBlur: _propTypes2.default.bool,
  379. maxUpload: _propTypes2.default.number,
  380. value: _propTypes2.default.string,
  381. onChange: _propTypes2.default.func,
  382. onSubmit: _propTypes2.default.func,
  383. beforeSubmit: _propTypes2.default.func,
  384. btnSubmitText: _propTypes2.default.string,
  385. btnLoading: _propTypes2.default.bool,
  386. btnDisabled: _propTypes2.default.bool,
  387. button: _propTypes2.default.node,
  388. emojiToolIcon: _propTypes2.default.node,
  389. imageToolIcon: _propTypes2.default.node,
  390. showError: _propTypes2.default.bool,
  391. onError: _propTypes2.default.func,
  392. maxLength: _propTypes2.default.number
  393. };
  394. Editor.defaultProps = {
  395. rows: 5,
  396. placeholder: "说点什么吧...",
  397. showEmoji: true,
  398. showUpload: true,
  399. closeUploadWhenBlur: false,
  400. maxUpload: 1,
  401. btnSubmitText: "发表",
  402. btnLoading: false,
  403. btnDisabled: false,
  404. showError: true,
  405. maxLength: 140
  406. };
  407. exports.default = Editor;
  408. //# sourceMappingURL=index.js.map