比较喜欢用kindeditor,YII上的版本比较旧,所以自己重新整了个扩展
先在protected\extensions下创建KEditor文件夹用来放文件,keSource里放kindeditor的源文件,然后建三个类KEditor、KEditorManage和KEditorUpload,KEditor是扩展的主文件,KEditorManage是用来浏览服务器文件的,KEditorUpload是用来示例接收上传文件的,
KEditor代码
- <?php
- class KEditor extends CWidget{
- /*
- * TEXTAREA输入框的属性,保证js调用KE失败时,文本框的样式。
- */
- public $textareaOptions=array();
- /*
- * 编辑器属性集。
- */
- public $properties=array();
- /*
- * TEXTAREA输入框的name,必须设置。
- * 数据类型:String
- */
- public $name;
- /*
- * TEXTAREA的id,可为空
- */
- public $id;
- public $model;
- public $baseUrl;
- public static function getUploadPath(){
- $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';
- if(isset(Yii::app()->params->uploadPath)){
- return Yii::getPathOfAlias('webroot').str_replace(
- '/',DIRECTORY_SEPARATOR,
- Yii::app()->params->
- uploadPath);
- }
- return Yii::app()->getAssetmanager()
- ->getPublishedPath($dir).DIRECTORY_SEPARATOR.'upload';
- }
- public static function getUploadUrl(){
- $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';
- if(isset(Yii::app()->params->uploadPath)){
- return Yii::app()->baseUrl.Yii::app()->params->uploadPath;
- }
- return Yii::app()->getAssetManager()->publish($dir).'/upload';
- }
- public function init(){
- if($this->name===null)
- throw new CException(Yii::t('zii','The id property cannot be empty.'));
- $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'keSource';
- $this->baseUrl=Yii::app()->getAssetManager()->publish($dir);
- $cs=Yii::app()->getClientScript();
- $cs->registerCssFile($this->baseUrl.'/themes/default/default.css');
- if(YII_DEBUG) $cs->registerScriptFile($this->baseUrl.'/kindeditor.js');
- else $cs->registerScriptFile($this->baseUrl.'/kindeditor-min.js');
- }
- public function run(){
- $cs=Yii::app()->getClientScript();
- $textAreaOptions=$this->gettextareaOptions();
- $textAreaOptions['name']=CHtml::resolveName($this->model,$this->name);
- $this->id=$textAreaOptions['id']=CHtml::getIdByName($textAreaOptions['name']);
- echo CHtml::activeTextArea($this->model,$this->name,$textAreaOptions);
- $properties_string = CJavaScript::encode($this->getKeProperties());
- $js=<<<EOF
- KindEditor.ready(function(K) {
- var editor_$this->id = K.create('#$this->id',
- $properties_string
- );
- });
- EOF;
- $cs->registerScript('KE'.$this->name,$js,CClientScript::POS_HEAD);
- }
- public function gettextareaOptions(){
- //允许获取的属性
- $allowParams=array('rows','cols','style');
- //准备返回的属性数组
- $params=array();
- foreach($allowParams as $key){
- if(isset($this->textareaOptions[$key]))
- $params[$key]=$this->textareaOptions[$key];
- }
- $params['name']=$params['id']=$this->name;
- return $params;
- }
- public function getKeProperties(){
- $properties_key=array(
- 'width',
- 'height',
- 'minWidth',
- 'minHeight',
- 'items',
- 'noDisableItems',
- 'filterMode',
- 'htmlTags',
- 'wellFormatMode',
- 'resizeType',
- 'themeType',
- 'langType',
- 'designMode',
- 'fullscreenMode',
- 'basePath',
- 'themesPath',
- 'pluginsPath',
- 'langPath',
- 'minChangeSize',
- 'urlType',
- 'newlineTag',
- 'pasteType',
- 'dialogAlignType',
- 'shadowMode',
- 'useContextmenu',
- 'syncType',
- 'indentChar',