Commit 47ab4ac1 authored by knight's avatar knight

init

parents
# Contributing
Contributions are **welcome** and will be fully **credited**.
We accept contributions via Pull Requests on [Github](https://github.com/2amigos/yii2-ckeditor-widget).
## Pull Requests
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
- **Create feature branches** - Don't ask us to pull from your master branch.
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
## Running Tests
``` bash
$ phpunit
```
**Happy coding**!
# The BSD License (BSD)
Copyright (c) 2013-2015, 2amigOS! Consulting Group LLC.
> Redistribution and use in source and binary forms, with or without modification,
> are permitted provided that the following conditions are met:
>
> Redistributions of source code must retain the above copyright notice, this
> list of conditions and the following disclaimer.
>
> Redistributions in binary form must reproduce the above copyright notice, this
> list of conditions and the following disclaimer in the documentation and/or
> other materials provided with the distribution.
>
> Neither the name of 2amigOS! Consulting Group, LLC. nor the names of its
> contributors may be used to endorse or promote products derived from
> this software without specific prior written permission.
>
>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
>ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
>WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
>DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
>ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
>(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
>LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
>ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
>(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
>SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
CKEditor Widget for Yii2
========================
[![Latest Version](https://img.shields.io/github/tag/2amigos/yii2-ckeditor-widget.svg?style=flat-square&label=release)](https://github.com/2amigos/yii2-ckeditor-widget/tags)
[![Software License](https://img.shields.io/badge/license-BSD-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/2amigos/yii2-ckeditor-widget/master.svg?style=flat-square)](https://travis-ci.org/2amigos/yii2-ckeditor-widget)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/2amigos/yii2-ckeditor-widget.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-ckeditor-widget/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/2amigos/yii2-ckeditor-widget.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-ckeditor-widget)
[![Total Downloads](https://img.shields.io/packagist/dt/2amigos/yii2-ckeditor-widget.svg?style=flat-square)](https://packagist.org/packages/2amigos/yii2-ckeditor-widget)
Renders a [CKEditor WYSIWYG text editor plugin](http://www.ckeditor.com) widget.
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
composer require 2amigos/yii2-ckeditor-widget
```
or add
```json
"2amigos/yii2-ckeditor-widget" : "2.0"
```
to the require section of your application's `composer.json` file.
Skins & Plugins
---------------
This widget works with default's `dev-full/stable` branch of CKEditor, with a set of plugins and skins. If you wish to
configure a different skins or plugins that the one proposed, you will have to download them separately and configure
the widget's `clientOptions` attribute accordingly.
Usage
-----
The library comes with two widgets: `CKEditor` and `CKEditorInline`. One is for classic edition and the other for inline
editing respectively.
Using a model with a basic preset:
```
use dosamigos\ckeditor\CKEditor;
<?= $form->field($model, 'text')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'basic'
]) ?>
```
Using inline edition with basic preset:
```
use dosamigos\ckeditor\CKEditorInline;
<?php CKEditorInline::begin(['preset' => 'basic']);?>
This text can be edited now :)
<?php CKEditorInline::end();?>
```
How to add custom plugins
-------------------------
This is the way to add custom plugins to the editor. Since version 2.0 we are working with the packagist version of the
CKEditor library, therefore we are required to use its configuration API in order to add external plugins.
Lets add the popular [Code Editor Plugin](http://ckeditor.com/addon/pbckcode) for example. This plugin would allow us to
add a button to our editor's toolbar so we can add code to the content we are editing.
Assuming you have downloaded the plugin and added to the root directory of your Yii2 site. I have it this way:
<pre>
+ frontend
+ -- web
+ -- pbckcode
</pre>
We can now add it to our CKEditor widget. For this example I am using `CKEditorInline` widget. One thing you notice on
this example is that we do not use the preset attribute; this is highly important as we want to add a customized toolbar to our
widget. No more talking, here is the code:
```php
<?php
use dosamigos\ckeditor\CKEditorInline;
// First we need to tell CKEDITOR variable where is our external plufin
$this->registerJs("CKEDITOR.plugins.addExternal('pbckcode', '/pbckcode/plugin.js', '');");
// ...
// Using the plugin
<?php CKEditorInline::begin(['preset' => 'custom', 'clientOptions' => [
'extraPlugins' => 'pbckcode',
'toolbarGroups' => [
['name' => 'undo'],
['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
['name' => 'colors'],
['name' => 'links', 'groups' => ['links', 'insert']],
['name' => 'others', 'groups' => ['others', 'about']],
['name' => 'pbckcode'] // <--- OUR NEW PLUGIN YAY!
]
]]) ?>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
</p>
<?php CKEditorInline::end() ?>
```
About extra assets
------------------
You maybe wonder why there is file `dosamigos-ckeditor.widget.js`. The reason is that due to the way Yii2 works with
forms and Cross-Site Request Forgery (csrf). CKEditor does not trigger the on change event nor collects the CSRF token
when using file uploads.
The asset tackles both issues.
Testing
-------
To test the extension, is better to clone this repository on your computer. After, go to the extensions folder and do
the following (assuming you have `composer` installed on your computer):
```bash
$ composer install --no-interaction --prefer-source --dev
```
Once all required libraries are installed then do:
```bash
$ vendor/bin/phpunit
```
Further Information
-------------------
Please, check the [CKEditor plugin site](http://www.ckeditor.com) documentation for further information about its configuration options.
Contributing
------------
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Credits
-------
- [Antonio Ramirez](https://github.com/tonydspaniard)
- [All Contributors](../../contributors)
License
-------
The BSD License (BSD). Please see [License File](LICENSE.md) for more information.
> [![2amigOS!](http://www.gravatar.com/avatar/55363394d72945ff7ed312556ec041e0.png)](http://www.2amigos.us)
<i>Web development has never been so fun!</i>
[www.2amigos.us](http://www.2amigos.us)
{
"name": "knight/yii2-ckeditor-widget",
"description": "CKEditor widget for Yii2.",
"type": "yii2-extension",
"keywords": [
"2amigos",
"yii",
"yii2",
"yii 2",
"extension",
"widget",
"ckeditor"
],
"homepage": "http://yiiwheels.com/extension/ckeditor-widget",
"license": "BSD-3-Clause",
"authors": [
{
"name": "2amigOS! Consulting Group",
"email": "hola@2amigos.us",
"homepage": "http://2amigos.us",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/2amigos/yii2-ckeditor-widget/issues",
"source": "https://github.com/2amigos/yii2-ckeditor-widget"
},
"require": {
"yiisoft/yii2": "^2.0",
"ckeditor/ckeditor": "dev-full/stable"
},
"require-dev": {
"phpunit/phpunit": "4.*"
},
"autoload": {
"psr-4": {
"dosamigos\\ckeditor\\": "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"asset-installer-paths": {
"bower-asset-library": "vendor/bower"
}
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* CKEditor renders a CKEditor js plugin for classic editing.
* @see http://docs.ckeditor.com/
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
class CKEditor extends InputWidget
{
use CKEditorTrait;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initOptions();
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextarea($this->model, $this->attribute, $this->options);
} else {
echo Html::textarea($this->name, $this->value, $this->options);
}
$this->registerPlugin();
}
/**
* Registers CKEditor plugin
* @codeCoverageIgnore
*/
protected function registerPlugin()
{
$js = [];
$view = $this->getView();
CKEditorWidgetAsset::register($view);
$id = $this->options['id'];
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '{}';
$js[] = "CKEDITOR.replace('$id', $options);";
$js[] = "dosamigos.ckEditorWidget.registerOnChangeHandler('$id');";
if (isset($this->clientOptions['filebrowserUploadUrl']) || isset($this->clientOptions['filebrowserImageUploadUrl'])) {
$js[] = "dosamigos.ckEditorWidget.registerCsrfImageUploadHandler();";
}
$view->registerJs(implode("\n", $js));
}
}
<?php
/**
* @copyright Copyright (c) 2013-16 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\web\AssetBundle;
/**
* CKEditorAsset
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
class CKEditorAsset extends AssetBundle
{
public $sourcePath = '@vendor/ckeditor/ckeditor/';
public $js = [
'ckeditor.js',
'adapters/jquery.js'
];
public $depends = [
'yii\web\YiiAsset',
'yii\web\JqueryAsset'
];
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* CKEditorInline renders a CKEditor js plugin for inline editing.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
class CKEditorInline extends Widget
{
use CKEditorTrait;
/**
* @var array the HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var string
*/
public $tag = 'div';
/**
* @var bool disables creating the inline editor automatically for elements with contenteditable attribute
* set to the true. Defaults to true.
*/
public $disableAutoInline = true;
/**
* @inheritdoc
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
$this->options['contenteditable'] = 'true';
parent::init();
$this->initOptions();
echo Html::beginTag($this->tag, $this->options);
}
/**
* @inheritdoc
*/
public function run()
{
echo Html::endTag($this->tag);
$this->registerPlugin();
}
/**
* Registers CKEditor plugin
* @codeCoverageIgnore
*/
protected function registerPlugin()
{
$js = [];
$view = $this->getView();
CKEditorAsset::register($view);
$id = $this->options['id'];
$options = $this->clientOptions !== false && !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '{}';
if ($this->disableAutoInline) {
$js[] = "CKEDITOR.disableAutoInline = true;";
}
$js[] = "CKEDITOR.inline('$id', $options);";
if (isset($this->clientOptions['filebrowserUploadUrl'])) {
$js[] = "dosamigos.ckEditorWidget.registerCsrfImageUploadHandler();";
}
$view->registerJs(implode("\n", $js));
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\helpers\ArrayHelper;
/**
* CKEditorTrait has common methods for both CKEditor and CKEditorInline widgets.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
trait CKEditorTrait
{
/**
* @var string the toolbar preset. It can be any of the following:
*
* - basic: will load the configuration on presets/basic.php
* - full: will load the configuration on presets/full.php
* - standard: will load the configuration on presets/standard.php
* - custom: configuration will be based on [[clientOptions]].
*
* Defaults to 'standard'. It is important to note that any configuration item of the loaded presets can be
* overrided by [[clientOptions]]
*/
public $preset = 'standard';
/**
* @var array the options for the CKEditor 4 JS plugin.
* Please refer to the CKEditor 4 plugin Web page for possible options.
* @see http://docs.ckeditor.com/#!/guide/dev_installation
*/
public $clientOptions = [];
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
$options = [];
switch ($this->preset) {
case 'custom':
$preset = null;
break;
case 'basic':
case 'full':
case 'standard':
$preset = 'presets/' . $this->preset . '.php';
break;
default:
$preset = 'presets/standard.php';
}
if ($preset !== null) {
$options = require($preset);
}
$this->clientOptions = ArrayHelper::merge($options, $this->clientOptions);
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\ckeditor;
use yii\web\AssetBundle;
/**
* CKEditorWidgetAsset
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\ckeditor
*/
class CKEditorWidgetAsset extends AssetBundle
{
public $sourcePath = '@vendor/2amigos/yii2-ckeditor-widget/src/assets/';
public $depends = [
'dosamigos\ckeditor\CKEditorAsset'
];
public $js = [
'dosamigos-ckeditor.widget.js'
];
}
/**
* @copyright Copyright (c) 2012-2015 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
if (typeof dosamigos == "undefined" || !dosamigos) {
var dosamigos = {};
}
dosamigos.ckEditorWidget = (function ($) {
var pub = {
registerOnChangeHandler: function (id) {
CKEDITOR && CKEDITOR.instances[id] && CKEDITOR.instances[id].on('change', function () {
CKEDITOR.instances[id].updateElement();
$('#' + id).trigger('change');
return false;
});
},
registerCsrfImageUploadHandler: function () {
yii & $(document).off('click', '.cke_dialog_tabs a[id^="cke_Upload_"]').on('click', '.cke_dialog_tabs a[id^="cke_Upload_"]', function () {
var $forms = $('.cke_dialog_ui_input_file iframe').contents().find('form');
var csrfName = yii.getCsrfParam();
$forms.each(function () {
if (!$(this).find('input[name=' + csrfName + ']').length) {
var csrfTokenInput = $('<input/>').attr({
'type': 'hidden',
'name': csrfName
}).val(yii.getCsrfToken());
$(this).append(csrfTokenInput);
}
});
});
}
};
return pub;
})(jQuery);
<?php
/**
*
* basic preset returns the basic toolbar configuration set for CKEditor.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
*/
return [
'height' => 200,
'toolbarGroups' => [
['name' => 'undo'],
['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
['name' => 'colors'],
['name' => 'links', 'groups' => ['links', 'insert']],
['name' => 'others', 'groups' => ['others', 'about']],
],
'removeButtons' => 'Subscript,Superscript,Flash,Table,HorizontalRule,Smiley,SpecialChar,PageBreak,Iframe',
'removePlugins' => 'elementspath',
'resize_enabled' => false
];
<?php
/**
*
* full preset returns the full toolbar configuration set for CKEditor.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
*/
return [
'height' => 400,
'toolbarGroups' => [
['name' => 'document', 'groups' => ['mode', 'document', 'doctools']],
['name' => 'clipboard', 'groups' => ['clipboard', 'undo']],
['name' => 'editing', 'groups' => [ 'find', 'selection', 'spellchecker']],
['name' => 'forms'],
'/',
['name' => 'basicstyles', 'groups' => ['basicstyles', 'colors','cleanup']],
['name' => 'paragraph', 'groups' => [ 'list', 'indent', 'blocks', 'align', 'bidi' ]],
['name' => 'links'],
['name' => 'insert'],
'/',
['name' => 'styles'],
['name' => 'blocks'],
['name' => 'colors'],
['name' => 'tools'],
['name' => 'others'],
],
];
<?php
/**
*
* standard preset returns the basic toolbar configuration set for CKEditor.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
*/
return [
'height' => 300,
'toolbarGroups' => [
['name' => 'clipboard', 'groups' => ['mode', 'undo', 'selection', 'clipboard', 'doctools']],
['name' => 'editing', 'groups' => ['tools', 'about']],
'/',
['name' => 'paragraph', 'groups' => ['templates', 'list', 'indent', 'align']],
['name' => 'insert'],
'/',
['name' => 'basicstyles', 'groups' => ['basicstyles', 'cleanup']],
['name' => 'colors'],
['name' => 'links'],
['name' => 'others'],
],
'removeButtons' => 'Smiley,Iframe'
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment