WangEditor.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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(/ /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();
}
}