@superdoc-dev/template-builder 1.1.0-next.9 → 1.1.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.
package/dist/index.js CHANGED
@@ -1 +1 @@
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:u,onSelect:R,onClose:W,onCreateField:V,existingFields:x=[],onSelectExisting:w})=>{const[m,y]=s.useState(!1),[F,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=F.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})]})}),u&&!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"}),u&&m&&t.jsxs("div",{style:{padding:"8px 16px"},children:[t.jsx("input",{type:"text",value:F,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:!F.trim(),style:{padding:"4px 12px",background:F.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:F.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"})]})]}),u&&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:u=>{i||(u.currentTarget.style.background="#f3f4f6")},onMouseLeave:u=>{i||(u.currentTarget.style.background="#f9fafb")},title:n.alias,children:[t.jsx("button",{onClick:u=>{u.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:u=>{u.currentTarget.style.color="#ef4444"},onMouseLeave:u=>{u.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,u]=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=>{u(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:F=>{F.currentTarget.style.background="#f3f4f6"},onMouseLeave:F=>{F.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(F=>t.jsx(me,{field:F,onSelect:o,onDelete:d,isSelected:i===F.id,isGrouped:!0},F.id))})]},x)})]})]})},U=n=>{const o=n.helpers?.structuredContentCommands;return o?.getStructuredContentTags?(o.getStructuredContentTags(n.state)||[]).map(i=>{const l=i?.node??i,u=l?.attrs??{},W=(l?.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 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:u={},toolbar:R,onReady:W,onTrigger:V,onFieldInsert:x,onFieldUpdate:w,onFieldDelete:m,onFieldsChange:y,onFieldSelect:F,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),k=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(!k.current?.activeEditor)return!1;const a=k.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(c=>c.id===j.id));S&&x?.(S)}return C??!1},[x,y,p]),X=s.useCallback((e,r)=>{if(!k.current?.activeEditor)return!1;const v=k.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=k.current?.activeEditor;if(!r){let c=!1;return D(g=>{if(!g.some(I=>I.id===e))return g;const $=g.filter(I=>I.id!==e);return c=!0,y?.($),m?.(e),$}),c&&O(g=>g===e?null:g),c}const v=p.find(c=>c.id===e)?.group;let C=!1;try{C=r.commands.deleteStructuredContentById?.(e)??!1}catch(c){console.warn("[TemplateBuilder] Failed to delete structured content:",e,c),C=!1}let f=U(r);const S=f.some(c=>c.id===e);if(!C&&S&&(f=f.filter(c=>c.id!==e)),v){const c=f.filter(g=>g.group===v);if(c.length===1){const g=c[0];r.commands.updateStructuredContentById?.(g.id,{attrs:{tag:void 0}}),f=U(r)}}let j=!1;return D(c=>{if(he(c,f))return c;const g=c.some(I=>I.id===e),$=f.some(I=>I.id===e);return g&&!$&&(j=!0),y?.(f),j&&m?.(e),f}),j&&O(c=>c===e?null:c),C||j},[m,y,p]),G=s.useCallback(e=>{if(!k.current?.activeEditor)return;k.current.activeEditor.commands.selectStructuredContentById?.(e),O(e);const a=p.find(v=>v.id===e);a&&F?.(a)},[p,F]),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:c}=j,{from:g}=c.selection;if(g>=K.length){const le=g-K.length;if(c.doc.textBetween(le,g)===K){const pe=j.view.coordsAtPos(g),xe=be(new DOMRect(pe.left,pe.top,0,0)),fe=()=>{const Z=k.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 $=c.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}),k.current=r})(),()=>{e=!0,T.current=null,z.current=null,r&&typeof r.destroy=="function"&&r.destroy(),k.current=null}},[d?.source,d?.mode,K,ie,W,V,A]);const Fe=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]),ke=s.useCallback(e=>{T.current&&(T.current(),T.current=null),z.current=null,B();const r=k.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(!k.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(!k.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 k.current?.export({exportType:["docx"],exportedName:r,triggerDownload:a}),C=k.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:()=>k.current}));const Me=l.component||ye,ue=u.component||ve;return t.jsxs("div",{className:`superdoc-template-builder ${L||""}`,style:q,children:[t.jsxs("div",{style:{display:"flex",gap:"20px"},children:[u.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"})]}),u.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:Fe,onClose:Se,onCreateField:E,existingFields:p,onSelectExisting:ke})]})});je.displayName="SuperDocTemplateBuilder";exports.FieldList=ve;exports.FieldMenu=ye;exports.default=je;
1
+ "use strict";var ze=Object.create;var ge=Object.defineProperty;var Be=Object.getOwnPropertyDescriptor;var We=Object.getOwnPropertyNames;var Le=Object.getPrototypeOf,He=Object.prototype.hasOwnProperty;var Pe=(n,o,d,i)=>{if(o&&typeof o=="object"||typeof o=="function")for(let l of We(o))!He.call(n,l)&&l!==d&&ge(n,l,{get:()=>o[l],enumerable:!(i=Be(o,l))||i.enumerable});return n};var Ae=(n,o,d)=>(d=n!=null?ze(Le(n)):{},Pe(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:p,onSelect:I,onClose:z,onCreateField:A,existingFields:f=[],onSelectExisting:S})=>{const[y,w]=s.useState(!1),[x,B]=s.useState(""),[W,R]=s.useState("inline"),[U,te]=s.useState(!0),[q,g]=s.useState(!0);s.useEffect(()=>{n||(w(!1),B(""),R("inline"))},[n]);const L=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]),E=i??d,V=!!l;if(s.useEffect(()=>{V&&g(!0)},[V]),!n)return null;const O=async()=>{const u=x.trim();if(!u)return;const H={id:`custom_${Date.now()}`,label:u,mode:W};try{if(A){const b=await A(H);I(b||H)}else I(H)}finally{w(!1),B(""),R("inline")}};return t.jsxs("div",{className:"superdoc-field-menu",style:L,children:[V&&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})]})}),p&&!y&&t.jsx("div",{className:"field-menu-item",onClick:()=>w(!0),style:{padding:"8px 16px",cursor:"pointer",color:"#0066cc",fontWeight:500},children:"+ Create New Field"}),p&&y&&t.jsxs("div",{style:{padding:"8px 16px"},children:[t.jsx("input",{type:"text",value:x,placeholder:"Field name...",onChange:u=>B(u.target.value),onKeyDown:u=>{u.key==="Enter"&&O(),u.key==="Escape"&&(w(!1),B(""),R("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:W==="inline",onChange:()=>R("inline")}),"Inline"]}),t.jsxs("label",{style:{display:"flex",alignItems:"center",gap:"4px",cursor:"pointer"},children:[t.jsx("input",{type:"radio",value:"block",checked:W==="block",onChange:()=>R("block")}),"Block"]})]}),t.jsxs("div",{style:{marginTop:"8px",display:"flex",gap:"8px"},children:[t.jsx("button",{onClick:O,disabled:!x.trim(),style:{padding:"4px 12px",background:x.trim()?"#0066cc":"#ccc",color:"white",border:"none",borderRadius:"3px",cursor:x.trim()?"pointer":"not-allowed"},children:"Create"}),t.jsx("button",{onClick:()=>{w(!1),B(""),R("inline")},style:{padding:"4px 12px",background:"white",border:"1px solid #ddd",borderRadius:"3px",cursor:"pointer"},children:"Cancel"})]})]}),p&&d.length>0&&t.jsx("div",{style:{borderTop:"1px solid #eee",margin:"4px 0"}}),f.length>0&&(()=>{const u=new Map;f.forEach(b=>{const J=b.group||`individual-${b.id}`,Y=u.get(J)||[];Y.push(b),u.set(J,Y)});const H=Array.from(u.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(!U),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 (",H.length,")"]}),t.jsx("span",{"aria-hidden":!0,style:{display:"inline-block",width:"8px",height:"8px",borderRight:"2px solid #666",borderBottom:"2px solid #666",transform:U?"rotate(45deg)":"rotate(-45deg)",transition:"transform 0.2s ease"}})]}),U&&t.jsx("div",{style:{maxHeight:"300px",overflowY:"auto"},children:H.map(b=>t.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:[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))})]})})(),E.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:()=>g(!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:["Available Fields (",E.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:E.map(u=>t.jsxs("div",{className:"field-menu-item",onClick:()=>I(u),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:u.label||u.id}),t.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["ID: ",u.id]})]}),t.jsx("span",{style:{fontSize:"11px",color:"#6b7280",padding:"2px 6px",background:"#f3f4f6",borderRadius:"3px",textTransform:"capitalize",flexShrink:0},children:u.mode||"inline"})]},u.id))})]}),t.jsx("div",{style:{borderTop:"1px solid #eee",marginTop:"4px"},children:t.jsx("button",{onClick:z,style:{width:"100%",padding:"6px 16px",background:"#f3f4f6",border:"none",borderRadius:"0 0 4px 4px",cursor:"pointer"},children:"Close"})})]})},Ve=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:p=>{i||(p.currentTarget.style.background="#f3f4f6")},onMouseLeave:p=>{i||(p.currentTarget.style.background="#f9fafb")},title:n.alias,children:[t.jsx("button",{onClick:p=>{p.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:p=>{p.currentTarget.style.color="#ef4444"},onMouseLeave:p=>{p.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,p]=s.useState(new Set),{groupedFields:I,ungroupedFields:z}=s.useMemo(()=>{const f={},S=[];return n.forEach(y=>{y.group?(f[y.group]||(f[y.group]=[]),f[y.group].push(y)):S.push(y)}),{groupedFields:f,ungroupedFields:S}},[n]),A=f=>{p(S=>{const y=new Set(S);return y.has(f)?y.delete(f):y.add(f),y})};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:[z.map(f=>t.jsx(me,{field:f,onSelect:o,onDelete:d,isSelected:i===f.id},f.id)),Object.entries(I).map(([f,S])=>{const y=l.has(f),w=S[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:()=>A(f),onMouseEnter:x=>{x.currentTarget.style.background="#f3f4f6"},onMouseLeave:x=>{x.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:y?"▼":"▶"}),t.jsxs("div",{style:{flex:1},children:[t.jsx("div",{style:{fontWeight:"500",fontSize:"14px"},children:w.alias}),t.jsxs("div",{style:{fontSize:"11px",color:"#9ca3af",marginTop:"2px"},children:["group: ",Ve(f)," (",S.length," fields)"]})]})]})}),y&&t.jsx("div",{style:{marginLeft:"16px",marginTop:"4px",display:"flex",flexDirection:"column",gap:"4px"},children:S.map(x=>t.jsx(me,{field:x,onSelect:o,onDelete:d,isSelected:i===x.id,isGrouped:!0},x.id))})]},f)})]})]})},$=n=>{const o=n.helpers?.structuredContentCommands;return o?.getStructuredContentTags?(o.getStructuredContentTags(n.state)||[]).map(i=>{const l=i?.node??i,p=l?.attrs??{},z=(l?.type?.name||"").includes("Block")?"block":"inline";return{id:p.id,alias:p.alias||p.label||"",tag:p.tag,mode:z,group:o.getGroup?.(p.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},Oe=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,Ge=250,_e=300,be=n=>{const o=window.innerWidth-Ge-ee,d=window.innerHeight-_e-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:p={},toolbar:I,cspNonce:z,onReady:A,onTrigger:f,onFieldInsert:S,onFieldUpdate:y,onFieldDelete:w,onFieldsChange:x,onFieldSelect:B,onFieldCreate:W,onExport:R,className:U,style:te,documentHeight:q="600px"}=n,[g,L]=s.useState(i.initial||[]),[E,V]=s.useState(null),[O,u]=s.useState(!1),[H,b]=s.useState(),[J,Y]=s.useState(""),[Ce,Fe]=s.useState(()=>i.available||[]),ne=s.useRef(null),F=s.useRef(null),T=s.useRef(null),ae=s.useRef(i);ae.current=i;const D=s.useRef(null),de=s.useRef(O);s.useEffect(()=>{de.current=O},[O]);const K=l.trigger||"{{",re=ae.current.available||[],P=s.useMemo(()=>Oe(I),[I]),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=>{Y(e),Fe(ce(e))},[ce]),N=s.useCallback(()=>{se("")},[se]),Q=s.useCallback((e,r)=>{if(!F.current?.activeEditor)return!1;const a=F.current.activeEditor,v=g,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 m=$(a);L(m),x?.(m);const k=m.find(j=>!v.some(c=>c.id===j.id));k&&S?.(k)}return C??!1},[S,x,g]),X=s.useCallback((e,r)=>{if(!F.current?.activeEditor)return!1;const v=F.current.activeEditor.commands.updateStructuredContentById?.(e,{attrs:r});return v&&L(C=>{const m=C.map(j=>j.id===e?{...j,...r}:j);x?.(m);const k=m.find(j=>j.id===e);return k&&y?.(k),m}),v??!1},[y,x]),oe=s.useCallback(e=>{const r=F.current?.activeEditor;if(!r){let c=!1;return L(h=>{if(!h.some(M=>M.id===e))return h;const _=h.filter(M=>M.id!==e);return c=!0,x?.(_),w?.(e),_}),c&&V(h=>h===e?null:h),c}const v=g.find(c=>c.id===e)?.group;let C=!1;try{C=r.commands.deleteStructuredContentById?.(e)??!1}catch(c){console.warn("[TemplateBuilder] Failed to delete structured content:",e,c),C=!1}let m=$(r);const k=m.some(c=>c.id===e);if(!C&&k&&(m=m.filter(c=>c.id!==e)),v){const c=m.filter(h=>h.group===v);if(c.length===1){const h=c[0];r.commands.updateStructuredContentById?.(h.id,{attrs:{tag:void 0}}),m=$(r)}}let j=!1;return L(c=>{if(he(c,m))return c;const h=c.some(M=>M.id===e),_=m.some(M=>M.id===e);return h&&!_&&(j=!0),x?.(m),j&&w?.(e),m}),j&&V(c=>c===e?null:c),C||j},[w,x,g]),G=s.useCallback(e=>{if(!F.current?.activeEditor)return;F.current.activeEditor.commands.selectStructuredContentById?.(e),V(e);const a=g.find(v=>v.id===e);a&&B?.(a)},[g,B]),ie=s.useCallback(e=>{if(!e)return;const r=$(e);L(a=>he(a,r)?a:(x?.(r),r))},[x]);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,...P&&{toolbar:{selector:P.selector,toolbarGroups:P.config.toolbarGroups||["center"],excludeItems:P.config.excludeItems||[],...P.config}}},m=()=>{if(!e){if(r?.activeEditor){const k=r.activeEditor;k.on("update",({editor:j})=>{const{state:c}=j,{from:h}=c.selection;if(h>=K.length){const le=h-K.length;if(c.doc.textBetween(le,h)===K){const pe=j.view.coordsAtPos(h),xe=be(new DOMRect(pe.left,pe.top,0,0)),fe=()=>{const Z=F.current?.activeEditor;if(!Z)return;const De=Z.state.selection.from,Ne=Z.state.tr.delete(le,De);Z.view.dispatch(Ne)};T.current=fe,D.current=h,b(xe),u(!0),N(),f?.({position:{from:le,to:h},bounds:xe,cleanup:fe});return}}if(!de.current)return;if(D.current==null){u(!1),N();return}if(h<D.current){u(!1),D.current=null,N();return}const _=c.doc.textBetween(D.current,h);se(_);const M=j.view.coordsAtPos(h),Re=be(new DOMRect(M.left,M.top,0,0));b(Re)}),k.on("update",()=>{ie(k)}),ie(k)}A?.()}};r=new v({selector:ne.current,document:d?.source,documentMode:d?.mode||"editing",modules:C,toolbar:P?.selector,cspNonce:z,onReady:m}),F.current=r})(),()=>{e=!0,T.current=null,D.current=null,r&&typeof r.destroy=="function"&&r.destroy(),F.current=null}},[d?.source,d?.mode,K,ie,A,f,P,z]);const ke=s.useCallback(async e=>{T.current&&(T.current(),T.current=null),D.current=null,N();const r=e.mode||"inline";if(e.id.startsWith("custom_")&&W){const a=await W(e);if(a){const v=a.mode||r;Q(v,{alias:a.label,metadata:a.metadata,defaultValue:a.defaultValue}),u(!1);return}}Q(r,{alias:e.label,metadata:e.metadata,defaultValue:e.defaultValue}),u(!1)},[Q,W,N]),Se=s.useCallback(e=>{T.current&&(T.current(),T.current=null),D.current=null,N();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}),u(!1);const j=$(r);L(j),x?.(j)}},[X,N,x]),we=s.useCallback(()=>{u(!1),D.current=null,N(),T.current&&(T.current(),T.current=null)},[N]),Te=s.useCallback(()=>{if(!F.current?.activeEditor||g.length===0)return;const e=g.findIndex(a=>a.id===E),r=e>=0?(e+1)%g.length:0;G(g[r].id)},[g,E,G]),Ee=s.useCallback(()=>{if(!F.current?.activeEditor||g.length===0)return;const e=g.findIndex(a=>a.id===E),r=e>0?e-1:g.length-1;G(g[r].id)},[g,E,G]),Me=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 m=$(C);R?.({fields:m,blob:a?void 0:v,fileName:r})}return v},[R]);s.useImperativeHandle(o,()=>({insertField:e=>Q("inline",e),insertBlockField:e=>Q("block",e),updateField:X,deleteField:oe,selectField:G,nextField:Te,previousField:Ee,getFields:()=>g,exportTemplate:Me,getSuperDoc:()=>F.current}));const Ie=l.component||ye,ue=p.component||ve;return t.jsxs("div",{className:`superdoc-template-builder ${U||""}`,style:te,children:[t.jsxs("div",{style:{display:"flex",gap:"20px"},children:[p.position==="left"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ue,{fields:g,onSelect:e=>G(e.id),onDelete:oe,onUpdate:e=>X(e.id,e),selectedFieldId:E||void 0})}),t.jsxs("div",{className:"superdoc-template-builder-document",style:{flex:1},children:[P?.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:q},"data-testid":"template-builder-editor"})]}),p.position==="right"&&t.jsx("div",{className:"superdoc-template-builder-sidebar",children:t.jsx(ue,{fields:g,onSelect:e=>G(e.id),onDelete:oe,onUpdate:e=>X(e.id,e),selectedFieldId:E||void 0})})]}),t.jsx(Ie,{isVisible:O,position:H,availableFields:i.available||[],filteredFields:Ce,filterQuery:J,allowCreate:i.allowCreate||!1,onSelect:ke,onClose:we,onCreateField:W,existingFields:g,onSelectExisting:Se})]})});je.displayName="SuperDocTemplateBuilder";exports.FieldList=ve;exports.FieldMenu=ye;exports.default=je;
package/dist/index.mjs CHANGED
@@ -1,23 +1,23 @@
1
1
  import { jsxs as o, jsx as r } from "react/jsx-runtime";
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 = ({
2
+ import { useState as E, useEffect as oe, useMemo as pe, forwardRef as We, useRef as q, useCallback as S, useImperativeHandle as He } from "react";
3
+ const Le = ({
4
4
  isVisible: n,
5
5
  position: l,
6
- availableFields: f,
6
+ availableFields: m,
7
7
  filteredFields: s,
8
- filterQuery: c,
9
- allowCreate: a,
10
- onSelect: z,
11
- onClose: P,
12
- onCreateField: O,
13
- existingFields: p = [],
14
- onSelectExisting: E
8
+ filterQuery: p,
9
+ allowCreate: c,
10
+ onSelect: N,
11
+ onClose: H,
12
+ onCreateField: G,
13
+ existingFields: f = [],
14
+ onSelectExisting: T
15
15
  }) => {
16
- const [x, y] = I(!1), [w, R] = I(""), [U, A] = I("inline"), [X, ie] = I(!0), [u, B] = I(!0);
16
+ const [y, I] = E(!1), [u, L] = E(""), [P, z] = E("inline"), [Q, ie] = E(!0), [X, g] = E(!0);
17
17
  oe(() => {
18
- n || (y(!1), R(""), A("inline"));
18
+ n || (I(!1), L(""), z("inline"));
19
19
  }, [n]);
20
- const V = pe(() => ({
20
+ const A = pe(() => ({
21
21
  position: "absolute",
22
22
  left: l?.left,
23
23
  top: l?.top,
@@ -28,30 +28,30 @@ const He = ({
28
28
  boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
29
29
  padding: "8px 0",
30
30
  width: "280px"
31
- }), [l]), _ = s ?? f, j = !!c;
31
+ }), [l]), R = s ?? m, O = !!p;
32
32
  if (oe(() => {
33
- j && B(!0);
34
- }, [j]), !n) return null;
35
- const D = async () => {
36
- const h = w.trim();
37
- if (!h) return;
38
- const W = {
33
+ O && g(!0);
34
+ }, [O]), !n) return null;
35
+ const _ = async () => {
36
+ const a = u.trim();
37
+ if (!a) return;
38
+ const V = {
39
39
  id: `custom_${Date.now()}`,
40
- label: h,
41
- mode: U
40
+ label: a,
41
+ mode: P
42
42
  };
43
43
  try {
44
- if (O) {
45
- const b = await O(W);
46
- z(b || W);
44
+ if (G) {
45
+ const b = await G(V);
46
+ N(b || V);
47
47
  } else
48
- z(W);
48
+ N(V);
49
49
  } finally {
50
- y(!1), R(""), A("inline");
50
+ I(!1), L(""), z("inline");
51
51
  }
52
52
  };
53
- return /* @__PURE__ */ o("div", { className: "superdoc-field-menu", style: V, children: [
54
- j && /* @__PURE__ */ r(
53
+ return /* @__PURE__ */ o("div", { className: "superdoc-field-menu", style: A, children: [
54
+ O && /* @__PURE__ */ r(
55
55
  "div",
56
56
  {
57
57
  style: {
@@ -61,15 +61,15 @@ const He = ({
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: c })
64
+ /* @__PURE__ */ r("span", { style: { fontWeight: 600, color: "#111827", marginLeft: "4px" }, children: p })
65
65
  ] })
66
66
  }
67
67
  ),
68
- a && !x && /* @__PURE__ */ r(
68
+ c && !y && /* @__PURE__ */ r(
69
69
  "div",
70
70
  {
71
71
  className: "field-menu-item",
72
- onClick: () => y(!0),
72
+ onClick: () => I(!0),
73
73
  style: {
74
74
  padding: "8px 16px",
75
75
  cursor: "pointer",
@@ -79,16 +79,16 @@ const He = ({
79
79
  children: "+ Create New Field"
80
80
  }
81
81
  ),
82
- a && x && /* @__PURE__ */ o("div", { style: { padding: "8px 16px" }, children: [
82
+ c && y && /* @__PURE__ */ o("div", { style: { padding: "8px 16px" }, children: [
83
83
  /* @__PURE__ */ r(
84
84
  "input",
85
85
  {
86
86
  type: "text",
87
- value: w,
87
+ value: u,
88
88
  placeholder: "Field name...",
89
- onChange: (h) => R(h.target.value),
90
- onKeyDown: (h) => {
91
- h.key === "Enter" && D(), h.key === "Escape" && (y(!1), R(""), A("inline"));
89
+ onChange: (a) => L(a.target.value),
90
+ onKeyDown: (a) => {
91
+ a.key === "Enter" && _(), a.key === "Escape" && (I(!1), L(""), z("inline"));
92
92
  },
93
93
  autoFocus: !0,
94
94
  style: {
@@ -124,8 +124,8 @@ const He = ({
124
124
  {
125
125
  type: "radio",
126
126
  value: "inline",
127
- checked: U === "inline",
128
- onChange: () => A("inline")
127
+ checked: P === "inline",
128
+ onChange: () => z("inline")
129
129
  }
130
130
  ),
131
131
  "Inline"
@@ -147,8 +147,8 @@ const He = ({
147
147
  {
148
148
  type: "radio",
149
149
  value: "block",
150
- checked: U === "block",
151
- onChange: () => A("block")
150
+ checked: P === "block",
151
+ onChange: () => z("block")
152
152
  }
153
153
  ),
154
154
  "Block"
@@ -170,15 +170,15 @@ const He = ({
170
170
  /* @__PURE__ */ r(
171
171
  "button",
172
172
  {
173
- onClick: D,
174
- disabled: !w.trim(),
173
+ onClick: _,
174
+ disabled: !u.trim(),
175
175
  style: {
176
176
  padding: "4px 12px",
177
- background: w.trim() ? "#0066cc" : "#ccc",
177
+ background: u.trim() ? "#0066cc" : "#ccc",
178
178
  color: "white",
179
179
  border: "none",
180
180
  borderRadius: "3px",
181
- cursor: w.trim() ? "pointer" : "not-allowed"
181
+ cursor: u.trim() ? "pointer" : "not-allowed"
182
182
  },
183
183
  children: "Create"
184
184
  }
@@ -187,7 +187,7 @@ const He = ({
187
187
  "button",
188
188
  {
189
189
  onClick: () => {
190
- y(!1), R(""), A("inline");
190
+ I(!1), L(""), z("inline");
191
191
  },
192
192
  style: {
193
193
  padding: "4px 12px",
@@ -203,7 +203,7 @@ const He = ({
203
203
  }
204
204
  )
205
205
  ] }),
206
- a && f.length > 0 && /* @__PURE__ */ r(
206
+ c && m.length > 0 && /* @__PURE__ */ r(
207
207
  "div",
208
208
  {
209
209
  style: {
@@ -212,13 +212,13 @@ const He = ({
212
212
  }
213
213
  }
214
214
  ),
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);
215
+ f.length > 0 && (() => {
216
+ const a = /* @__PURE__ */ new Map();
217
+ f.forEach((b) => {
218
+ const Y = b.group || `individual-${b.id}`, ee = a.get(Y) || [];
219
+ ee.push(b), a.set(Y, ee);
220
220
  });
221
- const W = Array.from(h.values()).map((b) => ({
221
+ const V = Array.from(a.values()).map((b) => ({
222
222
  ...b[0],
223
223
  count: b.length
224
224
  }));
@@ -227,7 +227,7 @@ const He = ({
227
227
  "button",
228
228
  {
229
229
  type: "button",
230
- onClick: () => ie(!X),
230
+ onClick: () => ie(!Q),
231
231
  style: {
232
232
  width: "100%",
233
233
  display: "flex",
@@ -245,7 +245,7 @@ const He = ({
245
245
  children: [
246
246
  /* @__PURE__ */ o("span", { children: [
247
247
  "Existing Fields (",
248
- W.length,
248
+ V.length,
249
249
  ")"
250
250
  ] }),
251
251
  /* @__PURE__ */ r(
@@ -258,7 +258,7 @@ const He = ({
258
258
  height: "8px",
259
259
  borderRight: "2px solid #666",
260
260
  borderBottom: "2px solid #666",
261
- transform: X ? "rotate(45deg)" : "rotate(-45deg)",
261
+ transform: Q ? "rotate(45deg)" : "rotate(-45deg)",
262
262
  transition: "transform 0.2s ease"
263
263
  }
264
264
  }
@@ -266,11 +266,11 @@ const He = ({
266
266
  ]
267
267
  }
268
268
  ),
269
- X && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: W.map((b) => /* @__PURE__ */ o(
269
+ Q && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: V.map((b) => /* @__PURE__ */ o(
270
270
  "div",
271
271
  {
272
272
  className: "field-menu-item",
273
- onClick: () => E?.(b),
273
+ onClick: () => T?.(b),
274
274
  style: {
275
275
  padding: "8px 16px",
276
276
  cursor: "pointer",
@@ -315,7 +315,7 @@ const He = ({
315
315
  )) })
316
316
  ] });
317
317
  })(),
318
- _.length === 0 ? /* @__PURE__ */ r(
318
+ R.length === 0 ? /* @__PURE__ */ r(
319
319
  "div",
320
320
  {
321
321
  style: {
@@ -331,7 +331,7 @@ const He = ({
331
331
  "button",
332
332
  {
333
333
  type: "button",
334
- onClick: () => B(!u),
334
+ onClick: () => g(!X),
335
335
  style: {
336
336
  width: "100%",
337
337
  display: "flex",
@@ -349,7 +349,7 @@ const He = ({
349
349
  children: [
350
350
  /* @__PURE__ */ o("span", { children: [
351
351
  "Available Fields (",
352
- _.length,
352
+ R.length,
353
353
  ")"
354
354
  ] }),
355
355
  /* @__PURE__ */ r(
@@ -362,7 +362,7 @@ const He = ({
362
362
  height: "8px",
363
363
  borderRight: "2px solid #666",
364
364
  borderBottom: "2px solid #666",
365
- transform: u ? "rotate(45deg)" : "rotate(-45deg)",
365
+ transform: X ? "rotate(45deg)" : "rotate(-45deg)",
366
366
  transition: "transform 0.2s ease"
367
367
  }
368
368
  }
@@ -370,11 +370,11 @@ const He = ({
370
370
  ]
371
371
  }
372
372
  ),
373
- u && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: _.map((h) => /* @__PURE__ */ o(
373
+ X && /* @__PURE__ */ r("div", { style: { maxHeight: "300px", overflowY: "auto" }, children: R.map((a) => /* @__PURE__ */ o(
374
374
  "div",
375
375
  {
376
376
  className: "field-menu-item",
377
- onClick: () => z(h),
377
+ onClick: () => N(a),
378
378
  style: {
379
379
  padding: "8px 16px",
380
380
  cursor: "pointer",
@@ -385,7 +385,7 @@ const He = ({
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: h.label || h.id }),
388
+ /* @__PURE__ */ r("div", { style: { fontWeight: 500, fontSize: "13px" }, children: a.label || a.id }),
389
389
  /* @__PURE__ */ o(
390
390
  "div",
391
391
  {
@@ -396,7 +396,7 @@ const He = ({
396
396
  },
397
397
  children: [
398
398
  "ID: ",
399
- h.id
399
+ a.id
400
400
  ]
401
401
  }
402
402
  )
@@ -413,12 +413,12 @@ const He = ({
413
413
  textTransform: "capitalize",
414
414
  flexShrink: 0
415
415
  },
416
- children: h.mode || "inline"
416
+ children: a.mode || "inline"
417
417
  }
418
418
  )
419
419
  ]
420
420
  },
421
- h.id
421
+ a.id
422
422
  )) })
423
423
  ] }),
424
424
  /* @__PURE__ */ r(
@@ -431,7 +431,7 @@ const He = ({
431
431
  children: /* @__PURE__ */ r(
432
432
  "button",
433
433
  {
434
- onClick: P,
434
+ onClick: H,
435
435
  style: {
436
436
  width: "100%",
437
437
  padding: "6px 16px",
@@ -446,10 +446,10 @@ const He = ({
446
446
  }
447
447
  )
448
448
  ] });
449
- }, Le = (n) => {
449
+ }, Pe = (n) => {
450
450
  const l = n.split("-");
451
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(
452
+ }, ve = ({ field: n, onSelect: l, onDelete: m, isSelected: s, isGrouped: p = !1 }) => /* @__PURE__ */ o(
453
453
  "div",
454
454
  {
455
455
  onClick: () => l(n),
@@ -461,21 +461,21 @@ const He = ({
461
461
  borderRadius: "6px",
462
462
  cursor: "pointer",
463
463
  transition: "all 0.2s",
464
- fontSize: c ? "13px" : "14px"
464
+ fontSize: p ? "13px" : "14px"
465
465
  },
466
- onMouseEnter: (a) => {
467
- s || (a.currentTarget.style.background = "#f3f4f6");
466
+ onMouseEnter: (c) => {
467
+ s || (c.currentTarget.style.background = "#f3f4f6");
468
468
  },
469
- onMouseLeave: (a) => {
470
- s || (a.currentTarget.style.background = "#f9fafb");
469
+ onMouseLeave: (c) => {
470
+ s || (c.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(), f(n.id);
477
+ onClick: (c) => {
478
+ c.stopPropagation(), m(n.id);
479
479
  },
480
480
  style: {
481
481
  position: "absolute",
@@ -491,11 +491,11 @@ const He = ({
491
491
  alignItems: "center",
492
492
  justifyContent: "center"
493
493
  },
494
- onMouseEnter: (a) => {
495
- a.currentTarget.style.color = "#ef4444";
494
+ onMouseEnter: (c) => {
495
+ c.currentTarget.style.color = "#ef4444";
496
496
  },
497
- onMouseLeave: (a) => {
498
- a.currentTarget.style.color = "#9ca3af";
497
+ onMouseLeave: (c) => {
498
+ c.currentTarget.style.color = "#9ca3af";
499
499
  },
500
500
  title: "Delete field",
501
501
  children: /* @__PURE__ */ r("svg", { width: "14", height: "14", viewBox: "0 0 16 16", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ r(
@@ -516,8 +516,8 @@ const He = ({
516
516
  {
517
517
  style: {
518
518
  fontWeight: "500",
519
- fontSize: c ? "12px" : "14px",
520
- color: c ? "#6b7280" : "#111827"
519
+ fontSize: p ? "12px" : "14px",
520
+ color: p ? "#6b7280" : "#111827"
521
521
  },
522
522
  children: n.alias || n.id
523
523
  }
@@ -558,16 +558,16 @@ const He = ({
558
558
  ] })
559
559
  ]
560
560
  }
561
- ), Pe = ({ fields: n, onSelect: l, onDelete: f, selectedFieldId: s }) => {
562
- const [c, a] = I(/* @__PURE__ */ new Set()), { groupedFields: z, ungroupedFields: P } = pe(() => {
563
- const p = {}, E = [];
564
- return n.forEach((x) => {
565
- x.group ? (p[x.group] || (p[x.group] = []), p[x.group].push(x)) : E.push(x);
566
- }), { groupedFields: p, ungroupedFields: E };
567
- }, [n]), O = (p) => {
568
- a((E) => {
569
- const x = new Set(E);
570
- return x.has(p) ? x.delete(p) : x.add(p), x;
561
+ ), Ae = ({ fields: n, onSelect: l, onDelete: m, selectedFieldId: s }) => {
562
+ const [p, c] = E(/* @__PURE__ */ new Set()), { groupedFields: N, ungroupedFields: H } = pe(() => {
563
+ const f = {}, T = [];
564
+ return n.forEach((y) => {
565
+ y.group ? (f[y.group] || (f[y.group] = []), f[y.group].push(y)) : T.push(y);
566
+ }), { groupedFields: f, ungroupedFields: T };
567
+ }, [n]), G = (f) => {
568
+ c((T) => {
569
+ const y = new Set(T);
570
+ return y.has(f) ? y.delete(f) : y.add(f), y;
571
571
  });
572
572
  };
573
573
  return /* @__PURE__ */ o(
@@ -603,18 +603,18 @@ const He = ({
603
603
  ]
604
604
  }
605
605
  ) : /* @__PURE__ */ o("div", { style: { display: "flex", flexDirection: "column", gap: "8px" }, children: [
606
- P.map((p) => /* @__PURE__ */ r(
606
+ H.map((f) => /* @__PURE__ */ r(
607
607
  ve,
608
608
  {
609
- field: p,
609
+ field: f,
610
610
  onSelect: l,
611
- onDelete: f,
612
- isSelected: s === p.id
611
+ onDelete: m,
612
+ isSelected: s === f.id
613
613
  },
614
- p.id
614
+ f.id
615
615
  )),
616
- Object.entries(z).map(([p, E]) => {
617
- const x = c.has(p), y = E[0];
616
+ Object.entries(N).map(([f, T]) => {
617
+ const y = p.has(f), I = T[0];
618
618
  return /* @__PURE__ */ o("div", { children: [
619
619
  /* @__PURE__ */ r(
620
620
  "div",
@@ -628,17 +628,17 @@ const He = ({
628
628
  cursor: "pointer",
629
629
  transition: "all 0.2s"
630
630
  },
631
- onClick: () => O(p),
632
- onMouseEnter: (w) => {
633
- w.currentTarget.style.background = "#f3f4f6";
631
+ onClick: () => G(f),
632
+ onMouseEnter: (u) => {
633
+ u.currentTarget.style.background = "#f3f4f6";
634
634
  },
635
- onMouseLeave: (w) => {
636
- w.currentTarget.style.background = "#f9fafb";
635
+ onMouseLeave: (u) => {
636
+ u.currentTarget.style.background = "#f9fafb";
637
637
  },
638
638
  children: /* @__PURE__ */ o("div", { style: { display: "flex", alignItems: "center", gap: "8px" }, children: [
639
- /* @__PURE__ */ r("span", { style: { fontSize: "12px", color: "#6b7280" }, children: x ? "▼" : "▶" }),
639
+ /* @__PURE__ */ r("span", { style: { fontSize: "12px", color: "#6b7280" }, children: y ? "▼" : "▶" }),
640
640
  /* @__PURE__ */ o("div", { style: { flex: 1 }, children: [
641
- /* @__PURE__ */ r("div", { style: { fontWeight: "500", fontSize: "14px" }, children: y.alias }),
641
+ /* @__PURE__ */ r("div", { style: { fontWeight: "500", fontSize: "14px" }, children: I.alias }),
642
642
  /* @__PURE__ */ o(
643
643
  "div",
644
644
  {
@@ -649,9 +649,9 @@ const He = ({
649
649
  },
650
650
  children: [
651
651
  "group: ",
652
- Le(p),
652
+ Pe(f),
653
653
  " (",
654
- E.length,
654
+ T.length,
655
655
  " fields)"
656
656
  ]
657
657
  }
@@ -660,7 +660,7 @@ const He = ({
660
660
  ] })
661
661
  }
662
662
  ),
663
- x && /* @__PURE__ */ r(
663
+ y && /* @__PURE__ */ r(
664
664
  "div",
665
665
  {
666
666
  style: {
@@ -670,47 +670,47 @@ const He = ({
670
670
  flexDirection: "column",
671
671
  gap: "4px"
672
672
  },
673
- children: E.map((w) => /* @__PURE__ */ r(
673
+ children: T.map((u) => /* @__PURE__ */ r(
674
674
  ve,
675
675
  {
676
- field: w,
676
+ field: u,
677
677
  onSelect: l,
678
- onDelete: f,
679
- isSelected: s === w.id,
678
+ onDelete: m,
679
+ isSelected: s === u.id,
680
680
  isGrouped: !0
681
681
  },
682
- w.id
682
+ u.id
683
683
  ))
684
684
  }
685
685
  )
686
- ] }, p);
686
+ ] }, f);
687
687
  })
688
688
  ] })
689
689
  ]
690
690
  }
691
691
  );
692
- }, Q = (n) => {
692
+ }, J = (n) => {
693
693
  const l = n.helpers?.structuredContentCommands;
694
694
  return l?.getStructuredContentTags ? (l.getStructuredContentTags(n.state) || []).map((s) => {
695
- const c = s?.node ?? s, a = c?.attrs ?? {}, P = (c?.type?.name || "").includes("Block") ? "block" : "inline";
695
+ const p = s?.node ?? s, c = p?.attrs ?? {}, H = (p?.type?.name || "").includes("Block") ? "block" : "inline";
696
696
  return {
697
- id: a.id,
698
- alias: a.alias || a.label || "",
699
- tag: a.tag,
700
- mode: P,
701
- group: l.getGroup?.(a.tag) ?? void 0
697
+ id: c.id,
698
+ alias: c.alias || c.label || "",
699
+ tag: c.tag,
700
+ mode: H,
701
+ group: l.getGroup?.(c.tag) ?? void 0
702
702
  };
703
703
  }) : [];
704
704
  }, Fe = (n, l) => {
705
705
  if (n === l) return !0;
706
706
  if (n.length !== l.length) return !1;
707
- for (let f = 0; f < n.length; f += 1) {
708
- const s = n[f], c = l[f];
709
- 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)
707
+ for (let m = 0; m < n.length; m += 1) {
708
+ const s = n[m], p = l[m];
709
+ if (!p || s.id !== p.id || s.alias !== p.alias || s.tag !== p.tag || s.position !== p.position || s.mode !== p.mode || s.group !== p.group)
710
710
  return !1;
711
711
  }
712
712
  return !0;
713
- }, Ae = (n) => {
713
+ }, Ve = (n) => {
714
714
  if (!n) return null;
715
715
  if (n === !0)
716
716
  return {
@@ -724,63 +724,64 @@ const He = ({
724
724
  config: {},
725
725
  renderDefaultContainer: !1
726
726
  };
727
- const { selector: l, ...f } = n;
727
+ const { selector: l, ...m } = n;
728
728
  return {
729
729
  selector: l || "#superdoc-toolbar",
730
- config: f,
730
+ config: m,
731
731
  renderDefaultContainer: l === void 0
732
732
  };
733
- }, re = 10, Ve = 250, je = 300, Ce = (n) => {
734
- const l = window.innerWidth - Ve - re, f = window.innerHeight - je - re, s = Math.min(n.left, l), c = Math.min(n.top, f);
733
+ }, re = 10, je = 250, Ge = 300, Ce = (n) => {
734
+ const l = window.innerWidth - je - re, m = window.innerHeight - Ge - re, s = Math.min(n.left, l), p = Math.min(n.top, m);
735
735
  return new DOMRect(
736
736
  Math.max(s, re),
737
- Math.max(c, re),
737
+ Math.max(p, re),
738
738
  n.width,
739
739
  n.height
740
740
  );
741
- }, Ge = Be(
741
+ }, Oe = We(
742
742
  (n, l) => {
743
743
  const {
744
- document: f,
744
+ document: m,
745
745
  fields: s = {},
746
- menu: c = {},
747
- list: a = {},
748
- toolbar: z,
749
- onReady: P,
750
- onTrigger: O,
751
- onFieldInsert: p,
752
- onFieldUpdate: E,
753
- onFieldDelete: x,
754
- onFieldsChange: y,
755
- onFieldSelect: w,
756
- onFieldCreate: R,
757
- onExport: U,
758
- className: A,
759
- style: X,
760
- documentHeight: ie = "600px"
761
- } = n, [u, B] = I(s.initial || []), [V, _] = I(null), [j, D] = I(!1), [h, W] = I(), [b, Y] = I(""), [ee, we] = I(() => s.available || []), se = J(null), k = J(null), M = J(null), fe = J(s);
746
+ menu: p = {},
747
+ list: c = {},
748
+ toolbar: N,
749
+ cspNonce: H,
750
+ onReady: G,
751
+ onTrigger: f,
752
+ onFieldInsert: T,
753
+ onFieldUpdate: y,
754
+ onFieldDelete: I,
755
+ onFieldsChange: u,
756
+ onFieldSelect: L,
757
+ onFieldCreate: P,
758
+ onExport: z,
759
+ className: Q,
760
+ style: ie,
761
+ documentHeight: X = "600px"
762
+ } = n, [g, A] = E(s.initial || []), [R, O] = E(null), [_, a] = E(!1), [V, b] = E(), [Y, ee] = E(""), [we, ke] = E(() => s.available || []), se = q(null), w = q(null), M = q(null), fe = q(s);
762
763
  fe.current = s;
763
- const H = J(null), ge = J(j);
764
+ const B = q(null), ge = q(_);
764
765
  oe(() => {
765
- ge.current = j;
766
- }, [j]);
767
- const te = c.trigger || "{{", le = fe.current.available || [], G = pe(() => Ae(z), [z]), me = T(
766
+ ge.current = _;
767
+ }, [_]);
768
+ const te = p.trigger || "{{", le = fe.current.available || [], j = pe(() => Ve(N), [N]), me = S(
768
769
  (e) => {
769
770
  const t = e.trim().toLowerCase();
770
771
  return t ? le.filter((i) => i.label.toLowerCase().includes(t)) : le;
771
772
  },
772
773
  [le]
773
- ), de = T(
774
+ ), de = S(
774
775
  (e) => {
775
- Y(e), we(me(e));
776
+ ee(e), ke(me(e));
776
777
  },
777
778
  [me]
778
- ), L = T(() => {
779
+ ), W = S(() => {
779
780
  de("");
780
- }, [de]), K = T(
781
+ }, [de]), K = S(
781
782
  (e, t) => {
782
- if (!k.current?.activeEditor) return !1;
783
- const i = k.current.activeEditor, v = u, C = e === "inline" ? i.commands.insertStructuredContentInline?.({
783
+ if (!w.current?.activeEditor) return !1;
784
+ const i = w.current.activeEditor, v = g, C = e === "inline" ? i.commands.insertStructuredContentInline?.({
784
785
  attrs: {
785
786
  alias: t.alias,
786
787
  tag: t.metadata ? JSON.stringify(t.metadata) : void 0
@@ -794,83 +795,83 @@ const He = ({
794
795
  text: t.defaultValue || t.alias
795
796
  });
796
797
  if (C) {
797
- const g = Q(i);
798
- B(g), y?.(g);
799
- const S = g.find(
798
+ const x = J(i);
799
+ A(x), u?.(x);
800
+ const k = x.find(
800
801
  (F) => !v.some((d) => d.id === F.id)
801
802
  );
802
- S && p?.(S);
803
+ k && T?.(k);
803
804
  }
804
805
  return C ?? !1;
805
806
  },
806
- [p, y, u]
807
- ), Z = T(
807
+ [T, u, g]
808
+ ), Z = S(
808
809
  (e, t) => {
809
- if (!k.current?.activeEditor) return !1;
810
- const v = k.current.activeEditor.commands.updateStructuredContentById?.(e, {
810
+ if (!w.current?.activeEditor) return !1;
811
+ const v = w.current.activeEditor.commands.updateStructuredContentById?.(e, {
811
812
  attrs: t
812
813
  });
813
- return v && B((C) => {
814
- const g = C.map((F) => F.id === e ? { ...F, ...t } : F);
815
- y?.(g);
816
- const S = g.find((F) => F.id === e);
817
- return S && E?.(S), g;
814
+ return v && A((C) => {
815
+ const x = C.map((F) => F.id === e ? { ...F, ...t } : F);
816
+ u?.(x);
817
+ const k = x.find((F) => F.id === e);
818
+ return k && y?.(k), x;
818
819
  }), v ?? !1;
819
820
  },
820
- [E, y]
821
- ), ae = T(
821
+ [y, u]
822
+ ), ae = S(
822
823
  (e) => {
823
- const t = k.current?.activeEditor;
824
+ const t = w.current?.activeEditor;
824
825
  if (!t) {
825
826
  let d = !1;
826
- return B((m) => {
827
- if (!m.some((N) => N.id === e)) return m;
828
- const q = m.filter((N) => N.id !== e);
829
- return d = !0, y?.(q), x?.(e), q;
830
- }), d && _((m) => m === e ? null : m), d;
827
+ return A((h) => {
828
+ if (!h.some((D) => D.id === e)) return h;
829
+ const U = h.filter((D) => D.id !== e);
830
+ return d = !0, u?.(U), I?.(e), U;
831
+ }), d && O((h) => h === e ? null : h), d;
831
832
  }
832
- const v = u.find((d) => d.id === e)?.group;
833
+ const v = g.find((d) => d.id === e)?.group;
833
834
  let C = !1;
834
835
  try {
835
836
  C = t.commands.deleteStructuredContentById?.(e) ?? !1;
836
837
  } catch (d) {
837
838
  console.warn("[TemplateBuilder] Failed to delete structured content:", e, d), C = !1;
838
839
  }
839
- let g = Q(t);
840
- const S = g.some((d) => d.id === e);
841
- if (!C && S && (g = g.filter((d) => d.id !== e)), v) {
842
- const d = g.filter((m) => m.group === v);
840
+ let x = J(t);
841
+ const k = x.some((d) => d.id === e);
842
+ if (!C && k && (x = x.filter((d) => d.id !== e)), v) {
843
+ const d = x.filter((h) => h.group === v);
843
844
  if (d.length === 1) {
844
- const m = d[0];
845
- t.commands.updateStructuredContentById?.(m.id, {
845
+ const h = d[0];
846
+ t.commands.updateStructuredContentById?.(h.id, {
846
847
  attrs: { tag: void 0 }
847
- }), g = Q(t);
848
+ }), x = J(t);
848
849
  }
849
850
  }
850
851
  let F = !1;
851
- return B((d) => {
852
- if (Fe(d, g))
852
+ return A((d) => {
853
+ if (Fe(d, x))
853
854
  return d;
854
- const m = d.some((N) => N.id === e), q = g.some((N) => N.id === e);
855
- return m && !q && (F = !0), y?.(g), F && x?.(e), g;
856
- }), F && _((d) => d === e ? null : d), C || F;
855
+ const h = d.some((D) => D.id === e), U = x.some((D) => D.id === e);
856
+ return h && !U && (F = !0), u?.(x), F && I?.(e), x;
857
+ }), F && O((d) => d === e ? null : d), C || F;
857
858
  },
858
- [x, y, u]
859
- ), $ = T(
859
+ [I, u, g]
860
+ ), $ = S(
860
861
  (e) => {
861
- if (!k.current?.activeEditor) return;
862
- k.current.activeEditor.commands.selectStructuredContentById?.(e), _(e);
863
- const i = u.find((v) => v.id === e);
864
- i && w?.(i);
862
+ if (!w.current?.activeEditor) return;
863
+ w.current.activeEditor.commands.selectStructuredContentById?.(e), O(e);
864
+ const i = g.find((v) => v.id === e);
865
+ i && L?.(i);
865
866
  },
866
- [u, w]
867
- ), ce = T(
867
+ [g, L]
868
+ ), ce = S(
868
869
  (e) => {
869
870
  if (!e) return;
870
- const t = Q(e);
871
- B((i) => Fe(i, t) ? i : (y?.(t), t));
871
+ const t = J(e);
872
+ A((i) => Fe(i, t) ? i : (u?.(t), t));
872
873
  },
873
- [y]
874
+ [u]
874
875
  );
875
876
  oe(() => {
876
877
  if (!se.current) return;
@@ -880,31 +881,31 @@ const He = ({
880
881
  if (e) return;
881
882
  const C = {
882
883
  comments: !1,
883
- ...G && {
884
+ ...j && {
884
885
  toolbar: {
885
- selector: G.selector,
886
- toolbarGroups: G.config.toolbarGroups || ["center"],
887
- excludeItems: G.config.excludeItems || [],
888
- ...G.config
886
+ selector: j.selector,
887
+ toolbarGroups: j.config.toolbarGroups || ["center"],
888
+ excludeItems: j.config.excludeItems || [],
889
+ ...j.config
889
890
  }
890
891
  }
891
- }, g = () => {
892
+ }, x = () => {
892
893
  if (!e) {
893
894
  if (t?.activeEditor) {
894
- const S = t.activeEditor;
895
- S.on("update", ({ editor: F }) => {
896
- const { state: d } = F, { from: m } = d.selection;
897
- if (m >= te.length) {
898
- const ue = m - te.length;
899
- if (d.doc.textBetween(ue, m) === te) {
900
- const he = F.view.coordsAtPos(m), be = Ce(new DOMRect(he.left, he.top, 0, 0)), ye = () => {
901
- const ne = k.current?.activeEditor;
895
+ const k = t.activeEditor;
896
+ k.on("update", ({ editor: F }) => {
897
+ const { state: d } = F, { from: h } = d.selection;
898
+ if (h >= te.length) {
899
+ const ue = h - te.length;
900
+ if (d.doc.textBetween(ue, h) === te) {
901
+ const he = F.view.coordsAtPos(h), be = Ce(new DOMRect(he.left, he.top, 0, 0)), ye = () => {
902
+ const ne = w.current?.activeEditor;
902
903
  if (!ne) return;
903
- const Ne = ne.state.selection.from, ze = ne.state.tr.delete(ue, Ne);
904
- ne.view.dispatch(ze);
904
+ const ze = ne.state.selection.from, Be = ne.state.tr.delete(ue, ze);
905
+ ne.view.dispatch(Be);
905
906
  };
906
- M.current = ye, H.current = m, W(be), D(!0), L(), O?.({
907
- position: { from: ue, to: m },
907
+ M.current = ye, B.current = h, b(be), a(!0), W(), f?.({
908
+ position: { from: ue, to: h },
908
909
  bounds: be,
909
910
  cleanup: ye
910
911
  });
@@ -913,50 +914,51 @@ const He = ({
913
914
  }
914
915
  if (!ge.current)
915
916
  return;
916
- if (H.current == null) {
917
- D(!1), L();
917
+ if (B.current == null) {
918
+ a(!1), W();
918
919
  return;
919
920
  }
920
- if (m < H.current) {
921
- D(!1), H.current = null, L();
921
+ if (h < B.current) {
922
+ a(!1), B.current = null, W();
922
923
  return;
923
924
  }
924
- const q = d.doc.textBetween(H.current, m);
925
- de(q);
926
- const N = F.view.coordsAtPos(m), De = Ce(new DOMRect(N.left, N.top, 0, 0));
927
- W(De);
928
- }), S.on("update", () => {
929
- ce(S);
930
- }), ce(S);
925
+ const U = d.doc.textBetween(B.current, h);
926
+ de(U);
927
+ const D = F.view.coordsAtPos(h), Ne = Ce(new DOMRect(D.left, D.top, 0, 0));
928
+ b(Ne);
929
+ }), k.on("update", () => {
930
+ ce(k);
931
+ }), ce(k);
931
932
  }
932
- P?.();
933
+ G?.();
933
934
  }
934
935
  };
935
936
  t = new v({
936
937
  selector: se.current,
937
- document: f?.source,
938
- documentMode: f?.mode || "editing",
938
+ document: m?.source,
939
+ documentMode: m?.mode || "editing",
939
940
  modules: C,
940
- toolbar: G?.selector,
941
- onReady: g
942
- }), k.current = t;
941
+ toolbar: j?.selector,
942
+ cspNonce: H,
943
+ onReady: x
944
+ }), w.current = t;
943
945
  })(), () => {
944
- e = !0, M.current = null, H.current = null, t && typeof t.destroy == "function" && t.destroy(), k.current = null;
946
+ e = !0, M.current = null, B.current = null, t && typeof t.destroy == "function" && t.destroy(), w.current = null;
945
947
  };
946
- }, [f?.source, f?.mode, te, ce, P, O, G]);
947
- const ke = T(
948
+ }, [m?.source, m?.mode, te, ce, G, f, j, H]);
949
+ const Se = S(
948
950
  async (e) => {
949
- M.current && (M.current(), M.current = null), H.current = null, L();
951
+ M.current && (M.current(), M.current = null), B.current = null, W();
950
952
  const t = e.mode || "inline";
951
- if (e.id.startsWith("custom_") && R) {
952
- const i = await R(e);
953
+ if (e.id.startsWith("custom_") && P) {
954
+ const i = await P(e);
953
955
  if (i) {
954
956
  const v = i.mode || t;
955
957
  K(v, {
956
958
  alias: i.label,
957
959
  metadata: i.metadata,
958
960
  defaultValue: i.defaultValue
959
- }), D(!1);
961
+ }), a(!1);
960
962
  return;
961
963
  }
962
964
  }
@@ -964,13 +966,13 @@ const He = ({
964
966
  alias: e.label,
965
967
  metadata: e.metadata,
966
968
  defaultValue: e.defaultValue
967
- }), D(!1);
969
+ }), a(!1);
968
970
  },
969
- [K, R, L]
970
- ), Se = T(
971
+ [K, P, W]
972
+ ), Te = S(
971
973
  (e) => {
972
- M.current && (M.current(), M.current = null), H.current = null, L();
973
- const t = k.current?.activeEditor;
974
+ M.current && (M.current(), M.current = null), B.current = null, W();
975
+ const t = w.current?.activeEditor;
974
976
  if (!t) return;
975
977
  const i = t.helpers?.structuredContentCommands;
976
978
  if (!i) return;
@@ -990,64 +992,64 @@ const He = ({
990
992
  },
991
993
  text: e.alias
992
994
  })) {
993
- e.group || Z(e.id, { tag: C }), D(!1);
994
- const F = Q(t);
995
- B(F), y?.(F);
995
+ e.group || Z(e.id, { tag: C }), a(!1);
996
+ const F = J(t);
997
+ A(F), u?.(F);
996
998
  }
997
999
  },
998
- [Z, L, y]
999
- ), Te = T(() => {
1000
- D(!1), H.current = null, L(), M.current && (M.current(), M.current = null);
1001
- }, [L]), Ee = T(() => {
1002
- if (!k.current?.activeEditor || u.length === 0) return;
1003
- const e = u.findIndex((i) => i.id === V), t = e >= 0 ? (e + 1) % u.length : 0;
1004
- $(u[t].id);
1005
- }, [u, V, $]), Ie = T(() => {
1006
- if (!k.current?.activeEditor || u.length === 0) return;
1007
- const e = u.findIndex((i) => i.id === V), t = e > 0 ? e - 1 : u.length - 1;
1008
- $(u[t].id);
1009
- }, [u, V, $]), Me = T(
1000
+ [Z, W, u]
1001
+ ), Ee = S(() => {
1002
+ a(!1), B.current = null, W(), M.current && (M.current(), M.current = null);
1003
+ }, [W]), Ie = S(() => {
1004
+ if (!w.current?.activeEditor || g.length === 0) return;
1005
+ const e = g.findIndex((i) => i.id === R), t = e >= 0 ? (e + 1) % g.length : 0;
1006
+ $(g[t].id);
1007
+ }, [g, R, $]), Me = S(() => {
1008
+ if (!w.current?.activeEditor || g.length === 0) return;
1009
+ const e = g.findIndex((i) => i.id === R), t = e > 0 ? e - 1 : g.length - 1;
1010
+ $(g[t].id);
1011
+ }, [g, R, $]), Re = S(
1010
1012
  async (e) => {
1011
- const { fileName: t = "document", triggerDownload: i = !0 } = e || {}, v = await k.current?.export({
1013
+ const { fileName: t = "document", triggerDownload: i = !0 } = e || {}, v = await w.current?.export({
1012
1014
  exportType: ["docx"],
1013
1015
  exportedName: t,
1014
1016
  triggerDownload: i
1015
- }), C = k.current?.activeEditor;
1017
+ }), C = w.current?.activeEditor;
1016
1018
  if (C) {
1017
- const g = Q(C);
1018
- U?.({ fields: g, blob: i ? void 0 : v, fileName: t });
1019
+ const x = J(C);
1020
+ z?.({ fields: x, blob: i ? void 0 : v, fileName: t });
1019
1021
  }
1020
1022
  return v;
1021
1023
  },
1022
- [U]
1024
+ [z]
1023
1025
  );
1024
- We(l, () => ({
1026
+ He(l, () => ({
1025
1027
  insertField: (e) => K("inline", e),
1026
1028
  insertBlockField: (e) => K("block", e),
1027
1029
  updateField: Z,
1028
1030
  deleteField: ae,
1029
1031
  selectField: $,
1030
- nextField: Ee,
1031
- previousField: Ie,
1032
- getFields: () => u,
1033
- exportTemplate: Me,
1034
- getSuperDoc: () => k.current
1032
+ nextField: Ie,
1033
+ previousField: Me,
1034
+ getFields: () => g,
1035
+ exportTemplate: Re,
1036
+ getSuperDoc: () => w.current
1035
1037
  }));
1036
- const Re = c.component || He, xe = a.component || Pe;
1037
- return /* @__PURE__ */ o("div", { className: `superdoc-template-builder ${A || ""}`, style: X, children: [
1038
+ const De = p.component || Le, xe = c.component || Ae;
1039
+ return /* @__PURE__ */ o("div", { className: `superdoc-template-builder ${Q || ""}`, style: ie, children: [
1038
1040
  /* @__PURE__ */ o("div", { style: { display: "flex", gap: "20px" }, children: [
1039
- a.position === "left" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1041
+ c.position === "left" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1040
1042
  xe,
1041
1043
  {
1042
- fields: u,
1044
+ fields: g,
1043
1045
  onSelect: (e) => $(e.id),
1044
1046
  onDelete: ae,
1045
1047
  onUpdate: (e) => Z(e.id, e),
1046
- selectedFieldId: V || void 0
1048
+ selectedFieldId: R || void 0
1047
1049
  }
1048
1050
  ) }),
1049
1051
  /* @__PURE__ */ o("div", { className: "superdoc-template-builder-document", style: { flex: 1 }, children: [
1050
- G?.renderDefaultContainer && /* @__PURE__ */ r(
1052
+ j?.renderDefaultContainer && /* @__PURE__ */ r(
1051
1053
  "div",
1052
1054
  {
1053
1055
  id: "superdoc-toolbar",
@@ -1060,44 +1062,44 @@ const He = ({
1060
1062
  {
1061
1063
  ref: se,
1062
1064
  className: "superdoc-template-builder-editor",
1063
- style: { height: ie },
1065
+ style: { height: X },
1064
1066
  "data-testid": "template-builder-editor"
1065
1067
  }
1066
1068
  )
1067
1069
  ] }),
1068
- a.position === "right" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1070
+ c.position === "right" && /* @__PURE__ */ r("div", { className: "superdoc-template-builder-sidebar", children: /* @__PURE__ */ r(
1069
1071
  xe,
1070
1072
  {
1071
- fields: u,
1073
+ fields: g,
1072
1074
  onSelect: (e) => $(e.id),
1073
1075
  onDelete: ae,
1074
1076
  onUpdate: (e) => Z(e.id, e),
1075
- selectedFieldId: V || void 0
1077
+ selectedFieldId: R || void 0
1076
1078
  }
1077
1079
  ) })
1078
1080
  ] }),
1079
1081
  /* @__PURE__ */ r(
1080
- Re,
1082
+ De,
1081
1083
  {
1082
- isVisible: j,
1083
- position: h,
1084
+ isVisible: _,
1085
+ position: V,
1084
1086
  availableFields: s.available || [],
1085
- filteredFields: ee,
1086
- filterQuery: b,
1087
+ filteredFields: we,
1088
+ filterQuery: Y,
1087
1089
  allowCreate: s.allowCreate || !1,
1088
- onSelect: ke,
1089
- onClose: Te,
1090
- onCreateField: R,
1091
- existingFields: u,
1092
- onSelectExisting: Se
1090
+ onSelect: Se,
1091
+ onClose: Ee,
1092
+ onCreateField: P,
1093
+ existingFields: g,
1094
+ onSelectExisting: Te
1093
1095
  }
1094
1096
  )
1095
1097
  ] });
1096
1098
  }
1097
1099
  );
1098
- Ge.displayName = "SuperDocTemplateBuilder";
1100
+ Oe.displayName = "SuperDocTemplateBuilder";
1099
1101
  export {
1100
- Pe as FieldList,
1101
- He as FieldMenu,
1102
- Ge as default
1102
+ Ae as FieldList,
1103
+ Le as FieldMenu,
1104
+ Oe as default
1103
1105
  };
package/dist/types.d.ts CHANGED
@@ -99,6 +99,8 @@ export interface SuperDocTemplateBuilderProps {
99
99
  menu?: MenuConfig;
100
100
  list?: ListConfig;
101
101
  toolbar?: boolean | string | ToolbarConfig;
102
+ /** Content Security Policy nonce for dynamically injected styles */
103
+ cspNonce?: string;
102
104
  onReady?: () => void;
103
105
  onTrigger?: (event: TriggerEvent) => void;
104
106
  onFieldInsert?: (field: TemplateField) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdoc-dev/template-builder",
3
- "version": "1.1.0-next.9",
3
+ "version": "1.1.0",
4
4
  "description": "React template builder component for SuperDoc",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",