CKEditor 4 自定义插件开发指南
本内容分享了 CKEditor 4 扩展插件的具体制作方法,旨在为读者提供参考并辅助实践。
接下来将深入探讨 CKEditor 的扩展能力,具体表现为添加自定义按钮及其点击后的响应逻辑。
构建自定义按钮需执行以下步骤:
- 首先准备一个图标,推荐使用 16*16 像素的 PNG 格式图片;若缺乏素材,可使用提供的
作为替代。 - 其次,在
plugins目录下创建与插件同名的文件夹,并在其中新建名为plugin.js的核心文件。 - 最后,在插件目录内建立 dialogs 子目录,并创建与插件同名的 JS 文件以定义具体业务功能。
进入实操环节。
第一步是搭建基础结构,姜哥在 ckeditor/plugins/ 路径下创建了 myplug 文件夹,该插件命名为 myplug。

第二步编辑 plugin.js 文件,填入初始化代码。务必确保文件名和变量名前后严格一致,后续运行报错多源于命名错误。
(function(){
//Section 1 : 按下自定义按钮时执行的代码
var a= {
exec:function(editor){
alert("This a custome button!");
}
},
//Section 2 : 创建自定义按钮、绑定方法
b='myplug'; //注意myplug名字
CKEDITOR.plugins.add(b,{
init:function(editor){
CKEDITOR.dialog.add('myplugDialog', this.path + 'dialogs/myplug.js'); //注意myplug名字
editor.addCommand('myplug', new CKEDITOR.dialogCommand('myplugDialog')); //注意myplug名字
//注意myplug名字 和 图片路径
editor.ui.addButton('myplug',{
label:'打开我的插件',
icon: this.path + 'myplug.png',
command:b
});
}
});
})();
第三步修改 config.js 中的 toolbar 配置,使自定义插件在界面可见。
CKEDITOR.editorConfig = function( config ) {
config.extraPlugins="myplug"; //增加了我们的自定义插件
config.toolbar = 'Full';
config.toolbar_Full =
[
{ name: 'custome_plugin', items : [ 'myplug'] }, //将自定义插件加入toolbar
{ name: 'insert', items : [ 'upload','album','-','Table' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','-','Blockquote',
'-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock' ] },
{ name: 'colors', items : [ 'TextColor','BGColor' ] },
{ name: 'document', items : [ 'Source' ] },
];
};
此处仅添加两行关键代码:config.extraPlugins="myplug"; 用于向 CKEditor 注册插件,告知系统存在 myplug 这一组件;另一行 { name: 'custome_plugin', items : [ 'myplug'] } 负责配置工具栏,确保按钮显示,注意末尾逗号不可遗漏。
保存后观察效果,图标已出现,但点击后瞬间消失,这是因为尚未编写对应的 dialog 处理代码。

若此步失败,建议从两个维度排查问题:
① 反复检查前文强调的名称是否拼写正确; ② 考虑文件编码问题,尝试删除所有中文注释替换为英文进行测试。
第四步开发 dialogs/myplug.js,以实现具体的业务交互功能。
在 myplug/dialogs/ 目录下新建 myplug.js 文件,该文件对应前一步中 plugin.js 里的特定引用行。
CKEDITOR.dialog.add('myplugDialog', this.path + 'dialogs/myplug.js'); //注意myplug名字
该行代码指定了 dialog 文件的目录与名称,虽可随意设定,但为便于管理,强烈建议保持一致以避免麻烦。接下来查看 myplug.js 中的具体内容。
(function () {
function myplugDialog(editor) {
return {
title: 'Who does you want to say hello?', //窗口标题
minWidth: 300,
minHeight: 80,
buttons: [{
type: 'button',
id: 'someButtonID',
label: 'Button',
onClick: function () {
alert('This is a custome button');
}
//对话框底部的自定义按钮
},
CKEDITOR.dialog.okButton, //对话框底部的确定按钮
CKEDITOR.dialog.cancelButton], //对话框底部的取消按钮
contents: //每一个contents在对话框中都是一个tab页
[
{
id: 'user', //contents的id
label: 'You name',
title: 'You name',
elements: //定义contents中的内容,我们这里放一个文本框,id是name
[
{
id: 'name',
type: 'text',
style: 'width: 50%;',
label: 'You name',
}
]
}
],
onLoad: function () {
//alert('onLoad');
},
onShow: function () {
//alert('onShow');
},
onHide: function () {
//alert('onHide');
},
onOk: function () {
//点击 确定 按钮之后触发的事件
var name = this.getValueOf( 'user', 'name' );
//从界面上取值的方法,getValueOf( 'contents的id', '控件的id' )
editor.insertHtml('' + name + ' : Hello world!' + '
');
//将内容放入到editor
this.commitContent();
},
onCancel: function () {
//alert('onCancel');
},
resizable: CKEDITOR.DIALOG_RESIZE_HEIGHT
};
}
CKEDITOR.dialog.add('myplugDialog', function (editor) {
return myplugDialog(editor);
});
})();
在上述代码中,首先定义了 myplugDialog 对象,设置其标题、最小宽度和高度,随后添加一个 tab 页并嵌入对话框。代码底部将该对话框注册至 CKEditor,从而允许前端通过按钮调用。
以下是常见错误汇总:
1. 名称未匹配:再次强调,myplugDialog 必须前后完全一致,且需与 plugin.js 中的定义保持同步。
2. 中文编码问题:若点击无反应,可先尝试删除代码中所有中文。或在 Notepad++ 中转换编码格式,操作如下:

使用标红的两个选项进行转换,通常能解决问题。重新保存后再次运行测试。

点击插件后弹出对话框,输入名字“姜哥”并确认。

输入的内容随即被插入到当前光标所在位置。
至此完成了 CKEditor 自定义插件及其内容交互的学习。篇幅较长,遇到问题时,姜哥总结主要归因于两点:
① 名称前后不一致; ② 中文编码格式引发的乱码。
完整实例代码可通过本站链接下载。
希望上述内容对大家的 JavaScript 程序设计工作有所助益。