一、安装依赖
npm install wangeditor -S
二、封装成一个组件(Wangeditor.vue)
<template lang="html">
<div class="editor">
<div ref="toolbar" class="toolbar"></div>
<div ref="editor" class="text"></div>
</div>
</template>
<script>
import mixin from '@/mixins/mixin'
import E from 'wangeditor'
import { imgUploadUrl } from '@/api/url.js'
import SparkMD5 from 'spark-md5'
import { upload } from '@/api/api.js'
export default {
name: 'Wangeditor',
mixins: [mixin],
props: {
value: {
type: String,
default: ''
},
isClear: {
type: Boolean,
default: false
},
isDisabled: {
type: Boolean,
default: false
}
},
watch: {
isClear(val) {
if (val) {
this.editor.txt.clear()
this.info_ = null
}
},
value(value) {
if (value !== this.editor.txt.html()) {
this.editor.txt.html(this.value)
}
}
},
model: {
prop: 'value',
event: 'change'
},
data() {
return {
info_: null,
editor: null
}
},
mounted() {
this.setEditor()
this.editor.txt.html(this.value)
if (this.isDisabled) {
this.editor.$textElem.attr('contenteditable', !this.isDisabled)
}
},
methods: {
setEditor() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor)
this.editor.config.uploadImgShowBase64 = true
this.editor.config.uploadImgServer = imgUploadUrl
this.editor.config.uploadImgHeaders = {}
this.editor.config.uploadFileName = 'file'
this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024
this.editor.config.uploadImgMaxLength = 1
this.editor.config.uploadImgTimeout = 3 * 60 * 1000
this.editor.config.menus = [
'head',
'bold',
'fontSize',
'fontName',
'italic',
'underline',
'strikeThrough',
'foreColor',
'backColor',
'link',
'list',
'justify',
'quote',
'emoticon',
'image',
'table',
'video',
'code',
'undo',
'redo',
'fullscreen'
]
this.editor.config.uploadImgHooks = {
before: (xhr, editor, files) => {
},
success: (xhr, editor, result) => {
},
fail: (xhr, editor, result) => {
},
error: (xhr, editor) => {
}
}
this.editor.config.customUploadImg = (files, insert) => {
const formData = new FormData()
const name = files[0].name.replace(/.+\./, '')
formData.append('file', files[0])
formData.append('suffix', name)
const fileReader = new FileReader()
const dataFile = files[0]
const spark = new SparkMD5.ArrayBuffer()
fileReader.readAsArrayBuffer(dataFile)
fileReader.onload = e => {
spark.append(e.target.result)
const md5 = spark.end()
formData.append('md5', md5)
upload(imgUploadUrl, formData).then(res => {
if (res.resultCode === 1) {
const imgUrl = this.getImgUrl(res.data.id)
insert(imgUrl)
}
})
}
}
this.editor.config.onchange = html => {
this.info_ = html
this.$emit('change', this.info_)
}
this.editor.create()
}
}
}
</script>
<style lang="scss" scoped>
.editor {
width: 100%;
.toolbar {
border: 1px solid #cccccc;
}
.text {
height: 250px;
border: 1px solid #cccccc;
border-top: none;
::v-deep .w-e-text::-webkit-scrollbar {
width: 10px;
height: 10px;
background-color: transparent;
}
::v-deep .w-e-text::-webkit-scrollbar-track {
background-color: transparent;
}
::v-deep .w-e-text::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #c4c6cc;
}
}
}
</style>
三、在父组件中使用
<el-form-item
label="中心内容"
prop="centerSummary"
>
<Wangeditor v-model="form.centerSummary"></Wangeditor>
</el-form-item>
四、相关链接
- wangEditor5:https://www.wangeditor.com
- wangEditor4:https://www.wangeditor.com/v4