Skip to content

Commit

Permalink
Initial 1.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
brussens committed Sep 25, 2018
1 parent f4681a2 commit c6bf1e2
Show file tree
Hide file tree
Showing 70 changed files with 2,123 additions and 2 deletions.
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# yii2-trumbowyg
Trumbowyg extension for YiiFramework 2.x.x
# Trumbowyg extension for YiiFramework 2.x.x

[![Latest Stable Version](https://poser.pugx.org/brussens/yii2-trumbowyg/v/stable)](https://packagist.org/packages/brussens/yii2-trumbowyg)
[![Total Downloads](https://poser.pugx.org/brussens/yii2-trumbowyg/downloads)](https://packagist.org/packages/brussens/yii2-trumbowyg)
[![License](https://poser.pugx.org/brussens/yii2-trumbowyg/license)](https://packagist.org/packages/brussens/yii2-trumbowyg)

## Install
Either run
```
php composer.phar require --prefer-dist brussens/yii2-trumbowyg "*"
```

or add

```
"brussens/yii2-trumbowyg": "*"
```

to the require section of your `composer.json` file.

## Basic usage:
```php
use brussens\yii2\extensions\trumbowyg\TrumbowygWidget;

echo $form->field($form, 'content')->widget(TrumbowygWidget::className());
```

## Advanced usage
```php
use brussens\yii2\extensions\trumbowyg\TrumbowygWidget;

echo $form->field($form, 'content')->widget(TrumbowygWidget::className(), [
'clientOptions' => [
'btns' => [
['viewHTML'],
['undo', 'redo'],
['table'],
['formatting'],
['strong', 'em', 'del'],
['link'],
['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'],
['unorderedList', 'orderedList'],
['removeformat'],
['fullscreen'],
['upload'],
]
],
'plugins' => [
TrumbowygWidget::PLUGIN_UPLOAD,
TrumbowygWidget::PLUGIN_TABLE,
TrumbowygWidget::PLUGIN_HISTORY
]
]);
```
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "brussens/yii2-trumbowyg",
"description": "Trumbowyg extension for YiiFramework 2.x.x",
"keywords": ["yii", "extension", "widget", "trumbowyg", "wysiwyg", "yii2"],
"type": "yii2-extension",
"license": "MIT",
"support": {
"issues": "https://github.com/brussens/yii2-trumbowyg/issues?state=open",
"source": "https://github.com/brussens/yii2-trumbowyg"
},
"authors": [
{
"name": "Dmitry Brusensky",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "^2.0.0",
"bower-asset/trumbowyg": "^2.11.0"
},
"autoload": {
"psr-4": {
"brussens\\yii2\\extensions\\trumbowyg\\": "src/"
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
233 changes: 233 additions & 0 deletions src/TrumbowygWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php
/**
* @link https://github.com/brussens/yii2-trumbowyg
* @copyright Copyright © since 2018 Brusensky Dmitry. All rights reserved
* @licence http://opensource.org/licenses/MIT MIT
*/

namespace brussens\yii2\extensions\trumbowyg;

use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\AssetBundle;
use yii\widgets\InputWidget;
use brussens\yii2\extensions\trumbowyg\assets\TrumbowygAsset;

/**
* Trumbowyg plugin widget.
*
* @since 1.0.0
* @author Brusensky Dmitry <[email protected]>
*/
class TrumbowygWidget extends InputWidget
{
const PLUGIN_ALLOWTAGSFROMPASTE = 'allowtagsfrompaste';
const PLUGIN_BASE64 = 'base64';
const PLUGIN_CLEANPASTE = 'cleanpaste';
const PLUGIN_COLORS = 'colors';
const PLUGIN_EMOJI = 'emoji';
const PLUGIN_FONTFAMILY = 'fontfamily';
const PLUGIN_FONTSIZE = 'fontsize';
const PLUGIN_HIGHLIGHT = 'highlight';
const PLUGIN_HISTORY = 'history';
const PLUGIN_INSERTAUDIO = 'insertaudio';
const PLUGIN_LINEHEIGHT = 'lineheight';
const PLUGIN_MATHML = 'mathml';
const PLUGIN_MENTION = 'mention';
const PLUGIN_NOEMBED = 'noembed';
const PLUGIN_PASTEEMBED = 'pasteembed';
const PLUGIN_PASTEIMAGE = 'pasteimage';
const PLUGIN_PREFORMATTED = 'preformatted';
const PLUGIN_RESIZIMG = 'resizimg';
const PLUGIN_RUBY = 'ruby';
const PLUGIN_TABLE = 'table';
const PLUGIN_TEMPLATE = 'template';
const PLUGIN_UPLOAD = 'upload';

/**
* jQuery plugin options
*
* @see https://alex-d.github.io/Trumbowyg/documentation/#basic-options
* @see https://alex-d.github.io/Trumbowyg/documentation/#advanced-options
* @var array
*/
public $clientOptions = [];
/**
* Plugins list for load
*
* @see https://alex-d.github.io/Trumbowyg/documentation/plugins/
* @var array
*/
public $plugins = [];

/**
* @inheritdoc
*/
public function init()
{
parent::init();
if(!isset($this->clientOptions['lang'])) {
$this->clientOptions['lang'] = $this->getLanguageName();
}
}

/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();

if ($this->hasModel()) {
return Html::activeTextarea($this->model, $this->attribute, $this->options);
}
return Html::textarea($this->name, $this->value, $this->options);
}

/**
* Register assets
*/
protected function registerAssets()
{
$view = $this->getView();
TrumbowygAsset::register($view);
$this->registerLangAsset();
$this->registerPluginsAssets();

$options = Json::encode($this->clientOptions);
$view->registerJs('jQuery("#' . $this->options['id'] . '").trumbowyg(' . $options . ');');
}

/**
* Register plugins assets
*/
protected function registerPluginsAssets()
{
$view = $this->getView();
foreach ($this->plugins as $plugin) {
if (is_array($plugin)) {
if(isset($plugin['name'], $plugin['asset'])) {
/** @var AssetBundle $asset */
$asset = $plugin['asset'];
$asset::register($view);
}
} else {
$this->registerCorePlugin(strtolower($plugin));
}
}
}

/**
* Register core plugin asset
*
* @param $name
*/
protected function registerCorePlugin($name)
{
$plugins = [
self::PLUGIN_ALLOWTAGSFROMPASTE => 'AllowTagsFromPastePluginAsset',
self::PLUGIN_BASE64 => 'Base64PluginAsset',
self::PLUGIN_CLEANPASTE => 'CleanPastePluginAsset',
self::PLUGIN_COLORS => 'ColorsPluginAsset',
self::PLUGIN_EMOJI => 'EmojiPluginAsset',
self::PLUGIN_FONTFAMILY => 'FontFamilyPluginAsset',
self::PLUGIN_FONTSIZE => 'FontSizePluginAsset',
self::PLUGIN_LINEHEIGHT => 'HighlightPluginAsset',
self::PLUGIN_HISTORY => 'HistoryPluginAsset',
self::PLUGIN_INSERTAUDIO => 'InsertAudioPluginAsset',
self::PLUGIN_LINEHEIGHT => 'LineHeightPluginAsset',
self::PLUGIN_MATHML => 'MathMLPluginAsset',
self::PLUGIN_MENTION => 'MentionPluginAsset',
self::PLUGIN_NOEMBED => 'NoEmbedPluginAsset',
self::PLUGIN_PASTEEMBED => 'PasteEmbedPluginAsset',
self::PLUGIN_PASTEIMAGE => 'PasteImagePluginAsset',
self::PLUGIN_PREFORMATTED => 'PreFormattedPluginAsset',
self::PLUGIN_RESIZIMG => 'ResizimgPluginAsset',
self::PLUGIN_RUBY => 'RubyPluginAsset',
self::PLUGIN_TABLE => 'TablePluginAsset',
self::PLUGIN_TEMPLATE => 'TemplatePluginAsset',
self::PLUGIN_UPLOAD => 'UploadPluginAsset'
];

if(isset($plugins[$name])) {
/** @var AssetBundle $asset */
$asset = 'brussens\yii2\extensions\trumbowyg\assets\plugins\\' . $plugins[$name];
$asset::register($this->getView());
}
}

/**
* Register translation asset
*/
protected function registerLangAsset()
{
$languages = [
'ar' => 'ArLangAsset',
'bg' => 'BgLangAsset',
'by' => 'ByLangAsset',
'ca' => 'CaLangAsset',
'cs' => 'CsLangAsset',
'da' => 'DaLangAsset',
'de' => 'DeLangAsset',
'el' => 'ElLangAsset',
'es' => 'EsLangAsset',
'es_ar' => 'EsArLangAsset',
'fa' => 'FaLangAsset',
'fi' => 'FiLangAsset',
'fr' => 'FrLangAsset',
'he' => 'HeLangAsset',
'hr' => 'HrLangAsset',
'hu' => 'HuLangAsset',
'id' => 'IdLangAsset',
'it' => 'ItLangAsset',
'ja' => 'JaLangAsset',
'ko' => 'KoLangAsset',
'lt' => 'LtLangAsset',
'mn' => 'MnLangAsset',
'my' => 'MyLangAsset',
'nl' => 'NlLangAsset',
'no_nb' => 'NoNbLangAsset',
'ph' => 'PhLangAsset',
'pl' => 'PlLangAsset',
'pt' => 'PtLangAsset',
'pt_br' => 'PtBrLangAsset',
'ro' => 'RoLangAsset',
'rs' => 'RsLangAsset',
'rs_latin' => 'RsLatinLangAsset',
'ru' => 'RuLangAsset',
'sk' => 'SkLangAsset',
'sq' => 'SqLangAsset',
'sv' => 'SvLangAsset',
'th' => 'ThLangAsset',
'tr' => 'TrLangAsset',
'ua' => 'UaLangAsset',
'vi' => 'ViLangAsset',
'zh_cn' => 'ZhCnLangAsset',
'zh_tw' => 'ZhTwLangAsset',
];

$language = $this->clientOptions['lang'];
if(isset($languages[$language])) {
/** @var AssetBundle $asset */
$asset = 'brussens\yii2\extensions\trumbowyg\assets\langs\\' . $languages[$language];
$asset::register($this->getView());
}
}

/**
* Normalize language code for jQuery plugin
*
* @see https://alex-d.github.io/Trumbowyg/documentation/#add-localization
* @return string
*/
protected function getLanguageName()
{
$language = strtolower(Yii::$app->language);
$except = ['zh-cn','zh-tw','es-ar','no-nb','pt-br','rs-latin'];
if(!in_array($language, $except) && preg_match('/[a-z]+\-[a-z0-9]+/', $language)) {
$language = explode('-', $language)[0];
}
return $language;
}
}
30 changes: 30 additions & 0 deletions src/assets/LangAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://github.com/brussens/yii2-trumbowyg
* @copyright Copyright © since 2018 Brusensky Dmitry. All rights reserved
* @licence http://opensource.org/licenses/MIT MIT
*/

namespace brussens\yii2\extensions\trumbowyg\assets;

use yii\web\AssetBundle;

/**
* Abstract locale translation Asset Bundle
*
* @since 1.0.0
* @author Brusensky Dmitry <[email protected]>
*/
abstract class LangAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = '@bower/trumbowyg/dist';
/**
* @inheritdoc
*/
public $depends = [
'brussens\yii2\extensions\trumbowyg\assets\TrumbowygAsset'
];
}
30 changes: 30 additions & 0 deletions src/assets/PluginAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://github.com/brussens/yii2-trumbowyg
* @copyright Copyright © since 2018 Brusensky Dmitry. All rights reserved
* @licence http://opensource.org/licenses/MIT MIT
*/

namespace brussens\yii2\extensions\trumbowyg\assets;

use yii\web\AssetBundle;

/**
* Abstract plugin Asset Bundle
*
* @since 1.0.0
* @author Brusensky Dmitry <[email protected]>
*/
abstract class PluginAsset extends AssetBundle
{
/**
* @var string
*/
public $sourcePath = '@bower/trumbowyg/dist';
/**
* @var array
*/
public $depends = [
'brussens\yii2\extensions\trumbowyg\assets\TrumbowygAsset'
];
}
Loading

0 comments on commit c6bf1e2

Please sign in to comment.