@superdoc-dev/template-builder 0.5.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,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;AAoGhC,QAAA,MAAM,uBAAuB,oJAolB3B,CAAC;AAIH,eAAe,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAUA,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;AAoGhC,QAAA,MAAM,uBAAuB,oJAymB3B,CAAC;AAIH,eAAe,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var De=Object.create;var ge=Object.defineProperty;var Ne=Object.getOwnPropertyDescriptor;var ze=Object.getOwnPropertyNames;var Be=Object.getPrototypeOf,We=Object.prototype.hasOwnProperty;var Le=(n,o,c,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let d of ze(o))!We.call(n,d)&&d!==c&&ge(n,d,{get:()=>o[d],enumerable:!(i=Ne(o,d))||i.enumerable});return n};var He=(n,o,c)=>(c=n!=null?De(Be(n)):{},Le(o||!n||!n.__esModule?ge(c,"default",{value:n,enumerable:!0}):c,n));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react/jsx-runtime"),r=require("react"),ye=({isVisible:n,position:o,availableFields:c,filteredFields:i,filterQuery:d,allowCreate:u,onSelect:I,onClose:W,onCreateField:A,existingFields:f=[],onSelectExisting:S})=>{const[g,y]=r.useState(!1),[k,E]=r.useState(""),[$,L]=r.useState("inline"),[q,te]=r.useState(!0),[x,R]=r.useState(!0);r.useEffect(()=>{n||(y(!1),E(""),L("inline"))},[n]);const H=r.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]),V=i??c,P=!!d;if(r.useEffect(()=>{P&&R(!0)},[P]),!n)return null;const M=async()=>{const m=k.trim();if(!m)return;const D={id:`custom_${Date.now()}`,label:m,mode:$};try{if(A){const h=await A(D);I(h||D)}else I(D)}finally{y(!1),E(""),L("inline")}};return e.jsxs("div",{className:"superdoc-field-menu",style:H,children:[P&&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:d})]})}),u&&!g&&e.jsx("div",{className:"field-menu-item",onClick:()=>y(!0),style:{padding:"8px 16px",cursor:"pointer",color:"#0066cc",fontWeight:500},children:"+ Create New Field"}),u&&g&&e.jsxs("div",{style:{padding:"8px 16px"},children:[e.jsx("input",{type:"text",value:k,placeholder:"Field name...",onChange:m=>E(m.target.value),onKeyDown:m=>{m.key==="Enter"&&M(),m.key==="Escape"&&(y(!1),E(""),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:M,disabled:!k.trim(),style:{padding:"4px 12px",background:k.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:k.trim()?"pointer":"not-allowed"},children:"Create"}),e.jsx("button",{onClick:()=>{y(!1),E(""),L("inline")},style:{padding:"4px 12px",background:"white",border:"1px solid #ddd",borderRadius:"3px",cursor:"pointer"},children:"Cancel"})]})]}),u&&c.length>0&&e.jsx("div",{style:{borderTop:"1px solid #eee",margin:"4px 0"}}),f.length>0&&(()=>{const m=new Map;f.forEach(h=>{const J=h.group||`individual-${h.id}`,Y=m.get(J)||[];Y.push(h),m.set(J,Y)});const D=Array.from(m.values()).map(h=>({...h[0],count:h.length}));return e.jsxs("div",{style:{borderBottom:"1px solid #f0f0f0"},children:[e.jsxs("button",{type:"button",onClick:()=>te(!q),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 (",D.length,")"]}),e.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:q?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),q&&e.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:D.map(h=>e.jsxs("div",{className:"field-menu-item",onClick:()=>S?.(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.alias||h.id}),e.jsx("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:h.group?`group (${h.count} fields)`:`ID: ${h.id}`})]}),e.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:h.mode||"inline"})]},h.group||h.id))})]})})(),V.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:()=>R(!x),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 (",V.length,")"]}),e.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:x?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),x&&e.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:V.map(m=>e.jsxs("div",{className:"field-menu-item",onClick:()=>I(m),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:m.label||m.id}),e.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["ID: ",m.id]})]}),e.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:m.mode||"inline"})]},m.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"})})]})},Pe=n=>{const o=n.split("-");return o.length>2?o[o.length-1].substring(0,6):n.substring(0,6)},me=({field:n,onSelect:o,onDelete:c,isSelected:i,isGrouped:d=!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:d?"13px":"14px"},onMouseEnter:u=>{i||(u.currentTarget.style.background="#f3f4f6")},onMouseLeave:u=>{i||(u.currentTarget.style.background="#f9fafb")},title:n.alias,children:[e.jsx("button",{onClick:u=>{u.stopPropagation(),c(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:u=>{u.currentTarget.style.color="#ef4444"},onMouseLeave:u=>{u.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:d?"12px":"14px",color:d?"#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})]})]})]}),ve=({fields:n,onSelect:o,onDelete:c,selectedFieldId:i})=>{const[d,u]=r.useState(new Set),{groupedFields:I,ungroupedFields:W}=r.useMemo(()=>{const f={},S=[];return n.forEach(g=>{g.group?(f[g.group]||(f[g.group]=[]),f[g.group].push(g)):S.push(g)}),{groupedFields:f,ungroupedFields:S}},[n]),A=f=>{u(S=>{const g=new Set(S);return g.has(f)?g.delete(f):g.add(f),g})};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(f=>e.jsx(me,{field:f,onSelect:o,onDelete:c,isSelected:i===f.id},f.id)),Object.entries(I).map(([f,S])=>{const g=d.has(f),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(f),onMouseEnter:k=>{k.currentTarget.style.background="#f3f4f6"},onMouseLeave:k=>{k.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:g?"▼":"▶"}),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: ",Pe(f)," (",S.length," fields)"]})]})]})}),g&&e.jsx("div",{style:{marginLeft:"16px",marginTop:"4px",display:"flex",flexDirection:"column",gap:"4px"},children:S.map(k=>e.jsx(me,{field:k,onSelect:o,onDelete:c,isSelected:i===k.id,isGrouped:!0},k.id))})]},f)})]})]})},U=n=>{const o=n.helpers?.structuredContentCommands;return o?.getStructuredContentTags?(o.getStructuredContentTags(n.state)||[]).map(i=>{const d=i?.node??i,u=d?.attrs??{},W=(d?.type?.name||"").includes("Block")?"block":"inline";return{id:u.id,alias:u.alias||u.label||"",tag:u.tag,mode:W,group:o.getGroup?.(u.tag)??void 0}}):[]},he=(n,o)=>{if(n===o)return!0;if(n.length!==o.length)return!1;for(let c=0;c<n.length;c+=1){const i=n[c],d=o[c];if(!d||i.id!==d.id||i.alias!==d.alias||i.tag!==d.tag||i.position!==d.position||i.mode!==d.mode||i.group!==d.group)return!1}return!0},Ae=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,...c}=n;return{selector:o||"#superdoc-toolbar",config:c,renderDefaultContainer:o===void 0}},ee=10,Ve=250,Oe=300,be=n=>{const o=window.innerWidth-Ve-ee,c=window.innerHeight-Oe-ee,i=Math.min(n.left,o),d=Math.min(n.top,c);return new DOMRect(Math.max(i,ee),Math.max(d,ee),n.width,n.height)},je=r.forwardRef((n,o)=>{const{document:c,fields:i={},menu:d={},list:u={},toolbar:I,onReady:W,onTrigger:A,onFieldInsert:f,onFieldUpdate:S,onFieldDelete:g,onFieldsChange:y,onFieldSelect:k,onFieldCreate:E,onExport:$,className:L,style:q,documentHeight:te="600px"}=n,[x,R]=r.useState(i.initial||[]),[H,V]=r.useState(null),[P,M]=r.useState(!1),[m,D]=r.useState(),[h,J]=r.useState(""),[Y,Ce]=r.useState(()=>i.available||[]),ne=r.useRef(null),C=r.useRef(null),T=r.useRef(null),ae=r.useRef(i);ae.current=i;const N=r.useRef(null),de=r.useRef(P);r.useEffect(()=>{de.current=P},[P]);const K=d.trigger||"{{",se=ae.current.available||[],ce=r.useCallback(t=>{const s=t.trim().toLowerCase();return s?se.filter(l=>l.label.toLowerCase().includes(s)):se},[se]),re=r.useCallback(t=>{J(t),Ce(ce(t))},[ce]),z=r.useCallback(()=>{re("")},[re]),Q=r.useCallback((t,s)=>{if(!C.current?.activeEditor)return!1;const l=C.current.activeEditor,v=x,b=t==="inline"?l.commands.insertStructuredContentInline?.({attrs:{alias:s.alias,tag:s.metadata?JSON.stringify(s.metadata):void 0},text:s.defaultValue||s.alias}):l.commands.insertStructuredContentBlock?.({attrs:{alias:s.alias,tag:s.metadata?JSON.stringify(s.metadata):void 0},text:s.defaultValue||s.alias});if(b){const p=U(l);R(p),y?.(p);const w=p.find(j=>!v.some(a=>a.id===j.id));w&&f?.(w)}return b},[f,y,x]),X=r.useCallback((t,s)=>{if(!C.current?.activeEditor)return!1;const v=C.current.activeEditor.commands.updateStructuredContentById?.(t,{attrs:s});return v&&R(b=>{const p=b.map(j=>j.id===t?{...j,...s}:j);y?.(p);const w=p.find(j=>j.id===t);return w&&S?.(w),p}),v},[S,y]),oe=r.useCallback(t=>{const s=C.current?.activeEditor;if(!s){let a=!1;return R(F=>{if(!F.some(B=>B.id===t))return F;const _=F.filter(B=>B.id!==t);return a=!0,y?.(_),_}),a&&(g?.(t),V(F=>F===t?null:F)),a}const v=x.find(a=>a.id===t)?.group;let b=!1;try{b=s.commands.deleteStructuredContentById?.(t)??!1}catch{b=!1}let p=U(s);const w=p.some(a=>a.id===t);if(!b&&w&&(p=p.filter(a=>a.id!==t)),v){const a=p.filter(F=>F.group===v);if(a.length===1){const F=a[0];s.commands.updateStructuredContentById?.(F.id,{attrs:{tag:void 0}}),p=U(s)}}let j=!1;return R(a=>{if(he(a,p))return a;const F=a.some(B=>B.id===t),_=p.some(B=>B.id===t);return F&&!_&&(j=!0),y?.(p),p}),j&&(g?.(t),V(a=>a===t?null:a)),b||j},[g,y,x]),O=r.useCallback(t=>{if(!C.current?.activeEditor)return;C.current.activeEditor.commands.selectStructuredContentById?.(t),V(t);const l=x.find(v=>v.id===t);l&&k?.(l)},[x,k]),ie=r.useCallback(t=>{if(!t)return;const s=U(t);R(l=>he(l,s)?l:(y?.(s),s))},[y]);r.useEffect(()=>ne.current?((async()=>{const{SuperDoc:s}=await import("superdoc"),l={comments:!1,...G&&{toolbar:{selector:G.selector,toolbarGroups:G.config.toolbarGroups||["center"],excludeItems:G.config.excludeItems||[],...G.config}}},v=()=>{if(b.activeEditor){const p=b.activeEditor;p.on("update",({editor:w})=>{const{state:j}=w,{from:a}=j.selection;if(a>=K.length){const le=a-K.length;if(j.doc.textBetween(le,a)===K){const pe=w.view.coordsAtPos(a),xe=be(new DOMRect(pe.left,pe.top,0,0)),fe=()=>{const Z=C.current?.activeEditor;if(!Z)return;const Ie=Z.state.selection.from,Re=Z.state.tr.delete(le,Ie);Z.view.dispatch(Re)};T.current=fe,N.current=a,D(xe),M(!0),z(),A?.({position:{from:le,to:a},bounds:xe,cleanup:fe});return}}if(!de.current)return;if(N.current==null){M(!1),z();return}if(a<N.current){M(!1),N.current=null,z();return}const F=j.doc.textBetween(N.current,a);re(F);const _=w.view.coordsAtPos(a),B=be(new DOMRect(_.left,_.top,0,0));D(B)}),p.on("update",()=>{ie(p)}),ie(p)}W?.()},b=new s({selector:ne.current,document:c?.source,documentMode:c?.mode||"editing",modules:l,toolbar:G?.selector,onReady:v});C.current=b})(),()=>{T.current=null,N.current=null;const s=C.current;s&&typeof s.destroy=="function"&&s.destroy(),C.current=null}):void 0,[c?.source,c?.mode,K,ie,W,A,I]);const ke=r.useCallback(async t=>{T.current&&(T.current(),T.current=null),N.current=null,z();const s=t.mode||"inline";if(t.id.startsWith("custom_")&&E){const l=await E(t);if(l){const v=l.mode||s;Q(v,{alias:l.label,metadata:l.metadata,defaultValue:l.defaultValue}),M(!1);return}}Q(s,{alias:t.label,metadata:t.metadata,defaultValue:t.defaultValue}),M(!1)},[Q,E,z]),Fe=r.useCallback(t=>{T.current&&(T.current(),T.current=null),N.current=null,z();const s=C.current?.activeEditor;if(!s)return;const l=s.helpers?.structuredContentCommands;if(!l)return;const v=t.group||`group-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,b=l.createTagObject?.({group:v});if((t.mode||"inline")==="inline"?s.commands.insertStructuredContentInline?.({attrs:{alias:t.alias,tag:b},text:t.alias}):s.commands.insertStructuredContentBlock?.({attrs:{alias:t.alias,tag:b},text:t.alias})){t.group||X(t.id,{tag:b}),M(!1);const j=U(s);R(j),y?.(j)}},[X,z,y]),Se=r.useCallback(()=>{M(!1),N.current=null,z(),T.current&&(T.current(),T.current=null)},[z]),we=r.useCallback(()=>{if(!C.current?.activeEditor||x.length===0)return;const t=x.findIndex(l=>l.id===H),s=t>=0?(t+1)%x.length:0;O(x[s].id)},[x,H,O]),Te=r.useCallback(()=>{if(!C.current?.activeEditor||x.length===0)return;const t=x.findIndex(l=>l.id===H),s=t>0?t-1:x.length-1;O(x[s].id)},[x,H,O]),Ee=r.useCallback(async t=>{const{fileName:s="document",triggerDownload:l=!0}=t||{},v=await C.current?.export({exportType:["docx"],exportedName:s,triggerDownload:l}),b=C.current?.activeEditor;if(b){const p=U(b);$?.({fields:p,blob:l?void 0:v,fileName:s})}return v},[$]);r.useImperativeHandle(o,()=>({insertField:t=>Q("inline",t),insertBlockField:t=>Q("block",t),updateField:X,deleteField:oe,selectField:O,nextField:we,previousField:Te,getFields:()=>x,exportTemplate:Ee,getSuperDoc:()=>C.current}));const Me=d.component||ye,ue=u.component||ve,G=Ae(I);return e.jsxs("div",{className:`superdoc-template-builder ${L||""}`,style:q,children:[e.jsxs("div",{style:{display:"flex",gap:"20px"},children:[u.position==="left"&&e.jsx("div",{className:"superdoc-template-builder-sidebar",children:e.jsx(ue,{fields:x,onSelect:t=>O(t.id),onDelete:oe,onUpdate:t=>X(t.id,t),selectedFieldId:H||void 0})}),e.jsxs("div",{className:"superdoc-template-builder-document",style:{flex:1},children:[G?.renderDefaultContainer&&e.jsx("div",{id:"superdoc-toolbar",className:"superdoc-template-builder-toolbar","data-testid":"template-builder-toolbar"}),e.jsx("div",{ref:ne,className:"superdoc-template-builder-editor",style:{height:te},"data-testid":"template-builder-editor"})]}),u.position==="right"&&e.jsx("div",{className:"superdoc-template-builder-sidebar",children:e.jsx(ue,{fields:x,onSelect:t=>O(t.id),onDelete:oe,onUpdate:t=>X(t.id,t),selectedFieldId:H||void 0})})]}),e.jsx(Me,{isVisible:P,position:m,availableFields:i.available||[],filteredFields:Y,filterQuery:h,allowCreate:i.allowCreate||!1,onSelect:ke,onClose:Se,onCreateField:E,existingFields:x,onSelectExisting:Fe})]})});je.displayName="SuperDocTemplateBuilder";exports.FieldList=ve;exports.FieldMenu=ye;exports.default=je;
1
+ "use strict";var Ne=Object.create;var ge=Object.defineProperty;var ze=Object.getOwnPropertyDescriptor;var Be=Object.getOwnPropertyNames;var We=Object.getPrototypeOf,Le=Object.prototype.hasOwnProperty;var He=(n,o,d,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let l of Be(o))!Le.call(n,l)&&l!==d&&ge(n,l,{get:()=>o[l],enumerable:!(i=ze(o,l))||i.enumerable});return n};var Pe=(n,o,d)=>(d=n!=null?Ne(We(n)):{},He(o||!n||!n.__esModule?ge(d,"default",{value:n,enumerable:!0}):d,n));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("react/jsx-runtime"),s=require("react"),ye=({isVisible:n,position:o,availableFields:d,filteredFields:i,filterQuery:l,allowCreate:c,onSelect:R,onClose:W,onCreateField:V,existingFields:x=[],onSelectExisting:w})=>{const[m,y]=s.useState(!1),[k,E]=s.useState(""),[_,L]=s.useState("inline"),[q,te]=s.useState(!0),[p,D]=s.useState(!0);s.useEffect(()=>{n||(y(!1),E(""),L("inline"))},[n]);const H=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]),O=i??d,P=!!l;if(s.useEffect(()=>{P&&D(!0)},[P]),!n)return null;const M=async()=>{const h=k.trim();if(!h)return;const N={id:`custom_${Date.now()}`,label:h,mode:_};try{if(V){const b=await V(N);R(b||N)}else R(N)}finally{y(!1),E(""),L("inline")}};return t.jsxs("div",{className:"superdoc-field-menu",style:H,children:[P&&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:l})]})}),c&&!m&&t.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&&t.jsxs("div",{style:{padding:"8px 16px"},children:[t.jsx("input",{type:"text",value:k,placeholder:"Field name...",onChange:h=>E(h.target.value),onKeyDown:h=>{h.key==="Enter"&&M(),h.key==="Escape"&&(y(!1),E(""),L("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:_==="inline",onChange:()=>L("inline")}),"Inline"]}),t.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[t.jsx("input",{type:"radio",value:"block",checked:_==="block",onChange:()=>L("block")}),"Block"]})]}),t.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"8px"},children:[t.jsx("button",{onClick:M,disabled:!k.trim(),style:{padding:"4px 12px",background:k.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:k.trim()?"pointer":"not-allowed"},children:"Create"}),t.jsx("button",{onClick:()=>{y(!1),E(""),L("inline")},style:{padding:"4px 12px",background:"white",border:"1px solid #ddd",borderRadius:"3px",cursor:"pointer"},children:"Cancel"})]})]}),c&&d.length>0&&t.jsx("div",{style:{borderTop:"1px solid #eee",margin:"4px 0"}}),x.length>0&&(()=>{const h=new Map;x.forEach(b=>{const J=b.group||`individual-${b.id}`,Y=h.get(J)||[];Y.push(b),h.set(J,Y)});const N=Array.from(h.values()).map(b=>({...b[0],count:b.length}));return t.jsxs("div",{style:{borderBottom:"1px solid #f0f0f0"},children:[t.jsxs("button",{type:"button",onClick:()=>te(!q),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:[t.jsxs("span",{children:["Existing Fields (",N.length,")"]}),t.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:q?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),q&&t.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:N.map(b=>t.jsxs("div",{className:"field-menu-item",onClick:()=>w?.(b),style:{padding:"8px 16px",cursor:"pointer",display:"flex",alignItems:"flex-start",justifyContent:"space-between",gap:"8px"},children:[t.jsxs("div",{style:{flex:1,minWidth:0},children:[t.jsx("div",{style:{fontWeight:500,fontSize:"13px"},children:b.alias||b.id}),t.jsx("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:b.group?`group (${b.count} fields)`:`ID: ${b.id}`})]}),t.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))})]})})(),O.length===0?t.jsx("div",{style:{padding:"16px",fontSize:"13px",color:"#6b7280",textAlign:"center"},children:"No matching fields"}):t.jsxs("div",{style:{borderBottom:"1px solid #f0f0f0"},children:[t.jsxs("button",{type:"button",onClick:()=>D(!p),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:[t.jsxs("span",{children:["Available Fields (",O.length,")"]}),t.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:p?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),p&&t.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:O.map(h=>t.jsxs("div",{className:"field-menu-item",onClick:()=>R(h),style:{padding:"8px 16px",cursor:"pointer",display:"flex",alignItems:"flex-start",justifyContent:"space-between",gap:"8px"},children:[t.jsxs("div",{style:{flex:1,minWidth:0},children:[t.jsx("div",{style:{fontWeight:500,fontSize:"13px"},children:h.label||h.id}),t.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["ID: ",h.id]})]}),t.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:h.mode||"inline"})]},h.id))})]}),t.jsx("div",{style:{borderTop:"1px solid #eee",marginTop:"4px"},children:t.jsx("button",{onClick:W,style:{width:"100%",padding:"6px 16px",background:"#f3f4f6",border:"none",borderRadius:"0 0 4px 4px",cursor:"pointer"},children:"Close"})})]})},Ae=n=>{const o=n.split("-");return o.length>2?o[o.length-1].substring(0,6):n.substring(0,6)},me=({field:n,onSelect:o,onDelete:d,isSelected:i,isGrouped:l=!1})=>t.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:[t.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:t.jsx("svg",{width:"14",height:"14",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:l?"12px":"14px",color:l?"#6b7280":"#111827"},children:n.alias||n.id}),t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"6px",fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:[t.jsxs("span",{children:["ID: ",n.id]}),n.mode&&t.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})]})]})]}),ve=({fields:n,onSelect:o,onDelete:d,selectedFieldId:i})=>{const[l,c]=s.useState(new Set),{groupedFields:R,ungroupedFields:W}=s.useMemo(()=>{const x={},w=[];return n.forEach(m=>{m.group?(x[m.group]||(x[m.group]=[]),x[m.group].push(m)):w.push(m)}),{groupedFields:x,ungroupedFields:w}},[n]),V=x=>{c(w=>{const m=new Set(w);return m.has(x)?m.delete(x):m.add(x),m})};return 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 (",n.length,")"]}),n.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.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:"8px"},children:[W.map(x=>t.jsx(me,{field:x,onSelect:o,onDelete:d,isSelected:i===x.id},x.id)),Object.entries(R).map(([x,w])=>{const m=l.has(x),y=w[0];return t.jsxs("div",{children:[t.jsx("div",{style:{position:"relative",padding:"12px",background:"#f9fafb",border:"1px solid #e5e7eb",borderRadius:"6px",cursor:"pointer",transition:"all 0.2s"},onClick:()=>V(x),onMouseEnter:k=>{k.currentTarget.style.background="#f3f4f6"},onMouseLeave:k=>{k.currentTarget.style.background="#f9fafb"},children:t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px"},children:[t.jsx("span",{style:{fontSize:"12px",color:"#6b7280"},children:m?"▼":"▶"}),t.jsxs("div",{style:{flex:1},children:[t.jsx("div",{style:{fontWeight:"500",fontSize:"14px"},children:y.alias}),t.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["group: ",Ae(x)," (",w.length," fields)"]})]})]})}),m&&t.jsx("div",{style:{marginLeft:"16px",marginTop:"4px",display:"flex",flexDirection:"column",gap:"4px"},children:w.map(k=>t.jsx(me,{field:k,onSelect:o,onDelete:d,isSelected:i===k.id,isGrouped:!0},k.id))})]},x)})]})]})},U=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}}):[]},he=(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},Ve=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}},ee=10,Oe=250,Ge=300,be=n=>{const o=window.innerWidth-Oe-ee,d=window.innerHeight-Ge-ee,i=Math.min(n.left,o),l=Math.min(n.top,d);return new DOMRect(Math.max(i,ee),Math.max(l,ee),n.width,n.height)},je=s.forwardRef((n,o)=>{const{document:d,fields:i={},menu:l={},list:c={},toolbar:R,onReady:W,onTrigger:V,onFieldInsert:x,onFieldUpdate:w,onFieldDelete:m,onFieldsChange:y,onFieldSelect:k,onFieldCreate:E,onExport:_,className:L,style:q,documentHeight:te="600px"}=n,[p,D]=s.useState(i.initial||[]),[H,O]=s.useState(null),[P,M]=s.useState(!1),[h,N]=s.useState(),[b,J]=s.useState(""),[Y,Ce]=s.useState(()=>i.available||[]),ne=s.useRef(null),F=s.useRef(null),T=s.useRef(null),ae=s.useRef(i);ae.current=i;const z=s.useRef(null),de=s.useRef(P);s.useEffect(()=>{de.current=P},[P]);const K=l.trigger||"{{",re=ae.current.available||[],A=s.useMemo(()=>Ve(R),[R]),ce=s.useCallback(e=>{const r=e.trim().toLowerCase();return r?re.filter(a=>a.label.toLowerCase().includes(r)):re},[re]),se=s.useCallback(e=>{J(e),Ce(ce(e))},[ce]),B=s.useCallback(()=>{se("")},[se]),Q=s.useCallback((e,r)=>{if(!F.current?.activeEditor)return!1;const a=F.current.activeEditor,v=p,C=e==="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(C){const f=U(a);D(f),y?.(f);const S=f.find(j=>!v.some(u=>u.id===j.id));S&&x?.(S)}return C??!1},[x,y,p]),X=s.useCallback((e,r)=>{if(!F.current?.activeEditor)return!1;const v=F.current.activeEditor.commands.updateStructuredContentById?.(e,{attrs:r});return v&&D(C=>{const f=C.map(j=>j.id===e?{...j,...r}:j);y?.(f);const S=f.find(j=>j.id===e);return S&&w?.(S),f}),v??!1},[w,y]),oe=s.useCallback(e=>{const r=F.current?.activeEditor;if(!r){let u=!1;return D(g=>{if(!g.some(I=>I.id===e))return g;const $=g.filter(I=>I.id!==e);return u=!0,y?.($),m?.(e),$}),u&&O(g=>g===e?null:g),u}const v=p.find(u=>u.id===e)?.group;let C=!1;try{C=r.commands.deleteStructuredContentById?.(e)??!1}catch{C=!1}let f=U(r);const S=f.some(u=>u.id===e);if(!C&&S&&(f=f.filter(u=>u.id!==e)),v){const u=f.filter(g=>g.group===v);if(u.length===1){const g=u[0];r.commands.updateStructuredContentById?.(g.id,{attrs:{tag:void 0}}),f=U(r)}}let j=!1;return D(u=>{if(he(u,f))return u;const g=u.some(I=>I.id===e),$=f.some(I=>I.id===e);return g&&!$&&(j=!0),y?.(f),j&&m?.(e),f}),j&&O(u=>u===e?null:u),C||j},[m,y,p]),G=s.useCallback(e=>{if(!F.current?.activeEditor)return;F.current.activeEditor.commands.selectStructuredContentById?.(e),O(e);const a=p.find(v=>v.id===e);a&&k?.(a)},[p,k]),ie=s.useCallback(e=>{if(!e)return;const r=U(e);D(a=>he(a,r)?a:(y?.(r),r))},[y]);s.useEffect(()=>{if(!ne.current)return;let e=!1,r=null;return(async()=>{const{SuperDoc:v}=await import("superdoc");if(e)return;const C={comments:!1,...A&&{toolbar:{selector:A.selector,toolbarGroups:A.config.toolbarGroups||["center"],excludeItems:A.config.excludeItems||[],...A.config}}},f=()=>{if(!e){if(r?.activeEditor){const S=r.activeEditor;S.on("update",({editor:j})=>{const{state:u}=j,{from:g}=u.selection;if(g>=K.length){const le=g-K.length;if(u.doc.textBetween(le,g)===K){const pe=j.view.coordsAtPos(g),xe=be(new DOMRect(pe.left,pe.top,0,0)),fe=()=>{const Z=F.current?.activeEditor;if(!Z)return;const Re=Z.state.selection.from,De=Z.state.tr.delete(le,Re);Z.view.dispatch(De)};T.current=fe,z.current=g,N(xe),M(!0),B(),V?.({position:{from:le,to:g},bounds:xe,cleanup:fe});return}}if(!de.current)return;if(z.current==null){M(!1),B();return}if(g<z.current){M(!1),z.current=null,B();return}const $=u.doc.textBetween(z.current,g);se($);const I=j.view.coordsAtPos(g),Ie=be(new DOMRect(I.left,I.top,0,0));N(Ie)}),S.on("update",()=>{ie(S)}),ie(S)}W?.()}};r=new v({selector:ne.current,document:d?.source,documentMode:d?.mode||"editing",modules:C,toolbar:A?.selector,onReady:f}),F.current=r})(),()=>{e=!0,T.current=null,z.current=null,r&&typeof r.destroy=="function"&&r.destroy(),F.current=null}},[d?.source,d?.mode,K,ie,W,V,A]);const ke=s.useCallback(async e=>{T.current&&(T.current(),T.current=null),z.current=null,B();const r=e.mode||"inline";if(e.id.startsWith("custom_")&&E){const a=await E(e);if(a){const v=a.mode||r;Q(v,{alias:a.label,metadata:a.metadata,defaultValue:a.defaultValue}),M(!1);return}}Q(r,{alias:e.label,metadata:e.metadata,defaultValue:e.defaultValue}),M(!1)},[Q,E,B]),Fe=s.useCallback(e=>{T.current&&(T.current(),T.current=null),z.current=null,B();const r=F.current?.activeEditor;if(!r)return;const a=r.helpers?.structuredContentCommands;if(!a)return;const v=e.group||`group-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,C=a.createTagObject?.({group:v});if((e.mode||"inline")==="inline"?r.commands.insertStructuredContentInline?.({attrs:{alias:e.alias,tag:C},text:e.alias}):r.commands.insertStructuredContentBlock?.({attrs:{alias:e.alias,tag:C},text:e.alias})){e.group||X(e.id,{tag:C}),M(!1);const j=U(r);D(j),y?.(j)}},[X,B,y]),Se=s.useCallback(()=>{M(!1),z.current=null,B(),T.current&&(T.current(),T.current=null)},[B]),we=s.useCallback(()=>{if(!F.current?.activeEditor||p.length===0)return;const e=p.findIndex(a=>a.id===H),r=e>=0?(e+1)%p.length:0;G(p[r].id)},[p,H,G]),Te=s.useCallback(()=>{if(!F.current?.activeEditor||p.length===0)return;const e=p.findIndex(a=>a.id===H),r=e>0?e-1:p.length-1;G(p[r].id)},[p,H,G]),Ee=s.useCallback(async e=>{const{fileName:r="document",triggerDownload:a=!0}=e||{},v=await F.current?.export({exportType:["docx"],exportedName:r,triggerDownload:a}),C=F.current?.activeEditor;if(C){const f=U(C);_?.({fields:f,blob:a?void 0:v,fileName:r})}return v},[_]);s.useImperativeHandle(o,()=>({insertField:e=>Q("inline",e),insertBlockField:e=>Q("block",e),updateField:X,deleteField:oe,selectField:G,nextField:we,previousField:Te,getFields:()=>p,exportTemplate:Ee,getSuperDoc:()=>F.current}));const Me=l.component||ye,ue=c.component||ve;return t.jsxs("div",{className:`superdoc-template-builder ${L||""}`,style:q,children:[t.jsxs("div",{style:{display:"flex",gap:"20px"},children:[c.position==="left"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ue,{fields:p,onSelect:e=>G(e.id),onDelete:oe,onUpdate:e=>X(e.id,e),selectedFieldId:H||void 0})}),t.jsxs("div",{className:"superdoc-template-builder-document",style:{flex:1},children:[A?.renderDefaultContainer&&t.jsx("div",{id:"superdoc-toolbar",className:"superdoc-template-builder-toolbar","data-testid":"template-builder-toolbar"}),t.jsx("div",{ref:ne,className:"superdoc-template-builder-editor",style:{height:te},"data-testid":"template-builder-editor"})]}),c.position==="right"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ue,{fields:p,onSelect:e=>G(e.id),onDelete:oe,onUpdate:e=>X(e.id,e),selectedFieldId:H||void 0})})]}),t.jsx(Me,{isVisible:P,position:h,availableFields:i.available||[],filteredFields:Y,filterQuery:b,allowCreate:i.allowCreate||!1,onSelect:ke,onClose:Se,onCreateField:E,existingFields:p,onSelectExisting:Fe})]})});je.displayName="SuperDocTemplateBuilder";exports.FieldList=ve;exports.FieldMenu=ye;exports.default=je;
package/dist/index.mjs CHANGED
@@ -1,26 +1,26 @@
1
1
  import { jsxs as o, jsx as r } from "react/jsx-runtime";
2
- import { useState as I, useEffect as oe, useMemo as Fe, forwardRef as ze, useRef as J, useCallback as S, useImperativeHandle as Be } from "react";
3
- const We = ({
2
+ import { useState as I, useEffect as oe, useMemo as pe, forwardRef as Be, useRef as J, useCallback as T, useImperativeHandle as We } from "react";
3
+ const He = ({
4
4
  isVisible: n,
5
- position: d,
6
- availableFields: g,
7
- filteredFields: l,
8
- filterQuery: u,
9
- allowCreate: a,
10
- onSelect: N,
5
+ position: l,
6
+ availableFields: f,
7
+ filteredFields: s,
8
+ filterQuery: c,
9
+ allowCreate: d,
10
+ onSelect: z,
11
11
  onClose: P,
12
- onCreateField: G,
13
- existingFields: f = [],
14
- onSelectExisting: T
12
+ onCreateField: O,
13
+ existingFields: p = [],
14
+ onSelectExisting: E
15
15
  }) => {
16
- const [x, y] = I(!1), [w, R] = I(""), [q, A] = I("inline"), [X, ie] = I(!0), [p, z] = I(!0);
16
+ const [m, y] = I(!1), [w, R] = I(""), [U, A] = I("inline"), [X, ie] = I(!0), [u, B] = I(!0);
17
17
  oe(() => {
18
18
  n || (y(!1), R(""), A("inline"));
19
19
  }, [n]);
20
- const V = Fe(() => ({
20
+ const V = pe(() => ({
21
21
  position: "absolute",
22
- left: d?.left,
23
- top: d?.top,
22
+ left: l?.left,
23
+ top: l?.top,
24
24
  zIndex: 1e3,
25
25
  background: "white",
26
26
  border: "1px solid #ddd",
@@ -28,24 +28,24 @@ const We = ({
28
28
  boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
29
29
  padding: "8px 0",
30
30
  width: "280px"
31
- }), [d]), O = l ?? g, j = !!u;
31
+ }), [l]), _ = s ?? f, j = !!c;
32
32
  if (oe(() => {
33
- j && z(!0);
33
+ j && B(!0);
34
34
  }, [j]), !n) return null;
35
35
  const D = async () => {
36
- const m = w.trim();
37
- if (!m) return;
38
- const B = {
36
+ const h = w.trim();
37
+ if (!h) return;
38
+ const W = {
39
39
  id: `custom_${Date.now()}`,
40
- label: m,
41
- mode: q
40
+ label: h,
41
+ mode: U
42
42
  };
43
43
  try {
44
- if (G) {
45
- const h = await G(B);
46
- N(h || B);
44
+ if (O) {
45
+ const b = await O(W);
46
+ z(b || W);
47
47
  } else
48
- N(B);
48
+ z(W);
49
49
  } finally {
50
50
  y(!1), R(""), A("inline");
51
51
  }
@@ -61,11 +61,11 @@ const We = ({
61
61
  },
62
62
  children: /* @__PURE__ */ o("div", { style: { fontSize: "12px", color: "#6b7280" }, children: [
63
63
  "Filtering results for",
64
- /* @__PURE__ */ r("span", { style: { fontWeight: 600, color: "#111827", marginLeft: "4px" }, children: u })
64
+ /* @__PURE__ */ r("span", { style: { fontWeight: 600, color: "#111827", marginLeft: "4px" }, children: c })
65
65
  ] })
66
66
  }
67
67
  ),
68
- a && !x && /* @__PURE__ */ r(
68
+ d && !m && /* @__PURE__ */ r(
69
69
  "div",
70
70
  {
71
71
  className: "field-menu-item",
@@ -79,16 +79,16 @@ const We = ({
79
79
  children: "+ Create New Field"
80
80
  }
81
81
  ),
82
- a && x && /* @__PURE__ */ o("div", { style: { padding: "8px 16px" }, children: [
82
+ d && m && /* @__PURE__ */ o("div", { style: { padding: "8px 16px" }, children: [
83
83
  /* @__PURE__ */ r(
84
84
  "input",
85
85
  {
86
86
  type: "text",
87
87
  value: w,
88
88
  placeholder: "Field name...",
89
- onChange: (m) => R(m.target.value),
90
- onKeyDown: (m) => {
91
- m.key === "Enter" && D(), m.key === "Escape" && (y(!1), R(""), A("inline"));
89
+ onChange: (h) => R(h.target.value),
90
+ onKeyDown: (h) => {
91
+ h.key === "Enter" && D(), h.key === "Escape" && (y(!1), R(""), A("inline"));
92
92
  },
93
93
  autoFocus: !0,
94
94
  style: {
@@ -124,7 +124,7 @@ const We = ({
124
124
  {
125
125
  type: "radio",
126
126
  value: "inline",
127
- checked: q === "inline",
127
+ checked: U === "inline",
128
128
  onChange: () => A("inline")
129
129
  }
130
130
  ),
@@ -147,7 +147,7 @@ const We = ({
147
147
  {
148
148
  type: "radio",
149
149
  value: "block",
150
- checked: q === "block",
150
+ checked: U === "block",
151
151
  onChange: () => A("block")
152
152
  }
153
153
  ),
@@ -203,7 +203,7 @@ const We = ({
203
203
  }
204
204
  )
205
205
  ] }),
206
- a && g.length > 0 && /* @__PURE__ */ r(
206
+ d && f.length > 0 && /* @__PURE__ */ r(
207
207
  "div",
208
208
  {
209
209
  style: {
@@ -212,15 +212,15 @@ const We = ({
212
212
  }
213
213
  }
214
214
  ),
215
- f.length > 0 && (() => {
216
- const m = /* @__PURE__ */ new Map();
217
- f.forEach((h) => {
218
- const Y = h.group || `individual-${h.id}`, ee = m.get(Y) || [];
219
- ee.push(h), m.set(Y, ee);
215
+ p.length > 0 && (() => {
216
+ const h = /* @__PURE__ */ new Map();
217
+ p.forEach((b) => {
218
+ const Y = b.group || `individual-${b.id}`, ee = h.get(Y) || [];
219
+ ee.push(b), h.set(Y, ee);
220
220
  });
221
- const B = Array.from(m.values()).map((h) => ({
222
- ...h[0],
223
- count: h.length
221
+ const W = Array.from(h.values()).map((b) => ({
222
+ ...b[0],
223
+ count: b.length
224
224
  }));
225
225
  return /* @__PURE__ */ o("div", { style: { borderBottom: "1px solid #f0f0f0" }, children: [
226
226
  /* @__PURE__ */ o(
@@ -245,7 +245,7 @@ const We = ({
245
245
  children: [
246
246
  /* @__PURE__ */ o("span", { children: [
247
247
  "Existing Fields (",
248
- B.length,
248
+ W.length,
249
249
  ")"
250
250
  ] }),
251
251
  /* @__PURE__ */ r(
@@ -266,11 +266,11 @@ const We = ({
266
266
  ]
267
267
  }
268
268
  ),
269
- X && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: B.map((h) => /* @__PURE__ */ o(
269
+ X && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: W.map((b) => /* @__PURE__ */ o(
270
270
  "div",
271
271
  {
272
272
  className: "field-menu-item",
273
- onClick: () => T?.(h),
273
+ onClick: () => E?.(b),
274
274
  style: {
275
275
  padding: "8px 16px",
276
276
  cursor: "pointer",
@@ -281,7 +281,7 @@ const We = ({
281
281
  },
282
282
  children: [
283
283
  /* @__PURE__ */ o("div", { style: { flex: 1, minWidth: 0 }, children: [
284
- /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: h.alias || h.id }),
284
+ /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: b.alias || b.id }),
285
285
  /* @__PURE__ */ r(
286
286
  "div",
287
287
  {
@@ -290,7 +290,7 @@ const We = ({
290
290
  color: "#9ca3af",
291
291
  marginTop: "2px"
292
292
  },
293
- children: h.group ? `group (${h.count} fields)` : `ID: ${h.id}`
293
+ children: b.group ? `group (${b.count} fields)` : `ID: ${b.id}`
294
294
  }
295
295
  )
296
296
  ] }),
@@ -306,16 +306,16 @@ const We = ({
306
306
  textTransform: "capitalize",
307
307
  flexShrink: 0
308
308
  },
309
- children: h.mode || "inline"
309
+ children: b.mode || "inline"
310
310
  }
311
311
  )
312
312
  ]
313
313
  },
314
- h.group || h.id
314
+ b.group || b.id
315
315
  )) })
316
316
  ] });
317
317
  })(),
318
- O.length === 0 ? /* @__PURE__ */ r(
318
+ _.length === 0 ? /* @__PURE__ */ r(
319
319
  "div",
320
320
  {
321
321
  style: {
@@ -331,7 +331,7 @@ const We = ({
331
331
  "button",
332
332
  {
333
333
  type: "button",
334
- onClick: () => z(!p),
334
+ onClick: () => B(!u),
335
335
  style: {
336
336
  width: "100%",
337
337
  display: "flex",
@@ -349,7 +349,7 @@ const We = ({
349
349
  children: [
350
350
  /* @__PURE__ */ o("span", { children: [
351
351
  "Available Fields (",
352
- O.length,
352
+ _.length,
353
353
  ")"
354
354
  ] }),
355
355
  /* @__PURE__ */ r(
@@ -362,7 +362,7 @@ const We = ({
362
362
  height: "8px",
363
363
  borderRight: "2px solid #666",
364
364
  borderBottom: "2px solid #666",
365
- transform: p ? "rotate(45deg)" : "rotate(-45deg)",
365
+ transform: u ? "rotate(45deg)" : "rotate(-45deg)",
366
366
  transition: "transform 0.2s ease"
367
367
  }
368
368
  }
@@ -370,11 +370,11 @@ const We = ({
370
370
  ]
371
371
  }
372
372
  ),
373
- p && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: O.map((m) => /* @__PURE__ */ o(
373
+ u && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: _.map((h) => /* @__PURE__ */ o(
374
374
  "div",
375
375
  {
376
376
  className: "field-menu-item",
377
- onClick: () => N(m),
377
+ onClick: () => z(h),
378
378
  style: {
379
379
  padding: "8px 16px",
380
380
  cursor: "pointer",
@@ -385,7 +385,7 @@ const We = ({
385
385
  },
386
386
  children: [
387
387
  /* @__PURE__ */ o("div", { style: { flex: 1, minWidth: 0 }, children: [
388
- /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: m.label || m.id }),
388
+ /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: h.label || h.id }),
389
389
  /* @__PURE__ */ o(
390
390
  "div",
391
391
  {
@@ -396,7 +396,7 @@ const We = ({
396
396
  },
397
397
  children: [
398
398
  "ID: ",
399
- m.id
399
+ h.id
400
400
  ]
401
401
  }
402
402
  )
@@ -413,12 +413,12 @@ const We = ({
413
413
  textTransform: "capitalize",
414
414
  flexShrink: 0
415
415
  },
416
- children: m.mode || "inline"
416
+ children: h.mode || "inline"
417
417
  }
418
418
  )
419
419
  ]
420
420
  },
421
- m.id
421
+ h.id
422
422
  )) })
423
423
  ] }),
424
424
  /* @__PURE__ */ r(
@@ -446,36 +446,36 @@ const We = ({
446
446
  }
447
447
  )
448
448
  ] });
449
- }, He = (n) => {
450
- const d = n.split("-");
451
- return d.length > 2 ? d[d.length - 1].substring(0, 6) : n.substring(0, 6);
452
- }, ye = ({ field: n, onSelect: d, onDelete: g, isSelected: l, isGrouped: u = !1 }) => /* @__PURE__ */ o(
449
+ }, Le = (n) => {
450
+ const l = n.split("-");
451
+ return l.length > 2 ? l[l.length - 1].substring(0, 6) : n.substring(0, 6);
452
+ }, ve = ({ field: n, onSelect: l, onDelete: f, isSelected: s, isGrouped: c = !1 }) => /* @__PURE__ */ o(
453
453
  "div",
454
454
  {
455
- onClick: () => d(n),
455
+ onClick: () => l(n),
456
456
  style: {
457
457
  position: "relative",
458
458
  padding: "10px 12px",
459
- background: l ? "#eff6ff" : "#f9fafb",
460
- border: l ? "1px solid #3b82f6" : "1px solid #e5e7eb",
459
+ background: s ? "#eff6ff" : "#f9fafb",
460
+ border: s ? "1px solid #3b82f6" : "1px solid #e5e7eb",
461
461
  borderRadius: "6px",
462
462
  cursor: "pointer",
463
463
  transition: "all 0.2s",
464
- fontSize: u ? "13px" : "14px"
464
+ fontSize: c ? "13px" : "14px"
465
465
  },
466
- onMouseEnter: (a) => {
467
- l || (a.currentTarget.style.background = "#f3f4f6");
466
+ onMouseEnter: (d) => {
467
+ s || (d.currentTarget.style.background = "#f3f4f6");
468
468
  },
469
- onMouseLeave: (a) => {
470
- l || (a.currentTarget.style.background = "#f9fafb");
469
+ onMouseLeave: (d) => {
470
+ s || (d.currentTarget.style.background = "#f9fafb");
471
471
  },
472
472
  title: n.alias,
473
473
  children: [
474
474
  /* @__PURE__ */ r(
475
475
  "button",
476
476
  {
477
- onClick: (a) => {
478
- a.stopPropagation(), g(n.id);
477
+ onClick: (d) => {
478
+ d.stopPropagation(), f(n.id);
479
479
  },
480
480
  style: {
481
481
  position: "absolute",
@@ -491,11 +491,11 @@ const We = ({
491
491
  alignItems: "center",
492
492
  justifyContent: "center"
493
493
  },
494
- onMouseEnter: (a) => {
495
- a.currentTarget.style.color = "#ef4444";
494
+ onMouseEnter: (d) => {
495
+ d.currentTarget.style.color = "#ef4444";
496
496
  },
497
- onMouseLeave: (a) => {
498
- a.currentTarget.style.color = "#9ca3af";
497
+ onMouseLeave: (d) => {
498
+ d.currentTarget.style.color = "#9ca3af";
499
499
  },
500
500
  title: "Delete field",
501
501
  children: /* @__PURE__ */ r(
@@ -526,8 +526,8 @@ const We = ({
526
526
  {
527
527
  style: {
528
528
  fontWeight: "500",
529
- fontSize: u ? "12px" : "14px",
530
- color: u ? "#6b7280" : "#111827"
529
+ fontSize: c ? "12px" : "14px",
530
+ color: c ? "#6b7280" : "#111827"
531
531
  },
532
532
  children: n.alias || n.id
533
533
  }
@@ -568,21 +568,21 @@ const We = ({
568
568
  ] })
569
569
  ]
570
570
  }
571
- ), Le = ({
571
+ ), Pe = ({
572
572
  fields: n,
573
- onSelect: d,
574
- onDelete: g,
575
- selectedFieldId: l
573
+ onSelect: l,
574
+ onDelete: f,
575
+ selectedFieldId: s
576
576
  }) => {
577
- const [u, a] = I(/* @__PURE__ */ new Set()), { groupedFields: N, ungroupedFields: P } = Fe(() => {
578
- const f = {}, T = [];
579
- return n.forEach((x) => {
580
- x.group ? (f[x.group] || (f[x.group] = []), f[x.group].push(x)) : T.push(x);
581
- }), { groupedFields: f, ungroupedFields: T };
582
- }, [n]), G = (f) => {
583
- a((T) => {
584
- const x = new Set(T);
585
- return x.has(f) ? x.delete(f) : x.add(f), x;
577
+ const [c, d] = I(/* @__PURE__ */ new Set()), { groupedFields: z, ungroupedFields: P } = pe(() => {
578
+ const p = {}, E = [];
579
+ return n.forEach((m) => {
580
+ m.group ? (p[m.group] || (p[m.group] = []), p[m.group].push(m)) : E.push(m);
581
+ }), { groupedFields: p, ungroupedFields: E };
582
+ }, [n]), O = (p) => {
583
+ d((E) => {
584
+ const m = new Set(E);
585
+ return m.has(p) ? m.delete(p) : m.add(p), m;
586
586
  });
587
587
  };
588
588
  return /* @__PURE__ */ o(
@@ -618,18 +618,18 @@ const We = ({
618
618
  ]
619
619
  }
620
620
  ) : /* @__PURE__ */ o("div", { style: { display: "flex", flexDirection: "column", gap: "8px" }, children: [
621
- P.map((f) => /* @__PURE__ */ r(
622
- ye,
621
+ P.map((p) => /* @__PURE__ */ r(
622
+ ve,
623
623
  {
624
- field: f,
625
- onSelect: d,
626
- onDelete: g,
627
- isSelected: l === f.id
624
+ field: p,
625
+ onSelect: l,
626
+ onDelete: f,
627
+ isSelected: s === p.id
628
628
  },
629
- f.id
629
+ p.id
630
630
  )),
631
- Object.entries(N).map(([f, T]) => {
632
- const x = u.has(f), y = T[0];
631
+ Object.entries(z).map(([p, E]) => {
632
+ const m = c.has(p), y = E[0];
633
633
  return /* @__PURE__ */ o("div", { children: [
634
634
  /* @__PURE__ */ r(
635
635
  "div",
@@ -643,7 +643,7 @@ const We = ({
643
643
  cursor: "pointer",
644
644
  transition: "all 0.2s"
645
645
  },
646
- onClick: () => G(f),
646
+ onClick: () => O(p),
647
647
  onMouseEnter: (w) => {
648
648
  w.currentTarget.style.background = "#f3f4f6";
649
649
  },
@@ -651,7 +651,7 @@ const We = ({
651
651
  w.currentTarget.style.background = "#f9fafb";
652
652
  },
653
653
  children: /* @__PURE__ */ o("div", { style: { display: "flex", alignItems: "center", gap: "8px" }, children: [
654
- /* @__PURE__ */ r("span", { style: { fontSize: "12px", color: "#6b7280" }, children: x ? "▼" : "▶" }),
654
+ /* @__PURE__ */ r("span", { style: { fontSize: "12px", color: "#6b7280" }, children: m ? "▼" : "▶" }),
655
655
  /* @__PURE__ */ o("div", { style: { flex: 1 }, children: [
656
656
  /* @__PURE__ */ r("div", { style: { fontWeight: "500", fontSize: "14px" }, children: y.alias }),
657
657
  /* @__PURE__ */ o(
@@ -664,9 +664,9 @@ const We = ({
664
664
  },
665
665
  children: [
666
666
  "group: ",
667
- He(f),
667
+ Le(p),
668
668
  " (",
669
- T.length,
669
+ E.length,
670
670
  " fields)"
671
671
  ]
672
672
  }
@@ -675,7 +675,7 @@ const We = ({
675
675
  ] })
676
676
  }
677
677
  ),
678
- x && /* @__PURE__ */ r(
678
+ m && /* @__PURE__ */ r(
679
679
  "div",
680
680
  {
681
681
  style: {
@@ -685,47 +685,47 @@ const We = ({
685
685
  flexDirection: "column",
686
686
  gap: "4px"
687
687
  },
688
- children: T.map((w) => /* @__PURE__ */ r(
689
- ye,
688
+ children: E.map((w) => /* @__PURE__ */ r(
689
+ ve,
690
690
  {
691
691
  field: w,
692
- onSelect: d,
693
- onDelete: g,
694
- isSelected: l === w.id,
692
+ onSelect: l,
693
+ onDelete: f,
694
+ isSelected: s === w.id,
695
695
  isGrouped: !0
696
696
  },
697
697
  w.id
698
698
  ))
699
699
  }
700
700
  )
701
- ] }, f);
701
+ ] }, p);
702
702
  })
703
703
  ] })
704
704
  ]
705
705
  }
706
706
  );
707
707
  }, Q = (n) => {
708
- const d = n.helpers?.structuredContentCommands;
709
- return d?.getStructuredContentTags ? (d.getStructuredContentTags(n.state) || []).map((l) => {
710
- const u = l?.node ?? l, a = u?.attrs ?? {}, P = (u?.type?.name || "").includes("Block") ? "block" : "inline";
708
+ const l = n.helpers?.structuredContentCommands;
709
+ return l?.getStructuredContentTags ? (l.getStructuredContentTags(n.state) || []).map((s) => {
710
+ const c = s?.node ?? s, d = c?.attrs ?? {}, P = (c?.type?.name || "").includes("Block") ? "block" : "inline";
711
711
  return {
712
- id: a.id,
713
- alias: a.alias || a.label || "",
714
- tag: a.tag,
712
+ id: d.id,
713
+ alias: d.alias || d.label || "",
714
+ tag: d.tag,
715
715
  mode: P,
716
- group: d.getGroup?.(a.tag) ?? void 0
716
+ group: l.getGroup?.(d.tag) ?? void 0
717
717
  };
718
718
  }) : [];
719
- }, ve = (n, d) => {
720
- if (n === d) return !0;
721
- if (n.length !== d.length) return !1;
722
- for (let g = 0; g < n.length; g += 1) {
723
- const l = n[g], u = d[g];
724
- if (!u || l.id !== u.id || l.alias !== u.alias || l.tag !== u.tag || l.position !== u.position || l.mode !== u.mode || l.group !== u.group)
719
+ }, Ce = (n, l) => {
720
+ if (n === l) return !0;
721
+ if (n.length !== l.length) return !1;
722
+ for (let f = 0; f < n.length; f += 1) {
723
+ const s = n[f], c = l[f];
724
+ if (!c || s.id !== c.id || s.alias !== c.alias || s.tag !== c.tag || s.position !== c.position || s.mode !== c.mode || s.group !== c.group)
725
725
  return !1;
726
726
  }
727
727
  return !0;
728
- }, Pe = (n) => {
728
+ }, Ae = (n) => {
729
729
  if (!n) return null;
730
730
  if (n === !0)
731
731
  return {
@@ -739,64 +739,64 @@ const We = ({
739
739
  config: {},
740
740
  renderDefaultContainer: !1
741
741
  };
742
- const { selector: d, ...g } = n;
742
+ const { selector: l, ...f } = n;
743
743
  return {
744
- selector: d || "#superdoc-toolbar",
745
- config: g,
746
- renderDefaultContainer: d === void 0
744
+ selector: l || "#superdoc-toolbar",
745
+ config: f,
746
+ renderDefaultContainer: l === void 0
747
747
  };
748
- }, re = 10, Ae = 250, Ve = 300, Ce = (n) => {
749
- const d = window.innerWidth - Ae - re, g = window.innerHeight - Ve - re, l = Math.min(n.left, d), u = Math.min(n.top, g);
748
+ }, re = 10, Ve = 250, je = 300, Fe = (n) => {
749
+ const l = window.innerWidth - Ve - re, f = window.innerHeight - je - re, s = Math.min(n.left, l), c = Math.min(n.top, f);
750
750
  return new DOMRect(
751
- Math.max(l, re),
752
- Math.max(u, re),
751
+ Math.max(s, re),
752
+ Math.max(c, re),
753
753
  n.width,
754
754
  n.height
755
755
  );
756
- }, je = ze((n, d) => {
756
+ }, Ge = Be((n, l) => {
757
757
  const {
758
- document: g,
759
- fields: l = {},
760
- menu: u = {},
761
- list: a = {},
762
- toolbar: N,
758
+ document: f,
759
+ fields: s = {},
760
+ menu: c = {},
761
+ list: d = {},
762
+ toolbar: z,
763
763
  onReady: P,
764
- onTrigger: G,
765
- onFieldInsert: f,
766
- onFieldUpdate: T,
767
- onFieldDelete: x,
764
+ onTrigger: O,
765
+ onFieldInsert: p,
766
+ onFieldUpdate: E,
767
+ onFieldDelete: m,
768
768
  onFieldsChange: y,
769
769
  onFieldSelect: w,
770
770
  onFieldCreate: R,
771
- onExport: q,
771
+ onExport: U,
772
772
  className: A,
773
773
  style: X,
774
774
  documentHeight: ie = "600px"
775
- } = n, [p, z] = I(l.initial || []), [V, O] = I(null), [j, D] = I(!1), [m, B] = I(), [h, Y] = I(""), [ee, we] = I(
776
- () => l.available || []
777
- ), se = J(null), F = J(null), M = J(null), pe = J(l);
778
- pe.current = l;
779
- const W = J(null), fe = J(j);
775
+ } = n, [u, B] = I(s.initial || []), [V, _] = I(null), [j, D] = I(!1), [h, W] = I(), [b, Y] = I(""), [ee, we] = I(
776
+ () => s.available || []
777
+ ), se = J(null), k = J(null), M = J(null), fe = J(s);
778
+ fe.current = s;
779
+ const H = J(null), ge = J(j);
780
780
  oe(() => {
781
- fe.current = j;
781
+ ge.current = j;
782
782
  }, [j]);
783
- const te = u.trigger || "{{", le = pe.current.available || [], ge = S(
783
+ const te = c.trigger || "{{", le = fe.current.available || [], G = pe(() => Ae(z), [z]), xe = T(
784
784
  (e) => {
785
785
  const t = e.trim().toLowerCase();
786
786
  return t ? le.filter((i) => i.label.toLowerCase().includes(t)) : le;
787
787
  },
788
788
  [le]
789
- ), de = S(
789
+ ), de = T(
790
790
  (e) => {
791
- Y(e), we(ge(e));
791
+ Y(e), we(xe(e));
792
792
  },
793
- [ge]
794
- ), H = S(() => {
793
+ [xe]
794
+ ), L = T(() => {
795
795
  de("");
796
- }, [de]), K = S(
796
+ }, [de]), K = T(
797
797
  (e, t) => {
798
- if (!F.current?.activeEditor) return !1;
799
- const i = F.current.activeEditor, v = p, b = e === "inline" ? i.commands.insertStructuredContentInline?.({
798
+ if (!k.current?.activeEditor) return !1;
799
+ const i = k.current.activeEditor, v = u, F = e === "inline" ? i.commands.insertStructuredContentInline?.({
800
800
  attrs: {
801
801
  alias: t.alias,
802
802
  tag: t.metadata ? JSON.stringify(t.metadata) : void 0
@@ -809,154 +809,168 @@ const We = ({
809
809
  },
810
810
  text: t.defaultValue || t.alias
811
811
  });
812
- if (b) {
813
- const c = Q(i);
814
- z(c), y?.(c);
815
- const E = c.find(
816
- (C) => !v.some((s) => s.id === C.id)
812
+ if (F) {
813
+ const g = Q(i);
814
+ B(g), y?.(g);
815
+ const S = g.find(
816
+ (C) => !v.some((a) => a.id === C.id)
817
817
  );
818
- E && f?.(E);
818
+ S && p?.(S);
819
819
  }
820
- return b;
820
+ return F ?? !1;
821
821
  },
822
- [f, y, p]
823
- ), Z = S(
822
+ [p, y, u]
823
+ ), Z = T(
824
824
  (e, t) => {
825
- if (!F.current?.activeEditor) return !1;
826
- const v = F.current.activeEditor.commands.updateStructuredContentById?.(e, {
825
+ if (!k.current?.activeEditor) return !1;
826
+ const v = k.current.activeEditor.commands.updateStructuredContentById?.(e, {
827
827
  attrs: t
828
828
  });
829
- return v && z((b) => {
830
- const c = b.map((C) => C.id === e ? { ...C, ...t } : C);
831
- y?.(c);
832
- const E = c.find((C) => C.id === e);
833
- return E && T?.(E), c;
834
- }), v;
829
+ return v && B((F) => {
830
+ const g = F.map((C) => C.id === e ? { ...C, ...t } : C);
831
+ y?.(g);
832
+ const S = g.find((C) => C.id === e);
833
+ return S && E?.(S), g;
834
+ }), v ?? !1;
835
835
  },
836
- [T, y]
837
- ), ae = S(
836
+ [E, y]
837
+ ), ae = T(
838
838
  (e) => {
839
- const t = F.current?.activeEditor;
839
+ const t = k.current?.activeEditor;
840
840
  if (!t) {
841
- let s = !1;
842
- return z((k) => {
843
- if (!k.some((L) => L.id === e)) return k;
844
- const U = k.filter((L) => L.id !== e);
845
- return s = !0, y?.(U), U;
846
- }), s && (x?.(e), O((k) => k === e ? null : k)), s;
841
+ let a = !1;
842
+ return B((x) => {
843
+ if (!x.some((N) => N.id === e)) return x;
844
+ const q = x.filter((N) => N.id !== e);
845
+ return a = !0, y?.(q), m?.(e), q;
846
+ }), a && _((x) => x === e ? null : x), a;
847
847
  }
848
- const v = p.find((s) => s.id === e)?.group;
849
- let b = !1;
848
+ const v = u.find((a) => a.id === e)?.group;
849
+ let F = !1;
850
850
  try {
851
- b = t.commands.deleteStructuredContentById?.(e) ?? !1;
851
+ F = t.commands.deleteStructuredContentById?.(e) ?? !1;
852
852
  } catch {
853
- b = !1;
853
+ F = !1;
854
854
  }
855
- let c = Q(t);
856
- const E = c.some((s) => s.id === e);
857
- if (!b && E && (c = c.filter((s) => s.id !== e)), v) {
858
- const s = c.filter((k) => k.group === v);
859
- if (s.length === 1) {
860
- const k = s[0];
861
- t.commands.updateStructuredContentById?.(k.id, {
855
+ let g = Q(t);
856
+ const S = g.some((a) => a.id === e);
857
+ if (!F && S && (g = g.filter((a) => a.id !== e)), v) {
858
+ const a = g.filter((x) => x.group === v);
859
+ if (a.length === 1) {
860
+ const x = a[0];
861
+ t.commands.updateStructuredContentById?.(x.id, {
862
862
  attrs: { tag: void 0 }
863
- }), c = Q(t);
863
+ }), g = Q(t);
864
864
  }
865
865
  }
866
866
  let C = !1;
867
- return z((s) => {
868
- if (ve(s, c))
869
- return s;
870
- const k = s.some((L) => L.id === e), U = c.some((L) => L.id === e);
871
- return k && !U && (C = !0), y?.(c), c;
872
- }), C && (x?.(e), O((s) => s === e ? null : s)), b || C;
867
+ return B((a) => {
868
+ if (Ce(a, g))
869
+ return a;
870
+ const x = a.some((N) => N.id === e), q = g.some((N) => N.id === e);
871
+ return x && !q && (C = !0), y?.(g), C && m?.(e), g;
872
+ }), C && _((a) => a === e ? null : a), F || C;
873
873
  },
874
- [x, y, p]
875
- ), _ = S(
874
+ [m, y, u]
875
+ ), $ = T(
876
876
  (e) => {
877
- if (!F.current?.activeEditor) return;
878
- F.current.activeEditor.commands.selectStructuredContentById?.(e), O(e);
879
- const i = p.find((v) => v.id === e);
877
+ if (!k.current?.activeEditor) return;
878
+ k.current.activeEditor.commands.selectStructuredContentById?.(e), _(e);
879
+ const i = u.find((v) => v.id === e);
880
880
  i && w?.(i);
881
881
  },
882
- [p, w]
883
- ), ce = S(
882
+ [u, w]
883
+ ), ce = T(
884
884
  (e) => {
885
885
  if (!e) return;
886
886
  const t = Q(e);
887
- z((i) => ve(i, t) ? i : (y?.(t), t));
887
+ B((i) => Ce(i, t) ? i : (y?.(t), t));
888
888
  },
889
889
  [y]
890
890
  );
891
- oe(() => se.current ? ((async () => {
892
- const { SuperDoc: t } = await import("superdoc"), i = {
893
- comments: !1,
894
- ...$ && {
895
- toolbar: {
896
- selector: $.selector,
897
- toolbarGroups: $.config.toolbarGroups || ["center"],
898
- excludeItems: $.config.excludeItems || [],
899
- ...$.config
900
- }
901
- }
902
- }, v = () => {
903
- if (b.activeEditor) {
904
- const c = b.activeEditor;
905
- c.on("update", ({ editor: E }) => {
906
- const { state: C } = E, { from: s } = C.selection;
907
- if (s >= te.length) {
908
- const ue = s - te.length;
909
- if (C.doc.textBetween(ue, s) === te) {
910
- const me = E.view.coordsAtPos(s), he = Ce(new DOMRect(me.left, me.top, 0, 0)), be = () => {
911
- const ne = F.current?.activeEditor;
912
- if (!ne) return;
913
- const De = ne.state.selection.from, Ne = ne.state.tr.delete(ue, De);
914
- ne.view.dispatch(Ne);
915
- };
916
- M.current = be, W.current = s, B(he), D(!0), H(), G?.({
917
- position: { from: ue, to: s },
918
- bounds: he,
919
- cleanup: be
920
- });
921
- return;
922
- }
923
- }
924
- if (!fe.current)
925
- return;
926
- if (W.current == null) {
927
- D(!1), H();
928
- return;
891
+ oe(() => {
892
+ if (!se.current) return;
893
+ let e = !1, t = null;
894
+ return (async () => {
895
+ const { SuperDoc: v } = await import("superdoc");
896
+ if (e) return;
897
+ const F = {
898
+ comments: !1,
899
+ ...G && {
900
+ toolbar: {
901
+ selector: G.selector,
902
+ toolbarGroups: G.config.toolbarGroups || ["center"],
903
+ excludeItems: G.config.excludeItems || [],
904
+ ...G.config
929
905
  }
930
- if (s < W.current) {
931
- D(!1), W.current = null, H();
932
- return;
906
+ }
907
+ }, g = () => {
908
+ if (!e) {
909
+ if (t?.activeEditor) {
910
+ const S = t.activeEditor;
911
+ S.on("update", ({ editor: C }) => {
912
+ const { state: a } = C, { from: x } = a.selection;
913
+ if (x >= te.length) {
914
+ const ue = x - te.length;
915
+ if (a.doc.textBetween(ue, x) === te) {
916
+ const he = C.view.coordsAtPos(x), be = Fe(new DOMRect(he.left, he.top, 0, 0)), ye = () => {
917
+ const ne = k.current?.activeEditor;
918
+ if (!ne) return;
919
+ const Ne = ne.state.selection.from, ze = ne.state.tr.delete(ue, Ne);
920
+ ne.view.dispatch(ze);
921
+ };
922
+ M.current = ye, H.current = x, W(be), D(!0), L(), O?.({
923
+ position: { from: ue, to: x },
924
+ bounds: be,
925
+ cleanup: ye
926
+ });
927
+ return;
928
+ }
929
+ }
930
+ if (!ge.current)
931
+ return;
932
+ if (H.current == null) {
933
+ D(!1), L();
934
+ return;
935
+ }
936
+ if (x < H.current) {
937
+ D(!1), H.current = null, L();
938
+ return;
939
+ }
940
+ const q = a.doc.textBetween(H.current, x);
941
+ de(q);
942
+ const N = C.view.coordsAtPos(x), De = Fe(new DOMRect(N.left, N.top, 0, 0));
943
+ W(De);
944
+ }), S.on("update", () => {
945
+ ce(S);
946
+ }), ce(S);
933
947
  }
934
- const k = C.doc.textBetween(W.current, s);
935
- de(k);
936
- const U = E.view.coordsAtPos(s), L = Ce(new DOMRect(U.left, U.top, 0, 0));
937
- B(L);
938
- }), c.on("update", () => {
939
- ce(c);
940
- }), ce(c);
941
- }
942
- P?.();
943
- }, b = new t({
944
- selector: se.current,
945
- document: g?.source,
946
- documentMode: g?.mode || "editing",
947
- modules: i,
948
- toolbar: $?.selector,
949
- onReady: v
950
- });
951
- F.current = b;
952
- })(), () => {
953
- M.current = null, W.current = null;
954
- const t = F.current;
955
- t && typeof t.destroy == "function" && t.destroy(), F.current = null;
956
- }) : void 0, [g?.source, g?.mode, te, ce, P, G, N]);
957
- const ke = S(
948
+ P?.();
949
+ }
950
+ };
951
+ t = new v({
952
+ selector: se.current,
953
+ document: f?.source,
954
+ documentMode: f?.mode || "editing",
955
+ modules: F,
956
+ toolbar: G?.selector,
957
+ onReady: g
958
+ }), k.current = t;
959
+ })(), () => {
960
+ e = !0, M.current = null, H.current = null, t && typeof t.destroy == "function" && t.destroy(), k.current = null;
961
+ };
962
+ }, [
963
+ f?.source,
964
+ f?.mode,
965
+ te,
966
+ ce,
967
+ P,
968
+ O,
969
+ G
970
+ ]);
971
+ const ke = T(
958
972
  async (e) => {
959
- M.current && (M.current(), M.current = null), W.current = null, H();
973
+ M.current && (M.current(), M.current = null), H.current = null, L();
960
974
  const t = e.mode || "inline";
961
975
  if (e.id.startsWith("custom_") && R) {
962
976
  const i = await R(e);
@@ -976,88 +990,88 @@ const We = ({
976
990
  defaultValue: e.defaultValue
977
991
  }), D(!1);
978
992
  },
979
- [K, R, H]
980
- ), Se = S(
993
+ [K, R, L]
994
+ ), Se = T(
981
995
  (e) => {
982
- M.current && (M.current(), M.current = null), W.current = null, H();
983
- const t = F.current?.activeEditor;
996
+ M.current && (M.current(), M.current = null), H.current = null, L();
997
+ const t = k.current?.activeEditor;
984
998
  if (!t) return;
985
999
  const i = t.helpers?.structuredContentCommands;
986
1000
  if (!i) return;
987
- const v = e.group || `group-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, b = i.createTagObject?.({
1001
+ const v = e.group || `group-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, F = i.createTagObject?.({
988
1002
  group: v
989
1003
  });
990
1004
  if ((e.mode || "inline") === "inline" ? t.commands.insertStructuredContentInline?.({
991
1005
  attrs: {
992
1006
  alias: e.alias,
993
- tag: b
1007
+ tag: F
994
1008
  },
995
1009
  text: e.alias
996
1010
  }) : t.commands.insertStructuredContentBlock?.({
997
1011
  attrs: {
998
1012
  alias: e.alias,
999
- tag: b
1013
+ tag: F
1000
1014
  },
1001
1015
  text: e.alias
1002
1016
  })) {
1003
- e.group || Z(e.id, { tag: b }), D(!1);
1017
+ e.group || Z(e.id, { tag: F }), D(!1);
1004
1018
  const C = Q(t);
1005
- z(C), y?.(C);
1019
+ B(C), y?.(C);
1006
1020
  }
1007
1021
  },
1008
- [Z, H, y]
1009
- ), Te = S(() => {
1010
- D(!1), W.current = null, H(), M.current && (M.current(), M.current = null);
1011
- }, [H]), Ee = S(() => {
1012
- if (!F.current?.activeEditor || p.length === 0) return;
1013
- const e = p.findIndex((i) => i.id === V), t = e >= 0 ? (e + 1) % p.length : 0;
1014
- _(p[t].id);
1015
- }, [p, V, _]), Ie = S(() => {
1016
- if (!F.current?.activeEditor || p.length === 0) return;
1017
- const e = p.findIndex((i) => i.id === V), t = e > 0 ? e - 1 : p.length - 1;
1018
- _(p[t].id);
1019
- }, [p, V, _]), Me = S(
1022
+ [Z, L, y]
1023
+ ), Te = T(() => {
1024
+ D(!1), H.current = null, L(), M.current && (M.current(), M.current = null);
1025
+ }, [L]), Ee = T(() => {
1026
+ if (!k.current?.activeEditor || u.length === 0) return;
1027
+ const e = u.findIndex((i) => i.id === V), t = e >= 0 ? (e + 1) % u.length : 0;
1028
+ $(u[t].id);
1029
+ }, [u, V, $]), Ie = T(() => {
1030
+ if (!k.current?.activeEditor || u.length === 0) return;
1031
+ const e = u.findIndex((i) => i.id === V), t = e > 0 ? e - 1 : u.length - 1;
1032
+ $(u[t].id);
1033
+ }, [u, V, $]), Me = T(
1020
1034
  async (e) => {
1021
- const { fileName: t = "document", triggerDownload: i = !0 } = e || {}, v = await F.current?.export({
1035
+ const { fileName: t = "document", triggerDownload: i = !0 } = e || {}, v = await k.current?.export({
1022
1036
  exportType: ["docx"],
1023
1037
  exportedName: t,
1024
1038
  triggerDownload: i
1025
- }), b = F.current?.activeEditor;
1026
- if (b) {
1027
- const c = Q(b);
1028
- q?.({ fields: c, blob: i ? void 0 : v, fileName: t });
1039
+ }), F = k.current?.activeEditor;
1040
+ if (F) {
1041
+ const g = Q(F);
1042
+ U?.({ fields: g, blob: i ? void 0 : v, fileName: t });
1029
1043
  }
1030
1044
  return v;
1031
1045
  },
1032
- [q]
1046
+ [U]
1033
1047
  );
1034
- Be(d, () => ({
1048
+ We(l, () => ({
1035
1049
  insertField: (e) => K("inline", e),
1036
1050
  insertBlockField: (e) => K("block", e),
1037
1051
  updateField: Z,
1038
1052
  deleteField: ae,
1039
- selectField: _,
1053
+ selectField: $,
1040
1054
  nextField: Ee,
1041
1055
  previousField: Ie,
1042
- getFields: () => p,
1056
+ getFields: () => u,
1043
1057
  exportTemplate: Me,
1044
- getSuperDoc: () => F.current
1058
+ getSuperDoc: () => k.current
1045
1059
  }));
1046
- const Re = u.component || We, xe = a.component || Le, $ = Pe(N);
1060
+ const Re = c.component || He, me = d.component || Pe;
1047
1061
  return /* @__PURE__ */ o("div", { className: `superdoc-template-builder ${A || ""}`, style: X, children: [
1048
1062
  /* @__PURE__ */ o("div", { style: { display: "flex", gap: "20px" }, children: [
1049
- a.position === "left" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1050
- xe,
1063
+ d.position === "left" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1064
+ me,
1051
1065
  {
1052
- fields: p,
1053
- onSelect: (e) => _(e.id),
1066
+ fields: u,
1067
+ onSelect: (e) => $(e.id),
1054
1068
  onDelete: ae,
1055
1069
  onUpdate: (e) => Z(e.id, e),
1056
1070
  selectedFieldId: V || void 0
1057
1071
  }
1058
1072
  ) }),
1059
1073
  /* @__PURE__ */ o("div", { className: "superdoc-template-builder-document", style: { flex: 1 }, children: [
1060
- $?.renderDefaultContainer && /* @__PURE__ */ r(
1074
+ G?.renderDefaultContainer && /* @__PURE__ */ r(
1061
1075
  "div",
1062
1076
  {
1063
1077
  id: "superdoc-toolbar",
@@ -1075,11 +1089,11 @@ const We = ({
1075
1089
  }
1076
1090
  )
1077
1091
  ] }),
1078
- a.position === "right" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1079
- xe,
1092
+ d.position === "right" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1093
+ me,
1080
1094
  {
1081
- fields: p,
1082
- onSelect: (e) => _(e.id),
1095
+ fields: u,
1096
+ onSelect: (e) => $(e.id),
1083
1097
  onDelete: ae,
1084
1098
  onUpdate: (e) => Z(e.id, e),
1085
1099
  selectedFieldId: V || void 0
@@ -1090,23 +1104,23 @@ const We = ({
1090
1104
  Re,
1091
1105
  {
1092
1106
  isVisible: j,
1093
- position: m,
1094
- availableFields: l.available || [],
1107
+ position: h,
1108
+ availableFields: s.available || [],
1095
1109
  filteredFields: ee,
1096
- filterQuery: h,
1097
- allowCreate: l.allowCreate || !1,
1110
+ filterQuery: b,
1111
+ allowCreate: s.allowCreate || !1,
1098
1112
  onSelect: ke,
1099
1113
  onClose: Te,
1100
1114
  onCreateField: R,
1101
- existingFields: p,
1115
+ existingFields: u,
1102
1116
  onSelectExisting: Se
1103
1117
  }
1104
1118
  )
1105
1119
  ] });
1106
1120
  });
1107
- je.displayName = "SuperDocTemplateBuilder";
1121
+ Ge.displayName = "SuperDocTemplateBuilder";
1108
1122
  export {
1109
- Le as FieldList,
1110
- We as FieldMenu,
1111
- je as default
1123
+ Pe as FieldList,
1124
+ He as FieldMenu,
1125
+ Ge as default
1112
1126
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdoc-dev/template-builder",
3
- "version": "0.5.0",
3
+ "version": "1.0.0",
4
4
  "description": "React template builder component for SuperDoc",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -54,7 +54,7 @@
54
54
  "peerDependencies": {
55
55
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
56
56
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
57
- "superdoc": "^0.35.3"
57
+ "superdoc": "^1.5.0"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@commitlint/cli": "^20.1.0",