通用评论

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