@superdoc-dev/esign 1.4.2 → 1.5.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 le=Object.create;var z=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var pe=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty;var me=(c,d,u,s)=>{if(d&&typeof d=="object"||typeof d=="function")for(let n of de(d))!ge.call(c,n)&&n!==u&&z(c,n,{get:()=>d[n],enumerable:!(s=ue(d,n))||s.enumerable});return c};var fe=(c,d,u)=>(u=c!=null?le(pe(c)):{},me(d||!c||!c.__esModule?z(u,"default",{value:c,enumerable:!0}):u,c));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const l=require("react/jsx-runtime"),i=require("react"),L=({value:c,onChange:d,isDisabled:u,label:s})=>l.jsxs("div",{className:"superdoc-esign-signature-input",style:{display:"flex",flexDirection:"column",gap:"8px"},children:[s&&l.jsx("label",{children:s}),l.jsx("input",{type:"text",value:String(c||""),onChange:n=>d(n.target.value),disabled:u,placeholder:"Type your full name",style:{fontFamily:"cursive",fontSize:"18px"}})]}),P=({value:c,onChange:d,isDisabled:u,label:s})=>l.jsxs("label",{className:"superdoc-esign-checkbox-input",style:{display:"flex",gap:"8px"},children:[l.jsx("input",{type:"checkbox",checked:!!c,onChange:n=>d(n.target.checked),disabled:u}),l.jsx("span",{children:s})]}),he=c=>({onClick:u,fileName:s,isDisabled:n})=>{const g=c?.label||"Download";return l.jsxs("button",{onClick:u,disabled:n,className:"superdoc-esign-btn superdoc-esign-btn--download",style:{padding:"8px 16px",borderRadius:"6px",border:"1px solid #d0d5dd",background:"#ffffff",color:"#333",cursor:n?"not-allowed":"pointer",opacity:n?.5:1,fontSize:"16px",fontWeight:"bold"},children:[g," ",s&&`(${s})`]})},ve=c=>({onClick:u,isValid:s,isDisabled:n,isSubmitting:g})=>{const C=()=>c?.label||"Submit";return l.jsx("button",{onClick:u,disabled:!s||n||g,className:"superdoc-esign-btn superdoc-esign-btn--submit",style:{padding:"12px 24px",borderRadius:"6px",border:"none",background:"#007bff",color:"#fff",cursor:!s||n?"not-allowed":"pointer",opacity:!s||n?.5:1,fontSize:"16px",fontWeight:"bold"},children:C()})},W=i.forwardRef((c,d)=>{const{eventId:u,document:s,fields:n={},download:g,submit:C,onSubmit:R,onDownload:k,onStateChange:E,onFieldChange:T,onFieldsDiscovered:N,isDisabled:f=!1,className:$,style:K,documentHeight:X="600px"}=c,[x,F]=i.useState(!s.validation?.scroll?.required),[p,M]=i.useState(new Map),[b,I]=i.useState(!1),[h,A]=i.useState(!1),[q,_]=i.useState([]),[B,Y]=i.useState(!1),w=i.useRef(null),D=i.useRef(null),G=i.useRef(Date.now()),v=i.useRef(n),y=i.useRef([]),U=i.useRef(N);v.current=n,U.current=N,i.useEffect(()=>{y.current=q},[q]);const j=i.useCallback(e=>{if(!D.current?.activeEditor)return;const t=D.current.activeEditor,r=v.current.signer?.find(o=>o.id===e.id);let a;r?.type==="signature"&&e.value?a={json:{type:"image",attrs:{src:typeof e.value=="string"&&e.value.startsWith("data:image/")?e.value:J(String(e.value)),alt:"Signature"}}}:a={text:String(e.value??"")},e.id&&t.commands.updateStructuredContentById(e.id,a)},[]);function J(e){const t=globalThis.document.createElement("canvas"),r=t.getContext("2d"),a=30;r.font=`italic ${a}px cursive`;const m=r.measureText(e).width,ie=a*1.3,ae=4,ce=6;return t.width=Math.ceil(m+ae*2)+20,t.height=Math.ceil(ie+ce*2),r.font=`italic ${a}px cursive`,r.fillStyle="black",r.textAlign="center",r.textBaseline="middle",r.fillText(e,t.width/2,t.height/2),t.toDataURL("image/png")}const V=i.useCallback(e=>{if(!e)return;const t=e.helpers.structuredContentCommands.getStructuredContentTags(e.state),r=new Map;v.current.document?.forEach(o=>{o.id&&r.set(o.id,o.value)}),v.current.signer?.forEach(o=>{o.value!==void 0&&r.set(o.id,o.value)});const a=t.map(({node:o})=>({id:o.attrs.id,label:o.attrs.label,value:r.get(o.attrs.id)??o.textContent??""})).filter(o=>o.id);a.length>0&&(U.current?.(a),[...v.current.document||[],...v.current.signer||[]].filter(m=>m.value!==void 0).forEach(m=>j({id:m.id,value:m.value})))},[j]),S=e=>{const t={...e,timestamp:new Date().toISOString()},r=globalThis?.__SUPERDOC_AUDIT_MOCK__;r&&r(t);const a=[...y.current,t];return y.current=a,_(a),a};i.useEffect(()=>{if(!w.current)return;let e=!1,t=null;return(async()=>{const{SuperDoc:a}=await import("superdoc");e||(t=new a({selector:w.current,document:s.source,documentMode:"viewing",modules:{comments:!1},onReady:()=>{e||(t?.activeEditor&&V(t.activeEditor),S({type:"ready"}),Y(!0))}}),D.current=t)})(),()=>{e=!0,t&&typeof t.destroy=="function"&&t.destroy(),D.current=null}},[s.source,s.mode,V]),i.useEffect(()=>{if(!s.validation?.scroll?.required||!B)return;const e=w.current;if(!e)return;const t=()=>{const{scrollTop:r,scrollHeight:a,clientHeight:o}=e,m=r/(a-o);(m>=.95||a<=o)&&(F(!0),S({type:"scroll",data:{percent:Math.round(m*100)}}))};return e.addEventListener("scroll",t),t(),()=>e.removeEventListener("scroll",t)},[s.validation?.scroll?.required,B]);const Q=i.useCallback((e,t)=>{M(r=>{const a=r.get(e),o=new Map(r);return o.set(e,t),j({id:e,value:t}),S({type:"field_change",data:{fieldId:e,value:t,previousValue:a}}),T?.({id:e,value:t,previousValue:a}),o})},[T,j]),H=i.useCallback(()=>s.validation?.scroll?.required&&!x?!1:(n.signer||[]).every(e=>{if(!e.validation?.required)return!0;const t=p.get(e.id);return t&&(typeof t!="string"||t.trim())}),[x,n.signer,p,s.validation?.scroll?.required]);i.useEffect(()=>{const e=H();I(e),E?.({scrolled:x,fields:p,isValid:e,isSubmitting:h})},[x,p,h,H,E]);const Z=i.useCallback(async()=>{if(f)return;const e={eventId:u,documentSource:s.source,fields:{document:n.document||[],signer:(n.signer||[]).map(t=>({id:t.id,value:p.get(t.id)??null}))},fileName:g?.fileName||"document.pdf"};await k?.(e)},[f,u,s.source,n,p,g,k]),ee=i.useCallback(async()=>{if(!b||f||h)return;A(!0),S({type:"submit"});const e=S({type:"submit"}),t={eventId:u,timestamp:new Date().toISOString(),duration:Math.floor((Date.now()-G.current)/1e3),auditTrail:e,documentFields:n.document||[],signerFields:(n.signer||[]).map(r=>({id:r.id,value:p.get(r.id)??null})),isFullyCompleted:b};try{await R(t)}finally{A(!1)}},[b,f,h,u,n,p,R]),te=e=>{const t=e.component||ne(e.type);return l.jsx(t,{value:p.get(e.id)??null,onChange:r=>Q(e.id,r),isDisabled:f,label:e.label},e.id)},ne=e=>{switch(e){case"signature":case"text":return L;case"checkbox":return P}},se=()=>{const e=g?.component||he(g);return e?l.jsx(e,{onClick:Z,fileName:g?.fileName,isDisabled:f}):null},re=()=>{if(s.mode==="download")return null;const e=C?.component||ve(C);return l.jsx("div",{className:"superdoc-esign-actions superdoc-esign-form-actions",children:l.jsx(e,{onClick:ee,isValid:b,isDisabled:f,isSubmitting:h})})},O=se(),oe=re();return i.useImperativeHandle(d,()=>({getState:()=>({scrolled:x,fields:p,isValid:b,isSubmitting:h}),getAuditTrail:()=>y.current,reset:()=>{F(!s.validation?.scroll?.required),M(new Map),I(!1),y.current=[],_([])}})),l.jsxs("div",{className:`superdoc-esign-container ${$||""}`,style:K,children:[l.jsxs("div",{className:"superdoc-esign-document","data-testid":"superdoc-esign-document",children:[O&&l.jsx("div",{className:"superdoc-esign-document-toolbar",children:l.jsx("div",{className:"superdoc-esign-document-controls",children:O})}),l.jsx("div",{ref:w,className:"superdoc-esign-document-viewer","data-testid":"superdoc-scroll-container",style:{height:X,overflow:"auto"}})]}),l.jsxs("div",{className:"superdoc-esign-controls","data-testid":"superdoc-esign-controls",children:[n.signer&&n.signer.length>0&&l.jsx("div",{className:"superdoc-esign-fields","data-testid":"superdoc-esign-fields",children:n.signer.map(te)}),oe]})]})});W.displayName="SuperDocESign";exports.CheckboxInput=P;exports.SignatureInput=L;exports.default=W;
1
+ "use strict";var de=Object.create;var P=Object.defineProperty;var pe=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var me=Object.getPrototypeOf,fe=Object.prototype.hasOwnProperty;var he=(l,p,u,n)=>{if(p&&typeof p=="object"||typeof p=="function")for(let s of ge(p))!fe.call(l,s)&&s!==u&&P(l,s,{get:()=>p[s],enumerable:!(n=pe(p,s))||n.enumerable});return l};var ve=(l,p,u)=>(u=l!=null?de(me(l)):{},he(p||!l||!l.__esModule?P(u,"default",{value:l,enumerable:!0}):u,l));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c=require("react/jsx-runtime"),a=require("react"),W=({value:l,onChange:p,isDisabled:u,label:n})=>c.jsxs("div",{className:"superdoc-esign-signature-input",style:{display:"flex",flexDirection:"column",gap:"8px"},children:[n&&c.jsx("label",{children:n}),c.jsx("input",{type:"text",value:String(l||""),onChange:s=>p(s.target.value),disabled:u,placeholder:"Type your full name",style:{fontFamily:"cursive",fontSize:"18px"}})]}),L=({value:l,onChange:p,isDisabled:u,label:n})=>c.jsxs("label",{className:"superdoc-esign-checkbox-input",style:{display:"flex",gap:"8px"},children:[c.jsx("input",{type:"checkbox",checked:!!l,onChange:s=>p(s.target.checked),disabled:u}),c.jsx("span",{children:n})]}),xe=l=>({onClick:u,fileName:n,isDisabled:s,isDownloading:d})=>{const x=l?.label||"Download",m=s||d;return c.jsxs("button",{onClick:u,disabled:m,className:`superdoc-esign-btn superdoc-esign-btn--download${d?" superdoc-esign-btn--loading":""}`,style:{padding:"8px 16px",borderRadius:"6px",border:"1px solid #d0d5dd",background:"#ffffff",color:"#333",cursor:m?"not-allowed":"pointer",opacity:m?.7:1,fontSize:"16px",fontWeight:"bold",display:"inline-flex",alignItems:"center",gap:"8px",transition:"opacity 0.2s ease"},children:[d&&c.jsx("span",{className:"superdoc-esign-spinner"}),d?"Downloading...":x,!d&&n&&` (${n})`]})},ye=l=>({onClick:u,isValid:n,isDisabled:s,isSubmitting:d})=>{const x=l?.label||"Submit",m=!n||s||d;return c.jsxs("button",{onClick:u,disabled:m,className:`superdoc-esign-btn superdoc-esign-btn--submit${d?" superdoc-esign-btn--loading":""}`,style:{padding:"12px 24px",borderRadius:"6px",border:"none",background:"#007bff",color:"#fff",cursor:m?"not-allowed":"pointer",opacity:m&&!d?.5:1,fontSize:"16px",fontWeight:"bold",display:"inline-flex",alignItems:"center",gap:"8px",transition:"opacity 0.2s ease"},children:[d&&c.jsx("span",{className:"superdoc-esign-spinner superdoc-esign-spinner--light"}),d?"Submitting...":x]})},K=a.forwardRef((l,p)=>{const{eventId:u,document:n,fields:s={},download:d,submit:x,onSubmit:m,onDownload:k,onStateChange:E,onFieldChange:N,onFieldsDiscovered:T,isDisabled:h=!1,className:X,style:Y,documentHeight:G="600px"}=l,[y,F]=a.useState(!n.validation?.scroll?.required),[g,I]=a.useState(new Map),[b,q]=a.useState(!1),[v,A]=a.useState(!1),[R,_]=a.useState(!1),[V,B]=a.useState([]),[U,J]=a.useState(!1),j=a.useRef(null),M=a.useRef(null),Q=a.useRef(Date.now()),S=a.useRef(s),w=a.useRef([]),$=a.useRef(T);S.current=s,$.current=T,a.useEffect(()=>{w.current=V},[V]);const C=a.useCallback(e=>{if(!M.current?.activeEditor)return;const t=M.current.activeEditor,o=S.current.signer?.find(r=>r.id===e.id);let i;o?.type==="signature"&&e.value?i={json:{type:"image",attrs:{src:typeof e.value=="string"&&e.value.startsWith("data:image/")?e.value:Z(String(e.value)),alt:"Signature"}}}:i={text:String(e.value??"")},e.id&&t.commands.updateStructuredContentById(e.id,i)},[]);function Z(e){const t=globalThis.document.createElement("canvas"),o=t.getContext("2d"),i=30;o.font=`italic ${i}px cursive`;const f=o.measureText(e).width,ce=i*1.3,le=4,ue=6;return t.width=Math.ceil(f+le*2)+20,t.height=Math.ceil(ce+ue*2),o.font=`italic ${i}px cursive`,o.fillStyle="black",o.textAlign="center",o.textBaseline="middle",o.fillText(e,t.width/2,t.height/2),t.toDataURL("image/png")}const H=a.useCallback(e=>{if(!e)return;const t=e.helpers.structuredContentCommands.getStructuredContentTags(e.state),o=new Map;S.current.document?.forEach(r=>{r.id&&o.set(r.id,r.value)}),S.current.signer?.forEach(r=>{r.value!==void 0&&o.set(r.id,r.value)});const i=t.map(({node:r})=>({id:r.attrs.id,label:r.attrs.label,value:o.get(r.attrs.id)??r.textContent??""})).filter(r=>r.id);i.length>0&&($.current?.(i),[...S.current.document||[],...S.current.signer||[]].filter(f=>f.value!==void 0).forEach(f=>C({id:f.id,value:f.value})))},[C]),D=e=>{const t={...e,timestamp:new Date().toISOString()},o=globalThis?.__SUPERDOC_AUDIT_MOCK__;o&&o(t);const i=[...w.current,t];return w.current=i,B(i),i};a.useEffect(()=>{if(!j.current)return;let e=!1,t=null;return(async()=>{const{SuperDoc:i}=await import("superdoc");e||(t=new i({selector:j.current,document:n.source,documentMode:"viewing",modules:{comments:!1},layoutMode:n.layoutMode,layoutMargins:n.layoutMargins,onReady:()=>{e||(t?.activeEditor&&H(t.activeEditor),D({type:"ready"}),J(!0))}}),M.current=t)})(),()=>{e=!0,t&&typeof t.destroy=="function"&&t.destroy(),M.current=null}},[n.source,n.mode,n.layoutMode,n.layoutMargins?.top,n.layoutMargins?.bottom,n.layoutMargins?.left,n.layoutMargins?.right,H]),a.useEffect(()=>{if(!n.validation?.scroll?.required||!U)return;const e=j.current;if(!e)return;const t=()=>{const{scrollTop:o,scrollHeight:i,clientHeight:r}=e,f=o/(i-r);(f>=.95||i<=r)&&(F(!0),D({type:"scroll",data:{percent:Math.round(f*100)}}))};return e.addEventListener("scroll",t),t(),()=>e.removeEventListener("scroll",t)},[n.validation?.scroll?.required,U]);const ee=a.useCallback((e,t)=>{I(o=>{const i=o.get(e),r=new Map(o);return r.set(e,t),C({id:e,value:t}),D({type:"field_change",data:{fieldId:e,value:t,previousValue:i}}),N?.({id:e,value:t,previousValue:i}),r})},[N,C]),O=a.useCallback(()=>n.validation?.scroll?.required&&!y?!1:(s.signer||[]).every(e=>{if(!e.validation?.required)return!0;const t=g.get(e.id);return t&&(typeof t!="string"||t.trim())}),[y,s.signer,g,n.validation?.scroll?.required]);a.useEffect(()=>{const e=O();q(e),E?.({scrolled:y,fields:g,isValid:e,isSubmitting:v})},[y,g,v,O,E]);const te=a.useCallback(async()=>{if(h||R)return;_(!0);const e={eventId:u,documentSource:n.source,fields:{document:s.document||[],signer:(s.signer||[]).map(t=>({id:t.id,value:g.get(t.id)??null}))},fileName:d?.fileName||"document.pdf"};try{await k?.(e)}finally{_(!1)}},[h,R,u,n.source,s,g,d,k]),ne=a.useCallback(async()=>{if(!b||h||v)return;A(!0),D({type:"submit"});const e=D({type:"submit"}),t={eventId:u,timestamp:new Date().toISOString(),duration:Math.floor((Date.now()-Q.current)/1e3),auditTrail:e,documentFields:s.document||[],signerFields:(s.signer||[]).map(o=>({id:o.id,value:g.get(o.id)??null})),isFullyCompleted:b};try{await m(t)}finally{A(!1)}},[b,h,v,u,s,g,m]),se=e=>{const t=e.component||oe(e.type);return c.jsx(t,{value:g.get(e.id)??null,onChange:o=>ee(e.id,o),isDisabled:h,label:e.label},e.id)},oe=e=>{switch(e){case"signature":case"text":return W;case"checkbox":return L}},re=()=>{const e=d?.component||xe(d);return e?c.jsx(e,{onClick:te,fileName:d?.fileName,isDisabled:h,isDownloading:R}):null},ae=()=>{if(n.mode==="download")return null;const e=x?.component||ye(x);return c.jsx("div",{className:"superdoc-esign-actions superdoc-esign-form-actions",children:c.jsx(e,{onClick:ne,isValid:b,isDisabled:h,isSubmitting:v})})},z=re(),ie=ae();return a.useImperativeHandle(p,()=>({getState:()=>({scrolled:y,fields:g,isValid:b,isSubmitting:v}),getAuditTrail:()=>w.current,reset:()=>{F(!n.validation?.scroll?.required),I(new Map),q(!1),w.current=[],B([])},updateFieldInDocument:C}),[y,g,b,v,n.validation?.scroll?.required,C]),c.jsxs("div",{className:`superdoc-esign-container ${X||""}`,style:Y,children:[c.jsxs("div",{className:"superdoc-esign-document","data-testid":"superdoc-esign-document",children:[z&&c.jsx("div",{className:"superdoc-esign-document-toolbar",children:c.jsx("div",{className:"superdoc-esign-document-controls",children:z})}),c.jsx("div",{ref:j,className:"superdoc-esign-document-viewer","data-testid":"superdoc-scroll-container",style:{height:G,overflow:"auto"}})]}),c.jsxs("div",{className:"superdoc-esign-controls","data-testid":"superdoc-esign-controls",children:[s.signer&&s.signer.length>0&&c.jsx("div",{className:"superdoc-esign-fields","data-testid":"superdoc-esign-fields",children:s.signer.map(se)}),ie]})]})});K.displayName="SuperDocESign";exports.CheckboxInput=L;exports.SignatureInput=W;exports.default=K;