wang.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. selectedImageCount: 0, // 记录选择的图片数量
  15. uploadedImageCount: 0 // 记录已上传的图片数量
  16. }
  17. },
  18. beforeDestroy() {
  19. if (this.editor != null) {
  20. this.editor.destroy()
  21. this.editor = null
  22. }
  23. },
  24. methods: {
  25. initEditor() {
  26. const that = this;
  27. if (this.editor == null) {
  28. this.editor = new E(that.$refs.editor1)
  29. this.editor.config.uploadImgServer = this.uploadUrl;
  30. this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 2M
  31. this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']
  32. this.editor.config.uploadFileName = 'fileName'
  33. this.editor.config.zIndex = 1
  34. // 关键配置:允许多选,串行上传
  35. this.editor.config.uploadImgMultiple = true;
  36. this.editor.config.uploadImgMaxLength = 5;
  37. this.editor.config.uploadImgConcurrent = 1;
  38. // 重写图片上传逻辑以获取选择的图片数量
  39. this.editor.config.customUploadImg = function (files, insertImgFn) {
  40. // 获取选择的图片数量
  41. that.selectedImageCount = files.length;
  42. that.uploadedImageCount = 0;
  43. console.log(`已选择 ${that.selectedImageCount} 张图片,准备开始上传`);
  44. that.$emit('image-selected', that.selectedImageCount);
  45. const uploadNext = (index) => {
  46. if (index >= files.length) {
  47. console.log(`所有 ${files.length} 张图片上传处理完毕`);
  48. that.$emit('all-images-uploaded', that.uploadedImageCount);
  49. return;
  50. }
  51. const formData = new FormData();
  52. formData.append(that.editor.config.uploadFileName, files[index]);
  53. const xhr = new XMLHttpRequest();
  54. xhr.open('POST', that.uploadUrl);
  55. xhr.onload = function () {
  56. if (xhr.status >= 200 && xhr.status < 300) {
  57. const result = JSON.parse(xhr.responseText);
  58. if (result.errno === 0 && result.data && result.data[0] && result.data[0].url) {
  59. insertImgFn(result.data[0].url);
  60. that.uploadedImageCount++;
  61. that.$emit('image-uploaded', that.uploadedImageCount, that.selectedImageCount);
  62. } else {
  63. console.error(`第 ${index+1} 张图片上传失败:`, result);
  64. that.$emit('image-upload-error', index+1);
  65. }
  66. } else {
  67. console.error(`第 ${index+1} 张图片上传请求失败:`, xhr.statusText);
  68. that.$emit('image-upload-error', index+1);
  69. }
  70. uploadNext(index + 1);
  71. };
  72. xhr.onerror = function () {
  73. console.error(`第 ${index+1} 张图片上传出错`);
  74. that.$emit('image-upload-error', index+1);
  75. uploadNext(index + 1);
  76. };
  77. // 发送请求
  78. xhr.send(formData);
  79. };
  80. // 开始上传第一张
  81. uploadNext(0);
  82. };
  83. this.editor.config.menus = [
  84. 'head', 'bold', 'fontSize', 'fontName', 'italic', 'underline',
  85. 'strikeThrough', 'indent', 'lineHeight', 'foreColor', 'backColor',
  86. 'link', 'list', 'todo', 'justify', 'quote', 'emoticon', 'image',
  87. 'table', 'code', 'splitLine', 'undo', 'redo'
  88. ]
  89. this.editor.config.onchange = function (newHtml) {
  90. that.$emit("on-text-change", newHtml);
  91. }
  92. this.editor.config.pasteFilterStyle = false
  93. this.editor.config.onchangeTimeout = 500
  94. this.editor.create()
  95. }
  96. this.editor.txt.html("");
  97. },
  98. setText(text) {
  99. if (this.editor == null) {
  100. this.initEditor()
  101. }
  102. this.editor.txt.html(text || "");
  103. },
  104. getSelectedImageCount() {
  105. return this.selectedImageCount;
  106. },
  107. getUploadedImageCount() {
  108. return this.uploadedImageCount;
  109. }
  110. }
  111. }
  112. </script>
  113. <style scoped>
  114. .myedit {
  115. min-height: 300px;
  116. z-index: 1 !important;
  117. }
  118. .w-e-toolbar, .w-e-text-container {
  119. z-index: 1 !important;
  120. }
  121. </style>