作者 RuoYi

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

@@ -3,14 +3,13 @@ @@ -3,14 +3,13 @@
3 <el-upload 3 <el-upload
4 :action="uploadUrl" 4 :action="uploadUrl"
5 :on-success="handleUploadSuccess" 5 :on-success="handleUploadSuccess"
6 - :before-upload="handleBeforeUpload"  
7 :on-error="handleUploadError" 6 :on-error="handleUploadError"
8 name="file" 7 name="file"
9 :show-file-list="false" 8 :show-file-list="false"
10 :headers="headers" 9 :headers="headers"
11 - style="display: none;"  
12 - ref='upload'  
13 - v-if='this.uploadUrl' 10 + style="display: none"
  11 + ref="upload"
  12 + v-if="this.uploadUrl"
14 > 13 >
15 </el-upload> 14 </el-upload>
16 <div class="editor" ref="editor" :style="styles"></div> 15 <div class="editor" ref="editor" :style="styles"></div>
@@ -22,6 +21,7 @@ import Quill from "quill"; @@ -22,6 +21,7 @@ import Quill from "quill";
22 import "quill/dist/quill.core.css"; 21 import "quill/dist/quill.core.css";
23 import "quill/dist/quill.snow.css"; 22 import "quill/dist/quill.snow.css";
24 import "quill/dist/quill.bubble.css"; 23 import "quill/dist/quill.bubble.css";
  24 +import { getToken } from "@/utils/auth";
25 25
26 export default { 26 export default {
27 name: "Editor", 27 name: "Editor",
@@ -49,11 +49,14 @@ export default { @@ -49,11 +49,14 @@ export default {
49 /* 上传地址 */ 49 /* 上传地址 */
50 uploadUrl: { 50 uploadUrl: {
51 type: String, 51 type: String,
52 - default: '', 52 + default: "",
53 } 53 }
54 }, 54 },
55 data() { 55 data() {
56 return { 56 return {
  57 + headers: {
  58 + Authorization: "Bearer " + getToken()
  59 + },
57 Quill: null, 60 Quill: null,
58 currentValue: "", 61 currentValue: "",
59 options: { 62 options: {
@@ -72,7 +75,7 @@ export default { @@ -72,7 +75,7 @@ export default {
72 [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色 75 [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
73 [{ align: [] }], // 对齐方式 76 [{ align: [] }], // 对齐方式
74 ["clean"], // 清除文本格式 77 ["clean"], // 清除文本格式
75 - ["link", "image", "video"] // 链接、图片、视频 78 + ["link", "image"] // 链接、图片
76 ], 79 ],
77 }, 80 },
78 placeholder: "请输入内容", 81 placeholder: "请输入内容",
@@ -115,23 +118,23 @@ export default { @@ -115,23 +118,23 @@ export default {
115 init() { 118 init() {
116 const editor = this.$refs.editor; 119 const editor = this.$refs.editor;
117 this.Quill = new Quill(editor, this.options); 120 this.Quill = new Quill(editor, this.options);
118 - // 如果设置了上传地址则自定义图片和视频的上传事件 121 + // 如果设置了上传地址则自定义图片上传事件
119 if (this.uploadUrl) { 122 if (this.uploadUrl) {
120 - let toolbar = this.Quill.getModule('toolbar');  
121 - toolbar.addHandler('image', (value) => {  
122 - this.uploadType = 'image'; 123 + let toolbar = this.Quill.getModule("toolbar");
  124 + toolbar.addHandler("image", (value) => {
  125 + this.uploadType = "image";
123 if (value) { 126 if (value) {
124 this.$refs.upload.$children[0].$refs.input.click(); 127 this.$refs.upload.$children[0].$refs.input.click();
125 } else { 128 } else {
126 - this.quill.format('image', false); 129 + this.quill.format("image", false);
127 } 130 }
128 }); 131 });
129 - toolbar.addHandler('video', (value) => {  
130 - this.uploadType = 'video'; 132 + toolbar.addHandler("video", (value) => {
  133 + this.uploadType = "video";
131 if (value) { 134 if (value) {
132 this.$refs.upload.$children[0].$refs.input.click(); 135 this.$refs.upload.$children[0].$refs.input.click();
133 } else { 136 } else {
134 - this.quill.format('video', false); 137 + this.quill.format("video", false);
135 } 138 }
136 }); 139 });
137 } 140 }
@@ -154,13 +157,31 @@ export default { @@ -154,13 +157,31 @@ export default {
154 this.$emit("on-editor-change", eventName, ...args); 157 this.$emit("on-editor-change", eventName, ...args);
155 }); 158 });
156 }, 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.url);
  169 + // 调整光标到最后
  170 + quill.setSelection(length + 1);
  171 + } else {
  172 + this.$message.error("图片插入失败");
  173 + }
  174 + },
  175 + handleUploadError() {
  176 + this.$message.error("图片插入失败");
  177 + },
157 }, 178 },
158 }; 179 };
159 </script> 180 </script>
160 181
161 <style> 182 <style>
162 .editor, .ql-toolbar { 183 .editor, .ql-toolbar {
163 - white-space: pre-wrap!important; 184 + white-space: pre-wrap !important;
164 line-height: normal !important; 185 line-height: normal !important;
165 } 186 }
166 .quill-img { 187 .quill-img {