分享推荐一款好用的TP富文本编辑器-CKEditor

 4730

本篇文章给大家推荐一款炒鸡好用的Thinkphp富文本编辑器--CKEditor,下面给大家介绍一下使用CKEditor的方法,希望对大家有所帮助!


分享推荐一款好用的TP富文本编辑器-CKEditor


最近一直在做Thinkphp后端开发,之前都是使用layui的富文本编辑器,layui的优点是简单易用,但缺点也比较明显,就是编辑器功能比较少,无意中发现别人的项目里使用的是CKEditor富文本编辑器,感觉还阔以!下面让我们一起来学习如何使用CKEditor。

Ckeditor4下载地址(本教程选择的是CKEditor 4.16版本):

https://ckeditor.com/ckeditor-4/download/


分享推荐一款好用的TP富文本编辑器-CKEditor


一、在页面中引入ckeditor核心文件ckeditor.js

  1. <script type="text/javascript" src="ckeditor/ckeditor.js"></script>


二、在使用编辑器的地方插入HTML控件

  1. <textarea  id="content" name="content" cols="30" rows="2"></textarea>


三、将相应的控件替换成编辑器代码

  1. <script type="text/javascript">
  2. var editor;
  3. window.onload = function()
  4. {
  5.     editor = CKEDITOR.replace( 'content', {
  6.         filebrowserImageUploadUrl : '{:url("@admin/article/uploadPic")}',//上传图片的后端URL地址
  7.         image_previewText : '&nbsp;'///去掉图片上传预览区域显示的文字
  8.     });
  9. };
  10. </script>


四、开启上传功能(上传功能被隐藏了,所以需要开启)

在ckeditor/plugins/image/dialogs/image.js文件中,搜索:id:"Upload",hidden:!0,把 !0改成false


五、thinkphp后端上传文件的方法

4.10版本之后,官方文档要求图片上传成功后,返回json格式,示例如下:

上传成功返回:

  1. {
  2.     "uploaded": 1,
  3.     "fileName": "demo.jpg",
  4.     "url": "/files/demo.jpg"
  5. }
  6.  
  7. {
  8.     "uploaded": 1,
  9.     "fileName": "test.jpg",
  10.     "url": "/files/test.jpg",
  11.     "error": {
  12.         "message": "A file with the same name already exists. The uploaded file was renamed to \"test.jpg\"."
  13.     }
  14. }

上传失败返回:

  1. {
  2.     "uploaded": 0,
  3.     "error": {
  4.         "message": "The file is too big."
  5.     }
  6. }


后端上传图片的代码:

  1. /**
  2.  * @name='上传图片'    
  3.  */
  4. public function uploadPic()
  5. {
  6.     //注明:ckeditor是使用ajax上传图片,而不是用表单提交,因此不能使用request()->file()接收图片,只能用$_FILES
  7.     $name = $_FILES['upload']['name']; 
  8.     $size = $_FILES['upload']['size'];
  9.     if($size  > 1024*2*1000){
  10.         $arr= array(
  11.             "uploaded" => 0,
  12.             "error"    => "上传的图片大小不能超过2M"
  13.         );
  14.         exit(json_encode($arr));
  15.     }
  16.     $extension = pathInfo($name,PATHINFO_EXTENSION);
  17.     $types = array("jpg","bmp","gif","png");        
  18.     if(in_array($extension,$types)){ 
  19.         //以日期为文件夹名,如public/uploads/20210327/
  20.         $dateFolder = date("Ymd",time());
  21.         $path = ROOT_PATH . 'public/uploads/'.$dateFolder.DS;
  22.         if(!file_exists($path)){
  23.             mkdir($path,0777,true);
  24.         }       
  25.         $img_name  = str_replace('.','',uniqid("",TRUE)).".".$extension; //图片名称
  26.         $save_path = $path.$img_name; //保存路径 
  27.         $img_path  = '/uploads/'.$dateFolder.DS.$img_name; //图片路径 
  28.         move_uploaded_file($_FILES['upload']['tmp_name'],$save_path);   
  29.         $arr= array(
  30.             "uploaded" => 1,
  31.             "fileName" => $img_name,
  32.             "url"      => $img_path
  33.         );
  34.     }else{ 
  35.         $arr= array(
  36.             "uploaded" => 0,
  37.             "error"    => "图片格式不正确(只能上传.jpg/.gif/.bmp/.png类型的文件)"
  38.         );       
  39.     } 
  40.     return json_encode($arr);
  41. }


六、js里获取ckeditor里的内容

  1. <script type="text/javascript">
  2. var editor;
  3. $(function() {
  4.     editor = CKEDITOR.replace('content');
  5. })
  6. editor.document.getBody().getText();//取得纯文本
  7. editor.document.getBody().getHtml();//取得html文本
  8. </script>


七、使用颜色插件

1、需要下载三个插件(缺一不可),下载地址:

https://ckeditor.com/cke4/addon/colorbutton

https://ckeditor.com/cke4/addon/floatpanel

https://ckeditor.com/cke4/addon/panelbutton

2、下载好的插件解压到ckeditor\plugins目录里

3、加载插件

方式一:在ckeditor/config.js文件中,添加插件的配置,如下:

  1. CKEDITOR.editorConfig = function( config ) {
  2.  
  3.     ...省略前面的代码
  4.  
  5.     //加载插件
  6.     config.extraPlugins = 'colorbutton,panelbutton,floatpanel';
  7. }

方式二:在js里初始化editor时,添加插件的配置

  1. <script type="text/javascript">
  2. var editor;
  3. window.onload = function()
  4. {
  5.     editor = CKEDITOR.replace( 'content', {
  6.         filebrowserImageUploadUrl : '{:url("@admin/article/uploadPic")}',//上传图片的后端URL地址
  7.         image_previewText : '&nbsp;',///去掉图片上传预览区域显示的文字
  8.         extraPlugins: 'colorbutton',//使用颜色插件
  9.     });
  10. };
  11. </script>


八、自定义工具栏配置

在ckeditor/config.js文件中设置

  1. CKEDITOR.editorConfig = function( config ) {
  2.     //工具栏设置
  3.     config.toolbar = 'MyToolbar';
  4.     config.toolbar_Full = [
  5.         { name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
  6.         { name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
  7.         { name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
  8.         { name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 
  9.             'HiddenField' ] },
  10.         '/',
  11.         { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
  12.         { name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv',
  13.         '-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
  14.         { name: 'links', items : [ 'Link','Unlink','Anchor' ] },
  15.         { name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
  16.         '/',
  17.         { name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
  18.         { name: 'colors', items : [ 'TextColor','BGColor' ] },
  19.         { name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
  20.     ]; 
  21.     config.toolbar_Basic = [
  22.         ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
  23.     ];
  24.     //自定义
  25.     config.toolbar_MyToolbar =[
  26.         //加粗    斜体,    下划线    穿过线    下标字        上标字
  27.         ['Bold','Italic','Underline','Strike','Subscript','Superscript'],
  28.         // 数字列表        实体列表         减小缩进  增大缩进
  29.         ['NumberedList','BulletedList','-','Outdent','Indent'],
  30.         //   左对齐        居中对齐        右对齐        两端对齐
  31.         ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
  32.         //超链接  取消超链接 锚点
  33.         ['Link','Unlink','Anchor'],
  34.         //图片    flash    表格       水平线        表情     特殊字符      分页符
  35.         ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
  36.         '/',
  37.         // 样式     格式    字体   字体大小
  38.         ['Styles','Format','Font','FontSize'],
  39.         //文本颜色   背景颜色
  40.         ['TextColor','BGColor'],
  41.         //全屏         显示区块         源码
  42.         ['Maximize', 'ShowBlocks','-','Source']
  43.     ],
  44.     config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre';
  45.     config.removeButtons = 'Underline,Subscript,Superscript';
  46.     config.removeDialogTabs = 'image:advanced;link:advanced';
  47.     //加载插件
  48.     config.extraPlugins = 'colorbutton,panelbutton,floatpanel'; 
  49. };


本文网址:https://www.zztuku.com/index.php/detail-9734.html
站长图库 - 分享推荐一款好用的TP富文本编辑器-CKEditor
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐