通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 _Upload = require('./Upload');
  24. var _Upload2 = _interopRequireDefault(_Upload);
  25. var _Emoji = require('./Emoji');
  26. var _Emoji2 = _interopRequireDefault(_Emoji);
  27. require('./index.css');
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. 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; }
  31. 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; }
  32. var TextArea = _input2.default.TextArea;
  33. var Editor = function (_React$Component) {
  34. _inherits(Editor, _React$Component);
  35. function Editor(props) {
  36. _classCallCheck(this, Editor);
  37. var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
  38. _this.state = {
  39. showUpload: false,
  40. value: '', // 编辑器里面的值
  41. fileList: [], // 图片列表
  42. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  43. };
  44. _this.handleChange = _this.handleChange.bind(_this);
  45. _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
  46. _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
  47. _this.handleShowUpload = _this.handleShowUpload.bind(_this);
  48. _this.handleUpload = _this.handleUpload.bind(_this);
  49. _this.handleSubmit = _this.handleSubmit.bind(_this);
  50. return _this;
  51. }
  52. _createClass(Editor, [{
  53. key: 'componentDidMount',
  54. value: function componentDidMount() {}
  55. /**
  56. * 编辑器的值改变事件
  57. * 将最新的值存储到 state 中
  58. * @param {string} value 输入的值
  59. */
  60. }, {
  61. key: 'handleChange',
  62. value: function handleChange(value) {
  63. this.setState({ value: value });
  64. if (this.props.onChange) {
  65. this.props.onChange(value);
  66. }
  67. }
  68. /**
  69. * 点击 emoji 的事件
  70. * 点击后,需要将改 emoji 插入到编辑器中
  71. * 插入的值为 [emoji chinese name]
  72. * 参数 emoji 即为 emoji chinese name
  73. * @param {string}} emoji emoji 的中文,如 微笑
  74. */
  75. }, {
  76. key: 'handleClickEmoji',
  77. value: function handleClickEmoji(emoji) {
  78. var value = this.state.value;
  79. value += '[' + emoji + ']';
  80. this.handleChange(value);
  81. }
  82. /**
  83. * 监听文件列表改变事件
  84. * @param {Array} fileList 文件列表
  85. */
  86. }, {
  87. key: 'handleChangeFileList',
  88. value: function handleChangeFileList(fileList) {
  89. this.setState({ fileList: fileList });
  90. }
  91. /**
  92. * 控制上传 Popover 的显示和隐藏
  93. * @param {boolean} showUpload 是否显示上传的 Popover
  94. */
  95. }, {
  96. key: 'handleShowUpload',
  97. value: function handleShowUpload(showUpload) {
  98. if (typeof showUpload === 'boolean') {
  99. this.setState({ showUpload: showUpload });
  100. } else {
  101. this.setState({ showUpload: !this.state.showUpload });
  102. }
  103. }
  104. /**
  105. * 上传文件
  106. * @param {object} param 文件对象
  107. */
  108. }, {
  109. key: 'handleUpload',
  110. value: function handleUpload(_ref) {
  111. var uid = _ref.uid,
  112. path = _ref.path;
  113. var fileMap = this.state.fileMap;
  114. fileMap[uid] = path;
  115. this.setState({ fileMap: fileMap });
  116. }
  117. /**
  118. * 提交编辑器内容
  119. * 提交功能,交给父组件来实现
  120. * 需要父组件传入 onSubmit
  121. */
  122. }, {
  123. key: 'handleSubmit',
  124. value: function handleSubmit() {
  125. var _this2 = this;
  126. var _state = this.state,
  127. value = _state.value,
  128. fileMap = _state.fileMap,
  129. fileList = _state.fileList;
  130. if (fileList.length) {
  131. value += '<br/>';
  132. fileList.forEach(function (item) {
  133. value += '[' + _constant.OSS_LINK + fileMap[item.uid] + ']';
  134. });
  135. }
  136. this.props.onSubmit(value, function () {
  137. _this2.setState({
  138. showUpload: false,
  139. value: '',
  140. fileList: [],
  141. fileMap: {}
  142. });
  143. });
  144. }
  145. }, {
  146. key: 'render',
  147. value: function render() {
  148. var _this3 = this;
  149. var _props = this.props,
  150. value = _props.value,
  151. placeholder = _props.placeholder,
  152. rows = _props.rows,
  153. showEmoji = _props.showEmoji,
  154. showUpload = _props.showUpload,
  155. btnSubmitText = _props.btnSubmitText,
  156. btnLoading = _props.btnLoading,
  157. btnDisabled = _props.btnDisabled,
  158. button = _props.button,
  159. emojiToolIcon = _props.emojiToolIcon,
  160. imageToolIcon = _props.imageToolIcon;
  161. var handleSubmit = this.handleSubmit;
  162. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.state.fileList.length;
  163. return _react2.default.createElement(
  164. 'div',
  165. { className: 'comment-editor' },
  166. _react2.default.createElement(TextArea, {
  167. value: value || this.state.value,
  168. onChange: function onChange(e) {
  169. return _this3.handleChange(e.target.value);
  170. },
  171. rows: rows,
  172. placeholder: placeholder
  173. }),
  174. _react2.default.createElement(
  175. 'div',
  176. { className: 'comment-toolbar' },
  177. _react2.default.createElement(
  178. 'div',
  179. { className: 'comment-toolbar-left' },
  180. showEmoji && _react2.default.createElement(
  181. _popover2.default,
  182. {
  183. trigger: 'click',
  184. placement: 'bottomLeft',
  185. autoAdjustOverflow: false,
  186. content: _react2.default.createElement(
  187. 'div',
  188. { style: { width: 200 } },
  189. _react2.default.createElement(_Emoji2.default, { onClick: this.handleClickEmoji })
  190. ),
  191. overlayClassName: 'comment-emoji-popover'
  192. },
  193. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: 'smile-o', className: 'comment-toolbar-icon' })
  194. ),
  195. showUpload && _react2.default.createElement(
  196. _popover2.default,
  197. {
  198. visible: this.state.showUpload,
  199. overlayStyle: { zIndex: 999 },
  200. content: _react2.default.createElement(
  201. 'div',
  202. {
  203. style: { width: 112 * _constant.MAX_UPLOAD_NUMBER, minHeight: 100 }
  204. },
  205. _react2.default.createElement(_Upload2.default, {
  206. onChangeFileList: this.handleChangeFileList,
  207. onUpload: this.handleUpload,
  208. fileList: this.state.fileList
  209. })
  210. ),
  211. placement: 'bottomLeft',
  212. title: _react2.default.createElement(
  213. 'div',
  214. { style: { margin: '5px auto' } },
  215. _react2.default.createElement(
  216. 'span',
  217. null,
  218. '\u4E0A\u4F20\u56FE\u7247',
  219. _react2.default.createElement(
  220. 'span',
  221. { style: { color: '#666', fontWeight: 400 } },
  222. '(\u60A8\u8FD8\u80FD\u4E0A\u4F20',
  223. _constant.MAX_UPLOAD_NUMBER - this.state.fileList.length,
  224. '\u5F20\u56FE\u7247)'
  225. )
  226. ),
  227. _react2.default.createElement(_icon2.default, {
  228. type: 'close',
  229. onClick: function onClick() {
  230. return _this3.handleShowUpload(false);
  231. },
  232. style: {
  233. float: 'right',
  234. cursor: 'pointer',
  235. marginTop: 4
  236. }
  237. })
  238. )
  239. },
  240. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  241. onClick: function onClick() {
  242. return _this3.handleShowUpload(true);
  243. }
  244. }) : _react2.default.createElement(_icon2.default, {
  245. type: 'picture',
  246. className: 'comment-toolbar-icon',
  247. style: { marginLeft: 10 },
  248. onClick: function onClick() {
  249. return _this3.handleShowUpload(true);
  250. }
  251. })
  252. )
  253. ),
  254. _react2.default.createElement(
  255. 'div',
  256. { className: 'comment-toolbar-right' },
  257. button ? _react2.default.cloneElement(button, {
  258. onClick: button.props.onClick || handleSubmit
  259. }) : _react2.default.createElement(
  260. _button2.default,
  261. {
  262. onClick: function onClick() {
  263. return _this3.handleSubmit();
  264. },
  265. type: 'primary',
  266. loading: btnLoading,
  267. disabled: disabledSubmit
  268. },
  269. btnSubmitText
  270. )
  271. )
  272. )
  273. );
  274. }
  275. }]);
  276. return Editor;
  277. }(_react2.default.Component);
  278. Editor.propTypes = {
  279. rows: _propTypes2.default.number,
  280. placeholder: _propTypes2.default.string,
  281. showEmoji: _propTypes2.default.bool,
  282. showUpload: _propTypes2.default.bool,
  283. value: _propTypes2.default.string,
  284. onChange: _propTypes2.default.func,
  285. onSubmit: _propTypes2.default.func,
  286. btnSubmitText: _propTypes2.default.string,
  287. btnLoading: _propTypes2.default.bool,
  288. btnDisabled: _propTypes2.default.bool,
  289. button: _propTypes2.default.node,
  290. emojiToolIcon: _propTypes2.default.node,
  291. imageToolIcon: _propTypes2.default.node
  292. };
  293. Editor.defaultProps = {
  294. rows: 5,
  295. placeholder: '说点什么吧...',
  296. showEmoji: true,
  297. showUpload: true,
  298. btnSubmitText: '发表',
  299. btnLoading: false,
  300. btnDisabled: false
  301. };
  302. exports.default = Editor;
  303. //# sourceMappingURL=index.js.map