Skip to content

Commit

Permalink
Fixes merge conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanneculai committed Apr 4, 2016
2 parents 9872eeb + b755e36 commit 234ee1b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 61 deletions.
165 changes: 126 additions & 39 deletions FroalaEditorAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,143 @@

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',
'js/plugins/align.min.js',
'js/plugins/char_counter.min.js',
'js/plugins/code_beautifier.min.js',
'js/plugins/code_view.min.js',
'js/plugins/colors.min.js',
'js/plugins/draggable.min.js',
'js/plugins/emoticons.min.js',
'js/plugins/entities.min.js',
'js/plugins/file.min.js',
'js/plugins/font_family.min.js',
'js/plugins/font_size.min.js',
'js/plugins/fullscreen.min.js',
'js/plugins/image.min.js',
'js/plugins/image_manager.min.js',
'js/plugins/inline_style.min.js',
'js/plugins/line_breaker.min.js',
'js/plugins/link.min.js',
'js/plugins/lists.min.js',
'js/plugins/paragraph_format.min.js',
'js/plugins/paragraph_style.min.js',
'js/plugins/quick_insert.min.js',
'js/plugins/quote.min.js',
'js/plugins/save.min.js',
'js/plugins/table.min.js',
'js/plugins/url.min.js',
'js/plugins/video.min.js'
];
public $css = [
'css/froala_editor.min.css',
'css/froala_style.min.css',
'css/plugins/char_counter.min.css',
'css/plugins/code_view.min.css',
'css/plugins/colors.min.css',
'css/plugins/draggable.min.css',
'css/plugins/emoticons.min.css',
'css/plugins/file.min.css',
'css/plugins/fullscreen.min.css',
'css/plugins/image_manager.min.css',
'css/plugins/image.min.css',
'css/plugins/line_breaker.min.css',
'css/plugins/quick_insert.min.css',
'css/plugins/table.min.css',
'css/plugins/video.min.css'
];
public $depends = [
// use depends instead of direct CDNs
'\yii\web\JqueryAsset',
'\rmrevin\yii\fontawesome\AssetBundle',
];
/**
* @var $clientPlugins array leave empty to load all plugins
* <pre>sample input:
* [
* //specify only needed forala plugins (local files)
* 'url',
* 'align',
* 'char_counter',
* ...
* //override default files for a specific plugin
* 'table' => [
* 'css' => '<new css file url>'
* ],
* //include custom plugin
* 'my_plugin' => [
* 'js' => '<js file url>' // required
* 'css' => '<css file url>' // optional
* ],
* ...
* ]
*/
static public $clientPlugins;

/**
* @var $excludedPlugins array list of plugin names to be excluded
*/
static public $excludedPlugins;

/**
* @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');
if (is_array(static::$clientPlugins)) {
if (ArrayHelper::isIndexed(static::$clientPlugins, true)) {
// sequential array = list of plugins to be included
// use default configurations for every plugin
$this->registerPlugins(static::$clientPlugins);
} else {
// associative array = custom plugins and options included
foreach (static::$clientPlugins as $key => $value) {
if (is_numeric($key)) {
$pluginName = $value;
if (!$this->isPluginExcluded($pluginName)) {
$this->registerPlugin($pluginName);
}
} else {
$pluginName = $key;
if (!$this->isPluginExcluded($pluginName)) {
$pluginOptions = $value;
$issetJs = isset($pluginOptions['js']);
$issetCss = isset($pluginOptions['css']);
if ($issetJs) {
$this->js[] = $pluginOptions['js'];
} else {
$jsFile = "js/plugins/$pluginName.min.js";
if (is_file($this->froalaBowerPath . '/' . $jsFile)) {
$this->js[] = $jsFile;
} else {
throw new Exception("you must set 'js' [and 'css'] for plugin '$pluginName'");
}
}
if ($issetCss) {
$this->css[] = $pluginOptions['css'];
}
}
}
}
}
} else {
$this->registerPlugins(array_diff($this->froalaPlugins, static::$excludedPlugins ?: []), false, true);
}
parent::init();
}

public function registerPlugin($pluginName, $checkJs = true, $checkCss = true)
{
$jsFile = "js/plugins/$pluginName.min.js";
if ($checkJs || $this->isPluginJsFileExist($pluginName)) {
$this->js[] = $jsFile;
$cssFile = "css/plugins/$pluginName.min.css";
if (!$checkCss || $this->isPluginCssFileExist($pluginName)) {
$this->css[] = $cssFile;
}
} else {
throw new Exception("plugin '$pluginName' is not supported, if you trying to set custom plugin, please set 'js' and 'css' 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/js/plugins/$pluginName.min.js");
}

public function isPluginCssFileExist($pluginName)
{
return is_file("$this->froalaBowerPath/css/plugins/$pluginName.min.css");
}

private function isPluginExcluded($pluginName)
{
return in_array($pluginName, static::$excludedPlugins);
}
}
40 changes: 19 additions & 21 deletions FroalaEditorWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\widgets\InputWidget;

class FroalaEditorWidget extends InputWidget
{
const PLUGIN_NAME = 'FroalaEditor';

/**
* FroalaEditor Plugins
* @var array
*/
public $clientPlugins;

/**
* Exclude listed plugins
* @var array
*/
public $excludedPlugins;

/**
* FroalaEditor Options
* @var array
Expand Down Expand Up @@ -43,6 +54,7 @@ public function run()
}
$this->registerClientScript();
}

/**
* register client scripts(css, javascript)
*/
Expand All @@ -51,7 +63,10 @@ public function registerClientScript()
$view = $this->getView();
$this->initClientOptions();

FroalaEditorAsset::$clientPlugins = $this->clientPlugins;
FroalaEditorAsset::$excludedPlugins = $this->excludedPlugins;
$asset = FroalaEditorAsset::register($view);

//theme
$themeType = isset($this->clientOptions['theme']) ? $this->clientOptions['theme'] : 'default';
if ($themeType != 'default') {
Expand All @@ -66,27 +81,10 @@ public function registerClientScript()
$options = empty($this->options) ? '' : Json::encode($this->options);
$id = $this->options['id'];

$this->updateAsset();
$view ->registerJsFile('http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js');
$view ->registerCssFile('http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css');
$varName = self::PLUGIN_NAME . '_' . str_replace('-', '_', $id);
$js = "
$('#".$id ."').froalaEditor(
" . Json::encode($this->clientOptions) . "
);
";
$view->registerJs($js);
}
/*
*
* */
public function updateAsset(){
//Replace jquery 2
Yii::$app->assetManager->bundles['yii\web\JqueryAsset'] = [
'sourcePath' => null,
'js' => ['jquery.js' => 'http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js'],
];
$jsOptions = Json::encode($this->clientOptions);
$view->registerJs("\$('#$id').froalaEditor($jsOptions);");
}

/**
* client options init
*/
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
],
"require": {
"yiisoft/yii2": "*",
"bower-asset/froala-wysiwyg-editor": "2.2.3"
"bower-asset/froala-wysiwyg-editor": "2.2.3",
"rmrevin/yii2-fontawesome": "*"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 234ee1b

Please sign in to comment.