|
|
@ -15,14 +15,16 @@ |
|
|
|
<div ref="editor" |
|
|
|
<div ref="editor" |
|
|
|
:style="styles" |
|
|
|
:style="styles" |
|
|
|
v-loading="loading"></div> |
|
|
|
v-loading="loading"></div> |
|
|
|
<el-upload :headers="headers" |
|
|
|
<Upload :max-size="1000" |
|
|
|
:action="this.api.fileupload" |
|
|
|
:limit="100" |
|
|
|
:before-upload="beforeUpload" |
|
|
|
@beforeUpload="beforeUpload" |
|
|
|
:on-success="editorUploadSuccess" |
|
|
|
@onSuccess="editorUploadSuccess" |
|
|
|
style="display: none"> |
|
|
|
style="display: none"> |
|
|
|
<el-button class="editorUpload" |
|
|
|
<div slot="trigger"> |
|
|
|
type="primary">点击上传</el-button> |
|
|
|
<el-button :id="'editorUpload' + index" |
|
|
|
</el-upload> |
|
|
|
type="primary">点击上传</el-button> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
</Upload> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
<mavon-editor class="md" |
|
|
|
<mavon-editor class="md" |
|
|
|
v-model="mdVal" |
|
|
|
v-model="mdVal" |
|
|
@ -44,10 +46,13 @@ import toolbarOptions from './options' |
|
|
|
import axios from 'axios' |
|
|
|
import axios from 'axios' |
|
|
|
import { mavonEditor } from 'mavon-editor' |
|
|
|
import { mavonEditor } from 'mavon-editor' |
|
|
|
import 'mavon-editor/dist/css/index.css' |
|
|
|
import 'mavon-editor/dist/css/index.css' |
|
|
|
|
|
|
|
import Upload from '@/components/upload'; |
|
|
|
|
|
|
|
import Oss from '@/components/upload/upload.js' |
|
|
|
|
|
|
|
|
|
|
|
export default { |
|
|
|
export default { |
|
|
|
name: 'quill', |
|
|
|
name: 'quill', |
|
|
|
components: { |
|
|
|
components: { |
|
|
|
|
|
|
|
Upload, |
|
|
|
mavonEditor |
|
|
|
mavonEditor |
|
|
|
}, |
|
|
|
}, |
|
|
|
props: { |
|
|
|
props: { |
|
|
@ -80,6 +85,11 @@ export default { |
|
|
|
elseRead: { |
|
|
|
elseRead: { |
|
|
|
type: String, default: 'false' |
|
|
|
type: String, default: 'false' |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
// 当前富文本的索引。一个页面引入多个富文本的时候会无法获取到对应的实例,所以通过在父级存储实例的方式来保存 |
|
|
|
|
|
|
|
index: { |
|
|
|
|
|
|
|
type: Number, |
|
|
|
|
|
|
|
default: 0 |
|
|
|
|
|
|
|
}, |
|
|
|
// 是否需要编辑器切换 |
|
|
|
// 是否需要编辑器切换 |
|
|
|
radio: { |
|
|
|
radio: { |
|
|
|
type: Boolean, |
|
|
|
type: Boolean, |
|
|
@ -92,6 +102,7 @@ export default { |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
data () { |
|
|
|
data () { |
|
|
|
|
|
|
|
const that = this |
|
|
|
return { |
|
|
|
return { |
|
|
|
headers: { |
|
|
|
headers: { |
|
|
|
token: sessionStorage.getItem('token') |
|
|
|
token: sessionStorage.getItem('token') |
|
|
@ -111,7 +122,7 @@ export default { |
|
|
|
'image': function (value) { |
|
|
|
'image': function (value) { |
|
|
|
if (value) { |
|
|
|
if (value) { |
|
|
|
// 调用iview图片上传 |
|
|
|
// 调用iview图片上传 |
|
|
|
document.querySelector('.editorUpload').click() |
|
|
|
document.querySelector("#editorUpload" + that.index).click(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.Quill.format('image', false); |
|
|
|
this.Quill.format('image', false); |
|
|
|
} |
|
|
|
} |
|
|
@ -232,20 +243,16 @@ export default { |
|
|
|
if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) { |
|
|
|
if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
const param = new FormData() |
|
|
|
// 把图片上传到oss,不然会直接把base64存到数据库 |
|
|
|
param.append('file', file) |
|
|
|
Oss.upload(file).then(res => { |
|
|
|
// 把图片上传到服务器,不然会直接把base64存到数据库 |
|
|
|
|
|
|
|
this.$post(this.api.fileupload, param, { |
|
|
|
|
|
|
|
headers: { "Content-Type": "multipart/form-data" } |
|
|
|
|
|
|
|
}).then(res => { |
|
|
|
|
|
|
|
var range = ins.getSelection() |
|
|
|
var range = ins.getSelection() |
|
|
|
if (range) { |
|
|
|
if (range) { |
|
|
|
// 在当前光标位置插入图片 |
|
|
|
// 在当前光标位置插入图片 |
|
|
|
ins.insertEmbed(range.index, 'image', res.data.filesResult.fileUrl) |
|
|
|
ins.insertEmbed(range.index, 'image', res.url) |
|
|
|
// 将光标移动到图片后面 |
|
|
|
// 将光标移动到图片后面 |
|
|
|
ins.setSelection(range.index + 1) |
|
|
|
ins.setSelection(range.index + 1) |
|
|
|
} |
|
|
|
} |
|
|
|
}).catch(res => { }) |
|
|
|
}) |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}, false) |
|
|
|
}, false) |
|
|
@ -254,11 +261,11 @@ export default { |
|
|
|
this.loading = true |
|
|
|
this.loading = true |
|
|
|
}, |
|
|
|
}, |
|
|
|
// quill图片上传 |
|
|
|
// quill图片上传 |
|
|
|
editorUploadSuccess (res) { |
|
|
|
editorUploadSuccess (file) { |
|
|
|
// 获取富文本组件实例 |
|
|
|
// 获取富文本组件实例 |
|
|
|
let quill = this.Quill |
|
|
|
let quill = this.Quill |
|
|
|
// 如果上传成功 |
|
|
|
// 如果上传成功 |
|
|
|
if (res.data.filesResult.fileUrl) { |
|
|
|
if (file.url) { |
|
|
|
// 获取光标所在位置 |
|
|
|
// 获取光标所在位置 |
|
|
|
let lengths; |
|
|
|
let lengths; |
|
|
|
if (quill.getSelection() == null) { |
|
|
|
if (quill.getSelection() == null) { |
|
|
@ -267,7 +274,7 @@ export default { |
|
|
|
lengths = quill.getSelection().index; |
|
|
|
lengths = quill.getSelection().index; |
|
|
|
} |
|
|
|
} |
|
|
|
// 插入图片,res为服务器返回的图片链接地址 |
|
|
|
// 插入图片,res为服务器返回的图片链接地址 |
|
|
|
quill.insertEmbed(lengths, 'image', res.data.filesResult.fileUrl) |
|
|
|
quill.insertEmbed(lengths, 'image', file.url) |
|
|
|
// 调整光标到最后 |
|
|
|
// 调整光标到最后 |
|
|
|
quill.setSelection(lengths + 1) |
|
|
|
quill.setSelection(lengths + 1) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -283,19 +290,9 @@ export default { |
|
|
|
// markdown图片上传 |
|
|
|
// markdown图片上传 |
|
|
|
imgAdd (pos, $file) { |
|
|
|
imgAdd (pos, $file) { |
|
|
|
let $vm = this.$refs.md |
|
|
|
let $vm = this.$refs.md |
|
|
|
// 第一步.将图片上传到服务器. |
|
|
|
// 将图片上传到oss |
|
|
|
const formData = new FormData(); |
|
|
|
Oss.upload($file).then(res => { |
|
|
|
formData.append('file', $file); |
|
|
|
$vm.$img2Url(pos, res.url); |
|
|
|
axios({ |
|
|
|
|
|
|
|
url: this.api.fileupload, |
|
|
|
|
|
|
|
method: 'post', |
|
|
|
|
|
|
|
data: formData, |
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
|
|
token: this.token, |
|
|
|
|
|
|
|
'Content-Type': 'multipart/form-data' |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}).then((res) => { |
|
|
|
|
|
|
|
$vm.$img2Url(pos, res.data.data.filesResult.fileUrl); |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|