[ Index ] |
|
Code source de WordPress 2.1.2 |
1 /** 2 * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $ 3 * 4 * @author Moxiecode 5 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved. 6 */ 7 8 /* Import plugin specific language pack */ 9 tinyMCE.importPluginLanguagePack('paste'); 10 11 var TinyMCE_PastePlugin = { 12 getInfo : function() { 13 return { 14 longname : 'Paste text/word', 15 author : 'Moxiecode Systems AB', 16 authorurl : 'http://tinymce.moxiecode.com', 17 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_paste.html', 18 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion 19 }; 20 }, 21 22 initInstance : function(inst) { 23 if (tinyMCE.isMSIE && tinyMCE.getParam("paste_auto_cleanup_on_paste", false)) 24 tinyMCE.addEvent(inst.getBody(), "paste", TinyMCE_PastePlugin._handlePasteEvent); 25 }, 26 27 getControlHTML : function(cn) { 28 switch (cn) { 29 case "pastetext": 30 return tinyMCE.getButtonHTML(cn, 'lang_paste_text_desc', '{$pluginurl}/images/pastetext.gif', 'mcePasteText', true); 31 32 case "pasteword": 33 return tinyMCE.getButtonHTML(cn, 'lang_paste_word_desc', '{$pluginurl}/images/pasteword.gif', 'mcePasteWord', true); 34 35 case "selectall": 36 return tinyMCE.getButtonHTML(cn, 'lang_selectall_desc', '{$pluginurl}/images/selectall.gif', 'mceSelectAll', true); 37 } 38 39 return ''; 40 }, 41 42 execCommand : function(editor_id, element, command, user_interface, value) { 43 switch (command) { 44 case "mcePasteText": 45 if (user_interface) { 46 if ((tinyMCE.isMSIE && !tinyMCE.isOpera) && !tinyMCE.getParam('paste_use_dialog', false)) 47 TinyMCE_PastePlugin._insertText(clipboardData.getData("Text"), true); 48 else { 49 var template = new Array(); 50 template['file'] = '../../plugins/paste/pastetext.htm'; // Relative to theme 51 template['width'] = 450; 52 template['height'] = 400; 53 var plain_text = ""; 54 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'}); 55 } 56 } else 57 TinyMCE_PastePlugin._insertText(value['html'], value['linebreaks']); 58 59 return true; 60 61 case "mcePasteWord": 62 if (user_interface) { 63 if ((tinyMCE.isMSIE && !tinyMCE.isOpera) && !tinyMCE.getParam('paste_use_dialog', false)) { 64 TinyMCE_PastePlugin._insertWordContent(TinyMCE_PastePlugin._clipboardHTML()); 65 } else { 66 var template = new Array(); 67 template['file'] = '../../plugins/paste/pasteword.htm'; // Relative to theme 68 template['width'] = 450; 69 template['height'] = 400; 70 var plain_text = ""; 71 tinyMCE.openWindow(template, {editor_id : editor_id, plain_text: plain_text, resizable : "yes", scrollbars : "no", inline : "yes", mceDo : 'insert'}); 72 } 73 } else 74 TinyMCE_PastePlugin._insertWordContent(value); 75 76 return true; 77 78 case "mceSelectAll": 79 tinyMCE.execInstanceCommand(editor_id, 'selectall'); 80 return true; 81 82 } 83 84 // Pass to next handler in chain 85 return false; 86 }, 87 88 // Private plugin internal methods 89 90 _handlePasteEvent : function(e) { 91 switch (e.type) { 92 case "paste": 93 var html = TinyMCE_PastePlugin._clipboardHTML(); 94 var r, inst = tinyMCE.selectedInstance; 95 96 // Removes italic, strong etc, the if was needed due to bug #1437114 97 if (inst && (r = inst.getRng()) && r.text.length > 0) 98 tinyMCE.execCommand('delete'); 99 100 if (html && html.length > 0) 101 tinyMCE.execCommand('mcePasteWord', false, html); 102 103 tinyMCE.cancelEvent(e); 104 return false; 105 } 106 107 return true; 108 }, 109 110 _insertText : function(content, bLinebreaks) { 111 if (content && content.length > 0) { 112 if (bLinebreaks) { 113 // Special paragraph treatment 114 if (tinyMCE.getParam("paste_create_paragraphs", true)) { 115 var rl = tinyMCE.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(','); 116 for (var i=0; i<rl.length; i+=2) 117 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]); 118 119 content = tinyMCE.regexpReplace(content, "\r\n\r\n", "</p><p>", "gi"); 120 content = tinyMCE.regexpReplace(content, "\r\r", "</p><p>", "gi"); 121 content = tinyMCE.regexpReplace(content, "\n\n", "</p><p>", "gi"); 122 123 // Has paragraphs 124 if ((pos = content.indexOf('</p><p>')) != -1) { 125 tinyMCE.execCommand("Delete"); 126 127 var node = tinyMCE.selectedInstance.getFocusElement(); 128 129 // Get list of elements to break 130 var breakElms = new Array(); 131 132 do { 133 if (node.nodeType == 1) { 134 // Don't break tables and break at body 135 if (node.nodeName == "TD" || node.nodeName == "BODY") 136 break; 137 138 breakElms[breakElms.length] = node; 139 } 140 } while(node = node.parentNode); 141 142 var before = "", after = "</p>"; 143 before += content.substring(0, pos); 144 145 for (var i=0; i<breakElms.length; i++) { 146 before += "</" + breakElms[i].nodeName + ">"; 147 after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; 148 } 149 150 before += "<p>"; 151 content = before + content.substring(pos+7) + after; 152 } 153 } 154 155 if (tinyMCE.getParam("paste_create_linebreaks", true)) { 156 content = tinyMCE.regexpReplace(content, "\r\n", "<br />", "gi"); 157 content = tinyMCE.regexpReplace(content, "\r", "<br />", "gi"); 158 content = tinyMCE.regexpReplace(content, "\n", "<br />", "gi"); 159 } 160 } 161 162 tinyMCE.execCommand("mceInsertRawHTML", false, content); 163 } 164 }, 165 166 _insertWordContent : function(content) { 167 if (content && content.length > 0) { 168 // Cleanup Word content 169 var bull = String.fromCharCode(8226); 170 var middot = String.fromCharCode(183); 171 var cb; 172 173 if ((cb = tinyMCE.getParam("paste_insert_word_content_callback", "")) != "") 174 content = eval(cb + "('before', content)"); 175 176 var rl = tinyMCE.getParam("paste_replace_list", '\u2122,<sup>TM</sup>,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(','); 177 for (var i=0; i<rl.length; i+=2) 178 content = content.replace(new RegExp(rl[i], 'gi'), rl[i+1]); 179 180 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) { 181 content = content.replace(new RegExp('<p class=MsoHeading.*?>(.*?)<\/p>', 'gi'), '<p><b>$1</b></p>'); 182 } 183 184 content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--"); 185 content = content.replace(new RegExp(bull + "(.*?)<BR>", "gi"), "<p>" + middot + "$1</p>"); 186 content = content.replace(new RegExp('<SPAN style="mso-list: Ignore">', 'gi'), "<span>" + bull); // Covert to bull list 187 content = content.replace(/<o:p><\/o:p>/gi, ""); 188 content = content.replace(new RegExp('<br style="page-break-before: always;.*>', 'gi'), '-- page break --'); // Replace pagebreaks 189 content = content.replace(new RegExp('<(!--)([^>]*)(--)>', 'g'), ""); // Word comments 190 191 if (tinyMCE.getParam("paste_remove_spans", true)) 192 content = content.replace(/<\/?span[^>]*>/gi, ""); 193 194 if (tinyMCE.getParam("paste_remove_styles", true)) 195 content = content.replace(new RegExp('<(\\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3"); 196 197 content = content.replace(/<\/?font[^>]*>/gi, ""); 198 199 // Strips class attributes. 200 switch (tinyMCE.getParam("paste_strip_class_attributes", "all")) { 201 case "all": 202 content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3"); 203 break; 204 205 case "mso": 206 content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3"); 207 break; 208 } 209 210 content = content.replace(new RegExp('href="?' + TinyMCE_PastePlugin._reEscape("" + document.location) + '', 'gi'), 'href="' + tinyMCE.settings['document_base_url']); 211 content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3"); 212 content = content.replace(/<\\?\?xml[^>]*>/gi, ""); 213 content = content.replace(/<\/?\w+:[^>]*>/gi, ""); 214 content = content.replace(/-- page break --\s*<p> <\/p>/gi, ""); // Remove pagebreaks 215 content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks 216 217 // content = content.replace(/\/? */gi, ""); 218 // content = content.replace(/<p> <\/p>/gi, ''); 219 220 if (!tinyMCE.settings['force_p_newlines']) { 221 content = content.replace('', '' ,'gi'); 222 content = content.replace('</p>', '<br /><br />' ,'gi'); 223 } 224 225 if (!tinyMCE.isMSIE && !tinyMCE.settings['force_p_newlines']) { 226 content = content.replace(/<\/?p[^>]*>/gi, ""); 227 } 228 229 content = content.replace(/<\/?div[^>]*>/gi, ""); 230 231 // Convert all middlot lists to UL lists 232 if (tinyMCE.getParam("paste_convert_middot_lists", true)) { 233 var div = document.createElement("div"); 234 div.innerHTML = content; 235 236 // Convert all middot paragraphs to li elements 237 var className = tinyMCE.getParam("paste_unindented_list_class", "unIndentedList"); 238 239 while (TinyMCE_PastePlugin._convertMiddots(div, "--list--")) ; // bull 240 while (TinyMCE_PastePlugin._convertMiddots(div, middot, className)) ; // Middot 241 while (TinyMCE_PastePlugin._convertMiddots(div, bull)) ; // bull 242 243 content = div.innerHTML; 244 } 245 246 // Replace all headers with strong and fix some other issues 247 if (tinyMCE.getParam("paste_convert_headers_to_strong", false)) { 248 content = content.replace(/<h[1-6]> <\/h[1-6]>/gi, '<p> </p>'); 249 content = content.replace(/<h[1-6]>/gi, '<p><b>'); 250 content = content.replace(/<\/h[1-6]>/gi, '</b></p>'); 251 content = content.replace(/<b> <\/b>/gi, '<b> </b>'); 252 content = content.replace(/^( )*/gi, ''); 253 } 254 255 content = content.replace(/--list--/gi, ""); // Remove --list-- 256 257 if ((cb = tinyMCE.getParam("paste_insert_word_content_callback", "")) != "") 258 content = eval(cb + "('after', content)"); 259 260 // Insert cleaned content 261 tinyMCE.execCommand("mceInsertContent", false, content); 262 263 if (tinyMCE.getParam('paste_force_cleanup_wordpaste', true)) 264 window.setTimeout('tinyMCE.execCommand("mceCleanup");', 1); // Do normal cleanup detached from this thread 265 } 266 }, 267 268 _reEscape : function(s) { 269 var l = "?.\\*[](){}+^$:"; 270 var o = ""; 271 272 for (var i=0; i<s.length; i++) { 273 var c = s.charAt(i); 274 275 if (l.indexOf(c) != -1) 276 o += '\\' + c; 277 else 278 o += c; 279 } 280 281 return o; 282 }, 283 284 _convertMiddots : function(div, search, class_name) { 285 var mdot = String.fromCharCode(183); 286 var bull = String.fromCharCode(8226); 287 288 var nodes = div.getElementsByTagName("p"); 289 var prevul; 290 for (var i=0; i<nodes.length; i++) { 291 var p = nodes[i]; 292 293 // Is middot 294 if (p.innerHTML.indexOf(search) == 0) { 295 var ul = document.createElement("ul"); 296 297 if (class_name) 298 ul.className = class_name; 299 300 // Add the first one 301 var li = document.createElement("li"); 302 li.innerHTML = p.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--| ', "gi"), ''); 303 ul.appendChild(li); 304 305 // Add the rest 306 var np = p.nextSibling; 307 while (np) { 308 // If the node is whitespace, then 309 // ignore it and continue on. 310 if (np.nodeType == 3 && new RegExp('^\\s$', 'm').test(np.nodeValue)) { 311 np = np.nextSibling; 312 continue; 313 } 314 315 if (search == mdot) { 316 if (np.nodeType == 1 && new RegExp('^o(\\s+| )').test(np.innerHTML)) { 317 // Second level of nesting 318 if (!prevul) { 319 prevul = ul; 320 ul = document.createElement("ul"); 321 prevul.appendChild(ul); 322 } 323 np.innerHTML = np.innerHTML.replace(/^o/, ''); 324 } else { 325 // Pop the stack if we're going back up to the first level 326 if (prevul) { 327 ul = prevul; 328 prevul = null; 329 } 330 // Not element or middot paragraph 331 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0) 332 break; 333 } 334 } else { 335 // Not element or middot paragraph 336 if (np.nodeType != 1 || np.innerHTML.indexOf(search) != 0) 337 break; 338 } 339 340 var cp = np.nextSibling; 341 var li = document.createElement("li"); 342 li.innerHTML = np.innerHTML.replace(new RegExp('' + mdot + '|' + bull + '|--list--| ', "gi"), ''); 343 np.parentNode.removeChild(np); 344 ul.appendChild(li); 345 np = cp; 346 } 347 348 p.parentNode.replaceChild(ul, p); 349 350 return true; 351 } 352 } 353 354 return false; 355 }, 356 357 _clipboardHTML : function() { 358 var div = document.getElementById('_TinyMCE_clipboardHTML'); 359 360 if (!div) { 361 var div = document.createElement('DIV'); 362 div.id = '_TinyMCE_clipboardHTML'; 363 364 with (div.style) { 365 visibility = 'hidden'; 366 overflow = 'hidden'; 367 position = 'absolute'; 368 width = 1; 369 height = 1; 370 } 371 372 document.body.appendChild(div); 373 } 374 375 div.innerHTML = ''; 376 var rng = document.body.createTextRange(); 377 rng.moveToElementText(div); 378 rng.execCommand('Paste'); 379 var html = div.innerHTML; 380 div.innerHTML = ''; 381 return html; 382 } 383 }; 384 385 tinyMCE.addPlugin("paste", TinyMCE_PastePlugin);
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |