最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP中为CKEditor开发插入代码的插件

日期:08-31    来源:中国设计秀    作者:cnwebshow.com

   按照惯例,先来段废话:CKEditor是新一代的FCKeditor,是一个重新开发的版本。CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛的被运用于各大网站。4rh中国设计秀
4rh中国设计秀
    从官网下载ckeditor,我下载的是ckeditor_3.0.2。CKEditor与原来的FCKeditor有太大的不同了,作为开发人员,在做自己的博客的时候总是需要贴代码的,只好给它先做一个插入代码的插件了。高亮代码用的是"SyntaxHighlighter"。4rh中国设计秀
4rh中国设计秀
    1、在"ckeditorplugins"目录下新建一个"insertcode"目录,然后在"insertcode"目录下新建一个"plugin.js",输入以下代码:4rh中国设计秀
4rh中国设计秀
CKEDITOR.plugins.add('insertcode', {4rh中国设计秀
    requires: ['dialog'],4rh中国设计秀
    init: function(a){4rh中国设计秀
        var b = a.addCommand('insertcode', new CKEDITOR.dialogCommand('insertcode'));4rh中国设计秀
        a.ui.addButton('insertcode', {4rh中国设计秀
            label: a.lang.insertcode.toolbar,4rh中国设计秀
            command: 'insertcode',4rh中国设计秀
            icon: this.path + 'images/code.jpg'4rh中国设计秀
        });4rh中国设计秀
        CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');4rh中国设计秀
    }4rh中国设计秀
});4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
    2、增加"images"目录,放入一个"code.jpg"的图片,当然图片可以从google找一个,16*16大小的就好了。4rh中国设计秀
4rh中国设计秀
    3、增加"dialogs"目录,新建一个"insertcode.js",输入如下代码:4rh中国设计秀
4rh中国设计秀
insertcode.js 4rh中国设计秀
CKEDITOR.dialog.add('insertcode', function(editor){4rh中国设计秀
    var escape = function(value){4rh中国设计秀
        return value;4rh中国设计秀
    };4rh中国设计秀
    return {4rh中国设计秀
        title: 'Insert Code Dialog',4rh中国设计秀
        resizable: CKEDITOR.DIALOG_RESIZE_BOTH,4rh中国设计秀
        minWidth: 720,4rh中国设计秀
        minHeight: 480,4rh中国设计秀
        contents: [{4rh中国设计秀
            id: 'cb',4rh中国设计秀
            name: 'cb',4rh中国设计秀
            label: 'cb',4rh中国设计秀
            title: 'cb',4rh中国设计秀
            elements: [{4rh中国设计秀
                type: 'select',4rh中国设计秀
                label: 'Language',4rh中国设计秀
                id: 'lang',4rh中国设计秀
                required: true,4rh中国设计秀
                'default': 'csharp',4rh中国设计秀
                items: [['ActionScript3', 'as3'], ['Bash/shell', 'bash'], ['C#', 'csharp'], ['C++', 'cpp'], ['css', 'css'], ['Delphi', 'delphi'], ['Diff', 'diff'], ['Groovy', 'groovy'], ['Html', 'xhtml'], ['javaScript', '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']]4rh中国设计秀
            }, {4rh中国设计秀
                type: 'textarea',4rh中国设计秀
                style: 'width:700px;height:420px',4rh中国设计秀
                label: 'Code',4rh中国设计秀
                id: 'code',4rh中国设计秀
                rows: 31,4rh中国设计秀
                'default': ''4rh中国设计秀
            }]4rh中国设计秀
        }],4rh中国设计秀
        onOk: function(){4rh中国设计秀
            code = this.getValueOf('cb', 'code');4rh中国设计秀
            lang = this.getValueOf('cb', 'lang');4rh中国设计秀
            html = '' + escape(code) + '';4rh中国设计秀
            editor.insertHtml("<PRe class="brush:" + lang + ";">" + html + "</pre>");4rh中国设计秀
        },4rh中国设计秀
        onLoad: function(){4rh中国设计秀
        }4rh中国设计秀
    };4rh中国设计秀
});4rh中国设计秀
4rh中国设计秀
我是用"syntaxhighlighter"来做代码高亮的,如果你不喜欢它也可以换成其它的。4rh中国设计秀
4rh中国设计秀
    4、接下来就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件的,因为我是把“插入代码”功能做为一个编辑器必要的功能来使用的。4rh中国设计秀
4rh中国设计秀
    找到ckeditor目录下的"ckeditor.js",这里的代码是经过压缩的,我们用CKEditor原来的about插件做参考。查找"about",找到"fullPage:false,height:200,plugins:'about,basicstyles",我们在"about"后面增加",insertcode",这里就变成"plugins:'about,insertcode,basicstyles"。4rh中国设计秀
4rh中国设计秀
    继续查找"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');}});"。4rh中国设计秀
4rh中国设计秀
    接下来查找"i.toolbar_Basic=",这就是CKEditor默认的工具栏了,我们在这里加上",insertcode",你可以加在你想要的位置。比如我的"['Maximize','ShowBlocks','-','insertcode']"。4rh中国设计秀
4rh中国设计秀
    5、进入"ckeditorlang",分别在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code'",",insertcode:'插入代碼'",",insertcode:'插入代码'"。4rh中国设计秀
4rh中国设计秀
    6、对CKEditor的修改已经OK了,还有最后一步就是在你需要高亮代码的页面引用:4rh中国设计秀
4rh中国设计秀
syntaxhighlighter 4rh中国设计秀
<link type="text/css" rel="stylesheet" href="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/styles/shCore.css"/>4rh中国设计秀
<link type="text/css" rel="stylesheet" href="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/styles/shThemeDefault.css"/>4rh中国设计秀
<script type="text/Javascript" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shCore.js"></script>4rh中国设计秀
<script type="text/javascript" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shBrushes.js"></script>4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
这四个文件在syntaxhighlighter的下载包里都有,最后,还在页面增加这段JS:4rh中国设计秀
4rh中国设计秀
<script type="text/javascript">4rh中国设计秀
    SyntaxHighlighter.config.clipboardSwf = 'http://blog.moozi.net/wp-includes/js/syntaxhighlighter/clipboard.swf';4rh中国设计秀
    SyntaxHighlighter.all();4rh中国设计秀
</script>4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
4rh中国设计秀
CKEditor的"插入代码"插件就OK了。4rh中国设计秀
4rh中国设计秀
具体的演示效果请移步:http://blog.moozi.net/archives/jquery-hashtable-plugin/4rh中国设计秀
4rh中国设计秀
评价功能即可插入代码进行高亮显示。同时也修改了表情插件,

本文引用地址:/bc/article_46182.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明