搜尋
首頁php框架ThinkPHP分享推薦一款好用的TP富文本編輯器-CKEditor

分享推薦一款好用的TP富文本編輯器-CKEditor

Oct 25, 2021 pm 07:16 PM
ckeditorthinkphp富文本編輯器

這篇文章建議大家推薦一款炒雞好用的Thinkphp富文本編輯器--CKEditor,以下跟大家介紹一下使用CKEditor的方法,希望對大家有幫助!

分享推薦一款好用的TP富文本編輯器-CKEditor

最近一直在做Thinkphp後端開發,之前都是使用layui的富文本編輯器,layui的優點是簡單易用,但缺點也比較明顯,就是編輯器功能比較少,無意中發現別人的專案裡使用的是CKEditor富文本編輯器,感覺還闊以!下面讓我們一起來學習如何使用CKEditor。 【相關教學建議:thinkphp框架

Ckeditor4下載位址(本教學選擇的是CKEditor 4.16版本):

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

分享推薦一款好用的TP富文本編輯器-CKEditor

#一、在頁面中引入ckeditor核心檔ckeditor.js 

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

二、在使用編輯器的地方插入HTML控制項

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

三、將對應的控制項替換成編輯器程式碼

<script type="text/javascript">
var editor;
window.onload = function()
{
	editor = CKEDITOR.replace( &#39;content&#39;, {
            filebrowserImageUploadUrl : &#39;{:url("@admin/article/uploadPic")}&#39;,//上传图片的后端URL地址
            image_previewText : &#39; &#39;///去掉图片上传预览区域显示的文字
    });
};
</script>

四、開啟上傳功能(上傳功能被隱藏了,所以需要開啟)

在ckeditor/plugins/image/dialogs/image.js檔案中,搜尋:id:"Upload",hidden:!0,把!0改成false

五、thinkphp後端上傳文件的方法

4.10版本之後,官方文件要求圖片上傳成功後,返回json格式,範例如下:

上傳成功返回:

{
    "uploaded": 1,
    "fileName": "demo.jpg",
    "url": "/files/demo.jpg"
}

{
    "uploaded": 1,
    "fileName": "test.jpg",
    "url": "/files/test.jpg",
    "error": {
        "message": "A file with the same name already exists. The uploaded file was renamed to \"test.jpg\"."
    }
}

上傳失敗返回:

{
    "uploaded": 0,
    "error": {
        "message": "The file is too big."
    }
}

#後端上傳圖片的程式碼:

/**
    * @name=&#39;上传图片&#39;    
    */
    public function uploadPic()
    {
		//注明:ckeditor是使用ajax上传图片,而不是用表单提交,因此不能使用request()->file()接收图片,只能用$_FILES
		$name = $_FILES[&#39;upload&#39;][&#39;name&#39;]; 
		$size = $_FILES[&#39;upload&#39;][&#39;size&#39;];
		if($size  > 1024*2*1000){
			$arr= array(
				"uploaded" => 0,
				"error"    => "上传的图片大小不能超过2M"
			);
			exit(json_encode($arr));
		}
		$extension = pathInfo($name,PATHINFO_EXTENSION);
		$types = array("jpg","bmp","gif","png");		
		if(in_array($extension,$types)){ 
			//以日期为文件夹名,如public/uploads/20210327/
			$dateFolder = date("Ymd",time());
			$path = ROOT_PATH . &#39;public/uploads/&#39;.$dateFolder.DS;
			if(!file_exists($path)){
				mkdir($path,0777,true);
			}		
			$img_name  = str_replace(&#39;.&#39;,&#39;&#39;,uniqid("",TRUE)).".".$extension; //图片名称
			$save_path = $path.$img_name; //保存路径 
			$img_path  = &#39;/uploads/&#39;.$dateFolder.DS.$img_name; //图片路径 
			move_uploaded_file($_FILES[&#39;upload&#39;][&#39;tmp_name&#39;],$save_path);   
			$arr= array(
				"uploaded" => 1,
				"fileName" => $img_name,
				"url"      => $img_path
			);
		}else{ 
			$arr= array(
				"uploaded" => 0,
				"error"    => "图片格式不正确(只能上传.jpg/.gif/.bmp/.png类型的文件)"
			);		 
		} 
		return json_encode($arr);
    }

六、js裡取得ckeditor裡的內容

<script type="text/javascript">
var editor;
$(function() {
	editor = CKEDITOR.replace(&#39;content&#39;);
})
editor.document.getBody().getText();//取得纯文本
editor.document.getBody().getHtml();//取得html文本
</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檔案中,加入外掛程式的配置,如下:

CKEDITOR.editorConfig = function( config ) {

    ...省略前面的代码

    //加载插件
    config.extraPlugins = &#39;colorbutton,panelbutton,floatpanel&#39;;
}

 方式二:在js裡初始化editor時,加入外掛程式的設定

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

八、自訂工具列配置

在ckeditor/config.js檔案中設定

CKEDITOR.editorConfig = function( config ) {
	//工具栏设置
	config.toolbar = &#39;MyToolbar&#39;;
	config.toolbar_Full = [
		{ name: &#39;document&#39;, items : [ &#39;Source&#39;,&#39;-&#39;,&#39;Save&#39;,&#39;NewPage&#39;,&#39;DocProps&#39;,&#39;Preview&#39;,&#39;Print&#39;,&#39;-&#39;,&#39;Templates&#39; ] },
		{ name: &#39;clipboard&#39;, items : [ &#39;Cut&#39;,&#39;Copy&#39;,&#39;Paste&#39;,&#39;PasteText&#39;,&#39;PasteFromWord&#39;,&#39;-&#39;,&#39;Undo&#39;,&#39;Redo&#39; ] },
		{ name: &#39;editing&#39;, items : [ &#39;Find&#39;,&#39;Replace&#39;,&#39;-&#39;,&#39;SelectAll&#39;,&#39;-&#39;,&#39;SpellChecker&#39;, &#39;Scayt&#39; ] },
		{ name: &#39;forms&#39;, items : [ &#39;Form&#39;, &#39;Checkbox&#39;, &#39;Radio&#39;, &#39;TextField&#39;, &#39;Textarea&#39;, &#39;Select&#39;, &#39;Button&#39;, &#39;ImageButton&#39;, 
			&#39;HiddenField&#39; ] },
		&#39;/&#39;,
		{ name: &#39;basicstyles&#39;, items : [ &#39;Bold&#39;,&#39;Italic&#39;,&#39;Underline&#39;,&#39;Strike&#39;,&#39;Subscript&#39;,&#39;Superscript&#39;,&#39;-&#39;,&#39;RemoveFormat&#39; ] },
		{ name: &#39;paragraph&#39;, items : [ &#39;NumberedList&#39;,&#39;BulletedList&#39;,&#39;-&#39;,&#39;Outdent&#39;,&#39;Indent&#39;,&#39;-&#39;,&#39;Blockquote&#39;,&#39;CreateDiv&#39;,
		&#39;-&#39;,&#39;JustifyLeft&#39;,&#39;JustifyCenter&#39;,&#39;JustifyRight&#39;,&#39;JustifyBlock&#39;,&#39;-&#39;,&#39;BidiLtr&#39;,&#39;BidiRtl&#39; ] },
		{ name: &#39;links&#39;, items : [ &#39;Link&#39;,&#39;Unlink&#39;,&#39;Anchor&#39; ] },
		{ name: &#39;insert&#39;, items : [ &#39;Image&#39;,&#39;Flash&#39;,&#39;Table&#39;,&#39;HorizontalRule&#39;,&#39;Smiley&#39;,&#39;SpecialChar&#39;,&#39;PageBreak&#39;,&#39;Iframe&#39; ] },
		&#39;/&#39;,
		{ name: &#39;styles&#39;, items : [ &#39;Styles&#39;,&#39;Format&#39;,&#39;Font&#39;,&#39;FontSize&#39; ] },
		{ name: &#39;colors&#39;, items : [ &#39;TextColor&#39;,&#39;BGColor&#39; ] },
		{ name: &#39;tools&#39;, items : [ &#39;Maximize&#39;, &#39;ShowBlocks&#39;,&#39;-&#39;,&#39;About&#39; ] }
	]; 
	config.toolbar_Basic = [
		[&#39;Bold&#39;, &#39;Italic&#39;, &#39;-&#39;, &#39;NumberedList&#39;, &#39;BulletedList&#39;, &#39;-&#39;, &#39;Link&#39;, &#39;Unlink&#39;,&#39;-&#39;,&#39;About&#39;]
	];
	//自定义
	config.toolbar_MyToolbar =[
        //加粗    斜体,    下划线    穿过线    下标字        上标字
        [&#39;Bold&#39;,&#39;Italic&#39;,&#39;Underline&#39;,&#39;Strike&#39;,&#39;Subscript&#39;,&#39;Superscript&#39;],
        // 数字列表        实体列表         减小缩进  增大缩进
        [&#39;NumberedList&#39;,&#39;BulletedList&#39;,&#39;-&#39;,&#39;Outdent&#39;,&#39;Indent&#39;],
        //   左对齐        居中对齐        右对齐        两端对齐
        [&#39;JustifyLeft&#39;,&#39;JustifyCenter&#39;,&#39;JustifyRight&#39;,&#39;JustifyBlock&#39;],
        //超链接  取消超链接 锚点
        [&#39;Link&#39;,&#39;Unlink&#39;,&#39;Anchor&#39;],
        //图片    flash    表格       水平线        表情     特殊字符      分页符
        [&#39;Image&#39;,&#39;Flash&#39;,&#39;Table&#39;,&#39;HorizontalRule&#39;,&#39;Smiley&#39;,&#39;SpecialChar&#39;,&#39;PageBreak&#39;],
        &#39;/&#39;,
        // 样式     格式    字体   字体大小
        [&#39;Styles&#39;,&#39;Format&#39;,&#39;Font&#39;,&#39;FontSize&#39;],
        //文本颜色   背景颜色
        [&#39;TextColor&#39;,&#39;BGColor&#39;],
        //全屏         显示区块         源码
        [&#39;Maximize&#39;, &#39;ShowBlocks&#39;,&#39;-&#39;,&#39;Source&#39;]
    ],
	config.format_tags = &#39;p;h1;h2;h3;h4;h5;h6;pre&#39;;
	config.removeButtons = &#39;Underline,Subscript,Superscript&#39;;
	config.removeDialogTabs = &#39;image:advanced;link:advanced&#39;;
	//加载插件
	config.extraPlugins = &#39;colorbutton,panelbutton,floatpanel&#39;; 
};

更多程式相關知識,請造訪:程式設計影片! !

以上是分享推薦一款好用的TP富文本編輯器-CKEditor的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金社区。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具