CKEditor擴展插件:自動排版功能


 

CKEditor是新一代的FCKeditor,是一個重新開發的版本。CKEditor是全球最優秀的網頁在線文字編輯器之一,因其驚人的性能與可擴展性而廣泛的被運用於各大網站。

如果還沒接觸過的可以看看,在線演示地址:http://ckeditor.com/demo

當然了,今天我們的主要目的還不是介紹。

還未下載CKEditor的同學可以點擊下載:http://ckeditor.com/download

下載完后的結構是這樣的:

image

 

好了,開始制作了,我們照着執行順序來吧。

1.注冊插件

首先找到根目錄下的config.js文件,打開文件如下:

CKEDITOR.editorConfig = function (config) {
    // Define changes to default configuration here. For example:
    // config.language = 'fr';
    // config.uiColor = '#AADC6E';
};

 

我們需要將我們的插件注冊進CKEDITOR中。

在方法內部加入如下代碼:

 config.extraPlugins = "autoformart";
 

2.創建Plugin.js文件

在Plugins文件下新建一個與插件名相同的文件夾:aotuformart 的文件夾,意為自動排版。

再在文件夾內創建一個plugin.js文件,因為在注冊插件后,首先加載和執行的就是plugin.js這個文件。

首先我們構建一個自執行函數,在自執行函數中添加一個插件:

1 (function()
2 {
3     CKEDITOR.plugins.add('autoformat',{
4         init:function(editor){
5         //初始化操作
6         }
7     });
8 })();

添加一個命令和按鈕在初始化函數中,如下:

 

1 (function()
2 {
3     CKEDITOR.plugins.add('autoformat',{
4         init:function(editor){
5             editor.addCommand( 'autoformat', new CKEDITOR.autoformatCommand());
6             editor.ui.addButton('Autoformat',{label:'自動排版',command:'autoformat',icon:CKEDITOR.getUrl( this.path + 'images/autoformat.gif' )});
7         }
8     });
9 })();

 

 

addCommand方法有兩個參數:插件命令名稱,第二個是命令執行的方法。

addButton方法的第一個參數是:插件的按鈕名稱

    label:鼠標懸浮時插件提示

    command:執行插件命令的名稱

    icon:插件圖標

好了,直接放出所有代碼吧

  1 //一鍵排版
  2 (function () {
  3     CKEDITOR.plugins.add('autoformat', {
  4         requires: ['styles', 'button'],
  5         init: function (a) {
  6             a.addCommand('autoformat', CKEDITOR.plugins.autoformat.commands.autoformat);
  7             a.ui.addButton('autoformat', {
  8                 label: "一鍵排版",
  9                 command: 'autoformat',
 10                 icon: this.path + "images/autoformat.gif"
 11             });
 12         }
 13     });
 14     CKEDITOR.plugins.autoformat = {
 15         commands: {
 16             autoformat: {
 17                 exec: function (editor) {
 18                     formatText(editor);
 19                 }
 20             }
 21         }
 22     };
 23 
 24     //格式化
 25     function formatText(editor) {
 26         var myeditor = editor;
 27         if (myeditor.mode == "wysiwyg") {
 28             var tempimg = new Array();
 29             var temptable = new Array();
 30             var tempobject = new Array();
 31 
 32             var isPart = false; //暫時無法實現局部格式化
 33             if (!isPart) {
 34                 var tmpDiv = document.createElement("DIV");
 35                 var editorhtml = myeditor.getData();
 36                 editorhtml = editorhtml.replace(/<div style="page-break-after: always;?">\s*<span style="display: none;?">&nbsp;<\/span>\s*<\/div>/gi, '<p>[page]</p>');  //將div span標簽替換為p 標簽
 37                 tmpDiv.innerHTML = editorhtml.replace(/&nbsp;/gi, '').replace(/<div/gi, '<p').replace(/<\/div/gi, '</p');     //移除空格標簽,div替換為p標簽。
 38                 if (window.navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
 39                     tmpDiv.innerHTML = tmpDiv.innerHTML.replace(/<\/p>/gi, '<br /><\/p>');      //每個段落相隔一行
 40                 }
 41                 var tables = tmpDiv.getElementsByTagName("TABLE");
 42                 if (tables != null && tables.length > 0) {
 43                     for (var j = 0; j < tables.length; j++) {
 44                         temptable[temptable.length] = tables[j].outerHTML;
 45                     }
 46                     var formattableCount = 0;
 47                     for (var j = 0; j < tables.length;) {
 48                         tables[j].outerHTML = "#FormatTableID_" + formattableCount + "#";
 49                         formattableCount++;
 50                     }
 51                 }
 52 
 53                 var objects = tmpDiv.getElementsByTagName("OBJECT");
 54                 if (objects != null && objects.length > 0) {
 55                     for (var j = 0; j < objects.length; j++) {
 56                         tempobject[tempobject.length] = objects[j].outerHTML;
 57                     }
 58                     var formatobjectCount = 0;
 59                     for (var j = 0; j < objects.length;) {
 60                         objects[j].outerHTML = "#FormatObjectID_" + formatobjectCount + "#";
 61                         formatobjectCount++;
 62                     }
 63                 }
 64 
 65                 var imgs = tmpDiv.getElementsByTagName("IMG");
 66                 if (imgs != null && imgs.length > 0) {
 67                     for (var j = 0; j < imgs.length; j++) {
 68                         var t = document.createElement("IMG");
 69                         t.alt = imgs[j].alt;
 70                         t.src = imgs[j].src;
 71                         t.width = imgs[j].width;
 72                         t.height = imgs[j].height;
 73                         t.align = imgs[j].align;
 74                         tempimg[tempimg.length] = t;
 75                     }
 76                     var formatImgCount = 0;
 77                     for (var j = 0; j < imgs.length;) {
 78                         imgs[j].outerHTML = "#FormatImgID_" + formatImgCount + "#";
 79                         formatImgCount++;
 80                     }
 81                 }
 82 
 83                 var strongarray = new Array();
 84                 var strongcount = 0;
 85                 for (var i = 0; i < tmpDiv.getElementsByTagName('b').length; i++) {
 86                     strongarray[strongcount] = tmpDiv.getElementsByTagName('b')[i].innerText.trim();
 87                     tmpDiv.getElementsByTagName('b')[i].innerHTML = "#FormatStrongID_" + strongcount + "#";
 88                     strongcount++;
 89                 }
 90 
 91                 for (var i = 0; i < tmpDiv.getElementsByTagName('strong').length; i++) {
 92                     strongarray[strongcount] = tmpDiv.getElementsByTagName('strong')[i].innerText.trim();
 93                     tmpDiv.getElementsByTagName('strong')[i].innerHTML = "#FormatStrongID_" + strongcount + "#";
 94                     strongcount++;
 95                 }
 96                 var html = processFormatText(tmpDiv.innerText);
 97                 html = html.replace(/<p>\[page\]<\/p>/gi, '<div style="page-break-after: always;"><span style="display: none;">&nbsp;</span></div>');   //p標簽替換回原來的div和span標簽。
 98                 if (temptable != null && temptable.length > 0) {
 99                     for (var j = 0; j < temptable.length; j++) {
100                         var tablehtml = temptable[j];
101                         html = html.replace("#FormatTableID_" + j + "#", tablehtml);
102                     }
103                 }
104 
105                 if (tempobject != null && tempobject.length > 0) {
106                     for (var j = 0; j < tempobject.length; j++) {
107                         var objecthtml = "<p align=\"center\">" + tempobject[j] + "</p>";
108                         html = html.replace("#FormatObjectID_" + j + "#", objecthtml);
109                     }
110                 }
111 
112                 if (tempimg != null && tempimg.length > 0) {
113                     for (var j = 0; j < tempimg.length; j++) {
114                         var imgheight = "";
115                         var imgwidth = "";
116                         if (tempimg[j].height != 0)
117                             imgheight = " height=\"" + tempimg[j].height + "\"";
118                         if (tempimg[j].width != 0)
119                             imgwidth = " width=\"" + tempimg[j].width + "\"";
120                         var imgalign = "";
121                         if (tempimg[j].align != "")
122                             imgalign = " align=\"" + tempimg[j].align + "\"";
123                         var imghtml = "<p align=\"center\"><img src=\"" + tempimg[j].src + "\" alt=\"" + tempimg[j].alt + "\"" + imgwidth + " " + imgheight + " align=\"" + tempimg[j].align + "\" border=\"0\"></p>";
124                         html = html.replace("#FormatImgID_" + j + "#", imghtml);
125                     }
126                 }
127 
128                 for (var i = 0; i < strongcount; i++) {
129                     html = html.replace("#FormatStrongID_" + i + "#", "<p><strong>" + strongarray[i] + "</strong></p>");
130                 }
131 
132                 while (html.indexOf("</p></p>") != -1) html = html.replace("</p></p>", "</p>");
133                 while (html.indexOf('<p><p align="center">') != -1) html = html.replace('<p><p align="center">', '<p align="center">');
134                 editor.setData(html);
135 
136             } else {
137 
138             }
139         } else {
140             alert('必須在設計模式下操作!');
141         }
142     }
143 
144     function processFormatText(textContext) {
145         var text = dbc2Sbc(textContext);
146         var prefix = "";
147         var tmps = text.split("\n");
148         var html = "";
149         for (var i = 0; i < tmps.length; i++) {
150             var tmp = tmps[i].trim();
151             if (tmp.length > 0) {
152                 var reg = /#Format[A-Za-z]+_\d+#/gi;
153                 var f = reg.exec(tmp);
154                 if (f != null) {
155                     tmp = tmp.replace(/#Format[A-Za-z]+_\d+#/gi, '');
156                     html += f;
157                     if (tmp != "")
158                         html += "<p align=\"center\">" + tmp + "</p>\n";
159                 } else {
160                     html += "<p style='text-indent:2em;'>" + tmp + "</p>\n";
161                 }
162             }
163         }
164         return html;
165     }
166 
167     function dbc2Sbc(str) {
168         var result = '';
169         for (var i = 0; i < str.length; i++) {
170             var code = str.charCodeAt(i);
171             // “65281”是“!”,“65373”是“}”,“65292”是“,”。不轉換","
172 
173             if (code >= 65281 && code < 65373 && code != 65292 && code != 65306) {
174                 //  “65248”是轉換碼距
175                 result += String.fromCharCode(str.charCodeAt(i) - 65248);
176             } else {
177                 result += str.charAt(i);
178             }
179         }
180         return result;
181     }
182 
183     String.prototype.trim = function () {
184         return this.replace(/(^[\s ]*)|([\s ]*$)/g, "");
185     };
186 
187     String.prototype.leftTrim = function () {
188         return this.replace(/(^\s*)/g, "");
189     };
190 
191     String.prototype.rightTrim = function () {
192         return this.replace(/(\s*$)/g, "");
193     };
194 })();
Plugin.js

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM