最近由于自己想做一個網站形式的代碼庫,自已寫一個在線文本編輯器,對于現在的我來,確實是很不切實際,呵呵!再說了,現在有一個非常好的在線文本編輯器(ckeditor)了,我和必再去費這等功夫呢!有現成的,拿過用就是的唄!正所謂的拿來主義!不過這個在線文本編輯器,對于我們程序員來說有一個算是缺陷吧!沒有代碼高亮的功能!這樣把代碼貼上去,很不好看!今天晚上,我總是把他給弄出來了。當然也采在別人的肩膀上做成的。在此感謝他們的分享!費話不多說了!咱們進入正題吧!
首先去官方網站下載個ckeditor
其次去官方網站下載個syntaxhighlighter ,這個是代碼高亮插件。
還有就是請把我下面提供下載的Demo里的syntaxhighlighter/shBrushes.js文件
下載以后,把他們解壓,加入項目,如下所示:
然后在ckeditor下面新建一個文件夾,命名為: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'); }});
目錄結構如下圖:圖二
再新建一個images文件夾,放入一個"code.jpg"的圖片,如上圖所示,當然圖片可以從google找一個,16*16大小的就好了。
再新建一個dialogs文件夾,新建一個"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",我們在"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');}});"。
如下圖:
接下來繼續在ckeditor.js查找"i.toolbar_Basic=",這就是CKEditor默認的工具欄了,我們在這里加上",insertcode",比如我的"['Maximize','ShowBlocks','-','insertcode']"
我添加在如下圖選中的文本那個地方:
最后一步:進入"ckeditor/lang",請注意是分別在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code'",",insertcode:'插入代碼'",",insertcode:'插入代碼'",一定要按這個順序加哦。
如下圖是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>
后臺代碼:
protected void Unnamed1_Click(object sender, EventArgs e)
{
this.Literal1.Text = txtcontent.Text;
}
還有就是在頁面的@ Page指令中加入 ValidateRequest="false",加入后的@Page指令如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
否則會報錯的。
效果圖:
怎么獲取這個文本編輯器里的文本今天白天再寫吧!該睡覺了,1點多了,O My god!
(O YE!現在總是很完善了的)
我的Demo下載:Cheditor+syntaxhighlighter Demo ,如果還有點不懂,請多多參考這個Demo,或者給我留言吧!
參考博文:http://zhidao.baidu.com/question/302527067.html
http://blog.sina.com.cn/s/blog_5fdcf5c90100uo4r.html