通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. var handleSubmit = this.handleSubmit;
  212. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
  213. var inputValue = value || this.state.value;
  214. return _react2.default.createElement(
  215. "div",
  216. null,
  217. _react2.default.createElement(
  218. "div",
  219. {
  220. className: (0, _classnames2.default)({
  221. "comment-editor-toolbar": true,
  222. "comment-editor-toolbar-error": inputValue.length > maxLength
  223. })
  224. },
  225. "\u5DF2\u8F93\u5165 ",
  226. inputValue.length,
  227. " / ",
  228. maxLength,
  229. " \u5B57"
  230. ),
  231. _react2.default.createElement(
  232. "div",
  233. { className: "comment-editor" },
  234. _react2.default.createElement(TextArea, {
  235. value: inputValue,
  236. onChange: function onChange(e) {
  237. return _this3.handleChange(e.target.value);
  238. },
  239. rows: rows,
  240. placeholder: placeholder
  241. }),
  242. _react2.default.createElement(
  243. "div",
  244. { className: "comment-toolbar" },
  245. _react2.default.createElement(
  246. "div",
  247. { className: "comment-toolbar-left" },
  248. showEmoji && _react2.default.createElement(
  249. _popover2.default,
  250. {
  251. trigger: "click",
  252. placement: "bottomLeft",
  253. autoAdjustOverflow: false,
  254. content: _react2.default.createElement(
  255. "div",
  256. { style: { width: 200 } },
  257. _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji })
  258. ),
  259. overlayClassName: "comment-emoji-popover"
  260. },
  261. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  262. ),
  263. showUpload ? _react2.default.createElement(
  264. _popover2.default,
  265. {
  266. trigger: "click",
  267. visible: this.state.showUpload,
  268. overlayStyle: { zIndex: 999 },
  269. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  270. _this3.handleShowUpload(visible);
  271. } : null,
  272. content: _react2.default.createElement(
  273. "div",
  274. {
  275. style: {
  276. width: 112 * maxUpload,
  277. minHeight: 100,
  278. margin: "0 auto"
  279. }
  280. },
  281. _react2.default.createElement(_Upload2.default, {
  282. onChangeFileList: this.handleChangeFileList,
  283. onUpload: this.handleUpload,
  284. maxUpload: maxUpload,
  285. fileList: this.state.fileList,
  286. showError: this.props.showError,
  287. onError: this.props.onError
  288. })
  289. ),
  290. placement: "bottomLeft",
  291. title: _react2.default.createElement(
  292. "div",
  293. { style: { margin: "5px auto" } },
  294. _react2.default.createElement(
  295. "span",
  296. null,
  297. "\u4E0A\u4F20\u56FE\u7247",
  298. maxUpload >= 2 ? _react2.default.createElement(
  299. "span",
  300. { style: { color: "#666", fontWeight: 400 } },
  301. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  302. maxUpload - this.state.fileList.length,
  303. "\u5F20\u56FE\u7247)"
  304. ) : null
  305. ),
  306. _react2.default.createElement(_icon2.default, {
  307. type: "close",
  308. onClick: function onClick() {
  309. return _this3.handleShowUpload(false);
  310. },
  311. style: {
  312. float: "right",
  313. cursor: "pointer",
  314. marginTop: 4
  315. }
  316. })
  317. )
  318. },
  319. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  320. onClick: function onClick() {
  321. return _this3.handleShowUpload(true);
  322. }
  323. }) : _react2.default.createElement(_icon2.default, {
  324. type: "picture",
  325. className: "comment-toolbar-icon",
  326. style: { marginLeft: 10 },
  327. onClick: function onClick() {
  328. return _this3.handleShowUpload(true);
  329. }
  330. })
  331. ) : null
  332. ),
  333. _react2.default.createElement(
  334. "div",
  335. { className: "comment-toolbar-right" },
  336. button ? _react2.default.cloneElement(button, {
  337. onClick: button.props.onClick || handleSubmit
  338. }) : _react2.default.createElement(
  339. _button2.default,
  340. {
  341. onClick: function onClick() {
  342. return _this3.handleSubmit();
  343. },
  344. type: "primary",
  345. loading: btnLoading,
  346. disabled: disabledSubmit
  347. },
  348. btnSubmitText
  349. )
  350. )
  351. )
  352. )
  353. );
  354. }
  355. }]);
  356. return Editor;
  357. }(_react2.default.Component);
  358. Editor.propTypes = {
  359. rows: _propTypes2.default.number,
  360. placeholder: _propTypes2.default.string,
  361. showEmoji: _propTypes2.default.bool,
  362. showUpload: _propTypes2.default.bool,
  363. closeUploadWhenBlur: _propTypes2.default.bool,
  364. maxUpload: _propTypes2.default.number,
  365. value: _propTypes2.default.string,
  366. onChange: _propTypes2.default.func,
  367. onSubmit: _propTypes2.default.func,
  368. beforeSubmit: _propTypes2.default.func,
  369. btnSubmitText: _propTypes2.default.string,
  370. btnLoading: _propTypes2.default.bool,
  371. btnDisabled: _propTypes2.default.bool,
  372. button: _propTypes2.default.node,
  373. emojiToolIcon: _propTypes2.default.node,
  374. imageToolIcon: _propTypes2.default.node,
  375. showError: _propTypes2.default.bool,
  376. onError: _propTypes2.default.func,
  377. maxLength: _propTypes2.default.number
  378. };
  379. Editor.defaultProps = {
  380. rows: 5,
  381. placeholder: "说点什么吧...",
  382. showEmoji: true,
  383. showUpload: true,
  384. closeUploadWhenBlur: false,
  385. maxUpload: 1,
  386. btnSubmitText: "发表",
  387. btnLoading: false,
  388. btnDisabled: false,
  389. showError: true,
  390. maxLength: 140
  391. };
  392. exports.default = Editor;
  393. //# sourceMappingURL=index.js.map