通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 _message2 = require("antd/es/message");
  12. var _message3 = _interopRequireDefault(_message2);
  13. var _input = require("antd/es/input");
  14. var _input2 = _interopRequireDefault(_input);
  15. 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; }; }();
  16. require("antd/es/button/style/css");
  17. require("antd/es/popover/style/css");
  18. require("antd/es/icon/style/css");
  19. require("antd/es/message/style/css");
  20. require("antd/es/input/style/css");
  21. var _react = require("react");
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require("prop-types");
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _classnames = require("classnames");
  26. var _classnames2 = _interopRequireDefault(_classnames);
  27. var _reactIntlUniversal = require("react-intl-universal");
  28. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  29. var _constant = require("../../constant");
  30. var _helper = require("../../helper");
  31. var _Upload = require("./Upload");
  32. var _Upload2 = _interopRequireDefault(_Upload);
  33. var _Emoji = require("./Emoji");
  34. var _Emoji2 = _interopRequireDefault(_Emoji);
  35. require("./index.css");
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  38. 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; }
  39. 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; }
  40. var TextArea = _input2.default.TextArea;
  41. var Editor = function (_React$Component) {
  42. _inherits(Editor, _React$Component);
  43. function Editor(props) {
  44. _classCallCheck(this, Editor);
  45. var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
  46. _this.state = {
  47. showUpload: false,
  48. value: props.value, // 编辑器里面的值
  49. fileList: props.fileList, // 图片列表
  50. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  51. };
  52. _this.handleChange = _this.handleChange.bind(_this);
  53. _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
  54. _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
  55. _this.handleShowUpload = _this.handleShowUpload.bind(_this);
  56. _this.handleUpload = _this.handleUpload.bind(_this);
  57. _this.handleSubmit = _this.handleSubmit.bind(_this);
  58. _this.resetState = _this.resetState.bind(_this);
  59. _this.handleEmojiScroll = _this.handleEmojiScroll.bind(_this);
  60. return _this;
  61. }
  62. _createClass(Editor, [{
  63. key: "componentDidMount",
  64. value: function componentDidMount() {
  65. if ((0, _helper.isFunction)(this.props.onRef)) {
  66. this.props.onRef(this);
  67. }
  68. }
  69. }, {
  70. key: "handleEmojiScroll",
  71. value: function handleEmojiScroll(e) {
  72. if (!this.emoji) {
  73. return;
  74. }
  75. e.preventDefault();
  76. if (e.deltaY > 0) {
  77. this.emoji.next();
  78. } else if (e.deltaY < 0) {
  79. this.emoji.prev();
  80. }
  81. }
  82. /**
  83. * 编辑器的值改变事件
  84. * 将最新的值存储到 state 中
  85. * @param {string} value 输入的值
  86. */
  87. }, {
  88. key: "handleChange",
  89. value: function handleChange(value) {
  90. this.setState({ value: value });
  91. if (this.props.onChange) {
  92. this.props.onChange(value);
  93. }
  94. }
  95. /**
  96. * 点击 emoji 的事件
  97. * 点击后,需要将改 emoji 插入到编辑器中
  98. * 插入的值为 [emoji chinese name]
  99. * 参数 emoji 即为 emoji chinese name
  100. * @param {string}} emoji emoji 的中文,如 微笑
  101. */
  102. }, {
  103. key: "handleClickEmoji",
  104. value: function handleClickEmoji(emoji) {
  105. var value = this.props.value || this.state.value;
  106. value += "[" + emoji + "]";
  107. this.handleChange(value);
  108. }
  109. /**
  110. * 监听文件列表改变事件
  111. * @param {Array} fileList 文件列表
  112. */
  113. }, {
  114. key: "handleChangeFileList",
  115. value: function handleChangeFileList(fileList) {
  116. var list = fileList;
  117. if (fileList.length > this.props.maxUpload) {
  118. list = fileList.slice(0, this.props.maxUpload);
  119. }
  120. this.props.handleChangeFileList(list);
  121. this.setState({ fileList: list });
  122. }
  123. /**
  124. * 控制上传 Popover 的显示和隐藏
  125. * @param {boolean} showUpload 是否显示上传的 Popover
  126. */
  127. }, {
  128. key: "handleShowUpload",
  129. value: function handleShowUpload(showUpload) {
  130. if (typeof showUpload === "boolean") {
  131. this.setState({ showUpload: showUpload });
  132. } else {
  133. this.setState({ showUpload: !this.state.showUpload });
  134. }
  135. }
  136. /**
  137. * 上传文件
  138. * @param {object} param 文件对象
  139. */
  140. }, {
  141. key: "handleUpload",
  142. value: function handleUpload(_ref) {
  143. var uid = _ref.uid,
  144. path = _ref.path;
  145. var fileMap = this.state.fileMap;
  146. var fileList = this.state.fileList;
  147. fileMap[uid] = path;
  148. fileList = fileList.map(function (item) {
  149. if (item.uid === uid) {
  150. item.thumbUrl = _constant.OSS_LINK + path;
  151. }
  152. return item;
  153. });
  154. this.props.handleChangeFileList(fileList);
  155. this.setState({ fileMap: fileMap, fileList: fileList });
  156. }
  157. /**
  158. * 提交编辑器内容
  159. * 提交功能,交给父组件来实现
  160. * 需要父组件传入 onSubmit
  161. */
  162. }, {
  163. key: "handleSubmit",
  164. value: function handleSubmit() {
  165. var _this2 = this;
  166. var maxLength = this.props.maxLength;
  167. var _state = this.state,
  168. value = _state.value,
  169. fileMap = _state.fileMap,
  170. fileList = _state.fileList;
  171. if (value.length > maxLength) {
  172. // message.error(`字数不得超过${maxLength}字`);
  173. _message3.default.error(_reactIntlUniversal2.default.get("editor.maxLength", { maxLength: maxLength }));
  174. return;
  175. }
  176. var files = [];
  177. if (fileList.length) {
  178. fileList.forEach(function (item) {
  179. if (item.url) {
  180. files.push(item.url);
  181. return;
  182. }
  183. if (!fileMap[item.uid]) {
  184. return;
  185. }
  186. files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
  187. });
  188. }
  189. if (this.props.beforeSubmit) {
  190. Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
  191. if (!(res === false)) {
  192. _this2.props.onSubmit({ text: value, files: files }, function (res, action) {
  193. _this2.resetState();
  194. if (action === "comment" && _this2.props.onCommentSuccess) {
  195. _this2.props.onCommentSuccess(res);
  196. }
  197. });
  198. }
  199. });
  200. } else {
  201. this.props.onSubmit({ text: value, files: files }, function (res, action) {
  202. _this2.resetState();
  203. if (action === "comment" && _this2.props.onCommentSuccess) {
  204. _this2.props.onCommentSuccess(res);
  205. }
  206. });
  207. }
  208. }
  209. }, {
  210. key: "resetState",
  211. value: function resetState() {
  212. this.handleChange("");
  213. this.setState({
  214. showUpload: false,
  215. value: "",
  216. fileList: [],
  217. fileMap: {}
  218. });
  219. }
  220. }, {
  221. key: "render",
  222. value: function render() {
  223. var _this3 = this;
  224. var _props = this.props,
  225. value = _props.value,
  226. rows = _props.rows,
  227. showEmoji = _props.showEmoji,
  228. showUpload = _props.showUpload,
  229. multiple = _props.multiple,
  230. emojiPopoverPlacement = _props.emojiPopoverPlacement,
  231. uploadPopoverPlacement = _props.uploadPopoverPlacement,
  232. uploadOverlayClassName = _props.uploadOverlayClassName,
  233. fileList = _props.fileList,
  234. closeUploadWhenBlur = _props.closeUploadWhenBlur,
  235. maxUpload = _props.maxUpload,
  236. btnLoading = _props.btnLoading,
  237. btnDisabled = _props.btnDisabled,
  238. button = _props.button,
  239. emojiToolIcon = _props.emojiToolIcon,
  240. imageToolIcon = _props.imageToolIcon,
  241. maxLength = _props.maxLength,
  242. autoFocus = _props.autoFocus;
  243. var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
  244. var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
  245. var handleSubmit = this.handleSubmit;
  246. var disabledSubmit = btnDisabled || !this.props.value && !this.state.value && !this.props.fileList.length && !this.state.fileList.length;
  247. var inputValue = value || this.state.value;
  248. var uploadFileList = fileList || this.state.fileList;
  249. return _react2.default.createElement(
  250. "div",
  251. { className: "comment-editor-container" },
  252. _react2.default.createElement("div", {
  253. className: (0, _classnames2.default)({
  254. "comment-editor-toolbar": true,
  255. "comment-editor-toolbar-error": inputValue.length > maxLength
  256. })
  257. }),
  258. _react2.default.createElement(
  259. "div",
  260. { className: "comment-editor" },
  261. _react2.default.createElement(TextArea, {
  262. value: inputValue,
  263. onChange: function onChange(e) {
  264. return _this3.handleChange(e.target.value);
  265. },
  266. rows: rows,
  267. placeholder: placeholder,
  268. autoFocus: autoFocus
  269. }),
  270. _react2.default.createElement(
  271. "div",
  272. { className: "comment-toolbar" },
  273. _react2.default.createElement(
  274. "div",
  275. { className: "comment-toolbar-left" },
  276. showEmoji && _react2.default.createElement(
  277. _popover2.default,
  278. {
  279. trigger: "click",
  280. placement: emojiPopoverPlacement,
  281. autoAdjustOverflow: false,
  282. overlayStyle: { zIndex: 9999 },
  283. content: _react2.default.createElement(
  284. "div",
  285. {
  286. style: { width: 240, height: 205 },
  287. onWheel: this.handleEmojiScroll
  288. },
  289. _react2.default.createElement(_Emoji2.default, {
  290. onClick: this.handleClickEmoji,
  291. ref: function ref(node) {
  292. _this3.emoji = node;
  293. }
  294. })
  295. ),
  296. overlayClassName: "comment-emoji-popover"
  297. },
  298. emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
  299. ),
  300. showUpload ? _react2.default.createElement(
  301. _popover2.default,
  302. {
  303. trigger: "click"
  304. // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
  305. // visible={this.state.showUpload}
  306. , placement: uploadPopoverPlacement,
  307. overlayClassName: uploadOverlayClassName,
  308. autoAdjustOverflow: false,
  309. overlayStyle: { zIndex: 9999 },
  310. onVisibleChange: closeUploadWhenBlur ? function (visible) {
  311. _this3.handleShowUpload(visible);
  312. } : null,
  313. content: _react2.default.createElement(
  314. "div",
  315. {
  316. style: {
  317. width: 336, // 一行显示3张
  318. minHeight: 100,
  319. margin: "0 auto"
  320. }
  321. },
  322. _react2.default.createElement(_Upload2.default, {
  323. multiple: multiple,
  324. onChangeFileList: this.handleChangeFileList,
  325. onUpload: this.handleUpload,
  326. maxUpload: maxUpload,
  327. fileList: uploadFileList,
  328. showError: this.props.showError,
  329. onError: this.props.onError
  330. }),
  331. _react2.default.createElement("div", { className: "clearfix" })
  332. ),
  333. title: _react2.default.createElement(
  334. "div",
  335. { style: { margin: "5px auto" } },
  336. _react2.default.createElement(
  337. "span",
  338. null,
  339. _reactIntlUniversal2.default.get("editor.uploadTip"),
  340. maxUpload >= 2 ? _react2.default.createElement(
  341. "span",
  342. { style: { color: "#666", fontWeight: 400 } },
  343. _reactIntlUniversal2.default.get("editor.uploadCount", {
  344. count: maxUpload - uploadFileList.length
  345. })
  346. ) : null
  347. )
  348. )
  349. },
  350. imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
  351. onClick: function onClick() {
  352. return _this3.handleShowUpload(true);
  353. }
  354. }) : _react2.default.createElement(_icon2.default, {
  355. type: "picture",
  356. className: "comment-toolbar-icon",
  357. style: { marginLeft: 20 },
  358. onClick: function onClick() {
  359. return _this3.handleShowUpload(true);
  360. }
  361. })
  362. ) : null
  363. ),
  364. _react2.default.createElement(
  365. "div",
  366. { className: "comment-toolbar-right" },
  367. button ? _react2.default.cloneElement(button, {
  368. onClick: button.props.onClick || handleSubmit
  369. }) : _react2.default.createElement(
  370. _button2.default,
  371. {
  372. onClick: function onClick() {
  373. return _this3.handleSubmit();
  374. },
  375. type: "primary",
  376. loading: btnLoading,
  377. disabled: disabledSubmit
  378. },
  379. btnSubmitText
  380. )
  381. )
  382. )
  383. )
  384. );
  385. }
  386. }]);
  387. return Editor;
  388. }(_react2.default.Component);
  389. Editor.propTypes = {
  390. rows: _propTypes2.default.number,
  391. placeholder: _propTypes2.default.string,
  392. showEmoji: _propTypes2.default.bool,
  393. emojiPopoverPlacement: _propTypes2.default.string,
  394. showUpload: _propTypes2.default.bool,
  395. uploadPopoverPlacement: _propTypes2.default.string,
  396. uploadOverlayClassName: _propTypes2.default.string,
  397. multiple: _propTypes2.default.bool,
  398. closeUploadWhenBlur: _propTypes2.default.bool,
  399. maxUpload: _propTypes2.default.number,
  400. value: _propTypes2.default.string,
  401. onChange: _propTypes2.default.func,
  402. onSubmit: _propTypes2.default.func,
  403. beforeSubmit: _propTypes2.default.func,
  404. btnSubmitText: _propTypes2.default.string,
  405. btnLoading: _propTypes2.default.bool,
  406. btnDisabled: _propTypes2.default.bool,
  407. button: _propTypes2.default.node,
  408. emojiToolIcon: _propTypes2.default.node,
  409. imageToolIcon: _propTypes2.default.node,
  410. showError: _propTypes2.default.bool,
  411. onError: _propTypes2.default.func,
  412. maxLength: _propTypes2.default.number
  413. };
  414. Editor.defaultProps = {
  415. rows: 5,
  416. // placeholder: "说点什么吧",
  417. showEmoji: true,
  418. showUpload: true,
  419. multiple: true,
  420. emojiPopoverPlacement: "bottomLeft",
  421. closeUploadWhenBlur: false,
  422. uploadPopoverPlacement: "bottomLeft",
  423. uploadOverlayClassName: "",
  424. maxUpload: 1,
  425. value: "",
  426. fileList: [],
  427. // btnSubmitText: "发表",
  428. btnLoading: false,
  429. btnDisabled: false,
  430. showError: true,
  431. maxLength: 5000
  432. };
  433. exports.default = Editor;
  434. //# sourceMappingURL=index.js.map