richTextEditors副本.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. const Editors = React.createClass({
  48. getInitialState() {
  49. return {
  50. value: null,
  51. theBool: true
  52. }
  53. },
  54. handleRichText(value) {
  55. this.setState({ value: value });
  56. this.props.handleRichText(value);
  57. },
  58. componentWillMount() {
  59. RichTextUploadUrl = this.props.uploadUrl;
  60. RichTextUploadSign = this.props.uploadSign;
  61. },
  62. componentWillReceiveProps(nextProps) {
  63. this.state.theBool = true;
  64. // if(nextProps.textContent){
  65. this.state.value = nextProps.textContent;
  66. // }
  67. },
  68. render() {
  69. const modules = {
  70. toolbar: {
  71. container: [
  72. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  73. [{ 'header': [1, 2, 3, false] }],
  74. ['bold', 'italic', 'underline', 'strike', 'blockquote'], // toggled buttons
  75. // [{ 'header': 1 }, { 'header': 2 }], // custom button values
  76. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  77. // [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
  78. [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
  79. // [{ 'direction': 'rtl' }], // text direction
  80. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  81. // [{ 'font': [] }],
  82. [{ 'align': [] }],
  83. ['link', 'image'],
  84. ['clean']
  85. ],
  86. handlers: { 'image': fileSelect }
  87. }
  88. };
  89. const formats = [
  90. 'size', 'header',
  91. 'bold', 'italic', 'underline', 'strike', 'blockquote',
  92. 'list', 'bullet', 'indent',
  93. 'color', 'background',
  94. 'align',
  95. 'link', 'image'
  96. ];
  97. return (
  98. <div>
  99. <ReactQuill theme="snow"
  100. value={this.state.value}
  101. modules={modules}
  102. formats={formats}
  103. placeholder='请填写内容!'
  104. onChange={this.handleRichText} />
  105. <input type="file"
  106. name="fileToUpload"
  107. id="fileToUpload"
  108. placeholder='请填写内容!'
  109. style={{ "display": "none" }} />
  110. </div>
  111. )
  112. },
  113. });
  114. export default Editors;