bgneal@45: /** bgneal@45: * $Id: editor_plugin_src.js 919 2008-09-08 20:31:23Z spocke $ bgneal@45: * bgneal@45: * @author Moxiecode bgneal@45: * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. bgneal@45: */ bgneal@45: bgneal@45: (function() { bgneal@45: var Event = tinymce.dom.Event; bgneal@45: bgneal@45: tinymce.create('tinymce.plugins.PastePlugin', { bgneal@45: init : function(ed, url) { bgneal@45: var t = this; bgneal@45: bgneal@45: t.editor = ed; bgneal@45: bgneal@45: // Register commands bgneal@45: ed.addCommand('mcePasteText', function(ui, v) { bgneal@45: if (ui) { bgneal@45: if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) { bgneal@45: ed.windowManager.open({ bgneal@45: file : url + '/pastetext.htm', bgneal@45: width : 450, bgneal@45: height : 400, bgneal@45: inline : 1 bgneal@45: }, { bgneal@45: plugin_url : url bgneal@45: }); bgneal@45: } else bgneal@45: t._insertText(clipboardData.getData("Text"), true); bgneal@45: } else bgneal@45: t._insertText(v.html, v.linebreaks); bgneal@45: }); bgneal@45: bgneal@45: ed.addCommand('mcePasteWord', function(ui, v) { bgneal@45: if (ui) { bgneal@45: if ((ed.getParam('paste_use_dialog', true)) || (!tinymce.isIE)) { bgneal@45: ed.windowManager.open({ bgneal@45: file : url + '/pasteword.htm', bgneal@45: width : 450, bgneal@45: height : 400, bgneal@45: inline : 1 bgneal@45: }, { bgneal@45: plugin_url : url bgneal@45: }); bgneal@45: } else bgneal@45: t._insertText(t._clipboardHTML()); bgneal@45: } else bgneal@45: t._insertWordContent(v); bgneal@45: }); bgneal@45: bgneal@45: ed.addCommand('mceSelectAll', function() { bgneal@45: ed.execCommand('selectall'); bgneal@45: }); bgneal@45: bgneal@45: // Register buttons bgneal@45: ed.addButton('pastetext', {title : 'paste.paste_text_desc', cmd : 'mcePasteText', ui : true}); bgneal@45: ed.addButton('pasteword', {title : 'paste.paste_word_desc', cmd : 'mcePasteWord', ui : true}); bgneal@45: ed.addButton('selectall', {title : 'paste.selectall_desc', cmd : 'mceSelectAll'}); bgneal@45: bgneal@45: if (ed.getParam("paste_auto_cleanup_on_paste", false)) { bgneal@45: ed.onPaste.add(function(ed, e) { bgneal@45: return t._handlePasteEvent(e) bgneal@45: }); bgneal@45: } bgneal@45: bgneal@45: if (!tinymce.isIE && ed.getParam("paste_auto_cleanup_on_paste", false)) { bgneal@45: // Force paste dialog if non IE browser bgneal@45: ed.onKeyDown.add(function(ed, e) { bgneal@45: if (e.ctrlKey && e.keyCode == 86) { bgneal@45: window.setTimeout(function() { bgneal@45: ed.execCommand("mcePasteText", true); bgneal@45: }, 1); bgneal@45: bgneal@45: Event.cancel(e); bgneal@45: } bgneal@45: }); bgneal@45: } bgneal@45: }, bgneal@45: bgneal@45: getInfo : function() { bgneal@45: return { bgneal@45: longname : 'Paste text/word', bgneal@45: author : 'Moxiecode Systems AB', bgneal@45: authorurl : 'http://tinymce.moxiecode.com', bgneal@45: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste', bgneal@45: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@45: }; bgneal@45: }, bgneal@45: bgneal@45: // Private methods bgneal@45: bgneal@45: _handlePasteEvent : function(e) { bgneal@45: var html = this._clipboardHTML(), ed = this.editor, sel = ed.selection, r; bgneal@45: bgneal@45: // Removes italic, strong etc, the if was needed due to bug #1437114 bgneal@45: if (ed && (r = sel.getRng()) && r.text.length > 0) bgneal@45: ed.execCommand('delete'); bgneal@45: bgneal@45: if (html && html.length > 0) bgneal@45: ed.execCommand('mcePasteWord', false, html); bgneal@45: bgneal@45: return Event.cancel(e); bgneal@45: }, bgneal@45: bgneal@45: _insertText : function(content, bLinebreaks) { bgneal@45: content = this.editor.dom.encode(content); bgneal@45: bgneal@45: if (content && content.length > 0) { bgneal@45: // Delete any highlighted text before pasting bgneal@45: if (!this.editor.selection.isCollapsed()) bgneal@45: this.editor.execCommand("Delete"); bgneal@45: bgneal@45: if (bLinebreaks) { bgneal@45: // Special paragraph treatment bgneal@45: if (this.editor.getParam("paste_create_paragraphs", true)) { bgneal@45: var rl = this.editor.getParam("paste_replace_list", '\u2122,TM,\u2026,...,\u201c|\u201d,",\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(','); bgneal@45: for (var i=0; i

'); bgneal@45: content = content.replace(/\r\r/g, '

'); bgneal@45: content = content.replace(/\n\n/g, '

'); bgneal@45: bgneal@45: // Has paragraphs bgneal@45: if ((pos = content.indexOf('

')) != -1) { bgneal@45: this.editor.execCommand("Delete"); bgneal@45: bgneal@45: var node = this.editor.selection.getNode(); bgneal@45: bgneal@45: // Get list of elements to break bgneal@45: var breakElms = []; bgneal@45: bgneal@45: do { bgneal@45: if (node.nodeType == 1) { bgneal@45: // Don't break tables and break at body bgneal@45: if (node.nodeName == "TD" || node.nodeName == "BODY") bgneal@45: break; bgneal@45: bgneal@45: breakElms[breakElms.length] = node; bgneal@45: } bgneal@45: } while(node = node.parentNode); bgneal@45: bgneal@45: var before = "", after = "

"; bgneal@45: before += content.substring(0, pos); bgneal@45: bgneal@45: for (var i=0; i"; bgneal@45: after += "<" + breakElms[(breakElms.length-1)-i].nodeName + ">"; bgneal@45: } bgneal@45: bgneal@45: before += "

"; bgneal@45: content = before + content.substring(pos+7) + after; bgneal@45: } bgneal@45: } bgneal@45: bgneal@45: if (this.editor.getParam("paste_create_linebreaks", true)) { bgneal@45: content = content.replace(/\r\n/g, '
'); bgneal@45: content = content.replace(/\r/g, '
'); bgneal@45: content = content.replace(/\n/g, '
'); bgneal@45: } bgneal@45: } bgneal@45: bgneal@45: this.editor.execCommand("mceInsertRawHTML", false, content); bgneal@45: } bgneal@45: }, bgneal@45: bgneal@45: _insertWordContent : function(content) { bgneal@45: var t = this, ed = t.editor; bgneal@45: bgneal@45: if (content && content.length > 0) { bgneal@45: // Cleanup Word content bgneal@45: var bull = String.fromCharCode(8226); bgneal@45: var middot = String.fromCharCode(183); bgneal@45: bgneal@45: if (ed.getParam('paste_insert_word_content_callback')) bgneal@45: content = ed.execCallback('paste_insert_word_content_callback', 'before', content); bgneal@45: bgneal@45: var rl = ed.getParam("paste_replace_list", '\u2122,TM,\u2026,...,\x93|\x94|\u201c|\u201d,",\x60|\x91|\x92|\u2018|\u2019,\',\u2013|\u2014|\u2015|\u2212,-').split(','); bgneal@45: for (var i=0; i(.*?)<\/p>', 'gi'), '

$1

'); bgneal@45: } bgneal@45: bgneal@45: content = content.replace(new RegExp('tab-stops: list [0-9]+.0pt">', 'gi'), '">' + "--list--"); bgneal@45: content = content.replace(new RegExp(bull + "(.*?)
", "gi"), "

" + middot + "$1

"); bgneal@45: content = content.replace(new RegExp('', 'gi'), "" + bull); // Covert to bull list bgneal@45: content = content.replace(/<\/o:p>/gi, ""); bgneal@45: content = content.replace(new RegExp('
]+>/g, ""); // Header elements bgneal@45: bgneal@45: if (this.editor.getParam("paste_remove_spans", true)) bgneal@45: content = content.replace(/<\/?span[^>]*>/gi, ""); bgneal@45: bgneal@45: if (this.editor.getParam("paste_remove_styles", true)) bgneal@45: content = content.replace(new RegExp('<(\\w[^>]*) style="([^"]*)"([^>]*)', 'gi'), "<$1$3"); bgneal@45: bgneal@45: content = content.replace(/<\/?font[^>]*>/gi, ""); bgneal@45: bgneal@45: // Strips class attributes. bgneal@45: switch (this.editor.getParam("paste_strip_class_attributes", "all")) { bgneal@45: case "all": bgneal@45: content = content.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3"); bgneal@45: break; bgneal@45: bgneal@45: case "mso": bgneal@45: content = content.replace(new RegExp('<(\\w[^>]*) class="?mso([^ |>]*)([^>]*)', 'gi'), "<$1$3"); bgneal@45: break; bgneal@45: } bgneal@45: bgneal@45: content = content.replace(new RegExp('href="?' + this._reEscape("" + document.location) + '', 'gi'), 'href="' + this.editor.documentBaseURI.getURI()); bgneal@45: content = content.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3"); bgneal@45: content = content.replace(/<\\?\?xml[^>]*>/gi, ""); bgneal@45: content = content.replace(/<\/?\w+:[^>]*>/gi, ""); bgneal@45: content = content.replace(/-- page break --\s*

 <\/p>/gi, ""); // Remove pagebreaks bgneal@45: content = content.replace(/-- page break --/gi, ""); // Remove pagebreaks bgneal@45: bgneal@45: // content = content.replace(/\/? */gi, "");   bgneal@45: // content = content.replace(/

 <\/p>/gi, ''); bgneal@45: bgneal@45: if (!this.editor.getParam('force_p_newlines')) { bgneal@45: content = content.replace('', '' ,'gi'); bgneal@45: content = content.replace('

', '

' ,'gi'); bgneal@45: } bgneal@45: bgneal@45: if (!tinymce.isIE && !this.editor.getParam('force_p_newlines')) { bgneal@45: content = content.replace(/<\/?p[^>]*>/gi, ""); bgneal@45: } bgneal@45: bgneal@45: content = content.replace(/<\/?div[^>]*>/gi, ""); bgneal@45: bgneal@45: // Convert all middlot lists to UL lists bgneal@45: if (this.editor.getParam("paste_convert_middot_lists", true)) { bgneal@45: var div = ed.dom.create("div", null, content); bgneal@45: bgneal@45: // Convert all middot paragraphs to li elements bgneal@45: var className = this.editor.getParam("paste_unindented_list_class", "unIndentedList"); bgneal@45: bgneal@45: while (this._convertMiddots(div, "--list--")) ; // bull bgneal@45: while (this._convertMiddots(div, middot, className)) ; // Middot bgneal@45: while (this._convertMiddots(div, bull)) ; // bull bgneal@45: bgneal@45: content = div.innerHTML; bgneal@45: } bgneal@45: bgneal@45: // Replace all headers with strong and fix some other issues bgneal@45: if (this.editor.getParam("paste_convert_headers_to_strong", false)) { bgneal@45: content = content.replace(/ <\/h[1-6]>/gi, '

  

'); bgneal@45: content = content.replace(//gi, '

'); bgneal@45: content = content.replace(/<\/h[1-6]>/gi, '

'); bgneal@45: content = content.replace(/ <\/b>/gi, '  '); bgneal@45: content = content.replace(/^( )*/gi, ''); bgneal@45: } bgneal@45: bgneal@45: content = content.replace(/--list--/gi, ""); // Remove --list-- bgneal@45: bgneal@45: if (ed.getParam('paste_insert_word_content_callback')) bgneal@45: content = ed.execCallback('paste_insert_word_content_callback', 'after', content); bgneal@45: bgneal@45: // Insert cleaned content bgneal@45: this.editor.execCommand("mceInsertContent", false, content); bgneal@45: bgneal@45: if (this.editor.getParam('paste_force_cleanup_wordpaste', true)) { bgneal@45: var ed = this.editor; bgneal@45: bgneal@45: window.setTimeout(function() { bgneal@45: ed.execCommand("mceCleanup"); bgneal@45: }, 1); // Do normal cleanup detached from this thread bgneal@45: } bgneal@45: } bgneal@45: }, bgneal@45: bgneal@45: _reEscape : function(s) { bgneal@45: var l = "?.\\*[](){}+^$:"; bgneal@45: var o = ""; bgneal@45: bgneal@45: for (var i=0; i