通用评论

index.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. var fileList = this.state.fileList;
  121. fileMap[uid] = path;
  122. fileList = fileList.map(function (item) {
  123. if (item.uid === uid) {
  124. item.thumbUrl = _constant.OSS_LINK + path;
  125. }
  126. return item;
  127. });
  128. this.setState({ fileMap: fileMap, fileList: fileList });
  129. }
  130. /**
  131. * 提交编辑器内容
  132. * 提交功能,交给父组件来实现
  133. * 需要父组件传入 onSubmit
  134. */
  135. }, {
  136. key: "handleSubmit",
  137. value: function handleSubmit() {
  138. var _this2 = this;
  139. var _state = this.state,
  140. value = _state.value,
  141. fileMap = _state.fileMap,
  142. fileList = _state.fileList;
  143. var files = [];
  144. if (fileList.length) {
  145. fileList.forEach(function (item) {
  146. if (!fileMap[item.uid]) {
  147. return;
  148. }
  149. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  150. });
  151. }
  152. if (this.props.beforeSubmit) {
  153. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  154. if (!(res === false)) {
  155. _this2.props.onSubmit({ text: value, files: files }, function () {
  156. _this2.resetState();
  157. if (_this2.props.onCommentSuccess) {
  158. _this2.props.onCommentSuccess();
  159. }
  160. });
  161. }
  162. });
  163. } else {
  164. this.props.onSubmit({ text: value, files: files }, function () {
  165. _this2.resetState();
  166. if (_this2.props.onCommentSuccess) {
  167. _this2.props.onCommentSuccess();
  168. }
  169. });
  170. }
  171. }
  172. }, {
  173. key: "resetState",
  174. value: function resetState() {
  175. this.setState({
  176. showUpload: false,
  177. value: "",
  178. fileList: [],
  179. fileMap: {}
  180. });
  181. }
  182. }, {
  183. key: "render",
  184. value: function render() {
  185. var _this3 = this;
  186. var _props = this.props,
  187. value = _props.value,
  188. placeholder = _props.placeholder,
  189. rows = _props.rows,
  190. showEmoji = _props.showEmoji,
  191. showUpload = _props.showUpload,
  192. closeUploadWhenBlur = _props.closeUploadWhenBlur,
  193. maxUpload = _props.maxUpload,
  194. btnSubmitText = _props.btnSubmitText,
  195. btnLoading = _props.btnLoading,
  196. btnDisabled = _props.btnDisabled,
  197. button = _props.button,
  198. emojiToolIcon = _props.emojiToolIcon,
  199. imageToolIcon = _props.imageToolIcon;
  200. var handleSubmit = this.handleSubmit;
  201. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
  202. return _react2.default.createElement(
  203. "div",
  204. { className: "comment-editor" },
  205. _react2.default.createElement(TextArea, {
  206. value: value || this.state.value,
  207. onChange: function onChange(e) {
  208. return _this3.handleChange(e.target.value);
  209. },
  210. rows: rows,
  211. placeholder: placeholder
  212. }),
  213. _react2.default.createElement(
  214. "div",
  215. { className: "comment-toolbar" },
  216. _react2.default.createElement(
  217. "div",
  218. { className: "comment-toolbar-left" },
  219. showEmoji && _react2.default.createElement(
  220. _popover2.default,
  221. {
  222. trigger: "click",
  223. placement: "bottomLeft",
  224. autoAdjustOverflow: false,
  225. content: _react2.default.createElement(
  226. "div",
  227. { style: { width: 200 } },
  228. _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji })
  229. ),
  230. overlayClassName: "comment-emoji-popover"
  231. },
  232. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  233. ),
  234. showUpload ? _react2.default.createElement(
  235. _popover2.default,
  236. {
  237. trigger: "click",
  238. visible: this.state.showUpload,
  239. overlayStyle: { zIndex: 999 },
  240. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  241. _this3.handleShowUpload(visible);
  242. } : null,
  243. content: _react2.default.createElement(
  244. "div",
  245. {
  246. style: {
  247. width: 112 * maxUpload,
  248. minHeight: 100,
  249. margin: "0 auto"
  250. }
  251. },
  252. _react2.default.createElement(_Upload2.default, {
  253. onChangeFileList: this.handleChangeFileList,
  254. onUpload: this.handleUpload,
  255. maxUpload: maxUpload,
  256. fileList: this.state.fileList,
  257. showError: this.props.showError,
  258. onError: this.props.onError
  259. })
  260. ),
  261. placement: "bottomLeft",
  262. title: _react2.default.createElement(
  263. "div",
  264. { style: { margin: "5px auto" } },
  265. _react2.default.createElement(
  266. "span",
  267. null,
  268. "\u4E0A\u4F20\u56FE\u7247",
  269. maxUpload >= 2 ? _react2.default.createElement(
  270. "span",
  271. { style: { color: "#666", fontWeight: 400 } },
  272. "(\u60A8\u8FD8\u80FD\u4E0A\u4F20",
  273. maxUpload - this.state.fileList.length,
  274. "\u5F20\u56FE\u7247)"
  275. ) : null
  276. ),
  277. _react2.default.createElement(_icon2.default, {
  278. type: "close",
  279. onClick: function onClick() {
  280. return _this3.handleShowUpload(false);
  281. },
  282. style: {
  283. float: "right",
  284. cursor: "pointer",
  285. marginTop: 4
  286. }
  287. })
  288. )
  289. },
  290. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  291. onClick: function onClick() {
  292. return _this3.handleShowUpload(true);
  293. }
  294. }) : _react2.default.createElement(_icon2.default, {
  295. type: "picture",
  296. className: "comment-toolbar-icon",
  297. style: { marginLeft: 10 },
  298. onClick: function onClick() {
  299. return _this3.handleShowUpload(true);
  300. }
  301. })
  302. ) : null
  303. ),
  304. _react2.default.createElement(
  305. "div",
  306. { className: "comment-toolbar-right" },
  307. button ? _react2.default.cloneElement(button, {
  308. onClick: button.props.onClick || handleSubmit
  309. }) : _react2.default.createElement(
  310. _button2.default,
  311. {
  312. onClick: function onClick() {
  313. return _this3.handleSubmit();
  314. },
  315. type: "primary",
  316. loading: btnLoading,
  317. disabled: disabledSubmit
  318. },
  319. btnSubmitText
  320. )
  321. )
  322. )
  323. );
  324. }
  325. }]);
  326. return Editor;
  327. }(_react2.default.Component);
  328. Editor.propTypes = {
  329. rows: _propTypes2.default.number,
  330. placeholder: _propTypes2.default.string,
  331. showEmoji: _propTypes2.default.bool,
  332. showUpload: _propTypes2.default.bool,
  333. closeUploadWhenBlur: _propTypes2.default.bool,
  334. maxUpload: _propTypes2.default.number,
  335. value: _propTypes2.default.string,
  336. onChange: _propTypes2.default.func,
  337. onSubmit: _propTypes2.default.func,
  338. beforeSubmit: _propTypes2.default.func,
  339. btnSubmitText: _propTypes2.default.string,
  340. btnLoading: _propTypes2.default.bool,
  341. btnDisabled: _propTypes2.default.bool,
  342. button: _propTypes2.default.node,
  343. emojiToolIcon: _propTypes2.default.node,
  344. imageToolIcon: _propTypes2.default.node,
  345. showError: _propTypes2.default.bool,
  346. onError: _propTypes2.default.func
  347. };
  348. Editor.defaultProps = {
  349. rows: 5,
  350. placeholder: "说点什么吧...",
  351. showEmoji: true,
  352. showUpload: true,
  353. closeUploadWhenBlur: false,
  354. maxUpload: 1,
  355. btnSubmitText: "发表",
  356. btnLoading: false,
  357. btnDisabled: false,
  358. showError: true
  359. };
  360. exports.default = Editor;
  361. //# sourceMappingURL=index.js.map