-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKindEditor.php
272 lines (242 loc) · 8.84 KB
/
KindEditor.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Description of KindEditor
*
* @author Qinn Pan <Pan JingKui, [email protected]>
* @link http://www.pjkui.com
* @QQ 714428042
* @date 2015-3-4
*/
namespace pjkui\kindeditor;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\View;
use yii\widgets\InputWidget;
class KindEditor extends InputWidget {
//配置选项,参阅KindEditor官网文档(定制菜单等)
public $clientOptions = [];
//定义编辑器的类型,
//默认为textEditor;
//uploadButton:自定义上传按钮
//dialog:弹窗
//colorpicker:取色器
//file-manager浏览服务器
//image-dialog 上传图片
//multiImageDialog批量上传图片
//fileDialog 文件上传
public $editorType;
//默认配置
protected $_options;
/**
* @throws \yii\base\InvalidConfigException
*/
public function init() {
$this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute).$this->id : $this->id;
$this->_options = [
'fileManagerJson' => Url::to(['Kupload', 'action' => 'fileManagerJson']),
'uploadJson' => Url::to(['Kupload', 'action' => 'uploadJson']),
'width' => '100%',
'height' => '400',
//'langType' => (strtolower(Yii::$app->language) == 'en-us') ? 'en' : 'zh_cn',//kindeditor支持一下语言:en,zh_CN,zh_TW,ko,ar
];
$this->clientOptions = ArrayHelper::merge($this->_options, $this->clientOptions);
if($this->hasModel()){
parent::init();
}
}
public function run() {
$this->registerClientScript();
$options = $this->options;
$options['id'] = $this->id;
if ($this->hasModel()) {
switch ($this->editorType) {
case 'uploadButton':
return Html::activeInput('text', $this->model, $this->attribute, $options) . '<input type="button" id="uploadButton" value="Upload" />';
break;
case 'colorpicker':
return Html::activeInput('text', $this->model, $this->attribute, $options) . '<input type="button" id="colorpicker" value="打开取色器" />';
break;
case 'file-manager':
return Html::activeInput('text', $this->model, $this->attribute, $options) . '<input type="button" id="filemanager" value="浏览服务器" />';
break;
case 'image-dialog':
return Html::activeInput('text', $this->model, $this->attribute, $options) . '<input type="button" id="imageBtn" value="选择图片" />';
break;
case 'file-dialog':
return Html::activeInput('text', $this->model, $this->attribute, $options) . '<input type="button" id="insertfile" value="选择文件" />';
break;
default:
return Html::activeTextarea($this->model, $this->attribute, $options);
break;
}
} else {
switch ($this->editorType) {
case 'uploadButton':
$options['readonly'] = 'readonly';
return Html::input('text', $this->id, $this->value, $options) . '<input type="button" id="uploadButton" value="Upload" />';
break;
case 'colorpicker':
return Html::input('text', $this->id, $this->value, $options) . '<input type="button" id="colorpicker" value="打开取色器" />';
break;
case 'file-manager':
return Html::input('text', $this->id, $this->value, $options) . '<input type="button" id="filemanager" value="浏览服务器" />';
break;
case 'image-dialog':
return Html::input('text', $this->id, $this->value, $options) . '<input type="button" id="imageBtn" value="选择图片" />';
break;
case 'file-dialog':
return Html::input('text', $this->id, $this->value, $options) . '<input type="button" id="insertfile" value="选择文件" />';
break;
default:
return Html::textarea($this->id, $this->value, $options);
break;
}
}
}
/**
* 注册客户端脚本
*/
protected function registerClientScript() {
//UEditorAsset::register($this->view);
KindEditorAsset::register($this->view);
$clientOptions = Json::encode($this->clientOptions);
$fileManagerJson = Url::to(['Kupload', 'action' => 'fileManagerJson']);
$uploadJson = Url::to(['Kupload', 'action' => 'uploadJson']);
switch ($this->editorType) {
case 'uploadButton':
$url = Url::to(['Kupload', 'action' => 'uploadJson', 'dir' => 'file']);
$script = <<<EOT
KindEditor.ready(function(K) {
var uploadbutton = K.uploadbutton({
button : K('#uploadButton')[0],
fieldName : 'imgFile',
url : '{$url}',
afterUpload : function(data) {
if (data.error === 0) {
var url = K.formatUrl(data.url, 'absolute');
K('#{$this->id}').val(url);
} else {
alert(data.message);
}
},
afterError : function(str) {
alert('自定义错误信息: ' + str);
}
});
uploadbutton.fileBox.change(function(e) {
uploadbutton.submit();
});
});
EOT;
break;
case 'colorpicker':
$script = <<<EOT
KindEditor.ready(function(K) {
var colorpicker;
K('#colorpicker').bind('click', function(e) {
e.stopPropagation();
if (colorpicker) {
colorpicker.remove();
colorpicker = null;
return;
}
var colorpickerPos = K('#colorpicker').pos();
colorpicker = K.colorpicker({
x : colorpickerPos.x,
y : colorpickerPos.y + K('#colorpicker').height(),
z : 19811214,
selectedColor : 'default',
noColor : '无颜色',
click : function(color) {
K('#{$this->id}').val(color);
colorpicker.remove();
colorpicker = null;
}
});
});
K(document).click(function() {
if (colorpicker) {
colorpicker.remove();
colorpicker = null;
}
});
});
EOT;
break;
case 'file-manager':
$script = <<<EOT
KindEditor.ready(function(K) {
var editor = K.editor({
fileManagerJson : '{$fileManagerJson}'
});
K('#filemanager').click(function() {
editor.loadPlugin('filemanager', function() {
editor.plugin.filemanagerDialog({
viewType : 'VIEW',
dirName : 'image',
clickFn : function(url, title) {
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'image-dialog':
$script = <<<EOT
KindEditor.ready(function(K) {
var editor = K.editor({
allowFileManager : true,
"uploadJson":"{$uploadJson}",
"fileManagerJson":"{$fileManagerJson}",
});
K('#imageBtn').click(function() {
editor.loadPlugin('image', function() {
editor.plugin.imageDialog({
imageUrl : K('#{$this->id}').val(),
clickFn : function(url, title, width, height, border, align) {
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'file-dialog':
$script = <<<EOT
KindEditor.ready(function(K) {
var editor = K.editor({
allowFileManager : true,
"uploadJson":"{$uploadJson}",
"fileManagerJson":"{$fileManagerJson}",
});
K('#insertfile').click(function() {
editor.loadPlugin('insertfile', function() {
editor.plugin.fileDialog({
fileUrl : K('#{$this->id}').val(),
clickFn : function(url, title) {
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
default:
$script = "KindEditor.ready(function(K) {
K.create('#" . $this->id . "', " . $clientOptions . ");
});";
break;
}
$this->view->registerJs($script, View::POS_READY);
}
}