-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editormd.php
70 lines (64 loc) · 1.99 KB
/
Editormd.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
<?php
namespace moxuandi\editormd;
use moxuandi\helpers\ArrayHelper;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\widgets\InputWidget;
/**
* Editormd renders a editor js plugin for classic editing.
*
* @author zhangmoxuan <1104984259@qq.com>
* @link http://www.zhangmoxuan.com
* @QQ 1104984259
* @Date 2019-9-2
* @see http://editor.md.ipandao.com/
*/
class Editormd extends InputWidget
{
/**
* @var array 编辑器配置接口
*/
public $editorOptions = [];
/**
* @var array textarea 外层嵌套的 div 的HTML属性
*/
public $nestingOptions = [];
/**
* @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([
'width' => '100%',
'height' => 640,
'path' => Yii::$app->assetManager->getBundle(EditormdAsset::class)->baseUrl . '/dist/lib/',
'imageUploadURL' => Url::to(['EditormdUpload']),
], $this->editorOptions);
$this->nestingOptions = array_merge([
'id' => 'nesting_' . $this->id,
], $this->editorOptions);
}
/**
* 渲染输入域
* @return string
*/
public function run()
{
$this->registerScript();
return Html::tag(ArrayHelper::remove($this->nestingOptions, 'tag', 'div'), $this->hasModel() ? Html::activeTextarea($this->model, $this->attribute, $this->options) : Html::textarea($this->name, $this->value, $this->options), $this->nestingOptions);
}
/**
* 注册客户端脚本
*/
public function registerScript()
{
EditormdAsset::register($this->view);
$editorOptions = Json::encode($this->editorOptions);
$this->view->registerJs("editormd('{$this->nestingOptions['id']}', {$editorOptions});");
}
}