CKEditor(原 FCKeditor)是功能強大的所見即所得網頁編輯器,支援多種程式語言後端。本文提供 Linux 環境下從 CDN/本地引入、基礎替換、工具列自訂、語言/尺寸/外觀配置、快捷鍵設定、內容 CSS 注入等完整整合指南,適用於 PHP、Java、Python、ASP.NET 等專案。
<script src="ckeditor/ckeditor.js"></script>
或使用 CDN://cdn.ckeditor.com/4.21.0/standard/ckeditor.js
<textarea id="editor1" name="content" class="ckeditor"></textarea>
ASP.NET 伺服器控制項:
<asp:TextBox ID="tbContent" runat="server" TextMode="MultiLine" CssClass="ckeditor" />
<script>
CKEDITOR.replace('editor1'); // 對應 textarea id
// ASP.NET 控制項需用 ClientID
CKEDITOR.replace('<%=tbContent.ClientID%>');
// 母版頁中 ClientID 含底線,需替換
CKEDITOR.replace('<%=tbContent.ClientID.Replace("_","$")%>');
</script>
配置檔位於 ckeditor/config.js,所有設定寫在 CKEDITOR.editorConfig = function(config) { ... } 內。
config.language = 'zh-tw'; // 繁體中文
config.width = 800;
config.height = 500;
config.skin = 'moono-lisa'; // 預設現代風格,可選 'kama', 'office2013', 自訂皮膚
config.uiColor = '#F7F7F7'; // 編輯區背景色
// 使用預設工具列
config.toolbar = 'Full';
// 自訂工具列(推薦:精簡常用功能)
config.toolbar = 'Custom';
config.toolbar_Custom = [
{ name: 'document', items: ['Source', '-', 'Save', 'NewPage', 'Preview', '-', 'Templates'] },
{ name: 'clipboard', items: ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'] },
{ name: 'editing', items: ['Find', 'Replace', '-', 'SelectAll', '-', 'Scayt'] },
{ name: 'forms', items: ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'] },
'/', // 換行
{ name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'] },
{ name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl', 'Language'] },
{ name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
{ name: 'insert', items: ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe'] },
'/',
{ name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] },
{ name: 'colors', items: ['TextColor', 'BGColor'] },
{ name: 'tools', items: ['Maximize', 'ShowBlocks'] },
{ name: 'about', items: ['About'] }
];
config.toolbarCanCollapse = true; // 允許摺疊
config.toolbarLocation = 'top'; // 'top' 或 'bottom'
config.toolbarStartupExpanded = true; // 預設展開
config.resize_enabled = true;
config.resize_maxWidth = 1200;
config.resize_minWidth = 400;
config.resize_maxHeight = 800;
config.resize_minHeight = 200;
config.contentsCss = ['/css/ckeditor-contents.css', '/css/bootstrap.min.css'];
// 讓編輯區套用網站前台樣式,所見即所得
config.keystrokes = [
[CKEDITOR.CTRL + 83, 'save'], // Ctrl+S 儲存
[CKEDITOR.CTRL + 90, 'undo'], // Ctrl+Z 撤銷
[CKEDITOR.CTRL + 89, 'redo'], // Ctrl+Y 重做
[CKEDITOR.CTRL + 76, 'link'], // Ctrl+L 連結
[CKEDITOR.CTRL + 66, 'bold'], // Ctrl+B 粗體
[CKEDITOR.CTRL + 73, 'italic'], // Ctrl+I 斜體
[CKEDITOR.CTRL + 85, 'underline'] // Ctrl+U 底線
];
config.blockedKeystrokes = [
CKEDITOR.CTRL + 66, // 避免與瀏覽器書籤衝突
CKEDITOR.CTRL + 73,
CKEDITOR.CTRL + 85
];
config.colorButton_colors = '000,800000,FF0000,008000,0000FF,FFFFFF,FFFF00,00FFFF,FF00FF';
config.colorButton_enableMore = true; // 顯示「其他顏色」
config.colorButton_foreStyle = { element: 'span', styles: { 'color': '#(color)' } };
config.colorButton_backStyle = { element: 'span', styles: { 'background-color': '#(color)' } };
config.filebrowserUploadUrl 指向後端上傳 API,回傳 JSON { "uploaded": 1, "fileName": "xxx", "url": "..." }。textarea 值,或手動呼叫 CKEDITOR.instances.editor1.updateElement()。| 版本 | 狀態 | 建議 |
|---|---|---|
| CKEditor 4 (LTS) | 維護模式 | 穩定專案、舊專案維護 |
| CKEditor 5 | 主力開發 | 新專案、現代框架、協同編輯需求 |
本文配置針對 CKEditor 4,CKEditor 5 架構全面重寫,配置方式不同(模組化、TypeScript、UI 框架整合)。