通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. var canSubmit = true;
  143. if (this.props.beforeSubmit) {
  144. canSubmit = this.props.beforeSubmit({ text: value, files: files }) === false ? false : true;
  145. }
  146. if (canSubmit) {
  147. this.props.onSubmit({ text: value, files: files }, function () {
  148. _this2.resetState();
  149. });
  150. }
  151. }
  152. }, {
  153. key: "resetState",
  154. value: function resetState() {
  155. this.setState({
  156. showUpload: false,
  157. value: "",
  158. fileList: [],
  159. fileMap: {}
  160. });
  161. }
  162. }, {
  163. key: "render",
  164. value: function render() {
  165. var _this3 = this;
  166. var _props = this.props,
  167. value = _props.value,
  168. placeholder = _props.placeholder,
  169. rows = _props.rows,
  170. showEmoji = _props.showEmoji,
  171. showUpload = _props.showUpload,
  172. closeUploadWhenBlur = _props.closeUploadWhenBlur,
  173. maxUpload = _props.maxUpload,
  174. btnSubmitText = _props.btnSubmitText,
  175. btnLoading = _props.btnLoading,
  176. btnDisabled = _props.btnDisabled,
  177. button = _props.button,
  178. emojiToolIcon = _props.emojiToolIcon,
  179. imageToolIcon = _props.imageToolIcon;
  180. var handleSubmit = this.handleSubmit;
  181. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
  182. return _react2.default.createElement(
  183. "div",
  184. { className: "comment-editor" },
  185. _react2.default.createElement(TextArea, {
  186. value: value || this.state.value,
  187. onChange: function onChange(e) {
  188. return _this3.handleChange(e.target.value);
  189. },
  190. rows: rows,
  191. placeholder: placeholder
  192. }),
  193. _react2.default.createElement(
  194. "div",
  195. { className: "comment-toolbar" },
  196. _react2.default.createElement(
  197. "div",
  198. { className: "comment-toolbar-left" },
  199. showEmoji && _react2.default.createElement(
  200. _popover2.default,
  201. {
  202. trigger: "click",
  203. placement: "bottomLeft",
  204. autoAdjustOverflow: false,
  205. content: _react2.default.createElement(
  206. "div",
  207. { style: { width: 200 } },
  208. _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji })
  209. ),
  210. overlayClassName: "comment-emoji-popover"
  211. },
  212. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  213. ),
  214. showUpload ? closeUploadWhenBlur ? _react2.default.createElement(
  215. _popover2.default,
  216. {
  217. trigger: "click",
  218. overlayStyle: { zIndex: 999 },
  219. content: _react2.default.createElement(
  220. "div",
  221. {
  222. style: {
  223. width: 112 * maxUpload,
  224. minHeight: 100,
  225. margin: "0 auto"
  226. }
  227. },
  228. _react2.default.createElement(_Upload2.default, {
  229. onChangeFileList: this.handleChangeFileList,
  230. onUpload: this.handleUpload,
  231. maxUpload: maxUpload,
  232. fileList: this.state.fileList
  233. })
  234. ),
  235. placement: "bottomLeft",
  236. title: _react2.default.createElement(
  237. "div",
  238. { style: { margin: "5px auto" } },
  239. _react2.default.createElement(
  240. "span",
  241. null,
  242. "\u4E0A\u4F20\u56FE\u7247",
  243. maxUpload >= 2 ? _react2.default.createElement(
  244. "span",
  245. { style: { color: "#666", fontWeight: 400 } },
  246. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  247. maxUpload - this.state.fileList.length,
  248. "\u5F20\u56FE\u7247)"
  249. ) : null
  250. ),
  251. _react2.default.createElement(_icon2.default, {
  252. type: "close",
  253. onClick: function onClick() {
  254. return _this3.handleShowUpload(false);
  255. },
  256. style: {
  257. float: "right",
  258. cursor: "pointer",
  259. marginTop: 4
  260. }
  261. })
  262. )
  263. },
  264. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  265. onClick: function onClick() {
  266. return _this3.handleShowUpload(true);
  267. }
  268. }) : _react2.default.createElement(_icon2.default, {
  269. type: "picture",
  270. className: "comment-toolbar-icon",
  271. style: { marginLeft: 10 },
  272. onClick: function onClick() {
  273. return _this3.handleShowUpload(true);
  274. }
  275. })
  276. ) : _react2.default.createElement(
  277. _popover2.default,
  278. {
  279. visible: this.state.showUpload,
  280. overlayStyle: { zIndex: 999 },
  281. content: _react2.default.createElement(
  282. "div",
  283. {
  284. style: {
  285. width: 112 * maxUpload,
  286. minHeight: 100,
  287. margin: "0 auto"
  288. }
  289. },
  290. _react2.default.createElement(_Upload2.default, {
  291. onChangeFileList: this.handleChangeFileList,
  292. onUpload: this.handleUpload,
  293. maxUpload: maxUpload,
  294. fileList: this.state.fileList,
  295. showError: this.props.showError
  296. })
  297. ),
  298. placement: "bottomLeft",
  299. title: _react2.default.createElement(
  300. "div",
  301. { style: { margin: "5px auto" } },
  302. _react2.default.createElement(
  303. "span",
  304. null,
  305. "\u4E0A\u4F20\u56FE\u7247",
  306. maxUpload >= 2 ? _react2.default.createElement(
  307. "span",
  308. { style: { color: "#666", fontWeight: 400 } },
  309. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  310. maxUpload - this.state.fileList.length,
  311. "\u5F20\u56FE\u7247)"
  312. ) : null
  313. ),
  314. _react2.default.createElement(_icon2.default, {
  315. type: "close",
  316. onClick: function onClick() {
  317. return _this3.handleShowUpload(false);
  318. },
  319. style: {
  320. float: "right",
  321. cursor: "pointer",
  322. marginTop: 4
  323. }
  324. })
  325. )
  326. },
  327. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  328. onClick: function onClick() {
  329. return _this3.handleShowUpload(true);
  330. }
  331. }) : _react2.default.createElement(_icon2.default, {
  332. type: "picture",
  333. className: "comment-toolbar-icon",
  334. style: { marginLeft: 10 },
  335. onClick: function onClick() {
  336. return _this3.handleShowUpload(true);
  337. }
  338. })
  339. ) : null
  340. ),
  341. _react2.default.createElement(
  342. "div",
  343. { className: "comment-toolbar-right" },
  344. button ? _react2.default.cloneElement(button, {
  345. onClick: button.props.onClick || handleSubmit
  346. }) : _react2.default.createElement(
  347. _button2.default,
  348. {
  349. onClick: function onClick() {
  350. return _this3.handleSubmit();
  351. },
  352. type: "primary",
  353. loading: btnLoading,
  354. disabled: disabledSubmit
  355. },
  356. btnSubmitText
  357. )
  358. )
  359. )
  360. );
  361. }
  362. }]);
  363. return Editor;
  364. }(_react2.default.Component);
  365. Editor.propTypes = {
  366. rows: _propTypes2.default.number,
  367. placeholder: _propTypes2.default.string,
  368. showEmoji: _propTypes2.default.bool,
  369. showUpload: _propTypes2.default.bool,
  370. closeUploadWhenBlur: _propTypes2.default.bool,
  371. maxUpload: _propTypes2.default.number,
  372. value: _propTypes2.default.string,
  373. onChange: _propTypes2.default.func,
  374. onSubmit: _propTypes2.default.func,
  375. beforeSubmit: _propTypes2.default.func,
  376. btnSubmitText: _propTypes2.default.string,
  377. btnLoading: _propTypes2.default.bool,
  378. btnDisabled: _propTypes2.default.bool,
  379. button: _propTypes2.default.node,
  380. emojiToolIcon: _propTypes2.default.node,
  381. imageToolIcon: _propTypes2.default.node,
  382. showError: _propTypes2.default.bool
  383. };
  384. Editor.defaultProps = {
  385. rows: 5,
  386. placeholder: "说点什么吧...",
  387. showEmoji: true,
  388. showUpload: true,
  389. closeUploadWhenBlur: false,
  390. maxUpload: 1,
  391. btnSubmitText: "发表",
  392. btnLoading: false,
  393. btnDisabled: false,
  394. showError: true
  395. };
  396. exports.default = Editor;
  397. //# sourceMappingURL=index.js.map