DIV CSS 佈局教程網

CKEditor 與 CKFinder 的安裝配置完整指南
編輯:JavaScript基礎知識     

CKEditor 是一套成熟的可視化網頁編輯器,前身是 FCKeditor。它純以 JavaScript 實作,不需要在伺服器端額外參考 DLL,這讓整合成本大幅降低。以下整理從安裝、常用配置到部署精簡的完整流程。

一、基本使用步驟

1. 引入核心檔案

在頁面的 <head> 中加入 CKEditor 的核心 js:

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

2. 放入 textarea 或伺服器端控制項

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

ASP.NET 環境下也可以用伺服器端控制項:

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

重點是記得加上 class="ckeditor"。

3. 將控制項替換成編輯器

<script type="text/javascript">
CKEDITOR.replace('TextArea1');
</script>

ASP.NET 的 TextBox 則用 ClientID。若控制項位於母版頁中,名稱會帶有前綴,此時需要處理:

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

二、config.js 常用配置參數

所有設定集中在 ckeditor/config.js。以下依功能分類列出實務上最常調整的項目。

介面外觀

config.language = 'zh-cn';   // 介面語言,預設 en
config.width = 400;
config.height = 400;
config.skin = 'v2';          // 樣式:kama(預設)、office2003、v2
config.uiColor = '#FFF';     // 背景顏色
config.contentsCss = './contents.css'; // 編輯區的 CSS 檔
config.contentsLangDirection = 'ltr';  // 文字方向
config.docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
config.fullPage = false;     // 是否使用完整 HTML 編輯模式

工具列

config.toolbar = 'Basic';   // 基礎、全能 Full,或自訂
config.toolbarCanCollapse = true;
config.toolbarLocation = 'top';        // 可選 bottom
config.toolbarStartupExpanded = true;

自訂工具列時搭配 config.toolbar_Full 這類陣列定義,用 '/' 表示換行,用 '-' 表示分隔線。

尺寸調整

config.resize_enabled = false;
config.resize_maxHeight = 3000;
config.resize_maxWidth = 3000;
config.resize_minHeight = 250;
config.resize_minWidth = 750;
config.disableObjectResizing = false; // 是否允許縮放圖片與表格

快捷鍵

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' ]
];

// 與瀏覽器快捷鍵衝突時,可停用某些組合
config.blockedKeystrokes = [
  CKEDITOR.CTRL + 66,
  CKEDITOR.CTRL + 73,
  CKEDITOR.CTRL + 85
];

顏色與字型

config.colorButton_colors = '000,800000,8B4513,2F4F4F,008080,...';
config.colorButton_enableMore = false; // 是否顯示「其他顏色」
config.font_names = 'Arial;Times New Roman;Verdana;'
  + '宋體;楷體;黑體;微軟雅黑';
config.font_defaultLabel = 'Arial';
config.fontSize_defaultLabel = '12px';
config.fontSize_sizes = '8/8px;9/9px;10/10px;12/12px;14/14px;16/16px;18/18px';

貼上與格式

config.forcePasteAsPlainText = false;      // 是否強制純文字貼上
config.pasteFromWordIgnoreFontFace = true; // 從 Word 貼上時忽略字型
config.pasteFromWordRemoveStyle = false;
config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';
config.enterMode = CKEDITOR.ENTER_P;       // 也可 ENTER_BR 或 ENTER_DIV
config.shiftEnterMode = CKEDITOR.ENTER_P;

若需要保護後端語言的程式碼區塊不被編輯器處理,可加入 protectedSource 規則(PHP、ASP、ASP.NET 各有對應寫法)。

其他實用項目

config.startupMode = 'wysiwyg';   // 或 'source'
config.startupFocus = false;
config.undoStackSize = 20;
config.tabSpaces = 0;             // 0 表示 Tab 讓焦點移出編輯框
config.autoUpdateElement = true;  // 表單送出時自動同步內容
config.baseFloatZIndex = 10000;
config.newpage_html = '';
config.templates = 'default';
config.templates_replaceContent = true;

三、在頁面中即時設定

除了改 config.js,也可以在呼叫 replace 時直接傳入設定物件,適合每個欄位需要不同工具列的情況:

<script type="text/javascript">
// 基本工具列、高度 70
CKEDITOR.replace('<%= tbLink.ClientID.Replace("_", "$") %>', {
  toolbar: 'Basic',
  height: 70
});

// 完全自訂工具列
CKEDITOR.replace('editor1', {
  toolbar: [
    ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript'],
    ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'],
    ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
    ['Link', 'Unlink', 'Anchor'],
    ['Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'],
    '/',
    ['Styles', 'Format', 'Font', 'FontSize'],
    ['TextColor', 'BGColor'],
    ['Maximize', 'ShowBlocks', '-']
  ]
});
</script>

四、整合 CKFinder 檔案上傳

CKEditor 本身不含上傳功能,這部分由另一個產品 CKFinder 負責。整合步驟如下:

第一,把 CKFinder 放到網站合適的目錄(例如根目錄下的 /ckfinder/)。

第二,開啟 CKFinder 的 config.aspx.cs(或 config 設定檔),把驗證方法改為允許:

public override bool CheckAuthentication()
{
    return true;
}

務必注意:這段設定代表「關閉存取驗證」。正式環境中請替換成真正的身分檢查邏輯,否則任何人都能透過檔案瀏覽器存取伺服器上的檔案,這是相當嚴重的安全漏洞。

第三,在 ckeditor/config.js 中設定各類瀏覽與上傳的端點:

CKEDITOR.editorConfig = function (config) {
  config.filebrowserBrowseUrl      = '/ckfinder/ckfinder.html';
  config.filebrowserImageBrowseUrl = '/ckfinder/ckfinder.html?Type=Images';
  config.filebrowserFlashBrowseUrl = '/ckfinder/ckfinder.html?Type=Flash';

  config.filebrowserUploadUrl      = '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';
  config.filebrowserImageUploadUrl = '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images';
  config.filebrowserFlashUploadUrl = '/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash';
};

路徑最容易出錯。建議一律使用「從網站根目錄起算」的絕對路徑(以 / 開頭),不要用相對路徑,否則在子頁面或巢狀目錄中會出現找不到資源的錯誤。

授權提醒:CKFinder 是商業授權的軟體,正式商用請向官方購買授權序號。取得合法授權後,除了能避免法律風險,也能獲得官方技術支援,長期反而更省成本。

五、部署前的精簡

上線前可以刪除下列檔案與目錄,有效縮小體積:

  • /_samples:範例目錄
  • /_source:未壓縮的原始碼
  • /lang 下除 zh-cn.js、en.js 以外的語言檔(依需求保留)
  • 根目錄的 changes.html、install.html、license.html
  • /skins 下不需要的佈景主題。若只保留 v2,記得在 config.js 中明確指定 config.skin = 'v2'

相較於舊的 FCKeditor,CKEditor 省下了伺服器端類庫的參考,直接在頁面用 js 就能運作,整合與部署都單純許多。

最後提醒:CKEditor 的「所見即所得」只是前端呈現,內容仍應在伺服器端做完整的 HTML 淨化(sanitize)與過濾,才能防範 XSS。永遠不要信任從瀏覽器送出的任何資料。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved