-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUeditor.php
91 lines (78 loc) · 1.84 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
<?php
/**
*
* @author macle
* Email [email protected]
* qq 429140141
*
*/
namespace macle\ueditor;
use Yii;
use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use macle\ueditor\UeditorAsset;
class Ueditor extends InputWidget
{
/**
* 百度编辑器内设置的参数
* @var array
*/
public $events = [];
public $ucontent = '';
/*
* Tag
* @var bool
*/
public $renderTag=true;
/*
* Initializes the widget
*/
public function init() {
if(isset($this->options['ucontent'])){
$this->ucontent = $this->options['ucontent'];
unset($this->options['ucontent']);
}
$this->events = $this->options;
\Yii::setAlias('@macle\ueditor\assets', '@vendor/macle/yii2-ueditor/assets');
if(empty($this->name)){
$this->name=$this->hasModel() ? Html::getInputName($this->model, $this->attribute): $this->id;
}
//register css & js
$asset = UeditorAsset::register($this->view);
//init options
parent::init();
}
/*
* Renders the widget
*/
public function run() {
$this->registerScripts();
if($this->renderTag===true){
echo $this->renderTag();
}
}
/**
* render file input tag
* @return string
*/
private function renderTag() {
$options = [
'type' => 'text/plain',
'name' => $this->id,
'id' => $this->id
];
return Html::script($this->ucontent, $options);
}
/**
* register script
*/
private function registerScripts() {
$jsonOptions = Json::encode($this->events);
$script = <<<EOF
UE.getEditor('{$this->id}', {$jsonOptions});
EOF;
$this->view->registerJs($script, View::POS_READY);
}
}