发布于2026-07-22 阅读(0)
扫一扫,手机访问
从官网下载CKEditor(我用的版本是3.3.1)之后,你会发现新版和原来的FCKeditor差别还挺大的。做个人博客时,贴代码是刚需,所以顺手给它写了个插入代码的插件。高亮部分用的是SyntaxHighlighter,下面直接上步骤。
在ckeditor/plugins/下新建一个insertcode目录,然后在里面创建plugin.js,写入以下代码:
CKEDITOR.plugins.add('insertcode', {
requires: ['dialog'],
init: function(a){
var b = a.addCommand('insertcode', new CKEDITOR.dialogCommand('insertcode'));
a.ui.addButton('insertcode', {
label: a.lang.insertcode.toolbar,
command: 'insertcode',
icon: this.path + 'images/code.jpg'
});
CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');
}
});
在insertcode目录下新建images文件夹,放入一个code.jpg图片(附件中已提供,可直接使用)。
再新建dialogs目录,创建insertcode.js,内容如下:
CKEDITOR.dialog.add('insertcode', function(editor){
var escape = function(value){
return value;
};
return {
title: 'Insert Code Dialog',
resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
minWidth: 720,
minHeight: 480,
contents: [{
id: 'cb',
name: 'cb',
label: 'cb',
title: 'cb',
elements: [{
type: 'select',
label: 'Language',
id: 'lang',
required: true,
'default': 'csharp',
items: [
['ActionScript3', 'as3'], ['Bash/shell', 'bash'], ['C#', 'csharp'],
['C++', 'cpp'], ['CSS', 'css'], ['Delphi', 'delphi'],
['Diff', 'diff'], ['Groovy', 'groovy'], ['Html', 'xhtml'],
['Ja vaScript', 'js'], ['Ja va', 'ja va'], ['Ja vaFX', 'jfx'],
['Perl', 'perl'], ['PHP', 'php'], ['Plain Text', 'plain'],
['PowerShell', 'ps'], ['Python', 'py'], ['Ruby', 'rails'],
['Scala', 'scala'], ['SQL', 'sql'], ['Visual Basic', 'vb'], ['XML', 'xml']
]
}, {
type: 'textarea',
style: 'width:700px;height:420px',
label: 'Code',
id: 'code',
rows: 31,
'default': ''
}]
}],
onOk: function(){
code = this.getValueOf('cb', 'code');
lang = this.getValueOf('cb', 'lang');
html = '' + escape(code) + '';
editor.insertHtml("" + html + "
");
},
onLoad: function(){}
};
});
这里用的是SyntaxHighlighter做高亮,如果你喜欢其他工具,替换掉相应部分即可。
接下来的操作比较直接,因为我是把“插入代码”当作编辑器标配功能来用的,所以直接修改核心文件ckeditor.js。注意,这个文件是压缩过的,我们以原有的about插件为参考。
首先,搜索 ,insertcodefullPage:false,height:200,plugins:'about,basicstyles,在about后面加上,insertcode,变成:plugins:'about。
然后,继续搜索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.add('insertcode', {requires: ['dialog'],init: function(l){l.addCommand('insertcode', new a.dialogCommand('insertcode'));l.ui.addButton('insertcode', {label: l.lang.insertcode.toolbar,command: 'insertcode',icon: this.path + 'images/code.jpg'});a.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');}});
接下来,查找 ,insertcode ['Maximize','ShowBlocks','-','insertcode']i.toolbar_Basic=,这是CKEditor默认工具栏的配置。在其中加上,你可以把它放在任何你想要的位置。比如我放在。
进入ckeditor/lang目录,分别修改en.js、zh.js和zh-cn.js。在合适的位置添加:
,insertcode:'Insert Code'
,insertcode:'插入代碼'
,insertcode:'插入代码'
最后,在需要高亮代码的页面中引入SyntaxHighlighter的样式和脚本:
这四个文件都在SyntaxHighlighter的下载包里。另外,在页面底部添加初始化代码:
至此,CKEditor的“插入代码”插件就完工了。
提示个小bug
修改之后,insertcode图标的title会显示为空。解决办法很简单:把代码中两处label: a.lang.insertcode.toolbar改为label: a.lang.insertcode即可。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8