通用评论

index.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _constant = require("../../constant");
  12. var _Comment = require("../../Comment");
  13. var _Comment2 = _interopRequireDefault(_Comment);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. 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; }
  17. 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; }
  18. var CommentInput = function (_Component) {
  19. _inherits(CommentInput, _Component);
  20. function CommentInput(props) {
  21. _classCallCheck(this, CommentInput);
  22. var _this = _possibleConstructorReturn(this, (CommentInput.__proto__ || Object.getPrototypeOf(CommentInput)).call(this, props));
  23. _this.state = {};
  24. _this.handleSubmit = _this.handleSubmit.bind(_this);
  25. return _this;
  26. }
  27. /**
  28. * 提交评论
  29. * @param {object} { text<string>, files<array> } 需要提交的评论的文本和图片
  30. * @param {function} cb 提交成功后的回掉
  31. */
  32. _createClass(CommentInput, [{
  33. key: "handleSubmit",
  34. value: function handleSubmit(_ref, cb) {
  35. var text = _ref.text,
  36. files = _ref.files;
  37. var value = text;
  38. if (files && files.length) {
  39. value += _constant.IMAGE_SPLIT;
  40. files.forEach(function (file) {
  41. value += file + ",";
  42. });
  43. }
  44. if (value.substr(-1) === ",") {
  45. value = value.slice(0, -1);
  46. }
  47. var _props = this.props,
  48. action = _props.action,
  49. commentId = _props.commentId,
  50. replyId = _props.replyId,
  51. userId = _props.userId,
  52. callback = _props.callback;
  53. if (action === "comment") {
  54. this.props.app.sCreateComment({
  55. content: value
  56. }, function (data) {
  57. return cb(data, action);
  58. });
  59. } else if (action === "reply") {
  60. this.props.app.sCreateReply({
  61. comment_id: commentId,
  62. content: value,
  63. business_user_id: userId
  64. }, function (data) {
  65. callback && callback(data);
  66. cb(data, action);
  67. });
  68. } else if (action === "replyToReply") {
  69. this.props.app.sCreateReply({
  70. comment_id: commentId,
  71. content: value,
  72. reply_id: replyId,
  73. business_user_id: userId
  74. }, function (data) {
  75. callback && callback(data);
  76. cb(data, action);
  77. });
  78. }
  79. }
  80. }, {
  81. key: "render",
  82. value: function render() {
  83. var _this2 = this;
  84. var childrenWithProps = _react2.default.Children.map(this.props.content, function (child) {
  85. return _react2.default.cloneElement(child, _extends({
  86. // 编辑器本身不提交值,但 CommentInput 会提交
  87. // CommentInput 主要是负责评论的业务逻辑,提交评论和回复
  88. // 默认使用 CommentInput 的 onSubmit 来提交评论
  89. // 但也可以使用 Editor 的 props 来覆盖 onSubmit
  90. onSubmit: _this2.handleSubmit
  91. }, child.props, {
  92. // 如果当前的编辑器不是“评论”,则编辑器高度减小一些
  93. rows: _this2.props.action === "comment" ? child.props.rows : child.props.rows - 1
  94. }));
  95. });
  96. return _react2.default.createElement(
  97. "div",
  98. null,
  99. childrenWithProps
  100. );
  101. }
  102. }]);
  103. return CommentInput;
  104. }(_react.Component);
  105. CommentInput.propTypes = {
  106. // comment 评论
  107. // reply 评论的回复
  108. // replyToReply 回复的回复
  109. action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"])
  110. };
  111. CommentInput.defaultProps = {
  112. action: "comment"
  113. };
  114. exports.default = (0, _Comment2.default)(CommentInput);
  115. //# sourceMappingURL=index.js.map