-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAceHelper.php
52 lines (43 loc) · 1.55 KB
/
AceHelper.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
<?php
namespace devgroup\ace;
use yii\web\Controller;
class AceHelper
{
/**
* converts all textarea with attribute 'data-editor'
* in the ace text editor
* @param Controller $controller
* @param string $theme
*/
public static function setAceScript(Controller $controller, $theme = 'chrome')
{
/* @var \yii\web\View $view */
$view = $controller->getView();
AceAsset::register($view);
$view->registerJs(
"$('textarea[data-editor]').each(function(index, element) {
var textarea = $(this);
var editDiv = $('<div>', {
width: textarea.width(),
height: textarea.height(),
class: textarea.attr('class')
}).insertBefore(textarea);
textarea.addClass('hidden');
var editor = ace.edit(editDiv[0]);
var mode = (textarea.data('editor')).trim();
var theme = ('{$theme}').trim();
editor.setReadOnly(textarea.data('read-only'));
editor.getSession().setValue(textarea.val());
if (mode.length !== 0) {
editor.getSession().setMode('ace/mode/' + mode);
}
if (theme.length !== 0) {
editor.setTheme('ace/theme/' + theme);
}
editor.getSession().on('change', function() {
textarea.val(editor.getSession().getValue());
});
});"
);
}
}