- // This next piece of code handles the situation where we're pasting more than one paragraph of plain\r
- // text, and we are pasting the content into the middle of a block node in the editor. The block\r
- // node gets split at the selection point into "Para A" and "Para B" (for the purposes of explaining).\r
- // The first paragraph of the pasted text is appended to "Para A", and the last paragraph of the\r
- // pasted text is prepended to "Para B". Any other paragraphs of pasted text are placed between\r
- // "Para A" and "Para B". This code solves a host of problems with the original plain text plugin and\r
- // now handles styles correctly. (Pasting plain text into a styled paragraph is supposed to make the\r
- // plain text take the same style as the existing paragraph.)\r
- if ((pos = h.indexOf("</p><p>")) != -1) {\r
- rpos = h.lastIndexOf("</p><p>");\r
- node = sel.getNode(); \r
- breakElms = []; // Get list of elements to break \r
-\r
- do {\r
- if (node.nodeType == 1) {\r
- // Don't break tables and break at body\r
- if (node.nodeName == "TD" || node.nodeName == "BODY") {\r
- break;\r
- }\r
-\r
- breakElms[breakElms.length] = node;\r
- }\r
- } while (node = node.parentNode);\r
-\r
- // Are we in the middle of a block node?\r
- if (breakElms.length > 0) {\r
- before = h.substring(0, pos);\r
- after = "";\r
-\r
- for (i=0, len=breakElms.length; i<len; i++) {\r
- before += "</" + breakElms[i].nodeName.toLowerCase() + ">";\r
- after += "<" + breakElms[breakElms.length-i-1].nodeName.toLowerCase() + ">";\r
- }\r
-\r
- if (pos == rpos) {\r
- h = before + after + h.substring(pos+7);\r
- }\r
- else {\r
- h = before + h.substring(pos+4, rpos+4) + after + h.substring(rpos+7);\r
- }\r
- }\r
- }\r
-\r
- // Insert content at the caret, plus add a marker for repositioning the caret\r
- ed.execCommand("mceInsertRawHTML", false, h + '<span id="_plain_text_marker"> </span>');\r
-\r
- // Reposition the caret to the marker, which was placed immediately after the inserted content.\r
- // Needs to be done asynchronously (in window.setTimeout) or else it doesn't work in all browsers.\r
- // The second part of the code scrolls the content up if the caret is positioned off-screen.\r
- // This is only necessary for WebKit browsers, but it doesn't hurt to use for all.\r
- window.setTimeout(function() {\r
- var marker = dom.get('_plain_text_marker'),\r
- elm, vp, y, elmHeight;\r
-\r
- sel.select(marker, false);\r
- d.execCommand("Delete", false, null);\r
- marker = null;\r
-\r
- // Get element, position and height\r
- elm = sel.getStart();\r
- vp = dom.getViewPort(w);\r
- y = dom.getPos(elm).y;\r
- elmHeight = elm.clientHeight;\r
-\r
- // Is element within viewport if not then scroll it into view\r
- if ((y < vp.y) || (y + elmHeight > vp.y + vp.h)) {\r
- d.body.scrollTop = y < vp.y ? y : y - vp.h + 25;\r
- }\r
- }, 0);\r