richTextEditors.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. function uploadImg(s) {
  9. let myform = new FormData(), file = document.getElementById("fileToUpload").files[0];
  10. if (!file) {
  11. return;
  12. };
  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(cursorPosition, 'image', globalConfig.avatarHost + '/upload' + data.data)
  28. s.quill.setSelection(cursorPosition + 1)
  29. };
  30. }
  31. })
  32. }
  33. function fileSelect() {
  34. let uploader = document.getElementById("fileToUpload");
  35. if (!uploader.onchange) {
  36. uploader.onchange = () => {
  37. uploadImg(this)
  38. }
  39. }
  40. uploader.click();
  41. }
  42. const Editors = React.createClass({
  43. getInitialState() {
  44. return {
  45. value: null,
  46. theBool: true
  47. }
  48. },
  49. handleRichText(value) {
  50. this.setState({ value: value });
  51. this.props.handleRichText(value);
  52. },
  53. componentWillMount() {
  54. RichTextUploadUrl = this.props.uploadUrl;
  55. RichTextUploadSign = this.props.uploadSign;
  56. },
  57. componentWillReceiveProps(nextProps) {
  58. this.state.theBool = true;
  59. if(nextProps.textContent){
  60. this.state.value = nextProps.textContent;
  61. }
  62. },
  63. render() {
  64. const modules = {
  65. toolbar: {
  66. container: [
  67. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  68. [{ 'header': [1, 2, 3, false] }],
  69. ['bold', 'italic', 'underline', 'strike', 'blockquote'], // toggled buttons
  70. // [{ 'header': 1 }, { 'header': 2 }], // custom button values
  71. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  72. // [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
  73. [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
  74. // [{ 'direction': 'rtl' }], // text direction
  75. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  76. // [{ 'font': [] }],
  77. [{ 'align': [] }],
  78. ['link', 'image'],
  79. ['clean']
  80. ],
  81. handlers: { 'image': fileSelect }
  82. }
  83. };
  84. const formats = [
  85. 'size', 'header',
  86. 'bold', 'italic', 'underline', 'strike', 'blockquote',
  87. 'list', 'bullet', 'indent',
  88. 'color', 'background',
  89. 'align',
  90. 'link', 'image'
  91. ];
  92. return (
  93. <div>
  94. <ReactQuill theme="snow"
  95. value={this.state.value}
  96. modules={modules}
  97. formats={formats}
  98. placeholder='填写的内容可包括服务介绍、项目价值、申办要求、申办流程'
  99. onChange={this.handleRichText} />
  100. <input type="file"
  101. name="fileToUpload"
  102. id="fileToUpload"
  103. placeholder='填写的内容可包括服务介绍、项目价值、申办要求、申办流程'
  104. style={{ "display": "none" }} />
  105. </div>
  106. )
  107. },
  108. });
  109. export default Editors;