wang.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div>
  3. <div ref='editor1' class="myedit"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import E from 'wangeditor'
  8. export default {
  9. name: 'editoritem',
  10. data() {
  11. return {
  12. index:0,
  13. uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadWang",
  14. editor: null
  15. }
  16. },
  17. created() {
  18. },
  19. beforeDestroy() {
  20. // 销毁编辑器
  21. if(this.editor!=null){
  22. this.editor.destroy()
  23. this.editor = null
  24. }
  25. },
  26. methods:{
  27. initEditor(){
  28. var that=this;
  29. if(this.editor==null){
  30. //创建编辑器
  31. this.editor = new E(that.$refs.editor1 )
  32. this.editor.config.uploadImgServer = this.uploadUrl;
  33. this.editor.config.uploadImgMaxLength = 5
  34. this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 2M
  35. this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']
  36. this.editor.config.uploadFileName = 'file'
  37. this.editor.config.zIndex = 1
  38. this.editor.config.menus = [
  39. 'head',
  40. 'bold',
  41. 'fontSize',
  42. 'fontName',
  43. 'italic',
  44. 'underline',
  45. 'strikeThrough',
  46. 'indent',
  47. 'lineHeight',
  48. 'foreColor',
  49. 'backColor',
  50. 'link',
  51. 'list',
  52. 'todo',
  53. 'justify',
  54. 'quote',
  55. 'emoticon',
  56. 'image',
  57. // 'video',
  58. 'table',
  59. 'code',
  60. 'splitLine',
  61. 'undo',
  62. 'redo',
  63. ]
  64. this.editor.config.onchange = function (newHtml) {
  65. console.log(newHtml)
  66. that.$emit("on-text-change", newHtml);
  67. }
  68. this.editor.config.pasteFilterStyle = false
  69. // 配置触发 onchange 的时间频率,默认为 200ms
  70. this.editor.config.onchangeTimeout = 500 // 修改为 500ms
  71. this.editor.config.uploadImgHooks = {
  72. // 上传图片之前
  73. before: function(xhr) {
  74. //console.log(xhr)
  75. },
  76. // 图片上传并返回了结果,图片插入已成功
  77. success: function(xhr) {
  78. //console.log('success', xhr)
  79. },
  80. // 图片上传并返回了结果,但图片插入时出错了
  81. fail: function(xhr, editor, resData) {
  82. console.log('fail', resData)
  83. },
  84. // 上传图片出错,一般为 http 请求的错误
  85. error: function(xhr, editor, resData) {
  86. console.log('error', xhr, resData)
  87. },
  88. // 上传图片超时
  89. timeout: function(xhr) {
  90. //console.log('timeout')
  91. },
  92. // 图片上传并返回了结果,想要自己把图片插入到编辑器中
  93. // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
  94. customInsert: function(insertImg, result, editor) {
  95. console.log(result);
  96. // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
  97. // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
  98. // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
  99. // var url = result.data[0].url
  100. if(result.data!=null){
  101. for(var i=0;i<result.data.length;i++){
  102. var url = result.data[i].url
  103. insertImg(url)
  104. }
  105. console.log(url);
  106. }
  107. // result 必须是一个 JSON 格式字符串!!!否则报错
  108. }
  109. }
  110. this.editor.create()
  111. }
  112. this.editor.txt.html("");
  113. },
  114. setText(text){
  115. if(this.editor==null){
  116. this.initEditor()
  117. }
  118. if(text==undefined||text==null){
  119. this.editor.txt.html("");
  120. }
  121. else{
  122. this.editor.txt.html(text);
  123. }
  124. },
  125. }
  126. }
  127. </script>
  128. <style scoped>
  129. .toolbar {
  130. border: 1px solid #ccc;
  131. }
  132. .myedit {
  133. min-height: 300px;
  134. z-index: 1 !important;
  135. }
  136. .w-e-toolbar{
  137. z-index: 1 !important;
  138. }
  139. .w-e-text-container{
  140. z-index: 1 !important;
  141. }
  142. </style>