程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 為CKEditor開發插入代碼的插件

為CKEditor開發插入代碼的插件

編輯:ASP技巧

   按照慣例,先來段廢話:CKEditor是新一代的FCKeditor,是一個重新開發的版本。CKEditor是全球最優秀的網頁在線文字編輯器之一,因其驚人的性能與可擴展性而廣泛的被運用於各大網站。

    從官網下載ckeditor,我下載的是ckeditor_3.0.2。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",輸入如下代碼:

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'], ['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']]
            }, {
                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("<PRe class=\"brush:" + lang + ";\">" + Html + "</pre>");
        },
        onLoad: function(){
        }
    };
});

我是用"syntaxhighlighter"來做代碼高亮的,如果你不喜歡它也可以換成其它的。

    4、接下來就是把插件加入到CKEditor裡了,我是直接修改CKEditor插件的核心文件的,因為我是把“插入代碼”功能做為一個編輯器必要的功能來使用的。

    找到ckeditor目錄下的"ckeditor.JS",這裡的代碼是經過壓縮的,我們用CKEditor原來的about插件做參考。查找"about",找到"fullPage:false,height: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了,還有最後一步就是在你需要高亮代碼的頁面引用:

syntaxhighlighter
<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/Javascript" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shCore.js"></script>
<script type="text/Javascript" src="http://blog.moozi.net/wp-includes/js/syntaxhighlighter/shBrushes.js"></script>

 

這四個文件在syntaxhighlighter的下載包裡都有,最後,還在頁面增加這段JS:

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

 


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

評價功能即可插入代碼進行高亮顯示。同時也修改了表情插件,現在使用的是QQ的表情包。有需要這個修改方法的朋友請留言,我會把QQ表情的插件制作方法貼出來。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved