PHPCMS V9 內置 CKEditor 4,默認配置功能較基礎。通過修改核心配置文件與插件,可顯著提升編輯體驗:批量清除超鏈接、圖片上傳自動填充 Alt、調整 Enter/Shift+Enter 行為、添加「一鍵排版」按鈕。
| 功能 | 修改文件 | 核心變更 |
|---|---|---|
| 批量去除超鏈接 | statics/js/ckeditor/ckeditor.js | `execCommand('unlink', false, true)` 第三參數設為 true |
| 圖片自動填充 Alt | statics/js/ckeditor/plugins/image/dialogs/image.js | Alt 輸入框 default 值綁定 `$('#title').val()` |
| 調整換行模式 | statics/js/ckeditor/config.js | `enterMode: CKEDITOR.ENTER_P`, `shiftEnterMode: CKEDITOR.ENTER_BR` |
| 添加一鍵排版按鈕 | phpcms/libs/classes/form.class.php + config.js | 工具欄添加 `autoformat` 插件,啟用 `extraPlugins` |
文件: `statics/js/ckeditor/ckeditor.js` (壓縮版,建議先美化格式)
搜尋: `$.execCommand('unlink',false,null)`
替換為:
$.execCommand('unlink', false, true);
原理: CKEditor `unlink` 命令第三參數 `true` 表示「移除所有選區內的鏈接」,`false`/`null` 僅移除光標處單個鏈接。
文件: `statics/js/ckeditor/plugins/image/dialogs/image.js`
搜尋: `accessKey:'T','default':''` (在 `alt` 輸入框定義處)
替換為:
accessKey: 'T', 'default': $('#title').val()
注意: 此處 `$('#title').val()` 需在對話框 DOM 就緒後執行,若無效請改用 CKEditor 對話框 API:
// 更穩健的寫法 (在 dialogDefinition 事件中)
CKEDITOR.on('dialogDefinition', function(evt) {
if (evt.data.name === 'image') {
var infoTab = evt.data.definition.getContents('info');
var altField = infoTab.get('alt');
altField.setup = function(widget) {
this.setValue(widget.data.title || '');
};
}
});
文件: `statics/js/ckeditor/config.js`
原配置:
config.enterMode = CKEDITOR.ENTER_BR; // Enter ->
config.shiftEnterMode = CKEDITOR.ENTER_P; // Shift+Enter ->
修改為 (符合 Word/WordPress 習慣):
config.enterMode = CKEDITOR.ENTER_P; // Enter -> (新段落)
config.shiftEnterMode = CKEDITOR.ENTER_BR; // Shift+Enter ->
(換行)
說明:
` 標籤,語義化段落,利於 SEO 與樣式控制。
步驟 A:註冊工具欄按鈕
文件: `phpcms/libs/classes/form.class.php`
搜尋: `['Maximize']` (工具欄配置數組)
在其下方添加:
['AutoFormat']
完整工具欄片段示例:
$toolbar = [
['Source', '-', 'Save', 'NewPage', 'Preview', '-', 'Templates'],
['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker', 'Scayt'],
['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
['Maximize'],
['AutoFormat'], // <-- 新增
'/',
['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
// ... 其餘按鈕
];
步驟 B:啟用 autoformat 插件
文件: `statics/js/ckeditor/config.js`
搜尋: `config.extraPlugins = 'capture';`
修改為:
config.extraPlugins = 'capture,autoformat';
前提: 確保 `statics/js/ckeditor/plugins/autoformat/` 目錄存在 (含 `plugin.js`)。若無,需從 CKEditor 官網插件庫 下載對應版本放入。
通過四處精準修改,CKEditor 4 即可獲得符合現代編輯習慣的核心體驗升級。建議將修改封裝為獨立插件或配置文件,納入版本控制,便於未來框架升級時快速遷移。若專案允許,升級至 CKEditor 5 (重寫架構、模塊化、TypeScript) 將獲得更長遠的維護保障。