Просмотр исходного кода

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

RuoYi лет назад: 5
Родитель
Сommit
d989e4a717
1 измененных файлов с 63 добавлено и 2 удалено
  1. 63 2
      ruoyi-ui/src/components/Editor/index.vue

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

@@ -1,5 +1,19 @@
1
 <template>
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
     <div class="editor" ref="editor" :style="styles"></div>
15
     <div class="editor" ref="editor" :style="styles"></div>
16
+  </div>
3
 </template>
17
 </template>
4
 
18
 
5
 <script>
19
 <script>
@@ -7,6 +21,7 @@ import Quill from "quill";
7
 import "quill/dist/quill.core.css";
21
 import "quill/dist/quill.core.css";
8
 import "quill/dist/quill.snow.css";
22
 import "quill/dist/quill.snow.css";
9
 import "quill/dist/quill.bubble.css";
23
 import "quill/dist/quill.bubble.css";
24
+import { getToken } from "@/utils/auth";
10
 
25
 
11
 export default {
26
 export default {
12
   name: "Editor",
27
   name: "Editor",
@@ -30,10 +45,18 @@ export default {
30
     readOnly: {
45
     readOnly: {
31
       type: Boolean,
46
       type: Boolean,
32
       default: false,
47
       default: false,
48
+    },
49
+    /* 上传地址 */
50
+    uploadUrl: {
51
+      type: String,
52
+      default: "",
33
     }
53
     }
34
   },
54
   },
35
   data() {
55
   data() {
36
     return {
56
     return {
57
+      headers: {
58
+        Authorization: "Bearer " + getToken()
59
+      },
37
       Quill: null,
60
       Quill: null,
38
       currentValue: "",
61
       currentValue: "",
39
       options: {
62
       options: {
@@ -52,7 +75,7 @@ export default {
52
             [{ color: [] }, { background: [] }],             // 字体颜色、字体背景颜色
75
             [{ color: [] }, { background: [] }],             // 字体颜色、字体背景颜色
53
             [{ align: [] }],                                 // 对齐方式
76
             [{ align: [] }],                                 // 对齐方式
54
             ["clean"],                                       // 清除文本格式
77
             ["clean"],                                       // 清除文本格式
55
-            ["link", "image", "video"]                       // 链接、图片、视频
78
+            ["link", "image"]                                // 链接、图片
56
           ],
79
           ],
57
         },
80
         },
58
         placeholder: "请输入内容",
81
         placeholder: "请输入内容",
@@ -95,6 +118,26 @@ export default {
95
     init() {
118
     init() {
96
       const editor = this.$refs.editor;
119
       const editor = this.$refs.editor;
97
       this.Quill = new Quill(editor, this.options);
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
       this.Quill.pasteHTML(this.currentValue);
141
       this.Quill.pasteHTML(this.currentValue);
99
       this.Quill.on("text-change", (delta, oldDelta, source) => {
142
       this.Quill.on("text-change", (delta, oldDelta, source) => {
100
         const html = this.$refs.editor.children[0].innerHTML;
143
         const html = this.$refs.editor.children[0].innerHTML;
@@ -114,13 +157,31 @@ export default {
114
         this.$emit("on-editor-change", eventName, ...args);
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
 </script>
180
 </script>
120
 
181
 
121
 <style>
182
 <style>
122
 .editor, .ql-toolbar {
183
 .editor, .ql-toolbar {
123
-  white-space: pre-wrap!important;
184
+  white-space: pre-wrap !important;
124
   line-height: normal !important;
185
   line-height: normal !important;
125
 }
186
 }
126
 .quill-img {
187
 .quill-img {