richTextEditors.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React from 'react';
  2. import theme from 'react-quill/dist/quill.snow.css';
  3. import { message } from 'antd';
  4. import ReactQuill,{Quill} from 'react-quill';
  5. import ajax from 'jquery/src/ajax/xhr.js';
  6. import $ from 'jquery/src/ajax';
  7. let BlockEmbed = Quill.import('blots/block/embed');
  8. class AppPanelEmbed extends BlockEmbed {
  9. static create(value) {
  10. const node = super.create(value);
  11. node.setAttribute('contenteditable', 'false');
  12. node.setAttribute('width', '100%');
  13. // 设置自定义html
  14. node.innerHTML = this.transformValue(value)
  15. return node;
  16. }
  17. static transformValue(value) {
  18. let handleArr = value.split('\n')
  19. handleArr = handleArr.map(e => e.replace(/^[\s]+/, '')
  20. .replace(/[\s]+$/, ''))
  21. return handleArr.join('')
  22. }
  23. // 返回节点自身的value值 用于撤销操作
  24. static value(node) {
  25. return node.innerHTML
  26. }
  27. }
  28. // blotName
  29. AppPanelEmbed.blotName = 'AppPanelEmbed';
  30. // class名将用于匹配blot名称
  31. AppPanelEmbed.className = 'embed-innerApp';
  32. // 标签类型自定义
  33. AppPanelEmbed.tagName = 'p';
  34. Quill.register(AppPanelEmbed);
  35. var RichTextUploadUrl, RichTextUploadSign;
  36. //上传图片
  37. function uploadImg(s) {
  38. let myform = new FormData()
  39. let file = document.getElementById("fileToUpload").files[0]
  40. if (!file) { return; };
  41. myform.append('file', file);
  42. myform.append('sign', RichTextUploadSign);
  43. $.ajax({
  44. method: "post",
  45. dataType: "json",
  46. url: location.origin + RichTextUploadUrl,
  47. data: myform,
  48. contentType: false,
  49. processData: false,
  50. success: function (data) {
  51. if (data.error && data.error.length) {
  52. message.warning(data.error[0].message);
  53. } else {
  54. const cursorPosition = s.quill.getSelection().index
  55. s.quill.insertEmbed(
  56. cursorPosition,
  57. "image",
  58. globalConfig.avatarUploadHost + data.data
  59. );
  60. s.quill.setSelection(cursorPosition + 1)
  61. };
  62. }
  63. })
  64. }
  65. //图片文件选择
  66. function fileSelect() {
  67. let uploader = document.getElementById("fileToUpload");
  68. if (!uploader.onchange) {
  69. uploader.onchange = () => {
  70. uploadImg(this)
  71. }
  72. }
  73. uploader.click();
  74. }
  75. //音频文件选择
  76. function fileSelectAudio() {
  77. let uploader = document.getElementById("fileToUploadAudio");
  78. if (!uploader.onchange) {
  79. uploader.onchange = () => {
  80. uploadAudio(this)
  81. }
  82. }
  83. uploader.click();
  84. }
  85. //音频文件上传
  86. function uploadAudio(s) {
  87. let myform = new FormData()
  88. let file = document.getElementById("fileToUploadAudio").files[0]
  89. if (!file) { return; };
  90. myform.append('file', file);
  91. myform.append('sign', RichTextUploadSign);
  92. $.ajax({
  93. method: "post",
  94. dataType: "json",
  95. url: location.origin + RichTextUploadUrl,
  96. data: myform,
  97. contentType: false,
  98. processData: false,
  99. success: function (data) {
  100. if (data.error && data.error.length) {
  101. message.warning(data.error[0].message);
  102. } else {
  103. const cursorPosition = s.quill.getSelection().index
  104. s.quill.insertEmbed(
  105. cursorPosition,
  106. 'AppPanelEmbed',
  107. `<audio src="${globalConfig.avatarUploadHost + data.data}" controls="controls">Your browser does not support the audio element.</audio>`
  108. );
  109. s.quill.setSelection(cursorPosition + 1)
  110. };
  111. },
  112. error: function(XMLHttpRequest, textStatus, errorThrown){
  113. //查看错误信息
  114. message("上传失败");
  115. }
  116. })
  117. }
  118. //视频文件选择
  119. function fileSelectVideo() {
  120. let uploader = document.getElementById("fileToUploadVideo");
  121. if (!uploader.onchange) {
  122. uploader.onchange = () => {
  123. uploadVideo(this)
  124. }
  125. }
  126. uploader.click();
  127. }
  128. //视频文件上传
  129. function uploadVideo(s) {
  130. let myform = new FormData()
  131. let file = document.getElementById("fileToUploadVideo").files[0]
  132. if (!file) { return; };
  133. myform.append('file', file);
  134. myform.append('sign', RichTextUploadSign);
  135. $.ajax({
  136. method: "post",
  137. dataType: "json",
  138. url: location.origin + RichTextUploadUrl,
  139. data: myform,
  140. contentType: false,
  141. processData: false,
  142. success: function (data) {
  143. if (data.error && data.error.length) {
  144. message.warning(data.error[0].message);
  145. } else {
  146. const cursorPosition = s.quill.getSelection().index
  147. s.quill.insertEmbed(
  148. cursorPosition,
  149. 'AppPanelEmbed',
  150. `<video controls="" autoPlay="" name="media">
  151. <source src=${globalConfig.avatarUploadHost + data.data} type="video/mp4">
  152. </video>`
  153. );
  154. s.quill.setSelection(cursorPosition + 1)
  155. };
  156. },
  157. error: function(XMLHttpRequest, textStatus, errorThrown){
  158. //查看错误信息
  159. message("上传失败");
  160. }
  161. })
  162. }
  163. const Editors = React.createClass({
  164. getInitialState() {
  165. return {
  166. value: null,
  167. theBool: true
  168. }
  169. },
  170. handleRichText(value) {
  171. this.setState({ value: value });
  172. this.props.handleRichText(value);
  173. // this.props.handleRichText(value.replace(/iframe/g, 'video'));
  174. },
  175. componentDidMount() {
  176. document.getElementsByClassName('ql-audio')[0].style = 'background: url("../../image/audio.png") no-repeat;background-size: contain;'
  177. },
  178. componentWillMount() {
  179. RichTextUploadUrl = this.props.uploadUrl;
  180. RichTextUploadSign = this.props.uploadSign;
  181. },
  182. componentWillReceiveProps(nextProps) {
  183. this.state.theBool = true;
  184. // if(nextProps.textContent){
  185. this.state.value = nextProps.textContent || '';
  186. // }
  187. },
  188. render() {
  189. const modules = {
  190. toolbar: {
  191. container: [
  192. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  193. [{ 'header': [1, 2, 3, false] }],
  194. ['bold', 'italic', 'underline', 'strike', 'blockquote'], // toggled buttons
  195. // [{ 'header': 1 }, { 'header': 2 }], // custom button values
  196. [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  197. // [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript
  198. [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent
  199. // [{ 'direction': 'rtl' }], // text direction
  200. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  201. // [{ 'font': [] }],
  202. [{ 'align': [] }],
  203. ['link', 'image','video'],
  204. ['clean'],
  205. ['audio']
  206. ],
  207. handlers: { 'image': fileSelect ,'video': fileSelectVideo,'audio':fileSelectAudio}
  208. },
  209. };
  210. const formats = [
  211. 'size', 'header',
  212. 'bold', 'italic', 'underline', 'strike', 'blockquote',
  213. 'list', 'bullet', 'indent',
  214. 'color', 'background',
  215. 'align',
  216. 'link', 'image','video','AppPanelEmbed'
  217. ];
  218. return (
  219. <div>
  220. <ReactQuill
  221. theme="snow"
  222. value={this.state.value}
  223. modules={modules}
  224. formats={formats}
  225. placeholder='请填写内容!'
  226. onChange={this.handleRichText}
  227. />
  228. <input accept="image/*" type="file" name="fileToUpload" id="fileToUpload" placeholder='请填写内容!' style={{ "display": "none" }} />
  229. <input accept="video/*" type="file" name="fileToUploadVideo" id="fileToUploadVideo" placeholder='请填写视频!' style={{ "display": "none" }} />
  230. <input accept="audio/*" type="file" name="fileToUploadAudio" id="fileToUploadAudio" placeholder='请选择音频!' style={{ "display": "none" }} />
  231. </div>
  232. )
  233. },
  234. });
  235. export default Editors;