新闻中心
消费主张 IT业界 今日观察 消费警示 曝光台 热点专栏 奇闻趣事 一语惊人 专利之窗 动态备忘
领券购
生活必须 吃货优选 学习艺术 旅游出行 手机电脑 电子电器 热血男装 热销女装
图片中心
美女欣赏 风景欣赏 艺术欣赏 实物图片 制作美图 生活图片
下载中心
手机软件 语文数学 常用软件 红绿软件 信息社政 音体美合 科学自然 英语外语
映讯频道
浓缩电影 邻国热剧 一技之长 艺术无界 教育教学 纪录之窗 战争片 动作片 情感片 国内片 喜剧片 恐怖片 科幻片 大陆剧 港澳台剧 动漫片 明星云集 欧美剧集
聚划算
数码产品 服装服饰 家电家具 装潢装饰 商旅游玩 生活必备 二手特色 健康保健
FLASH频道
MTV类 运动游戏 益智游戏 女生游戏 休闲游戏 电脑运用 有声故事 有声艺术
社会专题
美文园地 制作教程 历史天空 游山玩水 美食天下 科技科幻 天文地理 时尚酷玩 健康保健 寻医问药 有书有道 农村特色 幽默笑话 汽车电子 星新一代 教育咨询 天下军事 硬件网络 黑白社会 靓丽女人 代码效果 免费资源 便民服务 办公达人 玄幻奇幻 软件技术
关闭
当前位置:首页 → 社会专题 → 硬件网络
CKEditor插入代码插件( 代码高亮插件)

CKEditor与原来的FCKeditor有太大的不同了,作为开发人员,在做自己的博客的时候总是需要贴代码的,只好给它先做一个插入代码的插件了。高亮代码用的是"SyntaxHighlighter"。

1、在"ckeditor\plugins\"目录下新建一个"insertcode"目录,然后在"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');    }});

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

3、增加"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'], ['java script', 'js'], ['Java', 'java'], ['JavaFX', '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: 'wiidth:700px;heiight: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("<pre class=\"brush:" + lang + ";\">" + html + "</pre>");        },        onLoad: function(){        }    };});

我是用"syntaxhighlighter"来做代码高亮的,如果你不喜欢它也可以换成其它的。

4、接下来就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件的,因为我是把“插入代码”功能做为一个编辑器必要的功能来使用的。

找到ckeditor目录下的"ckeditor.js",这里的代码是经过压缩的,我们用CKEditor原来的about插件做参考。查找"about",找到"fullPage:false,heiight:200,plugins:'about,basicstyles",我们在"about"后面增加",insertcode",这里就变成"plugins:'about,insertcode,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.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');}});

接下来查找"i.toolbar_Basic=",这就是CKEditor默认的工具栏了,我们在这里加上",insertcode",你可以加在你想要的位置。比如我的"['Maximize','ShowBlocks','-','insertcode']"。

5、进入"ckeditor\lang",分别在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code'",",insertcode:'插入代碼'",",insertcode:'插入代码'"。

6、对CKEditor的修改已经OK了,还有最后一步就是在你需要高亮代码的页面引用:

<link type="text/css" rel="stylesheet" href="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/styles/shCore.css"/><link type="text/css" rel="stylesheet" href="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/styles/shThemeDefault.css"/><script type="text/java script" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shCore.js"></script><script type="text/java script" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shBrushes.js"></script>

最后,还在页面增加这段JS:

<script type="text/java script">    SyntaxHighlighter.config.clipboardSwf = 'http://blog.moozi.net/wp-includes/js/syntaxhighlighter/clipboard.swf';    SyntaxHighlighter.all();</script>

CKEditor的"插入代码"插件就OK了。

≡ 提示:输入修改代码都可运行!
1
酒精度
70
顶一下
相关阅读
内容加载中……