window.jvbQuill=function(t){const n=t.querySelectorAll("textarea[data-editor=true]"),e=[];return n.forEach(t=>{let n,i,l;if(t.parentNode.querySelector(".editor-container"))n=t.parentNode.querySelector(".editor-container"),i=n.querySelector(".editor"),l=n.querySelector(".toolbar");else{n=document.createElement("div"),n.className="editor-container",i=document.createElement("div"),i.className="editor",l=document.createElement("div"),l.className="toolbar";const e=!0===t.dataset.allowimage?`<button type="button" class="ql-jvb_image">\n ${window.getIcon("image")}\n </button>`:"";l.id=`toolbar-${t.id}`,l.innerHTML=`\n <span class="ql-formats">\n <button type="button" class="ql-p">\n <i class="icon icon-paragraph"></i>\n </button>\n <button type="button" class="ql-h1">\n <i class="icon icon-text-h-one"></i>\n </button>\n <button type="button" class="ql-h2">\n <i class="icon icon-text-h-two"></i>\n </button>\n <button type="button" class="ql-h3">\n <i class="icon icon-text-h-three"></i>\n </button>\n </span>\n <span class="ql-formats">\n <button type="button" class="ql-jvb_bold">\n <i class="icon icon-text-b-fi"></i>\n </button>\n <button type="button" class="ql-jvb_italic">\n <i class="icon icon-text-italic"></i>\n </button>\n <button type="button" class="ql-jvb_underline">\n <i class="icon icon-text-underline"></i>\n </button>\n <button type="button" class="ql-jvb_strike">\n <i class="icon icon-text-strikethrough"></i>\n </button>\n </span>\n <span class="ql-formats">\n <button type="button" class="ql-jvb_list" value="bullet">\n <i class="icon icon-list-dashes"></i>\n </button>\n <button type="button" class="ql-jvb_list" value="ordered">\n <i class="icon icon-list-numbers"></i>\n </button>\n </span>\n <span class="ql-formats">\n <button type="button" class="ql-jvb_align" value="left">\n <i class="icon icon-text-align-left"></i>\n </button>\n <button type="button" class="ql-jvb_align" value="center">\n <i class="icon icon-text-align-center"></i>\n </button>\n <button type="button" class="ql-jvb_align" value="right">\n <i class="icon icon-text-align-right"></i>\n </button>\n </span>\n <span class="ql-formats">\n <button type="button" class="ql-jvb_link">\n <i class="icon icon-link"></i>\n </button>\n ${e}\n </span>\n `,n.appendChild(l),n.appendChild(i),t.parentNode.insertBefore(n,t),t.style.display="none",i.innerHTML=t.value}const o=new Quill(i,{theme:"snow",modules:{toolbar:{container:l,handlers:{p:function(){this.quill.format("header",!1)},h1:function(){this.quill.format("header",1)},h2:function(){this.quill.format("header",2)},h3:function(){this.quill.format("header",3)},jvb_bold:function(){const t=this.quill.getFormat();this.quill.format("bold",!t.bold)},jvb_italic:function(){const t=this.quill.getFormat();this.quill.format("italic",!t.italic)},jvb_strike:function(){const t=this.quill.getFormat();this.quill.format("strike",!t.strike)},jvb_underline:function(){const t=this.quill.getFormat();this.quill.format("underline",!t.underline)},jvb_align:function(t){const n=this.quill.getFormat();this.quill.format("align",t!==n.align&&t)},jvb_list:function(t){const n=this.quill.getFormat();this.quill.format("list",t!==n.list&&t)},jvb_link:function(t){if(t){const t=this.quill.getSelection();if(null==t||0===t.length)return;const n=this.quill.getFormat(t).link,e=document.createElement("dialog");e.className="quill-link-modal",e.innerHTML=`\n <div class="quill-link-modal-content ">\n <label for="link">Enter URL</label>\n <input type="url" id="link" placeholder="Enter URL" value="${n||""}" />\n <div class="buttons">\n <button type="button" class="save">Save</button>\n ${n?'<button type="button" class="remove">Remove</button>':""}\n <button type="button" class="cancel">Cancel</button>\n </div>\n </div>\n `,document.body.appendChild(e),e.showModal();const i=e.querySelector("input");i.focus(),e.querySelector(".save").addEventListener("click",()=>{const t=i.value;t&&this.quill.format("link",t),e.remove()});const l=e.querySelector(".remove");l&&l.addEventListener("click",()=>{this.quill.format("link",!1),e.remove()}),e.querySelector(".cancel").addEventListener("click",()=>{e.remove()}),i.addEventListener("keyup",t=>{if("Enter"===t.key){const t=i.value;t&&this.quill.format("link",t),e.remove()}})}},jvb_image:function(){const n=t.dataset.postId||t.closest("form")?.dataset.postId,e=document.createElement("input");e.setAttribute("type","file"),e.setAttribute("accept","image/jpeg,image/png,image/gif,image/webp"),e.style.display="none",document.body.appendChild(e),e.onchange=async t=>{const i=t.target.files?.[0];if(!i)return;if(i.size>5242880)return this.quill.insertText(l.index,"File too large. Maximum size is 5MB",{color:"#f00",italic:!0},!0),void e.remove();const l=this.quill.getSelection(!0),o=new FormData;o.append("image",i),n&&o.append("post_id",n);try{const t=await fetch(`${jvbSettings.api}uploads/`,{method:"POST",headers:{"X-WP-Nonce":window.auth.getNonce()},body:o});if(!t.ok)throw new Error("Upload failed");const n=await t.json();this.quill.insertEmbed(l.index,"image",n.url)}catch(t){console.error("Upload error:",t),this.quill.insertText(l.index,"Failed to upload image. Please try again.",{color:"#f00",italic:!0},!0)}finally{e.remove()}},e.click()}}},history:{delay:2e3,maxStack:500},clipboard:{matchVisual:!1}}});e.push(o),o.on("selection-change",function(t){if(!t)return;const n=o.getFormat(t);Object.entries({"ql-jvb_bold":"bold","ql-jvb_italic":"italic","ql-jvb_underline":"underline","ql-jvb_strike":"strike"}).forEach(([t,e])=>{const i=l.querySelector(`.${t}`);i&&i.classList.toggle("active",!!n[e])}),l.querySelectorAll(".ql-jvb_list").forEach(t=>{const e=t.getAttribute("value");t.classList.toggle("ql-active",n.list===e)}),l.querySelectorAll(".ql-jvb_align").forEach(t=>{const e=t.getAttribute("value");t.classList.toggle("ql-active",n.align===e)});const e=l.querySelector(".ql-align");if(e){if(0===t.length){const[n]=this.quill.getLeaf(t.index);if(n&&n.domNode&&"IMG"===n.domNode.tagName)return void(e.style.display="inline-block")}e.style.display="none"}}),o.on("text-change",()=>{t.value=o.root.innerHTML,t.dispatchEvent(new Event("change",{bubbles:!0}))})}),e};
|