通用评论

index.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. console.log("----", fileList);
  40. this.setState({
  41. fileList
  42. });
  43. }}
  44. allowEnter
  45. onPressEnter={e => alert("enter pressed")}
  46. />
  47. </App>
  48. );
  49. }
  50. }
  51. /**
  52. * 渲染评论组件
  53. * @param {object} config 编辑器配置
  54. * - {string} id 渲染评论的DOM的 ID
  55. * - {number} type 评论的 type
  56. * - {string} businessId 评论的 businessId
  57. * - {string} API, API 前缀, 默认 http://api.links123.net/comment/v1
  58. */
  59. function renderComment(config) {
  60. if (!config.id) {
  61. throw new Error("id is required");
  62. }
  63. if (!config.type) {
  64. throw new Error("type is required");
  65. }
  66. if (!config.businessId) {
  67. // throw new Error("businessId is required");
  68. config.businessId = "test";
  69. console.warn("没有传入 businessId 参数,默认使用: test");
  70. }
  71. if (!config.API) {
  72. // throw new Error("API is required");
  73. config.API = "http://api.links123.net/comment/v1";
  74. console.warn(
  75. "没有传入 API 参数,默认使用: http://api.links123.net/comment/v1"
  76. );
  77. }
  78. ReactDOM.render(<Index {...config} />, document.getElementById(config.id));
  79. // registerServiceWorker();
  80. }
  81. window.renderComment = renderComment;
  82. if (process.env.NODE_ENV !== "production") {
  83. renderComment({
  84. id: "root-comment",
  85. type: 1,
  86. businessId: "test",
  87. businessUserId: 4,
  88. userId: 58297,
  89. currentUser: {
  90. user_id: 58297
  91. },
  92. userAvaHoverData: {
  93. 71763: {
  94. nickname: "aaa",
  95. followers: 20,
  96. fans: 2,
  97. isFollowed: true
  98. },
  99. 71748: {
  100. nickname: "L0",
  101. followers: 10,
  102. fans: 11,
  103. isFollowed: false
  104. },
  105. 71299: {
  106. nickname: "narro",
  107. followers: 10,
  108. fans: 11,
  109. isFollowed: false
  110. },
  111. 58297: {
  112. nickname: "aaaaa",
  113. followers: 10,
  114. fans: 11,
  115. isFollowed: false
  116. }
  117. },
  118. showHoverCard: true,
  119. showEdit: true,
  120. userAvaClick: id => {
  121. console.log("userAvaClick", id);
  122. },
  123. getUserInfo: id => {
  124. console.log("getinfo:", id);
  125. },
  126. focus: id => {
  127. return new Promise(function(resolve, reject) {
  128. console.log("focus:", id);
  129. resolve();
  130. });
  131. },
  132. unFocus: id => {
  133. return new Promise(function(resolve, reject) {
  134. console.log("unFocus:", id);
  135. resolve();
  136. });
  137. },
  138. onCountChange: c => {
  139. // console.log(c);
  140. },
  141. onDelete: (type, data) => {
  142. console.log(type, data);
  143. },
  144. onCommentFail: data => {
  145. console.log("onCommentFail", data);
  146. },
  147. onUpdateComment: (type, data) => {
  148. console.log("onUpdateComment", type);
  149. },
  150. onBeforeUpdateComment: () => {
  151. console.log("onBeforeUpdateComment");
  152. },
  153. sendMessage: id => {
  154. console.log("sendMessage", id);
  155. },
  156. preRenderValue: value => {
  157. return `${value}`;
  158. },
  159. editorProps: {
  160. onCommentSuccess: data => {
  161. console.log(data);
  162. }
  163. }
  164. });
  165. }
  166. // renderComment({
  167. // id: "root-comment",
  168. // type: 1,
  169. // businessId: "test",
  170. // API: 'http://api.links123.net/comment/v1',
  171. // });