-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codemirror.php
68 lines (59 loc) · 2.13 KB
/
Codemirror.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
<?php
namespace andreosoft\codemirror;
use yii\helpers\Json;
use yii\helpers\Html;
use yii\widgets\InputWidget;
class Codemirror extends InputWidget {
public $editorHeight = '800';
public $editorOptions = [];
private $defaultOptions = [
'lineNumbers' => true,
'matchBrackets' => true,
'indentUnit' => 4,
'indentWithTabs' => true,
'lineWrapping' => true,
'mode' => 'application/x-httpd-php',
'extraKeys' => [
'Ctrl-Space' => 'autocomplete',
'Alt-F' => 'autoformat'],
];
/**
* @inheritdoc
*/
public function init() {
parent::init();
CodemirrorAsset::register($this->getView());
$this->options['id'] = $this->getId();
$js = "
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.showHint(cm, CodeMirror.htmlHint);
}
CodeMirror.commands.autoformat = function(cm) {
var range = { from: cm.getCursor(true), to: cm.getCursor(false) };
cm.autoFormatRange(range.from, range.to);
}
CodeMirror.commands.save = function(cm) {
$('#".$this->options['id']."').text(cm.getValue());
}
";
$this->getView()->registerJs($js);
$this->editorOptions = \yii\helpers\ArrayHelper::merge($this->defaultOptions, $this->editorOptions);
$js = "var editor = CodeMirror.fromTextArea(document.getElementById(\"{$this->options['id']}\")";
// $js .= '{ extraKeys: {"Ctrl-Space": "autocomplete"} }';
$js .= empty($this->editorOptions) ? '' : ', ' . (Json::encode($this->editorOptions));
$js .= ");";
$js .= "editor.setSize(\"100%\", \"{$this->editorHeight}\");";
$this->getView()->registerJs($js);
}
/**
* @inheritdoc
*/
public function run() {
if ($this->hasModel()) {
$content = Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
$content = Html::textarea($this->name, $this->value, $this->options);
}
return $content;
}
}