-
Notifications
You must be signed in to change notification settings - Fork 10
/
CKEditor.php
125 lines (105 loc) · 3.11 KB
/
CKEditor.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace webvimark\extensions\ckeditor;
use yii\base\Widget;
use Yii;
class CKEditor extends Widget
{
const TYPE_FULL = 'full';
const TYPE_STANDARD = 'standard';
const TYPE_SIMPLE = 'simple';
const TYPE_INLINE = 'inline';
/**
* "full", "standard", "simple"
*
* @var string
*/
public $type = self::TYPE_STANDARD;
/**
* @var string
*/
public $height = '200px';
/**
* @var string
*/
public $language;
/**
* @var string
*/
public $toolBarConfig;
/**
* If empty, all textarea's will be replaced
*
* @var string
*/
public $replaceByClass;
/**
* @return string|void
*/
public function run()
{
if ( !$this->language )
$this->language = Yii::$app->language;
$bundle = CKEditorAsset::register($this->view);
$dir = $bundle->baseUrl;
$script = '';
if ( $this->type != CKEditor::TYPE_INLINE)
{
$script .= "
CKEDITOR.replaceAll(function(textarea, config) {
config.height = '{$this->height}';
";
if ( $this->replaceByClass ) {
$script .= " var classRegex = new RegExp('(?:^| )' + '" . $this->replaceByClass . "' + '(?:$| )');\n";
$script .= " if (!classRegex.test(textarea.className))\n";
$script .= " return false;\n";
}
$script .= "});";
}
$script .= "
CKEDITOR.config.language = '{$this->language}';
CKEDITOR.config.filebrowserBrowseUrl = '$dir/kcfinder/browse.php?type=files';
CKEDITOR.config.filebrowserImageBrowseUrl = '$dir/kcfinder/browse.php?type=images';
CKEDITOR.config.filebrowserFlashBrowseUrl = '$dir/kcfinder/browse.php?type=flash';
CKEDITOR.config.filebrowserUploadUrl = '$dir/kcfinder/upload.php?type=files';
CKEDITOR.config.filebrowserImageUploadUrl = '$dir/kcfinder/upload.php?type=images';
CKEDITOR.config.filebrowserFlashUploadUrl = '$dir/kcfinder/upload.php?type=flash';
CKEDITOR.config.allowedContent = true;
";
if ( $this->toolBarConfig )
{
$script .= $this->toolBarConfig;
}
elseif ( $this->type == CKEditor::TYPE_SIMPLE )
{
$script .= "
CKEDITOR.config.toolbar = [
['Maximize','Format','Bold','Italic','Underline','StrikeThrough','RemoveFormat','-','NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','Link', 'Unlink']
] ;
";
}
elseif ( $this->type == CKEditor::TYPE_STANDARD )
{
$script .= "
CKEDITOR.config.toolbar = [
['Maximize', 'Format'],
['Bold','Italic','Underline','StrikeThrough','RemoveFormat','-','TextColor'],
['NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Image','Table','-','Link', 'Unlink', 'Anchor'], ['Source'],
] ;
";
}
elseif ( $this->type == CKEditor::TYPE_INLINE )
{
$script .= "
CKEDITOR.config.extraPlugins = 'inlinesave';
CKEDITOR.config.toolbar = [
['Inlinesave', 'Inlinecancel','Format'],
['Bold','Italic','Underline','StrikeThrough','RemoveFormat','-','TextColor'],
['NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Image','Table','-','Link', 'Unlink']
] ;
";
}
$this->view->registerJs($script);
}
}