最近由于自己想做一個(gè)網(wǎng)站形式的代碼庫,自已寫一個(gè)在線文本編輯器,對(duì)于現(xiàn)在的我來,確實(shí)是很不切實(shí)際,呵呵!再說了,現(xiàn)在有一個(gè)非常好的在線文本編輯器(ckeditor)了,我和必再去費(fèi)這等功夫呢!有現(xiàn)成的,拿過用就是的唄!正所謂的拿來主義!不過這個(gè)在線文本編輯器,對(duì)于我們程序員來說有一個(gè)算是缺陷吧!沒有代碼高亮的功能!這樣把代碼貼上去,很不好看!今天晚上,我總是把他給弄出來了。當(dāng)然也采在別人的肩膀上做成的。在此感謝他們的分享!費(fèi)話不多說了!咱們進(jìn)入正題吧!
首先去官方網(wǎng)站下載個(gè)ckeditor
其次去官方網(wǎng)站下載個(gè)syntaxhighlighter ,這個(gè)是代碼高亮插件。
還有就是請(qǐng)把我下面提供下載的Demo里的syntaxhighlighter/shBrushes.js文件
下載以后,把他們解壓,加入項(xiàng)目,如下所示:

然后在ckeditor下面新建一個(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');
}
});
目錄結(jié)構(gòu)如下圖:圖二

再新建一個(gè)images文件夾,放入一個(gè)"code.jpg"的圖片,如上圖所示,當(dāng)然圖片可以從google找一個(gè),16*16大小的就好了。
再新建一個(gè)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 () {
}
};
});
接下來,我們就把高亮插件插入到ckeditor里來,找到ckeditor文件夾下的"ckeditor.js"。按ctrl+F查找"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');}});"。
如下圖:

接下來繼續(xù)在ckeditor.js查找"i.toolbar_Basic=",這就是CKEditor默認(rèn)的工具欄了,我們?cè)谶@里加上",insertcode",比如我的"['Maximize','ShowBlocks','-','insertcode']"
我添加在如下圖選中的文本那個(gè)地方:

最后一步:進(jìn)入"ckeditor\lang",請(qǐng)注意是分別在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code'",",insertcode:'插入代碼'",",insertcode:'插入代碼'",一定要按這個(gè)順序加哦。
如下圖是en.js中的,zh-cn.js,zh.js我就不一一截圖了。

最后在頁面上添加如下引用:
head runat="server">
title>/title>
link type="text/css" rel="stylesheet" href="syntaxhighlighter_3.0.83/styles/shCore.css" />
link type="text/css" rel="stylesheet" href="syntaxhighlighter_3.0.83/styles/shThemeDefault.css" />
script type="text/javascript" src="syntaxhighlighter_3.0.83/scripts/shCore.js">/script>
script type="text/javascript" src="syntaxhighlighter_3.0.83/scripts/shBrushes.js">/script>
script type="text/javascript" src="ckeditor/ckeditor.js">/script>
link type="text/css" rel="Stylesheet" href="syntaxhighlighter_3.0.83/styles/shCoreDefault.css" />
script type="text/javascript"> SyntaxHighlighter.all();/script>
/head>
頁面的代碼如下:
form id="form1" runat="server">
div>
asp:TextBox ID="txtcontent" runat="server" TextMode="MultiLine" Height="310px"
Width="100%">/asp:TextBox>
script type="text/javascript">
CKEDITOR.replace('%= txtcontent.ClientID %>', { skin: 'office2003' });
/script>
/script>--%>
asp:Button runat="server" Text="Button" OnClick="Unnamed1_Click" />
/div>
asp:Literal ID="Literal1" runat="server">/asp:Literal>
/form>
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
protected void Unnamed1_Click(object sender, EventArgs e)
{
this.Literal1.Text = txtcontent.Text;
}
還有就是在頁面的@ Page指令中加入 ValidateRequest="false",加入后的@Page指令如下:
復(fù)制代碼 代碼如下:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
否則會(huì)報(bào)錯(cuò)的。
效果圖:

怎么獲取這個(gè)文本編輯器里的文本今天白天再寫吧!該睡覺了,1點(diǎn)多了,O My god!
(O YE!現(xiàn)在總是很完善了的)
我的Demo下載:Cheditor+syntaxhighlighter Demo ,如果還有點(diǎn)不懂,請(qǐng)多多參考這個(gè)Demo,或者給我留言吧!
參考博文:http://zhidao.baidu.com/question/302527067.html
http://blog.sina.com.cn/s/blog_5fdcf5c90100uo4r.html
https://www.jb51.net/article/58407.htm
您可能感興趣的文章:- FCKeditor .NET的配置、擴(kuò)展與安全性經(jīng)驗(yàn)交流
- CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
- CKeditor富文本編輯器使用技巧之添加自定義插件的方法
- CKEDITOR二次開發(fā)之插件開發(fā)方法
- ckeditor自定義插件使用方法詳解
- FCKeditor 插件開發(fā) 示例(詳細(xì)版本)
- 添加FCKeditor插件需要注意的地方
- fckeditor 修改記錄添加行距功能插件
- ckeditor插件開發(fā)簡(jiǎn)單實(shí)例
- fckeditor 插件開發(fā)參考文檔
- SyntaxHighlighter配合CKEditor插件輕松打造代碼語法著色
- 手把手教你 CKEDITOR 4 擴(kuò)展插件制作