Commit a7feb9279fb5c482e2111a4124326c0d5552971c
1 parent
4bf5b04d
v3.5.0 富文本编辑器支持自定义上传地址
Showing
2 changed files
with
38 additions
and
16 deletions
yudao-ui-admin/src/components/Editor/index.vue
| ... | ... | @@ -3,14 +3,13 @@ |
| 3 | 3 | <el-upload |
| 4 | 4 | :action="uploadUrl" |
| 5 | 5 | :on-success="handleUploadSuccess" |
| 6 | - :before-upload="handleBeforeUpload" | |
| 7 | 6 | :on-error="handleUploadError" |
| 8 | 7 | name="file" |
| 9 | 8 | :show-file-list="false" |
| 10 | 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 | 14 | </el-upload> |
| 16 | 15 | <div class="editor" ref="editor" :style="styles"></div> |
| ... | ... | @@ -22,6 +21,7 @@ import Quill from "quill"; |
| 22 | 21 | import "quill/dist/quill.core.css"; |
| 23 | 22 | import "quill/dist/quill.snow.css"; |
| 24 | 23 | import "quill/dist/quill.bubble.css"; |
| 24 | +import { getToken } from "@/utils/auth"; | |
| 25 | 25 | |
| 26 | 26 | export default { |
| 27 | 27 | name: "Editor", |
| ... | ... | @@ -49,11 +49,14 @@ export default { |
| 49 | 49 | /* 上传地址 */ |
| 50 | 50 | uploadUrl: { |
| 51 | 51 | type: String, |
| 52 | - default: '', | |
| 52 | + default: "", | |
| 53 | 53 | } |
| 54 | 54 | }, |
| 55 | 55 | data() { |
| 56 | 56 | return { |
| 57 | + headers: { | |
| 58 | + Authorization: "Bearer " + getToken() | |
| 59 | + }, | |
| 57 | 60 | Quill: null, |
| 58 | 61 | currentValue: "", |
| 59 | 62 | options: { |
| ... | ... | @@ -72,7 +75,7 @@ export default { |
| 72 | 75 | [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色 |
| 73 | 76 | [{ align: [] }], // 对齐方式 |
| 74 | 77 | ["clean"], // 清除文本格式 |
| 75 | - ["link", "image", "video"] // 链接、图片、视频 | |
| 78 | + ["link", "image"] // 链接、图片 | |
| 76 | 79 | ], |
| 77 | 80 | }, |
| 78 | 81 | placeholder: "请输入内容", |
| ... | ... | @@ -115,23 +118,23 @@ export default { |
| 115 | 118 | init() { |
| 116 | 119 | const editor = this.$refs.editor; |
| 117 | 120 | this.Quill = new Quill(editor, this.options); |
| 118 | - // 如果设置了上传地址则自定义图片和视频的上传事件 | |
| 121 | + // 如果设置了上传地址则自定义图片上传事件 | |
| 119 | 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 | 126 | if (value) { |
| 124 | 127 | this.$refs.upload.$children[0].$refs.input.click(); |
| 125 | 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 | 134 | if (value) { |
| 132 | 135 | this.$refs.upload.$children[0].$refs.input.click(); |
| 133 | 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 | 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 | 180 | </script> |
| 160 | 181 | |
| 161 | 182 | <style> |
| 162 | 183 | .editor, .ql-toolbar { |
| 163 | - white-space: pre-wrap!important; | |
| 184 | + white-space: pre-wrap !important; | |
| 164 | 185 | line-height: normal !important; |
| 165 | 186 | } |
| 166 | 187 | .quill-img { | ... | ... |
更新日志.md
| ... | ... | @@ -32,7 +32,8 @@ TODO |
| 32 | 32 | * 【优化】使用 Lombok 简化 JsonUtils 工具类 [#73](https://github.com/YunaiV/ruoyi-vue-pro/pull/73) |
| 33 | 33 | * 【新增】兼容 Node 16 版本,通过升级 BPMN-JS 相关库 [commit](https://gitee.com/zhijiantianya/ruoyi-vue-pro/commit/4c169cbc58eb0d0cc4aead73ca127a2957e6a3aa) |
| 34 | 34 | * 【新增】前端的表格右侧工具栏组件支持显隐列,具体可见【用户管理】功能 [commit](https://gitee.com/zhijiantianya/ruoyi-vue-pro/commit/20e34e35a3bdc48e6a4c58e3849aa729bd18efe5) |
| 35 | -* 【新增】跟进 ruoyi-vue 3.4.0 版本 | |
| 35 | +* 【新增】菜单导航显示风格 TopNav(false 为 左侧导航菜单,true 为顶部导航菜单) [commit](https://gitee.com/zhijiantianya/ruoyi-vue-pro/commit/4bf5b04d542014a49c5a41b20935cef35033a518) | |
| 36 | +* 【新增】跟进 ruoyi-vue 3.4.0、3.5.0 版本,感谢这么优秀的开源项目! | |
| 36 | 37 | |
| 37 | 38 | ### 🐞 Bug Fixes |
| 38 | 39 | ... | ... |