通用评论

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