当前位置:网站首页>wangeditor 富文本编辑器
wangeditor 富文本编辑器
2022-07-25 13:49:00 【阿程_88】
一、安装依赖
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赋值了,子组件将会显示父组件赋给的值
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) // false 表示禁止修改
}
},
methods: {
setEditor() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor)
this.editor.config.uploadImgShowBase64 = true // base 64 存储图片
this.editor.config.uploadImgServer = imgUploadUrl // 配置服务器端地址
this.editor.config.uploadImgHeaders = {
} // 自定义 header
this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
this.editor.config.uploadImgMaxLength = 1 // 限制一次最多上传 3 张图片
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) => {
// console.log('上传之前')
},
success: (xhr, editor, result) => {
// console.log('上传成功');
},
fail: (xhr, editor, result) => {
// console.log('上传失败原因:', result)
},
error: (xhr, editor) => {
// console.log('上传出错');
}
}
// 上传图片
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)
// 计算 MD5 值
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)
// 异步执行函数
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) // 后台返回图片id
insert(imgUrl) // 生成 img 标签并插入文章中
}
})
}
}
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
边栏推荐
- Hcip eighth day experiment
- ES6数组去重 new Set()
- DNS resolution error during windows unbutu20 lts apt, WGet installation
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
- Brush questions - Luogu -p1075 prime factor decomposition
- hcip第九天笔记
- 0719RHCSA
- 刷题-洛谷-P1161 开灯
- redis集群的三种方式
- MXNet对DenseNet(稠密连接网络)的实现
猜你喜欢

DNS resolution error during windows unbutu20 lts apt, WGet installation

GCD details

「数字安全」警惕 NFT的七大骗局
![[server data recovery] HP EVA server storage raid information power loss data recovery](/img/ee/8f36ef1b5842f1778c0dd695401b40.jpg)
[server data recovery] HP EVA server storage raid information power loss data recovery

Discussion on principle and application technology of MLIR

IM system - some common problems of message streaming

刷题-洛谷-P1151 子数整数

Uniapp handles background transfer pictures

MXNet对DenseNet(稠密连接网络)的实现

What problems should SEOER pay attention to when baidu searches and attacks pirated websites?
随机推荐
mysql 01: source命令
Framework package merge script
嵌入式代码如何进行重构?
Leetcode -- addition of four numbers II
命名空间与库
Internal error of LabVIEW
What are the ranking strategies for mobile websites, independent apps and websites?
How can information security engineers prepare for the soft exam in the second half of 2022?
hcip第八天实验
Brush questions - luogu-p1089 Jinjin savings plan
刷题-洛谷-P1146 硬币翻转
Brush questions - Luogu -p1059 clear random number
Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
0716RHCSA
Hcip day 8 notes
adb通过Wi-Fi连接小米手机
uniapp处理后台传输图片
DNS resolution error during windows unbutu20 lts apt, WGet installation
Hcip day 9 notes