Explorar el Código

富文本编辑器支持自定义上传地址

RuoYi hace 5 años
padre
commit
d989e4a717
Se han modificado 1 ficheros con 63 adiciones y 2 borrados
  1. 63 2
      ruoyi-ui/src/components/Editor/index.vue

+ 63 - 2
ruoyi-ui/src/components/Editor/index.vue

@@ -1,5 +1,19 @@
1 1
 <template>
2
+  <div>
3
+    <el-upload
4
+      :action="uploadUrl"
5
+      :on-success="handleUploadSuccess"
6
+      :on-error="handleUploadError"
7
+      name="file"
8
+      :show-file-list="false"
9
+      :headers="headers"
10
+      style="display: none"
11
+      ref="upload"
12
+      v-if="this.uploadUrl"
13
+    >
14
+    </el-upload>
2 15
     <div class="editor" ref="editor" :style="styles"></div>
16
+  </div>
3 17
 </template>
4 18
 
5 19
 <script>
@@ -7,6 +21,7 @@ import Quill from "quill";
7 21
 import "quill/dist/quill.core.css";
8 22
 import "quill/dist/quill.snow.css";
9 23
 import "quill/dist/quill.bubble.css";
24
+import { getToken } from "@/utils/auth";
10 25
 
11 26
 export default {
12 27
   name: "Editor",
@@ -30,10 +45,18 @@ export default {
30 45
     readOnly: {
31 46
       type: Boolean,
32 47
       default: false,
48
+    },
49
+    /* 上传地址 */
50
+    uploadUrl: {
51
+      type: String,
52
+      default: "",
33 53
     }
34 54
   },
35 55
   data() {
36 56
     return {
57
+      headers: {
58
+        Authorization: "Bearer " + getToken()
59
+      },
37 60
       Quill: null,
38 61
       currentValue: "",
39 62
       options: {
@@ -52,7 +75,7 @@ export default {
52 75
             [{ color: [] }, { background: [] }],             // 字体颜色、字体背景颜色
53 76
             [{ align: [] }],                                 // 对齐方式
54 77
             ["clean"],                                       // 清除文本格式
55
-            ["link", "image", "video"]                       // 链接、图片、视频
78
+            ["link", "image"]                                // 链接、图片
56 79
           ],
57 80
         },
58 81
         placeholder: "请输入内容",
@@ -95,6 +118,26 @@ export default {
95 118
     init() {
96 119
       const editor = this.$refs.editor;
97 120
       this.Quill = new Quill(editor, this.options);
121
+      // 如果设置了上传地址则自定义图片上传事件
122
+      if (this.uploadUrl) {
123
+        let toolbar = this.Quill.getModule("toolbar");
124
+        toolbar.addHandler("image", (value) => {
125
+          this.uploadType = "image";
126
+          if (value) {
127
+            this.$refs.upload.$children[0].$refs.input.click();
128
+          } else {
129
+            this.quill.format("image", false);
130
+          }
131
+        });
132
+        toolbar.addHandler("video", (value) => {
133
+          this.uploadType = "video";
134
+          if (value) {
135
+            this.$refs.upload.$children[0].$refs.input.click();
136
+          } else {
137
+            this.quill.format("video", false);
138
+          }
139
+        });
140
+      }
98 141
       this.Quill.pasteHTML(this.currentValue);
99 142
       this.Quill.on("text-change", (delta, oldDelta, source) => {
100 143
         const html = this.$refs.editor.children[0].innerHTML;
@@ -114,13 +157,31 @@ export default {
114 157
         this.$emit("on-editor-change", eventName, ...args);
115 158
       });
116 159
     },
160
+    handleUploadSuccess(res, file) {
161
+      // 获取富文本组件实例
162
+      let quill = this.Quill;
163
+      // 如果上传成功
164
+      if (res.code == 200) {
165
+        // 获取光标所在位置
166
+        let length = quill.getSelection().index;
167
+        // 插入图片  res.url为服务器返回的图片地址
168
+        quill.insertEmbed(length, "image", res.data.url);
169
+        // 调整光标到最后
170
+        quill.setSelection(length + 1);
171
+      } else {
172
+        this.$message.error("图片插入失败");
173
+      }
174
+    },
175
+    handleUploadError() {
176
+      this.$message.error("图片插入失败");
177
+    },
117 178
   },
118 179
 };
119 180
 </script>
120 181
 
121 182
 <style>
122 183
 .editor, .ql-toolbar {
123
-  white-space: pre-wrap!important;
184
+  white-space: pre-wrap !important;
124 185
   line-height: normal !important;
125 186
 }
126 187
 .quill-img {