davidxu/yii2-imperavi-widget

The imperavi redactor widget for Yii 2 framework with qiniu upload function added.

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 99

Open Issues: 0

Language:JavaScript

Type:yii2-extension

2.1.3 2021-02-11 07:06 UTC

README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Imperavi Redactor Widget is a wrapper for Imperavi Redactor 10.2.5, a high quality WYSIWYG editor.

Note that Imperavi Redactor itself is a proprietary commercial copyrighted software but since Yii community bought OEM license you can use it for free with Yii.

Install

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require --prefer-dist davidxu/yii2-imperavi-widget "*"

or add

"davidxu/yii2-imperavi-widget": "*"

to the require section of your composer.json file.

Usage

If file/image info stores in DB or use qiniu bucket, please implement migrations below first:

yii migrate/up --migration-path @davidxu/imperavi/migrations/

Once the extension is installed, simply use it in your code:

Like a widget

echo \davidxu\imperavi\Redactor::widget([
    'name' => 'redactor',
    'settings' => [
        'lang' => 'zh_cn',
        'imageUpload' => '/api/v1/redactor/upload',
        'minHeight' => 200,
        'plugins' => [
            'clips',
            'fullscreen',
        ],
        'clips' => [
            ['Lorem ipsum...', 'Lorem...'],
            ['red', '<span class="label-red">red</span>'],
            ['green', '<span class="label-green">green</span>'],
            ['blue', '<span class="label-blue">blue</span>'],
        ],
    ],
]);

Like an ActiveForm widget

use davidxu\imperavi\Redactor;

echo $form->field($model, 'content')->widget(Redactor::class, [
    'settings' => [
        'lang' => 'zh_cn',
        'imageUpload' => '/api/v1/redactor/upload',
        'minHeight' => 200,
        'plugins' => [
            'clips',
            'fullscreen',
        ],
        'clips' => [
            ['Lorem ipsum...', 'Lorem...'],
            ['red', '<span class="label-red">red</span>'],
            ['green', '<span class="label-green">green</span>'],
            ['blue', '<span class="label-blue">blue</span>'],
        ],
    ],
]);

Like a widget for a predefined textarea

echo \davidxu\imperavi\Redactor::widget([
    'selector' => '#my-textarea-id',
    'settings' => [
        'lang' => 'zh_cn',
        'imageUpload' => '/api/v1/redactor/upload',
        'minHeight' => 200,
        'plugins' => [
            'clips',
            'fullscreen',
        ],
        'clips' => [
            ['Lorem ipsum...', 'Lorem...'],
            ['red', '<span class="label-red">red</span>'],
            ['green', '<span class="label-green">green</span>'],
            ['blue', '<span class="label-blue">blue</span>'],
        ],
    ],
]);

Upload image

// DefaultController.php
public function actions()
{
    return [
        'upload-local' => [
            'class' => 'davidxu\imperavi\actions\LocalUploadFileAction',
            'dnsBaseUrl' => 'http://my-site.com/', // Domain name or uri where files are stored, filelink will be dnsBaseUrl + url 
            'url' => 'images/', // Directory URL address, where files are stored.
            'path' => '@alias/to/my/path', // Or absolute path to directory where files are stored.
            'modelClass' => Attachment::class, // Please use migration first, if file/image info stored in DB
            'storeInDB' => true, // Default true
        ],
        'upload-qiniu' => [
            'class' => 'davidxu\imperavi\actions\QiniuUploadFileAction',
            'dnsBaseUrl' => 'http://my-site.com/', // Qiniu DNS domain name, filelink will be dnsBaseUrl + url 
            'url' => 'images/', // Qiniu key prefix, key will be url + filename.
            'modelClass' => Attachment::class, // Please use migration first
        ],
    ];
}

// View.php
echo \davidxu\imperavi\Redactor::widget([
    'selector' => '#my-textarea-id',
    'settings' => [
        'lang' => 'zh_cn',
        'minHeight' => 200,
        'imageUpload' => Url::to(['/default/image-upload']),
        'plugins' => [
            'imagemanager',
        ],
    ],
]);

Add custom plugins

echo \davidxu\imperavi\Redactor::widget([
    'selector' => '#my-textarea-id',
    'settings' => [
        'lang' => 'zh_cn',
        'minHeight' => 200,
        'plugins' => [
            'clips',
            'fullscreen'
        ]
    ],
    'plugins' => [
        'my-custom-plugin' => 'app\assets\MyPluginBundle',
    ],
]);

Testing

$ phpunit

Further Information

Please, check the Imperavi Redactor v10 documentation for further information about its configuration options.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The BSD License (BSD). Please see License File for more information.

Upgrade guide

Please check the UPGRADE GUIDE for details.