当前位置:网站首页>Wangeditor rich text editor
Wangeditor rich text editor
2022-07-25 13:54:00 【Cheng_ eighty-eight】
One 、 Installation dependency
npm install wangeditor -S
Two 、 Package into a component (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
},
// It is forbidden to modify the content
isDisabled: {
type: Boolean,
default: false
}
},
watch: {
isClear(val) {
// Clear the contents of the text field
if (val) {
this.editor.txt.clear()
this.info_ = null
}
},
// value Input for edit box , Here I monitor the value , When the parent component calls , If value Assigned a value to , The child component will display the value assigned by the parent component
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)
// It is forbidden to modify the content
if (this.isDisabled) {
this.editor.$textElem.attr('contenteditable', !this.isDisabled) // false Indicates that modification is prohibited
}
},
methods: {
setEditor() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor)
this.editor.config.uploadImgShowBase64 = true // base 64 Store image
this.editor.config.uploadImgServer = imgUploadUrl // Configure server address
this.editor.config.uploadImgHeaders = {
} // Customize header
this.editor.config.uploadFileName = 'file' // The backend accepts the parameter name of the uploaded file
this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // Limit the size of the picture to 2M
this.editor.config.uploadImgMaxLength = 1 // Limit one upload at most 3 A picture
this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // Set timeout
// Configuration menu
this.editor.config.menus = [
'head', // title
'bold', // bold
'fontSize', // Font size
'fontName', // typeface
'italic', // Italics
'underline', // Underline
'strikeThrough', // Delete line
'foreColor', // Text color
'backColor', // The background color
'link', // Insert link
'list', // list
'justify', // Alignment mode
'quote', // quote
'emoticon', // expression
'image', // Insert a picture
'table', // form
'video', // Insert the video
'code', // Insert code
'undo', // revoke
'redo', // repeat
'fullscreen' // Full screen
]
this.editor.config.uploadImgHooks = {
before: (xhr, editor, files) => {
// console.log(' Before uploading ')
},
success: (xhr, editor, result) => {
// console.log(' Upload successful ');
},
fail: (xhr, editor, result) => {
// console.log(' Reason for upload failure :', result)
},
error: (xhr, editor) => {
// console.log(' Upload error ');
}
}
// To upload pictures
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)
// Calculation MD5 value
const fileReader = new FileReader()
const dataFile = files[0]
// const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
const spark = new SparkMD5.ArrayBuffer()
fileReader.readAsArrayBuffer(dataFile)
// Execute functions asynchronously
fileReader.onload = e => {
spark.append(e.target.result)
const md5 = spark.end()
formData.append('md5', md5)
// Upload
upload(imgUploadUrl, formData).then(res => {
if (res.resultCode === 1) {
const imgUrl = this.getImgUrl(res.data.id) // Background return picture id
insert(imgUrl) // Generate img Tag and insert into the article
}
})
}
}
this.editor.config.onchange = html => {
this.info_ = html // Bind the currently entered value
this.$emit('change', this.info_) // Synchronize content to parent component
}
// Create rich text editor
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;
/* Scroll bar */
::v-deep .w-e-text::-webkit-scrollbar {
width: 10px;
height: 10px;
background-color: transparent;
}
/* Rolling groove */
::v-deep .w-e-text::-webkit-scrollbar-track {
background-color: transparent;
}
/* Scroll the slider */
::v-deep .w-e-text::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #c4c6cc;
}
}
}
</style>
3、 ... and 、 Use... In the parent component
<el-form-item
label=" Central content "
prop="centerSummary"
>
<Wangeditor v-model="form.centerSummary"></Wangeditor>
</el-form-item>
Four 、 Related links
- wangEditor5:https://www.wangeditor.com
- wangEditor4:https://www.wangeditor.com/v4
边栏推荐
猜你喜欢

科隆新能源IPO被终止:拟募资6亿 先进制造与战新基金是股东

Immortal software in the computer that I don't want to delete all my life

Leetcode202 --- Happy number

Install oh my Zsh

Construction and practice of yolov7 in hands-on teaching

刷题-洛谷-P1151 子数整数

Use of Bluetooth function of vs wireless vibrating wire acquisition instrument

2022年下半年软考初级程序员备考

Tm1637 four digit LED display module Arduino driver with second dot
Congestion control of TCP
随机推荐
Install oh my Zsh
Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
Brush questions - Luogu -p1151 sub number integer
Brush questions - Luogu -p1059 clear random number
JS array indexof includes sort() colon sort quick sort de duplication and random sample random
Audio and video technology development weekly | 255
Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt
IM系统-消息流化一些常见问题
刷题-洛谷-P1151 子数整数
Stay on Alibaba cloud mqtt IOT platform
Day1:三种语言暴刷牛客130题
[configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency
Common methods of arrays
Hcip day 9 notes
依迅总经理孙峰:公司已完成股改,准备IPO
Engineering monitoring multi-channel vibrating wire sensor wireless acquisition instrument external digital sensor process
Tm1637 four digit LED display module Arduino driver with second dot
刷题-洛谷-P1075 质因数分解
How happy is the frisbee bureau? 2022 youth trendy Sports Report