* Removed the Kevin Roth rich text editor and replaced it with TinyMCE.
[citadel.git] / webcit / tiny_mce / plugins / paste / editor_plugin_src.js
1 /* Import plugin specific language pack */ 
2 tinyMCE.importPluginLanguagePack('paste', 'en,sv,cs,zh_cn,fr_ca'); 
3
4 function TinyMCE_paste_getInfo() {
5         return {
6                 longname : 'Paste text/word',
7                 author : 'Moxiecode Systems',
8                 authorurl : 'http://tinymce.moxiecode.com',
9                 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_paste.html',
10                 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
11         };
12 };
13
14 function TinyMCE_paste_initInstance(inst) {
15         if (tinyMCE.isMSIE && tinyMCE.getParam("paste_auto_cleanup_on_paste", false))
16                 tinyMCE.addEvent(inst.getBody(), "paste", TinyMCE_paste_handleEvent);
17 }
18
19 function TinyMCE_paste_handleEvent(e) {
20         switch (e.type) {
21                 case "paste":
22                         var html = TinyMCE_paste__clipboardHTML();
23
24                         // Removes italic, strong etc
25                         tinyMCE.execCommand('delete');
26
27                         if (html && html.length > 0)
28                                 tinyMCE.execCommand('mcePasteWord', false, html);
29
30                         tinyMCE.cancelEvent(e);
31                         return false;
32         }
33
34         return true;
35 }
36
37 function TinyMCE_paste_getControlHTML(control_name) { 
38         switch (control_name) { 
39                 case "pastetext": 
40                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcePasteText\', true);" onmousedown="return false;"><img id="{$editor_id}pastetext" src="{$pluginurl}/images/pastetext.gif" title="{$lang_paste_text_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>'; 
41
42                 case "pasteword": 
43                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mcePasteWord\', true);" onmousedown="return false;"><img id="{$editor_id}pasteword" src="{$pluginurl}/images/pasteword.gif" title="{$lang_paste_word_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>'; 
44
45                 case "selectall": 
46                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceSelectAll\');" onmousedown="return false;"><img id="{$editor_id}selectall" src="{$pluginurl}/images/selectall.gif" title="{$lang_selectall_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreClass(this);" /></a>'; 
47         } 
48
49         return ''; 
50
51
52 function TinyMCE_paste_execCommand(editor_id, element, command, user_interface, value) { 
53         switch (command) { 
54                 case "mcePasteText": 
55                         if (user_interface) {
56                                 if (tinyMCE.isMSIE && !tinyMCE.getParam('paste_use_dialog', false))
57                                         TinyMCE_paste__insertText(clipboardData.getData("Text"), true); 
58                                 else { 
59                                         var template = new Array(); 
60                                         template['file']        = '../../plugins/paste/pastetext.htm'; // Relative to theme 
61                                         template['width']  = 450; 
62                                         template['height'] = 400; 
63                                         var plain_text = ""; 
64                                         tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'}); 
65                                 }
66                         } else
67                                 TinyMCE_paste__insertText(value['html'], value['linebreaks']);
68
69                         return true;
70
71                 case "mcePasteWord": 
72                         if (user_interface) {
73                                 if (tinyMCE.isMSIE && !tinyMCE.getParam('paste_use_dialog', false)) {
74                                         var html = TinyMCE_paste__clipboardHTML();
75
76                                         if (html && html.length > 0)
77                                                 TinyMCE_paste__insertWordContent(html);
78                                 } else { 
79                                         var template = new Array(); 
80                                         template['file']        = '../../plugins/paste/pasteword.htm'; // Relative to theme 
81                                         template['width']  = 450; 
82                                         template['height'] = 400; 
83                                         var plain_text = ""; 
84                                         tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'});
85                                 }
86                         } else
87                                 TinyMCE_paste__insertWordContent(value);
88
89                         return true;
90
91                 case "mceSelectAll":
92                         tinyMCE.execInstanceCommand(editor_id, 'selectall'); 
93                         return true; 
94
95         } 
96
97         // Pass to next handler in chain 
98         return false; 
99
100
101 function TinyMCE_paste__insertText(content, bLinebreaks) { 
102         if (content && content.length > 0) {
103                 if (bLinebreaks) { 
104                         // Special paragraph treatment 
105                         if (tinyMCE.getParam("plaintext_create_paragraphs", true)) { 
106                                 content = tinyMCE.regexpReplace(content, "\r\n\r\n", "</p><p>", "gi"); 
107                                 content = tinyMCE.regexpReplace(content, "\r\r", "</p><p>", "gi"); 
108                                 content = tinyMCE.regexpReplace(content, "\n\n", "</p><p>", "gi"); 
109
110                                 // Has paragraphs 
111                                 if ((pos = content.indexOf('</p><p>')) != -1) { 
112                                         tinyMCE.execCommand("Delete"); 
113
114                                         var node = tinyMCE.selectedInstance.getFocusElement(); 
115
116                                         // Get list of elements to break 
117                                         var breakElms = new Array(); 
118
119                                         do { 
120                                                 if (node.nodeType == 1) { 
121                                                         // Don't break tables and break at body 
122                                                         if (node.nodeName == "TD" || node.nodeName == "BODY") 
123                                                                 break; 
124         
125                                                         breakElms[breakElms.length] = node; 
126                                                 } 
127                                         } while(node = node.parentNode); 
128
129                                         var before = "", after = "</p>"; 
130                                         before += content.substring(0, pos); 
131
132                                         for (var i=0; i<breakElms.length; i++) { 
133                                                 before += "</" + breakElms[i].nodeName + ">"; 
134                                                 after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; 
135                                         } 
136
137                                         before += "<p>"; 
138                                         content = before + content.substring(pos+7) + after; 
139                                 } 
140                         } 
141
142                         content = tinyMCE.regexpReplace(content, "\r\n", "<br />", "gi"); 
143                         content = tinyMCE.regexpReplace(content, "\r", "<br />", "gi"); 
144                         content = tinyMCE.regexpReplace(content, "\n", "<br />", "gi"); 
145                 } 
146         
147                 tinyMCE.execCommand("mceInsertRawHTML", false, content); 
148         }
149 }
150
151 function TinyMCE_paste__insertWordContent(content) { 
152         if (content && content.length > 0) {
153                 // Cleanup Word content
154                 var bull = String.fromCharCode(8226);
155                 var middot = String.fromCharCode(183);
156
157                 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {
158                         content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>');
159                 }
160
161                 content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--");
162                 content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>");
163                 content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list
164                 content = content.replace(/<o:p><\/o:p>/gi, "");
165                 content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks
166                 content = content.replace(new RegExp('<(!--)([^>]*)(--)>', 'g'), "");  // Word comments
167                 content = content.replace(/<\/?span[^>]*>/gi, "");
168                 content = content.replace(new RegExp('<(\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3");
169                 content = content.replace(/<\/?font[^>]*>/gi, "");
170                 content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3");
171                 content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3");
172                 content = content.replace(/<\\?\?xml[^>]*>/gi, "");
173                 content = content.replace(/<\/?\w+:[^>]*>/gi, "");
174                 content = content.replace(/-- page break --\s*<p>&nbsp;<\/p>/gi, ""); // Remove pagebreaks
175                 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks
176
177 //              content = content.replace(/\/?&nbsp;*/gi, ""); &nbsp;
178 //              content = content.replace(/<p>&nbsp;<\/p>/gi, '');
179
180                 if (!tinyMCE.settings['force_p_newlines']) {
181                         content = content.replace('', '' ,'gi');
182                         content = content.replace('</p>', '<br /><br />' ,'gi');
183                 }
184
185                 if (!tinyMCE.isMSIE && !tinyMCE.settings['force_p_newlines']) {
186                         content = content.replace(/<\/?p[^>]*>/gi, "");
187                 }
188
189                 content = content.replace(/<\/?div[^>]*>/gi, "");
190
191                 // Convert all middlot lists to UL lists
192                 if (tinyMCE.getParam("paste_convert_middot_lists", true)) {
193                         var div = document.createElement("div");
194                         div.innerHTML = content;
195
196                         // Convert all middot paragraphs to li elements
197                         var className = tinyMCE.getParam("paste_unindented_list_class", "unIndentedList");
198
199                         while (TinyMCE_paste_convertMiddots(div, "--list--")) ; // bull
200                         while (TinyMCE_paste_convertMiddots(div, middot, className)) ; // Middot
201                         while (TinyMCE_paste_convertMiddots(div, bull)) ; // bull
202
203                         content = div.innerHTML;
204                 }
205
206                 // Replace all headers with strong and fix some other issues
207                 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) {
208                         content = content.replace(/<h[1-6]>&nbsp;<\/h[1-6]>/gi, '<p>&nbsp;&nbsp;</p>');
209                         content = content.replace(/<h[1-6]>/gi, '<p><b>');
210                         content = content.replace(/<\/h[1-6]>/gi, '</b></p>');
211                         content = content.replace(/<b>&nbsp;<\/b>/gi, '<b>&nbsp;&nbsp;</b>');
212                         content = content.replace(/^(&nbsp;)*/gi, '');
213                 }
214
215                 content = content.replace(/--list--/gi, ""); // Remove --list--
216
217                 // Insert cleaned content
218                 tinyMCE.execCommand("mceInsertContent", false, content);
219                 tinyMCE.execCommand("mceCleanup"); // Do normal cleanup
220         }
221 }
222
223 function TinyMCE_paste_convertMiddots(div, search, class_name) {
224         var mdot = String.fromCharCode(183);
225         var bull = String.fromCharCode(8226);
226
227         var nodes = div.getElementsByTagName("p");
228         for (var i=0; i<nodes.length; i++) {
229                 var p = nodes[i];
230
231                 // Is middot
232                 if (p.innerHTML.indexOf(search) != -1) {
233                         var ul = document.createElement("ul");
234
235                         if (class_name)
236                                 ul.className = class_name;
237
238                         // Add the first one
239                         var li = document.createElement("li");
240                         li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
241                         ul.appendChild(li);
242
243                         // Add the rest
244                         var np = p.nextSibling;
245                         while (np) {
246                                 // Not element or middot paragraph
247                                 if (np.nodeType != 1 || np.innerHTML.indexOf(search) == -1)
248                                         break;
249
250                                 var cp = np.nextSibling;
251                                 var li = document.createElement("li");
252                                 li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--|&nbsp;', "gi"), '');
253                                 np.parentNode.removeChild(np);
254                                 ul.appendChild(li);
255                                 np = cp;
256                         }
257
258                         p.parentNode.replaceChild(ul, p);
259
260                         return true;
261                 }
262         }
263
264         return false;
265 }
266
267 function TinyMCE_paste__clipboardHTML() {
268         var div = document.getElementById('_TinyMCE_clipboardHTML');
269
270         if (!div) {
271                 var div = document.createElement('DIV');
272                 div.id = '_TinyMCE_clipboardHTML';
273
274                 with (div.style) {
275                         visibility = 'hidden';
276                         overflow = 'hidden';
277                         position = 'absolute';
278                         width = 1;
279                         height = 1;
280                 }
281
282                 document.body.appendChild(div);
283         }
284
285         div.innerHTML = '';
286         var rng = document.body.createTextRange();
287         rng.moveToElementText(div);
288         rng.execCommand('Paste');
289         var html = div.innerHTML;
290         div.innerHTML = '';
291         return html;
292 }