最新版CKEditor的配置方法及插件(Plugin)编写示例

 更新时间:2017年03月23日 23:10:56   作者:屈伟  
本文记录配置CKEditor过程,并以文章分页插件为例,简要CKEditor Plugin编写过程。 从官网http://ckeditor.com/download下载最新版CKEditor,解压

FCKEditor重写了js框架,并改名为CKEditor。第一次在CKEditor网站上看到demo界面,就被CKEditor友好的界面和强大的功能所震撼。毫无疑问,CKEditor是当前互联网上最优秀的开源多媒体HTML编辑器。

本文记录配置CKEditor过程,并以文章分页插件为例,简要CKEditor Plugin编写过程。 从官网http://ckeditor.com/download下载最新版CKEditor,解压。

1. 调用CKEditor方法

在页面里加载核心js文件:<script type="text/javascript" src="ckeditor/ckeditor.js"></script>,按常规方式放置textarea,如:< textarea id="editor1″ name="editor1″ rows="10" cols="80">初始化html内容</textarea>
然后在textarea后面写js:<script type="text/javascript">CKEDITOR.replace('editor1');</script>

其他调用方式可参考 _samples 目录下的示例。

2. 配置个性化工具栏

ckeditor默认的工具栏中很多不常用,或者相对中文来说不适用。可通过配置默认工具栏方式实现,最简洁的方法是直接修改配置文件 config.js 我的config.js内容如下:

CKEDITOR.editorConfig = function( config )
 {
 // Define changes to default configuration here. For example:
 // config.language = 'fr';
 config.uiColor = '#ddd';
 config.toolbar = 'Cms';
 config.toolbar_Cms =
 [
 ['Source','-'],
 ['Cut','Copy','Paste','PasteText','PasteFromWord','-'],
 ['Undo','Redo','-','Find','Replace','RemoveFormat'],['Link','Unlink','Anchor'],
 ['Image','Flash','Table','HorizontalRule', '-'],['Maximize'],
 '/',
 ['Bold','Italic','Underline','Strike','-'],
 ['FontSize'],['TextColor','BGColor'],
 ['NumberedList','BulletedList','-','Outdent','Indent','pre'],
 ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
 ['PageBreak', 'Page']
 ];
 config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
 config.fontSize_sizes = '10/10px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;28/28px;32/32px;48/48px;';
 config.extraPlugins = 'apage';
 };

3. 将编辑器内文字修改为14px (默认12px,对中文显示不太好看)

1)可视化编辑里默认字体大小:修改根目录下 contents.css,将body中font-size: 12px改为 font-size: 14px

2)源代码视图字体大小:修改skins\kama\editor.css,在最后加一句:.cke_skin_kama textarea.cke_source { font-size:14px; }

4. 插件编写流程和实例代码

1) 在plugins目录新建文件夹apage,在apage下新建文件:plugin.js 内容如下:

CKEDITOR.plugins.add( 'apage',
 {
 init : function( editor )
 {
 // Add the link and unlink buttons.
 editor.addCommand( 'apage', new CKEDITOR.dialogCommand( 'apage' ) );
 editor.ui.addButton( 'Page',
 {
 //label : editor.lang.link.toolbar,
 label : “Page",
 //icon: 'images/anchor.gif',
 command : 'apage'
 } );
 //CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
 CKEDITOR.dialog.add( 'apage', function( editor )
 {
 return {
 title : '文章分页',
 minWidth : 350,
 minHeight : 100,
 contents : [
 {
 id : 'tab1',
 label : 'First Tab',
 title : 'First Tab',
 elements :
 [
 {
 id : 'pagetitle',
 type : 'text',
 label : '请输入下一页文章标题<br />(不输入默认使用当前标题+数字形式)'
 }
 ]
 }
 ],
 onOk : function()
 {
 editor.insertHtml("[page="+this.getValueOf( 'tab1', 'pagetitle' )+"]“);
 }
 };
 } );
 },
 requires : [ 'fakeobjects' ]
 } );

2)在toolbar中加一项Page,并在配置中声明添加扩展插件 config.extraPlugins = 'apage'; 有两种方法实现,方法一是直接在config.js中添加,示例本文上面的config.js示例代码; 方法二:在引用CKEditor的地方加配置参数,如:

CKEDITOR.replace( 'editor1', { extraPlugins : 'examenLink', toolbar : [ ['Undo','Redo','-','Cut','Copy','Paste'], ['ExamenLink','Bold','Italic','Underline',], ['Link','Unlink','Anchor','-','Source'],['Page'] ] });

此时你应该看到编辑器里多了一个空白的按钮了。

解决空白按钮的方法有二:

方法1:修改插件代码,plugin,将icon定义为一个存在的图标。

方法2:让编辑显示Label的文字。需要加在放编辑器的页面里加css(注意:cke_button_apage的名称与实际保持一致。)

<style type="text/css">
 .cke_button_apage .cke_icon { display : none !important; }
 .cke_button_apage .cke_label { display : inline !important; }
 </style>

如果你的分页只需要插入一个分页符,不需要像本文需要填写标题,那更简单,只需要修改插件代码即可。请在红麦软件团队wiki上查看本文提到的所有代码: http://www.teamwiki.cn/js/ckeditor_config_plugin

CKEditor 配置及插件编写示例

CKEditor 配置

config.js

CKEDITOR.editorConfig = function( config )
{
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	config.uiColor = '#ddd';
 
	config.toolbar = 'Cms';
 config.toolbar_Cms =
 [
 ['Source','-'],
 ['Cut','Copy','Paste','PasteText','PasteFromWord','-'],
 ['Undo','Redo','-','Find','Replace','RemoveFormat'],['Link','Unlink','Anchor'],
	['Image','Flash','Table','HorizontalRule', '-'],['Maximize'],
 '/',
 ['Bold','Italic','Underline','Strike','-'],
	['FontSize'],['TextColor','BGColor'],
 ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
 ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
 ['PageBreak','-','Page']
 ];
 
	config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
	config.fontSize_sizes = '10/10px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;28/28px;32/32px;48/48px;';
 
	config.extraPlugins = 'apage';
};

CKEditor 分页插件1:到提示输入下一页文章标题

CKEDITOR.plugins.add( 'apage',
{
	init : function( editor )
	{
		// Add the link and unlink buttons.
		editor.addCommand( 'apage', new CKEDITOR.dialogCommand( 'apage' ) );
		editor.ui.addButton( 'Page',
			{
				//label : editor.lang.link.toolbar,
				label : "Page",
				//icon: 'images/anchor.gif',
				command : 'apage'
			} );
		//CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
		CKEDITOR.dialog.add( 'apage', function( editor )
		{		
			return {
				title : '文章分页',
				minWidth : 350,
				minHeight : 100,
				contents : [
					{
						id : 'tab1',
						label : 'First Tab',
						title : 'First Tab',
						elements :
						[
							{
								id : 'pagetitle',
								type : 'text',
								label : '请输入下一页文章标题<br />(不输入默认使用当前标题+数字形式)'
							}
						]
					}
				],
				onOk : function()
					{
						editor.insertHtml("[page="+this.getValueOf( 'tab1', 'pagetitle' )+"]");
					}
			};
		} );
	},
 
	requires : [ 'fakeobjects' ]
} );

CKEditor 分页插件2:直接插入分页符

因为编辑器的默认转码,使用过程中需要将『page』中的『』去掉。

CKEDITOR.plugins.add( 'apage',
{
	var cmd = {
		exec:function(editor){
			editor.insertHtml("[[『page』]]");
		}
	}
	init : function( editor )
	{
		// Add the link and unlink buttons.
		editor.addCommand( 'apage', cmd );
		editor.ui.addButton( 'Page',
			{
				//label : editor.lang.link.toolbar,
				label : "Page",
				//icon: 'images/anchor.gif',
				command : 'apage'
			} );		
	},
 
	requires : [ 'fakeobjects' ]
} );

相关文章

  • 免费开源百度编辑器(UEditor)使用方法

    免费开源百度编辑器(UEditor)使用方法

    UEditor是一个开源免费的编辑器,由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于BSD协议,允许自由使用和修改代码
    2014-05-05
  • dedecms ckeditor编辑器添加链接默认新窗口打开的修改方法

    dedecms ckeditor编辑器添加链接默认新窗口打开的修改方法

    最近使用了dedecms ckeditor编辑器,发现每次都是当前页打开,对用户浏览造成一定的麻烦,所以特改成新窗口打开,这里脚本之家小编为大家分享下
    2014-07-07
  • jsp fckeditor 上传中文图片乱码问题的解决方法

    jsp fckeditor 上传中文图片乱码问题的解决方法

    彻底解决fckeditor(jsp版)上传中文图片乱码问题,我这里用的编码是utf-8的,这里用的fckeditor 是2.6的,fckeditor.java包是2.3的
    2009-02-02
  • FCKeditor + SyntaxHighlighter 让代码高亮着色插件

    FCKeditor + SyntaxHighlighter 让代码高亮着色插件

    FCKeditor是现在最为流行的开源编辑器,SyntaxHighlighter是一个用JS实现的代码高亮显示插件,FCKeditor + SyntaxHighlighter 让代码高亮着色,可以最小化修改您的程序实现效果
    2014-06-06
  • 百度UEditor修改右下角统计字数包含html样式

    百度UEditor修改右下角统计字数包含html样式

    百度UEditor修改右下角统计字数默认只统计前台所见的文字个数,如何让右下角统计字数包含html样式,需要的朋友可以参考下
    2014-07-07
  • KindEditor 编辑器 v3.5.1 修改版

    KindEditor 编辑器 v3.5.1 修改版

    最近刚认识了KindEditor,感觉挺好用,花了点时间做了些修改。如果您有更好的建议,欢迎大家一起讨论。
    2010-09-09
  • xhEditor编辑器入门基础

    xhEditor编辑器入门基础

    在线HTML编辑器就是在线编辑HTML代码的工具,它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入HTML的地方,是Web应用的常用模块之一。
    2010-12-12
  • asp.net 为FCKeditor开发代码高亮插件实现代码

    asp.net 为FCKeditor开发代码高亮插件实现代码

    昨天已经将BlogEngine的可视化编辑器换成了FCKeditor,作为一个程序员,在博客中插入代码是很重要的一块。网上现有的都是修改FCKeditor的fckeditorcode_gecko.js和fckeditorcode_ie.js以达到InsertCode的目的。这个方法非常麻烦,当要使用FCKeditor新版本时都要重新修改这两个文件,非常影响我们的效率。
    2008-08-08
  • fckeditor asp版本的文件重命名

    fckeditor asp版本的文件重命名

    最近不得不研究FCKEDITOR,而且是ASP版本。对其文件上传后的重命名,很郁闷。下面记录我修改的过程,部分函数来自网络。
    2009-08-08
  • FCKeditorAPI 手册 js操作获取等

    FCKeditorAPI 手册 js操作获取等

    FCKeditorAPI 手册 js操作控制,获取等函数代码,使用网页编辑器的朋友可以参考下。
    2011-01-01

最新评论