(來源:http://ipc.javaeye.com/blog/695393)
CKEditor是新一代的FCKeditor,是一個重新開發的版本。CKEditor是全球最優秀的網頁線上文字編輯器之一,因其驚人的效能與可擴充性而廣泛的被運用於各大網站。
從官網
下載ckeditor,我下載的是CKEditor 3.3.1
。CKEditor與原來的FCKeditor有太大的不同了,作為開發人員,在做自己的部落格的時候總是需要貼代碼的,只好給它先做一個插入代碼的外掛程式了。高亮代碼用的是"SyntaxHighlighter
"。
1、在"ckeditor/plugins/"目錄下建立一個"insertcode"目錄,然後在"insertcode"目錄下建立一個"plugin.js",輸入以下代碼:
Js代碼CKEDITOR.plugins.add('insertcode', {<br /> requires: ['dialog'],<br /> init: function(a){<br /> var b = a.addCommand('insertcode', new CKEDITOR.dialogCommand('insertcode'));<br /> a.ui.addButton('insertcode', {<br /> label: a.lang.insertcode.toolbar,<br /> command: 'insertcode',<br /> icon: this.path + 'images/code.jpg'<br /> });<br /> CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');<br /> }<br />});
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"的圖片(附件中上傳了一個code的jpg圖片,大家可以直接使用)。
3、增加"dialogs"目錄,建立一個"insertcode.js",輸入如下代碼:
Js代碼 CKEDITOR.dialog.add('insertcode', function(editor){<br /> var escape = function(value){<br /> return value;<br /> };<br /> return {<br /> title: 'Insert Code Dialog',<br /> resizable: CKEDITOR.DIALOG_RESIZE_BOTH,<br /> minWidth: 720,<br /> minHeight: 480,<br /> contents: [{<br /> id: 'cb',<br /> name: 'cb',<br /> label: 'cb',<br /> title: 'cb',<br /> elements: [{<br /> type: 'select',<br /> label: 'Language',<br /> id: 'lang',<br /> required: true,<br /> 'default': 'csharp',<br /> 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']]<br /> }, {<br /> type: 'textarea',<br /> style: 'width:700px;height:420px',<br /> label: 'Code',<br /> id: 'code',<br /> rows: 31,<br /> 'default': ''<br /> }]<br /> }],<br /> onOk: function(){<br /> code = this.getValueOf('cb', 'code');<br /> lang = this.getValueOf('cb', 'lang');<br /> html = '' + escape(code) + '';<br /> editor.insertHtml("<pre class=/"brush:" + lang + ";/">" + html + "</pre>");<br /> },<br /> onLoad: function(){<br /> }<br /> };<br />});
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了,還有最後一步就是在你需要高亮代碼的頁面引用:
Html代碼
- <
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
>
<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>
這四個檔案在syntaxhighlighter的下載包裡都有,最後,還在頁面增加這段JS:
Js代碼
- <script type=
"text/javascript"
>
- SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf'
;
- SyntaxHighlighter.all();
- </script>
<script type="text/javascript">SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf';SyntaxHighlighter.all();</script>
CKEditor的"插入代碼"外掛程式就OK了。