系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > 其它综合 > 详细页面

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

时间:2020-02-04来源:系统城作者:电脑系统城

本文记录配置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内容如下:


 
  1. CKEDITOR.editorConfig = function( config )
  2. {
  3. // Define changes to default configuration here. For example:
  4. // config.language = 'fr';
  5. config.uiColor = '#ddd';
  6. config.toolbar = 'Cms';
  7. config.toolbar_Cms =
  8. [
  9. ['Source','-'],
  10. ['Cut','Copy','Paste','PasteText','PasteFromWord','-'],
  11. ['Undo','Redo','-','Find','Replace','RemoveFormat'],['Link','Unlink','Anchor'],
  12. ['Image','Flash','Table','HorizontalRule', '-'],['Maximize'],
  13. '/',
  14. ['Bold','Italic','Underline','Strike','-'],
  15. ['FontSize'],['TextColor','BGColor'],
  16. ['NumberedList','BulletedList','-','Outdent','Indent','pre'],
  17. ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
  18. ['PageBreak', 'Page']
  19. ];
  20. config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
  21. 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;';
  22. config.extraPlugins = 'apage';
  23. };

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 内容如下:


 
  1. CKEDITOR.plugins.add( 'apage',
  2. {
  3. init : function( editor )
  4. {
  5. // Add the link and unlink buttons.
  6. editor.addCommand( 'apage', new CKEDITOR.dialogCommand( 'apage' ) );
  7. editor.ui.addButton( 'Page',
  8. {
  9. //label : editor.lang.link.toolbar,
  10. label : “Page",
  11. //icon: 'images/anchor.gif',
  12. command : 'apage'
  13. } );
  14. //CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
  15. CKEDITOR.dialog.add( 'apage', function( editor )
  16. {
  17. return {
  18. title : '文章分页',
  19. minWidth : 350,
  20. minHeight : 100,
  21. contents : [
  22. {
  23. id : 'tab1',
  24. label : 'First Tab',
  25. title : 'First Tab',
  26. elements :
  27. [
  28. {
  29. id : 'pagetitle',
  30. type : 'text',
  31. label : '请输入下一页文章标题<br />(不输入默认使用当前标题+数字形式)'
  32. }
  33. ]
  34. }
  35. ],
  36. onOk : function()
  37. {
  38. editor.insertHtml("[page="+this.getValueOf( 'tab1', 'pagetitle' )+"]“);
  39. }
  40. };
  41. } );
  42. },
  43. requires : [ 'fakeobjects' ]
  44. } );

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


 
  1. 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的名称与实际保持一致。)


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

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

CKEditor 配置及插件编写示例

CKEditor 配置

config.js


 
  1. CKEDITOR.editorConfig = function( config )
  2. {
  3. // Define changes to default configuration here. For example:
  4. // config.language = 'fr';
  5. config.uiColor = '#ddd';
  6.  
  7. config.toolbar = 'Cms';
  8. config.toolbar_Cms =
  9. [
  10. ['Source','-'],
  11. ['Cut','Copy','Paste','PasteText','PasteFromWord','-'],
  12. ['Undo','Redo','-','Find','Replace','RemoveFormat'],['Link','Unlink','Anchor'],
  13. ['Image','Flash','Table','HorizontalRule', '-'],['Maximize'],
  14. '/',
  15. ['Bold','Italic','Underline','Strike','-'],
  16. ['FontSize'],['TextColor','BGColor'],
  17. ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
  18. ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
  19. ['PageBreak','-','Page']
  20. ];
  21.  
  22. config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
  23. 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;';
  24.  
  25. config.extraPlugins = 'apage';
  26. };

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


 
  1. CKEDITOR.plugins.add( 'apage',
  2. {
  3. init : function( editor )
  4. {
  5. // Add the link and unlink buttons.
  6. editor.addCommand( 'apage', new CKEDITOR.dialogCommand( 'apage' ) );
  7. editor.ui.addButton( 'Page',
  8. {
  9. //label : editor.lang.link.toolbar,
  10. label : "Page",
  11. //icon: 'images/anchor.gif',
  12. command : 'apage'
  13. } );
  14. //CKEDITOR.dialog.add( 'link', this.path + 'dialogs/link.js' );
  15. CKEDITOR.dialog.add( 'apage', function( editor )
  16. {
  17. return {
  18. title : '文章分页',
  19. minWidth : 350,
  20. minHeight : 100,
  21. contents : [
  22. {
  23. id : 'tab1',
  24. label : 'First Tab',
  25. title : 'First Tab',
  26. elements :
  27. [
  28. {
  29. id : 'pagetitle',
  30. type : 'text',
  31. label : '请输入下一页文章标题<br />(不输入默认使用当前标题+数字形式)'
  32. }
  33. ]
  34. }
  35. ],
  36. onOk : function()
  37. {
  38. editor.insertHtml("[page="+this.getValueOf( 'tab1', 'pagetitle' )+"]");
  39. }
  40. };
  41. } );
  42. },
  43.  
  44. requires : [ 'fakeobjects' ]
  45. } );

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

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


 
  1. CKEDITOR.plugins.add( 'apage',
  2. {
  3. var cmd = {
  4. exec:function(editor){
  5. editor.insertHtml("[[『page』]]");
  6. }
  7. }
  8. init : function( editor )
  9. {
  10. // Add the link and unlink buttons.
  11. editor.addCommand( 'apage', cmd );
  12. editor.ui.addButton( 'Page',
  13. {
  14. //label : editor.lang.link.toolbar,
  15. label : "Page",
  16. //icon: 'images/anchor.gif',
  17. command : 'apage'
  18. } );
  19. },
  20.  
  21. requires : [ 'fakeobjects' ]
  22. } );
分享到:

相关信息

  • C++ AVLTree高度平衡的二叉搜索树深入分析

    一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...

    2023-03-09

  • idea构建web项目的超级详细教程

    1、idea构建web项目 1、新建一个空项目 2、新建java模块,名为webDemo1 3、选择webDemo1右键,选择Add Framework Support 4、在WEB-INF下新建文件夹classes和lib 5、打开项目结构(Project Structure) 6、项目配置 7、模...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载