richTextEditors.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React from 'react';
  2. import theme from 'react-quill/dist/quill.snow.css';
  3. import { message } from 'antd';
  4. import ReactQuill from 'react-quill';
  5. import ajax from 'jquery/src/ajax/xhr.js';
  6. import $ from 'jquery/src/ajax';
  7. var RichTextUploadUrl, RichTextUploadSign;
  8. //上传图片
  9. function uploadImg(s) {
  10. let myform = new FormData()
  11. let file = document.getElementById("fileToUpload").files[0]
  12. if (!file) { return; };
  13. myform.append('file', file);
  14. myform.append('sign', RichTextUploadSign);
  15. $.ajax({
  16. method: "post",
  17. dataType: "json",
  18. url: location.origin + RichTextUploadUrl,
  19. data: myform,
  20. contentType: false,
  21. processData: false,
  22. success: function (data) {
  23. if (data.error && data.error.length) {
  24. message.warning(data.error[0].message);
  25. } else {
  26. const cursorPosition = s.quill.getSelection().index
  27. s.quill.insertEmbed(
  28. cursorPosition,
  29. "image",
  30. globalConfig.avatarUploadHost + data.data
  31. );
  32. s.quill.setSelection(cursorPosition + 1)
  33. };
  34. }
  35. })
  36. }
  37. //图片文件选择
  38. function fileSelect() {
  39. let uploader = document.getElementById("fileToUpload");
  40. if (!uploader.onchange) {
  41. uploader.onchange = () => {
  42. uploadImg(this)
  43. }
  44. }
  45. uploader.click();
  46. }
  47. //视频文件选择
  48. function fileSelectVideo() {
  49. let uploader = document.getElementById("fileToUploadVideo");
  50. if (!uploader.onchange) {
  51. uploader.onchange = () => {
  52. uploadVideo(this)
  53. }
  54. }
  55. uploader.click();
  56. }
  57. //视频文件上传
  58. function uploadVideo(s) {
  59. let myform = new FormData()
  60. let file = document.getElementById("fileToUploadVideo").files[0]
  61. if (!file) { return; };
  62. myform.append('file', file);
  63. myform.append('sign', RichTextUploadSign);
  64. $.ajax({
  65. method: "post",
  66. dataType: "json",
  67. url: location.origin + RichTextUploadUrl,
  68. data: myform,
  69. contentType: false,
  70. processData: false,
  71. success: function (data) {
  72. if (data.error && data.error.length) {
  73. message.warning(data.error[0].message);
  74. } else {
  75. const cursorPosition = s.quill.getSelection().index
  76. s.quill.insertEmbed( cursorPosition, "video", globalConfig.avatarUploadHost + data.data );
  77. s.quill.setSelection(cursorPosition + 1)
  78. };
  79. console.log("上传成功")
  80. },
  81. error: function(XMLHttpRequest, textStatus, errorThrown){
  82. //查看错误信息
  83. message("上传失败");
  84. }
  85. })
  86. }
  87. const Editors = React.createClass({
  88. getInitialState() {
  89. return {
  90. value: null,
  91. theBool: true
  92. }
  93. },
  94. handleRichText(value) {
  95. this.setState({ value: value });
  96. this.props.handleRichText(value);
  97. },
  98. componentWillMount() {
  99. RichTextUploadUrl = this.props.uploadUrl;
  100. RichTextUploadSign = this.props.uploadSign;
  101. },
  102. componentWillReceiveProps(nextProps) {
  103. this.state.theBool = true;
  104. // if(nextProps.textContent){
  105. this.state.value = nextProps.textContent;
  106. // }
  107. },
  108. render() {
  109. const modules = {
  110. toolbar: {
  111. container: [
  112. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  113. [{ 'header': [1, 2, 3, false] }],
  114. ['bold', 'italic', 'underline', 'strike', 'blockquote'], // toggled buttons
  115. // [{ 'header': 1 }, { 'header': 2 }], // custom button values
  116. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  117. // [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
  118. [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
  119. // [{ 'direction': 'rtl' }], // text direction
  120. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  121. // [{ 'font': [] }],
  122. [{ 'align': [] }],
  123. ['link', 'image','video'],
  124. ['clean']
  125. ],
  126. handlers: { 'image': fileSelect ,'video': fileSelectVideo}
  127. }
  128. };
  129. const formats = [
  130. 'size', 'header',
  131. 'bold', 'italic', 'underline', 'strike', 'blockquote',
  132. 'list', 'bullet', 'indent',
  133. 'color', 'background',
  134. 'align',
  135. 'link', 'image','video'
  136. ];
  137. return (
  138. <div>
  139. <ReactQuill theme="snow" value={this.state.value} modules={modules} formats={formats} placeholder='请填写内容!' onChange={this.handleRichText} />
  140. <input type="file" name="fileToUpload" id="fileToUpload" placeholder='请填写内容!' style={{ "display": "none" }} />
  141. <input type="file" name="fileToUploadVideo" id="fileToUploadVideo" placeholder='请填写视频!' style={{ "display": "none" }} />
  142. </div>
  143. )
  144. },
  145. });
  146. export default Editors;