CKEditor中加入syntaxhighlighter代碼高亮插件
從官網(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"。解決!
上一篇:Ueditor和CKeditor 兩款編輯器的使用與配置方法
欄 目:網(wǎng)頁(yè)編輯器
下一篇:常用的HTML富文本編譯器UEditor、CKEditor、TinyMCE、HTMLArea、eWebEditor、Ki
本文標(biāo)題:CKEditor中加入syntaxhighlighter代碼高亮插件
本文地址:http://www.jygsgssxh.com/a1/wangyebianjiqi/11786.html
您可能感興趣的文章
- 01-11針對(duì)PHP環(huán)境下Fckeditor編輯器上傳圖片配置詳細(xì)教程
- 01-11ckeditor插件開(kāi)發(fā)簡(jiǎn)單實(shí)例
- 01-11CKEditor 取消轉(zhuǎn)義的兩種方法
- 01-11FCKeditor + SyntaxHighlighter 讓代碼高亮著色插件
- 01-11Fckeditor XML Request error:internal server error (500) 解決方法
- 01-11fckeditor編輯器下的自定義分頁(yè)符實(shí)現(xiàn)方法
- 01-11SyntaxHighlighter配合CKEditor插件輕松打造代碼語(yǔ)法著色
- 01-11ie9后瀏覽器fckeditor無(wú)法上傳圖片、彈出浮層內(nèi)容不顯示的解決方
- 01-11ajax php實(shí)現(xiàn)給fckeditor文本編輯器增加圖片刪除功能
- 01-11FCKeditor 在chrome中不顯示問(wèn)題


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11針對(duì)PHP環(huán)境下Fckeditor編輯器上傳圖片
- 01-11關(guān)于jsp版ueditor1.2.5的部分問(wèn)題解決(上
- 01-11UEditor編輯器自定義上傳圖片或文件路
- 01-11UEditor 編輯器跨域上傳解決方法
- 01-11免費(fèi)開(kāi)源百度編輯器(UEditor)使用方法
- 01-11ckeditor插件開(kāi)發(fā)簡(jiǎn)單實(shí)例
- 01-11使用ZeroClipboard解決跨瀏覽器復(fù)制到剪
- 01-11CKEditor 取消轉(zhuǎn)義的兩種方法
- 01-11FCKeditor + SyntaxHighlighter 讓代碼高亮著
- 01-11為SyntaxHighlighter添加新語(yǔ)言的方法
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法


