@gouvfr-lasuite/ui-kit 0.22.0 → 0.23.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/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.22.0",
7
+ "version": "0.23.0",
8
8
  "type": "module",
9
9
  "main": "./dist/index.cjs",
10
10
  "module": "./dist/index.js",
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-Bwp4cywr.cjs"),t=require("react"),$=require("@tanstack/react-query"),D=require("react-pdf");require("react-pdf/dist/Page/AnnotationLayer.css");require("react-pdf/dist/Page/TextLayer.css");require("react-virtualized/styles.css");const M=require("react-virtualized"),F=require("@gouvfr-lasuite/cunningham-react");typeof URL.parse>"u"&&(URL.parse=function(s,n){try{return new URL(s,n)}catch{return null}});const W=Promise;typeof W.withResolvers>"u"&&(W.withResolvers=function(){let s,n;return{promise:new Promise((l,o)=>{s=l,n=o}),resolve:s,reject:n}});const ee=({numPages:s,currentPage:n,scrollToPage:r})=>{const[l,o]=t.useState("1"),c=t.useCallback(x=>{const g=Math.max(1,Math.min(s,x));o(String(g)),r(g)},[s,r]),p=t.useCallback(({numPages:x})=>(o("1"),x),[]),E=x=>{o(x.target.value)},h=()=>{const x=parseInt(l,10);if(isNaN(x)){o(String(n));return}c(x)},f=x=>{x.key==="Enter"&&h()},w=t.useRef(c);t.useEffect(()=>{w.current=c},[c]);const b=t.useCallback(x=>{w.current(x.pageNumber)},[]);return{goToPage:c,onDocumentLoadSuccess:p,pageInputValue:l,setPageInputValue:o,handlePageInputChange:E,handlePageInputSubmit:h,handlePageInputKeyDown:f,onItemClick:b}},L=1.414;function te(){const s=t.useRef(null),n=t.useRef(new Map),r=t.useRef(new Map),[l,o]=t.useState(()=>new Map),c=t.useCallback(f=>{s.current=f,n.current=new Map,r.current=new Map,o(new Map)},[]),p=t.useCallback(f=>{const w=s.current;if(!w||r.current.has(f))return Promise.resolve();const b=n.current.get(f);if(b)return b;const x=w.getPage(f).then(g=>{const u=g.getViewport({scale:1}),i=new Map(r.current);i.set(f,{w:u.width,h:u.height}),r.current=i,o(i)}).catch(g=>{console.warn(`Failed to fetch dimensions for PDF page ${f}`,g)}).finally(()=>{n.current.delete(f)});return n.current.set(f,x),x},[]),E=t.useCallback(f=>{p(f)},[p]),h=t.useCallback(async f=>(await Promise.all(f.map(w=>p(w))),r.current),[p]);return{pageDimensions:l,requestPageDimension:E,ensurePageDimensions:h,setPdf:c}}function ne(s){const[n,r]=t.useState(!0),[l,o]=t.useState(s.isOpen);return t.useEffect(()=>{let c;return s.isOpen?(r(!1),c=setTimeout(()=>o(!0),e.OPEN_DELAY)):(o(!1),c=setTimeout(()=>r(!0),e.TRANSITION_DELAY*1.1)),()=>clearTimeout(c)},[s.isOpen]),n?null:e.jsxRuntimeExports.jsx(se,{...s,isOpen:l})}function se({numPages:s,currentPage:n,goToPage:r,isOpen:l,pageDimensions:o,requestPageDimension:c}){const p=t.useRef(null),E=t.useRef(!1),[h,f]=t.useState(n-1);t.useEffect(()=>{if(p.current){if(E.current){E.current=!1;return}p.current.scrollToRow(n-1)}},[n]);const w=t.useCallback(u=>{const i=o.get(u+1);return i?i.h/i.w:L},[o]),b=t.useCallback(({index:u})=>{const j=e.THUMBNAIL_WIDTH*w(u)+e.THUMBNAIL_LABEL_SPACE+e.THUMBNAIL_GAP;return u===s-1?j+e.THUMBNAIL_GAP:j},[w,s]);t.useEffect(()=>{var u;(u=p.current)==null||u.recomputeRowHeights()},[o,s]);const x=t.useCallback(({overscanStartIndex:u,overscanStopIndex:i})=>{for(let j=u;j<=i;j++)c(j+1);h!==void 0&&f(void 0)},[c,h]),g=({index:u,key:i,style:j})=>{const v=u+1,k=w(u),C=e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__thumbnail-skeleton",style:{width:e.THUMBNAIL_WIDTH,height:e.THUMBNAIL_WIDTH*k}});return e.jsxRuntimeExports.jsx("div",{style:{...j,paddingTop:e.THUMBNAIL_GAP,paddingBottom:u===s-1?e.THUMBNAIL_GAP:0,boxSizing:"border-box"},children:e.jsxRuntimeExports.jsxs("button",{"data-thumb-page":v,className:`pdf-preview__thumbnail${n===v?" pdf-preview__thumbnail--active":""}`,onClick:()=>{E.current=!0,r(v)},"aria-label":`Go to page ${v}`,children:[e.jsxRuntimeExports.jsx(D.Thumbnail,{pageNumber:v,width:e.THUMBNAIL_WIDTH,loading:C}),e.jsxRuntimeExports.jsx("span",{className:"pdf-preview__thumbnail-number",children:v})]})},i)};return e.jsxRuntimeExports.jsx("div",{className:`pdf-preview__sidebar${l?"":" pdf-preview__sidebar--closed"}`,children:e.jsxRuntimeExports.jsx(M.AutoSizer,{children:({height:u,width:i})=>e.jsxRuntimeExports.jsx(M.List,{ref:p,height:u,width:i,rowCount:s,rowHeight:b,estimatedRowSize:e.THUMBNAIL_WIDTH*L+e.THUMBNAIL_LABEL_SPACE+e.THUMBNAIL_GAP,overscanRowCount:5,rowRenderer:g,scrollToIndex:h,scrollToAlignment:"center",onRowsRendered:x,style:{outline:"none"}})})})}function re({numPages:s,pageInputValue:n,onToggleSidebar:r,onPageInputChange:l,onPageInputSubmit:o,onPageInputKeyDown:c,onZoomIn:p,onZoomReset:E,onZoomOut:h}){return e.jsxRuntimeExports.jsxs("div",{className:"file-preview__controls",children:[e.jsxRuntimeExports.jsx(F.Button,{variant:"tertiary",color:"neutral",onClick:r,"aria-label":"Toggle sidebar",size:"small",icon:e.jsxRuntimeExports.jsx(e.LeftSidebarIcon,{})}),e.jsxRuntimeExports.jsx("div",{className:"file-preview__controls__separator"}),e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__page-nav",children:e.jsxRuntimeExports.jsxs("div",{className:"pdf-preview__page-indicator",children:[e.jsxRuntimeExports.jsx(F.Input,{variant:"classic",value:n,onChange:l,onBlur:o,onKeyDown:c,"aria-label":"Current page",size:n.length||1}),e.jsxRuntimeExports.jsxs("span",{className:"pdf-preview__page-total",children:["/ ",s]})]})}),e.jsxRuntimeExports.jsx("div",{className:"file-preview__controls__separator"}),e.jsxRuntimeExports.jsx(e.ZoomControls,{zoomOut:h,zoomIn:p,resetView:E})]})}function oe(s,n=300){let r;const l=(...o)=>{r!==void 0&&clearTimeout(r),r=setTimeout(()=>{s(...o)},n)};return l.cancel=()=>{r!==void 0&&clearTimeout(r)},l}const ie=()=>{const[s,n]=t.useState({width:window.innerWidth,height:window.innerHeight});return t.useEffect(()=>{const r=oe(()=>{n({width:window.innerWidth,height:window.innerHeight})},100);return window.addEventListener("resize",r),()=>{r.cancel(),window.removeEventListener("resize",r)}},[]),s},N=16,K=800,V=32;function ce({numPages:s,zoom:n,onCurrentPageChange:r,onClick:l,pageDimensions:o,requestPageDimension:c,ensurePageDimensions:p,ref:E}){const h=t.useRef(null),f=t.useRef(n),[w,b]=t.useState(0),x=t.useRef(0),g=t.useRef(0),u=ie(),i=t.useMemo(()=>K+V>u.width?u.width-V:K,[u.width]),j=t.useCallback(a=>{const d=o.get(a+1);return d?d.h/d.w:L},[o]),v=t.useCallback(a=>{const d=i*j(a)*n,m=N*n;return a===s-1?d+2*m:d+m},[i,j,n,s]),k=t.useCallback(({index:a})=>v(a),[v]);t.useEffect(()=>{var a;if((a=h.current)==null||a.recomputeRowHeights(),f.current!==n&&h.current){const d=n/f.current,m=Math.round(w*d);h.current.scrollToPosition(m),f.current=n}},[n,i,o,s]);const C=t.useMemo(()=>{if(s===0)return 1;const a=w+x.current/2;let d=0;for(let m=0;m<s;m++){const _=v(m);if(d+_>a)return m+1;d+=_}return s},[w,s,v]);t.useEffect(()=>{r(C)},[C]);const A=t.useCallback(a=>{g.current+=1;const d=g.current,m=[];for(let _=1;_<=a;_++)m.push(_);p(m).then(_=>{if(g.current!==d||!h.current)return;let I=0;for(let S=0;S<a-1;S++){const T=_.get(S+1),O=T?T.h/T.w:L;I+=i*O*n+N*n}h.current.scrollToPosition(I)})},[p,i,n]);t.useImperativeHandle(E,()=>({scrollToPage:A}),[A]);const H=t.useCallback(({scrollTop:a,clientHeight:d})=>{x.current=d,b(a)},[]),U=t.useCallback(({overscanStartIndex:a,overscanStopIndex:d})=>{for(let m=a;m<=d;m++)c(m+1)},[c]),B=({index:a,key:d,style:m})=>{const _=j(a),I=e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__page-skeleton pdf-preview__page-skeleton--measured",style:{height:i*_*n,width:i*n}});return e.jsxRuntimeExports.jsx("div",{"data-preview-backdrop":"true",className:"pdf-preview__page-container",style:{...m,paddingTop:N*n,paddingBottom:a===s-1?N*n:0},children:e.jsxRuntimeExports.jsx(D.Page,{pageNumber:a+1,width:i,scale:n,loading:I})},d)};return e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__container",onClick:l,children:e.jsxRuntimeExports.jsx(M.AutoSizer,{children:({height:a,width:d})=>{const m=Math.max(d,i*n);return e.jsxRuntimeExports.jsx("div",{style:{width:d,height:a},className:"pdf-preview__horizontal-scroll",children:e.jsxRuntimeExports.jsx(M.List,{ref:h,height:a,width:m,rowCount:s,rowHeight:k,estimatedRowSize:i*L*n+N*n,overscanRowCount:3,onScroll:H,onRowsRendered:U,rowRenderer:B,style:{outline:"none"}})})}})})}const ae=["https:","http:"],le=()=>{const s=F.useModals(),{t:n}=e.useCustomTranslations();return{handlePdfClick:t.useCallback(l=>{const c=l.target.closest("a");if(c&&c.href){if(c.closest("[data-internal-link]"))return;l.preventDefault(),l.stopPropagation();try{const p=new URL(c.href);if(!ae.includes(p.protocol))return}catch{return}s.confirmationModal({title:n("components.filePreview.externalLink.title"),children:e.jsxRuntimeExports.jsxs("div",{children:[e.jsxRuntimeExports.jsx("p",{children:n("components.filePreview.externalLink.description")}),e.jsxRuntimeExports.jsx("span",{className:"pdf-preview__external-link",children:c.href}),e.jsxRuntimeExports.jsx("p",{children:n("components.filePreview.externalLink.confirmQuestion")})]}),onDecide:p=>{p==="yes"&&window.open(c.href,"_blank","noopener,noreferrer")}})}},[s,n])}},ue={cMapUrl:"/cmaps/",standardFontDataUrl:"/standard_fonts/",wasmUrl:"/wasm/",isEvalSupported:!1};function Z({src:s,onThumbailSidebarOpen:n,pdfWorkerSrc:r=e.DEFAULT_PDF_WORKER_SRC}){D.pdfjs.GlobalWorkerOptions.workerSrc=r;const{t:l}=e.useCustomTranslations(),[o,c]=t.useState(1),[p,E]=t.useState(null),[h,f]=t.useState(1),[w,b]=t.useState(!1),x=t.useRef(null),{handlePdfClick:g}=le(),[u,i]=t.useState(1),{pageDimensions:j,requestPageDimension:v,ensurePageDimensions:k,setPdf:C}=te(),A=t.useCallback(()=>{i(R=>Math.min(3,R+.25))},[]),H=t.useCallback(()=>{i(R=>Math.max(.5,R-.25))},[]),U=t.useCallback(()=>{i(1)},[]),B=t.useCallback(()=>{b(R=>(n==null||n(!R),!R))},[]),a=t.useCallback(R=>{var P;(P=x.current)==null||P.scrollToPage(R)},[]),{goToPage:d,onDocumentLoadSuccess:m,pageInputValue:_,setPageInputValue:I,handlePageInputChange:S,handlePageInputSubmit:T,handlePageInputKeyDown:O,onItemClick:z}=ee({numPages:o,currentPage:h,scrollToPage:a});t.useEffect(()=>{I(String(h))},[h,I]);const{data:q,error:y}=$.useQuery({queryKey:["pdf",s],queryFn:async({signal:R})=>{const P=await fetch(s,{credentials:"include",signal:R});if(!P.ok)throw new Error(`Failed to fetch PDF: ${P.status}`);const J=await P.blob(),X=s.split("/").pop()||"document.pdf";return new File([J],X,{type:"application/pdf"})},staleTime:1/0}),Q=t.useCallback(R=>{const P=m(R);c(P),C(R)},[m,C]),Y=t.useCallback(R=>{const P=["InvalidPDFException","FormatError","PasswordException","ResponseException"];console.log(R),E(P.includes(R.name)?"generic":"outdated")},[]);if(console.log(y),y!=null&&y.message||p==="generic")return e.jsxRuntimeExports.jsxs("div",{className:"file-preview-unsupported","data-preview-backdrop":"true",children:[e.jsxRuntimeExports.jsx("div",{className:"file-preview-unsupported__icon",children:e.jsxRuntimeExports.jsx(e.Icon,{name:"error",type:e.IconType.OUTLINED,size:48})}),e.jsxRuntimeExports.jsx("p",{className:"file-preview-unsupported__title",children:l("components.filePreview.error.title")}),e.jsxRuntimeExports.jsx("p",{className:"file-preview-unsupported__description",children:l("components.filePreview.error.description")})]});if(p==="outdated")return e.jsxRuntimeExports.jsx(e.OutdatedBrowserPreview,{});const G=e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__container-skeleton",children:e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__page-skeleton"})});return e.jsxRuntimeExports.jsxs("div",{className:"pdf-preview",children:[e.jsxRuntimeExports.jsx("div",{className:"pdf-preview__body","data-preview-backdrop":"true",children:q?e.jsxRuntimeExports.jsxs(D.Document,{file:q,onLoadSuccess:Q,onItemClick:z,options:ue,loading:G,onLoadError:Y,children:[e.jsxRuntimeExports.jsx(ne,{numPages:o,currentPage:h,goToPage:d,isOpen:w,pageDimensions:j,requestPageDimension:v}),e.jsxRuntimeExports.jsx(ce,{ref:x,numPages:o,zoom:u,onCurrentPageChange:f,onClick:g,pageDimensions:j,requestPageDimension:v,ensurePageDimensions:k})]}):G}),e.jsxRuntimeExports.jsx(re,{numPages:o,pageInputValue:_,onToggleSidebar:B,onPageInputChange:S,onPageInputSubmit:T,onPageInputKeyDown:O,onZoomIn:A,onZoomReset:U,onZoomOut:H})]})}exports.PdfPreview=Z;exports.default=Z;
2
- //# sourceMappingURL=PdfPreview-CnHLGN5f.cjs.map