-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAce.php
89 lines (78 loc) · 2.4 KB
/
Ace.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
<?php
namespace devgroup\ace;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;
class Ace extends InputWidget
{
public $mode = 'php';
public $theme = 'github';
public $jsOptions = [];
public $htmlOptions = [];
public function init()
{
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->id;
} else {
$this->id = $this->options['id'];
}
if (is_object($this->model)) {
$this->value = Html::getAttributeValue($this->model, $this->attribute);
$this->name = Html::getInputName($this->model, $this->attribute);
}
$this->options['id'] .= '-ace';
}
public function run()
{
$view = $this->getView();
AceAsset::register($view);
$jsOptions = Json::encode($this->jsOptions);
$htmlOptions = Json::encode(
ArrayHelper::merge(
[
],
$this->htmlOptions
)
);
$aceWidgetJs = <<< JS
var aceWidget = {
"idElement" : null,
"mode" : null,
"theme" : null,
"jsOptions" : [],
"htmlOptions" : [],
"init" : function(){
var textarea = $("#" + this.idElement);
var editDiv = $('<div>', this.htmlOptions).insertBefore(textarea);
textarea.addClass('hidden');
var editor = ace.edit(editDiv[0]);
editor.setOptions(this.jsOptions);
editor.getSession().setValue(textarea.val());
if (this.mode.length !== 0) {
editor.getSession().setMode('ace/mode/' + this.mode);
}
if (this.theme.length !== 0) {
editor.setTheme('ace/theme/' + this.theme);
}
editor.getSession().on('change', function() {
textarea.val(editor.getSession().getValue());
});
}
}
JS;
$view->registerJs($aceWidgetJs, View::POS_READY, 'Yii2AceWidget');
$elementJs = <<< JS
aceWidget.idElement = "{$this->options['id']}";
aceWidget.mode = "{$this->mode}";
aceWidget.theme = "{$this->theme}";
aceWidget.jsOptions = {$jsOptions};
aceWidget.htmlOptions = {$htmlOptions};
aceWidget.init();
JS;
$view->registerJs($elementJs);
echo Html::textarea($this->name, $this->value, $this->options);
}
}