/
UEditor.php
executable file
·156 lines (135 loc) · 4.93 KB
/
UEditor.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* UEditor Widget扩展
* @author xbzbing<xbzbing@gmail.com>
* @link www.crazydb.com
*
* UEditor版本v1.4.3.1
* Yii版本2.0+
*
* 使用方法:
* 1、AR
*
* <?=$form->field($model, 'content')->widget(\crazydb\ueditor\UEditor::className())?>
*
* 或者
*
* <?=\crazydb\ueditor\UEditor::widget([
* 'model' => $model,
* 'attribute' => 'content',
* ])?>
*
*
* 2、普通表单
*
* <?=\crazydb\ueditor\UEditor::widget([
* 'name' => $name,
* 'value' => $value,
* ])>
*/
namespace crazydb\ueditor;
use yii;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
/**
* Class UEditor
* UEditor Widget的主类,用于前端显示编辑器
* @package crazydb\ueditor
*/
class UEditor extends yii\widgets\InputWidget
{
/**
* 生成的ueditor对象的名称,默认为editor。
* 主要用于同一个页面的多个editor实例的管理。
* @var string
*/
public $name;
/**
* UEditor配置
* @var array
*/
public $config = [];
/**
* 初始化一些配置。
* 由于不引入config.js文件,因此需要手动配置一些东西。
*/
public function init()
{
parent::init();
//注册资源文件
$asset = UEditorAsset::register($this->getView());
//设置UEditor实例的名字
if (!$this->name)
$this->name = $this->hasModel() ? $this->model->formName() . '_' . $this->attribute : 'ueditor_' . $this->id;
//常用配置项
if (empty($this->config['UEDITOR_HOME_URL']))
$this->config['UEDITOR_HOME_URL'] = $asset->baseUrl . '/';
if (empty($this->config['serverUrl']))
$this->config['serverUrl'] = Url::to(['/ueditor/index']);
elseif (is_array($this->config['serverUrl']))
$this->config['serverUrl'] = Url::to($this->config['serverUrl']);
if (empty($this->config['lang']))
$this->config['lang'] = 'zh-cn';
if (empty($this->config['initialFrameHeight']))
$this->config['initialFrameHeight'] = 400;
if (empty($this->config['initialFrameWidth']))
$this->config['initialFrameWidth'] = '100%';
if (empty($this->config['enableAutoSave']))
$this->config['enableAutoSave'] = false;
//扩展默认不直接引入config.js文件,因此需要自定义配置项.
if (empty($this->config['toolbars'])) {
//为了避免每次使用都输入乱七八糟的按钮,这里预先定义一些常用的编辑器按钮。
//这是一个丑陋的二维数组
$this->config['toolbars'] = [
[
'fullscreen', 'source', 'undo', 'redo', '|',
'customstyle', 'paragraph', 'fontfamily', 'fontsize'
],
[
'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat',
'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', '|',
'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
'directionalityltr', 'directionalityrtl', 'indent', '|'
],
[
'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
'link', 'unlink', '|',
'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'insertcode', 'pagebreak', '|',
'horizontal', 'inserttable', '|',
'print', 'preview', 'searchreplace', 'help'
]
];
}
}
/**
* 输出widget页面,注册相关JS代码。
*/
public function run()
{
$id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
if (!preg_match('/^[\w\d_]+$/', $this->name))
$name = preg_replace('/[^\w\d_]/', '_', $this->name);
else
$name = $this->name;
$config = Json::encode($this->config);
//ready部分代码,是为了缩略图管理。UEditor本身就很大,在后台直接加载大文件图片会很卡。
$script = <<<UEDITOR
var {$name} = UE.getEditor('{$id}',{$config});
{$name}.ready(function(){
this.addListener( "beforeInsertImage", function ( type, imgObjs ) {
for(var i=0;i < imgObjs.length;i++){
imgObjs[i].src = imgObjs[i].src.replace(".thumbnail","");
}
});
});
UEDITOR;
$this->getView()->registerJs($script);
if ($this->hasModel())
return Html::activeTextarea($this->model, $this->attribute);
else
return Html::textarea(ArrayHelper::getValue($this->config, 'textarea', $this->name), $this->value, ['id' => $id]);
}
}