通用评论

index.js 13KB

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