Files
sigpro/dist/sigpro.editor.js
2026-05-14 13:41:32 +02:00

2 lines
5.0 KiB
JavaScript

var{$:u,isF:C}=window.SigPro,S=(x)=>{let{value:s,class:z}=x,l=u(0),g=u(""),v=u(0),h=u(0),c=u(0),w=()=>window.getSelection().rangeCount?window.getSelection().getRangeAt(0):0,o,d,R="\uD83D\uDE00 \uD83D\uDE0A \uD83D\uDE09 \uD83E\uDDD0 \uD83D\uDE2E \uD83E\uDD14 \uD83D\uDE05 \uD83D\uDE02 \uD83D\uDE0D \uD83D\uDE18 \uD83E\uDD70 \uD83D\uDC4D \uD83D\uDC4E \uD83D\uDC4C \uD83E\uDD1D \uD83E\uDD1E \uD83D\uDC4B \uD83D\uDC4F \uD83D\uDE4C \uD83D\uDE4F \uD83D\uDCAA ☝️ \uD83D\uDC47 \uD83D\uDC48 \uD83D\uDC49 \uD83D\uDD95 ✅ ⚠️ \uD83D\uDE80 \uD83D\uDCE2 ✉️ ❤️".split(" "),k=()=>{h(h()+1),o&&v(o.innerText.length)},b=()=>{if(o){let e=o.innerHTML;C(s)?s(e):x.onchange?.(e),k()}},r=(e,i=null)=>{if(o?.focus(),d){let n=window.getSelection();n.removeAllRanges(),n.addRange(d)}document.execCommand(e,0,i),d=0,b()},L=(e,i=null)=>{if(h(),!o||l())return 0;try{if(e=="formatBlock"){let n=window.getSelection().getRangeAt(0).commonAncestorContainer;while(n&&n!==o){if(n.tagName===i)return 1;n=n.parentNode}return 0}return document.queryCommandState(e)}catch(n){return 0}},T=(e)=>{if(e){let i=new FileReader;i.onload=(n)=>{let f=e.type.startsWith("image/"),m=n.target.result;r("insertHTML",f?div({style:"display:inline-block;resize:both;overflow:hidden;vertical-align:bottom;width:200px;border:1px dashed #ccc;padding:2px;cursor:pointer",class:"resizable-img-container"},[img({src:m,style:"width:100%;height:100%;object-fit:contain;pointer-events:none"})]):a({href:m,download:e.name,contenteditable:"false",style:"display:inline-flex;align-items:center;gap:5px;padding:4px 8px;border:1px solid #ccc;border-radius:4px;background:#f9f9f9;text-decoration:none;color:#333;font-size:12px;margin:2px;cursor:pointer"},[span({class:"icon-[lucide--paperclip] w-3 h-3"}),e.name]))},i.readAsDataURL(e)}},t=(e,i,n)=>button({type:"button",class:()=>`btn btn-ghost btn-xs ${L(i,n)?"btn-active bg-primary/20":""}`,onclick:()=>typeof i=="function"?i():r(i,n)},[span({class:`icon-[lucide--${e}]`})]);return div({class:`border border-base-300 rounded-box bg-base-100 overflow-hidden flex flex-col ${z||""}`},[div({class:"flex flex-wrap items-center gap-1 p-2 border-b border-base-300 bg-base-200 sticky top-0 z-20"},[div({class:"flex flex-wrap gap-1 flex-1"},[t("bold","bold"),t("italic","italic"),t("underline","underline"),input({type:"color",class:"w-5 h-5 p-0 bg-transparent cursor-pointer",oninput:(e)=>r("foreColor",e.target.value)}),span({class:"w-px h-5 bg-base-300 mx-1"}),t("align-left","justifyLeft"),t("align-center","justifyCenter"),t("align-right","justifyRight"),span({class:"w-px h-5 bg-base-300 mx-1"}),t("list","insertUnorderedList"),t("list-ordered","insertOrderedList"),t("indent-decrease","outdent"),t("indent-increase","indent"),t("quote",()=>r("formatBlock",L("formatBlock","BLOCKQUOTE")?"P":"BLOCKQUOTE"),"BLOCKQUOTE"),span({class:"w-px h-5 bg-base-300 mx-1"}),t("link",()=>{let e=prompt("URL:");e&&r("createLink",e)}),t("paperclip",()=>{let e=document.createElement("input");e.type="file",e.onchange=(i)=>T(i.target.files[0]),e.click()}),div({class:"relative"},[t("smile",()=>{d=w(),c(!c())}),div({class:"absolute top-full left-0 mt-1 p-2 bg-base-100 border shadow-xl rounded-box w-52 z-50 flex flex-wrap gap-1",style:()=>c()?"":"display:none"},R.map((e)=>span({class:"cursor-pointer p-1 text-lg",onclick:()=>{r("insertText",e),c(0)}},e)))]),t("undo-2","undo"),t("redo-2","redo")]),t("code-2",()=>{if(!l())g(o?.innerHTML||"");else if(o)o.innerHTML=g(),b();l(!l())})]),div({class:"relative flex-1 flex flex-col",onclick:()=>c(0)},[div({ref:(e)=>{if(e&&!o)o=e,e.innerHTML=(C(s)?s():s)||"",document.execCommand("defaultParagraphSeparator",0,"br"),e.onclick=(i)=>{let n=i.target.closest(".resizable-img-container");if(n){let f=n.querySelector("img");f&&((m)=>{let p=document.createElement("div");p.style="position:fixed;top:0;left:0;width:100%;height:100%;background:#000e;z-index:9999;display:flex;align-items:center;justify-content:center;cursor:zoom-out",p.onclick=()=>p.remove();let y=document.createElement("img");y.src=m,y.style="max-width:95%;max-height:95%",p.appendChild(y),document.body.appendChild(p)})(f.src)}}},style:()=>`min-height:22rem;${l()?"display:none":""}`,class:"p-4 outline-none text-base-content leading-relaxed [&>div]:m-0 [&>p]:m-0 [&>div]:min-h-[1em] [&_.resizable-img-container]:hover:border-primary [&_blockquote]:border-l-4 [&_blockquote]:border-base-300 [&_blockquote]:pl-4 [&_blockquote]:italic [&_ul]:list-disc [&_ul]:pl-8 [&_ol]:list-decimal [&_ol]:pl-8",contenteditable:"true",oninput:b,onkeydown:(e)=>e.key==="Tab"&&(e.preventDefault(),r("indent")),onkeyup:()=>{k(),d=w()},onmouseup:b,onpaste:(e)=>{e.preventDefault(),r("insertText",e.clipboardData.getData("text/plain"))},ondrop:(e)=>{e.preventDefault(),T(e.dataTransfer.files[0])},ondragover:(e)=>e.preventDefault()}),textarea({class:"w-full flex-1 min-h-[22rem] p-4 font-mono text-sm bg-base-200 border-0",style:()=>l()?"":"display:none",value:g,oninput:(e)=>{if(g(e.target.value),o)o.innerHTML=e.target.value;x.onchange?.(e.target.value)}})]),div({class:"px-3 py-1 border-t text-[10px] text-right opacity-60"},[span(()=>v())])])};export{S as Editor};