CKEDITOR二次开发之插件开发方法

所属分类: 网络编程 / 网页编辑器 阅读数: 1439
收藏 0 赞 0 分享

在开始之前,感性的认知一下CKEditor源码的组织形式是很有用的. CKEditor固有的一些文件被组织到ckeditor\_source目录里. 核心的功能,诸如DOM元素操作,事件处理,初始化脚本和一些环境设置被包含在ckeditor\_source\core文件夹内. 而其它的一些功能, 比如格式化,拷贝和粘贴, 图片和链接, 都被实现为插件形式放在ckeditor\_source\plugins文件夹内. 每个文件夹表示一个插件. 并且在每个文件夹内, 有一个plugin.js的文件包含了该插件需要用到的代码.

你可以看到源代码被组织成不同的文件. 为了减少HTTP请求, CKEditor把不同的文件压缩并打包到ckeditor.js和ckeditor_basic.js里。

创建一个日期插件(date)

1、在"ckeditor\plugins\"目录下新建一个"date"目录,然后在"date"目录下新建一个"plugin.js",输入以下代码:

CKEDITOR.plugins.add('date', {
  requires: ['dialog'],
  init: function (a) {
    var b = a.addCommand('date', new CKEDITOR.dialogCommand('date'));
    a.ui.addButton('date', {
      label: a.lang.date.toolbar,
      command: 'date',
      icon: this.path + 'images/date.jpg'
    });
    CKEDITOR.dialog.add('date', this.path + 'dialogs/date.js');
  }
});

2、增加"images"目录,放入一个"date.jpg"的图片,当然图片可以从google找一个,16*16大小的正好。

3、增加"dialogs"目录,新建一个"date.js",输入如下代码:

CKEDITOR.dialog.add('date', function(editor){
  var escape = function(value){
    return value;
  };
  return {
    title: '日历控件',
    resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
    minWidth: 300,
    minHeight: 80,
    contents: [{
      id: 'cb',
      name: 'cb',
      label: 'cb',
      title: 'cb',
      elements: [{
        type: 'text',
        label: '请输入日期控件名称',
        id: 'lang',
        required: true,
      },{
        type:'html',
        html:'<span>说明:日历控件选择的日期、时间将回填到该输入框中。</span>'
      }]
    }],
    onOk: function(){
      lang = this.getValueOf('cb', 'lang');
      editor.insertHtml("<p>" + lang + "</p>");
    },
    onLoad: function(){
    }
  };
});

4、接下来就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件。

找到ckeditor目录下的"ckeditor.js",这里的代码是经过压缩的,我们用CKEditor原来的about插件做参考。查找"about",找到

fullPage:false,height:200,plugins:'about,basicstyles

然后在"about"后面增加"date",这里就变成

plugins:'about,date,basicstyles

继续查找"about",找到

j.add('about',{init:function(l){var m=l.addCommand('about',new a.dialogCommand('about'));m.modes={wysiwyg:1,source:1};m.canUndo=false;l.ui.addButton('About',{label:l.lang.about.title,command:'about'});a.dialog.add('about',this.path+'dialogs/about.js');}});

在这个 j 前面增加

j.add('date', {requires: ['dialog'],init: function(l){l.addCommand('date', new a.dialogCommand('date'));l.ui.addButton('date', {label: l.lang.date.toolbar,command: 'date',icon: this.path + 'images/code.jpg'});a.dialog.add('date', this.path + 'dialogs/date.js');}});

接下来查找"i.toolbar_Basic=",这就是CKEditor默认的工具栏了,我们在这里加上"date",你可以加在你想要的位置,例如

['Maximize','ShowBlocks','-','date']

5、进入"ckeditor\lang",在"zh-cn.js"中增加"date:'日期插件'"。

,date:{toolbar: '日期控件'}, link: { toolbar: '插入/编辑超链接', other: '<其他>', 

6、对CKEditor的修改已经OK了。

当然了,显示ckeditor的工具栏时,也可以配置:打开config.js

/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

CKEDITOR.editorConfig = function( config )
{
  // Define changes to default configuration here. For example:
  // config.language = 'fr';
  // config.uiColor = '#AADC6E';

  config.toolbar =
  [
    ['Source'],
    ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'Link', 'Unlink', 'Anchor'],
    ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'ImageButton', 'Image'],
    ['Styles', 'Format', 'Font', 'FontSize'],
    ['TextColor', 'BGColor'],
    ['date'] //刚创建的日期插件(date)
  ];


};

实例图片:

更多精彩内容其他人还在看

为SyntaxHighlighter添加新语言的方法

因为经常要在博客里贴一些Lua代码,但是所使用的SyntaxHighlighter插件默认不支持Lua语言,所以去研究了一下如何为SyntaxHighlighter添加并激活一个新的语言,这里将过程和有同样需求的童鞋分享
收藏 0 赞 0 分享

syntaxhighlighter 去掉右上角问号图标的三种方法

syntaxhighlighter可以高亮着色显示几乎所有语言形式的源代码,还可以显示行号,是一款深受网站建设人员喜爱的工具,并且它还是免费的
收藏 0 赞 0 分享

百度编辑器ueditor前台代码高亮无法自动换行解决方法

这两天本站成功安装整合了百度编辑器ueditor,用着还挺不错,但是遇到了点小问题
收藏 0 赞 0 分享

FCKeditor 在chrome中不显示问题

今天在使用chrome打开后台的时候,发现FCKeditor编辑器菜单等都不显示,chrome确实不适合后台使用,以后还是继续ie核心的吧
收藏 0 赞 0 分享

ie9后浏览器fckeditor无法上传图片、弹出浮层内容不显示的解决方法

升级到IE9后,fckeditor在IE9里的弹出浮动层会出现bug,里面的内容不会出现。原因是IE9不支持var $=document.getElementById;这样的写法了
收藏 0 赞 0 分享

FCKeditor编辑器添加图片上传功能及图片路径问题解决方法

现在很多CMS系统因为安全原因会把后台编辑器里的上传功能给去除,但这样一来对实际使用过程造成了很多麻烦,今天我们以ASPCMS系统的FCKeditor编辑器为例,说明一下如何增加图片上传功能
收藏 0 赞 0 分享

CKEditor/FCKEditor 使用FCKeditor 2.6.5 快速使用教程(含插入图片)

CKEditor 是著名的 HTML 编辑器,IBM、Oracle、Adobe 等都在用。CKEditor 创建于 2003 年,其前身为 FCKEditor,在 2009 年的时候把“F”去掉了,更名为 CKEditor。
收藏 0 赞 0 分享

CKEditor/FCKEditor 使用 CKeditor 3.0.1 快速使用教程(含插入图片)

一般来说,我们在编辑内容时,先是读入到 textarea,再将 textarea 的内容赋给编辑器。
收藏 0 赞 0 分享

FCKEditor 自定义用户目录的修改步骤 (附源码)

FCKEditor 自定义用户目录的修改步骤 (附源码) ,需要的朋友可以参考下。
收藏 0 赞 0 分享

FCKeidtor 清除编辑器内容的代码

FCKeidtor清除编辑器内容的代码,需要的朋友可以参考下。
收藏 0 赞 0 分享
查看更多