Yii wangeditor Widget

wangeditor是一个轻量编辑器,地址

http://www.wangeditor.com/

首先我使用了yii2-bootstrap,所以widget继承自yii\bootstrap\InputWidget

wangeditor怎么使用官网很详细,怎么集成php也很多案例,直接上代码:

use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\InputWidget;
use yii\web\AssetBundle;

class WangEditor extends InputWidget {

    public $pluginOptions = [];
 
    public function init()
    {
        parent::init();
    }
 
    public function run()
    {
        $view = $this->getView();

        $classOptions = [
            'class' => 'hide'
        ];
        
        $this->options = array_merge($this->options, $classOptions);
        $input =  Html::textarea($this->name, $this->value, $this->options);
        if ($this->hasModel()) {
            $input =  Html::activeTextarea($this->model, $this->attribute, $this->options);
        }
        
        $inputTemplate = "<div id='content'></div><div id='contentText'>" . $input . "</div>";
        echo $inputTemplate;
    
        WangEditorAsset::register($view);


        $js = "var E = window.wangEditor;";
        $js .= "var editor = new E('#content');";
        $js .= "var richContent = jQuery('#contentText textarea');";
        $js .= "editor.customConfig.onchange = function (html) { richContent.val(html); };";
        $js .= "editor.customConfig.uploadImgServer = '/uploads';";
        $js .= "editor.customConfig.menus = ['head', 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'foreColor', 'backColor', 'link', 'list', 'justify', 'quote', 'undo', 'redo'];";
        $js .= "editor.create();";
        $js .= "editor.txt.html(richContent.val());";
       
        $view->registerJs($js);
    }

}

class WangEditorAsset extends AssetBundle
{
    public $js = [
        'plugins/richeditor/wangEditor.min.js',
    ];

    public function init()
    {
        parent::init();
    }
}

使用

$form->field($model, 'content')->widget(common\widgets\WangEditor::classname());

 

修改:

使用上传图片:

namespace common\widgets;

use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\InputWidget;
use yii\web\AssetBundle;

class WangEditor extends InputWidget {

    public $pluginOptions = [];
 
    public function init()
    {
        parent::init();
    }
 
    public function run()
    {
        $view = $this->getView();

        $classOptions = [
            'class' => 'hide'
        ];

        $this->options = array_merge($this->options, $classOptions);
        $input =  Html::textarea($this->name, $this->value, $this->options);
        if ($this->hasModel()) {
            $input =  Html::activeTextarea($this->model, $this->attribute, $this->options);
        }
        
        $inputTemplate = "<div id='content'></div><div id='contentText'>" . $input . "</div>";
        echo $inputTemplate;
    
        WangEditorAsset::register($view);

        $css = '.w-e-text-container{ height: 600px !important; }';
        $view->registerCss($css);


        /*$js = "var E = window.wangEditor;";
        $js .= "var editor = new E('#content');";
        $js .= "var richContent = jQuery('#contentText textarea');";
        $js .= "editor.customConfig.onchange = function (html) { richContent.val(html); };";
        //$js .= "editor.customConfig.uploadImgServer = '/uploads';";
        $js .= "editor.customConfig.menus = ['head', 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'foreColor', 'backColor', 'link', 'image', 'list', 'justify', 'quote', 'undo', 'redo'];";
        $js .= "editor.create();";
        $js .= "editor.txt.html(richContent.val());";
        $js .= "editor.config.customUploadInit = wanguploader.init(); ";
       
        $view->registerJs($js);*/
    }

}

class WangEditorAsset extends AssetBundle
{
    public $js = [
        'plugins/richeditor/wangEditor.min.js',
        'js/editor.js',
    ];

    public function init()
    {
        parent::init();
    }
}

 

js


$(document).ready(function() {
    var baseBackend = window.location.protocol + "//" +window.location.host;

    var E = window.wangEditor;
    var editor = new E('#content');
    var richContent = jQuery('#contentText textarea');
    editor.customConfig.onchange = function (html) { richContent.val(html); };
    //editor.customConfig.uploadImgServer = '/uploads';
    editor.customConfig.zIndex = 99;
    editor.customConfig.menus = ['head', 'bold', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'foreColor', 'backColor', 'link', 'image', 'list', 'justify', 'quote', 'undo', 'redo'];
    editor.create();
    editor.txt.html(richContent.val());
   
       

    // 图片上传
    var token = $('meta[name="csrf-token"]').attr('content');
    var wanguploader = new plupload.Uploader({
        runtimes: 'html5,flash,silverlight,html4',
        browse_button: editor.imgMenuId,
        url: baseBackend + '/uploader/uploader',
        file_data_name: 'ysx_media',
        multi_selection: true,
        multipart_params: { 'updir': 'detail', '_token': token },
        auto_start: true,
        flash_swf_url: '../plupload/js/Moxie.swf',
        silverlight_xap_url: '../plupload/js/Moxie.xap',

        filters: {
            mime_types: [ //只允许上传图片和zip,rar文件
                { title: "Image files", extensions: "jpg,jpeg,gif,png,bmp" },
            ],
            max_file_size: '10mb', //最大只能上传10mb的文件
            prevent_duplicates: false //不允许选取重复文件
        },

        init: {
            PostInit: function() {
                
            },
            FilesAdded: function(up) {
                up.start(); //选择完后直接上传
            },

            FileUploaded: function(up, file, info) {
                if (info.status == 200) {
                    var fileType = file.type;
                    // 获取返回德数据
                    var fileInfos = $.parseJSON(info.response);
                    if(fileInfos.error == 0) {
                        var fileAttachementId = fileInfos.attachement_id
                        var fileUrl = fileInfos.url;
                     
                        editor.cmd.do('insertHtml', '<img src="' + fileUrl + '" style="width: auto; max-width:100%;"/>');
                    } else {
                        alert(fileInfos.msg);
                    }
                } else {
                    alert(info.response);
                }
            },

            Error: function(up, err) {
                if (err.code == -600) {
                    alert("\n选择的文件太大了,可以根据应用情况,在upload.js 设置一下上传的最大大小");
                } else if (err.code == -601) {
                    alert("\n选择的文件后缀不对,可以根据应用情况,在upload.js进行设置可允许的上传文件类型");
                } else if (err.code == -602) {
                    alert("\n这个文件已经上传过一遍了");
                } else {
                    alert("\nError xml:" + err.response);
                }
            }
        }
    })
    
    editor.config.customUploadInit = wanguploader.init(); 
})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值