通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  6. 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; }; }();
  7. var _react = require("react");
  8. var _react2 = _interopRequireDefault(_react);
  9. var _propTypes = require("prop-types");
  10. var _propTypes2 = _interopRequireDefault(_propTypes);
  11. var _Comment = require("../../Comment");
  12. var _Comment2 = _interopRequireDefault(_Comment);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  15. 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; }
  16. 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; }
  17. var CommentInput = function (_Component) {
  18. _inherits(CommentInput, _Component);
  19. function CommentInput(props) {
  20. _classCallCheck(this, CommentInput);
  21. var _this = _possibleConstructorReturn(this, (CommentInput.__proto__ || Object.getPrototypeOf(CommentInput)).call(this, props));
  22. _this.state = {};
  23. _this.handleSubmit = _this.handleSubmit.bind(_this);
  24. return _this;
  25. }
  26. /**
  27. * 提交评论
  28. * @param {string} value 需要提交的评论的值
  29. * @param {function} cb 提交成功后的回掉
  30. */
  31. _createClass(CommentInput, [{
  32. key: "handleSubmit",
  33. value: function handleSubmit(value, cb) {
  34. var _props = this.props,
  35. action = _props.action,
  36. commentId = _props.commentId,
  37. replyId = _props.replyId,
  38. callback = _props.callback;
  39. if (action === "comment") {
  40. this.props.app.sCreateComment({
  41. content: value
  42. }, cb);
  43. } else if (action === "reply") {
  44. this.props.app.sCreateReply({
  45. comment_id: commentId,
  46. content: value
  47. }, function () {
  48. return callback && callback();
  49. });
  50. } else if (action === "replyToReply") {
  51. this.props.app.sCreateReply({
  52. comment_id: commentId,
  53. content: value,
  54. reply_id: replyId
  55. }, function () {
  56. return callback && callback();
  57. });
  58. }
  59. }
  60. }, {
  61. key: "render",
  62. value: function render() {
  63. var _this2 = this;
  64. var childrenWithProps = _react2.default.Children.map(this.props.content, function (child) {
  65. return _react2.default.cloneElement(child, _extends({
  66. // 编辑器本身不提交值,但 CommentInput 会提交
  67. // CommentInput 主要是负责评论的业务逻辑,提交评论和回复
  68. // 默认使用 CommentInput 的 onSubmit 来提交评论
  69. // 但也可以使用 Editor 的 props 来覆盖 onSubmit
  70. onSubmit: _this2.handleSubmit
  71. }, child.props, {
  72. // 如果当前的编辑器不是“评论”,则编辑器高度减小一些
  73. rows: _this2.props.action === "comment" ? child.props.rows : child.props.rows - 1
  74. }));
  75. });
  76. return _react2.default.createElement(
  77. "div",
  78. null,
  79. childrenWithProps
  80. );
  81. }
  82. }]);
  83. return CommentInput;
  84. }(_react.Component);
  85. CommentInput.propTypes = {
  86. // comment 评论
  87. // reply 评论的回复
  88. // replyToReply 回复的回复
  89. action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"])
  90. };
  91. CommentInput.defaultProps = {
  92. action: "comment"
  93. };
  94. exports.default = (0, _Comment2.default)(CommentInput);
  95. //# sourceMappingURL=index.js.map