当前位置:网站首页>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
边栏推荐
- What is your revenue rank among global developers in 2022?
- Three ways of redis cluster
- Brush questions - Luogu -p1047 trees outside the school gate
- QGIS loading online map: Gaode, Tiandi map, etc
- Brush questions - Luogu -p1151 sub number integer
- wangeditor 富文本编辑器
- [force deduction] 1030. Arrange matrix cells in distance order
- window unbutu20 LTS apt,wget 安装时 DNS 解析错误
- 【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
- Pytest.mark.parameterize and mock use
猜你喜欢

刷题-洛谷-P1151 子数整数

运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?

word设置粘贴仅保留文本

Multidimensional pivoting analysis of CDA level1 knowledge points summary

Applet enterprise red envelope function

pytest.mark.parametrize及mock使用

飞盘局有多快乐?2022年轻人新潮运动报告

刷题-洛谷-P1035 级数求和

Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency

Pytest.mark.parameterize and mock use
随机推荐
IM系统-消息流化一些常见问题
【目录爆破工具】信息收集阶段:robots.txt、御剑、dirsearch、Dirb、Gobuster
刷题-洛谷-P1075 质因数分解
Leetcode 113. path sum II
音视频技术开发周刊 | 255
高版本MySQL服务端安装不上怎么办,忘记密码(MySQL8.0.29)?
命名空间与库
Tensorflow2 installation quick pit avoidance summary
What financial products can I invest with some money in my hand?
@Wrap decorator
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
Leetcode202 --- Happy number
科隆新能源IPO被终止:拟募资6亿 先进制造与战新基金是股东
NoSQL,关系型数据库,行列数据库对比、类比
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
Uniapp handles background transfer pictures
Brush questions - Luogu -p1150 Peter's smoke
CSV文本文件导入excel的四种方法
Explain the precision of floating point numbers in detail
Brush questions - Luogu -p1035 series summation