function readFile(e,p){const l=new FileReader;l.onload=c=>{const{result:t}=c.target,n=t.indexOf("base64")+7,o=t.slice(n,t.length);p(e.name,o)},l.readAsDataURL(e)}window.tinymce.PluginManager.add("jdragndrop",e=>{const p=e.options.register;p("uploadUri",{processor:"string"}),p("parentUploadFolder",{processor:"string"});const l=t=>{t.dataTransfer.types.includes("Files")&&(t.stopPropagation(),t.preventDefault(),e.contentAreaContainer.style.borderWidth="0")};window.tinyMCE.DOM.bind(document,"dragleave",l),e.on("remove",()=>window.tinyMCE.DOM.unbind(document,"dragleave",l)),e.on("dragenter",t=>{t.dataTransfer.types.includes("Files")&&t.stopPropagation()}),e.on("dragover",t=>{t.dataTransfer.types.includes("Files")&&(t.preventDefault(),e.contentAreaContainer.style.borderStyle="dashed",e.contentAreaContainer.style.borderWidth="5px")});async function c(t,n){const o=e.options.get;Joomla.request({url:`${o("uploadUri")}&path=${o("parentUploadFolder")}`,method:"POST",data:JSON.stringify({name:t,content:n,parent:o("parentUploadFolder")}),headers:{"Content-Type":"application/json"},promise:!0}).then(d=>{let a;try{a=JSON.parse(d.responseText)}catch(r){e.windowManager.alert(`${Joomla.Text._("ERROR")}: {${r}}`)}if(a.data&&a.data.path){const r=a.data;let u;const y=Joomla.getOptions("system.paths"),{rootFull:T}=y,g=a.data.url.split(T);g.length>1?u=`${g[1]}`:r.url&&(u=r.url);const m=function(h){const s=h.getData(),f=s.altEmpty?' alt=""':"",D=s.altText?` alt="${s.altText}"`:f,_=s.isLazy?' loading="lazy"':"",w=s.isLazy?` width="${r.width}"`:"",b=s.isLazy?` height="${r.height}"`:"";e.execCommand("mceInsertContent",!1,`<img src="${u}"${D}${_}${w}${b}/>`)};e.windowManager.open({title:Joomla.Text._("PLG_TINY_DND_ADDITIONALDATA"),body:{type:"panel",items:[{type:"input",name:"altText",label:Joomla.Text._("PLG_TINY_DND_ALTTEXT")},{type:"checkbox",name:"altEmpty",label:Joomla.Text._("PLG_TINY_DND_EMPTY_ALT")},{type:"checkbox",name:"isLazy",label:Joomla.Text._("PLG_TINY_DND_LAZYLOADED")}]},buttons:[{type:"cancel",text:"Cancel"},{type:"submit",name:"submitButton",text:"Save",primary:!0}],initialData:{altText:"",isLazy:!0,altEmpty:!1},onSubmit:i=>{m(i),i.close()},onCancel:i=>m(i)})}}).catch(d=>{let a=`Error: ${d.statusText}`;d.status===409&&(a=Joomla.Text._("PLG_TINY_DND_FILE_EXISTS_ERROR").replace("%s",`${o("parentUploadFolder")}/${t}`)),e.windowManager.alert(a)})}return e.on("drop",t=>{t.dataTransfer.types.includes("Files")&&(t.preventDefault(),t.dataTransfer.files.length>0&&Array.from(t.dataTransfer.files).forEach(n=>{n.name.toLowerCase().match(/\.(jpg|jpeg|png|gif|webp)$/)&&readFile(n,c)}),e.contentAreaContainer.style.borderWidth="0")}),{getMetadata:()=>({name:"Drag and Drop (Joomla)",url:"https://www.joomla.org/"})}});
