1 line
1.9 KiB
JavaScript
1 line
1.9 KiB
JavaScript
import{JoomlaEditorDecorator,JoomlaEditorButton}from"editor-api";import JoomlaDialog from"joomla.dialog";if(!window.Joomla)throw new Error("JoomlaEditors API require Joomla to be loaded.");Joomla.editors=Joomla.editors||{},Joomla.editors.instances=new Proxy({},{set:(o,t,e)=>(e instanceof JoomlaEditorDecorator||(e.getId=()=>t,console.warn("Legacy editors is deprecated. Register the editor instance with JoomlaEditor.register().",t,e)),o[t]=e,!0),get:(o,t)=>(console.warn("Direct access to Joomla.editors.instances is deprecated. Use JoomlaEditor.getActive() or JoomlaEditor.get(id) to retrieve the editor instance."),o[t])}),JoomlaEditorButton.registerAction("insert",((o,t)=>{const e=t.content||"";o.replaceSelection(e)})),JoomlaEditorButton.registerAction("modal",((o,t)=>{if(t.src&&"#"!==t.src[0]&&"."!==t.src[0]){const e=0===t.src.indexOf("http")?new URL(t.src):new URL(t.src,window.location.origin);e.searchParams.set("editor",o.getId()),e.searchParams.has("e_name")&&e.searchParams.set("e_name",o.getId()),t.src=e.toString()}const e=new JoomlaDialog(t),a=t=>{t.origin===window.location.origin&&("joomla:content-select"===t.data.messageType?(o.replaceSelection(t.data.html||t.data.text),e.close()):"joomla:cancel"===t.data.messageType&&e.close())};window.JoomlaExpectingPostMessage=!0,window.addEventListener("message",a),e.addEventListener("joomla-dialog:close",(()=>{delete window.JoomlaExpectingPostMessage,window.removeEventListener("message",a),Joomla.Modal.setCurrent(null),e.destroy()})),Joomla.Modal.setCurrent(e),e.show()}));const btnDelegateSelector="[data-joomla-editor-button-action]",btnActionDataAttr="joomlaEditorButtonAction",btnConfigDataAttr="joomlaEditorButtonOptions";document.addEventListener("click",(o=>{const t=o.target.closest(btnDelegateSelector);if(!t)return;const e=t.dataset[btnActionDataAttr],a=t.dataset[btnConfigDataAttr]?JSON.parse(t.dataset[btnConfigDataAttr]):{};e&&JoomlaEditorButton.runAction(e,a,t)})); |