业务型 编辑器组件的封装(复制即可使用)

使用需要安装 wangeditor npm i --save wangeditor

javascript 复制代码
import React from 'react';
import E from 'wangeditor';
import './index.less'

class EditorElem extends React.Component {

    constructor(props) {
        super(props);
        this.isChange = false;
        this.state = {
        }

    }
    componentDidMount() {
        const elemBody = this.refs.editorElemBody;
        this.editor = new E(elemBody)
        this.initEditor()
    }
    componentWillReceiveProps(nextProps) {

        if (nextProps.value != this.props.value) {
            if (!this.isChange) {
                this.editor.txt.html(nextProps.value)
            }
            this.isChange = false;
        }
    }

    initEditor() {
        // 使用 onchange 函数监听内容的变化,并实时更新到 state 中
        this.editor.config.onchange = html => {
            this.isChange = true;
            // console.log(editor.txt.html())
            let editorContent = this.editor.txt.html();
            this.props.onChange(editorContent)
            // 不给延时,会导致详情调整过的内容修改后组件数据更新不了
            setTimeout(() => {
                this.isChange = false
            }, 50);
        }
        this.editor.config.menus = [
            'head', // 标题
            'bold', // 粗体
            'fontSize', // 字号
            'fontName', // 字体
            'italic', // 斜体
            'underline', // 下划线
            'strikeThrough', // 删除线
            'indent',
            'lineHeight',
            'foreColor', // 文字颜色
            'backColor', // 背景颜色
            'link', // 插入链接
            'list', // 列表
            'todo',
            'justify', // 对齐方式
            'quote', // 引用
            'emoticon', // 表情
            'image', // 插入图片
            'table', // 表格
            'video', // 插入视频
            'code', // 插入代码
            'splitLine',
            'undo', // 撤销
            'redo' // 重复
        ]
        this.editor.config.colors = ['#999', '#666', '#000000',
            '#eeece0',
            '#1c487f',
            '#4d80bf',
            '#c24f4a',
            '#8baa4a',
            '#7b5ba1',
            '#46acc8',
            '#f9963b',
            '#ffffff'];
        // editor.config.uploadImgShowBase64 = true;
        this.editor.config.pasteIgnoreImg = true;
        this.editor.config.uploadImgServer = `${configs.host.test}/api/FileUpload/Upload`;  // 上传图片到服务器 
        this.editor.config.uploadFileName = 'fileName'
        this.editor.config.uploadImgParams = {
            merchantId: localStorage.getItem('MerchantId'),
            Directory: 'Image'
        }
        // 限制一次最多上传 1 张图片
        this.editor.config.uploadImgMaxLength = 1;
        // 将 timeout 时间改为 3s
        // this.editor.config.uploadImgTimeout = 3000;
        this.props.zIndex && (this.editor.config.zIndex = this.props.zIndex);
        this.editor.config.uploadImgHeaders = {
            Accept: 'multipart/form-data',
            // Authorization: `Bearer ${localStorage.getItem('access_token')}`,
            // MerchantId: localStorage.getItem('MerchantId')
        }
        this.editor.config.uploadImgHooks = {
            before: function (xhr, editor, files) {
                // 图片上传之前触发
            },
            success: function (xhr, editor, result) {
                // 图片上传并返回结果,图片插入成功之后触发
            },
            fail: function (xhr, editor, result) {
                // 图片上传并返回结果,但图片插入错误时触发
            },
            error: function (xhr, editor) {
                // 图片上传出错时触发
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
            // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
            customInsert: function (insertImg, result, editor) {
                // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
                // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
                // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
                var url = result.data;
                insertImg(url);
                // result 必须是一个 JSON 格式字符串!!!否则报错
            }
        }
        this.editor.create()
        this.props.value && this.editor.txt.html(this.props.value);
        // 开启编辑功能
        if (this.props.disabled) {
            this.editor.disable()
        }
        // this.editor.$textElem.attr('contenteditable', this.props.disabled ? false : true)
    }
    render() {
        return (
            <div className="text-area" >
                <div
                    style={{
                        // height: 335,
                    }}
                    ref="editorElemBody" className="editorElem-body">

                </div>
            </div>
        )
    }
}

export default EditorElem;
css 复制代码
.editorElem-body{
    /* table 样式 */
table {
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
  }
  table td,
  table th {
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    padding: 3px 5px;
  }
  table th {
    border-bottom: 2px solid #ccc;
    text-align: center;
  }
  
  /* blockquote 样式 */
  blockquote {
    display: block;
    border-left: 8px solid #d0e5f2;
    padding: 5px 10px;
    margin: 10px 0;
    line-height: 1.4;
    font-size: 100%;
    background-color: #f1f1f1;
  }
  
  /* code 样式 */
  code {
    display: inline-block;
    *display: inline;
    *zoom: 1;
    background-color: #f1f1f1;
    border-radius: 3px;
    padding: 3px 5px;
    margin: 0 3px;
  }
  pre code {
    display: block;
  }
  
  /* ul ol 样式 */
  ul, ol {
    margin: 10px 0 10px 20px;
  }
}
css 复制代码
 {/* 编辑器组件 ---开始 */}
                <FormItem {...formItemLayout2} label="编辑器组件">
                  {getFieldDecorator("editorValue", {
                    rules: [{ required: true, message: "请填写内容" }],
                    initialValue: detailData.editorValue,
                  })(<EditorElemItem />)}
                </FormItem>
                {/* 编辑器组件 ---结束 */}

使用便捷,无需关注内部实现和定义一堆函数,只需要传入value值 即可回显数据

可以触发form的表单验证,无需额外在提交的时候验证是否有值进行message提示

相关推荐
用户6000718191016 小时前
【翻译】React 如何乱序流式输出 UI,却仍保持最终顺序
前端
江南十四行16 小时前
AI Agent应用类型及Function Calling开发实战(三)
服务器·前端·javascript
yqcoder16 小时前
JavaScript 数据类型全景图:从基础到进阶
开发语言·javascript·ecmascript
GISer_Jing16 小时前
AI原生全栈架构理论体系:从分布式范式演进到全链路工程化理论基石
前端·人工智能·学习·ai编程
GISer_Jing16 小时前
从“切图仔”到“增长架构师”:AI时代营销前端的范式革命
前端·人工智能·ai编程
广州华水科技17 小时前
单北斗GNSS在水库变形监测中的应用与系统安装解析
前端
xingpanvip17 小时前
星盘接口开发文档:组合三限盘接口指南
android·开发语言·前端·python·php·lua
阿拉丁的梦17 小时前
blender最好的多通道吸色工具(拾取纹理颜色排除灯光)
前端·html
一帘忧梦17 小时前
vscode 搭建stm32开发环境 +HAL 库
ide·vscode·编辑器
吴声子夜歌17 小时前
Vue3——脚手架Vite
前端·javascript·vue.js·vite