vue-cli引入wangEditor、Element,封装可上传附件的富文本编辑器组件(附源代码直接应用,菜单可调整)

news/2024/7/10 1:38:17 标签: elementui, vue, vuecli, wangeditor, 富文本编辑器

关于Element安装引入,请参考我的另一篇文章:vue-cli引入Element Plus(element-ui),修改主题变量,定义全局样式_shawxlee的博客-CSDN博客_chalk variables


wangeditor_4">1、安装wangeditor

npm i wangeditor --save

wangEditor官方文档:Introduction · wangEditor 用户文档

wangeditor_10">2、在页面中引入wangeditor创建编辑器

附上自己封装的富文本编辑器组件源代码,可上传图片和附件:

<template>
    <!-- 富文本编辑器 -->
    <div class="rich_editor">
        <div :id="id" style="margin-bottom: 10px">
            <slot></slot>
        </div>
        <!-- 上传附件 -->
        <el-upload ref="upload" action="#" multiple :on-remove="onRemove" :on-change="onChange" :auto-upload="false" :file-list="fileList">
            <template #trigger>
                <el-button type="text" icon="el-icon-circle-plus" style="line-height: normal;">添加附件</el-button>
            </template>
            <!-- 操作按钮 -->
            <span style="float:right;">
                <el-button type="primary" size="small" @click="submit" :disabled="(!editorInput.content && fileList.length == 0) || loading"><i class="el-icon-loading" v-show="loading"></i> 提交</el-button>
                <el-button size="small" @click="cancel">取消</el-button>
            </span>
            <!-- 提示文本 -->
            <template #tip>
                <div class="el-upload__tip info" v-if="tip">* {{ tip }}</div>
            </template>
        </el-upload>
    </div>
</template>
<script>
import E from 'wangeditor' //引入wangeditor
export default {
    props: {
        id: String, //组件唯一ID
        files: Array, //已上传的文件列表(非必填)
        loading: Boolean, //缓冲条件(非必填)
        tip: String, //提示文本(非必填)
        size: Number, //文件大小限制(字节)(非必填)
    },
    data() {
        return {
            editorInput: { //编辑器内容
                content: '',
                files: []
            },
            fileList: this.files ? this.files : [] //文件列表
        }
    },
    mounted() {
        //初始化编辑器
        var editor = new E('#' + this.id)
        //配置属性
        editor.config.height = 100
        editor.config.menus = [
            'bold',
            'underline',
            'link',
            'image',
        ]
        editor.config.showFullScreen = false
        editor.config.placeholder = '请输入内容或上传附件……'
        editor.config.pasteIgnoreImg = true
        editor.config.uploadImgShowBase64 = true
        //实时监听输入内容
        editor.config.onchange = (newHtml) => {
            this.editorInput.content = newHtml
        }
        //创建编辑器
        editor.create()
    },
    methods: {
        //文件列表移除文件时的钩子
        onRemove(file, fileList) {
            this.fileList = fileList
        },
        //文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
        onChange(file, fileList) {
            if (this.size && (file.size > this.size)) { //处理文件大小限制
                this.$message.error(this.tip)
                this.fileList = fileList.slice(0, -1)
            } else {
                this.fileList = fileList
            }
        },
        //提交编辑内容
        submit() {
            var formData = new FormData() //根据后端接口参数的格式要求进行处理
            if (this.fileList.length > 0) { //处理上传的文件
                for (let i = 0, len = this.fileList.length; i < len; i++) {
                    if (this.fileList[i].raw) {
                        formData.append('file', this.fileList[i].raw)
                    }
                    if (this.files && this.files.length > 0) {
                        for (let j = 0, lenj = this.files.length; j < lenj; j++) {
                            if (this.fileList[i].uid == this.files[j].uid) {
                                this.editorInput.files.push(this.fileList[i].uid)
                            }
                        }
                    }
                }
            }
            this.$emit('submit', this.editorInput, formData) //@submit调用父组件函数完成提交,参数:(editorInput, formData)
        },
        //取消编辑
        cancel() {
            this.$emit('cancel') //@cancel调用父组件函数取消编辑
        },
    }
}
</script>
<style lang="scss">
.w-e-toolbar {
    z-index: 100 !important;
}

.w-e-text-container {
    z-index: 99 !important;
}

.w-e-text {
    padding: 6px 10px;
}

.w-e-text-container .placeholder {
    font-size: 12px;
    line-height: normal;
}

.w-e-toolbar p,
.w-e-text-container p,
.w-e-menu-panel p {
    font-size: 12px !important;
    line-height: normal !important;
}

.el-upload__tip {
    margin-top: 0;
    font-size: 10px;
    margin-bottom: 10px;
    line-height: normal;
}

.el-upload-list__item {
    font-size: 12px;
    line-height: normal;
    color: $label;
    width: calc(100% - 100px);
}
</style>

组件应用示例:

<OrderEditor id="unique_id" :files="files" :loading="loading" tip="文件大小不超过 200 MB" :size="209715200" @submit="handleSubmit" @cancel="handleCancel">
    <!-- slot文本框初始填充的内容 -->
</OrderEditor>

// id - String, 唯一ID
// files - Array, 已上传的文件列表(非必填)
// loading - Boolean, 缓冲条件(非必填)
// tip - String, 提示文本(非必填)
// size - Number, 文件大小限制(字节)(非必填)
// submit - Function, 提交内容调用函数,参数:(editorInput, formData)
// cancel - Function, 取消编辑调用函数

组件效果展示:
在这里插入图片描述


http://www.niftyadmin.cn/n/96420.html

相关文章

闭包(回顾)

概念作用保护作用保存作用优缺点命名空间 概念 闭包(closure)指有权访问另一个函数作用域中变量的函数 — Javacript高级程序设计 p309 简单理解&#xff0c;一个作用域可以访问另一个函数内部的私有变量 // 其中 test就是一个闭包 function fn(){var num 10function test …

OpenCV只含基本图像模块编译

以Windows10-x64下的OpenCV4.5.5只含基本图像模块编译为例&#xff0c;编译环境为CMake3.23.3VS2019。 Step1&#xff1a;在CMake中选择/填写OpenCV源代码路径和编译文件路径。 Step2&#xff1a;点击Configure按钮&#xff0c;会弹出一个对话框&#xff0c;在第二个下拉框中…

论坛项目小程序和h5登录

项目中安装uview出现npm安装uview 直接报错&#xff1a;创建一个package.json配置文件在进行安装。cmd到项目。初始化一个package.json文件&#xff08;vue项目的配置文件&#xff09; npm init --yes 安装uview项目点击关注进入管页面&#xff0c;需要验证用户是否登录查用户是…

关于物理像素,逻辑像素,像素比

关于物理像素、逻辑像素&#xff08;css像素&#xff09;、分辨率、像素比的超详细讲解 在日常生活中&#xff0c;有这样一个问题。同样的图片为什么在不同的设备上显示的大小是不一样的。&#x1f912;带着这个问题来说明一下。 一、物理像素 设备刚生产出来就已经固定了&a…

实验室信息化管理行业方案

为适应新时代下的管理机制与应用场景&#xff0c;越来越多的检测实验室需对研发部门和实验部门进行全面的、现代化的、电子化的综合管理&#xff0c;帮助检测机构对实验室的规划与计划、项目立项与管理、项目成果、合同&#xff0c;以及基建等工作进行统一的管理&#xff0c;而…

pytorch1.2.0+python3.6

一、说明 pytorch1.2.0python3.6CUDA10.0cudnn7.4.1.5 二、步骤 在conda中创建一个新的虚拟环境 查看一下自己的所有环境 激活虚拟环境 conda activate torch1.2.0 关于cuda和cudnn 1、查看自己电脑系统是10.2版本 http://链接&#xff1a;https://pan.baidu.com/s/1v5cN6…

【Java基础 下】 027 -- 异常、File、综合案例

目录 一、异常 1、异常的分类 ①、Error ②、Exception ③、小结 2、编译时异常和运行时异常 ①、编译时异常 ②、运行时异常 ③、为什么异常要分成编译时异常和运行时异常&#xff1f; ④、小结&#xff08;运行时异常和编译时异常的区别&#xff09; 3、异常的作用 ①、查看b…

leetcode Day5(卡线复试,放弃版)

Day5 最后一个单词长度&#xff08;要求最后一个&#xff0c;可以反向计数&#xff09; int lens.length()-1; while(s.charAt(len)){len--;//最后是一个空格&#xff0c;就是无字符时 } int wordlen0;//记录字符长度 /*charAt() 方法用于返回指定索引处的字符。索引范围为从 0…