RichEditor.vue
<template>
<div class="root" style="border: 1px solid #ccc margin-top: 10px">
<!-- 工具栏 -->
<Toolbar :defaultConfig="toolbarConfig" :editor="editor" style="border-bottom: 1px solid #ccc" />
<!-- 编辑器 -->
<div class="editor-area">
<div class="area">编辑区</div>
<Editor :defaultConfig="editorConfig" :value="value" @onCreated="onCreated" class="editor" v-on="$listeners" />
<div class="area">预览区</div>
<!-- <div class="editor preview" v-html="$richTextAddOssToken(value)"></div> -->
<div class="editor preview" v-html="value"></div>
<div class="area">[注:小程序端不同手机屏幕像素不同,显示略有差异]</div>
</div>
</div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue"
import { imageUpload, imageDelete } from '@/api/basic/system'
export default {
name: "RichEditor",
components: { Editor, Toolbar },
props: {
value: ''
},
data () {
return {
editor: null,
toolbarConfig: {
excludeKeys: ['fontFamily', 'group-video', 'fullScreen', 'insertLink', 'blockquote']
},
editorConfig: {
placeholder: "请输入内容...",
MENU_CONF: {
uploadImage: {
maxFileSize: 10 * 1024 * 1024,
maxNumberOfFiles: 100,
allowedFileTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/bmp'],
timeout: 30000
}
}
},
originalImgs: [],
insertedImgs: []
}
},
methods: {
onCreated (editor) {
this.editor = Object.seal(editor)
},
setOriginalImgs (html) {
this.originalImgs = html.match(/evo-oss[0-9a-z/.-]*\?/g).map(item => item.replace('evo-oss/', '').replace('?', ''))
},
updateImgs () {
let finalImgs = this.editor.getElemsByType('image').map(item => item.src.match(/evo-oss[0-9a-z/.-]*\?/g)[0].replace('evo-oss/', '').replace('?', ''))
let allServerImgs = this.originalImgs.concat(this.insertedImgs)
let wastedImgs = allServerImgs.filter(i => finalImgs.indexOf(i) === -1)
this.originalImgs = [].concat(finalImgs)
this.insertedImgs = []
wastedImgs.length && imageDelete(wastedImgs)
this.editor.history.redos = []
this.editor.history.undos = []
},
deleteWastedImgs () {
this.insertedImgs.length && imageDelete(this.insertedImgs)
}
},
created () {
this.editorConfig.MENU_CONF.uploadImage.customUpload = async (image, insertFn) => {
const form = new FormData()
form.append('image', image)
let res = await imageUpload(form)
if (res.success) {
console.log('上传了', res.data)
this.insertedImgs.push(res.data)
insertFn(`/evo-apigw/evo-oss/${res.data}?token=${localStorage.getItem('accessToken')}`, '加载失败')
}
}
},
beforeDestroy () {
console.log('RichEditor beforeDestroy')
const editor = this.editor
if (editor == null) return
editor.destroy()
}
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="less" scoped>
.root {
position: relative;
}
.area {
display: inline-block;
vertical-align: top;
max-width: 135px;
text-align: justify;
color: rgb(140, 140, 140);
}
.editor {
display: inline-block;
width: 330px;
height: 700px;
overflow-y: scroll;
border: 1px solid rgb(204, 204, 204);
border-radius: 5px;
}
.preview {
width: 330px;
vertical-align: top;
padding: 16px;
}
.editor-area {
margin: 0 auto;
margin-top: 10px;
display: flex;
justify-content: space-between;
}
</style>