ソースを参照

富文本复制粘贴图片上传至url

RuoYi 11 ヶ月 前
コミット
1a0f37a2dc
共有1 個のファイルを変更した25 個の追加2 個の削除を含む
  1. 25 2
      ruoyi-ui/src/components/Editor/index.vue

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

@@ -18,6 +18,7 @@
18 18
 </template>
19 19
 
20 20
 <script>
21
+import axios from "axios";
21 22
 import Quill from "quill";
22 23
 import "quill/dist/quill.core.css";
23 24
 import "quill/dist/quill.snow.css";
@@ -135,6 +136,7 @@ export default {
135 136
             this.quill.format("image", false);
136 137
           }
137 138
         });
139
+        this.Quill.root.addEventListener('paste', this.handlePasteCapture, true);
138 140
       }
139 141
       this.Quill.clipboard.dangerouslyPasteHTML(this.currentValue);
140 142
       this.Quill.on("text-change", (delta, oldDelta, source) => {
@@ -192,8 +194,29 @@ export default {
192 194
     handleUploadError() {
193 195
       this.$message.error("图片插入失败");
194 196
     },
195
-  },
196
-};
197
+    // 复制粘贴图片处理
198
+    handlePasteCapture(e) {
199
+      const clipboard = e.clipboardData || window.clipboardData;
200
+      if (clipboard && clipboard.items) {
201
+        for (let i = 0; i < clipboard.items.length; i++) {
202
+          const item = clipboard.items[i];
203
+          if (item.type.indexOf('image') !== -1) {
204
+            e.preventDefault();
205
+            const file = item.getAsFile();
206
+            this.insertImage(file);
207
+          }
208
+        }
209
+      }
210
+    },
211
+    insertImage(file) {
212
+      const formData = new FormData();
213
+      formData.append("file", file);
214
+      axios.post(this.uploadUrl, formData, { headers: { "Content-Type": "multipart/form-data", Authorization: this.headers.Authorization } }).then(res => {
215
+        this.handleUploadSuccess(res.data);
216
+      })
217
+    }
218
+  }
219
+}
197 220
 </script>
198 221
 
199 222
 <style>