天天看點

富文本編輯器 CKeditor 的使用

下載下傳位址:http://ckeditor.com/download  (全套裝)

一、基礎步驟

<script type="text/javascript">

    CKEDITOR.replace('TextArea1');

    //如果是在ASP.NET環境下用的伺服器端控 件<TextBox>

    CKEDITOR.replace('tbContent');

    //如 果<TextBox>控件在母版頁中,要這樣寫

    CKEDITOR.replace('<%=tbContent.ClientID.Replace("_","$") %>');

</script>

1、在頁面<head>中引入ckeditor核心檔案ckeditor.js

<script type="text/JavaScript" src="ckeditor/ckeditor.js"></script>

2、在使用編輯器的地方插入HTML控件<textarea>

<textarea id="TextArea1" cols="20" rows="2" class="ckeditor"></textarea>      

如果是ASP.NET環境,也可用伺服器端控件<TextBox>

<asp:TextBox ID="tbContent" runat="server" TextMode="MultiLine" class="ckeditor"></asp:TextBox>      

注意在控件中加上 class="ckeditor" 。

3、将相應的控件替換成編輯器代碼

CKEDITOR.replace('TextArea1');

//如果是在ASP.Net環境下用的伺服器端控件<TextBox>

CKEDITOR.replace('tbContent');

//如果<TextBox>控件在母版頁中,要這樣寫

CKEDITOR.replace('<%=tbContent.ClientID.Replace("_","$") %>');

二、配置檔案  config.js 

// 界面語言,預設為 'en'

    config.language = 'zh-cn';

    // 設定寬高

    config.width = 400;

    config.height = 400;

    // 編輯器樣式,有三種:'kama'(預設)、'office2003'、'v2'

    config.skin = 'v2';

    // 背景顔色

    config.uiColor = '#FFF';

    // 工具欄(基礎'Basic'、全能'Full'、自定義)plugins/toolbar/plugin.js

    config.toolbar = 'Basic';

    config.toolbar = 'Full';

    這将配合:

    config.toolbar_Full = [

       ['Source','-','Save','NewPage','Preview','-','Templates'],

       ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],

       ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],

       ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],

       '/',

       ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],

        ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],

        ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],

        ['Link','Unlink','Anchor'],

       ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],

       '/',

        ['Styles','Format','Font','FontSize'],

        ['TextColor','BGColor']

    ];

    //工具欄是否可以被收縮

    config.toolbarCanCollapse = true;

    //工具欄的位置

    config.toolbarLocation = 'top';//可選:bottom

    //工具欄預設是否展開

    config.toolbarStartupExpanded = true;

    // 取消 “拖拽以改變尺寸”功能 plugins/resize/plugin.js

    config.resize_enabled = false;

    //改變大小的最大高度

    config.resize_maxHeight = 3000;

    //改變大小的最大寬度

    config.resize_maxWidth = 3000;

    //改變大小的最小高度

    config.resize_minHeight = 250;

    //改變大小的最小寬度

    config.resize_minWidth = 750;

    // 當送出包含有此編輯器的表單時,是否自動更新元素内的資料

    config.autoUpdateElement = true;

    // 設定是使用絕對目錄還是相對目錄,為空為相對目錄

    config.baseHref = ''

    // 編輯器的z-index值

    config.baseFloatZIndex = 10000;

    //設定快捷鍵

    config.keystrokes = [

       [ CKEDITOR.ALT + 121 , 'toolbarFocus' ],  //擷取焦點

        [ CKEDITOR.ALT + 122 , 'elementsPathFocus' ],  //元素焦點

       [ CKEDITOR.SHIFT + 121 , 'contextMenu' ],  //文本菜單

       [ CKEDITOR.CTRL + 90 , 'undo' ],  //撤銷

        [ CKEDITOR.CTRL + 89 , 'redo' ],  //重做

        [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 , 'redo' ],  //

        [ CKEDITOR.CTRL + 76 , 'link' ],  //連結

        [ CKEDITOR.CTRL + 66 , 'bold' ],  //粗體

        [ CKEDITOR.CTRL + 73 , 'italic' ],  //斜體

        [ CKEDITOR.CTRL + 85 , 'underline' ],  //下劃線

        [ CKEDITOR.ALT + 109 , 'toolbarCollapse' ]

    ]

    //設定快捷鍵 可能與浏覽器快捷鍵沖突 plugins/keystrokes/plugin.js.

    config.blockedKeystrokes = [

        CKEDITOR.CTRL + 66 ,

        CKEDITOR.CTRL + 73 ,

        CKEDITOR.CTRL + 85

    ]

    //設定編輯内元素的背景色的取值 plugins/colorbutton/plugin.js.

    config.colorButton_backStyle = {

        element : 'span',

        styles : { 'background-color' : '#(color)' }

    }

    //設定前景色的取值 plugins/colorbutton/plugin.js

    config.colorButton_colors = '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF’

    //是否在選擇顔色時顯示“其它顔色”選項 plugins/colorbutton/plugin.js

    config.colorButton_enableMore = false

    //區塊的前景色預設值設定 plugins/colorbutton/plugin.js

    config.colorButton_foreStyle = {

        element : 'span',

        styles : { 'color' : '#(color)' }

    };

    //所需要添加的CSS檔案 在此添加 可使用相對路徑和網站的絕對路徑

    config.contentsCss = './contents.css';

    //文字方向

    config.contentsLangDirection = 'rtl'; //從左到右

    //CKeditor的配置檔案 若不想配置 留白即可

    CKEDITOR.replace( 'myfiled', { customConfig : './config.js' } );

    //界面編輯框的背景色 plugins/dialog/plugin.js

    config.dialog_backgroundCoverColor = '#fffefd'; //可設定參考

    config.dialog_backgroundCoverColor = 'white' //預設

    //背景的不透明度 數值應該在:0.0~1.0 之間 plugins/dialog/plugin.js

    config.dialog_backgroundCoverOpacity = 0.5

    //移動或者改變元素時 邊框的吸附距離 機關:像素 plugins/dialog/plugin.js

    config.dialog_magnetDistance = 20;

    //是否拒絕本地拼寫檢查和提示 預設為拒絕 目前僅firefox和safari支援 plugins/wysiwygarea/plugin.js.

    config.disableNativeSpellChecker = true

    //進行表格編輯功能 如:添加行或列 目前僅firefox支援 plugins/wysiwygarea/plugin.js

    config.disableNativeTableHandles = true; //預設為不開啟

    //是否開啟 圖檔和表格 的改變大小的功能 config.disableObjectResizing = true;

    config.disableObjectResizing = false //預設為開啟

    //設定HTML文檔類型

    config.docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22' ;

    //是否對編輯區域進行渲染 plugins/editingblock/plugin.js

    config.editingBlock = true;

    //編輯器中回車産生的标簽

    config.enterMode = CKEDITOR.ENTER_P; //可選:CKEDITOR.ENTER_BR或CKEDITOR.ENTER_DIV

    //是否使用HTML實體進行輸出 plugins/entities/plugin.js

    config.entities = true;

    //定義更多的實體 plugins/entities/plugin.js

    config.entities_additional = '#39'; //其中#代替了&

    //是否轉換一些難以顯示的字元為相應的HTML字元 plugins/entities/plugin.js

    config.entities_greek = true;

    //是否轉換一些拉丁字元為HTML plugins/entities/plugin.js

    config.entities_latin = true;

    //是否轉換一些特殊字元為ASCII字元 如"This is Chinese: 漢語."轉換為"This is Chinese: 漢語." plugins/entities/plugin.js

    config.entities_processNumerical = false;

    //添加新元件

    config.extraPlugins = 'myplugin'; //非預設 僅示例

    //使用搜尋時的高亮色 plugins/find/plugin.js

    config.find_highlight = {

        element : 'span',

        styles : { 'background-color' : '#ff0', 'color' : '#00f' }

    };

    //預設的字型名 plugins/font/plugin.js

    config.font_defaultLabel = 'Arial';

    //字型編輯時的字元集 可以添加常用的中文字元:宋體、楷體、黑體等 plugins/font/plugin.js

    config.font_names = 'Arial;Times New Roman;Verdana';

    //文字的預設式樣 plugins/font/plugin.js

    config.font_style = {

        element   : 'span',

        styles   : { 'font-family' : '#(family)' },

        overrides : [ { element : 'font', attributes : { 'face' : null } } ]

    };

    //字型預設大小 plugins/font/plugin.js

    config.fontSize_defaultLabel = '12px';

    //字型編輯時可選的字型大小 plugins/font/plugin.js

    config.fontSize_sizes ='8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px'

    //設定字型大小時 使用的式樣 plugins/font/plugin.js

    config.fontSize_style = {

        element   : 'span',

        styles   : { 'font-size' : '#(size)' },

        overrides : [ { element : 'font', attributes : { 'size' : null } } ]

    };

    //是否強制複制來的内容去除格式 plugins/pastetext/plugin.js

    config.forcePasteAsPlainText =false //不去除

    //是否強制用“&”來代替“&amp;”plugins/htmldataprocessor/plugin.js

    config.forceSimpleAmpersand = false;

    //對address标簽進行格式化 plugins/format/plugin.js

    config.format_address = { element : 'address', attributes : { class : 'styledAddress' } };

    //對DIV标簽自動進行格式化 plugins/format/plugin.js

    config.format_div = { element : 'div', attributes : { class : 'normalDiv' } };

    //對H1标簽自動進行格式化 plugins/format/plugin.js

    config.format_h1 = { element : 'h1', attributes : { class : 'contentTitle1' } };

    //對H2标簽自動進行格式化 plugins/format/plugin.js

    config.format_h2 = { element : 'h2', attributes : { class : 'contentTitle2' } };

    //對H3标簽自動進行格式化 plugins/format/plugin.js

    config.format_h1 = { element : 'h3', attributes : { class : 'contentTitle3' } };

    //對H4标簽自動進行格式化 plugins/format/plugin.js

    config.format_h1 = { element : 'h4', attributes : { class : 'contentTitle4' } };

    //對H5标簽自動進行格式化 plugins/format/plugin.js

    config.format_h1 = { element : 'h5', attributes : { class : 'contentTitle5' } };

    //對H6标簽自動進行格式化 plugins/format/plugin.js

    config.format_h1 = { element : 'h6', attributes : { class : 'contentTitle6' } };

    //對P标簽自動進行格式化 plugins/format/plugin.js

    config.format_p = { element : 'p', attributes : { class : 'normalPara' } };

    //對PRE标簽自動進行格式化 plugins/format/plugin.js

    config.format_pre = { element : 'pre', attributes : { class : 'code' } };

    //用分号分隔的标簽名字 在工具欄上顯示 plugins/format/plugin.js

    config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';

    //是否使用完整的html編輯模式 如使用,其源碼将包含:<html><body></body></html>等标簽

    config.fullPage = false;

    //是否忽略段落中的空字元 若不忽略 則字元将以“”表示 plugins/wysiwygarea/plugin.js

    config.ignoreEmptyParagraph = true;

    //在清除圖檔屬性框中的連結屬性時 是否同時清除兩邊的<a>标簽 plugins/image/plugin.js

    config.image_removeLinkByEmptyURL = true;

    //一組用逗号分隔的标簽名稱,顯示在左下角的層次嵌套中 plugins/menu/plugin.js.

    config.menu_groups ='clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea';

    //顯示子菜單時的延遲,機關:ms plugins/menu/plugin.js

    config.menu_subMenuDelay = 400;

    //當執行“建立”指令時,編輯器中的内容 plugins/newpage/plugin.js

    config.newpage_html = '';

    //當從word裡複制文字進來時,是否進行文字的格式化去除 plugins/pastefromword/plugin.js

    config.pasteFromWordIgnoreFontFace = true; //預設為忽略格式

    //是否使用<h1><h2>等标簽修飾或者代替從word文檔中粘貼過來的内容 plugins/pastefromword/plugin.js

    config.pasteFromWordKeepsStructure = false;

    //從word中粘貼内容時是否移除格式 plugins/pastefromword/plugin.js

    config.pasteFromWordRemoveStyle = false;

    //對應背景語言的類型來對輸出的HTML内容進行格式化,預設為空

    config.protectedSource.push( /<"?["s"S]*?"?>/g );   // PHP Code

    config.protectedSource.push( //g );   // ASP Code

    config.protectedSource.push( /(]+>["s|"S]*?<"/asp:[^">]+>)|(]+"/>)/gi );   // ASP.Net Code

    //當輸入:shift+Enter時插入的标簽

    config.shiftEnterMode = CKEDITOR.ENTER_P;  //可選:CKEDITOR.ENTER_BR或CKEDITOR.ENTER_DIV

    //可選的表情替代字元 plugins/smiley/plugin.js.

    config.smiley_descriptions = [

        ':)', ':(', ';)', ':D', ':/', ':P',

        '', '', '', '', '', '',

        '', ';(', '', '', '', '',

        '', ':kiss', '' ];

    //對應的表情圖檔 plugins/smiley/plugin.js

    config.smiley_images = [

        'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','confused_smile.gif','tounge_smile.gif',

        'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angry_smile.gif','angel_smile.gif','shades_smile.gif',

        'devil_smile.gif','cry_smile.gif','lightbulb.gif','thumbs_down.gif','thumbs_up.gif','heart.gif',

        'broken_heart.gif','kiss.gif','envelope.gif'];

    //表情的位址 plugins/smiley/plugin.js

    config.smiley_path = 'plugins/smiley/images/';

    //頁面載入時,編輯框是否立即獲得焦點 plugins/editingblock/plugin.js plugins/editingblock/plugin.js.

    config.startupFocus = false;

    //載入時,以何種方式編輯 源碼和所見即所得 "source"和"wysiwyg" plugins/editingblock/plugin.js.

    config.startupMode ='wysiwyg';

    //載入時,是否顯示框體的邊框 plugins/showblocks/plugin.js

    config.startupOutlineBlocks = false;

    //是否載入樣式檔案 plugins/stylescombo/plugin.js.

    config.stylesCombo_stylesSet = 'default';

    //以下為可選

    config.stylesCombo_stylesSet = 'mystyles';

    config.stylesCombo_stylesSet = 'mystyles:/editorstyles/styles.js';

    config.stylesCombo_stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';

    //起始的索引值

    config.tabIndex = 0;

    //當使用者鍵入TAB時,編輯器走過的空格數,(&nbsp;) 當值為0時,焦點将移出編輯框 plugins/tab/plugin.js

    config.tabSpaces = 0;

    //預設使用的模闆 plugins/templates/plugin.js.

    config.templates = 'default';

    //用逗号分隔的模闆檔案plugins/templates/plugin.js.

    config.templates_files = [ 'plugins/templates/templates/default.js' ]

    //當使用模闆時,“編輯内容将被替換”框是否選中 plugins/templates/plugin.js

    config.templates_replaceContent = true;

    //主題

    config.theme = 'default';

    //撤銷的記錄步數 plugins/undo/plugin.js

    config.undoStackSize =20;

    // 在 CKEditor 中內建 CKFinder,注意 ckfinder 的路徑選擇要正确。

    //CKFinder.SetupCKEditor(null, '/ckfinder/');

三、精簡ckeditor

    在部署到Web伺服器上時,下列檔案夾和檔案都可以删除:

    /_samples :示例檔案夾;

    /_source :未壓縮源程式;

    /lang檔案夾下除 zh-cn.js、en.js 以外的檔案(也可以根據需要保留其他語言檔案);

    根目錄下的 changes.html(更新清單),install.html(安裝指向),license.html(使用許可);

    /skins 目錄下不需要的皮膚,一般用V2(簡單,樸素) ,如果隻保留V2則必須在config.js中指定皮膚。

四, 版本問題

    在CKEditor的官方網站上下載下傳,最要下載下傳全部功能,最後得到的壓縮包是ckeditor_4.1.1_full.zip,如果選擇simple或者standed,可能會有些功能不能用,比如字型顔色,背景顔色,這下功能在standed版本裡面就不支援。是以你會很奇怪明明設定了['TextColor','BGColor'],但是文本顔色和背景顔色選項就是不顯示。

五,和kcfinder搭配使用

預設情況下CKEditor是沒有上傳圖檔功能的,可以在編輯器中粘貼圖檔,這樣的話儲存在資料庫中的是圖檔的源檔案,這很容易出現問題,即使字段是text格式的也很容易就超出最大範圍了,是以要配合kcfinder來上傳檔案。一個是ck一個是kc真實一對好兄弟。

在配置CKEditor的配置檔案ckeditor\config.js中添加以下配置

富文本編輯器 CKeditor 的使用
config.filebrowserBrowseUrl = 'kcfinder/browse.php?type=files';  
config.filebrowserImageBrowseUrl = 'kcfinder/browse.php?type=images';  
config.filebrowserFlashBrowseUrl = 'kcfinder/browse.php?type=flash';  
config.filebrowserUploadUrl = 'kcfinder/upload.php?type=files';  
config.filebrowserImageUploadUrl = 'kcfinder/upload.php?type=images';  
config.filebrowserFlashUploadUrl = 'kcfinder/upload.php?type=flash';      
富文本編輯器 CKeditor 的使用

要注意如果kcfinder的路徑放在CKEditor的同一級目錄下不能這樣

config.filebrowserBrowseUrl = '/kcfinder/browse.php?type=files';      

要注意為安全考慮預設情況下是不允許上傳圖檔的,在自己的源代碼中要設定可以上傳,代碼如下

富文本編輯器 CKeditor 的使用
session_start();
if(!empty($_SESSION['USERNAME'])&&!empty($_SESSION['PASSWORD'])){
$_SESSION['KCFINDER']['disabled'] = false;
}else{
echo "<script>top.location.href="http://xxx.com/login.php" target="_blank" rel="external nofollow" > 
}      
富文本編輯器 CKeditor 的使用

這個就是判斷是否是登陸使用者,不是登陸使用者的話不可以使用,并且跳轉到登陸界面去登陸,網上有人說把這個放在kcfinder\config.php這個kcfinder的配置檔案中我發現這個是行不通的。這樣設定之後在選擇上傳圖檔的時候會有錯誤,如下

富文本編輯器 CKeditor 的使用

繼續閱讀