engine/public/javascripts/admin/aloha/plugins/com.gentics.aloha.plugins.Paste/wordpastehandler.js

7 lines
5.9 KiB
JavaScript
Raw Normal View History

/*
* Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH
* aloha-sales@gentics.com
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
GENTICS.Aloha.PastePlugin.WordPasteHandler=new GENTICS.Aloha.PastePlugin.PasteHandler();GENTICS.Aloha.PastePlugin.WordPasteHandler.handlePaste=function(jqPasteDiv){if(this.detectWordContent(jqPasteDiv)){this.transformWordContent(jqPasteDiv)}};GENTICS.Aloha.PastePlugin.WordPasteHandler.detectWordContent=function(jqPasteDiv){var wordDetected=false;jqPasteDiv.find("*").each(function(){var style=jQuery(this).attr("style");if(style){if(style.toLowerCase().indexOf("mso")>=0){wordDetected=true;return false}}var clazz=jQuery(this).attr("class");if(clazz){if(clazz.toLowerCase().indexOf("mso")>=0){wordDetected=true;return false}}});return wordDetected};GENTICS.Aloha.PastePlugin.WordPasteHandler.isOrderedList=function(listSpan){if(listSpan.css("fontFamily")=="Wingdings"||listSpan.css("fontFamily")=="Symbol"){return false}return listSpan.text().match(/^([0-9]{1,3}\.)|([0-9]{1,3}\)|([a-zA-Z]{1,5}\.)|([a-zA-Z]{1,5}\)))$/)?true:false};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformListsFromWord=function(jqPasteDiv){var that=this;var listElementClass="aloha-list-element";var bulletClass="aloha-list-bullet";var detectionFilter="p.MsoListParagraphCxSpFirst,p.MsoListParagraph,p span";var paragraphs=jqPasteDiv.find(detectionFilter);paragraphs.each(function(){var jqElem=jQuery(this);if(jqElem.hasClass("MsoListParagraphCxSpFirst")||jqElem.hasClass("MsoListParagraph")){jqElem.addClass(listElementClass)}else{if(jqElem.css("font-family").indexOf("Symbol")>=0){jqElem.closest("p").addClass(listElementClass)}else{if(jqElem.css("mso-list")!=""){jqElem.closest("p").addClass(listElementClass)}}}});detectionFilter="p span span span";var spans=jqPasteDiv.find(detectionFilter);spans.each(function(){var jqElem=jQuery(this);var innerText=jqElem.text().trim().replace(/&nbsp;/g,"");if(innerText.length==0){var outerText=jqElem.parent().parent().text().trim().replace(/&nbsp;/g,"");if(outerText.match(/^([0-9]{1,3}\.)|([0-9]{1,3}\)|([a-zA-Z]{1,5}\.)|([a-zA-Z]{1,5}\)))$/)){jqElem.closest("p").addClass(listElementClass);jqElem.parent().parent().addClass(bulletClass)}}});detectionFilter="p."+listElementClass;var negateDetectionFilter=":not("+detectionFilter+")";paragraphs=jqPasteDiv.find(detectionFilter);if(paragraphs.length>0){paragraphs.each(function(){var jqElem=jQuery(this);jqElem.removeClass(listElementClass);jqElem.find("font").each(function(){jQuery(this).contents().unwrap()});var nestLevel=0;var margin=parseFloat(jqElem.css("marginLeft"));var lists=[];var following=jqElem.nextUntil(negateDetectionFilter);var firstSpan=jQuery(jqElem.find("span."+bulletClass));if(firstSpan.length==0){firstSpan=jQuery(jqElem.children("span:first"))}var ordered=that.isOrderedList(firstSpan);firstSpan.remove();var jqList=jQuery(ordered?"<ol></ol>":"<ul></ul>");lists.push(jqList);var jqNewLi=jQuery("<li></li>");jqList.append(jqNewLi);jqElem.contents().appendTo(jqNewLi);jqElem.replaceWith(jqList);following.each(function(){var jqElem=jQuery(this);jqElem.find("font").each(function(){jQuery(this).contents().unwrap()});var newMargin=parseFloat(jqElem.css("marginLeft"));firstSpan=jQuery(jqElem.find("span."+bulletClass));if(firstSpan.length==0){firstSpan=jQuery(jqElem.children("span:first"))}ordered=that.isOrderedList(firstSpan);firstSpan.remove();if(newMargin>margin){var jqNewList=jQuery(ordered?"<ol></ol>":"<ul></ul>");jqList.children(":last").append(jqNewList);jqList=jqNewList;lists.push(jqList);nestLevel++;margin=newMargin}else{if(newMargin<margin&&nestLevel>0){lists.pop();nestLevel--;jqList=lists[nestLevel];margin=newMargin}}jqNewLi=jQuery("<li></li>");jqList.append(jqNewLi);jqElem.contents().appendTo(jqNewLi);jqElem.remove()})})}};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformTitles=function(jqPasteDiv){jqPasteDiv.find("p.MsoTitle").each(function(){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"h1")});jqPasteDiv.find("p.MsoSubtitle").each(function(){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"h2")})};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformTables=function(jqPasteDiv){jqPasteDiv.find("table").each(function(){jQuery(this).removeAttr("border")