dopecanvas 0.1.6 → 0.1.7

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,11 +1,11 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),l=require("react"),Be=require("react-dom"),we=({dimensions:s,margins:e,pageNumber:t,totalPages:o,children:r})=>n.jsxs("div",{className:"dopecanvas-page",style:{width:`${s.width}px`,height:`${s.height}px`,backgroundColor:"#ffffff",boxShadow:"0 2px 8px rgba(0, 0, 0, 0.15), 0 0 1px rgba(0, 0, 0, 0.1)",position:"relative",overflow:"hidden",flexShrink:0},children:[n.jsx("div",{className:"dopecanvas-page-content",style:{paddingTop:`${e.top}px`,paddingRight:`${e.right}px`,paddingBottom:`${e.bottom}px`,paddingLeft:`${e.left}px`,height:"100%",boxSizing:"border-box",overflow:"hidden"},children:r}),n.jsxs("div",{className:"dopecanvas-page-number",style:{position:"absolute",bottom:`${Math.max(e.bottom/3,16)}px`,left:0,right:0,textAlign:"center",fontSize:"11px",color:"#999",fontFamily:"system-ui, -apple-system, sans-serif",pointerEvents:"none",userSelect:"none"},children:[t," / ",o]})]}),We=({visible:s,onAddBelow:e,onEditHTML:t,onDelete:o})=>n.jsxs("div",{className:"dopecanvas-block-toolbar",style:{...Ue,display:s?"flex":"none"},onMouseDown:r=>r.preventDefault(),children:[n.jsx(Se,{onClick:e,title:"Add block below",children:n.jsx($e,{})}),n.jsx(Se,{onClick:t,title:"Edit HTML",children:n.jsx(Oe,{})}),n.jsx(Se,{onClick:o,title:"Delete block",danger:!0,children:n.jsx(Ve,{})})]}),$e=()=>n.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",children:[n.jsx("line",{x1:"7",y1:"3",x2:"7",y2:"11"}),n.jsx("line",{x1:"3",y1:"7",x2:"11",y2:"7"})]}),Oe=()=>n.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",children:[n.jsx("polyline",{points:"4.5,3 1.5,7 4.5,11"}),n.jsx("polyline",{points:"9.5,3 12.5,7 9.5,11"})]}),Ve=()=>n.jsx("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",children:n.jsx("path",{d:"M2.5 4h9M5 4V2.5h4V4M3.5 4l.5 8h6l.5-8"})}),Se=({onClick:s,title:e,children:t,danger:o})=>{const[r,a]=l.useState(!1);return n.jsx("button",{onClick:s,title:e,onMouseEnter:()=>a(!0),onMouseLeave:()=>a(!1),style:{...Ge,background:r?o?"#fff0f0":"#f0f0f0":"transparent",color:r&&o?"#d32f2f":"#666"},children:t})},Ue={position:"absolute",top:0,left:-40,flexDirection:"column",gap:"1px",zIndex:100,background:"#fff",borderRadius:"6px",boxShadow:"0 1px 5px rgba(0,0,0,0.12)",padding:"3px",paddingRight:"6px"},Ge={width:"30px",height:"26px",border:"none",cursor:"pointer",borderRadius:"4px",fontSize:"13px",fontFamily:"system-ui, -apple-system, sans-serif",display:"flex",alignItems:"center",justifyContent:"center",padding:0,transition:"background 0.1s, color 0.1s"},Ke=({html:s,onSave:e,onCancel:t})=>{const[o,r]=l.useState(s),a=i=>{if(i.key==="Enter"&&(i.metaKey||i.ctrlKey)&&(i.preventDefault(),e(o)),i.key==="Escape"&&(i.preventDefault(),t()),i.key==="Tab"){i.preventDefault();const d=i.target,p=d.selectionStart,f=d.selectionEnd,x=o.substring(0,p)+" "+o.substring(f);r(x),requestAnimationFrame(()=>{d.selectionStart=d.selectionEnd=p+2})}};return Be.createPortal(n.jsx("div",{style:Xe,onClick:t,children:n.jsxs("div",{style:Ze,onClick:i=>i.stopPropagation(),children:[n.jsxs("div",{style:Je,children:[n.jsx("span",{style:{fontWeight:600,fontSize:"14px"},children:"Edit Block HTML"}),n.jsx("span",{style:{fontSize:"11px",color:"#888"},children:"⌘Enter to save · Escape to cancel"})]}),n.jsx("textarea",{style:Qe,value:o,onChange:i=>r(i.target.value),onKeyDown:a,spellCheck:!1,autoFocus:!0}),n.jsxs("div",{style:Ye,children:[n.jsx("button",{style:et,onClick:t,onMouseEnter:i=>{i.target.style.borderColor="#888"},onMouseLeave:i=>{i.target.style.borderColor="#555"},children:"Cancel"}),n.jsx("button",{style:tt,onClick:()=>e(o),onMouseEnter:i=>{i.target.style.background="#0055dd"},onMouseLeave:i=>{i.target.style.background="#0066ff"},children:"Save"})]})]})}),document.body)},Xe={position:"fixed",inset:0,background:"rgba(0, 0, 0, 0.5)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:1e4},Ze={width:"min(800px, 90vw)",height:"min(600px, 80vh)",background:"#1e1e1e",borderRadius:"10px",display:"flex",flexDirection:"column",overflow:"hidden",boxShadow:"0 20px 60px rgba(0, 0, 0, 0.4)"},Je={display:"flex",justifyContent:"space-between",alignItems:"center",padding:"12px 16px",background:"#2d2d2d",color:"#ccc",borderBottom:"1px solid #444"},Qe={flex:1,background:"#1e1e1e",color:"#d4d4d4",border:"none",padding:"16px",fontFamily:"'SF Mono', 'Fira Code', 'Consolas', 'Monaco', monospace",fontSize:"13px",lineHeight:"1.6",resize:"none",outline:"none",tabSize:2},Ye={display:"flex",justifyContent:"flex-end",gap:"8px",padding:"12px 16px",background:"#2d2d2d",borderTop:"1px solid #444"},et={padding:"6px 16px",border:"1px solid #555",borderRadius:"6px",background:"transparent",color:"#ccc",cursor:"pointer",fontSize:"13px",transition:"border-color 0.15s"},tt={padding:"6px 16px",border:"none",borderRadius:"6px",background:"#0066ff",color:"#fff",cursor:"pointer",fontSize:"13px",fontWeight:500,transition:"background 0.15s"},Te={letter:{width:816,height:1056},a4:{width:794,height:1123},legal:{width:816,height:1344}},Pe={top:96,right:96,bottom:96,left:96},he={size:"letter",margins:{...Pe}};let nt=0;function _e(){return`split-${++nt}-${Date.now()}`}const ot=40,rt=new Set(["script","style","img","video","canvas","svg","hr","iframe","object","embed","audio","picture","figure"]),st=new Set(["p","h1","h2","h3","h4","h5","h6","blockquote","div","li","span","td","th","pre","code"]);function de(s,e){const t=s.tagName.toLowerCase();if(rt.has(t)||e<ot)return null;const o=window.getComputedStyle(s);if(o.getPropertyValue("break-before")==="page"||o.getPropertyValue("page-break-before")==="always")return null;const r=Array.from(s.children);if(r.length>1){const a=it(s,r,e);if(a)return a}if(st.has(t)||s.childNodes.length>0){const a=at(s,e);if(a)return a}return null}function De(s){if(s.length===0)return s;const e=[];let t=0;for(;t<s.length;){const o=s[t],r=Le(o);if(!r){e.push(o),t++;continue}const a=[o];let i=t+1;for(;i<s.length&&Le(s[i])===r;)a.push(s[i]),i++;e.push(lt(a)),t=i}return e}function it(s,e,t){const o=s.getBoundingClientRect().top;let r=-1;for(let p=0;p<e.length;p++){const f=e[p].getBoundingClientRect(),x=window.getComputedStyle(e[p]),H=parseFloat(x.marginBottom)||0;if(f.bottom+H-o>t&&p>0){r=p;break}}if(r<=0)return null;const a=s.cloneNode(!1),i=s.cloneNode(!1);for(let p=0;p<e.length;p++)p<r?a.appendChild(e[p].cloneNode(!0)):i.appendChild(e[p].cloneNode(!0));const d=_e();return a.setAttribute("data-dopecanvas-split-id",d),a.setAttribute("data-dopecanvas-split-part","0"),i.setAttribute("data-dopecanvas-split-id",d),i.setAttribute("data-dopecanvas-split-part","1"),{firstHTML:a.outerHTML,secondHTML:i.outerHTML}}function at(s,e){const t=s.getBoundingClientRect().top,o=[],r=document.createTreeWalker(s,NodeFilter.SHOW_TEXT);for(;r.nextNode();)o.push(r.currentNode);if(o.length===0)return null;let a=null,i=0;for(const f of o){const x=document.createRange();x.selectNodeContents(f);const H=x.getBoundingClientRect();if(H.bottom-t<=e)continue;if(H.top-t>=e){a=f,i=0;break}let E=0,k=f.length;for(;E<k;){const R=Math.floor((E+k)/2),T=document.createRange();T.setStart(f,R),T.collapse(!0),T.getBoundingClientRect().top-t>=e?k=R:E=R+1}a=f,i=E;break}if(!a)return null;const d=a.textContent||"";let p=i;for(;p>0&&d[p-1]!==" "&&d[p-1]!==`
2
- `;)p--;if(p>0&&(i=p),i===0&&a===o[0]||i>=d.length&&a===o[o.length-1])return null;try{const f=document.createRange();f.setStart(s,0),f.setEnd(a,i);const x=document.createRange();x.setStart(a,i),x.setEndAfter(s.lastChild);const H=f.cloneContents(),E=x.cloneContents(),k=je(H),R=je(E);if(k.trim().length===0||R.trim().length===0)return null;const T=s.cloneNode(!1);T.appendChild(H);const N=s.cloneNode(!1);N.appendChild(E);const O=_e();return T.setAttribute("data-dopecanvas-split-id",O),T.setAttribute("data-dopecanvas-split-part","0"),N.setAttribute("data-dopecanvas-split-id",O),N.setAttribute("data-dopecanvas-split-part","1"),{firstHTML:T.outerHTML,secondHTML:N.outerHTML}}catch{return null}}function Le(s){const e=s.match(/data-dopecanvas-split-id="([^"]+)"/);return e?e[1]:null}function lt(s){const e=document.createElement("div"),t=[];let o="",r="";for(const a of s){e.innerHTML=a;const i=e.firstElementChild;if(i){if(!o){o=i.tagName.toLowerCase();const d=i.cloneNode(!1);d.removeAttribute("data-dopecanvas-split-id"),d.removeAttribute("data-dopecanvas-split-part");const p=document.createElement("div");p.appendChild(d);const f=p.innerHTML,x=f.lastIndexOf("</");r=x>=0?f.substring(0,x):f}t.push(i.innerHTML)}}return o?`${r}${t.join("")}</${o}>`:s[0]}function je(s){const e=document.createElement("div");return e.appendChild(s.cloneNode(!0)),e.textContent||""}function ct(s){const e=[];return s.querySelectorAll("script").forEach(t=>{const o=document.createElement("script");Array.from(t.attributes).forEach(r=>o.setAttribute(r.name,r.value)),o.textContent=t.textContent||"",t.parentNode?.replaceChild(o,t),e.push(o)}),e}function ut(s){const e=window.getSelection();if(!e||e.rangeCount===0)return null;const t=e.getRangeAt(0),o=t.startContainer,r=Array.from(s.querySelectorAll(".dopecanvas-block-content")),a=r.findIndex(i=>i.contains(o));if(a===-1)return null;try{const i=document.createRange();i.selectNodeContents(r[a]),i.setEnd(t.startContainer,t.startOffset);const d=i.toString().length;return{blockIndex:a,textOffset:d}}catch{return null}}function dt(s,e){const t=s.querySelectorAll(".dopecanvas-block-content");if(e.blockIndex>=t.length)return;const o=t[e.blockIndex],r=document.createTreeWalker(o,NodeFilter.SHOW_TEXT);let a=e.textOffset;for(;r.nextNode();){const i=r.currentNode;if(a<=i.length){try{const d=document.createRange();d.setStart(i,a),d.collapse(!0);const p=window.getSelection();p?.removeAllRanges(),p?.addRange(d)}catch{}return}a-=i.length}try{const i=document.createRange();i.selectNodeContents(o),i.collapse(!1);const d=window.getSelection();d?.removeAllRanges(),d?.addRange(i)}catch{}}const ht=l.memo(({html:s,isEditable:e,isTable:t})=>n.jsx("div",{className:"dopecanvas-block-content",contentEditable:e&&!t?!0:void 0,suppressContentEditableWarning:!0,dangerouslySetInnerHTML:{__html:s}}));function pt(s){const e=s.replace(/\s+/g," ").toLowerCase();return e.includes("break-before")&&e.includes("page")||e.includes("page-break-before")&&e.includes("always")}const gt=l.forwardRef(({html:s,css:e,pageConfig:t,layoutEngine:o,editableManager:r,onContentChange:a,onPaginationChange:i,showPageBreaks:d=!1},p)=>{const f=l.useRef(null),x=l.useRef(null),H=l.useRef(null),E=l.useRef(a);E.current=a;const[k,R]=l.useState([]),T=l.useRef(null),N=l.useRef(!1),O=l.useRef([]),[pe,Z]=l.useState(null),[j,ee]=l.useState(null),[ae,le]=l.useState(""),X=l.useRef(null),q=typeof t.size=="string"?Te[t.size]:t.size,K=l.useCallback(()=>{if(!x.current)return;const h=x.current.querySelectorAll(".dopecanvas-block-content"),c=[];h.forEach(b=>{const v=b,w=v.children;if(w.length!==0)if(w.length===1)c.push(w[0].outerHTML);else{const S=document.createElement("div");S.innerHTML=v.innerHTML,c.push(S.outerHTML)}});const L=c.join(`
3
- `);E.current?.(L)},[]),D=l.useCallback(h=>{if(!f.current)return;const c=f.current,L=o.getContentAreaWidth(),b=o.getContentAreaHeight();if(c.style.width=`${L}px`,c.style.position="absolute",c.style.left="-9999px",c.style.top="0",c.style.visibility="hidden",c.innerHTML="",e){const u=document.createElement("style");u.textContent=e,c.appendChild(u)}const v=document.createElement("div");v.innerHTML=h,c.appendChild(v);const S=o.measureBlocks(v).map(u=>{const P=u.element;return{html:P.cloneNode(!0).outerHTML,height:u.height,element:P,breakBefore:u.breakBefore,breakAfter:u.breakAfter}}),M=[];let y=[],B=0,V=0;for(;V<S.length;){const u=S[V];u.breakBefore&&y.length>0&&(M.push(y),y=[],B=0);const P=b-B;if(u.height<=P)y.push(u.html),B+=u.height;else if(y.length>0&&P>=60){const F=de(u.element,P);if(F){y.push(F.firstHTML),M.push(y),y=[],B=0;const W=document.createElement("div");W.innerHTML=F.secondHTML;const z=W.firstElementChild;v.appendChild(z);const oe=z.getBoundingClientRect(),re=window.getComputedStyle(z),m=parseFloat(re.marginTop)||0,$=parseFloat(re.marginBottom)||0;S.splice(V+1,0,{html:z.outerHTML,height:oe.height+m+$,element:z,breakBefore:!1,breakAfter:u.breakAfter})}else M.push(y),y=[u.html],B=u.height}else if(y.length>0)M.push(y),y=[u.html],B=u.height;else{const F=de(u.element,b);if(F){y.push(F.firstHTML),M.push(y),y=[],B=0;const W=document.createElement("div");W.innerHTML=F.secondHTML;const z=W.firstElementChild;v.appendChild(z);const oe=z.getBoundingClientRect(),re=window.getComputedStyle(z),m=parseFloat(re.marginTop)||0,$=parseFloat(re.marginBottom)||0;S.splice(V+1,0,{html:z.outerHTML,height:oe.height+m+$,element:z,breakBefore:!1,breakAfter:u.breakAfter})}else y.push(u.html),M.push(y),y=[],B=0}u.breakAfter&&y.length>0&&(M.push(y),y=[],B=0),V++}y.length>0&&M.push(y),M.length===0&&M.push([]),c.innerHTML="";const Y=M.map(u=>({blocks:u})),I={pages:M.map((u,P)=>({blockIndices:Array.from({length:M[P].length},(F,W)=>W)})),pageCount:M.length};O.current=Y,R(Y),i?.(I),E.current?.(h)},[e,o,i]),te=l.useCallback(()=>{if(!x.current||!f.current)return;const h=x.current,c=ut(h),L=h.querySelectorAll(".dopecanvas-block-content"),b=[];if(L.forEach(m=>{const $=m,U=$.children;if(U.length!==0)if(U.length===1)b.push(U[0].outerHTML);else{const Q=document.createElement("div");Q.innerHTML=$.innerHTML,b.push(Q.outerHTML)}}),b.length===0)return;const w=De(b).join(`
4
- `),S=f.current,M=o.getContentAreaWidth(),y=o.getContentAreaHeight();if(S.style.width=`${M}px`,S.style.position="absolute",S.style.left="-9999px",S.style.top="0",S.style.visibility="hidden",S.innerHTML="",e){const m=document.createElement("style");m.textContent=e,S.appendChild(m)}const B=document.createElement("div");B.innerHTML=w,S.appendChild(B);const Y=o.measureBlocks(B).map(m=>({html:m.element.cloneNode(!0).outerHTML,height:m.height,element:m.element,breakBefore:m.breakBefore,breakAfter:m.breakAfter})),I=[];let u=[],P=0,F=0;for(;F<Y.length;){const m=Y[F];m.breakBefore&&u.length>0&&(I.push(u),u=[],P=0);const $=y-P;if(m.height<=$)u.push(m.html),P+=m.height;else if(u.length>0&&$>=60){const U=de(m.element,$);if(U){u.push(U.firstHTML),I.push(u),u=[],P=0;const Q=document.createElement("div");Q.innerHTML=U.secondHTML;const G=Q.firstElementChild;B.appendChild(G);const xe=G.getBoundingClientRect(),ue=window.getComputedStyle(G),Ce=parseFloat(ue.marginTop)||0,ye=parseFloat(ue.marginBottom)||0;Y.splice(F+1,0,{html:G.outerHTML,height:xe.height+Ce+ye,element:G,breakBefore:!1,breakAfter:m.breakAfter})}else I.push(u),u=[m.html],P=m.height}else if(u.length>0)I.push(u),u=[m.html],P=m.height;else{const U=de(m.element,y);if(U){u.push(U.firstHTML),I.push(u),u=[],P=0;const Q=document.createElement("div");Q.innerHTML=U.secondHTML;const G=Q.firstElementChild;B.appendChild(G);const xe=G.getBoundingClientRect(),ue=window.getComputedStyle(G),Ce=parseFloat(ue.marginTop)||0,ye=parseFloat(ue.marginBottom)||0;Y.splice(F+1,0,{html:G.outerHTML,height:xe.height+Ce+ye,element:G,breakBefore:!1,breakAfter:m.breakAfter})}else u.push(m.html),I.push(u),u=[],P=0}m.breakAfter&&u.length>0&&(I.push(u),u=[],P=0),F++}u.length>0&&I.push(u),I.length===0&&I.push([]),S.innerHTML="";const W=I.map(m=>({blocks:m})),z=O.current.map(m=>m.blocks.length),oe=W.map(m=>m.blocks.length);z.length!==oe.length||z.some((m,$)=>m!==oe[$])?(N.current=!0,T.current=c,O.current=W,R(W),i?.({pages:I.map(()=>({blockIndices:[]})),pageCount:I.length})):O.current=W},[e,o,i]),ne=l.useRef(te);ne.current=te;const J=l.useCallback(()=>{const h=new DOMParser().parseFromString(s,"text/html");h.head.querySelectorAll('style, link[rel="stylesheet"]').forEach(c=>{h.body.insertBefore(c,h.body.firstChild)}),D(h.body.innerHTML)},[s,D]);l.useEffect(()=>{J()},[J]);const A=l.useCallback(()=>{if(!x.current)return[];const h=x.current.querySelectorAll(".dopecanvas-block-content"),c=[];return h.forEach(L=>{const b=L.firstElementChild;b&&c.push(b.outerHTML)}),c},[]),me=l.useCallback(h=>{const c=A();c.splice(h+1,0,'<p style="min-height: 1.5em; line-height: 1.6;">&nbsp;</p>'),Z(null),D(c.join(`
5
- `))},[A,D]),ce=l.useCallback(h=>{const c=A();c.length<=1||(c.splice(h,1),Z(null),D(c.join(`
6
- `)))},[A,D]),be=l.useCallback(h=>{const c=A();h<c.length&&(ee(h),le(c[h]))},[A]),ge=l.useCallback(h=>{if(j===null)return;const c=A();j<c.length&&(c[j]=h),ee(null),le(""),Z(null),D(c.join(`
7
- `))},[j,A,D]),g=l.useCallback(()=>{ee(null),le("")},[]),C=l.useCallback(()=>{const h='<div style="break-before: page;"></div>',c=A();let L=c.length;const b=window.getSelection();if(b&&b.rangeCount>0&&x.current){const v=b.getRangeAt(0).startContainer,S=Array.from(x.current.querySelectorAll(".dopecanvas-block-content")).findIndex(M=>M.contains(v));S!==-1&&(L=S+1)}c.splice(L,0,h),Z(null),D(c.join(`
8
- `))},[A,D]);return l.useImperativeHandle(p,()=>({insertPageBreak:C}),[C]),l.useEffect(()=>{const h=x.current;if(!h)return;H.current&&H.current.disconnect(),h.querySelectorAll(".dopecanvas-block-content").forEach(w=>{const S=w.firstElementChild;S&&S.tagName==="TABLE"&&S.querySelectorAll("td, th").forEach(y=>{y.contentEditable="true"})});const L=ct(h);let b=null;const v=new MutationObserver(()=>{N.current||(b&&clearTimeout(b),b=setTimeout(()=>{K(),ne.current()},800))});return v.observe(h,{childList:!0,subtree:!0,characterData:!0,attributes:!1}),H.current=v,T.current?requestAnimationFrame(()=>{T.current&&x.current&&(dt(x.current,T.current),T.current=null),N.current=!1}):N.current=!1,()=>{v.disconnect(),L.forEach(w=>w.remove()),b&&clearTimeout(b)}},[k,K]),n.jsxs("div",{className:"dopecanvas-paged-view",style:ft,children:[n.jsx("div",{ref:f,"aria-hidden":"true"}),n.jsxs("div",{ref:x,style:mt,children:[e&&n.jsx("style",{dangerouslySetInnerHTML:{__html:e}}),k.map((h,c)=>{const L=k.slice(0,c).reduce((b,v)=>b+v.blocks.length,0);return n.jsx(we,{dimensions:q,margins:t.margins,pageNumber:c+1,totalPages:k.length,children:h.blocks.map((b,v)=>{const w=L+v,S=b.trim().toLowerCase(),M=!S.startsWith("<script")&&!S.startsWith("<style"),y=S.startsWith("<table"),B=pt(b);return B&&d?n.jsxs("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},children:[n.jsx("div",{className:"dopecanvas-block-content",style:{display:"none"},dangerouslySetInnerHTML:{__html:b}}),n.jsxs("div",{style:bt,children:[n.jsx("span",{style:He}),n.jsx("span",{style:xt,children:"Page Break"}),n.jsx("span",{style:He}),n.jsx("button",{type:"button",title:"Remove page break",onClick:()=>ce(w),onMouseDown:V=>V.preventDefault(),style:Ct,children:"✕"})]})]},`${c}-${v}`):B?n.jsx("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},children:n.jsx("div",{className:"dopecanvas-block-content",dangerouslySetInnerHTML:{__html:b}})},`${c}-${v}`):n.jsxs("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},onMouseEnter:()=>{M&&(X.current&&(clearTimeout(X.current),X.current=null),Z(w))},onMouseLeave:()=>{X.current=setTimeout(()=>{Z(V=>V===w?null:V)},250)},children:[n.jsx(ht,{html:b,isEditable:M,isTable:y}),M&&n.jsx(We,{visible:pe===w,onAddBelow:()=>me(w),onEditHTML:()=>be(w),onDelete:()=>ce(w)})]},`${c}-${v}`)})},c)}),k.length===0&&n.jsx(we,{dimensions:q,margins:t.margins,pageNumber:1,totalPages:1,children:n.jsx("div",{contentEditable:"true",style:{minHeight:"1em",outline:"none"},"data-placeholder":"Start typing..."})})]}),j!==null&&n.jsx(Ke,{html:ae,onSave:ge,onCancel:g})]})}),ft={flex:1,overflow:"auto",backgroundColor:"#e8e8e8",display:"flex",flexDirection:"column",alignItems:"center"},mt={display:"flex",flexDirection:"column",alignItems:"center",gap:"24px",padding:"24px 0"},bt={display:"flex",alignItems:"center",gap:8,padding:"6px 0",userSelect:"none"},He={flex:1,height:0,borderTop:"2px dashed #b0b0b0"},xt={fontSize:11,fontWeight:600,color:"#888",textTransform:"uppercase",letterSpacing:"0.05em",whiteSpace:"nowrap",fontFamily:"system-ui, -apple-system, sans-serif"},Ct={width:20,height:20,border:"1px solid #ccc",borderRadius:4,backgroundColor:"#fff",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",fontSize:11,color:"#999",padding:0,flexShrink:0,lineHeight:1};class Me{config;constructor(e=he){this.config={...e}}getConfig(){return{...this.config}}setConfig(e){e.size!==void 0&&(this.config.size=e.size),e.margins!==void 0&&(this.config.margins={...e.margins})}getPageDimensions(){return typeof this.config.size=="string"?Te[this.config.size]:this.config.size}getContentAreaHeight(){return this.getPageDimensions().height-this.config.margins.top-this.config.margins.bottom}getContentAreaWidth(){return this.getPageDimensions().width-this.config.margins.left-this.config.margins.right}measureBlocks(e){const t=Array.from(e.children),o=[];for(let r=0;r<t.length;r++){const a=t[r],i=window.getComputedStyle(a),d=i.getPropertyValue("break-before")==="page"||i.getPropertyValue("page-break-before")==="always",p=i.getPropertyValue("break-after")==="page"||i.getPropertyValue("page-break-after")==="always",f=a.getBoundingClientRect(),x=parseFloat(i.marginTop)||0,H=parseFloat(i.marginBottom)||0,E=f.height+x+H;o.push({index:r,height:E,element:a,breakBefore:d,breakAfter:p})}return o}paginate(e){if(e.length===0)return{pages:[{blockIndices:[]}],pageCount:1};const t=this.getContentAreaHeight(),o=[];let r=[],a=0;for(let i=0;i<e.length;i++){const d=e[i];d.breakBefore&&r.length>0&&(o.push({blockIndices:r}),r=[],a=0),a+d.height>t&&r.length>0&&(o.push({blockIndices:r}),r=[],a=0),r.push(d.index),a+=d.height,d.breakAfter&&(o.push({blockIndices:r}),r=[],a=0)}return r.length>0&&o.push({blockIndices:r}),o.length===0&&o.push({blockIndices:[]}),{pages:o,pageCount:o.length}}}class ie{observer=null;changeCallbacks=new Set;contextCallbacks=new Set;undoStack=[];redoStack=[];container=null;debounceTimer=null;selectionHandler=null;currentContext="none";static MAX_UNDO_STACK=100;static DEBOUNCE_MS=150;attach(e){this.detach(),this.container=e,this.makeChildrenEditable(e),this.pushUndoSnapshot(),this.observer=new MutationObserver(this.handleMutations),this.observer.observe(e,{childList:!0,subtree:!0,characterData:!0,attributes:!0,attributeFilter:["style","class"]}),this.selectionHandler=this.handleSelectionChange.bind(this),document.addEventListener("selectionchange",this.selectionHandler)}detach(){this.observer&&(this.observer.disconnect(),this.observer=null),this.selectionHandler&&(document.removeEventListener("selectionchange",this.selectionHandler),this.selectionHandler=null),this.debounceTimer&&(clearTimeout(this.debounceTimer),this.debounceTimer=null),this.container=null}makeChildrenEditable(e){const t=Array.from(e.children);for(const o of t)o.tagName==="TABLE"?this.makeTableCellsEditable(o):o.contentEditable="true"}makeTableCellsEditable(e){e.querySelectorAll("td, th").forEach(o=>{o.contentEditable="true"})}handleMutations=e=>{this.debounceTimer&&clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(()=>{this.pushUndoSnapshot(),this.notifyChange()},ie.DEBOUNCE_MS)};handleSelectionChange(){const e=window.getSelection();if(!e||e.rangeCount===0||!this.container){this.setContext("none");return}const o=e.getRangeAt(0).startContainer;let r=o;for(;r&&r!==this.container;){if(r instanceof HTMLElement){const a=r.tagName;if(a==="TD"||a==="TH"||a==="TABLE"){this.setContext("table");return}if(a==="IMG"){this.setContext("image");return}if(r.dataset?.dopecanvasChart){this.setContext("chart");return}}r=r.parentNode}this.container.contains(o)?this.setContext("text"):this.setContext("none")}setContext(e){e!==this.currentContext&&(this.currentContext=e,this.contextCallbacks.forEach(t=>t(e)))}getContext(){return this.currentContext}pushUndoSnapshot(){if(!this.container)return;const e=this.container.innerHTML,t=this.undoStack[this.undoStack.length-1];t&&t.html===e||(this.undoStack.push({html:e,timestamp:Date.now()}),this.redoStack=[],this.undoStack.length>ie.MAX_UNDO_STACK&&this.undoStack.shift())}undo(){if(!this.container||this.undoStack.length<=1)return!1;const e=this.undoStack.pop();this.redoStack.push(e);const t=this.undoStack[this.undoStack.length-1];return this.pauseObserver(()=>{this.container.innerHTML=t.html,this.makeChildrenEditable(this.container)}),this.notifyChange(),!0}redo(){if(!this.container||this.redoStack.length===0)return!1;const e=this.redoStack.pop();return this.undoStack.push(e),this.pauseObserver(()=>{this.container.innerHTML=e.html,this.makeChildrenEditable(this.container)}),this.notifyChange(),!0}pauseObserver(e){this.observer&&this.observer.disconnect(),e(),this.observer&&this.container&&this.observer.observe(this.container,{childList:!0,subtree:!0,characterData:!0,attributes:!0,attributeFilter:["style","class"]})}onChange(e){return this.changeCallbacks.add(e),()=>{this.changeCallbacks.delete(e)}}onContextChange(e){return this.contextCallbacks.add(e),()=>{this.contextCallbacks.delete(e)}}notifyChange(){this.changeCallbacks.forEach(e=>e())}execCommand(e,t){return document.execCommand(e,!1,t)}queryCommandState(e){return document.queryCommandState(e)}queryCommandValue(e){return document.queryCommandValue(e)}getHTML(){return this.container?this.container.innerHTML:""}getPlainText(){return this.container&&(this.container.innerText||this.container.textContent)||""}}const yt=`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),l=require("react"),Be=require("react-dom"),we=({dimensions:s,margins:e,pageNumber:t,totalPages:o,children:r})=>n.jsxs("div",{className:"dopecanvas-page",style:{width:`${s.width}px`,height:`${s.height}px`,backgroundColor:"#ffffff",boxShadow:"0 2px 8px rgba(0, 0, 0, 0.15), 0 0 1px rgba(0, 0, 0, 0.1)",position:"relative",overflow:"hidden",flexShrink:0},children:[n.jsx("div",{className:"dopecanvas-page-content",style:{paddingTop:`${e.top}px`,paddingRight:`${e.right}px`,paddingBottom:`${e.bottom}px`,paddingLeft:`${e.left}px`,height:"100%",boxSizing:"border-box",overflow:"hidden"},children:r}),n.jsxs("div",{className:"dopecanvas-page-number",style:{position:"absolute",bottom:`${Math.max(e.bottom/3,16)}px`,left:0,right:0,textAlign:"center",fontSize:"11px",color:"#999",fontFamily:"system-ui, -apple-system, sans-serif",pointerEvents:"none",userSelect:"none"},children:[t," / ",o]})]}),$e=({visible:s,onAddBelow:e,onEditHTML:t,onDelete:o})=>n.jsxs("div",{className:"dopecanvas-block-toolbar",style:{...Ge,display:s?"flex":"none"},onMouseDown:r=>r.preventDefault(),children:[n.jsx(Se,{onClick:e,title:"Add block below",children:n.jsx(Oe,{})}),n.jsx(Se,{onClick:t,title:"Edit HTML",children:n.jsx(Ve,{})}),n.jsx(Se,{onClick:o,title:"Delete block",danger:!0,children:n.jsx(Ue,{})})]}),Oe=()=>n.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",children:[n.jsx("line",{x1:"7",y1:"3",x2:"7",y2:"11"}),n.jsx("line",{x1:"3",y1:"7",x2:"11",y2:"7"})]}),Ve=()=>n.jsxs("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",children:[n.jsx("polyline",{points:"4.5,3 1.5,7 4.5,11"}),n.jsx("polyline",{points:"9.5,3 12.5,7 9.5,11"})]}),Ue=()=>n.jsx("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",children:n.jsx("path",{d:"M2.5 4h9M5 4V2.5h4V4M3.5 4l.5 8h6l.5-8"})}),Se=({onClick:s,title:e,children:t,danger:o})=>{const[r,a]=l.useState(!1);return n.jsx("button",{onClick:s,title:e,onMouseEnter:()=>a(!0),onMouseLeave:()=>a(!1),style:{...Ke,background:r?o?"#fff0f0":"#f0f0f0":"transparent",color:r&&o?"#d32f2f":"#666"},children:t})},Ge={position:"absolute",top:0,left:-40,flexDirection:"column",gap:"1px",zIndex:100,background:"#fff",borderRadius:"6px",boxShadow:"0 1px 5px rgba(0,0,0,0.12)",padding:"3px",paddingRight:"6px"},Ke={width:"30px",height:"26px",border:"none",cursor:"pointer",borderRadius:"4px",fontSize:"13px",fontFamily:"system-ui, -apple-system, sans-serif",display:"flex",alignItems:"center",justifyContent:"center",padding:0,transition:"background 0.1s, color 0.1s"},Xe=({html:s,onSave:e,onCancel:t})=>{const[o,r]=l.useState(s),a=i=>{if(i.key==="Enter"&&(i.metaKey||i.ctrlKey)&&(i.preventDefault(),e(o)),i.key==="Escape"&&(i.preventDefault(),t()),i.key==="Tab"){i.preventDefault();const u=i.target,p=u.selectionStart,b=u.selectionEnd,C=o.substring(0,p)+" "+o.substring(b);r(C),requestAnimationFrame(()=>{u.selectionStart=u.selectionEnd=p+2})}};return Be.createPortal(n.jsx("div",{style:Ze,onClick:t,children:n.jsxs("div",{style:Je,onClick:i=>i.stopPropagation(),children:[n.jsxs("div",{style:Qe,children:[n.jsx("span",{style:{fontWeight:600,fontSize:"14px"},children:"Edit Block HTML"}),n.jsx("span",{style:{fontSize:"11px",color:"#888"},children:"⌘Enter to save · Escape to cancel"})]}),n.jsx("textarea",{style:Ye,value:o,onChange:i=>r(i.target.value),onKeyDown:a,spellCheck:!1,autoFocus:!0}),n.jsxs("div",{style:et,children:[n.jsx("button",{style:tt,onClick:t,onMouseEnter:i=>{i.target.style.borderColor="#888"},onMouseLeave:i=>{i.target.style.borderColor="#555"},children:"Cancel"}),n.jsx("button",{style:nt,onClick:()=>e(o),onMouseEnter:i=>{i.target.style.background="#0055dd"},onMouseLeave:i=>{i.target.style.background="#0066ff"},children:"Save"})]})]})}),document.body)},Ze={position:"fixed",inset:0,background:"rgba(0, 0, 0, 0.5)",display:"flex",alignItems:"center",justifyContent:"center",zIndex:1e4},Je={width:"min(800px, 90vw)",height:"min(600px, 80vh)",background:"#1e1e1e",borderRadius:"10px",display:"flex",flexDirection:"column",overflow:"hidden",boxShadow:"0 20px 60px rgba(0, 0, 0, 0.4)"},Qe={display:"flex",justifyContent:"space-between",alignItems:"center",padding:"12px 16px",background:"#2d2d2d",color:"#ccc",borderBottom:"1px solid #444"},Ye={flex:1,background:"#1e1e1e",color:"#d4d4d4",border:"none",padding:"16px",fontFamily:"'SF Mono', 'Fira Code', 'Consolas', 'Monaco', monospace",fontSize:"13px",lineHeight:"1.6",resize:"none",outline:"none",tabSize:2},et={display:"flex",justifyContent:"flex-end",gap:"8px",padding:"12px 16px",background:"#2d2d2d",borderTop:"1px solid #444"},tt={padding:"6px 16px",border:"1px solid #555",borderRadius:"6px",background:"transparent",color:"#ccc",cursor:"pointer",fontSize:"13px",transition:"border-color 0.15s"},nt={padding:"6px 16px",border:"none",borderRadius:"6px",background:"#0066ff",color:"#fff",cursor:"pointer",fontSize:"13px",fontWeight:500,transition:"background 0.15s"},Te={letter:{width:816,height:1056},a4:{width:794,height:1123},legal:{width:816,height:1344}},Pe={top:96,right:96,bottom:96,left:96},he={size:"letter",margins:{...Pe}};let ot=0;function De(){return`split-${++ot}-${Date.now()}`}const rt=40,st=new Set(["script","style","img","video","canvas","svg","hr","iframe","object","embed","audio","picture","figure"]),it=new Set(["p","h1","h2","h3","h4","h5","h6","blockquote","div","li","span","td","th","pre","code"]);function de(s,e){const t=s.tagName.toLowerCase();if(st.has(t)||e<rt)return null;const o=window.getComputedStyle(s);if(o.getPropertyValue("break-before")==="page"||o.getPropertyValue("page-break-before")==="always")return null;const r=Array.from(s.children);if(r.length>1){const a=at(s,r,e);if(a)return a}if(it.has(t)||s.childNodes.length>0){const a=lt(s,e);if(a)return a}return null}function _e(s){if(s.length===0)return s;const e=[];let t=0;for(;t<s.length;){const o=s[t],r=Le(o);if(!r){e.push(o),t++;continue}const a=[o];let i=t+1;for(;i<s.length&&Le(s[i])===r;)a.push(s[i]),i++;e.push(ct(a)),t=i}return e}function at(s,e,t){const o=s.getBoundingClientRect().top;let r=-1;for(let p=0;p<e.length;p++){const b=e[p].getBoundingClientRect(),C=window.getComputedStyle(e[p]),k=parseFloat(C.marginBottom)||0;if(b.bottom+k-o>t&&p>0){r=p;break}}if(r<=0)return null;const a=s.cloneNode(!1),i=s.cloneNode(!1);for(let p=0;p<e.length;p++)p<r?a.appendChild(e[p].cloneNode(!0)):i.appendChild(e[p].cloneNode(!0));const u=De();return a.setAttribute("data-dopecanvas-split-id",u),a.setAttribute("data-dopecanvas-split-part","0"),i.setAttribute("data-dopecanvas-split-id",u),i.setAttribute("data-dopecanvas-split-part","1"),{firstHTML:a.outerHTML,secondHTML:i.outerHTML}}function lt(s,e){const t=s.getBoundingClientRect().top,o=[],r=document.createTreeWalker(s,NodeFilter.SHOW_TEXT);for(;r.nextNode();)o.push(r.currentNode);if(o.length===0)return null;let a=null,i=0;for(const b of o){const C=document.createRange();C.selectNodeContents(b);const k=C.getBoundingClientRect();if(k.bottom-t<=e)continue;if(k.top-t>=e){a=b,i=0;break}let T=0,M=b.length;for(;T<M;){const j=Math.floor((T+M)/2),L=document.createRange();L.setStart(b,j),L.collapse(!0),L.getBoundingClientRect().top-t>=e?M=j:T=j+1}a=b,i=T;break}if(!a)return null;const u=a.textContent||"";let p=i;for(;p>0&&u[p-1]!==" "&&u[p-1]!==`
2
+ `;)p--;if(p>0&&(i=p),i===0&&a===o[0]||i>=u.length&&a===o[o.length-1])return null;try{const b=document.createRange();b.setStart(s,0),b.setEnd(a,i);const C=document.createRange();C.setStart(a,i),C.setEndAfter(s.lastChild);const k=b.cloneContents(),T=C.cloneContents(),M=je(k),j=je(T);if(M.trim().length===0||j.trim().length===0)return null;const L=s.cloneNode(!1);L.appendChild(k);const _=s.cloneNode(!1);_.appendChild(T);const $=De();return L.setAttribute("data-dopecanvas-split-id",$),L.setAttribute("data-dopecanvas-split-part","0"),_.setAttribute("data-dopecanvas-split-id",$),_.setAttribute("data-dopecanvas-split-part","1"),{firstHTML:L.outerHTML,secondHTML:_.outerHTML}}catch{return null}}function Le(s){const e=s.match(/data-dopecanvas-split-id="([^"]+)"/);return e?e[1]:null}function ct(s){const e=document.createElement("div"),t=[];let o="",r="";for(const a of s){e.innerHTML=a;const i=e.firstElementChild;if(i){if(!o){o=i.tagName.toLowerCase();const u=i.cloneNode(!1);u.removeAttribute("data-dopecanvas-split-id"),u.removeAttribute("data-dopecanvas-split-part");const p=document.createElement("div");p.appendChild(u);const b=p.innerHTML,C=b.lastIndexOf("</");r=C>=0?b.substring(0,C):b}t.push(i.innerHTML)}}return o?`${r}${t.join("")}</${o}>`:s[0]}function je(s){const e=document.createElement("div");return e.appendChild(s.cloneNode(!0)),e.textContent||""}function ut(s){const e=[];return s.querySelectorAll("script").forEach(t=>{const o=document.createElement("script");Array.from(t.attributes).forEach(r=>o.setAttribute(r.name,r.value)),o.textContent=t.textContent||"",t.parentNode?.replaceChild(o,t),e.push(o)}),e}function dt(s){const e=window.getSelection();if(!e||e.rangeCount===0)return null;const t=e.getRangeAt(0),o=t.startContainer,r=Array.from(s.querySelectorAll(".dopecanvas-block-content")),a=r.findIndex(i=>i.contains(o));if(a===-1)return null;try{const i=document.createRange();i.selectNodeContents(r[a]),i.setEnd(t.startContainer,t.startOffset);const u=i.toString().length;return{blockIndex:a,textOffset:u}}catch{return null}}function ht(s,e){const t=s.querySelectorAll(".dopecanvas-block-content");if(e.blockIndex>=t.length)return;const o=t[e.blockIndex],r=document.createTreeWalker(o,NodeFilter.SHOW_TEXT);let a=e.textOffset;for(;r.nextNode();){const i=r.currentNode;if(a<=i.length){try{const u=document.createRange();u.setStart(i,a),u.collapse(!0);const p=window.getSelection();p?.removeAllRanges(),p?.addRange(u)}catch{}return}a-=i.length}try{const i=document.createRange();i.selectNodeContents(o),i.collapse(!1);const u=window.getSelection();u?.removeAllRanges(),u?.addRange(i)}catch{}}const pt=l.memo(({html:s,isEditable:e,isTable:t})=>n.jsx("div",{className:"dopecanvas-block-content",contentEditable:e&&!t?!0:void 0,suppressContentEditableWarning:!0,dangerouslySetInnerHTML:{__html:s}}));function gt(s){const e=s.replace(/\s+/g," ").toLowerCase();return e.includes("break-before")&&e.includes("page")||e.includes("page-break-before")&&e.includes("always")}const ft=l.forwardRef(({html:s,css:e,pageConfig:t,layoutEngine:o,editableManager:r,onContentChange:a,onPaginationChange:i,showPageBreaks:u=!1},p)=>{const b=l.useRef(null),C=l.useRef(null),k=l.useRef(null),T=l.useRef(a);T.current=a;const[M,j]=l.useState([]),L=l.useRef(null),_=l.useRef(!1),$=l.useRef([]),[pe,K]=l.useState(null),[X,ie]=l.useState(null),[ge,ae]=l.useState(""),Z=l.useRef(null),B=typeof t.size=="string"?Te[t.size]:t.size,le=l.useCallback(()=>{if(!C.current)return;const S=C.current.querySelectorAll(".dopecanvas-block-content"),g=[];S.forEach(v=>{const c=v,d=c.children;if(d.length!==0)if(d.length===1)g.push(d[0].outerHTML);else{const f=document.createElement("div");f.innerHTML=c.innerHTML,g.push(f.outerHTML)}});const R=g.join(`
3
+ `);T.current?.(R)},[]),I=l.useCallback(S=>{if(!b.current)return;const g=b.current,R=o.getContentAreaWidth(),v=o.getContentAreaHeight();if(g.style.width=`${R}px`,g.style.position="absolute",g.style.left="-9999px",g.style.top="0",g.style.visibility="hidden",g.innerHTML="",e){const h=document.createElement("style");h.textContent=e,g.appendChild(h)}const c=document.createElement("div");c.innerHTML=S,g.appendChild(c);const f=o.measureBlocks(c).map(h=>{const E=h.element;return{html:E.cloneNode(!0).outerHTML,height:h.height,element:E,breakBefore:h.breakBefore,breakAfter:h.breakAfter}}),m=[];let y=[],w=0,P=0;for(;P<f.length;){const h=f[P];h.breakBefore&&y.length>0&&(m.push(y),y=[],w=0);const E=v-w;if(h.height<=E)y.push(h.html),w+=h.height;else if(y.length>0&&E>=60){const F=de(h.element,E);if(F){y.push(F.firstHTML),m.push(y),y=[],w=0;const z=document.createElement("div");z.innerHTML=F.secondHTML;const N=z.firstElementChild;c.appendChild(N);const ne=N.getBoundingClientRect(),oe=window.getComputedStyle(N),x=parseFloat(oe.marginTop)||0,W=parseFloat(oe.marginBottom)||0;f.splice(P+1,0,{html:N.outerHTML,height:ne.height+x+W,element:N,breakBefore:!1,breakAfter:h.breakAfter})}else m.push(y),y=[h.html],w=h.height}else if(y.length>0)m.push(y),y=[h.html],w=h.height;else{const F=de(h.element,v);if(F){y.push(F.firstHTML),m.push(y),y=[],w=0;const z=document.createElement("div");z.innerHTML=F.secondHTML;const N=z.firstElementChild;c.appendChild(N);const ne=N.getBoundingClientRect(),oe=window.getComputedStyle(N),x=parseFloat(oe.marginTop)||0,W=parseFloat(oe.marginBottom)||0;f.splice(P+1,0,{html:N.outerHTML,height:ne.height+x+W,element:N,breakBefore:!1,breakAfter:h.breakAfter})}else y.push(h.html),m.push(y),y=[],w=0}h.breakAfter&&y.length>0&&(m.push(y),y=[],w=0),P++}y.length>0&&m.push(y),m.length===0&&m.push([]),g.innerHTML="";const U=m.map(h=>({blocks:h})),D={pages:m.map((h,E)=>({blockIndices:Array.from({length:m[E].length},(F,z)=>z)})),pageCount:m.length};$.current=U,j(U),i?.(D),T.current?.(S)},[e,o,i]),fe=l.useCallback(()=>{if(!C.current||!b.current)return;const S=C.current,g=dt(S),R=S.querySelectorAll(".dopecanvas-block-content"),v=[];if(R.forEach(x=>{const W=x,O=W.children;if(O.length!==0)if(O.length===1)v.push(O[0].outerHTML);else{const J=document.createElement("div");J.innerHTML=W.innerHTML,v.push(J.outerHTML)}}),v.length===0)return;const d=_e(v).join(`
4
+ `),f=b.current,m=o.getContentAreaWidth(),y=o.getContentAreaHeight();if(f.style.width=`${m}px`,f.style.position="absolute",f.style.left="-9999px",f.style.top="0",f.style.visibility="hidden",f.innerHTML="",e){const x=document.createElement("style");x.textContent=e,f.appendChild(x)}const w=document.createElement("div");w.innerHTML=d,f.appendChild(w);const U=o.measureBlocks(w).map(x=>({html:x.element.cloneNode(!0).outerHTML,height:x.height,element:x.element,breakBefore:x.breakBefore,breakAfter:x.breakAfter})),D=[];let h=[],E=0,F=0;for(;F<U.length;){const x=U[F];x.breakBefore&&h.length>0&&(D.push(h),h=[],E=0);const W=y-E;if(x.height<=W)h.push(x.html),E+=x.height;else if(h.length>0&&W>=60){const O=de(x.element,W);if(O){h.push(O.firstHTML),D.push(h),h=[],E=0;const J=document.createElement("div");J.innerHTML=O.secondHTML;const V=J.firstElementChild;w.appendChild(V);const ye=V.getBoundingClientRect(),ue=window.getComputedStyle(V),xe=parseFloat(ue.marginTop)||0,Ce=parseFloat(ue.marginBottom)||0;U.splice(F+1,0,{html:V.outerHTML,height:ye.height+xe+Ce,element:V,breakBefore:!1,breakAfter:x.breakAfter})}else D.push(h),h=[x.html],E=x.height}else if(h.length>0)D.push(h),h=[x.html],E=x.height;else{const O=de(x.element,y);if(O){h.push(O.firstHTML),D.push(h),h=[],E=0;const J=document.createElement("div");J.innerHTML=O.secondHTML;const V=J.firstElementChild;w.appendChild(V);const ye=V.getBoundingClientRect(),ue=window.getComputedStyle(V),xe=parseFloat(ue.marginTop)||0,Ce=parseFloat(ue.marginBottom)||0;U.splice(F+1,0,{html:V.outerHTML,height:ye.height+xe+Ce,element:V,breakBefore:!1,breakAfter:x.breakAfter})}else h.push(x.html),D.push(h),h=[],E=0}x.breakAfter&&h.length>0&&(D.push(h),h=[],E=0),F++}h.length>0&&D.push(h),D.length===0&&D.push([]),f.innerHTML="";const z=D.map(x=>({blocks:x})),N=$.current.map(x=>x.blocks.length),ne=z.map(x=>x.blocks.length);N.length!==ne.length||N.some((x,W)=>x!==ne[W])?(_.current=!0,L.current=g,$.current=z,j(z),i?.({pages:D.map(()=>({blockIndices:[]})),pageCount:D.length})):$.current=z},[e,o,i]),Q=l.useRef(fe);Q.current=fe;const q=l.useCallback(()=>{const S=new DOMParser().parseFromString(s,"text/html");S.head.querySelectorAll('style, link[rel="stylesheet"]').forEach(g=>{S.body.insertBefore(g,S.body.firstChild)}),I(S.body.innerHTML)},[s,I]);l.useEffect(()=>{q()},[q]);const H=l.useCallback(()=>{if(!C.current)return[];const S=C.current.querySelectorAll(".dopecanvas-block-content"),g=[];return S.forEach(R=>{const v=R.firstElementChild;v&&g.push(v.outerHTML)}),g},[]),ee=l.useCallback(S=>{const g=H();g.splice(S+1,0,'<p style="min-height: 1.5em; line-height: 1.6;">&nbsp;</p>'),K(null),I(g.join(`
5
+ `))},[H,I]),te=l.useCallback(S=>{const g=H();g.length<=1||(g.splice(S,1),K(null),I(g.join(`
6
+ `)))},[H,I]),ce=l.useCallback(S=>{const g=H();S<g.length&&(ie(S),ae(g[S]))},[H]),Y=l.useCallback(S=>{if(X===null)return;const g=H();X<g.length&&(g[X]=S),ie(null),ae(""),K(null),I(g.join(`
7
+ `))},[X,H,I]),G=l.useCallback(()=>{ie(null),ae("")},[]),me=l.useCallback(()=>{const S='<div style="break-before: page;"></div>',g=H();let R=g.length;const v=window.getSelection();if(v&&v.rangeCount>0&&C.current){const c=v.getRangeAt(0).startContainer,f=Array.from(C.current.querySelectorAll(".dopecanvas-block-content")).findIndex(m=>m.contains(c));f!==-1&&(R=f+1)}g.splice(R,0,S),K(null),I(g.join(`
8
+ `))},[H,I]);return l.useImperativeHandle(p,()=>({insertPageBreak:me}),[me]),l.useEffect(()=>{const S=C.current;if(!S)return;k.current&&k.current.disconnect(),S.querySelectorAll(".dopecanvas-block-content").forEach(d=>{const f=d.firstElementChild;f&&f.tagName==="TABLE"&&f.querySelectorAll("td, th").forEach(y=>{y.contentEditable="true"})});const R=ut(S);let v=null;const c=new MutationObserver(()=>{_.current||(v&&clearTimeout(v),v=setTimeout(()=>{le(),Q.current()},800))});return c.observe(S,{childList:!0,subtree:!0,characterData:!0,attributes:!1}),k.current=c,L.current?requestAnimationFrame(()=>{L.current&&C.current&&(ht(C.current,L.current),L.current=null),_.current=!1}):_.current=!1,()=>{c.disconnect(),R.forEach(d=>d.remove()),v&&clearTimeout(v)}},[M,le]),n.jsxs("div",{className:"dopecanvas-paged-view",style:mt,children:[n.jsx("div",{ref:b,"aria-hidden":"true"}),n.jsxs("div",{ref:C,style:bt,children:[e&&n.jsx("style",{dangerouslySetInnerHTML:{__html:e}}),M.map((S,g)=>{const R=M.slice(0,g).reduce((v,c)=>v+c.blocks.length,0);return n.jsx(we,{dimensions:B,margins:t.margins,pageNumber:g+1,totalPages:M.length,children:S.blocks.map((v,c)=>{const d=R+c,f=v.trim().toLowerCase(),m=!f.startsWith("<script")&&!f.startsWith("<style"),y=f.startsWith("<table"),w=gt(v);return w&&u?n.jsxs("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},children:[n.jsx("div",{className:"dopecanvas-block-content",style:{display:"none"},dangerouslySetInnerHTML:{__html:v}}),n.jsxs("div",{style:yt,children:[n.jsx("span",{style:He}),n.jsx("span",{style:xt,children:"Page Break"}),n.jsx("span",{style:He}),n.jsx("button",{type:"button",title:"Remove page break",onClick:()=>te(d),onMouseDown:P=>P.preventDefault(),style:Ct,children:"✕"})]})]},`${g}-${c}`):w?n.jsx("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},children:n.jsx("div",{className:"dopecanvas-block-content",dangerouslySetInnerHTML:{__html:v}})},`${g}-${c}`):n.jsxs("div",{className:"dopecanvas-block-wrapper",style:{position:"relative"},onMouseEnter:()=>{m&&(Z.current&&(clearTimeout(Z.current),Z.current=null),K(d))},onMouseLeave:()=>{Z.current=setTimeout(()=>{K(P=>P===d?null:P)},250)},children:[n.jsx(pt,{html:v,isEditable:m,isTable:y}),m&&n.jsx($e,{visible:pe===d,onAddBelow:()=>ee(d),onEditHTML:()=>ce(d),onDelete:()=>te(d)})]},`${g}-${c}`)})},g)}),M.length===0&&n.jsx(we,{dimensions:B,margins:t.margins,pageNumber:1,totalPages:1,children:n.jsx("div",{contentEditable:"true",style:{minHeight:"1em",outline:"none"},"data-placeholder":"Start typing..."})})]}),X!==null&&n.jsx(Xe,{html:ge,onSave:Y,onCancel:G})]})}),mt={flex:1,overflow:"auto",backgroundColor:"#e8e8e8",display:"flex",flexDirection:"column",alignItems:"center"},bt={display:"flex",flexDirection:"column",alignItems:"center",gap:"24px",padding:"24px 0"},yt={display:"flex",alignItems:"center",gap:8,padding:"6px 0",userSelect:"none"},He={flex:1,height:0,borderTop:"2px dashed #b0b0b0"},xt={fontSize:11,fontWeight:600,color:"#888",textTransform:"uppercase",letterSpacing:"0.05em",whiteSpace:"nowrap",fontFamily:"system-ui, -apple-system, sans-serif"},Ct={width:20,height:20,border:"1px solid #ccc",borderRadius:4,backgroundColor:"#fff",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",fontSize:11,color:"#999",padding:0,flexShrink:0,lineHeight:1};class Me{config;constructor(e=he){this.config={...e}}getConfig(){return{...this.config}}setConfig(e){e.size!==void 0&&(this.config.size=e.size),e.margins!==void 0&&(this.config.margins={...e.margins})}getPageDimensions(){return typeof this.config.size=="string"?Te[this.config.size]:this.config.size}getContentAreaHeight(){return this.getPageDimensions().height-this.config.margins.top-this.config.margins.bottom}getContentAreaWidth(){return this.getPageDimensions().width-this.config.margins.left-this.config.margins.right}measureBlocks(e){const t=Array.from(e.children),o=[];for(let r=0;r<t.length;r++){const a=t[r],i=window.getComputedStyle(a),u=i.getPropertyValue("break-before")==="page"||i.getPropertyValue("page-break-before")==="always",p=i.getPropertyValue("break-after")==="page"||i.getPropertyValue("page-break-after")==="always",b=a.getBoundingClientRect(),C=parseFloat(i.marginTop)||0,k=parseFloat(i.marginBottom)||0,T=b.height+C+k;o.push({index:r,height:T,element:a,breakBefore:u,breakAfter:p})}return o}paginate(e){if(e.length===0)return{pages:[{blockIndices:[]}],pageCount:1};const t=this.getContentAreaHeight(),o=[];let r=[],a=0;for(let i=0;i<e.length;i++){const u=e[i];u.breakBefore&&r.length>0&&(o.push({blockIndices:r}),r=[],a=0),a+u.height>t&&r.length>0&&(o.push({blockIndices:r}),r=[],a=0),r.push(u.index),a+=u.height,u.breakAfter&&(o.push({blockIndices:r}),r=[],a=0)}return r.length>0&&o.push({blockIndices:r}),o.length===0&&o.push({blockIndices:[]}),{pages:o,pageCount:o.length}}}class se{observer=null;changeCallbacks=new Set;contextCallbacks=new Set;undoStack=[];redoStack=[];container=null;debounceTimer=null;selectionHandler=null;currentContext="none";static MAX_UNDO_STACK=100;static DEBOUNCE_MS=150;attach(e){this.detach(),this.container=e,this.makeChildrenEditable(e),this.pushUndoSnapshot(),this.observer=new MutationObserver(this.handleMutations),this.observer.observe(e,{childList:!0,subtree:!0,characterData:!0,attributes:!0,attributeFilter:["style","class"]}),this.selectionHandler=this.handleSelectionChange.bind(this),document.addEventListener("selectionchange",this.selectionHandler)}detach(){this.observer&&(this.observer.disconnect(),this.observer=null),this.selectionHandler&&(document.removeEventListener("selectionchange",this.selectionHandler),this.selectionHandler=null),this.debounceTimer&&(clearTimeout(this.debounceTimer),this.debounceTimer=null),this.container=null}makeChildrenEditable(e){const t=Array.from(e.children);for(const o of t)o.tagName==="TABLE"?this.makeTableCellsEditable(o):o.contentEditable="true"}makeTableCellsEditable(e){e.querySelectorAll("td, th").forEach(o=>{o.contentEditable="true"})}handleMutations=e=>{this.debounceTimer&&clearTimeout(this.debounceTimer),this.debounceTimer=setTimeout(()=>{this.pushUndoSnapshot(),this.notifyChange()},se.DEBOUNCE_MS)};handleSelectionChange(){const e=window.getSelection();if(!e||e.rangeCount===0||!this.container){this.setContext("none");return}const o=e.getRangeAt(0).startContainer;let r=o;for(;r&&r!==this.container;){if(r instanceof HTMLElement){const a=r.tagName;if(a==="TD"||a==="TH"||a==="TABLE"){this.setContext("table");return}if(a==="IMG"){this.setContext("image");return}if(r.dataset?.dopecanvasChart){this.setContext("chart");return}}r=r.parentNode}this.container.contains(o)?this.setContext("text"):this.setContext("none")}setContext(e){e!==this.currentContext&&(this.currentContext=e,this.contextCallbacks.forEach(t=>t(e)))}getContext(){return this.currentContext}pushUndoSnapshot(){if(!this.container)return;const e=this.container.innerHTML,t=this.undoStack[this.undoStack.length-1];t&&t.html===e||(this.undoStack.push({html:e,timestamp:Date.now()}),this.redoStack=[],this.undoStack.length>se.MAX_UNDO_STACK&&this.undoStack.shift())}undo(){if(!this.container||this.undoStack.length<=1)return!1;const e=this.undoStack.pop();this.redoStack.push(e);const t=this.undoStack[this.undoStack.length-1];return this.pauseObserver(()=>{this.container.innerHTML=t.html,this.makeChildrenEditable(this.container)}),this.notifyChange(),!0}redo(){if(!this.container||this.redoStack.length===0)return!1;const e=this.redoStack.pop();return this.undoStack.push(e),this.pauseObserver(()=>{this.container.innerHTML=e.html,this.makeChildrenEditable(this.container)}),this.notifyChange(),!0}pauseObserver(e){this.observer&&this.observer.disconnect(),e(),this.observer&&this.container&&this.observer.observe(this.container,{childList:!0,subtree:!0,characterData:!0,attributes:!0,attributeFilter:["style","class"]})}onChange(e){return this.changeCallbacks.add(e),()=>{this.changeCallbacks.delete(e)}}onContextChange(e){return this.contextCallbacks.add(e),()=>{this.contextCallbacks.delete(e)}}notifyChange(){this.changeCallbacks.forEach(e=>e())}execCommand(e,t){return document.execCommand(e,!1,t)}queryCommandState(e){return document.queryCommandState(e)}queryCommandValue(e){return document.queryCommandValue(e)}getHTML(){return this.container?this.container.innerHTML:""}getPlainText(){return this.container&&(this.container.innerText||this.container.textContent)||""}}const St=`
9
9
  :host {
10
10
  display: block;
11
11
  width: 100%;
@@ -47,9 +47,9 @@
47
47
  outline: 1px solid rgba(66, 133, 244, 0.2);
48
48
  border-radius: 2px;
49
49
  }
50
- `;function St(s){return s==="flow"?"flow":"paged"}function vt(s,e){const t=s.trim();return t?t===":root"||t==="html"||t==="body"?e:t==="*"?`${e} *`:t.startsWith(":root")||t.startsWith("html")||t.startsWith("body")?t.replace(/^(:root|html|body)\b/,e):`${e} ${t}`:`${e} *`}function ke(s,e){const t=[],o=typeof CSSMediaRule<"u",r=typeof CSSSupportsRule<"u";for(const a of Array.from(s)){if(a instanceof CSSStyleRule){const i=a.selectorText.split(",").map(d=>vt(d,e)).join(", ");t.push(`${i} { ${a.style.cssText} }`);continue}if(o&&a instanceof CSSMediaRule){t.push(`@media ${a.conditionText} { ${ke(a.cssRules,e)} }`);continue}if(r&&a instanceof CSSSupportsRule){t.push(`@supports ${a.conditionText} { ${ke(a.cssRules,e)} }`);continue}t.push(a.cssText)}return t.join(`
51
- `)}function kt(s,e){if(!s.trim())return s;const t=document.createElement("style");t.textContent=s,document.head.appendChild(t);try{const o=t.sheet;return o?ke(o.cssRules,e):s}catch{return s}finally{t.remove()}}function Tt(s){const e=new DOMParser().parseFromString(s,"text/html"),t=Array.from(e.head.querySelectorAll('style, link[rel="stylesheet"]')).map(o=>o.outerHTML).join(`
52
- `);return{bodyHTML:e.body.innerHTML,headAssetsHTML:t}}const Mt=l.forwardRef(({html:s="",css:e,renderMode:t="page",isolation:o="shadow",pageConfig:r,onContentChange:a,onPageConfigChange:i,style:d},p)=>{const[f,x]=l.useState(r||he),[H,E]=l.useState({pages:[],pageCount:0}),k=l.useRef(s),R=l.useRef(null),T=l.useRef(null),[N,O]=l.useState(null),[pe,Z]=l.useState(""),j=l.useRef(null),ee=l.useRef(null),[ae,le]=l.useState(!1),X=l.useRef(a);X.current=a;const q=r||f,K=St(t),D=o==="iframe"?"shadow":o,te=D==="shadow",ne=l.useMemo(()=>e&&(D==="scoped-css"||D==="shadow")?kt(e,".dopecanvas-root"):e,[e,D]),J=l.useMemo(()=>new Me(q),[]),A=l.useMemo(()=>new ie,[]);l.useEffect(()=>{J.setConfig(q)},[q,J]),l.useEffect(()=>{const g=R.current;if(!g)return;if(!te){O(null);return}let C=g.shadowRoot;C||(C=g.attachShadow({mode:"open"})),C.innerHTML="";const h=document.createElement("style");h.textContent=yt,C.appendChild(h);const c=document.createElement("div");return c.style.height="100%",c.style.width="100%",C.appendChild(c),O(c),()=>{g.shadowRoot&&(g.shadowRoot.innerHTML=""),O(null)}},[te]);const me=l.useCallback(g=>{k.current=g,X.current?.(g)},[]),ce=l.useCallback(g=>{const C={...q,...g,margins:{...q.margins,...g.margins||{}}};x(C),J.setConfig(C),i?.(C)},[q,J,i]),be=l.useCallback(g=>{E(g)},[]);l.useEffect(()=>{if(K!=="flow"||!j.current)return;const g=k.current||s,C=Tt(g);j.current.innerHTML=C.bodyHTML,Z(C.headAssetsHTML),k.current=C.bodyHTML},[s,K,N]),l.useImperativeHandle(p,()=>({execCommand:(g,C)=>A.execCommand(g,C),queryCommandState:g=>A.queryCommandState(g),queryCommandValue:g=>A.queryCommandValue(g),getPageConfig:()=>({...q}),setPageConfig:g=>{ce(g)},getPageCount:()=>H.pageCount,getHTML:()=>{if(K==="flow"&&j.current){const g=j.current.innerHTML;return g?(k.current=g,g):k.current}if(T.current){const g=T.current.querySelectorAll(".dopecanvas-block-content");if(g.length>0){const C=[];if(g.forEach(h=>{const c=h,L=c.children;if(L.length!==0)if(L.length===1)C.push(L[0].outerHTML);else{const b=document.createElement("div");b.innerHTML=c.innerHTML,C.push(b.outerHTML)}}),C.length>0){const h=C.join(`
53
- `);return k.current=h,h}}}return k.current},getPlainText:()=>{if(K==="flow"&&j.current)return j.current.innerText||j.current.textContent||"";const g=T.current?(()=>{const h=T.current.querySelectorAll(".dopecanvas-block-content"),c=[];return h.forEach(L=>{const b=L,v=b.children;if(v.length!==0)if(v.length===1)c.push(v[0].outerHTML);else{const w=document.createElement("div");w.innerHTML=b.innerHTML,c.push(w.outerHTML)}}),c.length>0?c.join(`
54
- `):k.current})():k.current,C=document.createElement("div");return C.innerHTML=g,C.innerText||C.textContent||""},undo:()=>A.undo(),redo:()=>A.redo(),insertPageBreak:()=>{if(K==="flow"&&j.current){const g=window.getSelection();if(!g||g.rangeCount===0)return;const C=g.getRangeAt(0);if(!j.current.contains(C.startContainer))return;const h=document.createElement("div");h.style.breakBefore="page",C.insertNode(h),C.setStartAfter(h),C.collapse(!0),g.removeAllRanges(),g.addRange(C);const c=j.current.innerHTML;k.current=c,X.current?.(c);return}ee.current?.insertPageBreak()},setShowPageBreaks:g=>{le(g)},getShowPageBreaks:()=>ae}),[A,q,H.pageCount,ce,ae,K]);const ge=n.jsx("div",{ref:T,className:"dopecanvas-root",style:{display:"flex",flexDirection:"column",height:"100%",width:"100%",minHeight:0,minWidth:0,fontFamily:"system-ui, -apple-system, BlinkMacSystemFont, sans-serif",...d},children:K==="paged"?n.jsx(gt,{ref:ee,html:s,css:ne,pageConfig:q,layoutEngine:J,editableManager:A,onContentChange:me,onPaginationChange:be,showPageBreaks:ae}):n.jsxs("div",{style:wt,children:[ne&&n.jsx("style",{dangerouslySetInnerHTML:{__html:ne}}),pe&&n.jsx("div",{className:"dopecanvas-flow-head-assets",dangerouslySetInnerHTML:{__html:pe}}),n.jsx("div",{ref:j,className:"dopecanvas-flow-content",contentEditable:!0,suppressContentEditableWarning:!0,style:Lt,onInput:()=>{if(!j.current)return;const g=j.current.innerHTML;k.current=g,X.current?.(g)}})]})});return n.jsx("div",{ref:R,className:"dopecanvas-host",style:{height:"100%",width:"100%",minHeight:0,minWidth:0,overflow:"hidden"},children:te?N?Be.createPortal(ge,N):null:ge})}),wt={flex:1,overflow:"auto",backgroundColor:"#f7f7f5",padding:"24px"},Lt={minHeight:"100%",outline:"none",backgroundColor:"#fff",border:"1px solid #e5e7eb",borderRadius:8,padding:"20px 24px"};class Ie{layoutEngine;editableManager;sourceHTML="";sourceCSS="";measureContainer=null;contentContainer=null;paginationResult={pages:[],pageCount:0};paginationCallbacks=new Set;changeCallbacks=new Set;constructor(e=he){this.layoutEngine=new Me(e),this.editableManager=new ie}getLayoutEngine(){return this.layoutEngine}getEditableManager(){return this.editableManager}getPaginationResult(){return this.paginationResult}getSourceHTML(){return this.sourceHTML}getPageConfig(){return this.layoutEngine.getConfig()}loadHTML(e,t){this.sourceHTML=e,this.sourceCSS=t||""}setMeasureContainer(e){this.measureContainer=e}setContentContainer(e){this.contentContainer&&this.editableManager.detach(),this.contentContainer=e}runPagination(){if(!this.measureContainer)return{result:{pages:[{blockIndices:[]}],pageCount:1},measurements:[]};const e=this.layoutEngine.getContentAreaWidth();this.measureContainer.style.width=`${e}px`,this.measureContainer.style.position="absolute",this.measureContainer.style.left="-9999px",this.measureContainer.style.top="0",this.measureContainer.style.visibility="hidden";let t=null;this.sourceCSS&&(t=document.createElement("style"),t.textContent=this.sourceCSS,this.measureContainer.appendChild(t));const o=document.createElement("div");o.innerHTML=this.sourceHTML,this.measureContainer.appendChild(o);const r=this.layoutEngine.measureBlocks(o);return this.paginationResult=this.layoutEngine.paginate(r),this.measureContainer.innerHTML="",this.paginationCallbacks.forEach(a=>a(this.paginationResult)),{result:this.paginationResult,measurements:r}}rePaginate(){if(!this.contentContainer)return this.paginationResult;const e=this.layoutEngine.measureBlocks(this.contentContainer);return this.paginationResult=this.layoutEngine.paginate(e),this.paginationCallbacks.forEach(t=>t(this.paginationResult)),this.paginationResult}attachEditing(e){this.contentContainer=e,this.editableManager.attach(e),this.editableManager.onChange(()=>{this.sourceHTML=e.innerHTML,this.changeCallbacks.forEach(t=>t(this.sourceHTML))})}setPageConfig(e){this.layoutEngine.setConfig(e)}onPagination(e){return this.paginationCallbacks.add(e),()=>{this.paginationCallbacks.delete(e)}}onChange(e){return this.changeCallbacks.add(e),()=>{this.changeCallbacks.delete(e)}}getHTML(){return this.contentContainer?this.contentContainer.innerHTML:this.sourceHTML}getPlainText(){return this.contentContainer&&(this.contentContainer.innerText||this.contentContainer.textContent)||""}destroy(){this.editableManager.detach(),this.paginationCallbacks.clear(),this.changeCallbacks.clear(),this.measureContainer=null,this.contentContainer=null}}class jt{_html="";_css="";_pageConfig=null;_paginationResult={pages:[],pageCount:0};_changeCallbacks=new Set;_loadCallbacks=new Set;_pageConfigCallbacks=new Set;_getHTMLFn=null;_getPlainTextFn=null;loadHTML(e,t){this._html=e,this._css=t||"",this._loadCallbacks.forEach(o=>o(e,t))}getHTML(){return this._getHTMLFn?this._getHTMLFn():this._html}getPlainText(){if(this._getPlainTextFn)return this._getPlainTextFn();const e=document.createElement("div");return e.innerHTML=this._html,e.innerText||e.textContent||""}onChange(e){return this._changeCallbacks.add(e),()=>{this._changeCallbacks.delete(e)}}onLoad(e){return this._loadCallbacks.add(e),()=>{this._loadCallbacks.delete(e)}}onPageConfigChange(e){return this._pageConfigCallbacks.add(e),()=>{this._pageConfigCallbacks.delete(e)}}getPageCount(){return this._paginationResult.pageCount}getPageConfig(){return this._pageConfig}setPageConfig(e){this._pageConfig&&(this._pageConfig={...this._pageConfig,...e,margins:{...this._pageConfig.margins,...e.margins||{}}},this._pageConfigCallbacks.forEach(t=>t(this._pageConfig)))}querySelectorAll(e){const t=document.createElement("div");return t.innerHTML=this.getHTML(),Array.from(t.querySelectorAll(e))}getElementContent(e){const t=document.createElement("div");t.innerHTML=this.getHTML();const o=t.querySelector(`#${e}`);return o?o.innerHTML:null}setElementContent(e,t){const o=document.createElement("div");o.innerHTML=this.getHTML();const r=o.querySelector(`#${e}`);r&&(r.innerHTML=t,this.loadHTML(o.innerHTML,this._css))}_connectGetHTML(e){this._getHTMLFn=e}_connectGetPlainText(e){this._getPlainTextFn=e}_notifyChange(e){this._html=e,this._changeCallbacks.forEach(t=>t(e))}_updatePagination(e){this._paginationResult=e}_updatePageConfig(e){this._pageConfig=e}}function Ht(s){const[e,t]=l.useState("none");return l.useEffect(()=>s?s.onContextChange(r=>{t(r)}):void 0,[s]),e}const Et={bold:!1,italic:!1,underline:!1,strikethrough:!1,justifyLeft:!1,justifyCenter:!1,justifyRight:!1,justifyFull:!1,orderedList:!1,unorderedList:!1,superscript:!1,subscript:!1,fontName:"",fontSize:"3",foreColor:"#000000",backColor:"",formatBlock:"p"};function Ne(){const[s,e]=l.useState(Et),t=l.useCallback(()=>{try{let o=document.queryCommandValue("formatBlock")||"";o=o.replace(/^<|>$/g,"").toLowerCase(),(!o||o==="div")&&(o="p"),e({bold:document.queryCommandState("bold"),italic:document.queryCommandState("italic"),underline:document.queryCommandState("underline"),strikethrough:document.queryCommandState("strikethrough"),justifyLeft:document.queryCommandState("justifyLeft"),justifyCenter:document.queryCommandState("justifyCenter"),justifyRight:document.queryCommandState("justifyRight"),justifyFull:document.queryCommandState("justifyFull"),orderedList:document.queryCommandState("insertOrderedList"),unorderedList:document.queryCommandState("insertUnorderedList"),superscript:document.queryCommandState("superscript"),subscript:document.queryCommandState("subscript"),fontName:document.queryCommandValue("fontName")||"",fontSize:document.queryCommandValue("fontSize")||"3",foreColor:document.queryCommandValue("foreColor")||"#000000",backColor:document.queryCommandValue("backColor")||"",formatBlock:o})}catch{}},[]);return l.useEffect(()=>(document.addEventListener("selectionchange",t),()=>{document.removeEventListener("selectionchange",t)}),[t]),s}function Fe(){const s=l.useRef(null),e=l.useCallback(()=>{const r=window.getSelection();r&&r.rangeCount>0&&(s.current=r.getRangeAt(0).cloneRange())},[]),t=l.useCallback(()=>{const r=s.current;if(!r)return;const a=window.getSelection();a&&(a.removeAllRanges(),a.addRange(r))},[]),o=l.useCallback(r=>{t(),r()},[t]);return{saveSelection:e,restoreSelection:t,restoreAndExec:o}}const ze=({onExecCommand:s})=>{const e=Ne(),{saveSelection:t,restoreAndExec:o}=Fe(),r=l.useCallback((p,f)=>x=>{x.preventDefault(),s(p,f)},[s]),a=l.useCallback(p=>f=>{o(()=>s(p,f.target.value))},[s,o]),i=l.useCallback(p=>f=>{o(()=>s(p,f.target.value))},[s,o]),d=l.useCallback(p=>{p.preventDefault();const f=prompt("Enter URL:");f&&s("createLink",f)},[s]);return n.jsxs("div",{style:Rt,children:[n.jsxs("select",{value:e.formatBlock,onChange:a("formatBlock"),style:ve,title:"Block Format",onMouseDown:t,children:[n.jsx("option",{value:"p",children:"Paragraph"}),n.jsx("option",{value:"h1",children:"Heading 1"}),n.jsx("option",{value:"h2",children:"Heading 2"}),n.jsx("option",{value:"h3",children:"Heading 3"}),n.jsx("option",{value:"h4",children:"Heading 4"}),n.jsx("option",{value:"h5",children:"Heading 5"}),n.jsx("option",{value:"h6",children:"Heading 6"})]}),n.jsx(se,{}),n.jsxs("select",{value:e.fontName.replace(/['"]/g,""),onChange:a("fontName"),style:{...ve,width:110},title:"Font Family",onMouseDown:t,children:[n.jsx("option",{value:"Arial",children:"Arial"}),n.jsx("option",{value:"Georgia",children:"Georgia"}),n.jsx("option",{value:"Times New Roman",children:"Times New Roman"}),n.jsx("option",{value:"Courier New",children:"Courier New"}),n.jsx("option",{value:"Verdana",children:"Verdana"}),n.jsx("option",{value:"Trebuchet MS",children:"Trebuchet MS"}),n.jsx("option",{value:"Comic Sans MS",children:"Comic Sans MS"}),n.jsx("option",{value:"Impact",children:"Impact"}),n.jsx("option",{value:"system-ui",children:"System UI"})]}),n.jsxs("select",{value:e.fontSize,onChange:a("fontSize"),style:{...ve,width:56},title:"Font Size",onMouseDown:t,children:[n.jsx("option",{value:"1",children:"8"}),n.jsx("option",{value:"2",children:"10"}),n.jsx("option",{value:"3",children:"12"}),n.jsx("option",{value:"4",children:"14"}),n.jsx("option",{value:"5",children:"18"}),n.jsx("option",{value:"6",children:"24"}),n.jsx("option",{value:"7",children:"36"})]}),n.jsx(se,{}),n.jsx(_,{icon:"B",title:"Bold (Ctrl+B)",active:e.bold,onMouseDown:r("bold"),extraStyle:{fontWeight:700}}),n.jsx(_,{icon:"I",title:"Italic (Ctrl+I)",active:e.italic,onMouseDown:r("italic"),extraStyle:{fontStyle:"italic"}}),n.jsx(_,{icon:"U",title:"Underline (Ctrl+U)",active:e.underline,onMouseDown:r("underline"),extraStyle:{textDecoration:"underline"}}),n.jsx(_,{icon:"S",title:"Strikethrough",active:e.strikethrough,onMouseDown:r("strikethrough"),extraStyle:{textDecoration:"line-through"}}),n.jsx(_,{icon:"x²",title:"Superscript",active:e.superscript,onMouseDown:r("superscript")}),n.jsx(_,{icon:"x₂",title:"Subscript",active:e.subscript,onMouseDown:r("subscript")}),n.jsx(se,{}),n.jsx(Ee,{label:"A",title:"Text Color",defaultValue:"#000000",onChange:i("foreColor"),onMouseDown:t}),n.jsx(Ee,{label:"A",title:"Highlight Color",defaultValue:"#ffff00",onChange:i("hiliteColor"),onMouseDown:t,highlight:!0}),n.jsx(se,{}),n.jsx(_,{icon:"≡",title:"Align Left",active:e.justifyLeft,onMouseDown:r("justifyLeft")}),n.jsx(_,{icon:"≣",title:"Align Center",active:e.justifyCenter,onMouseDown:r("justifyCenter")}),n.jsx(_,{icon:"≢",title:"Align Right",active:e.justifyRight,onMouseDown:r("justifyRight")}),n.jsx(_,{icon:"☰",title:"Justify",active:e.justifyFull,onMouseDown:r("justifyFull")}),n.jsx(se,{}),n.jsx(_,{icon:"•",title:"Bullet List",active:e.unorderedList,onMouseDown:r("insertUnorderedList")}),n.jsx(_,{icon:"1.",title:"Numbered List",active:e.orderedList,onMouseDown:r("insertOrderedList"),extraStyle:{fontSize:"11px",fontWeight:600}}),n.jsx(_,{icon:"⇤",title:"Decrease Indent",onMouseDown:r("outdent")}),n.jsx(_,{icon:"⇥",title:"Increase Indent",onMouseDown:r("indent")}),n.jsx(se,{}),n.jsx(_,{icon:"🔗",title:"Insert Link",onMouseDown:d}),n.jsx(_,{icon:"—",title:"Horizontal Rule",onMouseDown:r("insertHorizontalRule")}),n.jsx(_,{icon:"T̸",title:"Clear Formatting",onMouseDown:r("removeFormat")})]})},_=({icon:s,title:e,active:t,onMouseDown:o,extraStyle:r})=>n.jsx("button",{type:"button",title:e,onMouseDown:o,style:{width:"28px",height:"28px",borderWidth:"1px",borderStyle:"solid",borderColor:t?"#b0b5bd":"transparent",borderRadius:"3px",backgroundColor:t?"#d0d5dd":"transparent",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",fontSize:"13px",color:"#333",padding:0,fontFamily:"inherit",...r},dangerouslySetInnerHTML:{__html:s}}),se=()=>n.jsx("div",{style:At}),Ee=({label:s,title:e,defaultValue:t,onChange:o,onMouseDown:r,highlight:a})=>n.jsxs("label",{style:Bt,title:e,onMouseDown:r,children:[a?n.jsx("span",{style:{backgroundColor:"#ffff00",padding:"0 2px"},children:s}):s,n.jsx("input",{type:"color",defaultValue:t,onChange:o,style:Pt})]}),Rt={display:"flex",alignItems:"center",gap:"2px",flexWrap:"wrap"},ve={height:"28px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"12px",padding:"0 4px",cursor:"pointer",backgroundColor:"#fff"},At={width:"1px",height:"20px",backgroundColor:"#ddd",margin:"0 4px"},Bt={position:"relative",width:"28px",height:"28px",display:"flex",alignItems:"center",justifyContent:"center",cursor:"pointer",fontSize:"13px",fontWeight:"bold"},Pt={position:"absolute",bottom:0,left:0,width:"100%",height:"4px",padding:0,borderWidth:0,borderStyle:"none",cursor:"pointer"},qe=({pageConfig:s,pageCount:e,onPageConfigChange:t})=>{const o=l.useCallback(i=>{const d=i.target.value;t({size:d})},[t]),r=l.useCallback(i=>d=>{const p=Math.max(0,parseInt(d.target.value)||0);t({margins:{...s.margins,[i]:p}})},[s.margins,t]),a=typeof s.size=="string"?s.size:"custom";return n.jsxs("div",{style:_t,children:[n.jsxs("label",{style:Dt,children:["Page:",n.jsxs("select",{value:a,onChange:o,style:It,children:[n.jsx("option",{value:"letter",children:"Letter (8.5 x 11)"}),n.jsx("option",{value:"a4",children:"A4 (210 x 297mm)"}),n.jsx("option",{value:"legal",children:"Legal (8.5 x 14)"})]})]}),n.jsx("div",{style:Re}),n.jsx("span",{style:{fontSize:"12px",color:"#666"},children:"Margins (px):"}),n.jsx(fe,{label:"T",value:s.margins.top,onChange:r("top")}),n.jsx(fe,{label:"R",value:s.margins.right,onChange:r("right")}),n.jsx(fe,{label:"B",value:s.margins.bottom,onChange:r("bottom")}),n.jsx(fe,{label:"L",value:s.margins.left,onChange:r("left")}),n.jsx("div",{style:Re}),n.jsxs("span",{style:{fontSize:"12px",color:"#666"},children:[e," ",e===1?"page":"pages"]})]})},fe=({label:s,value:e,onChange:t})=>n.jsxs("label",{style:Nt,title:`${s} margin`,children:[s,":",n.jsx("input",{type:"number",value:e,onChange:t,style:Ft,min:0,max:300,step:12})]}),_t={display:"flex",alignItems:"center",gap:"6px",flexWrap:"wrap"},Dt={display:"flex",alignItems:"center",gap:"4px",fontSize:"12px",color:"#666"},It={height:"26px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"12px",padding:"0 4px",cursor:"pointer",backgroundColor:"#fff"},Re={width:"1px",height:"20px",backgroundColor:"#ddd",margin:"0 4px"},Nt={display:"flex",alignItems:"center",gap:"2px",fontSize:"11px",color:"#666"},Ft={width:"44px",height:"24px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"11px",textAlign:"center",padding:"0 2px"},zt=({pageConfig:s,pageCount:e,onExecCommand:t,onPageConfigChange:o})=>n.jsxs("div",{style:qt,children:[n.jsx("div",{style:Ae,children:n.jsx(ze,{onExecCommand:t})}),n.jsx("div",{style:Ae,children:n.jsx(qe,{pageConfig:s,pageCount:e,onPageConfigChange:o})})]}),qt={borderBottomWidth:"1px",borderBottomStyle:"solid",borderBottomColor:"#d0d0d0",backgroundColor:"#f8f8f8",padding:"4px 8px",display:"flex",flexDirection:"column",gap:"4px",flexShrink:0,zIndex:10},Ae={display:"flex",alignItems:"center",gap:"4px",minHeight:"32px"};function Wt(s={}){const{initialHTML:e="",initialCSS:t="",initialConfig:o=he}=s,r=l.useRef(new Ie(o)),[a,i]=l.useState({pages:[{blockIndices:[]}],pageCount:1}),[d,p]=l.useState(o),f=l.useCallback((R,T)=>{r.current.loadHTML(R,T)},[]),x=l.useCallback(R=>{r.current.setPageConfig(R),p(r.current.getPageConfig())},[]),H=l.useCallback(()=>{const{result:R}=r.current.runPagination();i(R)},[]),E=l.useCallback(()=>r.current.getHTML(),[]),k=l.useCallback(()=>r.current.getPlainText(),[]);return l.useEffect(()=>{e&&r.current.loadHTML(e,t)},[e,t]),l.useEffect(()=>r.current.onPagination(T=>{i(T)}),[]),l.useEffect(()=>()=>{r.current.destroy()},[]),{engine:r.current,paginationResult:a,pageConfig:d,loadHTML:f,setPageConfig:x,triggerPagination:H,getHTML:E,getPlainText:k}}exports.DEFAULT_MARGINS=Pe;exports.DEFAULT_PAGE_CONFIG=he;exports.DocumentAPI=jt;exports.DocumentEngine=Ie;exports.DopeCanvas=Mt;exports.EditableManager=ie;exports.PAGE_SIZE_PRESETS=Te;exports.PageLayoutEngine=Me;exports.PageSetupToolbar=qe;exports.TextToolbar=ze;exports.Toolbar=zt;exports.recombineSplitBlocks=De;exports.trySplitBlock=de;exports.useDocumentEngine=Wt;exports.useFormattingState=Ne;exports.useSelectionContext=Ht;exports.useSelectionSaver=Fe;
50
+ `;function vt(s){return s==="flow"?"flow":"paged"}function kt(s,e){const t=s.trim();return t?t===":root"||t==="html"||t==="body"?e:t==="*"?`${e} *`:t.startsWith(":root")||t.startsWith("html")||t.startsWith("body")?t.replace(/^(:root|html|body)\b/,e):`${e} ${t}`:`${e} *`}function ke(s,e){const t=[],o=typeof CSSMediaRule<"u",r=typeof CSSSupportsRule<"u";for(const a of Array.from(s)){if(a instanceof CSSStyleRule){const i=a.selectorText.split(",").map(u=>kt(u,e)).join(", ");t.push(`${i} { ${a.style.cssText} }`);continue}if(o&&a instanceof CSSMediaRule){t.push(`@media ${a.conditionText} { ${ke(a.cssRules,e)} }`);continue}if(r&&a instanceof CSSSupportsRule){t.push(`@supports ${a.conditionText} { ${ke(a.cssRules,e)} }`);continue}t.push(a.cssText)}return t.join(`
51
+ `)}function Ie(s,e){if(!s.trim())return s;const t=document.createElement("style");t.textContent=s,document.head.appendChild(t);try{const o=t.sheet;return o?ke(o.cssRules,e):s}catch{return s}finally{t.remove()}}function Tt(s){const e={};for(const t of s){if(t.tagName.toLowerCase()!=="style")continue;const o=document.createElement("style");o.textContent=t.textContent||"",document.head.appendChild(o);try{const r=o.sheet;if(!r)continue;for(const a of Array.from(r.cssRules)){if(!(a instanceof CSSStyleRule))continue;if(a.selectorText.split(",").map(u=>u.trim()).some(u=>u==="body"||u==="html"||u===":root"))for(const u of Array.from(a.style))e[u]=a.style.getPropertyValue(u)}}catch{}finally{o.remove()}}return e}function Mt(s){const e=/<\s*(?:!doctype|html|head|body)\b/i.test(s),t=new DOMParser().parseFromString(s,"text/html"),o=e?t.head.innerHTML:"",r=[...Array.from(t.head.querySelectorAll('style, link[rel="stylesheet"]')),...Array.from(t.body.querySelectorAll('style, link[rel="stylesheet"]'))],a=e?Tt(r):{};if(e&&t.body.getAttribute("style")){const k=document.createElement("div");k.style.cssText=t.body.getAttribute("style");for(const T of Array.from(k.style))a[T]=k.style.getPropertyValue(T)}const i=".dopecanvas-flow-content",u=k=>Array.from(k.querySelectorAll('style, link[rel="stylesheet"]')).map(T=>T.tagName.toLowerCase()!=="style"?T.outerHTML:`<style>${e?Ie(T.textContent||"",i):T.textContent||""}</style>`),p=u(t.head),b=u(t.body);t.body.querySelectorAll('style, link[rel="stylesheet"]').forEach(k=>k.remove()),t.body.querySelectorAll("script").forEach(k=>k.remove());const C=e?Array.from(t.head.querySelectorAll('link[rel="preconnect"], link[href*="fonts"]')).map(k=>k.outerHTML):[];return{bodyHTML:t.body.innerHTML,headAssetsHTML:[...C,...p,...b].join(`
52
+ `),rawHeadHTML:o,isFullDocument:e,bodyElementStyles:a}}const wt=l.forwardRef(({html:s="",css:e,renderMode:t="page",isolation:o="shadow",pageConfig:r,onContentChange:a,onPageConfigChange:i,style:u},p)=>{const[b,C]=l.useState(r||he),[k,T]=l.useState({pages:[],pageCount:0}),M=l.useRef(s),j=l.useRef(null),L=l.useRef(null),[_,$]=l.useState(null),[pe,K]=l.useState(""),[X,ie]=l.useState(!1),[ge,ae]=l.useState({}),Z=l.useRef(""),B=l.useRef(null),le=l.useRef(null),[I,fe]=l.useState(!1),Q=l.useRef(a);Q.current=a;const q=r||b,H=vt(t),ee=o==="iframe"?"shadow":o,te=ee==="shadow",ce=l.useMemo(()=>e&&(ee==="scoped-css"||ee==="shadow")?Ie(e,".dopecanvas-root"):e,[e,ee]),Y=l.useMemo(()=>new Me(q),[]),G=l.useMemo(()=>new se,[]);l.useEffect(()=>{Y.setConfig(q)},[q,Y]),l.useEffect(()=>{const c=j.current;if(!c)return;if(!te){$(null);return}let d=c.shadowRoot;d||(d=c.attachShadow({mode:"open"})),d.innerHTML="";const f=document.createElement("style");f.textContent=St,d.appendChild(f);const m=document.createElement("div");return m.style.height="100%",m.style.width="100%",d.appendChild(m),$(m),()=>{c.shadowRoot&&(c.shadowRoot.innerHTML=""),$(null)}},[te]);const me=l.useCallback(c=>{M.current=c,Q.current?.(c)},[]),S=l.useCallback(c=>{const d={...q,...c,margins:{...q.margins,...c.margins||{}}};C(d),Y.setConfig(d),i?.(d)},[q,Y,i]),g=l.useCallback(c=>{T(c)},[]);l.useEffect(()=>{if(H!=="flow"||!B.current)return;const c=M.current||s,d=Mt(c);B.current.innerHTML=d.bodyHTML,K(d.headAssetsHTML),ie(d.isFullDocument),ae(d.bodyElementStyles),Z.current=d.rawHeadHTML,M.current=d.bodyHTML},[s,H,_]),l.useImperativeHandle(p,()=>({execCommand:(c,d)=>G.execCommand(c,d),queryCommandState:c=>G.queryCommandState(c),queryCommandValue:c=>G.queryCommandValue(c),getPageConfig:()=>({...q}),setPageConfig:c=>{S(c)},getPageCount:()=>k.pageCount,getHTML:()=>{if(H==="flow"&&B.current){const c=B.current.innerHTML;c&&(M.current=c);const d=M.current;return Z.current?`<html><head>${Z.current}</head><body>${d}</body></html>`:d}if(L.current){const c=L.current.querySelectorAll(".dopecanvas-block-content");if(c.length>0){const d=[];if(c.forEach(f=>{const m=f,y=m.children;if(y.length!==0)if(y.length===1)d.push(y[0].outerHTML);else{const w=document.createElement("div");w.innerHTML=m.innerHTML,d.push(w.outerHTML)}}),d.length>0){const f=d.join(`
53
+ `);return M.current=f,f}}}return M.current},getPlainText:()=>{if(H==="flow"&&B.current)return B.current.innerText||B.current.textContent||"";const c=L.current?(()=>{const f=L.current.querySelectorAll(".dopecanvas-block-content"),m=[];return f.forEach(y=>{const w=y,P=w.children;if(P.length!==0)if(P.length===1)m.push(P[0].outerHTML);else{const U=document.createElement("div");U.innerHTML=w.innerHTML,m.push(U.outerHTML)}}),m.length>0?m.join(`
54
+ `):M.current})():M.current,d=document.createElement("div");return d.innerHTML=c,d.innerText||d.textContent||""},undo:()=>G.undo(),redo:()=>G.redo(),insertPageBreak:()=>{if(H==="flow"&&B.current){const c=window.getSelection();if(!c||c.rangeCount===0)return;const d=c.getRangeAt(0);if(!B.current.contains(d.startContainer))return;const f=document.createElement("div");f.style.breakBefore="page",d.insertNode(f),d.setStartAfter(f),d.collapse(!0),c.removeAllRanges(),c.addRange(d);const m=B.current.innerHTML;M.current=m,Q.current?.(m);return}le.current?.insertPageBreak()},setShowPageBreaks:c=>{fe(c)},getShowPageBreaks:()=>I}),[G,q,k.pageCount,S,I,H]);const R=l.useMemo(()=>{const c={};for(const[d,f]of Object.entries(ge)){const m=d.replace(/-([a-z])/g,(y,w)=>w.toUpperCase());c[m]=f}return c},[ge]),v=n.jsx("div",{ref:L,className:"dopecanvas-root",style:{display:"flex",flexDirection:"column",height:"100%",width:"100%",minHeight:0,minWidth:0,fontFamily:"system-ui, -apple-system, BlinkMacSystemFont, sans-serif",...u},children:H==="paged"?n.jsx(ft,{ref:le,html:s,css:ce,pageConfig:q,layoutEngine:Y,editableManager:G,onContentChange:me,onPaginationChange:g,showPageBreaks:I}):n.jsxs("div",{style:X?{...Ht,...R}:Lt,children:[ce&&n.jsx("style",{dangerouslySetInnerHTML:{__html:ce}}),pe&&n.jsx("div",{className:"dopecanvas-flow-head-assets",dangerouslySetInnerHTML:{__html:pe}}),n.jsx("div",{ref:B,className:"dopecanvas-flow-content",contentEditable:!0,suppressContentEditableWarning:!0,style:X?{...Et,...R}:jt,onInput:()=>{if(!B.current)return;const c=B.current.innerHTML;M.current=c,Q.current?.(c)}})]})});return n.jsx("div",{ref:j,className:"dopecanvas-host",style:{height:"100%",width:"100%",minHeight:0,minWidth:0,overflow:"hidden"},children:te?_?Be.createPortal(v,_):null:v})}),Lt={flex:1,overflow:"auto",backgroundColor:"#f7f7f5",padding:"24px"},jt={minHeight:"100%",outline:"none",backgroundColor:"#fff",border:"1px solid #e5e7eb",borderRadius:8,padding:"20px 24px"},Ht={flex:1,overflow:"auto",padding:0},Et={minHeight:"100%",outline:"none"};class Fe{layoutEngine;editableManager;sourceHTML="";sourceCSS="";measureContainer=null;contentContainer=null;paginationResult={pages:[],pageCount:0};paginationCallbacks=new Set;changeCallbacks=new Set;constructor(e=he){this.layoutEngine=new Me(e),this.editableManager=new se}getLayoutEngine(){return this.layoutEngine}getEditableManager(){return this.editableManager}getPaginationResult(){return this.paginationResult}getSourceHTML(){return this.sourceHTML}getPageConfig(){return this.layoutEngine.getConfig()}loadHTML(e,t){this.sourceHTML=e,this.sourceCSS=t||""}setMeasureContainer(e){this.measureContainer=e}setContentContainer(e){this.contentContainer&&this.editableManager.detach(),this.contentContainer=e}runPagination(){if(!this.measureContainer)return{result:{pages:[{blockIndices:[]}],pageCount:1},measurements:[]};const e=this.layoutEngine.getContentAreaWidth();this.measureContainer.style.width=`${e}px`,this.measureContainer.style.position="absolute",this.measureContainer.style.left="-9999px",this.measureContainer.style.top="0",this.measureContainer.style.visibility="hidden";let t=null;this.sourceCSS&&(t=document.createElement("style"),t.textContent=this.sourceCSS,this.measureContainer.appendChild(t));const o=document.createElement("div");o.innerHTML=this.sourceHTML,this.measureContainer.appendChild(o);const r=this.layoutEngine.measureBlocks(o);return this.paginationResult=this.layoutEngine.paginate(r),this.measureContainer.innerHTML="",this.paginationCallbacks.forEach(a=>a(this.paginationResult)),{result:this.paginationResult,measurements:r}}rePaginate(){if(!this.contentContainer)return this.paginationResult;const e=this.layoutEngine.measureBlocks(this.contentContainer);return this.paginationResult=this.layoutEngine.paginate(e),this.paginationCallbacks.forEach(t=>t(this.paginationResult)),this.paginationResult}attachEditing(e){this.contentContainer=e,this.editableManager.attach(e),this.editableManager.onChange(()=>{this.sourceHTML=e.innerHTML,this.changeCallbacks.forEach(t=>t(this.sourceHTML))})}setPageConfig(e){this.layoutEngine.setConfig(e)}onPagination(e){return this.paginationCallbacks.add(e),()=>{this.paginationCallbacks.delete(e)}}onChange(e){return this.changeCallbacks.add(e),()=>{this.changeCallbacks.delete(e)}}getHTML(){return this.contentContainer?this.contentContainer.innerHTML:this.sourceHTML}getPlainText(){return this.contentContainer&&(this.contentContainer.innerText||this.contentContainer.textContent)||""}destroy(){this.editableManager.detach(),this.paginationCallbacks.clear(),this.changeCallbacks.clear(),this.measureContainer=null,this.contentContainer=null}}class Rt{_html="";_css="";_pageConfig=null;_paginationResult={pages:[],pageCount:0};_changeCallbacks=new Set;_loadCallbacks=new Set;_pageConfigCallbacks=new Set;_getHTMLFn=null;_getPlainTextFn=null;loadHTML(e,t){this._html=e,this._css=t||"",this._loadCallbacks.forEach(o=>o(e,t))}getHTML(){return this._getHTMLFn?this._getHTMLFn():this._html}getPlainText(){if(this._getPlainTextFn)return this._getPlainTextFn();const e=document.createElement("div");return e.innerHTML=this._html,e.innerText||e.textContent||""}onChange(e){return this._changeCallbacks.add(e),()=>{this._changeCallbacks.delete(e)}}onLoad(e){return this._loadCallbacks.add(e),()=>{this._loadCallbacks.delete(e)}}onPageConfigChange(e){return this._pageConfigCallbacks.add(e),()=>{this._pageConfigCallbacks.delete(e)}}getPageCount(){return this._paginationResult.pageCount}getPageConfig(){return this._pageConfig}setPageConfig(e){this._pageConfig&&(this._pageConfig={...this._pageConfig,...e,margins:{...this._pageConfig.margins,...e.margins||{}}},this._pageConfigCallbacks.forEach(t=>t(this._pageConfig)))}querySelectorAll(e){const t=document.createElement("div");return t.innerHTML=this.getHTML(),Array.from(t.querySelectorAll(e))}getElementContent(e){const t=document.createElement("div");t.innerHTML=this.getHTML();const o=t.querySelector(`#${e}`);return o?o.innerHTML:null}setElementContent(e,t){const o=document.createElement("div");o.innerHTML=this.getHTML();const r=o.querySelector(`#${e}`);r&&(r.innerHTML=t,this.loadHTML(o.innerHTML,this._css))}_connectGetHTML(e){this._getHTMLFn=e}_connectGetPlainText(e){this._getPlainTextFn=e}_notifyChange(e){this._html=e,this._changeCallbacks.forEach(t=>t(e))}_updatePagination(e){this._paginationResult=e}_updatePageConfig(e){this._pageConfig=e}}function At(s){const[e,t]=l.useState("none");return l.useEffect(()=>s?s.onContextChange(r=>{t(r)}):void 0,[s]),e}const Bt={bold:!1,italic:!1,underline:!1,strikethrough:!1,justifyLeft:!1,justifyCenter:!1,justifyRight:!1,justifyFull:!1,orderedList:!1,unorderedList:!1,superscript:!1,subscript:!1,fontName:"",fontSize:"3",foreColor:"#000000",backColor:"",formatBlock:"p"};function Ne(){const[s,e]=l.useState(Bt),t=l.useCallback(()=>{try{let o=document.queryCommandValue("formatBlock")||"";o=o.replace(/^<|>$/g,"").toLowerCase(),(!o||o==="div")&&(o="p"),e({bold:document.queryCommandState("bold"),italic:document.queryCommandState("italic"),underline:document.queryCommandState("underline"),strikethrough:document.queryCommandState("strikethrough"),justifyLeft:document.queryCommandState("justifyLeft"),justifyCenter:document.queryCommandState("justifyCenter"),justifyRight:document.queryCommandState("justifyRight"),justifyFull:document.queryCommandState("justifyFull"),orderedList:document.queryCommandState("insertOrderedList"),unorderedList:document.queryCommandState("insertUnorderedList"),superscript:document.queryCommandState("superscript"),subscript:document.queryCommandState("subscript"),fontName:document.queryCommandValue("fontName")||"",fontSize:document.queryCommandValue("fontSize")||"3",foreColor:document.queryCommandValue("foreColor")||"#000000",backColor:document.queryCommandValue("backColor")||"",formatBlock:o})}catch{}},[]);return l.useEffect(()=>(document.addEventListener("selectionchange",t),()=>{document.removeEventListener("selectionchange",t)}),[t]),s}function qe(){const s=l.useRef(null),e=l.useCallback(()=>{const r=window.getSelection();r&&r.rangeCount>0&&(s.current=r.getRangeAt(0).cloneRange())},[]),t=l.useCallback(()=>{const r=s.current;if(!r)return;const a=window.getSelection();a&&(a.removeAllRanges(),a.addRange(r))},[]),o=l.useCallback(r=>{t(),r()},[t]);return{saveSelection:e,restoreSelection:t,restoreAndExec:o}}const ze=({onExecCommand:s})=>{const e=Ne(),{saveSelection:t,restoreAndExec:o}=qe(),r=l.useCallback((p,b)=>C=>{C.preventDefault(),s(p,b)},[s]),a=l.useCallback(p=>b=>{o(()=>s(p,b.target.value))},[s,o]),i=l.useCallback(p=>b=>{o(()=>s(p,b.target.value))},[s,o]),u=l.useCallback(p=>{p.preventDefault();const b=prompt("Enter URL:");b&&s("createLink",b)},[s]);return n.jsxs("div",{style:Pt,children:[n.jsxs("select",{value:e.formatBlock,onChange:a("formatBlock"),style:ve,title:"Block Format",onMouseDown:t,children:[n.jsx("option",{value:"p",children:"Paragraph"}),n.jsx("option",{value:"h1",children:"Heading 1"}),n.jsx("option",{value:"h2",children:"Heading 2"}),n.jsx("option",{value:"h3",children:"Heading 3"}),n.jsx("option",{value:"h4",children:"Heading 4"}),n.jsx("option",{value:"h5",children:"Heading 5"}),n.jsx("option",{value:"h6",children:"Heading 6"})]}),n.jsx(re,{}),n.jsxs("select",{value:e.fontName.replace(/['"]/g,""),onChange:a("fontName"),style:{...ve,width:110},title:"Font Family",onMouseDown:t,children:[n.jsx("option",{value:"Arial",children:"Arial"}),n.jsx("option",{value:"Georgia",children:"Georgia"}),n.jsx("option",{value:"Times New Roman",children:"Times New Roman"}),n.jsx("option",{value:"Courier New",children:"Courier New"}),n.jsx("option",{value:"Verdana",children:"Verdana"}),n.jsx("option",{value:"Trebuchet MS",children:"Trebuchet MS"}),n.jsx("option",{value:"Comic Sans MS",children:"Comic Sans MS"}),n.jsx("option",{value:"Impact",children:"Impact"}),n.jsx("option",{value:"system-ui",children:"System UI"})]}),n.jsxs("select",{value:e.fontSize,onChange:a("fontSize"),style:{...ve,width:56},title:"Font Size",onMouseDown:t,children:[n.jsx("option",{value:"1",children:"8"}),n.jsx("option",{value:"2",children:"10"}),n.jsx("option",{value:"3",children:"12"}),n.jsx("option",{value:"4",children:"14"}),n.jsx("option",{value:"5",children:"18"}),n.jsx("option",{value:"6",children:"24"}),n.jsx("option",{value:"7",children:"36"})]}),n.jsx(re,{}),n.jsx(A,{icon:"B",title:"Bold (Ctrl+B)",active:e.bold,onMouseDown:r("bold"),extraStyle:{fontWeight:700}}),n.jsx(A,{icon:"I",title:"Italic (Ctrl+I)",active:e.italic,onMouseDown:r("italic"),extraStyle:{fontStyle:"italic"}}),n.jsx(A,{icon:"U",title:"Underline (Ctrl+U)",active:e.underline,onMouseDown:r("underline"),extraStyle:{textDecoration:"underline"}}),n.jsx(A,{icon:"S",title:"Strikethrough",active:e.strikethrough,onMouseDown:r("strikethrough"),extraStyle:{textDecoration:"line-through"}}),n.jsx(A,{icon:"x²",title:"Superscript",active:e.superscript,onMouseDown:r("superscript")}),n.jsx(A,{icon:"x₂",title:"Subscript",active:e.subscript,onMouseDown:r("subscript")}),n.jsx(re,{}),n.jsx(Ee,{label:"A",title:"Text Color",defaultValue:"#000000",onChange:i("foreColor"),onMouseDown:t}),n.jsx(Ee,{label:"A",title:"Highlight Color",defaultValue:"#ffff00",onChange:i("hiliteColor"),onMouseDown:t,highlight:!0}),n.jsx(re,{}),n.jsx(A,{icon:"≡",title:"Align Left",active:e.justifyLeft,onMouseDown:r("justifyLeft")}),n.jsx(A,{icon:"≣",title:"Align Center",active:e.justifyCenter,onMouseDown:r("justifyCenter")}),n.jsx(A,{icon:"≢",title:"Align Right",active:e.justifyRight,onMouseDown:r("justifyRight")}),n.jsx(A,{icon:"☰",title:"Justify",active:e.justifyFull,onMouseDown:r("justifyFull")}),n.jsx(re,{}),n.jsx(A,{icon:"•",title:"Bullet List",active:e.unorderedList,onMouseDown:r("insertUnorderedList")}),n.jsx(A,{icon:"1.",title:"Numbered List",active:e.orderedList,onMouseDown:r("insertOrderedList"),extraStyle:{fontSize:"11px",fontWeight:600}}),n.jsx(A,{icon:"⇤",title:"Decrease Indent",onMouseDown:r("outdent")}),n.jsx(A,{icon:"⇥",title:"Increase Indent",onMouseDown:r("indent")}),n.jsx(re,{}),n.jsx(A,{icon:"🔗",title:"Insert Link",onMouseDown:u}),n.jsx(A,{icon:"—",title:"Horizontal Rule",onMouseDown:r("insertHorizontalRule")}),n.jsx(A,{icon:"T̸",title:"Clear Formatting",onMouseDown:r("removeFormat")})]})},A=({icon:s,title:e,active:t,onMouseDown:o,extraStyle:r})=>n.jsx("button",{type:"button",title:e,onMouseDown:o,style:{width:"28px",height:"28px",borderWidth:"1px",borderStyle:"solid",borderColor:t?"#b0b5bd":"transparent",borderRadius:"3px",backgroundColor:t?"#d0d5dd":"transparent",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",fontSize:"13px",color:"#333",padding:0,fontFamily:"inherit",...r},dangerouslySetInnerHTML:{__html:s}}),re=()=>n.jsx("div",{style:Dt}),Ee=({label:s,title:e,defaultValue:t,onChange:o,onMouseDown:r,highlight:a})=>n.jsxs("label",{style:_t,title:e,onMouseDown:r,children:[a?n.jsx("span",{style:{backgroundColor:"#ffff00",padding:"0 2px"},children:s}):s,n.jsx("input",{type:"color",defaultValue:t,onChange:o,style:It})]}),Pt={display:"flex",alignItems:"center",gap:"2px",flexWrap:"wrap"},ve={height:"28px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"12px",padding:"0 4px",cursor:"pointer",backgroundColor:"#fff"},Dt={width:"1px",height:"20px",backgroundColor:"#ddd",margin:"0 4px"},_t={position:"relative",width:"28px",height:"28px",display:"flex",alignItems:"center",justifyContent:"center",cursor:"pointer",fontSize:"13px",fontWeight:"bold"},It={position:"absolute",bottom:0,left:0,width:"100%",height:"4px",padding:0,borderWidth:0,borderStyle:"none",cursor:"pointer"},We=({pageConfig:s,pageCount:e,onPageConfigChange:t})=>{const o=l.useCallback(i=>{const u=i.target.value;t({size:u})},[t]),r=l.useCallback(i=>u=>{const p=Math.max(0,parseInt(u.target.value)||0);t({margins:{...s.margins,[i]:p}})},[s.margins,t]),a=typeof s.size=="string"?s.size:"custom";return n.jsxs("div",{style:Ft,children:[n.jsxs("label",{style:Nt,children:["Page:",n.jsxs("select",{value:a,onChange:o,style:qt,children:[n.jsx("option",{value:"letter",children:"Letter (8.5 x 11)"}),n.jsx("option",{value:"a4",children:"A4 (210 x 297mm)"}),n.jsx("option",{value:"legal",children:"Legal (8.5 x 14)"})]})]}),n.jsx("div",{style:Re}),n.jsx("span",{style:{fontSize:"12px",color:"#666"},children:"Margins (px):"}),n.jsx(be,{label:"T",value:s.margins.top,onChange:r("top")}),n.jsx(be,{label:"R",value:s.margins.right,onChange:r("right")}),n.jsx(be,{label:"B",value:s.margins.bottom,onChange:r("bottom")}),n.jsx(be,{label:"L",value:s.margins.left,onChange:r("left")}),n.jsx("div",{style:Re}),n.jsxs("span",{style:{fontSize:"12px",color:"#666"},children:[e," ",e===1?"page":"pages"]})]})},be=({label:s,value:e,onChange:t})=>n.jsxs("label",{style:zt,title:`${s} margin`,children:[s,":",n.jsx("input",{type:"number",value:e,onChange:t,style:Wt,min:0,max:300,step:12})]}),Ft={display:"flex",alignItems:"center",gap:"6px",flexWrap:"wrap"},Nt={display:"flex",alignItems:"center",gap:"4px",fontSize:"12px",color:"#666"},qt={height:"26px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"12px",padding:"0 4px",cursor:"pointer",backgroundColor:"#fff"},Re={width:"1px",height:"20px",backgroundColor:"#ddd",margin:"0 4px"},zt={display:"flex",alignItems:"center",gap:"2px",fontSize:"11px",color:"#666"},Wt={width:"44px",height:"24px",borderWidth:"1px",borderStyle:"solid",borderColor:"#ccc",borderRadius:"3px",fontSize:"11px",textAlign:"center",padding:"0 2px"},$t=({pageConfig:s,pageCount:e,onExecCommand:t,onPageConfigChange:o})=>n.jsxs("div",{style:Ot,children:[n.jsx("div",{style:Ae,children:n.jsx(ze,{onExecCommand:t})}),n.jsx("div",{style:Ae,children:n.jsx(We,{pageConfig:s,pageCount:e,onPageConfigChange:o})})]}),Ot={borderBottomWidth:"1px",borderBottomStyle:"solid",borderBottomColor:"#d0d0d0",backgroundColor:"#f8f8f8",padding:"4px 8px",display:"flex",flexDirection:"column",gap:"4px",flexShrink:0,zIndex:10},Ae={display:"flex",alignItems:"center",gap:"4px",minHeight:"32px"};function Vt(s={}){const{initialHTML:e="",initialCSS:t="",initialConfig:o=he}=s,r=l.useRef(new Fe(o)),[a,i]=l.useState({pages:[{blockIndices:[]}],pageCount:1}),[u,p]=l.useState(o),b=l.useCallback((j,L)=>{r.current.loadHTML(j,L)},[]),C=l.useCallback(j=>{r.current.setPageConfig(j),p(r.current.getPageConfig())},[]),k=l.useCallback(()=>{const{result:j}=r.current.runPagination();i(j)},[]),T=l.useCallback(()=>r.current.getHTML(),[]),M=l.useCallback(()=>r.current.getPlainText(),[]);return l.useEffect(()=>{e&&r.current.loadHTML(e,t)},[e,t]),l.useEffect(()=>r.current.onPagination(L=>{i(L)}),[]),l.useEffect(()=>()=>{r.current.destroy()},[]),{engine:r.current,paginationResult:a,pageConfig:u,loadHTML:b,setPageConfig:C,triggerPagination:k,getHTML:T,getPlainText:M}}exports.DEFAULT_MARGINS=Pe;exports.DEFAULT_PAGE_CONFIG=he;exports.DocumentAPI=Rt;exports.DocumentEngine=Fe;exports.DopeCanvas=wt;exports.EditableManager=se;exports.PAGE_SIZE_PRESETS=Te;exports.PageLayoutEngine=Me;exports.PageSetupToolbar=We;exports.TextToolbar=ze;exports.Toolbar=$t;exports.recombineSplitBlocks=_e;exports.trySplitBlock=de;exports.useDocumentEngine=Vt;exports.useFormattingState=Ne;exports.useSelectionContext=At;exports.useSelectionSaver=qe;
55
55
  //# sourceMappingURL=dopecanvas.cjs.map