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