CKEDITOR.plugins.add("ipspaste",{requires:"clipboard",init:function(a){function p(b){var c=a.getSelection().createBookmarks2();if(d.length&&b.data.dataTransfer.getFilesCount())for(var e=0;e<b.data.dataTransfer.getFilesCount();e++)d.trigger("injectFile",{file:b.data.dataTransfer.getFile(e),data:{insertPoint:c}})}function n(){var b=$("."+a.id).closest("[data-ipsEditor]").find('[data-role\x3d"imageMessage"]');b.slideDown({queue:!1});b.find('[data-action\x3d"removeImageMessage"]').click(function(){b.slideUp()})}
if(a.contextMenu)a.on("pluginsLoaded",function(){a.removeMenuItem("cut");a.removeMenuItem("copy")});a.addCommand("ipspasteplain",{canUndo:!1,async:!0,exec:function(b){b.getClipboardData({title:ips.getString("pasteAsPlaintext")},function(a){a&&b.fire("paste",{type:"text",dataValue:a.dataValue,method:"paste",dataTransfer:CKEDITOR.plugins.clipboard.initPasteDataTransfer(),force:!0});b.fire("afterCommandExec",{name:"pastetext",command:"ipspasteplain",returnValue:!!a})})}});a.contextMenu&&(a.addMenuItem("ipspasteplain",
{label:ips.getString("pasteAsPlaintext"),icon:this.path+"icons"+(CKEDITOR.env.hidpi?"/hidpi":"")+"/pastetext.png",command:"ipspasteplain",group:"clipboard",order:16}),a.contextMenu.addListener(function(a,c){inReadOnly=c.getRanges()[0].checkReadOnly();return{ipspasteplain:CKEDITOR.TRISTATE_OFF}}));var g=function(b,c){var e=a.getSelection().createBookmarks(!0);a.focus();if(c.data.insertPoint)a.getSelection().selectBookmarks(c.data.insertPoint),a.insertHtml(c.content);else if(c.data.placeholder){var d=
a.document.find("img.pastedImagePlaceholder"+c.data.placeholder);d.count()&&a.getSelection().selectElement(d.getItem(0));a.insertHtml(c.content)}a.focus();a.getSelection().selectBookmarks(e)},d=$(a.element.$).closest("[data-ipsEditor]").find("[data-ipsUploader]");if(d.length)d.on("injectedFileReadyForInsert",g);else{var h=$(a.element.$).closest("[data-ipsEditor]");h.on("uploaderReady",function(){d=h.find("[data-ipsUploader]");d.length&&!d.data("pasteReady")&&(d.on("injectedFileReadyForInsert",g),
d.data("pasteReady",!0))})}if("force"==a.config.ipsPasteBehaviour)a.on("paste",function(a){p(a);"paste"==a.data.method&&(a.data.type="text")},this,{},0);else a.on("paste",function(b){b.data.dataValue&&(CKEDITOR.env.gecko||CKEDITOR.env.edge||CKEDITOR.env.chrome&&!CKEDITOR.env.mac)&&"paste"==b.data.method||(p(b),b.data.dataTransfer.getFilesCount()&&(b.data.dataValue=""));if(b.data.dataValue){if(CKEDITOR.env.safari){var c=b.data.dataValue.match(/<img src="webkit-fake-url:\/\//);c&&(b.data.dataValue=
b.data.dataValue.replace(/<img src="webkit-fake-url:\/\/[^>]*>/,""),n());if(c=b.data.dataValue.match(/<img src="blob:(.+?)"/))b.data.dataValue=b.data.dataValue.replace(/<img src="blob:[^>]*>/,""),n()}if(!b.data.dataValue.match(/data-cke-widget-drag-handler/i)&&(c=b.data.dataValue.match(/<img[^>]*src=(["'])data:image\/(jpeg|png|gif|webp);base64,[A-Z0-9\/\+]*=*\1[^>]*>/gi)))if(d.length)for(var e=0;e<c.length;e++){var g=Math.random().toString(36).substring(7);b.data.dataValue=b.data.dataValue.replace(c[e],
'\x3cimg class\x3d"pastedImagePlaceholder'+g+'" content-editable\x3d"false"\x3e');for(var k=c[e].match(/src=(["'])data:(image\/(jpeg|png|gif|webp));base64,([A-Z0-9\/\+]*=*)\1/i),h=atob(k[4]),l=h.length,q=new Uint8Array(l);l--;)q[l]=h.charCodeAt(l);k=new File([q],"image."+k[3],{type:k[2]});d.trigger("injectFile",{file:k,data:{placeholder:g}})}else{for(e=0;e<c.length;e++)b.data.dataValue=b.data.dataValue.replace(c[e],"");n()}if(-1!==b.data.dataValue.replace(/<((\/)?(p|a( href=["'].+?['"])?)|br(.+?)?)>/g,
"").indexOf("\x3c")&&"force"!=a.config.ipsPasteBehaviour&&!b.data.force){var u=a.getSnapshot(),r=a.getSelection().createBookmarks2(!0),t=null;a.once("afterPaste",function(b){t=a.getSelection().createBookmarks2(!0);a.once("key",function(){m()});a.once("setData",function(){m()})});var f=$("."+a.id).closest("[data-ipsEditor]").find('[data-role\x3d"pasteMessage"]');f.slideDown({queue:!1});var m=function(){f.slideUp({queue:!1});f.find('[data-action\x3d"keepPasteFormatting"]').off("click.ipsPaste");f.find('[data-action\x3d"removePasteFormatting"]').off("click.ipsPaste")};
f.find('[data-action\x3d"keepPasteFormatting"]').on("click.ipsPaste",function(){a.focus();a.getSelection().selectBookmarks(t||r);m()});f.find('[data-action\x3d"removePasteFormatting"]').on("click.ipsPaste",function(){a.focus();a.loadSnapshot(u);a.getSelection().selectBookmarks(r);a.fire("paste",{type:"text",dataValue:b.data.dataValue,method:"paste",dataTransfer:CKEDITOR.plugins.clipboard.initPasteDataTransfer()});m()})}}},this)}});