CKEDITOR.plugins.add("ipsquote",{requires:"widget",icons:"ipsquote",hidpi:!0,allowedContent:"blockquote",init:function(c){c.widgets.add("ipsquote",{button:ips.getString("editorQuote"),template:ips.templates.render("core.editor.quote",{citation:ips.getString("editorQuote"),contents:""}),editables:{content:{selector:".ipsQuote_contents"}},upcast:function(b){if("blockquote"==b.name&&b.hasClass("ipsQuote")){var a={},d=0;for(d in b.attributes)"data-ipsquote-"==d.substr(0,14)&&(a[d.substr(14)]=b.attributes[d]);
b.children[0].hasClass("ipsQuote_citation")?b.children[0].setHtml(ips.utils.getCitation(a)):b.setHtml(ips.templates.render("core.editor.legacyQuoteUpcast",{citation:ips.utils.getCitation(a),contents:b.getHtml()}));return!0}},init:function(){var b=c.getSelectedHtml(!0),a=c.getSelection(),a=a?a.getSelectedElement():null;if(!a||!a.hasClass("cke_widget_wrapper"))this.once("focus",function(){if(b){"<p>"===b.substr(0,1)?this.editables.content.setHtml(b):this.editables.content.setHtml("<p>"+b+"</p>");var a=
c.createRange();a.moveToElementEditablePosition(this.wrapper.getNext())}else a=c.createRange(),a.moveToElementEditablePosition(this.editables.content);a.select()},this)}});c.on("key",function(b){if(13==b.data.keyCode)for(var a=c.getSelection().getRanges(),d=0;d<a.length;d++)if(b=a[d].getCommonAncestor(),b=b instanceof CKEDITOR.dom.text?b.getParent():b,"p"==b.getName()&&"<br>"==b.getHtml()){var e=b.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&"div"==a.getName()&&a.hasClass("ipsQuote_contents")},
!0);if(e){if(b.getNext()){for(var a=CKEDITOR.dom.element.createFromHtml(ips.templates.render("core.editor.quote",{citation:e.getParent().findOne(".ipsQuote_citation").getText(),contents:""})),f=b,d=[];f=f.getNext();)d.push(f);for(var f=a.findOne(".ipsQuote_contents"),g=0;g<d.length;g++)d[g].move(f);d=CKEDITOR.dom.element.createFromHtml("<p><br></p>");d.insertAfter(e.getParent().getParent());a.insertAfter(d);c.widgets.initOn(a,"ipsquote");a=c.createRange();a.moveToPosition(d,CKEDITOR.POSITION_AFTER_START);
c.getSelection().selectRanges([a]);b.remove()}else b.remove(),a=c.createRange(),b=e.getParent().getParent(),e=b.getNext(),e||(e=new CKEDITOR.dom.element("p",c.document),e.insertAfter(b)),a.moveToPosition(e,CKEDITOR.POSITION_AFTER_START),c.getSelection().selectRanges([a]);return!1}}},this,0);c.addCommand("ipsQuoteBreakout",{exec:function(b){var a=b.getSelection(),d=a.getSelectedElement();if(!d)for(var c=a.getRanges(),a=0;a<c.length;a++)d=c[a].getCommonAncestor();a=d.getAscendant(function(a){return a instanceof
CKEDITOR.dom.element&&a.hasClass("cke_widget_wrapper")},!0);d=a.find(".ipsQuote_contents");a.remove();for(a=0;a<d.count();a++)b.insertElement(d.getItem(a))}});c.addCommand("ipsQuoteRemove",{exec:function(b){var a=b.getSelection(),d=a.createBookmarks2(!0),c=a.getSelectedElement();if(!c)for(var a=a.getRanges(),f=0;f<a.length;f++)c=a[f].getCommonAncestor();c.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&a.hasClass("cke_widget_wrapper")},!0).remove();b.getSelection().selectBookmarks(d)}});
c.contextMenu&&(c.addMenuGroup("ipsQuote"),c.addMenuItem("ipsQuoteBreakout",{label:ips.getString("editorQuoteBreakout"),icon:null,command:"ipsQuoteBreakout",group:"ipsQuote"}),c.addMenuItem("ipsQuoteRemove",{label:ips.getString("editorQuoteRemove"),icon:null,command:"ipsQuoteRemove",group:"ipsQuote"}),c.contextMenu.addListener(function(b){if(b.getAscendant(function(a){return a instanceof CKEDITOR.dom.element&&a.hasClass("ipsQuote")},!0)||b.find(".ipsQuote").count())return{ipsQuoteBreakout:CKEDITOR.TRISTATE_OFF,
ipsQuoteRemove:CKEDITOR.TRISTATE_OFF}}))}});