wang.vue 4.4 KB

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