1import{r as o,j as a}from"./vendor-react-C_xdI0Ng.js";import{u as Wt}from"./index-DbFRh42P.js";import{d as $t}from"./preview-service-CUDLI67J.js";import{L as y}from"./linear-icon-BS_Bu1OG.js";import"./vendor-icons-CmfhFn4k.js";function I(s,c,R){return Math.max(c,Math.min(R,s))}function vt(s){return s.replace(/\r\n/gu,` 2`).replace(/\r/gu,` 3`).replace(/\n+/gu," ").replace(/\s+/gu," ").trim()}function ct(s){return s instanceof HTMLElement&&(s.isContentEditable||s instanceof HTMLInputElement||s instanceof HTMLTextAreaElement)}function At(s,c){const R=s.yRatio+(s.ascentRatio??0),f=Math.max(.004,(s.heightRatio??.02)*.65),m=c.filter(h=>Math.abs(h.yRatio+(h.ascentRatio??0)-R)<=f).sort((h,l)=>h.xRatio-l.xRatio);let j="",z=s.xRatio,C=s.yRatio,T=s.xRatio+s.widthRatio,N=s.yRatio+s.heightRatio;for(let h=0;h<m.length;h+=1){const l=m[h];if(!l)continue;const S=m[h-1];S&&l.xRatio-(S.xRatio+S.widthRatio)>Math.max(.002,l.heightRatio*.25)&&!j.endsWith(" ")&&!l.text.startsWith(" ")&&(j+=" "),j+=l.text,z=Math.min(z,l.xRatio),C=Math.min(C,l.yRatio),T=Math.max(T,l.xRatio+l.widthRatio),N=Math.max(N,l.yRatio+l.heightRatio)}const B=m.find(h=>h.id===s.id)??m[0]??s;return{spans:m,text:vt(j||s.text),rect:{x:z*100,y:C*100,w:Math.max(.1,(T-z)*100),h:Math.max(.1,(N-C)*100)},representative:B}}function bt(s,c){const R=(s.xRatio+s.widthRatio/2)*100,f=(s.yRatio+s.heightRatio/2)*100;return R>=c.x&&R<=c.x+c.w&&f>=c.y&&f<=c.y+c.h}function wt(s,c,R){const f=R.getBoundingClientRect();return{xRatio:I((s-f.left)/f.width*100,0,100),yRatio:I((c-f.top)/f.height*100,0,100)}}const Ut=2.1,jt=80,Ht={width:595,height:842};function Z(s){return`${s}-${crypto.randomUUID()}`}function _(s){return s.map(c=>c.type==="text"?{...c,originalRect:c.originalRect?{...c.originalRect}:void 0}:{...c})}function Nt(s){return JSON.stringify(s)}function Gt({inputFiles:s,onStart:c,onBack:R,onPickFiles:f,onClearFiles:m,currentStep:j="config",progress:z=0,outputCount:C=0,onDownload:T}){const{runtime:N}=Wt(),B=o.useRef(null),h=o.useRef(null),l=o.useRef(null),S=o.useRef(null),v=o.useRef([[]]),g=o.useRef(0),[lt,dt]=o.useState([]),[P,q]=o.useState(1),[J,Q]=o.use
3State(1),[ut,tt]=o.useState(null),[Y,pt]=o.useState(Ht),[X,ft]=o.useState(1),[kt,ht]=o.useState(!1),[xt,gt]=o.useState(null),[et,at]=o.useState([]),[d,U]=o.useState([]),[k,E]=o.useState(null),[L,M]=o.useState(null),[H,yt]=o.useState(null),[n,O]=o.useState(null),[Ft,K]=o.useState(0),[Ct,it]=o.useState(!1),b=s[0]??null,D=P-1,ot=o.useMemo(()=>d.filter(t=>t.pageIndex===D),[d,D]),V=o.useMemo(()=>ot.filter(t=>t.type==="text"),[ot]),u=o.useMemo(()=>d.find(t=>t.id===k)??null,[d,k]),St=g.current>0,Et=g.current<v.current.length-1,W=o.useCallback(t=>{const e=_(t),i=v.current[g.current]??[];if(Nt(i)===Nt(e))return;let r=v.current.slice(0,g.current+1);r.push(e),r.length>jt&&(r=r.slice(r.length-jt)),v.current=r,g.current=r.length-1,K(p=>p+1)},[]),w=o.useCallback((t,e=!0)=>{U(t),e&&W(t)},[W]),$=o.useCallback(t=>{E(t),M(t),yt(null),O(null)},[]),A=o.useCallback(()=>{const t=l.current;if(!t||!L)return;const e=vt(t.innerText);U(i=>{const r=i.map(p=>p.id===L&&p.type==="text"?{...p,text:e||p.text,backgroundColor:"#ffffff"}:p);return W(r),r}),M(null)},[L,W]),nt=o.useCallback(()=>{if(g.current<=0)return;g.current-=1;const t=_(v.current[g.current]??[]);U(t),E(e=>t.some(i=>i.id===e)?e:null),M(null),K(e=>e+1)},[]),st=o.useCallback(()=>{if(g.current>=v.current.length-1)return;g.current+=1;const t=_(v.current[g.current]??[]);U(t),E(e=>t.some(i=>i.id===e)?e:null),M(null),K(e=>e+1)},[]);o.useEffect(()=>{const t=_(d);v.current=[t],g.current=0,K(e=>e+1),E(null),M(null),q(1)},[b]),o.useEffect(()=>{let t=!1;return(async()=>{if(!b){dt([]);return}const e=await Promise.all(s.map(async i=>{try{return(await N.vfs.read(i)).getName()}catch{return i}}));t||dt(e)})(),()=>{t=!0}},[b,s,N.vfs]),o.useEffect(()=>{const t=new AbortController;return(async()=>{if(!b){tt(null),at([]),Q(1);return}ht(!0),gt(null);try{const e={id:crypto.randomUUID(),type:"COMMAND",payload:{type:"GET_PDF_TEXT_LAYER",payload:{fileId:b,pageNumber:P}}},[i,r]=await Promise.all([$t.getPdfPagePreview(N,b,P,{scale:Ut},t.signal),N.workerOrchestrator.dispatch(e,void 0,t.signal)]);if(t.signal.aborted)return;if(tt(i.thumbnailUrl),i.pageCount&&Q(i.pageCount),r.payload.type==="TEXT_LAYER_RESULT"){const{spans:p,width:x,height:zt,pageCount:Lt}=r.payload.payload;at(p),pt({width:x,height:zt}),Q(Lt)}else throw r.payload.type==="ERROR"?new Error(r.payload.payload.message):new Error("Unexpected worker response")}catch{t.signal.aborted||(gt("Could not load PDF preview."),tt(null),at([]))}finally{t.signal.aborted||ht(!1)}})(),()=>t.abort()},[b,P,N]),o.useEffect(()=>{if(!L||!l.current)return;const t=l.current;t.focus();const e=document.createRange();e.selectNodeContents(t),e.collapse(!1);const i=window.getSelection();i==null||i.removeAllRanges(),i==null||i.addRange(e)},[L]),o.useEffect(()=>{const t=e=>{if(ct(e.target)){e.key==="Escape"&&(e.preventDefault(),A());return}if((e.metaKey||e.ctrlKey)&&e.key.toLowerCase()==="z"){e.preventDefault(),e.shiftKey?st():nt();return}if((e.key==="Delete"||e.key==="Backspace")&&k){e.preventDefault();const r=d.filter(p=>p.id!==k);E(null),M(null),w(r)}};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)},[w,A,d,st,k,nt]);const Mt=o.useCallback(t=>{const e=At(t,et),i=V.find(x=>x.originalRect&&bt(t,x.originalRect));if(i){$(i.id);return}const r={type:"text",id:Z("text"),pageIndex:D,text:e.text||t.text,xRatio:e.rect.x,yRatio:e.rect.y,widthRatio:e.rect.w,heightRatio:e.rect.h,fontSize:Math.max(6,e.representative.fontSizeRatio*Y.height),fontFamily:e.representative.fontFamilyHint||"Roboto",color:"#000000",backgroundColor:"#ffffff",bold:/bold|black|heavy/iu.test(e.representative.fontName??""),italic:/italic|oblique/iu.test(e.representative.fontName??""),opacity:1,textAlign:"left",horizontalScaling:1,ascentRatio:e.representative.ascentRatio,descentRatio:e.representative.descentRatio,sourceFontSizeRatio:e.representative.fontSizeRatio,originalRect:e.rect},p=[...d,r];w(p),$(r.id)},[w,d,D,V,et,Y.height,$]),Rt=o.useCallback((t,e)=>{const i={type:"text",id:Z("text"),pageIndex:D,text:"Add text",xRatio:t,yRatio:e,widthRatio:28,heightRatio:3,fontSize:18,fontFamily:"Roboto",color:"#000000",backgroundColor:"#ffffff",bold:!1,italic:!1,opacity:1,textAlign:"left",horizontalScaling:1},r=[...d,i];w(r),$(i.id)},[w,d,D,$]),Dt=o.useCallback(t=>{if(t.button!==0||ct(t.target)||t.target!==t.currentTarget&&!(t.target instanceof HTMLImageElement))return;const e=wt(t.clientX,t.clientY,t.currentTarget);if(E(null),M(null),H){O({tool:H,pageIndex:D,startXRatio:e.xRatio,startYRatio:e.yRatio,currentXRatio:e.xRatio,currentYRatio:e.yRatio});return}Rt(e.xRatio,e.yRatio)},[Rt,D,H]),It=o.useCallback(t=>{const e=S.current;if(e){const i=(t.clientX-e.startClientX)/e.stageWidth*100,r=(t.clientY-e.startClientY)/e.stageHeight*100;U(p=>p.map(x=>x.id!==e.id||x.type!=="text"?x:{...x,xRatio:I(e.origXRatio+i,0,100-x.widthRatio),yRatio:I(e.origYRatio+r,0,100-x.heightRatio)}));return}if(n){const i=wt(t.clientX,t.clientY,t.currentTarget);O({...n,currentXRatio:i.xRatio,currentYRatio:i.yRatio})}},[n]),mt=o.useCallback(()=>
3{if(S.current&&(S.current=null,W(d)),n){const t=Math.min(n.startXRatio,n.currentXRatio),e=Math.min(n.startYRatio,n.currentYRatio),i=Math.abs(n.currentXRatio-n.startXRatio),r=Math.abs(n.currentYRatio-n.startYRatio);if(i>=.4||r>=.4){const p=n.tool==="line"?{type:"line",id:Z("line"),pageIndex:n.pageIndex,x1Ratio:n.startXRatio,y1Ratio:n.startYRatio,x2Ratio:n.currentXRatio,y2Ratio:n.currentYRatio,color:"#000000",strokeWidth:2,opacity:1}:{type:n.tool,id:Z(n.tool),pageIndex:n.pageIndex,xRatio:t,yRatio:e,widthRatio:Math.max(i,.4),heightRatio:Math.max(r,.4),color:n.tool==="whiteout"?"#ffffff":"#000000",strokeWidth:n.tool==="whiteout"?0:2,opacity:1},x=[...d,p];E(p.id),w(x)}O(null)}},[w,n,d,W]),Tt=o.useCallback((t,e)=>{if(t.button!==0||e.type!=="text"||ct(t.target))return;const i=h.current;if(!i)return;t.stopPropagation();const r=i.getBoundingClientRect();S.current={id:e.id,startClientX:t.clientX,startClientY:t.clientY,origXRatio:e.xRatio,origYRatio:e.yRatio,stageWidth:r.width,stageHeight:r.height},E(e.id),M(null)},[]),G=o.useCallback(async t=>{const e=Array.from(t).filter(i=>i.type==="application/pdf"||i.name.toLowerCase().endsWith(".pdf"));e.length>0&&await(f==null?void 0:f(e))},[f]),Pt=o.useCallback(t=>{t.target.files&&(G(t.target.files),t.target.value="")},[G]),Yt=o.useCallback(t=>{t.preventDefault(),it(!1),G(t.dataTransfer.files)},[G]),Xt=o.useCallback(()=>{A(),c({elements:_(d)})},[A,d,c]),rt=o.useCallback(t=>{if(!k)return;const e=d.map(i=>i.id===k&&i.type==="text"?{...i,...t,backgroundColor:"#ffffff"}:i);w(e)},[w,d,k]),F=n?{left:Math.min(n.startXRatio,n.currentXRatio),top:Math.min(n.startYRatio,n.currentYRatio),width:Math.abs(n.currentXRatio-n.startXRatio),height:Math.abs(n.currentYRatio-n.startYRatio)}:null;return a.jsx("div",{className:"tool-config-root pdf-editor-concept-root",children:a.jsxs("div",{className:"ocr-concept-workspace",children:[a.jsxs("section",{className:"tool-config-card ocr-concept-left pdf-editor-left",children:[a.jsx("h3",{className:"pdf-editor-title",children:"PDF Text Editor"}),a.jsxs("div",{className:`ocr-concept-upload ${Ct?"dragging":""}`,onDragOver:t=>{t.preventDefault(),it(!0)},onDragLeave:()=>it(!1),onDrop:Yt,onClick:()=>{var t;return(t=B.current)==null?void 0:t.click()},children:[a.jsx("input",{ref:B,type:"file",accept:"application/pdf,.pdf",multiple:!0,hidden:!0,onChange:Pt}),a.jsx("div",{className:"ocr-concept-upload-icon",children:a.jsx(y,{name:"upload",className:"linear-icon icon-lg"})}),a.jsx("p",{className:"ocr-concept-upload-title",children:b?"Selected PDF":"Drop PDF here"}),a.jsx("p",{className:"ocr-concept-upload-copy",children:lt[0]??"Choose a local PDF file"})]}),s.length>0&&a.jsxs("div",{className:"ocr-concept-file-chip",children:[a.jsx("span",{className:"ocr-concept-file-name",children:lt.join(", ")}),a.jsx("button",{className:"ocr-concept-clear-btn",type:"button",onClick:()=>
3void(m==null?void 0:m()),children:a.jsx(y,{name:"x",className:"linear-icon"})})]}),a.jsxs("div",{className:"ocr-concept-settings",children:[a.jsxs("label",{className:"pdf-editor-field",children:[a.jsx("span",{children:"Text"}),a.jsx("textarea",{className:"tool-config-input pdf-editor-textarea",disabled:!u||u.type!=="text",value:(u==null?void 0:u.type)==="text"?u.text:"",onChange:t=>rt({text:t.target.value})})]}),a.jsxs("div",{className:"pdf-editor-color-row",children:[a.jsxs("label",{className:"pdf-editor-field",children:[a.jsx("span",{children:"Color"}),a.jsx("input",{className:"tool-config-input",type:"color",disabled:!u||u.type!=="text",value:(u==null?void 0:u.type)==="text"?u.color:"#000000",onChange:t=>rt({color:t.target.value})})]}),a.jsxs("label",{className:"pdf-editor-field",children:[a.jsx("span",{children:"Size"}),a.jsx("input",{className:"tool-config-input",type:"number",min:6,max:144,disabled:!u||u.type!=="text",value:(u==null?void 0:u.type)==="text"?Math.round(u.fontSize):16,onChange:t=>rt({fontSize:I(Number(t.target.value),6,144)})})]})]})]}),xt&&a.jsx("p",{className:"pdf-editor-warning",children:xt}),j==="processing"&&a.jsx("div",{className:"ocr-concept-progress-track",children:a.jsx("span",{style:{width:`${I(z,0,100)}%`}})}),j==="result"&&C>0&&a.jsxs("p",{className:"tool-config-copy",children:[C," edited PDF ready."]}),a.jsxs("div",{className:"tool-config-actions ocr-concept-actions",children:[a.jsx("button",{className:"btn-ghost",type:"button",onClick:R,children:a.jsxs("span",{className:"btn-inline",children:[a.jsx(y,{name:"chevron-left",className:"linear-icon"}),"Back"]})}),j==="result"&&C>0?a.jsx("button",{className:"btn-primary",type:"button",onClick:()=>void(T==null?void 0:T()),children:a.jsxs("span",{className:"btn-inline",children:[a.jsx(y,{name:"download",className:"linear-icon"}),"Download"]})}):a.jsx("button",{className:"btn-primary",type:"button",disabled:!b||j==="processing",onClick:Xt,children:a.jsxs("span",{className:"btn-inline",children:[a.jsx(y,{name:"check",className:"linear-icon"}),"Save PDF"]})})]})]}),a.jsxs("section",{className:"tool-config-card ocr-concept-right pdf-editor-right",children:[a.jsxs("div",{className:"ocr-concept-toolbar pdf-editor-toolbar",children:[a.jsxs("div",{className:"pdf-editor-pager",children:[a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",disabled:P<=1,onClick:()=>q(t=>Math.max(1,t-1)),children:a.jsx(y,{name:"chevron-left",className:"linear-icon"})}),a.jsxs("span",{className:"pdf-editor-page-copy",children:["Page ",P,"/",J]}),a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",disabled:P>=J,onClick:()=>q(t=>Math.min(J,t+1)),children:a.jsx(y,{name:"chevron-right",className:"linear-icon"})})]}),a.jsxs("div",{className:"pdf-editor-toolbar-right",children:[a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",disabled:!St,onClick:nt,title:"Undo",children:a.jsx(y,{name:"rotate",className:"linear-icon"})}),a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",disabled:!Et,onClick:st,title:"Redo",children:a.jsx(y,{name:"refresh",className:"linear-icon"})}),["rect","circle","line","whiteout"].map(t=>a.jsx("button",{className:`ocr-concept-tool-btn ${H===t?"pdf-editor-btn-active":""}`,type:"button",onClick:()=>yt(e=>e===t?null:t),title:t,children:a.jsx(y,{name:t==="whiteout"?"eraser":t==="line"?"minus":"shape",className:"linear-icon"})},t)),a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",onClick:()=>ft(t=>I(t-.1,.5,2.5)),children:a.jsx(y,{name:"minus",className:"linear-icon"})}),a.jsxs("span",{className:"pdf-editor-zoom-copy",children:[Math.round(X*100),"%"]}),a.jsx("button",{className:"ocr-concept-tool-btn",type:"button",onClick:()=>ft(t=>I(t+.1,.5,2.5)),children:a.jsx(y,{name:"plus",className:"linear-icon"})})]})]}),a.jsx("div",{className:"pdf-editor-preview-scroll",children:b?a.jsxs("div",{ref:h,className:`pdf-editor-preview-stage ${H?"pdf-editor-preview-add":""}`,style:{width:Y.width*X,minHeight:Y.height*X},onMouseDown:Dt,onMouseMove:It,onMouseUp:mt,onMouseLeave:mt,children:[ut&&a.jsx("img",{className:"pdf-editor-preview-image",src:ut,alt:"PDF page preview",draggable:!1,style:{width:Y.width*X,height:Y.height*X},onLoad:t=>{const e=t.currentTarget;
3e.naturalWidth>0&&e.naturalHeight>0&&pt({width:e.naturalWidth,height:e.naturalHeight})}}),a.jsx("div",{className:"pdf-editor-text-layer",children:et.map(t=>{const e=V.some(i=>i.originalRect&&bt(t,i.originalRect));return a.jsx("button",{className:"pdf-editor-text-span",type:"button",disabled:e,onClick:i=>{i.stopPropagation(),Mt(t)},style:{left:`${t.xRatio*100}%`,top:`${t.yRatio*100}%`,width:`${t.widthRatio*100}%`,height:`${t.heightRatio*100}%`,opacity:e?0:1,pointerEvents:e?"none":"auto",fontSize:`${Math.max(1,t.fontSizeRatio*Y.height*X)}px`},"aria-label":`Edit text ${t.text}`},t.id)})}),a.jsx("div",{className:"pdf-editor-shape-layer",children:a.jsxs("svg",{viewBox:"0 0 100 100",preserveAspectRatio:"none",children:[ot.map(t=>{if(t.type==="line")return a.jsx("line",{x1:t.x1Ratio,y1:t.y1Ratio,x2:t.x2Ratio,y2:t.y2Ratio,stroke:t.color,strokeWidth:t.strokeWidth/8,opacity:t.opacity},t.id);if(t.type==="text")return null;const e={key:t.id,stroke:t.type==="whiteout"?"#ffffff":t.color,strokeWidth:t.strokeWidth/8,opacity:t.opacity,fill:t.type==="whiteout"?"#ffffff":"transparent"};return t.type==="circle"?a.jsx("ellipse",{...e,cx:t.xRatio+t.widthRatio/2,cy:t.yRatio+t.heightRatio/2,rx:t.widthRatio/2,ry:t.heightRatio/2}):a.jsx("rect",{...e,x:t.xRatio,y:t.yRatio,width:t.widthRatio,height:t.heightRatio})}),F&&n&&(n.tool==="line"?a.jsx("line",{x1:n.startXRatio,y1:n.startYRatio,x2:n.currentXRatio,y2:n.currentYRatio,stroke:"#2563eb",strokeWidth:.25}):a.jsx("rect",{x:F.left,y:F.top,width:F.width,height:F.height,fill:n.tool==="whiteout"?"#ffffff":"transparent",stroke:"#2563eb",strokeWidth:.25}))]})}),V.map(t=>{const e=k===t.id,i=L===t.id;return a.jsx("div",{className:`pdf-editor-overlay ${e?"active":""}`,onMouseDown:r=>Tt(r,t),onDoubleClick:r=>{r.stopPropagation(),$(t.id)},style:{left:`${t.xRatio}%`,top:`${t.yRatio}%`,width:`${t.widthRatio}%`,height:`${t.heightRatio}%`,backgroundColor:"#ffffff",color:t.color,opacity:t.opacity,fontFamily:t.fontFamily,fontSize:`${t.fontSize*X}px`,fontWeight:t.bold?700:400,fontStyle:t.italic?"italic":"normal",textAlign:t.textAlign,cursor:e?"grab":"pointer",zIndex:e?8:6},children:i?a.jsx("div",{ref:l,className:"pdf-editor-overlay-input",contentEditable:!0,suppressContentEditableWarning:!0,onBlur:A,onKeyDown:r=>{r.key==="Escape"&&(r.preventDefault(),A())},children:t.text}):a.jsx("span",{className:"pdf-editor-overlay-text",children:t.text})},t.id)}),kt&&a.jsx("div",{className:"pdf-editor-preview-loading",children:"Loading"})]}):a.jsxs("div",{className:"ocr-concept-empty",children:[a.jsx("p",{className:"ocr-concept-empty-title",children:"No PDF selected"}),a.jsx("p",{className:"ocr-concept-empty-copy",children:"Upload a PDF to edit visible text locally."})]})})]})]})})}export{Gt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.