当前位置:网站首页>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 day 6 notes
- 百度搜索打击盗版网文站点,SEOer应该关注哪些问题?
- Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
- How to use handwritten JDBC?
- 高版本MySQL服务端安装不上怎么办,忘记密码(MySQL8.0.29)?
- 伯克利博士『机器学习工程』大实话;AI副总裁『2022 ML就业市场』分析;半导体创业公司大列表;大规模视频人脸属性数据集;前沿论文 | ShowMeAI资讯日报
- AQS of concurrent programming
- 音视频技术开发周刊 | 255
- 嵌入式代码如何进行重构?
- Lesson of C function without brackets
猜你喜欢

Pytest.mark.parameterize and mock use

Audio and video technology development weekly | 255

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

Redux usage and analysis

Brush questions - Luogu -p1047 trees outside the school gate

From input URL to web page display

刷题-洛谷-P1089 津津的储蓄计划

移动端网站,独立APP,网站排名策略有哪些?

Brush questions - Luogu -p1035 series summation

Leetcode1 -- sum of two numbers
随机推荐
刷题-洛谷-P1035 级数求和
飞盘局有多快乐?2022年轻人新潮运动报告
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
MySQL 01: Source command
hcip第八天笔记
adb通过Wi-Fi连接小米手机
6.27 uniapp项目历程
0710RHCSA
Applet starts wechat payment
刷题-洛谷-P1150 Peter的烟
Numpy quick start
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
hcip第十天笔记
leetcode1 --两数之和
From input URL to web page display
ES6 array de duplication new set()
Hcip day 6 notes
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
Hcip eighth day experiment
2022全球开发者中,你的收入排多少?