相關資源
1. 首頁地址:http://ckeditor.com/
2. 下載地址:http://ckeditor.com/download
3. SDK地址:http://sdk.ckeditor.com/
如何開始
1、選擇位置,放置CKEditor的包
如:放置在webapp/plugin路徑下,其中webapp是web項目的根目錄。
2、在頁面中引入ckeditor.js
<script type="text/javascript" src="${pageContext.request.contextPath}/plugin/ckeditor/ckeditor.js"></script>
3、在頁面中定義textarea,定義id和name屬性
<textarea id="editor" name="content"></textarea>
4、激活控件
在頁面加載完成后調用下述語句:
CKEDITOR.replace("editor"); // 指定textarea的id或name如果順利,可以看到效果:
補充:
1. 激活控件需要指定textarea的id或name,優先使用id,name用於作為請求參數的key。
2. 激活控件之后,原textarea隱藏,CKEditor取代textarea展示。控件中輸入的內容和隱藏的textarea的內容不是實時同步的,目前所知,表單提交前是會同步的,后台根據textarea的name獲取的參數值和ckeditor的輸入是一致的。
3. 如果想在js隨時獲取控件的內容,可以使用下述的語句:
var value = CKEDITOR.instances.editor.getData();
注意:editor是CKEDITOR的一個instance,與激活控件時傳入的字符串一致。
自定義配置
ckeditor/config.js文件用於CKEditor的配置。剛下載下來時,內容如下:
/** * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or http://ckeditor.com/license */ CKEDITOR.editorConfig = function( config ) { // Define changes to default configuration here. For example: // config.language = 'fr'; // config.uiColor = '#AADC6E'; };
我們需要自定義配置,寫在方法里。
配置工具欄
CKEditor划分了三個版本,支持詳簡不同的工具欄。如果我們需要在此基礎上進行配置,可以使用下載包里提供的配置工具,直接以本瀏覽器打開/ckeditor/samples/toolbarconfigurator/index.html
工具欄的層級划分:行 — 工具條 — 組 — 項目。行與行之間,以row separator分隔;工具條之間有明顯的間距;組之間有豎線。
配置完成之后,點擊Get toolbar config按鈕,可以得到一份源碼,將源碼copy到config.js中,就實現了工具欄的配置。
字體配置
config.font_names = '宋體/SimSun;新宋體/NSimSun;仿宋/FangSong';如:將上述代碼復制到config.js的方法中,可以配置控件的字體分別為宋體、新宋體、仿宋。
控件可以配置多個字體實體,字體之間以
;
分隔。字體又分顯示名和設置名之別,以/
分隔;顯示名用於在控件中顯示,設置名用於設置對應的font-family,所以設置名不能隨意填寫。如,按照上述的配置,選擇了新宋體之后,輸出的代碼為style="font-family:nsimsun"
。
font-family
可以設置多個字體的序列,所以控件的一個字體實體,可以有多個輸出名,以,
分隔。如:config.font_names = 'Times New Roman/Times New Roman, Times, serif';
換行模式配置
在默認的情況下,Enter鍵是添加一個p標簽,而Shift+Enter是添加一個br標簽。控件提供了三種模式:
1. CKEDITOR.ENTER_P
新增一個p標簽
2. CKEDITOR.ENTER_BR
新增一個br標簽
3. CKEDITOR.ENTER_DIV
新增一個div標簽
控件使用下述的參數名來配置模式:
1. enterMode
配置單擊Enter鍵的模式
2. shiftEnterMode
配置Shift + Enter組合鍵的模式
如下述代碼:
config.enterMode = CKEDITOR.ENTER_BR; // 配置Enter是換行 config.shiftEnterMode = CKEDITOR.ENTER_P; // 配置Shift + Enter是換段落
更多配置
參考CKEDITOR.config的API。
配置的方式
除了上文中描述的,直接修改config.js文件,還有另外兩種配置的方式。
1. 激活時配置
CKEDITOR.replace( 'editor', { language: 'fr', uiColor: '#9AB8F3' });2. 自定義配置文件
CKEDITOR.replace( 'editor1', { customConfig: '/custom/ckeditor_config.js' });要求自定義配置文件的結構和默認的config.js一致。
功能試煉
1、初始最大化
CKEDITOR.editor有事件instanceReady,CKEDITOR.editor#on( 'instanceReady', function(evt) )可以捕捉控件初始化完成的時機;
CKEDITOR.instances.editorId可以獲取指定editorId的CKEDITOR.editor實例;
CKEDITOR.editor#execCommand( commandName, [data] )用於執行命令,'maximize'是最大化命令;
結合以上知識,我們可以得到代碼
CKEDITOR.instances.editor.on('instanceReady', function (e) { CKEDITOR.instances.editor.execCommand('maximize'); // 初始最大化 });
2、獲取控件的富文本內容
CKEDITOR.editor#getData()可用於獲取富文本的內容
var value = CKEDITOR.instances.editor.getData();
3、打印預覽
CKEDITOR.instances.editor.execCommand('preview'); // 預覽 CKEDITOR.instances.editor.execCommand('print'); // 打印
關於execCommand的說明
"maximize"是最大化的命令,"preview"是預覽的命令,"print"的命令。大家一定想要一份command的清單,求知有哪些命令可供我們使用。很遺憾,我沒有找到這樣的清單,通過走讀源碼,在ckeditor.js中,會調用CKEDITOR.plugins.add( {String}name, {Object}[definition] )來注冊資源,"maximize"、"preview"、"print"都在其中。
通過關鍵字匹配,共有72個資源:
dialogui, dialog, about, a11yhelp, dialogadvtab, basicstyles, bidi, blockquote, clipboard, button, panelbutton, panel, floatpanel, colorbutton, colordialog, templates, menu, contextmenu, div, resize, toolbar, elementspath, enterkey, entities, popup, filebrowser, find, fakeobjects, flash, floatingspace, listblock, richcombo, font, forms, format, horizontalrule, htmlwriter, iframe, wysiwygarea, image, indent, indentblock, indentlist, smiley, justify, menubutton, language, link, list, liststyle, magicline, maximize, newpage, pagebreak, pastetext, pastefromword, preview, print, removeformat, save, selectall, showblocks, showborders, sourcearea, specialchar, scayt, stylescombo, tab, table, tabletools, undo, wsc