@meenainwal/rich-text-editor 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class u{getSelection(){return window.getSelection()}getRange(){const e=this.getSelection();return!e||e.rangeCount===0?null:e.getRangeAt(0)}saveSelection(){const e=this.getRange();return e?e.cloneRange():null}restoreSelection(e){if(!e)return;const t=this.getSelection();t&&(t.removeAllRanges(),t.addRange(e))}isSelectionInElement(e){const t=this.getRange();return t?e.contains(t.commonAncestorContainer):!1}clearSelection(){const e=this.getSelection();e&&e.removeAllRanges()}}class g{editor;activeContainer=null;isResizing=!1;startX=0;startY=0;startWidth=0;startHeight=0;currentHandle=null;aspectRatio=1;boundMouseDown;boundMouseMove;boundMouseUp;boundKeyDown;constructor(e){this.editor=e,this.boundMouseDown=this.handleMouseDown.bind(this),this.boundMouseMove=this.handleMouseMove.bind(this),this.boundMouseUp=this.handleMouseUp.bind(this),this.boundKeyDown=this.handleKeyDown.bind(this),this.setupListeners()}setupListeners(){const e=this.editor.el;e.addEventListener("mousedown",this.boundMouseDown),window.addEventListener("mousemove",this.boundMouseMove),window.addEventListener("mouseup",this.boundMouseUp),e.addEventListener("keydown",this.boundKeyDown)}handleMouseDown(e){const t=e.target;if(t.classList.contains("te-image-resizer")){e.preventDefault(),e.stopPropagation();const n=t.closest(".te-image-container");n&&(this.selectImage(n),this.startResize(e,t));return}const i=t.closest(".te-image-container");i?this.selectImage(i):this.deselectImage()}handleMouseMove(e){this.isResizing&&this.handleResize(e)}handleMouseUp(){this.isResizing&&this.stopResize()}handleKeyDown(e){(e.key==="Backspace"||e.key==="Delete")&&this.activeContainer&&(e.preventDefault(),this.activeContainer.remove(),this.activeContainer=null,this.editor.el.dispatchEvent(new Event("input",{bubbles:!0})))}destroy(){const e=this.editor.el;e&&(e.removeEventListener("mousedown",this.boundMouseDown),e.removeEventListener("keydown",this.boundKeyDown)),window.removeEventListener("mousemove",this.boundMouseMove),window.removeEventListener("mouseup",this.boundMouseUp)}selectImage(e){this.activeContainer&&this.activeContainer.classList.remove("active"),this.activeContainer=e,this.activeContainer.classList.add("active")}deselectImage(){this.activeContainer&&(this.activeContainer.classList.remove("active"),this.activeContainer=null)}startResize(e,t){if(!this.activeContainer)return;this.isResizing=!0,this.currentHandle=Array.from(t.classList).find(n=>n.startsWith("te-resizer-"))?.replace("te-resizer-","")||null;const i=this.activeContainer.querySelector("img");this.startX=e.clientX,this.startY=e.clientY,this.startWidth=i.clientWidth,this.startHeight=i.clientHeight,this.aspectRatio=this.startWidth/this.startHeight,document.body.style.cursor=window.getComputedStyle(t).cursor}handleResize(e){if(!this.activeContainer||!this.isResizing)return;const t=this.activeContainer.querySelector("img"),i=e.clientX-this.startX,n=e.clientY-this.startY;let s=this.startWidth,o=this.startHeight;this.currentHandle?.includes("right")?s=this.startWidth+i:this.currentHandle?.includes("left")?s=this.startWidth-i:this.currentHandle?.includes("bottom")?s=this.startWidth+n*this.aspectRatio:this.currentHandle?.includes("top")&&(s=this.startWidth-n*this.aspectRatio),o=s/this.aspectRatio,s>50&&s<this.editor.el.clientWidth&&(t.style.width=`${s}px`,t.style.height=`${o}px`)}stopResize(){this.isResizing=!1,this.currentHandle=null,document.body.style.cursor="",this.editor.el.dispatchEvent(new Event("input",{bubbles:!0}))}}class m{stack=[];index=-1;maxDepth=50;constructor(e){e!==void 0&&this.record(e)}record(e){this.index>=0&&this.stack[this.index].html===e||(this.index<this.stack.length-1&&(this.stack=this.stack.slice(0,this.index+1)),this.stack.push({html:e}),this.index++,this.stack.length>this.maxDepth&&(this.stack.shift(),this.index--))}undo(){return this.index>0?(this.index--,this.stack[this.index].html):null}redo(){return this.index<this.stack.length-1?(this.index++,this.stack[this.index].html):null}canUndo(){return this.index>0}canRedo(){return this.index<this.stack.length-1}}class p{container;editableElement;selection;imageManager;history;options;saveTimeout=null;historyTimeout=null;pendingStyles={};observer=null;eventListeners=[];constructor(e,t={}){if(this.options=t,this.container=e,typeof document>"u"||!e){this.editableElement={},this.selection={},this.imageManager={},this.history={};return}this.container.innerHTML="",this.container.classList.add("te-container"),this.options.dark&&this.container.classList.add("te-dark"),this.editableElement=this.createEditableElement(),this.selection=new u,this.imageManager=new g(this),this.history=new m(this.editableElement.innerHTML),this.setupInputHandlers(),this.setupImageObserver(),this.checkPlaceholder(),this.container.appendChild(this.editableElement),this.options.autofocus&&this.focus(),this.options.theme&&this.applyTheme(this.options.theme),document.execCommand("defaultParagraphSeparator",!1,"p")}applyTheme(e){const t=this.container,i={primaryColor:"--te-primary-color",primaryHover:"--te-primary-hover",bgApp:"--te-bg-app",bgEditor:"--te-bg-editor",toolbarBg:"--te-toolbar-bg",borderColor:"--te-border-color",borderFocus:"--te-border-focus",textMain:"--te-text-main",textMuted:"--te-text-muted",placeholder:"--te-placeholder",btnHover:"--te-btn-hover",btnActive:"--te-btn-active",radiusLg:"--te-radius-lg",radiusMd:"--te-radius-md",radiusSm:"--te-radius-sm",shadowSm:"--te-shadow-sm",shadowMd:"--te-shadow-md",shadowLg:"--te-shadow-lg"};for(const[n,s]of Object.entries(i)){const o=e[n];o&&t.style.setProperty(s,o)}}setDarkMode(e){this.options.dark=e,e?this.container.classList.add("te-dark"):this.container.classList.remove("te-dark")}destroy(){this.observer&&(this.observer.disconnect(),this.observer=null),this.saveTimeout&&clearTimeout(this.saveTimeout),this.historyTimeout&&clearTimeout(this.historyTimeout),this.imageManager&&typeof this.imageManager.destroy=="function"&&this.imageManager.destroy(),this.eventListeners.forEach(({target:e,type:t,handler:i})=>{e.removeEventListener(t,i)}),this.eventListeners=[],this.container.innerHTML="",this.container.classList.remove("te-container","te-dark"),this.container.removeAttribute("style")}checkPlaceholder(){if(!this.editableElement)return;this.editableElement.textContent?.trim()===""&&!this.editableElement.querySelector("img")&&!this.editableElement.querySelector("table")?this.editableElement.classList.add("is-empty"):this.editableElement.classList.remove("is-empty")}addEventListener(e,t,i,n){e.addEventListener(t,i,n),this.eventListeners.push({target:e,type:t,handler:i})}setupImageObserver(){this.observer=new MutationObserver(e=>{e.forEach(t=>{t.addedNodes.forEach(i=>{if(i.nodeType===Node.ELEMENT_NODE){const n=i;n.tagName==="IMG"&&!n.closest(".te-image-container")?this.wrapImage(n):n.querySelectorAll("img:not(.te-image)").forEach(o=>{o.closest(".te-image-container")||this.wrapImage(o)})}})})}),this.observer.observe(this.editableElement,{childList:!0,subtree:!0})}wrapImage(e){const t=e.parentElement;if(!t)return;const i=document.createElement("figure");i.classList.add("te-image-container"),i.setAttribute("contenteditable","false");const n=document.createElement("img");n.src=e.src,n.alt=e.alt||"",e.width&&(n.style.width=`${e.width}px`),e.height&&(n.style.height=`${e.height}px`),n.classList.add("te-image");const s=document.createElement("figcaption");if(s.classList.add("te-image-caption"),s.setAttribute("contenteditable","true"),s.setAttribute("data-placeholder","Type caption..."),["top-left","top-right","bottom-left","bottom-right"].forEach(a=>{const l=document.createElement("div");l.classList.add("te-image-resizer",`te-resizer-${a}`),i.appendChild(l)}),i.appendChild(n),i.appendChild(s),t.replaceChild(i,e),!i.nextElementSibling){const a=document.createElement("p");a.innerHTML="<br>",i.after(a)}}setupInputHandlers(){this.addEventListener(this.editableElement,"beforeinput",e=>{if(e.inputType==="insertText"&&Object.keys(this.pendingStyles).length>0){const t=e.data;if(!t)return;e.preventDefault();const i=document.createElement("span");for(const[s,o]of Object.entries(this.pendingStyles))i.style.setProperty(s,o);i.textContent=t;const n=this.selection.getRange();if(n){n.deleteContents(),n.insertNode(i);const s=document.createRange();s.setStart(i.firstChild,t.length),s.setEnd(i.firstChild,t.length),this.selection.restoreSelection(s),this.pendingStyles={},this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}}}),this.addEventListener(this.editableElement,"input",()=>{this.checkPlaceholder()}),this.addEventListener(document,"selectionchange",()=>{const e=window.getSelection();e&&e.rangeCount>0&&(e.getRangeAt(0).collapsed||(this.pendingStyles={}))}),this.addEventListener(this.editableElement,"dragover",e=>{e.preventDefault(),e.dataTransfer.dropEffect="copy",this.editableElement.classList.add("dragover")}),this.addEventListener(this.editableElement,"dragleave",()=>{this.editableElement.classList.remove("dragover")}),this.addEventListener(this.editableElement,"drop",e=>{e.preventDefault(),this.editableElement.classList.remove("dragover");const t=e.dataTransfer?.files;if(t&&t.length>0)for(let i=0;i<t.length;i++){const n=t[i];if(n.type.startsWith("image/")){const s=new FileReader;s.onload=o=>{const a=o.target?.result;this.insertImage(a)},s.readAsDataURL(n)}}}),this.addEventListener(this.editableElement,"paste",e=>{const t=e.clipboardData;if(!t)return;if(t.items)for(let s=0;s<t.items.length;s++){const o=t.items[s];if(o.type.startsWith("image/")){const a=o.getAsFile();if(a){e.preventDefault();const l=new FileReader;l.onload=r=>{const d=r.target?.result;this.insertImage(d)},l.readAsDataURL(a);return}}}const i=t.getData("text/plain"),n=/<([a-z1-6]+)\b[^>]*>[\s\S]*<\/\1>/i.test(i)||/^\s*<[a-z1-6]+\b[^>]*>/i.test(i);if(i&&n){const s=i.replace(/(\r\n|\n|\r)/gm," ").replace(/>\s+</g,"><").trim();e.preventDefault(),this.execute("insertHTML",s);return}}),this.addEventListener(this.editableElement,"input",()=>{this.handleInput()}),this.addEventListener(this.editableElement,"keydown",e=>{(e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="z"?(e.preventDefault(),e.shiftKey?this.redo():this.undo()):(e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="y"&&(e.preventDefault(),this.redo())})}handleInput(){this.scheduleHistoryRecord(),this.options.autoSave&&(this.options.onSaving&&this.options.onSaving(),this.scheduleAutoSave()),this.options.onChange&&this.options.onChange(this.editableElement.innerHTML)}scheduleHistoryRecord(){this.historyTimeout&&clearTimeout(this.historyTimeout),this.historyTimeout=setTimeout(()=>{this.history.record(this.editableElement.innerHTML)},500)}scheduleAutoSave(){this.saveTimeout&&clearTimeout(this.saveTimeout);const e=this.options.autoSaveInterval||1e3;this.saveTimeout=setTimeout(()=>{this.save()},e)}save(){this.options.onSave&&this.options.onSave(this.editableElement.innerHTML)}undo(){const e=this.history.undo();e!==null&&(this.editableElement.innerHTML=e,this.triggerChange())}redo(){const e=this.history.redo();e!==null&&(this.editableElement.innerHTML=e,this.triggerChange())}triggerChange(){this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}createEditableElement(){const e=document.createElement("div");return e.setAttribute("contenteditable","true"),e.setAttribute("role","textbox"),e.setAttribute("spellcheck","true"),e.classList.add("te-content"),this.options.placeholder&&e.setAttribute("data-placeholder",this.options.placeholder),e.style.minHeight="150px",e.style.outline="none",e.style.padding="1rem",e.innerHTML===""&&(e.innerHTML="<p><br></p>"),e}focus(){this.editableElement.focus()}execute(e,t=null){this.focus(),document.execCommand(e,!1,t??void 0),this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}insertTable(e=3,t=3){this.focus();const i=this.selection.getRange();if(!i)return;const n=document.createElement("table");n.classList.add("te-table");for(let o=0;o<e;o++){const a=document.createElement("tr");for(let l=0;l<t;l++){const r=document.createElement("td");r.innerHTML="<br>",a.appendChild(r)}n.appendChild(a)}i.deleteContents(),i.insertNode(n);const s=n.nextElementSibling;if(!s||s.tagName!=="P"){const o=document.createElement("p");o.innerHTML="<br>",n.after(o),s&&s.tagName==="BR"&&s.remove()}this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}addRow(){const e=this.getSelectedTable();if(!e)return;const t=document.createElement("tr");t.style.borderBottom="1px solid var(--te-border-color)";const i=e.rows[0].cells.length;for(let s=0;s<i;s++){const o=document.createElement("td");o.innerHTML="<br>",t.appendChild(o)}const n=this.getSelectedTd();n?n.parentElement?.after(t):e.appendChild(t),this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}deleteRow(){const e=this.getSelectedTd();if(e&&e.parentElement){const t=e.parentElement;t.parentElement.rows.length>1&&(t.remove(),this.editableElement.dispatchEvent(new Event("input",{bubbles:!0})))}}addColumn(){const e=this.getSelectedTable();if(!e)return;const t=this.getSelectedTd(),i=t?t.cellIndex:-1;for(let n=0;n<e.rows.length;n++){const s=e.rows[n],o=document.createElement("td");o.innerHTML="<br>",i!==-1?s.cells[i].after(o):s.appendChild(o)}this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}deleteColumn(){const e=this.getSelectedTd();if(!e)return;const t=this.getSelectedTable();if(!t)return;const i=e.cellIndex;if(t.rows[0].cells.length>1){for(let n=0;n<t.rows.length;n++)t.rows[n].cells[i].remove();this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}}getSelectedTd(){const e=window.getSelection();if(!e||e.rangeCount===0)return null;let t=e.anchorNode;for(;t&&t!==this.editableElement;){if(t.nodeName==="TD")return t;t=t.parentNode}return null}getSelectedTable(){const e=this.getSelectedTd();return e?e.closest("table"):null}clearStyleRecursive(e,t){const i=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT);let n=i.nextNode();for(;n;)n.style.getPropertyValue(t)&&n.style.removeProperty(t),n=i.nextNode()}setStyle(e,t,i){if(e==="font-size"&&t.endsWith("px"),!i){const o=window.getSelection();if(!o||o.rangeCount===0)return null;i=o.getRangeAt(0)}if(i.collapsed)return this.pendingStyles[e]=t,i;let n=i.commonAncestorContainer;n.nodeType===Node.TEXT_NODE&&(n=n.parentElement);let s=null;if(n.tagName==="SPAN"&&n.children.length===0&&n.textContent===i.toString())n.style.setProperty(e,t),s=i.cloneRange();else{const o=document.createElement("span");o.style.setProperty(e,t);try{const a=n.tagName==="SPAN"?n:null,l=i.extractContents();this.clearStyleRecursive(l,e),o.appendChild(l),i.insertNode(o),a&&a.innerHTML===""&&a.remove();const r=document.createRange();r.selectNodeContents(o),s=r;const d=window.getSelection();d&&d.rangeCount>0&&(d.removeAllRanges(),d.addRange(r))}catch(a){console.warn("Failed to apply style:",a)}}return this.editableElement.dispatchEvent(new Event("input",{bubbles:!0})),s}createLink(e){this.focus(),!/^https?:\/\//i.test(e)&&!/^mailto:/i.test(e)&&!e.startsWith("#")&&(e="https://"+e),document.execCommand("createLink",!1,e);const t=window.getSelection();if(t&&t.rangeCount>0){let n=t.getRangeAt(0).commonAncestorContainer;n.nodeType===Node.TEXT_NODE&&(n=n.parentElement);let s=null;n.tagName==="A"?s=n:s=n.querySelector("a"),s&&(s.setAttribute("target","_blank"),s.setAttribute("rel","noopener noreferrer"))}this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}insertImage(e){this.focus();const t=this.selection.getRange();if(!t)return;const i=document.createElement("figure");i.classList.add("te-image-container"),i.setAttribute("contenteditable","false");const n=document.createElement("img");n.src=e,n.classList.add("te-image");const s=document.createElement("figcaption");s.classList.add("te-image-caption"),s.setAttribute("contenteditable","true"),s.setAttribute("data-placeholder","Type caption..."),["top-left","top-right","bottom-left","bottom-right"].forEach(r=>{const d=document.createElement("div");d.classList.add("te-image-resizer",`te-resizer-${r}`),i.appendChild(d)}),i.appendChild(n),i.appendChild(s),t.deleteContents(),t.insertNode(i);const a=document.createElement("p");a.innerHTML="<br>",i.after(a);const l=document.createRange();l.setStart(a,0),l.setEnd(a,0),this.selection.restoreSelection(l),this.editableElement.dispatchEvent(new Event("input",{bubbles:!0}))}getHTML(){return this.editableElement.innerHTML}setHTML(e){this.editableElement.innerHTML=e}get el(){return this.editableElement}getOptions(){return this.options}}const f={type:"button",title:"Undo",command:"undo",icon:'<svg viewBox="0 0 24 24"><path d="M9 14L4 9l5-5"></path><path d="M20 20v-7a4 4 0 0 0-4-4H4"></path></svg>'},b={type:"button",title:"Redo",command:"redo",icon:'<svg viewBox="0 0 24 24"><path d="M15 14l5-5-5-5"></path><path d="M4 20v-7a4 4 0 0 1 4-4h12"></path></svg>'},y={type:"select",title:"Heading",command:"formatBlock",options:[{label:"Paragraph",value:"P"},{label:"Heading 1",value:"H1"},{label:"Heading 2",value:"H2"},{label:"Heading 3",value:"H3"},{label:"Heading 4",value:"H4"},{label:"Heading 5",value:"H5"},{label:"Heading 6",value:"H6"}]},E={type:"select",title:"Font",command:"fontFamily",options:[{label:"Inter",value:"'Inter', sans-serif"},{label:"Arial",value:"Arial, sans-serif"},{label:"Georgia",value:"Georgia, serif"},{label:"Courier",value:"'Courier New', monospace"},{label:"Times New Roman",value:"'Times New Roman', serif"},{label:"Verdana",value:"Verdana, sans-serif"},{label:"Tahoma",value:"Tahoma, sans-serif"},{label:"Roboto",value:"'Roboto', sans-serif"},{label:"Open Sans",value:"'Open Sans', sans-serif"},{label:"Montserrat",value:"'Montserrat', sans-serif"},{label:"Lato",value:"'Lato', sans-serif"},{label:"Poppins",value:"'Poppins', sans-serif"},{label:"Oswald",value:"'Oswald', sans-serif"},{label:"Playfair Display",value:"'Playfair Display', serif"},{label:"Merriweather",value:"'Merriweather', serif"}]},w={type:"input",title:"Size (px)",command:"fontSize",placeholder:"Size",value:"16"},x={type:"select",title:"Line Height",command:"lineHeight",options:[{label:"Normal",value:"normal"},{label:"0.5",value:"0.5"},{label:"1.0",value:"1.0"},{label:"1.15",value:"1.15"},{label:"1.5",value:"1.5"},{label:"2.0",value:"2.0"},{label:"2.5",value:"2.5"},{label:"3.0",value:"3.0"},{label:"3.5",value:"3.5"},{label:"4.0",value:"4.0"},{label:"4.5",value:"4.5"},{label:"5.0",value:"5.0"},{label:"5.5",value:"5.5"},{label:"6.0",value:"6.0"},{label:"6.5",value:"6.5"},{label:"7.0",value:"7.0"},{label:"7.5",value:"7.5"},{label:"8.0",value:"8.0"},{label:"8.5",value:"8.5"},{label:"9.0",value:"9.0"},{label:"9.5",value:"9.5"},{label:"10.0",value:"10.0"}],value:"normal"},L={type:"button",title:"Bold",command:"bold",icon:'<svg viewBox="0 0 24 24"><path d="M6 4h8a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"></path><path d="M6 12h9a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"></path></svg>'},C={type:"button",title:"Italic",command:"italic",icon:'<svg viewBox="0 0 24 24"><line x1="19" y1="4" x2="10" y2="4"></line><line x1="14" y1="20" x2="5" y2="20"></line><line x1="15" y1="4" x2="9" y2="20"></line></svg>'},S={type:"button",title:"Underline",command:"underline",icon:'<svg viewBox="0 0 24 24"><path d="M6 3v7a6 6 0 0 0 6 6 6 6 0 0 0 6-6V3"></path><line x1="4" y1="21" x2="20" y2="21"></line></svg>'},k={type:"button",title:"Strikethrough",command:"strikeThrough",icon:'<svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"></line><path d="M16 4.9C15.1 4.3 13.9 4 12.6 4c-3.1 0-5.6 1.8-5.6 4s1.8 3.3 4.4 3.8"></path><path d="M7 19.1c.9.6 2.1.9 3.4.9 3.1 0 5.6-1.8 5.6-4s-1.8-3.3-4.4-3.8"></path></svg>'},M={type:"color-picker",title:"Text Color",command:"foreColor",icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 20h16"/><path d="m6 16 6-12 6 12"/><path d="M8 12h8"/></svg>',value:"#1e293b"},T={type:"color-picker",title:"Highlight Color",command:"backColor",icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 11-6 6v3h9l3-3"/><path d="m22 12-4.6 4.6a2 2 0 0 1-2.8 0l-5.2-5.2a2 2 0 0 1 0-2.8L14 4"/></svg>',value:"#ffffff"},H={type:"button",title:"Align Left",command:"justifyLeft",icon:'<svg viewBox="0 0 24 24"><line x1="17" y1="10" x2="3" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="17" y1="18" x2="3" y2="18"></line></svg>'},R={type:"button",title:"Align Center",command:"justifyCenter",icon:'<svg viewBox="0 0 24 24"><line x1="18" y1="10" x2="6" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="18" y1="18" x2="6" y2="18"></line></svg>'},I={type:"button",title:"Align Right",command:"justifyRight",icon:'<svg viewBox="0 0 24 24"><line x1="21" y1="10" x2="7" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="7" y2="18"></line></svg>'},A={type:"button",title:"Justify",command:"justifyFull",icon:'<svg viewBox="0 0 24 24"><line x1="21" y1="10" x2="3" y2="10"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="14" x2="3" y2="14"></line><line x1="21" y1="18" x2="3" y2="18"></line></svg>'},j={type:"button",title:"Bulleted List",command:"insertUnorderedList",icon:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line><line x1="8" y1="18" x2="21" y2="18"></line><circle cx="3" cy="6" r="1" fill="currentColor"></circle><circle cx="3" cy="12" r="1" fill="currentColor"></circle><circle cx="3" cy="18" r="1" fill="currentColor"></circle></svg>'},z={type:"button",title:"Numbered List",command:"insertOrderedList",icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="10" y1="6" x2="21" y2="6"></line><line x1="10" y1="12" x2="21" y2="12"></line><line x1="10" y1="18" x2="21" y2="18"></line><path d="M4 6h1v4"></path><path d="M4 10h2"></path><path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"></path></svg>'},D={type:"button",title:"Outdent",command:"outdent",icon:'<svg viewBox="0 0 24 24"><polyline points="15 18 9 12 15 6"></polyline><line x1="21" y1="12" x2="9" y2="12"></line><line x1="21" y1="6" x2="3" y2="6"></line><line x1="21" y1="18" x2="3" y2="18"></line></svg>'},P={type:"button",title:"Indent",command:"indent",icon:'<svg viewBox="0 0 24 24"><polyline points="9 18 15 12 9 6"></polyline><line x1="3" y1="12" x2="15" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg>'},N={type:"button",title:"Horizontal Rule",command:"insertHorizontalRule",icon:'<svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"></line></svg>'},B={type:"button",title:"Clear Formatting",command:"removeFormat",icon:'<svg viewBox="0 0 24 24"><path d="M17.41 15.41L12 10l-5.41 5.41L5.17 14l5.41-5.41L5.17 3.17 6.59 1.76 12 7.17l5.41-5.41 1.41 1.41-5.41 5.41 5.41 5.41-1.41 1.42z"></path></svg>'},O={type:"button",title:"Insert Emoji",command:"insertEmoji",icon:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M8 14s1.5 2 4 2 4-2 4-2"></path><line x1="9" y1="9" x2="9.01" y2="9"></line><line x1="15" y1="9" x2="15.01" y2="9"></line></svg>'},W={type:"button",title:"Insert Link",command:"createLink",icon:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>'},U={type:"button",title:"Insert Image",command:"insertImage",icon:'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></svg>'},F={type:"button",command:"insertTable",title:"Insert Table",icon:'<svg viewBox="0 0 24 24" width="18" height="18"><path fill="currentColor" d="M3 3h18v18H3V3m2 2v4h4V5H5m6 0v4h4V5h-4m6 0v4h2V5h-2M5 11v4h4v-4H5m6 0v4h4v-4h-4m6 0v4h2v-4h-2M5 17v2h4v-2H5m6 0v2h4v-2h-4m6 0v2h2v-2h-2Z"/></svg>'},h={type:"divider",title:""},G=[{...f,id:"undo"},{...b,id:"redo"},h,{...y,id:"heading"},{...E,id:"font-family"},{...w,id:"font-size"},{...x,id:"line-height"},h,{...L,id:"bold"},{...C,id:"italic"},{...S,id:"underline"},{...k,id:"strikethrough"},h,{...M,id:"text-color"},{...T,id:"highlight-color"},h,{...H,id:"align-left"},{...R,id:"align-center"},{...I,id:"align-right"},{...A,id:"align-justify"},h,{...j,id:"bullet-list"},{...z,id:"ordered-list"},{...D,id:"outdent"},{...P,id:"indent"},h,{...N,id:"horizontal-rule"},{...O,id:"emoji"},{...W,id:"link"},{...U,id:"image"},{...F,id:"table"},{...B,id:"clear-formatting"}];class q{container;searchInput;emojiGrid;onSelect;onClose;emojiList=[];theme;dark;constructor(e,t,i,n){this.onSelect=e,this.onClose=t,this.theme=i,this.dark=n,this.container=this.createPickerElement(),this.searchInput=this.container.querySelector(".te-emoji-search"),this.emojiGrid=this.container.querySelector(".te-emoji-grid"),this.setupEvents(),this.loadEmojis()}async loadEmojis(){this.emojiGrid.innerHTML='<div class="te-emoji-loading">Loading...</div>';try{const{EMOJI_LIST:e}=await Promise.resolve().then(()=>require("./EmojiList-qdrW08up.cjs"));this.emojiList=e,this.renderEmojis(this.emojiList)}catch(e){console.error("Failed to load emojis:",e),this.emojiGrid.innerHTML='<div class="te-emoji-error">Failed to load</div>'}}createPickerElement(){const e=document.createElement("div");return e.classList.add("te-emoji-picker"),this.theme&&this.applyTheme(e,this.theme),this.dark&&e.classList.add("te-dark"),e.innerHTML=`
2
- <div class="te-emoji-header">
3
- <input type="text" class="te-emoji-search" placeholder="Search emoji...">
4
- </div>
5
- <div class="te-emoji-body">
6
- <div class="te-emoji-grid"></div>
7
- </div>
8
- `,e}setupEvents(){this.searchInput.addEventListener("mousedown",t=>t.stopPropagation()),this.searchInput.addEventListener("click",t=>t.stopPropagation()),this.searchInput.addEventListener("input",()=>{const t=this.searchInput.value.toLowerCase(),i=this.emojiList.filter(n=>n.name.toLowerCase().includes(t)||n.category.toLowerCase().includes(t));this.renderEmojis(i)});const e=t=>{this.container.contains(t.target)||(this.close(),document.removeEventListener("mousedown",e))};setTimeout(()=>document.addEventListener("mousedown",e),0)}renderEmojis(e){if(this.emojiGrid.innerHTML="",e.length===0){this.emojiGrid.innerHTML='<div class="te-emoji-empty">No emoji found</div>';return}this.searchInput.value.length>0?this.renderGridItems(e):["Smileys","Symbols","Hands","Animals","Food","Travel","Objects","Activities"].forEach(n=>{const s=e.filter(o=>o.category===n);if(s.length>0){const o=document.createElement("div");o.classList.add("te-emoji-category-title"),o.textContent=n,this.emojiGrid.appendChild(o),this.renderGridItems(s)}})}renderGridItems(e){e.forEach(t=>{const i=document.createElement("button");i.type="button",i.classList.add("te-emoji-item"),i.textContent=t.emoji,i.title=t.name,i.addEventListener("click",()=>{this.onSelect(t.emoji),this.close()}),this.emojiGrid.appendChild(i)})}applyTheme(e,t){const i={primaryColor:"--te-primary-color",primaryHover:"--te-primary-hover",bgApp:"--te-bg-app",bgEditor:"--te-bg-editor",toolbarBg:"--te-toolbar-bg",borderColor:"--te-border-color",borderFocus:"--te-border-focus",textMain:"--te-text-main",textMuted:"--te-text-muted",placeholder:"--te-placeholder",btnHover:"--te-btn-hover",btnActive:"--te-btn-active",radiusLg:"--te-radius-lg",radiusMd:"--te-radius-md",radiusSm:"--te-radius-sm",shadowSm:"--te-shadow-sm",shadowMd:"--te-shadow-md",shadowLg:"--te-shadow-lg"};for(const[n,s]of Object.entries(i)){const o=t[n];o&&e.style.setProperty(s,o)}}show(e){document.body.appendChild(this.container);const t=e.getBoundingClientRect(),i=280;let n=t.bottom+window.scrollY+5,s=t.left+window.scrollX;s+i>window.innerWidth&&(s=window.innerWidth-i-10),this.container.style.top=`${n}px`,this.container.style.left=`${s}px`,this.searchInput.focus()}close(){this.container.parentElement&&(this.container.remove(),this.onClose())}get el(){return this.container}}class v{editor;container;savedRange=null;items=G;activePicker=null;statusEl=null;boundUpdateActiveStates;constructor(e){this.editor=e,this.container=this.createToolbarElement(),this.boundUpdateActiveStates=this.updateActiveStates.bind(this),this.render()}createToolbarElement(){const e=document.createElement("div");return e.classList.add("te-toolbar"),this.statusEl=document.createElement("div"),this.statusEl.classList.add("te-toolbar-status"),this.statusEl.style.marginLeft="auto",this.statusEl.style.display="flex",this.statusEl.style.alignItems="center",this.statusEl.style.gap="6px",this.statusEl.style.fontSize="12px",this.statusEl.style.color="var(--te-text-muted)",this.statusEl.style.paddingRight="12px",e}render(){const e=this.editor.getOptions().toolbarItems,t=[];this.items.forEach(s=>{(s.type==="divider"||s.id&&(!e||e.includes(s.id)))&&t.push(s)});const i=[];t.forEach((s,o)=>{if(s.type==="divider"){if(i.length===0||i[i.length-1].type==="divider"||!t.slice(o+1).some(l=>l.type!=="divider"))return;i.push(s)}else i.push(s)}),i.forEach(s=>{if(s.type==="button")this.renderButton(s);else if(s.type==="select")this.renderSelect(s);else if(s.type==="input")this.renderInput(s);else if(s.type==="color-picker")this.renderColorPicker(s);else if(s.type==="divider"){const o=document.createElement("div");o.classList.add("te-divider"),this.container.appendChild(o)}}),this.editor.getOptions().showStatus!==!1&&this.container.appendChild(this.statusEl),this.editor.el.addEventListener("keyup",this.boundUpdateActiveStates),this.editor.el.addEventListener("mouseup",this.boundUpdateActiveStates)}renderButton(e){const t=document.createElement("button");t.classList.add("te-button"),t.innerHTML=e.icon||"",t.title=e.title,t.addEventListener("mousedown",i=>{if(i.preventDefault(),e.command==="insertEmoji"){this.activePicker?this.activePicker.close():(this.activePicker=new q(n=>{this.editor.execute("insertText",n)},()=>{this.activePicker=null},this.editor.getOptions().theme,this.editor.getOptions().dark),this.activePicker.show(t));return}if(e.command==="createLink"){const n=window.prompt("Enter the URL");n&&this.editor.createLink(n);return}if(e.command==="insertImage"){const n=document.createElement("input");n.type="file",n.accept="image/*",n.style.display="none",n.addEventListener("change",s=>{const a=s.target.files?.[0];if(a){const l=new FileReader;l.onload=r=>{const d=r.target?.result;this.editor.insertImage(d)},l.readAsDataURL(a)}document.body.removeChild(n)}),document.body.appendChild(n),n.click();return}if(e.command==="insertTable"){const n=window.prompt("Enter number of rows","3"),s=window.prompt("Enter number of columns","3");n&&s&&this.editor.insertTable(parseInt(n,10),parseInt(s,10));return}if(["addRow","deleteRow","addColumn","deleteColumn"].includes(e.command||"")){const n=e.command;this.editor[n]();return}e.command&&this.editor.execute(e.command,e.value||null),this.updateActiveStates()}),this.container.appendChild(t)}renderInput(e){const t=document.createElement("input");t.type="number",t.classList.add("te-input"),t.title=e.title,t.value=e.value||"",t.min="1",t.max="100";const i=()=>{const s=window.getSelection();if(s&&s.rangeCount>0){const o=s.getRangeAt(0);this.editor.el.contains(o.commonAncestorContainer)&&(this.savedRange=o.cloneRange())}};t.addEventListener("mousedown",i),t.addEventListener("focus",i);const n=()=>{let s=parseInt(t.value,10);if(!isNaN(s)&&(s=Math.max(1,Math.min(100,s)),t.value=s.toString(),e.command==="fontSize")){if(this.savedRange){const o=this.editor.setStyle("font-size",`${s}px`,this.savedRange);o&&(this.savedRange=o)}else this.editor.setStyle("font-size",`${s}px`);t.focus()}};t.addEventListener("input",n),t.addEventListener("keydown",s=>{s.key==="Enter"&&(n(),this.editor.focus())}),this.container.appendChild(t)}renderSelect(e){const t=document.createElement("select");t.classList.add("te-select"),t.title=e.title,e.options&&e.options.forEach(i=>{const n=document.createElement("option");n.value=i.value,n.textContent=i.label,t.appendChild(n)}),t.addEventListener("change",()=>{const i=t.value;this.savedRange&&this.editor.selection.restoreSelection(this.savedRange),e.command==="formatBlock"?this.editor.execute(e.command,i):e.command==="fontFamily"?this.editor.setStyle("font-family",i):e.command==="lineHeight"&&this.editor.setStyle("line-height",i),this.editor.focus()}),t.addEventListener("mousedown",()=>{this.savedRange=this.editor.selection.saveSelection()}),this.container.appendChild(t)}renderColorPicker(e){const t=document.createElement("div");if(t.classList.add("te-color-picker-wrapper"),t.title=e.title,e.icon){const n=document.createElement("div");n.classList.add("te-button","te-color-icon"),n.innerHTML=e.icon;const s=document.createElement("div");s.classList.add("te-color-indicator"),s.style.backgroundColor=e.value||"#000000",n.appendChild(s),t.appendChild(n)}const i=document.createElement("input");i.type="color",i.classList.add("te-color-picker-input"),e.icon||(i.classList.add("te-color-picker"),i.title=e.title),i.value=e.value||"#000000",i.addEventListener("mousedown",()=>{this.savedRange=this.editor.selection.saveSelection()}),i.addEventListener("input",()=>{if(e.icon){const n=t.querySelector(".te-color-indicator");n&&(n.style.backgroundColor=i.value)}}),i.addEventListener("change",()=>{this.savedRange&&this.editor.selection.restoreSelection(this.savedRange),e.command&&this.editor.execute(e.command,i.value),this.editor.focus()}),t.appendChild(i),this.container.appendChild(e.icon?t:i)}get el(){return this.container}updateActiveStates(){const e=this.container.querySelectorAll(".te-button");let t=0;this.items.forEach(i=>{if(i.type==="button"){const n=e[t++];i.command&&document.queryCommandState(i.command)?n.classList.add("active"):n.classList.remove("active")}})}updateStatus(e,t=!1){if(!this.statusEl||this.editor.getOptions().showStatus===!1)return;if(this.statusEl.innerHTML="",t){const n=document.createElement("div");n.classList.add("te-toolbar-loader"),this.statusEl.appendChild(n)}const i=document.createElement("span");i.textContent=e,this.statusEl.appendChild(i)}destroy(){this.editor.el.removeEventListener("keyup",this.boundUpdateActiveStates),this.editor.el.removeEventListener("mouseup",this.boundUpdateActiveStates),this.activePicker&&(this.activePicker.close(),this.activePicker=null),this.container.parentNode&&this.container.parentNode.removeChild(this.container)}}class K extends p{toolbar;constructor(e,t={}){const i={...t,onSaving:()=>{this.toolbar?.updateStatus("Auto saving...",!0),t.onSaving&&t.onSaving()},onSave:n=>{const s=new Date().toLocaleString([],{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit",hour12:!0});this.toolbar?.updateStatus(`Saved at ${s}`,!1),t.onSave&&t.onSave(n)}};if(super(e,i),typeof document>"u"||!e){this.toolbar={};return}this.toolbar=new v(this),this.container.insertBefore(this.toolbar.el,this.editableElement),i.showStatus!==!1&&this.toolbar&&this.toolbar.updateStatus("All changes saved",!1)}getToolbar(){return this.toolbar}destroy(){this.toolbar&&this.toolbar.destroy(),super.destroy()}}exports.CoreEditor=p;exports.HistoryManager=m;exports.SelectionManager=u;exports.TestEditor=K;exports.Toolbar=v;