通用评论

index.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. value: "",
  25. fileList: [], // 图片列表
  26. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  27. };
  28. _this.handleChange = _this.handleChange.bind(_this);
  29. _this.handleSubmit = _this.handleSubmit.bind(_this);
  30. _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
  31. _this.handleChangeEmoji = _this.handleChangeEmoji.bind(_this);
  32. _this.handleUpload = _this.handleUpload.bind(_this);
  33. return _this;
  34. }
  35. _createClass(CommentInput, [{
  36. key: "handleChange",
  37. value: function handleChange(value) {
  38. this.setState({ value: value });
  39. }
  40. }, {
  41. key: "handleChangeFileList",
  42. value: function handleChangeFileList(fileList) {
  43. this.setState({ fileList: fileList });
  44. }
  45. }, {
  46. key: "handleChangeEmoji",
  47. value: function handleChangeEmoji(emojiId) {
  48. var value = this.state.value;
  49. value += "[" + emojiId + "]";
  50. this.setState({ value: value });
  51. _react2.default.Children.forEach(this.props.content, function (child) {
  52. // 如果 Editor 的父组件传入了 onChange 事件,则需要将改变之后的值传递给父组件
  53. if (child.props.onChange) {
  54. child.props.onChange(value);
  55. }
  56. });
  57. }
  58. }, {
  59. key: "handleUpload",
  60. value: function handleUpload(_ref) {
  61. var uid = _ref.uid,
  62. path = _ref.path;
  63. var fileMap = this.state.fileMap;
  64. fileMap[uid] = path;
  65. this.setState({ fileMap: fileMap });
  66. }
  67. }, {
  68. key: "handleSubmit",
  69. value: function handleSubmit() {
  70. var _state = this.state,
  71. value = _state.value,
  72. fileMap = _state.fileMap,
  73. fileList = _state.fileList;
  74. if (fileList.length) {
  75. value += "<br/>";
  76. fileList.forEach(function (item) {
  77. value += "[" + _constant.OSS_LINK + fileMap[item.uid] + "]";
  78. });
  79. }
  80. var _props = this.props,
  81. type = _props.type,
  82. commentId = _props.commentId,
  83. replyId = _props.replyId,
  84. handleToggleInput = _props.handleToggleInput;
  85. if (type === "normal") {
  86. this.props.app.sCreateComment({
  87. content: value
  88. });
  89. } else if (type === "comment") {
  90. this.props.app.sCreateReply({
  91. comment_id: commentId,
  92. content: value
  93. }, function () {
  94. return handleToggleInput();
  95. });
  96. } else if (type === "reply") {
  97. this.props.app.sCreateReply({
  98. comment_id: commentId,
  99. content: value,
  100. reply_id: replyId
  101. }, function () {
  102. return handleToggleInput();
  103. });
  104. }
  105. _react2.default.Children.forEach(this.props.content, function (child) {
  106. // 如果 Editor 的父组件传入了 onSubmit 事件,则需要将改变之后的值传递给父组件
  107. if (child.props.onSubmit) {
  108. child.props.onSubmit(value);
  109. }
  110. });
  111. }
  112. }, {
  113. key: "render",
  114. value: function render() {
  115. var _this2 = this;
  116. var type = this.props.type;
  117. var _state2 = this.state,
  118. value = _state2.value,
  119. fileList = _state2.fileList;
  120. var childrenWithProps = _react2.default.Children.map(this.props.content, function (child) {
  121. return _react2.default.cloneElement(child, _extends({
  122. value: value,
  123. fileList: fileList,
  124. onChange: _this2.handleChange,
  125. onSubmit: _this2.handleSubmit,
  126. onChangeFileList: _this2.handleChangeFileList,
  127. onChangeEmoji: _this2.handleChangeEmoji,
  128. onUpload: _this2.handleUpload,
  129. loading: _this2.props.app.loading.sCreateComment
  130. }, child.props));
  131. });
  132. return _react2.default.createElement(
  133. "div",
  134. null,
  135. type === "normal" ? _react2.default.createElement(
  136. "div",
  137. null,
  138. _react2.default.createElement(
  139. "span",
  140. {
  141. style: {
  142. border: "1px solid #CECECE",
  143. color: "#666",
  144. padding: "2px 3px"
  145. }
  146. },
  147. "\u56DE\u590D"
  148. ),
  149. _react2.default.createElement(
  150. "span",
  151. { style: { marginLeft: "20px", color: "#5198EB" } },
  152. "\u53E3\u7891",
  153. _react2.default.createElement(
  154. "span",
  155. { style: { marginLeft: "20px", color: "#666666" } },
  156. "(\u5168\u7AD9\u6311\u51FA\u6BDB\u75C5\u6216\u63D0\u51FA\u5408\u7406\u5EFA\u8BAE\uFF0C\u5956\u52B110\u5230100\u5143\u7EA2\u5305)"
  157. )
  158. )
  159. ) : null,
  160. _react2.default.createElement(
  161. "div",
  162. { style: { marginTop: 40 } },
  163. childrenWithProps
  164. )
  165. );
  166. }
  167. }]);
  168. return CommentInput;
  169. }(_react.Component);
  170. CommentInput.propTypes = {
  171. // normal 有切换回复/口碑的 header ; comment 评论输入框 / reply 回复输入框
  172. type: _propTypes2.default.oneOf(["normal", "comment", "reply"])
  173. };
  174. CommentInput.defaultProps = {
  175. type: "normal"
  176. };
  177. exports.default = (0, _Comment2.default)(CommentInput);
  178. //# sourceMappingURL=index.js.map