| 1 |
var CharitableCampaignBuilderFieldHTML=window.CharitableCampaignBuilderFieldHTML||function(e,t,i){var r,o=!1,n=[],d={settings:{load_html_option:!1},state:{editors:{},addEditor:function(e,t){this.editors[e]=t},getEditor:function(e){return this.editors[e]||null},removeEditor:function(e){this.editors[e]&&delete this.editors[e]}},init:function(){wpchar.debug("init","field-html-js");charitable_panel_switch=!0,s=this.settings,this.handleResize(),this.setupCleanup(),i(d.ready),i(t).on("load",function(){"function"==typeof i.ready.then?i.ready.then(d.load):d.load()}),this.initErrorHandling()},initErrorHandling:function(){t.onerror=(e,t,i,r,o)=>-1!==e.indexOf("CodeMirror")&&(console.error("CodeMirror Error:",{message:e,url:t,lineNo:i,columnNo:r,error:o}),!0)},recoverEditor:function(e){const t=this.state.getEditor(e);if(t&&t.codemirror)try{t.codemirror.refresh();const e=t.codemirror.getWrapperElement();e&&"none"===e.style.display&&(e.style.display="",t.codemirror.refresh())}catch(t){console.error("Editor recovery failed:",t),this.destroy(e),this.codemirrorInit(e)}},initAutoSave:function(e){const t=this.state.getEditor(e);if(!t||!t.codemirror)return;let i;t.codemirror.on("change",()=>{clearTimeout(i),i=setTimeout(()=>{const i=t.codemirror.getValue();this.saveEditorContent(e,i)},1e3)})},saveEditorContent:function(e,t){const r=i("#charitable-panel-field-settings-field_html_html_"+e);r.length&&(r.val(t).trigger("change"),wpchar.debug("Content saved for editor: "+e,"field-html-js"))},cleanupMemory:function(){Object.keys(this.state.editors).forEach(e=>{i("#charitable-panel-field-settings-field_html_html_"+e).length||this.destroy(e)}),i(".CodeMirror").each((e,t)=>{const r=i(t);r.next("textarea").length||r.remove()})},initPeriodicCleanup:function(){setInterval(()=>{this.cleanupMemory()},3e5)},ready:function(){wpchar.debug("ready","field-html-js"),r=i("#charitable-builder"),d.initPeriodicCleanup(),r.on("charitableFieldAddHTML",function(e,t,i){if(wpchar.debug("Field added: "+t,"field-html-js"),wpchar.debug("Field type: "+i,"field-html-js"),"html"===i){if(parseInt(t)>0){wpchar.debug("Field ID is valid: "+t,"field-html-js");d.state.getEditor(t)?wpchar.debug("Recovering existing codemirror at "+t,"field-html-js"):-1===n.indexOf(t)&&(wpchar.debug("Initializing new codemirror at "+t,"field-html-js"),d.codemirrorInit(t),d.initAutoSave(t))}}else wpchar.debug("Filed type is not HTML: "+i,"field-html-js")}),r.on("charitableFieldEdit",function(e,t,i,r,o,a){if("html"===a&&(""==r&&""!==o&&(r=o),parseInt(r)>0)){d.state.getEditor(r)?d.recoverEditor(r):-1===n.indexOf(r)&&(wpchar.debug("Initializing new codemirror at "+r,"field-html-js"),d.codemirrorInit(r),d.initAutoSave(r))}}),r.on("charitableFieldDelete",function(e,t){n.includes(t)&&d.destroy(t)}),r.on("charitablePanelSwitch",function(){setTimeout(()=>{Object.keys(d.state.editors).forEach(e=>{const t=d.state.getEditor(e);t&&t.codemirror&&t.codemirror.refresh()})},100)}),i(t).on("focus",()=>{Object.keys(d.state.editors).forEach(e=>{const t=d.state.getEditor(e);t&&t.codemirror&&t.codemirror.refresh()})})},load:function(){wpchar.debug("load","field-html-js"),setTimeout(()=>{Object.keys(d.state.editors).forEach(e=>{const t=d.state.getEditor(e);t&&t.codemirror&&t.codemirror.refresh()})},500)},destroy:function(e){o&&o.codemirror&&(o.codemirror.toTextArea(),o=null);const t=n.indexOf(e);t>-1&&n.splice(t,1)},validateField:function(e){const t=i("#charitable-panel-field-settings-field_html_html_"+e);return t.length?t.is("textarea")?!(t.parent().find(".CodeMirror").length>0)||(wpchar.debug("CodeMirror already initialized for: "+e,"field-html-js"),!1):(wpchar.debug("Field is not a textarea: "+e,"field-html-js"),!1):(wpchar.debug("Field not found: "+e,"field-html-js"),!1)},waitForElement:function(e,t,r=10){let o=0;const n=setInterval(function(){o++;const d=i("#charitable-panel-field-settings-field_html_html_"+e);d.length?(clearInterval(n),t(d),wpchar.debug("Trying to initialize field: "+e,"field-html-js")):o>=r&&(clearInterval(n),wpchar.debug("Field not found after "+r+" attempts","field-html-js"))},500)},codemirrorInit:function(e){this.waitForElement(e,t=>{if(!this.validateField(e))return;const i=this.getEditorSettings();t.css("visibility","hidden");try{setTimeout(()=>{const r=wp.codeEditor.initialize(t[0],i);r&&r.codemirror&&(this.state.addEditor(e,r),r.codemirror.on("change",()=>{r.codemirror.save(),t.trigger("change")}),t.css("visibility","visible"),n.push(e),r.codemirror.setOption("extraKeys",{"Ctrl-Space":"autocomplete",Tab:function(e){e.somethingSelected()?e.indentSelection("add"):e.replaceSelection(Array(e.getOption("indentUnit")+1).join(" "))}}),r.codemirror.refresh(),wpchar.debug("CodeMirror initialized for: "+e,"field-html-js"))},500)}catch(e){console.error("CodeMirror initialization error:",e),t.css("visibility","visible")}})},getEditorSettings:function(){const e=wp.codeEditor.defaultSettings?_.clone(wp.codeEditor.defaultSettings):{};return{codemirror:_.extend({},e.codemirror,{mode:"html",lineNumbers:!0,lineWrapping:!0,indentUnit:2,tabSize:2,autoCloseTags:!0,autoCloseBrackets:!0,matchBrackets:!0,foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],extraKeys:{"Ctrl-Space":"autocomplete"},hintOptions:{completeSingle:!1},theme:"default",scrollbarStyle:"overlay"})}},handleResize:function(){i(t).on("resize",_.debounce(()=>{Object.values(this.state.editors).forEach(e=>{e&&e.codemirror&&e.codemirror.refresh()})},250))},setupCleanup:function(){i(t).on("beforeunload",()=>{Object.keys(this.state.editors).forEach(e=>{this.destroy(e)})})},diagnostics:function(){return{activeEditors:Object.keys(this.state.editors).length,trackedFields:n.length,editorInstances:i(".CodeMirror").length,textareaFields:i("[id^=charitable-panel-field-settings-field_html_html_]").length,memoryUsage:t.performance&&t.performance.memory?t.performance.memory.usedJSHeapSize/1048576+" MB":"Not available"}}};return d}(document,window,jQuery);CharitableCampaignBuilderFieldHTML.init(); |