通用评论

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import App, { Editor } from "./App";
  4. // import registerServiceWorker from "./registerServiceWorker";
  5. class Index extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. fileList: [
  10. // {
  11. // url:
  12. // "//links-comment.oss-cn-beijing.aliyuncs.com/comment/20190727/YQ-4VC1bL.jpeg",
  13. // type: "image/jpeg",
  14. // uid: "rc-upload-1564206005248-2"
  15. // }
  16. ],
  17. value: ""
  18. };
  19. }
  20. render() {
  21. return (
  22. <App
  23. showAlertComment
  24. showAlertReply
  25. showAlertFavor
  26. showError
  27. {...this.props}
  28. >
  29. <Editor
  30. maxUpload={9}
  31. autoFocus
  32. {...this.props.editorProps}
  33. fileList={this.state.fileList}
  34. value={this.state.value}
  35. onChange={value => {
  36. this.setState({ value });
  37. }}
  38. handleChangeFileList={fileList => {
  39. this.setState({
  40. fileList
  41. });
  42. }}
  43. allowEnterSubmit
  44. />
  45. </App>
  46. );
  47. }
  48. }
  49. /**
  50. * 渲染评论组件
  51. * @param {object} config 编辑器配置
  52. * - {string} id 渲染评论的DOM的 ID
  53. * - {number} type 评论的 type
  54. * - {string} businessId 评论的 businessId
  55. * - {string} API, API 前缀, 默认 http://api.links123.net/comment/v1
  56. */
  57. function renderComment(config) {
  58. if (!config.id) {
  59. throw new Error("id is required");
  60. }
  61. if (!config.type) {
  62. throw new Error("type is required");
  63. }
  64. if (!config.businessId) {
  65. // throw new Error("businessId is required");
  66. config.businessId = "test";
  67. console.warn("没有传入 businessId 参数,默认使用: test");
  68. }
  69. if (!config.API) {
  70. // throw new Error("API is required");
  71. config.API = "http://api.links123.net/comment/v1";
  72. console.warn(
  73. "没有传入 API 参数,默认使用: http://api.links123.net/comment/v1"
  74. );
  75. }
  76. ReactDOM.render(<Index {...config} />, document.getElementById(config.id));
  77. // registerServiceWorker();
  78. }
  79. window.renderComment = renderComment;
  80. if (process.env.NODE_ENV !== "production") {
  81. renderComment({
  82. id: "root-comment",
  83. type: 1,
  84. businessId: "test",
  85. businessUserId: 4,
  86. userId: 58297,
  87. currentUser: {
  88. user_id: 58297
  89. },
  90. userAvaHoverData: {
  91. 71763: {
  92. nickname: "aaa",
  93. followers: 20,
  94. fans: 2,
  95. isFollowed: true
  96. },
  97. 71748: {
  98. nickname: "L0",
  99. followers: 10,
  100. fans: 11,
  101. isFollowed: false
  102. },
  103. 71299: {
  104. nickname: "narro",
  105. followers: 10,
  106. fans: 11,
  107. isFollowed: false
  108. },
  109. 58297: {
  110. nickname: "aaaaa",
  111. followers: 10,
  112. fans: 11,
  113. isFollowed: false
  114. }
  115. },
  116. showHoverCard: true,
  117. showEdit: true,
  118. userAvaClick: id => {
  119. console.log("userAvaClick", id);
  120. },
  121. getUserInfo: id => {
  122. console.log("getinfo:", id);
  123. },
  124. focus: id => {
  125. return new Promise(function(resolve, reject) {
  126. console.log("focus:", id);
  127. resolve();
  128. });
  129. },
  130. unFocus: id => {
  131. return new Promise(function(resolve, reject) {
  132. console.log("unFocus:", id);
  133. resolve();
  134. });
  135. },
  136. onCountChange: c => {
  137. // console.log(c);
  138. },
  139. onDelete: (type, data) => {
  140. console.log(type, data);
  141. },
  142. onCommentFail: data => {
  143. console.log("onCommentFail", data);
  144. },
  145. onUpdateComment: (type, data) => {
  146. console.log("onUpdateComment", type);
  147. },
  148. onBeforeUpdateComment: () => {
  149. console.log("onBeforeUpdateComment");
  150. },
  151. sendMessage: id => {
  152. console.log("sendMessage", id);
  153. },
  154. preRenderValue: value => {
  155. return `${value}`;
  156. },
  157. editorProps: {
  158. onCommentSuccess: data => {
  159. console.log(data);
  160. }
  161. }
  162. });
  163. }
  164. // renderComment({
  165. // id: "root-comment",
  166. // type: 1,
  167. // businessId: "test",
  168. // API: 'http://api.links123.net/comment/v1',
  169. // });