WangEditor.php 4.0 KB
<?php

namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class WangEditor extends Field
{
    protected $view = 'admin.wang-editor';
    protected static $css = [
        '/vendor/wangEditor-3.1.1/wangEditor.min.css',
    ];

    protected static $js = [
        '/vendor/wangEditor-3.1.1/wangEditor.min.js',
    ];
    public function render()
    {
        $name = $this->formatName($this->column);
        $this->script = <<<EOT
var E = window.wangEditor
var editor = new E('#{$this->id}');

 // 自定义菜单配置
// editor.customConfig.menus = [
//     'emoticon',  // 表情
//     'image',  // 插入图片
// ];

// 配置服务器端地址
editor.customConfig.uploadImgServer = '/service/upload/image';
 // 粘贴过滤样式,关闭
editor.customConfig.pasteFilterStyle = false
// 将图片大小限制为 3M
editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024
// 限制一次最多上传 5 张图片
editor.customConfig.uploadImgMaxLength = 5
// 自定义 fileName
editor.customConfig.uploadFileName = 'file[]';
// 自定义设置 heade
editor.customConfig.uploadImgHeaders = {
    'X-CSRF-TOKEN': $('input[name="_token"]').val()
}
editor.customConfig.zIndex = 0;
editor.customConfig.onchange = function (html) {
    $('input[name=$name]').val(html);
}
editor.customConfig.pasteTextHandle = function (content) {

    var html = content;
    html = html.replace(/<\/?SPANYES[^>]*>/gi,"");//  移除span
    html = html.replace(/<(\w[^>]*)  lang=([^|>]*)([^>]*)/gi, "<$1$3");// 移除lang属性
    html = html.replace(/<\\?\?xml[^>]*>/gi, "");//  移除xml和相关描述
    html = html.replace(/<\/?\w+:[^>]*>/gi, "");// 移除xml命名空间的标签
    html = html.replace(/&nbsp;/gi, "");//  移除空格
    html = html.replace(/^\s+|\s+$/g,"");
    html = html.replace(/<o:p> <\/o:p>/g,"");//移除标签
    html = html.replace(/<br\/>/g,"");//移除br标签
    return html;
}
// 可使用监听函数在上传图片的不同阶段做相应处理
editor.customConfig.uploadImgHooks = {
     before: function (xhr, editor, files) {
        // 图片上传之前触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files 是选择的图片文件

        // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
        // return {
        //     prevent: true,
        //     msg: '放弃上传'
        // }
        layer_loading();
    },
    success: function (xhr, editor, result) {
        // 打开弹窗
        layer_loading_close();
        // 图片上传并返回结果,图片插入成功之后触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
    },
    fail: function (xhr, editor, result) {
        layer_loading_close();
        // 图片上传并返回结果,但图片插入错误时触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
    },
    error: function (xhr, editor) {
        layer_loading_close();
        // 图片上传出错时触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
    },
    timeout: function (xhr, editor) {
        layer_loading_close();
        // 图片上传超时时触发
        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
    },

    // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
    // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
    customInsert: function (insertImg, result, editor) {
        if (result['code'] === 0) {
            for (var i = 0; i <= result['data'].length - 1; i++) {
                var url = result['data'][i]['full_path'];
                insertImg(url);
            }
            toastr.success(result['msg']);
        } else {
            toastr.error(result['msg']);
        }
    }
}
editor.create();
/*// var editor = new wangEditor('{$this->id}');
//     editor.create();*/
EOT;
        return parent::render();
    }
}