richTextEditor.jsx 10 KB

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