-
Notifications
You must be signed in to change notification settings - Fork 68
/
UEditor.php
66 lines (59 loc) · 1.88 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
<?php
/**
* @link https://github.com/BigKuCha/yii2-ueditor-widget
* @link http://ueditor.baidu.com/website/index.html
*/
namespace kucha\ueditor;
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 UEditor extends InputWidget
{
//配置选项,参阅Ueditor官网文档(定制菜单等)
public $clientOptions = [];
//默认配置
protected $_options;
/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
if (isset($this->options['id'])) {
$this->id = $this->options['id'];
} else {
$this->id = $this->hasModel() ? Html::getInputId($this->model,
$this->attribute) : $this->id;
}
$this->_options = [
'serverUrl' => Url::to(['upload']),
'initialFrameWidth' => '100%',
'initialFrameHeight' => '400',
'lang' => (strtolower(Yii::$app->language) == 'en-us') ? 'en' : 'zh-cn',
];
$this->clientOptions = ArrayHelper::merge($this->_options, $this->clientOptions);
parent::init();
}
public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
return Html::activeTextarea($this->model, $this->attribute, ['id' => $this->id]);
} else {
return Html::textarea($this->name, $this->value, ['id' => $this->id]);
}
}
/**
* 注册客户端脚本
*/
protected function registerClientScript()
{
UEditorAsset::register($this->view);
$clientOptions = Json::encode($this->clientOptions);
$script = "UE.getEditor('" . $this->id . "', " . $clientOptions . ");";
$this->view->registerJs($script, View::POS_READY);
}
}