通用评论

index.js 15KB

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