Packages

CKEditor 5 integration for Phoenix Framework

Retired package: Renamed - Moved to ckeditor5-phoenix

Current section

Files

Jump to
ckeditor5 dist index.cjs
Raw

dist/index.cjs

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