Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeMirror integration for vova07 Yii2 Redactor widget #169

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

NickGoodwind
Copy link

CodeMirror integration for Yii2 Redactor widget

In this fork from the original vova07 Imperavi widget for Yii2, we created a new implementation to allow the correct integration with the CodeMirror API. In this fork, two files were changed, including the original redactor.js v10 and the Widget class. Also, a new CodemirroAsset asset bundle file was created to internally manage the codemirror assets for the Yii application. Finally, the CodeMirror assets were included inside the assets folder.

Here are the main changes:

Widget

class Widget extends BaseWidget
{
    ...
    protected function registerClientScripts()
    {
        ...
        if (isset($this->settings['codemirror']) && $this->settings['codemirror'] !== false) {
            CodemirrorAsset::register($view);
        }
        ...
    }
}

CodemirrorAsset

namespace vova07\imperavi;

use yii\web\AssetBundle;

/**
 * Codemirror asset bundle.
 *
 * @author NickGoodwind <nickgoodwind@gmail.com>
 */
class CodemirrorAsset extends AssetBundle
{
    public $sourcePath = '@vova07/imperavi/assets';
    
    public $js = [
        'codemirror/codemirror.js',
        'codemirror/mode/htmlmixed/htmlmixed.js',
    ];

    public $css = [
        'codemirror/codemirror.css'
    ];

    public $depends = [
        'app\modules\admin\assets\AdminAppAsset'
    ];
}

Redactor.js - Creation of a new method to implement the CodeMirror editor instant inside the Redactor.build function and modification of the Redactor.build.run function

run: function()
    {
        this.build.createContainerBox();
        this.build.loadContent();
        this.build.loadEditor();
        this.build.addCodeMirror(); // Added line
        this.build.enableEditor();
        this.build.setCodeAndCall();
}
...
addCodeMirror: function() 
{
    if(this.opts.codemirror) {
        var $el = (this.build.isTextarea()) ? this.$element : this.$textarea;
        console.log($el);
        var editor = CodeMirror.fromTextArea($el[0]);
    }
}

NickGoodwind and others added 20 commits May 26, 2022 19:03
Redactor modification to allow the correct integration with CodeMirror
Final CodeMirror integration
Structure update to remove unnecessary parent folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant