CKEditor添加一鍵排版插件實例,
大家都知道phpcms也是ckeditor編輯器,那么如果增加這個一鍵排版這個牛逼功能呢
增加好了后,效果圖是這樣的
廢話不多說,直接說步驟
第一步:config.js中statics\js\ckeditor\config.js中注冊autoformat控件
config.extraPlugins = 'capture,videoforpc,flashplayer,autoformat';
第二步,在statics\js\ckeditor\plugins 新建文件夾autoformat
第三步,在statics\js\ckeditor\plugins\autoformat新建文件plugin.js
寫入如下內容
1 (function() { 2 CKEDITOR.plugins.add('autoformat', { 3 requires: ['styles', 'button'], 4 init: function(a) { 5 a.addCommand('autoformat', CKEDITOR.plugins.autoformat.commands.autoformat); 6 a.ui.addButton('autoformat', { 7 label: "清除格式,一鍵排版", 8 command: 'autoformat', 9 //這個autoformat.gif是你的插件圖標,放在同目錄下 10 icon: this.path + "autoformat.gif" 11 }); 12 } 13 }); 14 CKEDITOR.plugins.autoformat = { 15 commands: { 16 autoformat: { 17 exec: function(a) { 18 var _html = a.getData(); 19 //清除樣式代碼 20 _html = _html.replace(/<div/ig, '<p'); 21 _html = _html.replace(/<\/div>/ig, '</p>'); 22 _html = _html.replace(/<strong[^>]*>/ig, ''); 23 _html = _html.replace(/<\/strong>/ig, ''); 24 _html = _html.replace(/<em[^>]*>/ig, ''); 25 _html = _html.replace(/<\/em>/ig, ''); 26 _html = _html.replace(/<u[^>]*>/ig, ''); 27 _html = _html.replace(/<\/u>/, ''); 28 _html = _html.replace(/<li[^>]*>/ig, ''); 29 _html = _html.replace(/<\/li>/ig, ''); 30 _html = _html.replace(/<span[^>]*>/ig, ''); 31 _html = _html.replace(/<\/span>/ig, ''); 32 _html = _html.replace(/ /ig, ''); 33 _html = _html.replace(/ /ig, ''); 34 _html = _html.replace(/<p><\/p>/ig, ''); 35 _html = _html.replace(/<a/ig, '<a rel="nofollow"'); 36 37 38 //將p標簽替換成<br /> 39 _html = _html.replace(/<p[^>]*>/ig, ''); 40 _html = _html.replace(/<\/p>/ig, '<br />'); 41 _html = _html.replace(/<br \/><br \/>/ig, '<br />'); 42 _html = _html.replace(/[\n]/ig, ''); 43 44 //按<br />分組,將換行<br>全部替換成p標簽 45 bb = _html.split("<br />"); 46 aa = ''; 47 for (var i = 0; i < bb.length; i++) { 48 aa = aa + '<p>' + bb[i] + '</p>'; 49 } 50 51 //首行縮進 52 _html = aa.replace(/<p[^>]*>/ig, '<p> '); 53 _html = _html.replace(/<p> <\/p>/ig, ''); 54 _html = _html.replace(/<p><\/p>/ig, ''); 55 56 //在這里執行你將_html中的空行替換掉的操作 57 a.setData(_html); 58 } 59 } 60 } 61 }; 62 })();
寫到這里,就完成啦,完成了CKEditor添加一鍵排版插件
但是,到這里再phpcms里面,還是不能直接用的,在別的系統里面是可以的。因為phpcms的編輯器控件是需要單獨選擇的,還需要修改phpcms文件
打開phpcms/libs/classes/form.class.php
搜索['Maximize'] 在它的后面加上 ['autoformat'],就可以了。
轉自:http://www.wfuyu.com/biji/25066.html
原文是程序員人生。