通用评论

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