forked from froala/yii2-froala-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FroalaEditorAsset.php
150 lines (135 loc) · 5.43 KB
/
FroalaEditorAsset.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace froala\froalaeditor;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use yii\web\AssetBundle;
class FroalaEditorAsset extends AssetBundle
{
public $sourcePath = '@bower/froala-wysiwyg-editor';
public $froalaPlugins = [
'align', 'char_counter', 'code_beautifier', 'code_view', 'colors',
'draggable', 'emoticons', 'entities', 'file', 'font_family',
'font_size', 'fullscreen', 'image', 'image_manager', 'inline_style',
'line_breaker', 'link', 'lists', 'paragraph_format', 'paragraph_style',
'quick_insert', 'quote', 'save', 'table', 'url', 'video',
];
public $js = [
'js/froala_editor.min.js',
];
public $css = [
'css/froala_editor.min.css',
'css/froala_style.min.css',
];
public $depends = [
// use depends instead of direct CDNs
'\yii\web\JqueryAsset',
'\rmrevin\yii\fontawesome\AssetBundle',
];
/**
* @var $froalaBowerPath string path to library folder 'froala-wysiwyg-editor'
*/
public $froalaBowerPath;
public function init()
{
$this->froalaBowerPath = $this->froalaBowerPath ?: \Yii::getAlias('@bower/froala-wysiwyg-editor');
parent::init();
}
public function registerClientPlugins($clientPlugins, $excludedPlugins)
{
if (is_array($clientPlugins)) {
if (ArrayHelper::isIndexed($clientPlugins, true)) {
// sequential array = list of plugins to be included
// use default configurations for every plugin
$this->registerPlugins($clientPlugins);
} else {
// associative array = custom plugins and options included
foreach ($clientPlugins as $key => $value) {
if (is_numeric($key)) {
$pluginName = $value;
if (!$this->isPluginExcluded($pluginName, $excludedPlugins)) {
$this->registerPlugin($pluginName);
}
} else {
$pluginName = $key;
if (!$this->isPluginExcluded($pluginName, $excludedPlugins)) {
$pluginOptions = $value;
$issetJs = isset($pluginOptions['js']);
$issetCss = isset($pluginOptions['css']);
if ($issetJs) {
if (is_array($pluginOptions['js'])) {
foreach ($pluginOptions['js'] as $_js) {
$this->addJs($_js);
}
} else {
$this->addJs($pluginOptions['js']);
}
} else {
if ($this->isPluginJsFileExist($pluginName)) {
$this->addJs($this->getDefaultJsUrl($pluginName));
} else {
throw new Exception("you must set 'js' [and 'css'] for plugin '$pluginName'");
}
}
if ($issetCss) {
$this->addCss($pluginOptions['css']);
} else {
if ($this->isPluginCssFileExist($pluginName)) {
$this->addCss($this->getDefaultCssUrl($pluginName));
}
}
}
}
}
}
} else {
$this->registerPlugins(array_diff($this->froalaPlugins, $excludedPlugins ?: []), false, true);
}
}
public function registerPlugin($pluginName, $checkJs = true, $checkCss = true)
{
$jsFile = "js/plugins/$pluginName.min.js";
if ($checkJs || $this->isPluginJsFileExist($pluginName)) {
$this->addJs($jsFile);
$cssFile = "css/plugins/$pluginName.min.css";
if (!$checkCss || $this->isPluginCssFileExist($pluginName)) {
$this->addCss($cssFile);
}
} else {
throw new Exception("plugin '$pluginName' is not supported, if you trying to set custom plugin, please set 'js' and 'css' options for your plugin");
}
}
public function registerPlugins(array $pluginsArray, $checkJs = true, $checkCss = true)
{
foreach ($pluginsArray as $pluginName) {
$this->registerPlugin($pluginName, $checkJs, $checkCss);
}
}
public function isPluginJsFileExist($pluginName)
{
return is_file($this->froalaBowerPath . '/' . $this->getDefaultJsUrl($pluginName));
}
public function isPluginCssFileExist($pluginName)
{
return is_file($this->froalaBowerPath . '/' . $this->getDefaultCssUrl($pluginName));
}
public function isPluginExcluded($pluginName, $excludedPlugins)
{
return in_array($pluginName, $excludedPlugins ?: []);
}
public function addJs($jsFile)
{
$this->js[] = $jsFile;
}
public function addCss($cssFile)
{
$this->css[] = $cssFile;
}
public function getDefaultCssUrl($pluginName)
{
return "css/plugins/$pluginName.min.css";
}
private function getDefaultJsUrl($pluginName)
{
return "js/plugins/$pluginName.min.js";
}
}