從官網(wǎng) 下載ckeditor,我下載的是CKEditor 3.3.1 。CKEditor與原來(lái)的FCKeditor有太大的不同了,作為開(kāi)發(fā)人員,在做自己的博客的時(shí)候總是需要貼代碼的,只好給它先做一個(gè)插入代碼的插件了。高亮代碼用的是"SyntaxHighlighter "。
1、在"ckeditor/plugins/"目錄下新建一個(gè)"insertcode"目錄,然后在"insertcode"目錄下新建一個(gè)"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"目錄,放入一個(gè)"code.jpg"的圖片(附件中上傳了一個(gè)code的jpg圖片,大家可以直接使用)。
3、增加"dialogs"目錄,新建一個(gè)"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"來(lái)做代碼高亮的,如果你不喜歡它也可以換成其它的。
4、接下來(lái)就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件的,因?yàn)槲沂前选安迦氪a”功能做為一個(gè)編輯器必要的功能來(lái)使用的。
找到ckeditor目錄下的"ckeditor.js",這里的代碼是經(jīng)過(guò)壓縮的,我們用CKEditor原來(lái)的about插件做參考。查找"about",找到"fullPage:false,height:200,plugins:'about,basicstyles ",我們?cè)?about"后面增加",insertcode",這里就變成"plugins:'about,insertcode ,basicstyles"。
繼續(xù)查找"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');}}); ",我們?cè)谶@個(gè)分號(hào)后面增加"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');}}); "。
接下來(lái)查找"i.toolbar_Basic=",這就是CKEditor默認(rèn)的工具欄了,我們?cè)谶@里加上",insertcode ",你可以加在你想要的位置。比如我的"['Maximize','ShowBlocks','-','insertcode'] "。
5、進(jìn)入"ckeditor/lang",分別在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code' ",",insertcode:'插入代碼' ",",insertcode:'插入代碼' "。
6、對(duì)CKEditor的修改已經(jīng)OK了,還有最后一步就是在你需要高亮代碼的頁(yè)面引用:
link type = "text/css" rel = "stylesheet" href ="js/syntaxhighlighter/styles/shCore.css" /> link type = "text/css" rel = "stylesheet" href ="js/syntaxhighlighter/styles/shThemeDefault.css" /> script type = "text/javascript" src = "js/syntaxhighlighter/scripts/shCore.js" > /script> script type = "text/javascript" src = "js/syntaxhighlighter/scripts/shBrushes.js" >/script>
這四個(gè)文件在syntaxhighlighter的下載包里都有,最后,還在頁(yè)面增加這段JS:
script type= "text/javascript" > SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf' ; SyntaxHighlighter.all(); /script>
CKEditor的"插入代碼"插件就OK了。
提示個(gè)小bug
修改之后insertcode圖標(biāo)的title為空。我把你的代碼中的"label: a.lang.insertcode.toolbar"(共2處)改為"label: a.lang.insertcode"。解決!
標(biāo)簽:黑龍江 漯河 石嘴山 巴彥淖爾 貴港 運(yùn)城 青海 新疆
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《CKEditor中加入syntaxhighlighter代碼高亮插件》,本文關(guān)鍵詞 CKEditor,中,加入,syntaxhighlighter,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。