Current section

Files

Jump to
ckeditor5_phoenix dist index.cjs
Raw

dist/index.cjs

"use strict";var P=Object.create;var y=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,C=Object.prototype.hasOwnProperty;var S=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of I(t))!C.call(n,o)&&o!==e&&y(n,o,{get:()=>t[o],enumerable:!(i=v(t,o))||i.enumerable});return n};var m=(n,t,e)=>(e=n!=null?P(A(n)):{},S(t||!n||!n.__esModule?y(e,"default",{value:n,enumerable:!0}):e,n));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function p(n,t){let e=null;return(...i)=>{e&&clearTimeout(e),e=setTimeout(()=>{t(...i)},n)}}class g{el;liveSocket;pushEvent;pushEventTo;handleEvent}function f(n){return{mounted(){const t=new n;this.el.instance=t,t.el=this.el,t.liveSocket=this.liveSocket,t.pushEvent=(e,i,o)=>this.pushEvent?.(e,i,o),t.pushEventTo=(e,i,o,r)=>this.pushEventTo?.(e,i,o,r),t.handleEvent=(e,i)=>this.handleEvent?.(e,i),t.mounted?.()},beforeUpdate(){this.el.instance.beforeUpdate?.()},destroyed(){this.el.instance.destroyed?.()},disconnected(){this.el.instance.disconnected?.()},reconnected(){this.el.instance.reconnected?.()}}}function w(n,t){const e=Object.entries(n).map(([i,o])=>[i,t(o,i)]);return Object.fromEntries(e)}function H(n){if(n===null)return null;const t=Number.parseInt(n,10);return Number.isNaN(t)?null:t}class l{static the=new l;editors=new Map;callbacks=new Map;constructor(){}execute(t,e){const{callbacks:i,editors:o}=this,r=o.get(t);return r?Promise.resolve(e(r)):new Promise(s=>{const a=async u=>s(await e(u));this.callbacks.has(t)||i.set(t,[]),i.set(t,[...i.get(t),a])})}register(t,e){const{editors:i,callbacks:o}=this,r=o.get(t);if(i.has(t))throw new Error(`Editor with ID "${t}" is already registered.`);i.set(t,e),r&&(r.forEach(s=>s(e)),o.delete(t)),this.editors.size===1&&this.register(null,e)}unregister(t){const{editors:e,callbacks:i}=this;if(!e.has(t))throw new Error(`Editor with ID "${t}" is not registered.`);t&&this.editors.get(null)===e.get(t)&&this.unregister(null),e.delete(t),i.delete(t)}getEditors(){return Array.from(this.editors.values())}hasEditor(t){return this.editors.has(t)}waitForEditor(t){return this.execute(t,e=>e)}async destroyAllEditors(){const t=Array.from(this.editors.values()).map(e=>e.destroy());this.editors.clear(),this.callbacks.clear(),await Promise.all(t)}}class R extends g{mountedPromise=null;get attrs(){const t={editableId:this.el.getAttribute("id"),editorId:this.el.getAttribute("data-cke-editor-id")||null,rootName:this.el.getAttribute("data-cke-editable-root-name"),initialValue:this.el.getAttribute("data-cke-editable-initial-value")||""};return Object.defineProperty(this,"attrs",{value:t,writable:!1,configurable:!1,enumerable:!0}),t}async mounted(){const{editableId:t,editorId:e,rootName:i,initialValue:o}=this.attrs,r=this.el.querySelector(`#${t}_input`);this.mountedPromise=l.the.execute(e,s=>{const{ui:a,editing:u,model:h}=s;if(h.document.getRoot(i))return;s.addRoot(i,{isUndoable:!1,data:o});const c=this.el.querySelector("[data-cke-editable-content]"),d=a.view.createEditable(i,c);a.addEditable(d),u.view.forceRender(),r&&$(r,s,i)})}async destroyed(){const{editorId:t,rootName:e}=this.attrs;this.el.style.display="none",await this.mountedPromise,this.mountedPromise=null,await l.the.execute(t,i=>{const o=i.model.document.getRoot(e);o&&(i.detachEditable(o),i.detachRoot(e,!1))})}}const V=f(R);function $(n,t,e){const i=()=>{n.value=t.getData({rootName:e})};t.model.document.on("change:data",p(100,i)),i()}function E(n){return["inline","classic","balloon","decoupled"].includes(n)}async function O(n){const t=await import("ckeditor5"),i={inline:t.InlineEditor,balloon:t.BalloonEditor,classic:t.ClassicEditor,decoupled:t.DecoupledEditor,multiroot:t.MultiRootEditor}[n];if(!i)throw new Error(`Unsupported editor type: ${n}`);return i}async function T(n){const t=await import("ckeditor5");let e=null;const i=n.map(async o=>{const{[o]:r}=t;if(r)return r;if(!e)try{e=await import("ckeditor5-premium-features")}catch(a){console.error(`Failed to load premium package: ${a}`)}const{[o]:s}=e||{};if(s)return s;throw new Error(`Plugin "${o}" not found in base or premium packages.`)});return Promise.all(i)}function b(n){const t=document.querySelectorAll([`[data-cke-editor-id="${n}"][data-cke-editable-root-name]`,"[data-cke-editable-root-name]:not([data-cke-editor-id])"].join(", "));return Array.from(t).reduce((e,i)=>{const o=i.getAttribute("data-cke-editable-root-name"),r=i.getAttribute("data-cke-editable-initial-value")||"",s=i.querySelector("[data-cke-editable-content]");return!o||!s?e:{...e,[o]:{content:s,initialValue:r}}},Object.create({}))}const k=["inline","classic","balloon","decoupled","multiroot"];function j(n){const t=n.getAttribute("cke-preset");if(!t)throw new Error('CKEditor5 hook requires a "cke-preset" attribute on the element.');const{type:e,config:i,license:o}=JSON.parse(t);if(!e||!i||!o)throw new Error('CKEditor5 hook configuration must include "editor", "config", and "license" properties.');if(!k.includes(e))throw new Error(`Invalid editor type: ${e}. Must be one of: ${k.join(", ")}.`);return{type:e,config:i,license:o}}function N(n,t){const{editing:e}=n;e.view.change(i=>{i.setStyle("height",`${t}px`,e.view.document.getRoot())})}class U extends g{editorPromise=null;get attrs(){const t={editorId:this.el.getAttribute("id"),preset:j(this.el),editableHeight:H(this.el.getAttribute("cke-editable-height"))};return Object.defineProperty(this,"attrs",{value:t,writable:!1,configurable:!1,enumerable:!0}),t}async mounted(){return this.editorPromise=this.createEditor(),l.the.register(this.attrs.editorId,await this.editorPromise),this}async destroyed(){this.el.style.display="none",(await this.editorPromise)?.destroy(),this.editorPromise=null,l.the.unregister(this.attrs.editorId)}async createEditor(){const{preset:t,editorId:e,editableHeight:i}=this.attrs,{type:o,license:r,config:{plugins:s,...a}}=t,u=await O(o),h=M(e,o),c=await u.create(h,{...a,initialData:K(e,o),licenseKey:r.key,plugins:await T(s)});if(this.setupContentSync(e,c),E(o)){const d=document.getElementById(`${e}_input`);d&&D(d,c),i&&N(c,i)}return c}setupContentSync(t,e){const i=()=>{this.pushEvent("ckeditor5:change",{editorId:t,data:x(e)})};e.model.document.on("change:data",p(250,i)),i(),this.handleEvent("ckeditor5:set-data",({data:o})=>{e.setData(o)})}}function x(n){return n.model.document.getRootNames().reduce((e,i)=>(e[i]=n.getData({rootName:i}),e),Object.create({}))}function D(n,t){const e=()=>{n.value=t.getData()};t.model.document.on("change:data",p(250,e)),e()}function M(n,t){if(E(t))return document.getElementById(`${n}_editor`);const e=b(n);return w(e,({content:i})=>i)}function K(n,t){if(t==="decoupled"){const i=b(n).main?.initialValue;if(i)return i}if(E(t))return document.getElementById(n)?.getAttribute("cke-initial-value")||"";const e=b(n);return w(e,({initialValue:i})=>i)}const q=f(U);class B extends g{mountedPromise=null;get attrs(){const t={editorId:this.el.getAttribute("data-cke-editor-id")||null,name:this.el.getAttribute("data-cke-ui-part-name")};return Object.defineProperty(this,"attrs",{value:t,writable:!1,configurable:!1,enumerable:!0}),t}async mounted(){const{editorId:t,name:e}=this.attrs;this.mountedPromise=l.the.execute(t,i=>{const{ui:o}=i,r=F(e),s=o.view[r];if(!s){console.error(`Unknown UI part name: "${e}". Supported names are "toolbar" and "menubar".`);return}this.el.appendChild(s.element)})}async destroyed(){this.el.style.display="none",await this.mountedPromise,this.mountedPromise=null,this.el.innerHTML=""}}function F(n){switch(n){case"toolbar":return"toolbar";case"menubar":return"menuBarView";default:return null}}const _=f(B),L={CKEditor5:q,CKEditable:V,CKUIPart:_};exports.EditorsRegistry=l;exports.Hooks=L;
//# sourceMappingURL=index.cjs.map