@superdoc-dev/template-builder 0.2.0-next.8 → 0.2.0-next.9

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 +1 @@
1
- {"version":3,"file":"FieldList.d.ts","sourceRoot":"","sources":["../../src/defaults/FieldList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAwJ9C,CAAC"}
1
+ {"version":3,"file":"FieldList.d.ts","sourceRoot":"","sources":["../../src/defaults/FieldList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,UAAU,CAAC;AA0H9D,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAsJ9C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"FieldMenu.d.ts","sourceRoot":"","sources":["../../src/defaults/FieldMenu.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,UAAU,CAAC;AAEhE,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CAiY9C,CAAC"}
1
+ {"version":3,"file":"FieldMenu.d.ts","sourceRoot":"","sources":["../../src/defaults/FieldMenu.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAmB,cAAc,EAAE,MAAM,UAAU,CAAC;AAEhE,eAAO,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CA+b9C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,KAAK,KAAK,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAElD,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;AA0GhC,QAAA,MAAM,uBAAuB,oJAwkB3B,CAAC;AAIH,eAAe,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,KAAK,KAAK,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAElD,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;AA4GhC,QAAA,MAAM,uBAAuB,oJAwnB3B,CAAC;AAIH,eAAe,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var Te=Object.create;var pe=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var Ee=Object.getOwnPropertyNames;var Ie=Object.getPrototypeOf,De=Object.prototype.hasOwnProperty;var Ne=(o,a,c,d)=>{if(a&&typeof a=="object"||typeof a=="function")for(let n of Ee(a))!De.call(o,n)&&n!==c&&pe(o,n,{get:()=>a[n],enumerable:!(d=Re(a,n))||d.enumerable});return o};var Be=(o,a,c)=>(c=o!=null?Te(Ie(o)):{},Ne(a||!o||!o.__esModule?pe(c,"default",{value:o,enumerable:!0}):c,o));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("react/jsx-runtime"),s=require("react"),me=({isVisible:o,position:a,availableFields:c,filteredFields:d,filterQuery:n,allowCreate:m,onSelect:N,onClose:O,onCreateField:z})=>{const[_,M]=s.useState(!1),[w,b]=s.useState(""),[V,j]=s.useState("inline");s.useEffect(()=>{o||(M(!1),b(""),j("inline"))},[o]);const K=s.useMemo(()=>({position:"absolute",left:a?.left,top:a?.top,zIndex:1e3,background:"white",border:"1px solid #ddd",borderRadius:"4px",boxShadow:"0 2px 8px rgba(0,0,0,0.1)",padding:"8px 0",minWidth:"200px"}),[a]),q=d??c,A=!!n,f=s.useMemo(()=>{const i=[],g=new Map;return q.forEach(v=>{const h=v.category?.trim()||"Uncategorized",T=g.get(h);if(T!==void 0){i[T].fields.push(v);return}g.set(h,i.length),i.push({category:h,fields:[v]})}),i},[q]),[B,S]=s.useState({});s.useEffect(()=>{S(i=>{if(f.length===0)return Object.keys(i).length===0?i:{};const g={};let v=Object.keys(i).length!==f.length;return f.forEach(({category:h},T)=>{const R=A?!0:i[h]??T===0;g[h]=R,!v&&i[h]!==R&&(v=!0)}),v?g:i})},[f,A]);const U=s.useCallback(i=>{S(g=>({...g,[i]:!g[i]}))},[]);if(!o)return null;const L=async()=>{const i=w.trim();if(!i)return;const g={id:`custom_${Date.now()}`,label:i,category:"Custom",metadata:{mode:V}};try{if(z){const v=await z(g);N(v||g)}else N(g)}finally{M(!1),b(""),j("inline")}};return t.jsxs("div",{className:"superdoc-field-menu",style:K,children:[A&&t.jsx("div",{style:{padding:"8px 16px",borderBottom:"1px solid #f0f0f0",marginBottom:"4px"},children:t.jsxs("div",{style:{fontSize:"12px",color:"#6b7280"},children:["Filtering results for",t.jsx("span",{style:{fontWeight:600,color:"#111827",marginLeft:"4px"},children:n})]})}),m&&!_&&t.jsx("div",{className:"field-menu-item",onClick:()=>M(!0),style:{padding:"8px 16px",cursor:"pointer",color:"#0066cc",fontWeight:500},children:"+ Create New Field"}),m&&_&&t.jsxs("div",{style:{padding:"8px 16px"},children:[t.jsx("input",{type:"text",value:w,placeholder:"Field name...",onChange:i=>b(i.target.value),onKeyDown:i=>{i.key==="Enter"&&L(),i.key==="Escape"&&(M(!1),b(""),j("inline"))},autoFocus:!0,style:{width:"100%",padding:"4px 8px",border:"1px solid #ddd",borderRadius:"3px"}}),t.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"12px",fontSize:"13px"},children:[t.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[t.jsx("input",{type:"radio",value:"inline",checked:V==="inline",onChange:()=>j("inline")}),"Inline"]}),t.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[t.jsx("input",{type:"radio",value:"block",checked:V==="block",onChange:()=>j("block")}),"Block"]})]}),t.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"8px"},children:[t.jsx("button",{onClick:L,disabled:!w.trim(),style:{padding:"4px 12px",background:w.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:w.trim()?"pointer":"not-allowed"},children:"Create"}),t.jsx("button",{onClick:()=>{M(!1),b(""),j("inline")},style:{padding:"4px 12px",background:"white",border:"1px solid #ddd",borderRadius:"3px",cursor:"pointer"},children:"Cancel"})]})]}),m&&c.length>0&&t.jsx("div",{style:{borderTop:"1px solid #eee",margin:"4px 0"}}),f.length===0?t.jsx("div",{style:{padding:"16px",fontSize:"13px",color:"#6b7280",textAlign:"center"},children:"No matching fields"}):f.map(({category:i,fields:g},v)=>{const h=!!B[i],T=`${Math.max(g.length*40,0)}px`;return t.jsxs("div",{style:{borderTop:v===0&&m?void 0:"1px solid #f0f0f0"},children:[t.jsxs("button",{type:"button",onClick:()=>U(i),style:{width:"100%",display:"flex",alignItems:"center",justifyContent:"space-between",padding:"8px 16px",background:"transparent",border:"none",cursor:"pointer",fontWeight:500,textAlign:"left"},children:[t.jsxs("span",{children:[i," (",g.length,")"]}),t.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:h?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease",marginLeft:"12px"}})]}),t.jsx("div",{"data-category":i,"aria-hidden":!h,style:{overflow:"hidden",maxHeight:h?T:"0px",opacity:h?1:0,transition:"max-height 0.2s ease, opacity 0.2s ease",pointerEvents:h?"auto":"none"},children:t.jsx("div",{style:{padding:h?"4px 0":0},children:g.map(R=>t.jsx("div",{className:"field-menu-item",onClick:()=>N(R),style:{padding:"8px 16px",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"space-between"},children:t.jsx("span",{style:{fontWeight:500},children:R.label})},R.id))})})]},i)}),t.jsx("div",{style:{borderTop:"1px solid #eee",marginTop:"4px"},children:t.jsx("button",{onClick:O,style:{width:"100%",padding:"6px 16px",background:"#f3f4f6",border:"none",borderRadius:"0 0 4px 4px",cursor:"pointer"},children:"Close"})})]})},ge=({fields:o,onSelect:a,onDelete:c,selectedFieldId:d})=>t.jsxs("div",{className:"superdoc-field-list",style:{width:"250px",background:"white",border:"1px solid #e5e7eb",borderRadius:"8px",padding:"16px"},children:[t.jsxs("h3",{style:{margin:"0 0 16px 0",fontSize:"16px",fontWeight:"600"},children:["Template Fields (",o.length,")"]}),o.length===0?t.jsxs("div",{style:{color:"#9ca3af",fontSize:"14px",textAlign:"center",padding:"20px 0"},children:["No fields yet. Type ","{{"," to add a field."]}):t.jsx("div",{style:{display:"flex",flexDirection:"column",gap:"8px"},children:o.map(n=>t.jsxs("div",{onClick:()=>a(n),style:{position:"relative",padding:"12px",background:d===n.id?"#eff6ff":"#f9fafb",border:d===n.id?"1px solid #3b82f6":"1px solid #e5e7eb",borderRadius:"6px",cursor:"pointer",transition:"all 0.2s"},onMouseEnter:m=>{d!==n.id&&(m.currentTarget.style.background="#f3f4f6")},onMouseLeave:m=>{d!==n.id&&(m.currentTarget.style.background="#f9fafb")},title:n.alias,children:[t.jsx("button",{onClick:m=>{m.stopPropagation(),c(n.id)},style:{position:"absolute",top:"8px",right:"8px",padding:"4px",background:"transparent",border:"none",cursor:"pointer",color:"#9ca3af",transition:"color 0.2s",display:"flex",alignItems:"center",justifyContent:"center"},onMouseEnter:m=>{m.currentTarget.style.color="#ef4444"},onMouseLeave:m=>{m.currentTarget.style.color="#9ca3af"},title:"Delete field",children:t.jsx("svg",{width:"16",height:"16",viewBox:"0 0 16 16",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:t.jsx("path",{d:"M6 2V1.5C6 1.22386 6.22386 1 6.5 1H9.5C9.77614 1 10 1.22386 10 1.5V2M2 4H14M12.6667 4L12.1991 11.0129C12.129 12.065 12.0939 12.5911 11.8667 12.99C11.6666 13.3412 11.3648 13.6235 11.0011 13.7998C10.588 14 10.0607 14 9.00623 14H6.99377C5.93927 14 5.41202 14 4.99889 13.7998C4.63517 13.6235 4.33339 13.3412 4.13332 12.99C3.90607 12.5911 3.871 12.065 3.80086 11.0129L3.33333 4",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})}),t.jsxs("div",{style:{paddingRight:"24px"},children:[t.jsx("div",{style:{fontWeight:"500",fontSize:"14px",marginBottom:n.alias&&n.alias!==n.id?"4px":"0"},children:n.id}),t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"6px",fontSize:"12px",color:"#4b5563"},children:[n.alias&&n.alias!==n.id&&t.jsx("span",{children:n.alias}),n.mode&&t.jsx("span",{style:{fontSize:"10px",padding:"2px 6px",borderRadius:"4px",background:n.mode==="block"?"#dbeafe":"#f3f4f6",color:n.mode==="block"?"#1e40af":"#4b5563",fontWeight:"500"},children:n.mode})]})]})]},n.id))})]}),oe=o=>{const a=o.helpers?.structuredContentCommands;return a?.getStructuredContentTags?(a.getStructuredContentTags(o.state)||[]).map(d=>{const n=d?.node??d,m=n?.attrs??{},O=(n?.type?.name||"").includes("Block")?"block":"inline";return{id:m.id,alias:m.alias||m.label||"",tag:m.tag,mode:O}}):[]},xe=(o,a)=>{if(o===a)return!0;if(o.length!==a.length)return!1;for(let c=0;c<o.length;c+=1){const d=o[c],n=a[c];if(!n||d.id!==n.id||d.alias!==n.alias||d.tag!==n.tag||d.position!==n.position||d.mode!==n.mode)return!1}return!0},Le=o=>{if(!o)return null;if(o===!0)return{selector:"#superdoc-toolbar",config:{},renderDefaultContainer:!0};if(typeof o=="string")return{selector:o,config:{},renderDefaultContainer:!1};const{selector:a,...c}=o;return{selector:a||"#superdoc-toolbar",config:c,renderDefaultContainer:a===void 0}},Q=10,He=250,Pe=300,fe=o=>{const a=window.innerWidth-He-Q,c=window.innerHeight-Pe-Q,d=Math.min(o.left,a),n=Math.min(o.top,c);return new DOMRect(Math.max(d,Q),Math.max(n,Q),o.width,o.height)},he=s.forwardRef((o,a)=>{const{document:c,fields:d={},menu:n={},list:m={},toolbar:N,onReady:O,onTrigger:z,onFieldInsert:_,onFieldUpdate:M,onFieldDelete:w,onFieldsChange:b,onFieldSelect:V,onFieldCreate:j,className:K,style:q,documentHeight:A="600px"}=o,[f,B]=s.useState(d.initial||[]),[S,U]=s.useState(null),[L,i]=s.useState(!1),[g,v]=s.useState(),[h,T]=s.useState(""),[R,be]=s.useState(()=>d.available||[]),X=s.useRef(null),y=s.useRef(null),E=s.useRef(null),se=s.useRef(d);se.current=d;const I=s.useRef(null),ie=s.useRef(L);s.useEffect(()=>{ie.current=L},[L]);const $=n.trigger||"{{",Y=se.current.available||[],le=s.useCallback(e=>{const r=e.trim().toLowerCase();return r?Y.filter(l=>{const u=l.label.toLowerCase(),F=l.category?.toLowerCase()||"";return u.includes(r)||F.includes(r)}):Y},[Y]),Z=s.useCallback(e=>{T(e),be(le(e))},[le]),H=s.useCallback(()=>{Z("")},[Z]),G=s.useCallback((e,r)=>{if(!y.current?.activeEditor)return!1;const l=y.current.activeEditor,u=f,F=e==="inline"?l.commands.insertStructuredContentInline?.({attrs:{alias:r.alias,tag:r.metadata?JSON.stringify(r.metadata):r.category},text:r.defaultValue||r.alias}):l.commands.insertStructuredContentBlock?.({attrs:{alias:r.alias,tag:r.metadata?JSON.stringify(r.metadata):r.category},text:r.defaultValue||r.alias});if(F){const C=oe(l);B(C),b?.(C);const p=C.find(x=>!u.some(D=>D.id===x.id));p&&_?.(p)}return F},[_,b,f]),ee=s.useCallback((e,r)=>{if(!y.current?.activeEditor)return!1;const u=y.current.activeEditor.commands.updateStructuredContentById?.(e,{attrs:r});return u&&B(F=>{const C=F.map(x=>x.id===e?{...x,...r}:x);b?.(C);const p=C.find(x=>x.id===e);return p&&M?.(p),C}),u},[M,b]),te=s.useCallback(e=>{const r=y.current?.activeEditor;if(!r){console.warn("[SuperDocTemplateBuilder] deleteField called without active editor");let p=!1;return B(x=>{if(!x.some(k=>k.id===e))return x;const D=x.filter(k=>k.id!==e);return p=!0,b?.(D),D}),p&&(w?.(e),U(x=>x===e?null:x)),p}let l=!1;try{l=r.commands.deleteStructuredContentById?.(e)??!1}catch(p){console.error("[SuperDocTemplateBuilder] Delete command failed:",p)}let u=oe(r);const F=u.some(p=>p.id===e);!l&&F&&(u=u.filter(p=>p.id!==e));let C=!1;return B(p=>{if(xe(p,u))return p;const x=p.some(k=>k.id===e),D=u.some(k=>k.id===e);return x&&!D&&(C=!0),b?.(u),u}),C&&(w?.(e),U(p=>p===e?null:p)),l||C},[w,b]),P=s.useCallback(e=>{if(!y.current?.activeEditor)return;y.current.activeEditor.commands.selectStructuredContentById?.(e),U(e);const l=f.find(u=>u.id===e);l&&V?.(l)},[f,V]),ne=s.useCallback(e=>{if(!e)return;const r=oe(e);B(l=>xe(l,r)?l:(b?.(r),r))},[b]);s.useEffect(()=>X.current?((async()=>{const{SuperDoc:r}=await import("superdoc"),l={selector:X.current,document:c?.source,documentMode:c?.mode||"editing",onReady:()=>{if(u.activeEditor){const F=u.activeEditor;F.on("update",({editor:C})=>{const{state:p}=C,{from:x}=p.selection;if(x>=$.length){const re=x-$.length;if(p.doc.textBetween(re,x)===$){const de=C.view.coordsAtPos(x),ce=fe(new DOMRect(de.left,de.top,0,0)),ue=()=>{const J=y.current?.activeEditor;if(!J)return;const Se=J.state.selection.from,Me=J.state.tr.delete(re,Se);J.view.dispatch(Me)};E.current=ue,I.current=x,v(ce),i(!0),H(),z?.({position:{from:re,to:x},bounds:ce,cleanup:ue});return}}if(!ie.current)return;if(I.current==null){i(!1),H();return}if(x<I.current){i(!1),I.current=null,H();return}const D=p.doc.textBetween(I.current,x);Z(D);const k=C.view.coordsAtPos(x),we=fe(new DOMRect(k.left,k.top,0,0));v(we)}),F.on("update",()=>{ne(F)}),ne(F)}O?.()}},u=new r({selector:X.current,documentMode:c?.mode||"editing",...l,...W&&{toolbar:W.selector,modules:{toolbar:{selector:W.selector,toolbarGroups:W.config.toolbarGroups||["center"],excludeItems:W.config.excludeItems||[],...W.config}}}});y.current=u})(),()=>{E.current=null,I.current=null;const r=y.current;r&&typeof r.destroy=="function"&&r.destroy(),y.current=null}):void 0,[c?.source,c?.mode,$,ne,O,z,N]);const ye=s.useCallback(async e=>{E.current&&(E.current(),E.current=null),I.current=null,H();const r=e.metadata?.mode||"inline";if(e.id.startsWith("custom_")&&j)try{const l=await j(e);if(l){const u=l.metadata?.mode||r;G(u,{alias:l.label,category:l.category,metadata:l.metadata,defaultValue:l.defaultValue}),i(!1);return}}catch(l){console.error("Field creation failed:",l)}G(r,{alias:e.label,category:e.category,metadata:e.metadata,defaultValue:e.defaultValue}),i(!1)},[G,j,H]),Ce=s.useCallback(()=>{i(!1),I.current=null,H(),E.current&&(E.current(),E.current=null)},[H]),ve=s.useCallback(()=>{if(!y.current?.activeEditor||f.length===0)return;const e=f.findIndex(l=>l.id===S),r=e>=0?(e+1)%f.length:0;P(f[r].id)},[f,S,P]),Fe=s.useCallback(()=>{if(!y.current?.activeEditor||f.length===0)return;const e=f.findIndex(l=>l.id===S),r=e>0?e-1:f.length-1;P(f[r].id)},[f,S,P]),je=s.useCallback(async e=>{const{fileName:r="document",triggerDownload:l=!0}=e||{};try{return await y.current?.export({exportType:["docx"],exportedName:r,triggerDownload:l})}catch(u){throw console.error("Failed to export DOCX",u),u}},[]);s.useImperativeHandle(a,()=>({insertField:e=>G("inline",e),insertBlockField:e=>G("block",e),updateField:ee,deleteField:te,selectField:P,nextField:ve,previousField:Fe,getFields:()=>f,exportTemplate:je,getSuperDoc:()=>y.current}));const ke=n.component||me,ae=m.component||ge,W=Le(N);return t.jsxs("div",{className:`superdoc-template-builder ${K||""}`,style:q,children:[t.jsxs("div",{style:{display:"flex",gap:"20px"},children:[m.position==="left"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ae,{fields:f,onSelect:e=>P(e.id),onDelete:te,onUpdate:e=>ee(e.id,e),selectedFieldId:S||void 0})}),t.jsxs("div",{className:"superdoc-template-builder-document",style:{flex:1},children:[W?.renderDefaultContainer&&t.jsx("div",{id:"superdoc-toolbar",className:"superdoc-template-builder-toolbar","data-testid":"template-builder-toolbar"}),t.jsx("div",{ref:X,className:"superdoc-template-builder-editor",style:{height:A},"data-testid":"template-builder-editor"})]}),m.position==="right"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ae,{fields:f,onSelect:e=>P(e.id),onDelete:te,onUpdate:e=>ee(e.id,e),selectedFieldId:S||void 0})})]}),t.jsx(ke,{isVisible:L,position:g,availableFields:d.available||[],filteredFields:R,filterQuery:h,allowCreate:d.allowCreate||!1,onSelect:ye,onClose:Ce,onCreateField:j})]})});he.displayName="SuperDocTemplateBuilder";exports.FieldList=ge;exports.FieldMenu=me;exports.default=he;
1
+ "use strict";var Ie=Object.create;var fe=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var De=Object.getOwnPropertyNames;var Ne=Object.getPrototypeOf,ze=Object.prototype.hasOwnProperty;var Be=(n,o,d,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let l of De(o))!ze.call(n,l)&&l!==d&&fe(n,l,{get:()=>o[l],enumerable:!(i=Re(o,l))||i.enumerable});return n};var We=(n,o,d)=>(d=n!=null?Ie(Ne(n)):{},Be(o||!n||!n.__esModule?fe(d,"default",{value:n,enumerable:!0}):d,n));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react/jsx-runtime"),s=require("react"),be=({isVisible:n,position:o,availableFields:d,filteredFields:i,filterQuery:l,allowCreate:c,onSelect:D,onClose:W,onCreateField:A,existingFields:p=[],onSelectExisting:S})=>{const[m,y]=s.useState(!1),[C,I]=s.useState(""),[_,L]=s.useState("inline"),[$,f]=s.useState(!0),[R,N]=s.useState(!0);s.useEffect(()=>{n||(y(!1),I(""),L("inline"))},[n]);const U=s.useMemo(()=>({position:"absolute",left:o?.left,top:o?.top,zIndex:1e3,background:"white",border:"1px solid #ddd",borderRadius:"4px",boxShadow:"0 2px 8px rgba(0,0,0,0.1)",padding:"8px 0",width:"280px"}),[o]),H=i??d,T=!!l;if(s.useEffect(()=>{T&&N(!0)},[T]),!n)return null;const Y=async()=>{const h=C.trim();if(!h)return;const P={id:`custom_${Date.now()}`,label:h,metadata:{mode:_}};try{if(A){const b=await A(P);D(b||P)}else D(P)}finally{y(!1),I(""),L("inline")}};return e.jsxs("div",{className:"superdoc-field-menu",style:U,children:[T&&e.jsx("div",{style:{padding:"8px 16px",borderBottom:"1px solid #f0f0f0",marginBottom:"4px"},children:e.jsxs("div",{style:{fontSize:"12px",color:"#6b7280"},children:["Filtering results for",e.jsx("span",{style:{fontWeight:600,color:"#111827",marginLeft:"4px"},children:l})]})}),c&&!m&&e.jsx("div",{className:"field-menu-item",onClick:()=>y(!0),style:{padding:"8px 16px",cursor:"pointer",color:"#0066cc",fontWeight:500},children:"+ Create New Field"}),c&&m&&e.jsxs("div",{style:{padding:"8px 16px"},children:[e.jsx("input",{type:"text",value:C,placeholder:"Field name...",onChange:h=>I(h.target.value),onKeyDown:h=>{h.key==="Enter"&&Y(),h.key==="Escape"&&(y(!1),I(""),L("inline"))},autoFocus:!0,style:{width:"100%",padding:"4px 8px",border:"1px solid #ddd",borderRadius:"3px"}}),e.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"12px",fontSize:"13px"},children:[e.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[e.jsx("input",{type:"radio",value:"inline",checked:_==="inline",onChange:()=>L("inline")}),"Inline"]}),e.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[e.jsx("input",{type:"radio",value:"block",checked:_==="block",onChange:()=>L("block")}),"Block"]})]}),e.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"8px"},children:[e.jsx("button",{onClick:Y,disabled:!C.trim(),style:{padding:"4px 12px",background:C.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:C.trim()?"pointer":"not-allowed"},children:"Create"}),e.jsx("button",{onClick:()=>{y(!1),I(""),L("inline")},style:{padding:"4px 12px",background:"white",border:"1px solid #ddd",borderRadius:"3px",cursor:"pointer"},children:"Cancel"})]})]}),c&&d.length>0&&e.jsx("div",{style:{borderTop:"1px solid #eee",margin:"4px 0"}}),p.length>0&&(()=>{const h=new Map;p.forEach(b=>{const q=b.group||`individual-${b.id}`,K=h.get(q)||[];K.push(b),h.set(q,K)});const P=Array.from(h.values()).map(b=>({...b[0],count:b.length}));return e.jsxs("div",{style:{borderBottom:"1px solid #f0f0f0"},children:[e.jsxs("button",{type:"button",onClick:()=>f(!$),style:{width:"100%",display:"flex",alignItems:"center",justifyContent:"space-between",padding:"8px 16px",background:"transparent",border:"none",cursor:"pointer",fontWeight:500,fontSize:"13px",color:"#374151",textAlign:"left"},children:[e.jsxs("span",{children:["Existing Fields (",P.length,")"]}),e.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:$?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),$&&e.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:P.map(b=>e.jsxs("div",{className:"field-menu-item",onClick:()=>S?.(b),style:{padding:"8px 16px",cursor:"pointer",display:"flex",alignItems:"flex-start",justifyContent:"space-between",gap:"8px"},children:[e.jsxs("div",{style:{flex:1,minWidth:0},children:[e.jsx("div",{style:{fontWeight:500,fontSize:"13px"},children:b.alias||b.id}),e.jsx("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:b.group?`group (${b.count} fields)`:`ID: ${b.id}`})]}),e.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:b.mode||"inline"})]},b.group||b.id))})]})})(),H.length===0?e.jsx("div",{style:{padding:"16px",fontSize:"13px",color:"#6b7280",textAlign:"center"},children:"No matching fields"}):e.jsxs("div",{style:{borderBottom:"1px solid #f0f0f0"},children:[e.jsxs("button",{type:"button",onClick:()=>N(!R),style:{width:"100%",display:"flex",alignItems:"center",justifyContent:"space-between",padding:"8px 16px",background:"transparent",border:"none",cursor:"pointer",fontWeight:500,fontSize:"13px",color:"#374151",textAlign:"left"},children:[e.jsxs("span",{children:["Available Fields (",H.length,")"]}),e.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:R?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),R&&e.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:H.map(h=>e.jsxs("div",{className:"field-menu-item",onClick:()=>D(h),style:{padding:"8px 16px",cursor:"pointer",display:"flex",alignItems:"flex-start",justifyContent:"space-between",gap:"8px"},children:[e.jsxs("div",{style:{flex:1,minWidth:0},children:[e.jsx("div",{style:{fontWeight:500,fontSize:"13px"},children:h.label||h.id}),e.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["ID: ",h.id]})]}),e.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:h.metadata?.mode||"inline"})]},h.id))})]}),e.jsx("div",{style:{borderTop:"1px solid #eee",marginTop:"4px"},children:e.jsx("button",{onClick:W,style:{width:"100%",padding:"6px 16px",background:"#f3f4f6",border:"none",borderRadius:"0 0 4px 4px",cursor:"pointer"},children:"Close"})})]})},Le=n=>{const o=n.split("-");return o.length>2?o[o.length-1].substring(0,6):n.substring(0,6)},ge=({field:n,onSelect:o,onDelete:d,isSelected:i,isGrouped:l=!1})=>e.jsxs("div",{onClick:()=>o(n),style:{position:"relative",padding:"10px 12px",background:i?"#eff6ff":"#f9fafb",border:i?"1px solid #3b82f6":"1px solid #e5e7eb",borderRadius:"6px",cursor:"pointer",transition:"all 0.2s",fontSize:l?"13px":"14px"},onMouseEnter:c=>{i||(c.currentTarget.style.background="#f3f4f6")},onMouseLeave:c=>{i||(c.currentTarget.style.background="#f9fafb")},title:n.alias,children:[e.jsx("button",{onClick:c=>{c.stopPropagation(),d(n.id)},style:{position:"absolute",top:"6px",right:"6px",padding:"4px",background:"transparent",border:"none",cursor:"pointer",color:"#9ca3af",transition:"color 0.2s",display:"flex",alignItems:"center",justifyContent:"center"},onMouseEnter:c=>{c.currentTarget.style.color="#ef4444"},onMouseLeave:c=>{c.currentTarget.style.color="#9ca3af"},title:"Delete field",children:e.jsx("svg",{width:"14",height:"14",viewBox:"0 0 16 16",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:e.jsx("path",{d:"M6 2V1.5C6 1.22386 6.22386 1 6.5 1H9.5C9.77614 1 10 1.22386 10 1.5V2M2 4H14M12.6667 4L12.1991 11.0129C12.129 12.065 12.0939 12.5911 11.8667 12.99C11.6666 13.3412 11.3648 13.6235 11.0011 13.7998C10.588 14 10.0607 14 9.00623 14H6.99377C5.93927 14 5.41202 14 4.99889 13.7998C4.63517 13.6235 4.33339 13.3412 4.13332 12.99C3.90607 12.5911 3.871 12.065 3.80086 11.0129L3.33333 4",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsxs("div",{style:{paddingRight:"24px"},children:[e.jsx("div",{style:{fontWeight:"500",fontSize:l?"12px":"14px",color:l?"#6b7280":"#111827"},children:n.alias||n.id}),e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"6px",fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:[e.jsxs("span",{children:["ID: ",n.id]}),n.mode&&e.jsx("span",{style:{fontSize:"9px",padding:"2px 5px",borderRadius:"3px",background:n.mode==="block"?"#dbeafe":"#f3f4f6",color:n.mode==="block"?"#1e40af":"#4b5563",fontWeight:"500"},children:n.mode})]})]})]}),ye=({fields:n,onSelect:o,onDelete:d,selectedFieldId:i})=>{const[l,c]=s.useState(new Set),{groupedFields:D,ungroupedFields:W}=s.useMemo(()=>{const p={},S=[];return n.forEach(m=>{m.group?(p[m.group]||(p[m.group]=[]),p[m.group].push(m)):S.push(m)}),{groupedFields:p,ungroupedFields:S}},[n]),A=p=>{c(S=>{const m=new Set(S);return m.has(p)?m.delete(p):m.add(p),m})};return e.jsxs("div",{className:"superdoc-field-list",style:{width:"250px",background:"white",border:"1px solid #e5e7eb",borderRadius:"8px",padding:"16px"},children:[e.jsxs("h3",{style:{margin:"0 0 16px 0",fontSize:"16px",fontWeight:"600"},children:["Template Fields (",n.length,")"]}),n.length===0?e.jsxs("div",{style:{color:"#9ca3af",fontSize:"14px",textAlign:"center",padding:"20px 0"},children:["No fields yet. Type ","{{"," to add a field."]}):e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:"8px"},children:[W.map(p=>e.jsx(ge,{field:p,onSelect:o,onDelete:d,isSelected:i===p.id},p.id)),Object.entries(D).map(([p,S])=>{const m=l.has(p),y=S[0];return e.jsxs("div",{children:[e.jsx("div",{style:{position:"relative",padding:"12px",background:"#f9fafb",border:"1px solid #e5e7eb",borderRadius:"6px",cursor:"pointer",transition:"all 0.2s"},onClick:()=>A(p),onMouseEnter:C=>{C.currentTarget.style.background="#f3f4f6"},onMouseLeave:C=>{C.currentTarget.style.background="#f9fafb"},children:e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px"},children:[e.jsx("span",{style:{fontSize:"12px",color:"#6b7280"},children:m?"▼":"▶"}),e.jsxs("div",{style:{flex:1},children:[e.jsx("div",{style:{fontWeight:"500",fontSize:"14px"},children:y.alias}),e.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["group: ",Le(p)," (",S.length," fields)"]})]})]})}),m&&e.jsx("div",{style:{marginLeft:"16px",marginTop:"4px",display:"flex",flexDirection:"column",gap:"4px"},children:S.map(C=>e.jsx(ge,{field:C,onSelect:o,onDelete:d,isSelected:i===C.id,isGrouped:!0},C.id))})]},p)})]})]})},X=n=>{const o=n.helpers?.structuredContentCommands;return o?.getStructuredContentTags?(o.getStructuredContentTags(n.state)||[]).map(i=>{const l=i?.node??i,c=l?.attrs??{},W=(l?.type?.name||"").includes("Block")?"block":"inline";return{id:c.id,alias:c.alias||c.label||"",tag:c.tag,mode:W,group:o.getGroup?.(c.tag)??void 0}}):[]},me=(n,o)=>{if(n===o)return!0;if(n.length!==o.length)return!1;for(let d=0;d<n.length;d+=1){const i=n[d],l=o[d];if(!l||i.id!==l.id||i.alias!==l.alias||i.tag!==l.tag||i.position!==l.position||i.mode!==l.mode||i.group!==l.group)return!1}return!0},He=n=>{if(!n)return null;if(n===!0)return{selector:"#superdoc-toolbar",config:{},renderDefaultContainer:!0};if(typeof n=="string")return{selector:n,config:{},renderDefaultContainer:!1};const{selector:o,...d}=n;return{selector:o||"#superdoc-toolbar",config:d,renderDefaultContainer:o===void 0}},ne=10,Pe=250,Ae=300,he=n=>{const o=window.innerWidth-Pe-ne,d=window.innerHeight-Ae-ne,i=Math.min(n.left,o),l=Math.min(n.top,d);return new DOMRect(Math.max(i,ne),Math.max(l,ne),n.width,n.height)},ve=s.forwardRef((n,o)=>{const{document:d,fields:i={},menu:l={},list:c={},toolbar:D,onReady:W,onTrigger:A,onFieldInsert:p,onFieldUpdate:S,onFieldDelete:m,onFieldsChange:y,onFieldSelect:C,onFieldCreate:I,className:_,style:L,documentHeight:$="600px"}=n,[f,R]=s.useState(i.initial||[]),[N,U]=s.useState(null),[H,T]=s.useState(!1),[Y,h]=s.useState(),[P,b]=s.useState(""),[q,K]=s.useState(()=>i.available||[]),Z=s.useRef(null),k=s.useRef(null),E=s.useRef(null),le=s.useRef(i);le.current=i;const z=s.useRef(null),ae=s.useRef(H);s.useEffect(()=>{ae.current=H},[H]);const ee=l.trigger||"{{",re=le.current.available||[],de=s.useCallback(t=>{const r=t.trim().toLowerCase();return r?re.filter(a=>a.label.toLowerCase().includes(r)):re},[re]),se=s.useCallback(t=>{b(t),K(de(t))},[de]),B=s.useCallback(()=>{se("")},[se]),J=s.useCallback((t,r)=>{if(!k.current?.activeEditor)return!1;const a=k.current.activeEditor,v=f,j=t==="inline"?a.commands.insertStructuredContentInline?.({attrs:{alias:r.alias,tag:r.metadata?JSON.stringify(r.metadata):void 0},text:r.defaultValue||r.alias}):a.commands.insertStructuredContentBlock?.({attrs:{alias:r.alias,tag:r.metadata?JSON.stringify(r.metadata):void 0},text:r.defaultValue||r.alias});if(j){const g=X(a);R(g),y?.(g);const w=g.find(u=>!v.some(x=>x.id===u.id));w&&p?.(w)}return j},[p,y,f]),Q=s.useCallback((t,r)=>{if(!k.current?.activeEditor)return!1;const v=k.current.activeEditor.commands.updateStructuredContentById?.(t,{attrs:r});return v&&R(j=>{const g=j.map(u=>u.id===t?{...u,...r}:u);y?.(g);const w=g.find(u=>u.id===t);return w&&S?.(w),g}),v},[S,y]),oe=s.useCallback(t=>{const r=k.current?.activeEditor;if(!r){let x=!1;return R(F=>{if(!F.some(M=>M.id===t))return F;const G=F.filter(M=>M.id!==t);return x=!0,y?.(G),G}),x&&(m?.(t),U(F=>F===t?null:F)),x}const v=f.find(x=>x.id===t)?.group;let j=!1;try{j=r.commands.deleteStructuredContentById?.(t)??!1}catch{j=!1}let g=X(r);const w=g.some(x=>x.id===t);if(!j&&w&&(g=g.filter(x=>x.id!==t)),v){const x=g.filter(F=>F.group===v);if(x.length===1){const F=x[0];r.commands.updateStructuredContentById?.(F.id,{attrs:{tag:void 0}}),g=X(r)}}let u=!1;return R(x=>{if(me(x,g))return x;const F=x.some(M=>M.id===t),G=g.some(M=>M.id===t);return F&&!G&&(u=!0),y?.(g),g}),u&&(m?.(t),U(x=>x===t?null:x)),j||u},[m,y,f]),V=s.useCallback(t=>{if(!k.current?.activeEditor)return;k.current.activeEditor.commands.selectStructuredContentById?.(t),U(t);const a=f.find(v=>v.id===t);a&&C?.(a)},[f,C]),ie=s.useCallback(t=>{if(!t)return;const r=X(t);R(a=>me(a,r)?a:(y?.(r),r))},[y]);s.useEffect(()=>Z.current?((async()=>{const{SuperDoc:r}=await import("superdoc"),a={selector:Z.current,document:d?.source,documentMode:d?.mode||"editing",onReady:()=>{if(v.activeEditor){const j=v.activeEditor;j.on("update",({editor:g})=>{const{state:w}=g,{from:u}=w.selection;if(u>=ee.length){const M=u-ee.length;if(w.doc.textBetween(M,u)===ee){const ue=g.view.coordsAtPos(u),pe=he(new DOMRect(ue.left,ue.top,0,0)),xe=()=>{const te=k.current?.activeEditor;if(!te)return;const Ee=te.state.selection.from,Me=te.state.tr.delete(M,Ee);te.view.dispatch(Me)};E.current=xe,z.current=u,h(pe),T(!0),B(),A?.({position:{from:M,to:u},bounds:pe,cleanup:xe});return}}if(!ae.current)return;if(z.current==null){T(!1),B();return}if(u<z.current){T(!1),z.current=null,B();return}const x=w.doc.textBetween(z.current,u);se(x);const F=g.view.coordsAtPos(u),G=he(new DOMRect(F.left,F.top,0,0));h(G)}),j.on("update",()=>{ie(j)}),ie(j)}W?.()}},v=new r({selector:Z.current,documentMode:d?.mode||"editing",...a,...O&&{toolbar:O.selector,modules:{toolbar:{selector:O.selector,toolbarGroups:O.config.toolbarGroups||["center"],excludeItems:O.config.excludeItems||[],...O.config}}}});k.current=v})(),()=>{E.current=null,z.current=null;const r=k.current;r&&typeof r.destroy=="function"&&r.destroy(),k.current=null}):void 0,[d?.source,d?.mode,ee,ie,W,A,D]);const je=s.useCallback(async t=>{E.current&&(E.current(),E.current=null),z.current=null,B();const r=t.metadata?.mode||"inline";if(t.id.startsWith("custom_")&&I){const a=await I(t);if(a){const v=a.metadata?.mode||r;J(v,{alias:a.label,metadata:a.metadata,defaultValue:a.defaultValue}),T(!1);return}}J(r,{alias:t.label,metadata:t.metadata,defaultValue:t.defaultValue}),T(!1)},[J,I,B]),Ce=s.useCallback(t=>{E.current&&(E.current(),E.current=null),z.current=null,B();const r=k.current?.activeEditor;if(!r)return;const a=r.helpers?.structuredContentCommands;if(!a)return;const v=t.group||`group-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,j=a.createTagObject?.({group:v});if((t.mode||"inline")==="inline"?r.commands.insertStructuredContentInline?.({attrs:{alias:t.alias,tag:j},text:t.alias}):r.commands.insertStructuredContentBlock?.({attrs:{alias:t.alias,tag:j},text:t.alias})){t.group||Q(t.id,{tag:j}),T(!1);const u=X(r);R(u),y?.(u)}},[Q,B,y]),ke=s.useCallback(()=>{T(!1),z.current=null,B(),E.current&&(E.current(),E.current=null)},[B]),Fe=s.useCallback(()=>{if(!k.current?.activeEditor||f.length===0)return;const t=f.findIndex(a=>a.id===N),r=t>=0?(t+1)%f.length:0;V(f[r].id)},[f,N,V]),Se=s.useCallback(()=>{if(!k.current?.activeEditor||f.length===0)return;const t=f.findIndex(a=>a.id===N),r=t>0?t-1:f.length-1;V(f[r].id)},[f,N,V]),we=s.useCallback(async t=>{const{fileName:r="document",triggerDownload:a=!0}=t||{};return await k.current?.export({exportType:["docx"],exportedName:r,triggerDownload:a})},[]);s.useImperativeHandle(o,()=>({insertField:t=>J("inline",t),insertBlockField:t=>J("block",t),updateField:Q,deleteField:oe,selectField:V,nextField:Fe,previousField:Se,getFields:()=>f,exportTemplate:we,getSuperDoc:()=>k.current}));const Te=l.component||be,ce=c.component||ye,O=He(D);return e.jsxs("div",{className:`superdoc-template-builder ${_||""}`,style:L,children:[e.jsxs("div",{style:{display:"flex",gap:"20px"},children:[c.position==="left"&&e.jsx("div",{className:"superdoc-template-builder-sidebar",children:e.jsx(ce,{fields:f,onSelect:t=>V(t.id),onDelete:oe,onUpdate:t=>Q(t.id,t),selectedFieldId:N||void 0})}),e.jsxs("div",{className:"superdoc-template-builder-document",style:{flex:1},children:[O?.renderDefaultContainer&&e.jsx("div",{id:"superdoc-toolbar",className:"superdoc-template-builder-toolbar","data-testid":"template-builder-toolbar"}),e.jsx("div",{ref:Z,className:"superdoc-template-builder-editor",style:{height:$},"data-testid":"template-builder-editor"})]}),c.position==="right"&&e.jsx("div",{className:"superdoc-template-builder-sidebar",children:e.jsx(ce,{fields:f,onSelect:t=>V(t.id),onDelete:oe,onUpdate:t=>Q(t.id,t),selectedFieldId:N||void 0})})]}),e.jsx(Te,{isVisible:H,position:Y,availableFields:i.available||[],filteredFields:q,filterQuery:P,allowCreate:i.allowCreate||!1,onSelect:je,onClose:ke,onCreateField:I,existingFields:f,onSelectExisting:Ce})]})});ve.displayName="SuperDocTemplateBuilder";exports.FieldList=ye;exports.FieldMenu=be;exports.default=ve;