-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKindEditor.php
332 lines (324 loc) · 11.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
namespace moxuandi\kindeditor;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\widgets\InputWidget;
/**
* KindEditor renders a editor js plugin for classic editing.
*
* @author zhangmoxuan <[email protected]>
* @link http://www.zhangmoxuan.com
* @QQ 1104984259
* @Date 2019-2-8
* @see http://kindeditor.net
*/
class KindEditor extends InputWidget
{
/**
* @var array 配置接口, 参阅[KindEditor 官方文档](http://kindeditor.net/docs/option.html)
*/
public $editorOptions = [];
/**
* @var string 编辑器的类型, 可用值有:
* - `textEditor`: HTML 编辑器(默认值)
* - `colorPicker`: 取色器, 无法自定义颜色(编辑器调用时正常)
* - `fileDialog`: 上传文件
* - `imageDialog`: 上传图片(网络图片 + 本地上传)
* - `remoteImageDialog`: 上传图片(网络图片)
* - `localImageDialog`: 上传图片(本地上传)
* - `imageManager`: 浏览服务器(图片)
* - `flashManager`: 浏览服务器(Flash)
* - `mediaManager`: 浏览服务器(视音频)
* - `fileManager`: 浏览服务器(文件)
* - `uploadButton`: 自定义上传按钮(不建议)
* - `multiImageDialog`: 批量上传图片(未实现)
*/
public $editorType;
/**
* @var string 单独调用插件时, 渲染 HTML 的模板
*/
public $editorTemplate = "<div class='input-group'>{input}<span class='input-group-btn'>{button}</span></div>";
/**
* @var array input 输入域的 HTML 属性
*/
public $options = ['class' => 'form-control'];
/**
* @var array button 按钮的 HTML 属性. 特殊属性:
* - `label`: button 按钮的文本内容(不是 label 属性)
*/
public $buttonOptions = ['class' => 'btn btn-default'];
/**
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
$this->hasModel() ? $this->id = $this->options['id'] : $this->id = $this->options['id'] = $this->id . '_' . $this->name;
$this->editorOptions = array_merge([
'allowFileManager' => true, // 显示浏览远程服务器按钮
'uploadJson' => Url::to(['Kupload', 'action'=>'uploadJson']), // 指定上传文件的服务器端程序
'fileManagerJson' => Url::to(['Kupload', 'action'=>'fileManagerJson']), // 指定浏览远程图片的服务器端程序
'width' => '100%',
'height' => 300,
], $this->editorOptions);
}
public function run()
{
switch($this->editorType){
case 'colorPicker': $html = $this->renderHtml($this->id . '_colorPicker', '打开取色器'); break;
case 'fileDialog': $html = $this->renderHtml($this->id . '_fileDialog', '选择文件'); break;
case 'imageDialog': $html = $this->renderHtml($this->id . '_imageDialog', '选择图片'); break;
case 'remoteImageDialog': $html = $this->renderHtml($this->id . '_remoteImageDialog', '选择图片'); break;
case 'localImageDialog': $html = $this->renderHtml($this->id . '_localImageDialog', '选择图片'); break;
case 'imageManager': $html = $this->renderHtml($this->id . '_imageManager', '浏览服务器'); break;
case 'flashManager': $html = $this->renderHtml($this->id . '_flashManager', '浏览服务器'); break;
case 'mediaManager': $html = $this->renderHtml($this->id . '_mediaManager', '浏览服务器'); break;
case 'fileManager': $html = $this->renderHtml($this->id . '_fileManager', '浏览服务器'); break;
//case 'uploadButton': break;
//case 'multiImageDialog': break;
default: $html = $this->hasModel() ? Html::activeTextarea($this->model, $this->attribute, $this->options) : Html::textarea($this->name, $this->value, $this->options); break;
}
$this->registerScript(); // 放在最后, 是为了可以自定义 $this->buttonOptions['id']
return $html;
}
/**
* 单独调用插件时, 渲染的 HTML.
* @param string $buttonID button 按钮的ID
* @param string $buttonLabel button 按钮的文本内容(不是 label 属性)
* @return mixed 渲染的 HTML.
*/
public function renderHtml($buttonID, $buttonLabel)
{
$this->buttonOptions = array_merge(['id' => $buttonID], $this->buttonOptions);
$buttonLabel = ArrayHelper::remove($this->buttonOptions, 'label', $buttonLabel);
if($this->hasModel()){
return str_replace(['{input}', '{button}'], [Html::activeTextInput($this->model, $this->attribute, $this->options), Html::button($buttonLabel, $this->buttonOptions)], $this->editorTemplate);
}else{
return str_replace(['{input}', '{button}'], [Html::textInput($this->name, $this->value, $this->options), Html::button($buttonLabel, $this->buttonOptions)], $this->editorTemplate);
}
}
/**
* 注册客户端脚本
*/
public function registerScript()
{
KindEditorAsset::register($this->view);
$buttonID = ArrayHelper::getValue($this->buttonOptions, 'id');
switch($this->editorType){
case 'colorPicker':
$script = <<<EOT
KindEditor.ready(function(K){
var colorpicker;
K('#{$buttonID}').bind('click', function(e){
e.stopPropagation();
if(colorpicker){
colorpicker.remove();
colorpicker = null;
return;
}
var colorpickerPos = K('#{$buttonID}').pos();
colorpicker = K.colorpicker({
x: colorpickerPos.x,
y: colorpickerPos.y + K('#{$buttonID}').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 'fileDialog':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
allowFileManager: true,
uploadJson: "{$this->editorOptions['uploadJson']}",
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').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;
case 'imageDialog':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
allowFileManager: true,
uploadJson: "{$this->editorOptions['uploadJson']}",
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').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 'remoteImageDialog':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
allowFileManager: true,
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').click(function(){
editor.loadPlugin('image', function(){
editor.plugin.imageDialog({
showLocal: false,
imageUrl: K('#{$this->id}').val(),
clickFn: function(url, title, width, height, border, align){
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'localImageDialog':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
uploadJson: "{$this->editorOptions['uploadJson']}"
});
K('#{$buttonID}').click(function(){
editor.loadPlugin('image', function(){
editor.plugin.imageDialog({
showRemote: false,
imageUrl: K('#{$this->id}').val(),
clickFn: function(url, title, width, height, border, align){
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'imageManager':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').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 'flashManager':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').click(function(){
editor.loadPlugin('filemanager', function(){
editor.plugin.filemanagerDialog({
viewType: 'LIST',
dirName: 'flash',
clickFn: function(url, title){
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'mediaManager':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').click(function(){
editor.loadPlugin('filemanager', function(){
editor.plugin.filemanagerDialog({
viewType: 'LIST',
dirName: 'media',
clickFn: function(url, title){
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
case 'fileManager':
$script = <<<EOT
KindEditor.ready(function(K){
var editor = K.editor({
fileManagerJson: "{$this->editorOptions['fileManagerJson']}"
});
K('#{$buttonID}').click(function(){
editor.loadPlugin('filemanager', function(){
editor.plugin.filemanagerDialog({
viewType: 'LIST',
dirName: 'file',
clickFn: function(url, title){
K('#{$this->id}').val(url);
editor.hideDialog();
}
});
});
});
});
EOT;
break;
//case 'uploadButton': break;
//case 'multiImageDialog': break;
default:
$script = "KindEditor.ready(function(K){K.create('#{$this->id}', " . Json::encode($this->editorOptions) . ")});";
break;
}
$this->view->registerJs($script);
}
}