@nice2dev/ui-forms 1.1.5 → 1.1.7

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.
@@ -0,0 +1,180 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),h=require("./index-B8LQvpDi.cjs"),r=require("react");function ve(o,i){var c;const t=((c=o.split(".").pop())==null?void 0:c.toLowerCase().split("?")[0])??"";return t==="pdf"?"pdf":["jpg","jpeg","png","gif","bmp","webp","svg","ico","tiff","tif"].includes(t)?"image":["mp4","webm","ogg","mov","avi","mkv"].includes(t)?"video":["mp3","wav","flac","aac","m4a","ogg"].includes(t)?"audio":["doc","docx","xls","xlsx","ppt","pptx","odt","ods","odp"].includes(t)?"office":t==="html"||t==="htm"?"html":["txt","log","md","json","xml","csv","yaml","yml"].includes(t)?"text":"unknown"}function he(o){if(o===0)return"0 B";const i=1024,t=["B","KB","MB","GB"],c=Math.floor(Math.log(o)/Math.log(i));return parseFloat((o/Math.pow(i,c)).toFixed(1))+" "+t[c]}function we(o){const i=Math.floor(o/3600),t=Math.floor(o%3600/60),c=Math.floor(o%60);return i>0?`${i}:${t.toString().padStart(2,"0")}:${c.toString().padStart(2,"0")}`:`${t}:${c.toString().padStart(2,"0")}`}function xe({src:o,page:i,zoom:t,rotation:c,onPageChange:u,onLoad:l,onError:p,workerUrl:m}){const f=r.useRef(null),k=r.useRef(null),[x,q]=r.useState(0),[A,K]=r.useState(!1);return r.useEffect(()=>{let g=!1;return(async()=>{try{let d;if(d=window.pdfjsLib,!d)try{d=await new Function("m","return import(m)")("pdfjs-dist")}catch{throw new Error('PDF.js library not found. Please install pdfjs-dist or include it via CDN: <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"><\/script>')}m?d.GlobalWorkerOptions.workerSrc=m:d.version&&(d.GlobalWorkerOptions.workerSrc=`https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${d.version}/pdf.worker.min.js`);const w=await d.getDocument(o).promise;if(g)return;k.current=w,q(w.numPages),l({name:o.split("/").pop()??"document.pdf",type:"pdf",mimeType:"application/pdf",pageCount:w.numPages})}catch(d){g||p(d instanceof Error?d:new Error("Failed to load PDF"))}})(),()=>{g=!0}},[o,m,l,p]),r.useEffect(()=>{const g=k.current,d=f.current;if(!g||!d||x===0)return;let w=!1;return(async()=>{if(!A){K(!0);try{const z=Math.max(1,Math.min(i,x)),L=await g.getPage(z);if(w)return;const b=L.getViewport({scale:t,rotation:c}),C=d.getContext("2d");if(!C)return;d.height=b.height,d.width=b.width,await L.render({canvasContext:C,viewport:b}).promise}catch(z){console.error("Error rendering PDF page:",z)}finally{w||K(!1)}}})(),()=>{w=!0}},[i,t,c,x,A]),e.jsx("div",{className:"nice-document-viewer__pdf",children:e.jsx("canvas",{ref:f,className:"nice-document-viewer__pdf-canvas"})})}function ge({src:o,zoom:i,rotation:t,onLoad:c,onError:u}){const[l,p]=r.useState(!1),m=r.useCallback(k=>{const x=k.currentTarget;p(!0),c({name:o.split("/").pop()??"image",type:"image",width:x.naturalWidth,height:x.naturalHeight})},[o,c]),f=r.useCallback(()=>{u(new Error("Failed to load image"))},[u]);return e.jsx("div",{className:"nice-document-viewer__image",children:e.jsx("img",{src:o,alt:"Document",onLoad:m,onError:f,style:{transform:`scale(${i}) rotate(${t}deg)`,opacity:l?1:0},draggable:!1})})}function be({src:o,type:i,onLoad:t,onError:c}){const u=r.useRef(null),l=r.useCallback(()=>{const m=u.current;if(!m)return;const f={name:o.split("/").pop()??(i==="video"?"video":"audio"),type:i,duration:m.duration};i==="video"&&"videoWidth"in m&&(f.width=m.videoWidth,f.height=m.videoHeight),t(f)},[o,i,t]),p=r.useCallback(()=>{c(new Error(`Failed to load ${i}`))},[i,c]);return i==="video"?e.jsx("div",{className:"nice-document-viewer__video",children:e.jsx("video",{ref:u,src:o,controls:!0,onLoadedMetadata:l,onError:p})}):e.jsx("div",{className:"nice-document-viewer__audio",children:e.jsx("audio",{ref:u,src:o,controls:!0,onLoadedMetadata:l,onError:p})})}function _e({src:o,viewerUrl:i,onLoad:t}){const[c,u]=r.useState(!1),l=`${i??"https://view.officeapps.live.com/op/embed.aspx?src="}${encodeURIComponent(o)}`,p=r.useCallback(()=>{u(!0),t({name:o.split("/").pop()??"document",type:"office"})},[o,t]);return e.jsx("div",{className:"nice-document-viewer__office",children:e.jsx("iframe",{src:l,onLoad:p,title:"Office Document",style:{opacity:c?1:0}})})}function je({src:o,onLoad:i,onError:t}){const[c,u]=r.useState(null);return r.useEffect(()=>{fetch(o).then(l=>{if(!l.ok)throw new Error("Failed to fetch text");return l.text()}).then(l=>{u(l),i({name:o.split("/").pop()??"text",type:"text",size:l.length})}).catch(l=>{t(l instanceof Error?l:new Error("Failed to load text"))})},[o,i,t]),c===null?e.jsx("div",{className:"nice-document-viewer__loading",children:"Loading..."}):e.jsx("div",{className:"nice-document-viewer__text",children:e.jsx("pre",{children:c})})}function ye({src:o,onLoad:i}){const t=r.useCallback(()=>{i({name:o.split("/").pop()??"page.html",type:"html"})},[o,i]);return e.jsx("div",{className:"nice-document-viewer__html",children:e.jsx("iframe",{src:o,onLoad:t,title:"HTML Document",sandbox:"allow-same-origin allow-scripts"})})}const ke=r.forwardRef(function(o,i){const{src:t,type:c,name:u,height:l=600,width:p="100%",zoom:m=1,minZoom:f=.25,maxZoom:k=4,page:x=1,showToolbar:q=!0,showPageNav:A=!0,showZoomControls:K=!0,showFullscreen:g=!0,showDownload:d=!0,showPrint:w=!0,showRotate:z=!0,enableKeyboard:L=!0,onLoad:b,onError:C,onPageChange:Z,onZoomChange:U,pdfWorkerUrl:ee,officeViewerUrl:te,loading:ne,errorContent:G,className:re,"data-testid":oe}=o,O=r.useRef(null),[n,ie]=r.useState(null),[_,ae]=r.useState(x),[E,ce]=r.useState(m),[J,se]=r.useState(0),[B,Y]=r.useState(!1),[I,le]=r.useState(null),[de,Q]=r.useState(!0),j=r.useMemo(()=>c??ve(t),[t,c]),N=r.useCallback(a=>{ie(a),Q(!1),b==null||b(a)},[b]),T=r.useCallback(a=>{le(a),Q(!1),C==null||C(a)},[C]),v=r.useCallback(a=>{const s=(n==null?void 0:n.pageCount)??1,R=Math.max(1,Math.min(a,s));ae(R),Z==null||Z(R)},[n==null?void 0:n.pageCount,Z]),S=r.useCallback(()=>v(_+1),[_,v]),P=r.useCallback(()=>v(_-1),[_,v]),y=r.useCallback(a=>{const s=Math.max(f,Math.min(a,k));ce(s),U==null||U(s)},[f,k,U]),M=r.useCallback(()=>y(E*1.25),[E,y]),D=r.useCallback(()=>y(E/1.25),[E,y]),F=r.useCallback(()=>y(1),[y]),W=r.useCallback(a=>{se(s=>(s+a)%360)},[]),H=r.useCallback(()=>{const a=document.createElement("a");a.href=t,a.download=u??(n==null?void 0:n.name)??"document",document.body.appendChild(a),a.click(),document.body.removeChild(a)},[t,u,n==null?void 0:n.name]),V=r.useCallback(()=>{const a=window.open(t,"_blank");a&&(a.onload=()=>a.print())},[t]),$=r.useCallback(()=>{var a,s,R;O.current&&(B?((a=document.exitFullscreen)==null||a.call(document),Y(!1)):((R=(s=O.current).requestFullscreen)==null||R.call(s),Y(!0)))},[B]);r.useEffect(()=>{if(!L)return;const a=s=>{switch(s.key){case"ArrowRight":case"PageDown":S();break;case"ArrowLeft":case"PageUp":P();break;case"Home":v(1);break;case"End":v((n==null?void 0:n.pageCount)??1);break;case"+":case"=":(s.ctrlKey||s.metaKey)&&(s.preventDefault(),M());break;case"-":(s.ctrlKey||s.metaKey)&&(s.preventDefault(),D());break;case"0":(s.ctrlKey||s.metaKey)&&(s.preventDefault(),F());break;case"f":(s.ctrlKey||s.metaKey)&&(s.preventDefault(),$());break}};return window.addEventListener("keydown",a),()=>window.removeEventListener("keydown",a)},[L,S,P,v,n==null?void 0:n.pageCount,M,D,F,$]),r.useImperativeHandle(i,()=>({goToPage:v,nextPage:S,prevPage:P,setZoom:y,zoomIn:M,zoomOut:D,resetZoom:F,rotate:W,download:H,print:V,toggleFullscreen:$,getInfo:()=>n}),[v,S,P,y,M,D,F,W,H,V,$,n]);const ue=()=>{if(I)return(typeof G=="function"?G(I):G)??e.jsx("div",{className:"nice-document-viewer__error",children:e.jsxs("span",{children:[e.jsx(h.Ae,{name:"warning",size:14})," ",I.message]})});switch(j){case"pdf":return e.jsx(xe,{src:t,page:_,zoom:E,rotation:J,onPageChange:v,onLoad:N,onError:T,workerUrl:ee});case"image":return e.jsx(ge,{src:t,zoom:E,rotation:J,onLoad:N,onError:T});case"video":case"audio":return e.jsx(be,{src:t,type:j,onLoad:N,onError:T});case"office":return e.jsx(_e,{src:t,viewerUrl:te,onLoad:N});case"text":return e.jsx(je,{src:t,onLoad:N,onError:T});case"html":return e.jsx(ye,{src:t,onLoad:N});default:return e.jsxs("div",{className:"nice-document-viewer__unknown",children:[e.jsxs("span",{children:[e.jsx(h.Ae,{name:"file",size:14})," Unknown document type"]}),e.jsx("button",{type:"button",onClick:H,children:"Download"})]})}},me=A&&j==="pdf"&&((n==null?void 0:n.pageCount)??0)>1,pe=K&&(j==="pdf"||j==="image"),fe=z&&(j==="pdf"||j==="image");return e.jsxs("div",{ref:O,className:`nice-document-viewer ${B?"nice-document-viewer--fullscreen":""} ${re??""}`,style:{height:l,width:p},"data-testid":oe,"data-type":j,children:[q&&e.jsxs("div",{className:"nice-document-viewer__toolbar",children:[e.jsx("div",{className:"nice-document-viewer__toolbar-left",children:me&&e.jsxs("div",{className:"nice-document-viewer__pagination",children:[e.jsx("button",{type:"button",onClick:P,disabled:_<=1,"aria-label":"Previous page",children:e.jsx(h.Ae,{name:"chevron-left",size:16})}),e.jsxs("span",{children:[e.jsx("input",{type:"number",value:_,onChange:a=>v(parseInt(a.target.value,10)),min:1,max:(n==null?void 0:n.pageCount)??1}),"/ ",(n==null?void 0:n.pageCount)??1]}),e.jsx("button",{type:"button",onClick:S,disabled:_>=((n==null?void 0:n.pageCount)??1),"aria-label":"Next page",children:e.jsx(h.Ae,{name:"chevron-right",size:16})})]})}),e.jsxs("div",{className:"nice-document-viewer__toolbar-center",children:[pe&&e.jsxs("div",{className:"nice-document-viewer__zoom",children:[e.jsx("button",{type:"button",onClick:D,"aria-label":"Zoom out",children:"−"}),e.jsxs("span",{children:[Math.round(E*100),"%"]}),e.jsx("button",{type:"button",onClick:M,"aria-label":"Zoom in",children:"+"}),e.jsx("button",{type:"button",onClick:F,"aria-label":"Reset zoom",children:e.jsx(h.Ae,{name:"refresh",size:16})})]}),fe&&e.jsxs("div",{className:"nice-document-viewer__rotation",children:[e.jsx("button",{type:"button",onClick:()=>W(270),"aria-label":"Rotate left",children:e.jsx(h.Ae,{name:"refresh",size:16})}),e.jsx("button",{type:"button",onClick:()=>W(90),"aria-label":"Rotate right",children:e.jsx(h.Ae,{name:"refresh",size:16})})]})]}),e.jsxs("div",{className:"nice-document-viewer__toolbar-right",children:[d&&e.jsx("button",{type:"button",onClick:H,"aria-label":"Download",children:e.jsx(h.Ae,{name:"download",size:16})}),w&&e.jsx("button",{type:"button",onClick:V,"aria-label":"Print",children:e.jsx(h.Ae,{name:"printer",size:16})}),g&&e.jsx("button",{type:"button",onClick:$,"aria-label":"Fullscreen",children:e.jsx(h.Ae,{name:B?"collapse":"expand",size:16})})]})]}),e.jsxs("div",{className:"nice-document-viewer__content",children:[de&&!I&&(ne??e.jsx("div",{className:"nice-document-viewer__loading",children:"Loading..."})),ue()]}),n&&e.jsxs("div",{className:"nice-document-viewer__info",children:[e.jsx("span",{className:"nice-document-viewer__name",children:u??n.name}),n.size&&e.jsx("span",{className:"nice-document-viewer__size",children:he(n.size)}),n.width&&n.height&&e.jsxs("span",{className:"nice-document-viewer__dimensions",children:[n.width," × ",n.height]}),n.duration&&e.jsx("span",{className:"nice-document-viewer__duration",children:we(n.duration)})]})]})}),X="nice-document-viewer-styles";if(typeof document<"u"&&!document.getElementById(X)){const o=document.createElement("style");o.id=X,o.textContent=`
2
+ .nice-document-viewer {
3
+ display: flex;
4
+ flex-direction: column;
5
+ border: 1px solid var(--nice-border-color, #e5e7eb);
6
+ border-radius: var(--nice-radius-sm, 4px);
7
+ overflow: hidden;
8
+ background: var(--nice-bg-primary, #ffffff);
9
+ }
10
+
11
+ .nice-document-viewer--fullscreen {
12
+ position: fixed;
13
+ top: 0;
14
+ left: 0;
15
+ right: 0;
16
+ bottom: 0;
17
+ z-index: 9999;
18
+ border: none;
19
+ border-radius: 0;
20
+ }
21
+
22
+ .nice-document-viewer__toolbar {
23
+ display: flex;
24
+ align-items: center;
25
+ justify-content: space-between;
26
+ padding: var(--nice-space-2, 8px) var(--nice-space-3, 12px);
27
+ background: var(--nice-bg-primary, #ffffff);
28
+ border-bottom: 1px solid var(--nice-border-color, #e5e7eb);
29
+ flex-shrink: 0;
30
+ }
31
+
32
+ .nice-document-viewer__toolbar-left,
33
+ .nice-document-viewer__toolbar-center,
34
+ .nice-document-viewer__toolbar-right {
35
+ display: flex;
36
+ align-items: center;
37
+ gap: var(--nice-space-2, 8px);
38
+ }
39
+
40
+ .nice-document-viewer__toolbar button {
41
+ padding: var(--nice-space-1-5, 6px) var(--nice-space-2-5, 10px);
42
+ border: 1px solid var(--nice-border-color, #e5e7eb);
43
+ border-radius: var(--nice-radius-sm, 4px);
44
+ background: var(--nice-bg-primary, #ffffff);
45
+ cursor: pointer;
46
+ font-size: 14px;
47
+ }
48
+
49
+ .nice-document-viewer__toolbar button:hover:not(:disabled) {
50
+ background: var(--nice-hover-bg, color-mix(in srgb, var(--nice-text-primary, #000) 4%, transparent));
51
+ }
52
+
53
+ .nice-document-viewer__toolbar button:disabled {
54
+ opacity: 0.4;
55
+ cursor: not-allowed;
56
+ }
57
+
58
+ .nice-document-viewer__pagination,
59
+ .nice-document-viewer__zoom,
60
+ .nice-document-viewer__rotation {
61
+ display: flex;
62
+ align-items: center;
63
+ gap: var(--nice-space-1-5, 6px);
64
+ }
65
+
66
+ .nice-document-viewer__pagination input {
67
+ width: 50px;
68
+ padding: var(--nice-space-1, 4px);
69
+ border: 1px solid var(--nice-border-color, #e5e7eb);
70
+ border-radius: var(--nice-radius-sm, 4px);
71
+ text-align: center;
72
+ }
73
+
74
+ .nice-document-viewer__zoom span {
75
+ min-width: 50px;
76
+ text-align: center;
77
+ font-size: 13px;
78
+ }
79
+
80
+ .nice-document-viewer__content {
81
+ flex: 1;
82
+ overflow: auto;
83
+ display: flex;
84
+ align-items: center;
85
+ justify-content: center;
86
+ position: relative;
87
+ }
88
+
89
+ .nice-document-viewer__loading,
90
+ .nice-document-viewer__error,
91
+ .nice-document-viewer__unknown {
92
+ display: flex;
93
+ flex-direction: column;
94
+ align-items: center;
95
+ justify-content: center;
96
+ gap: var(--nice-space-3, 12px);
97
+ padding: var(--nice-space-10, 40px);
98
+ color: var(--nice-text-secondary, #475569);
99
+ }
100
+
101
+ .nice-document-viewer__error span {
102
+ color: var(--nice-error, #d32f2f);
103
+ }
104
+
105
+ .nice-document-viewer__pdf,
106
+ .nice-document-viewer__image {
107
+ display: flex;
108
+ align-items: center;
109
+ justify-content: center;
110
+ min-height: 100%;
111
+ min-width: 100%;
112
+ }
113
+
114
+ .nice-document-viewer__pdf-canvas {
115
+ max-width: 100%;
116
+ height: auto;
117
+ }
118
+
119
+ .nice-document-viewer__image img {
120
+ max-width: 100%;
121
+ max-height: 100%;
122
+ object-fit: contain;
123
+ transition: transform 0.2s;
124
+ }
125
+
126
+ .nice-document-viewer__video,
127
+ .nice-document-viewer__audio {
128
+ display: flex;
129
+ align-items: center;
130
+ justify-content: center;
131
+ width: 100%;
132
+ height: 100%;
133
+ }
134
+
135
+ .nice-document-viewer__video video,
136
+ .nice-document-viewer__audio audio {
137
+ max-width: 100%;
138
+ max-height: 100%;
139
+ }
140
+
141
+ .nice-document-viewer__office iframe,
142
+ .nice-document-viewer__html iframe {
143
+ width: 100%;
144
+ height: 100%;
145
+ border: none;
146
+ }
147
+
148
+ .nice-document-viewer__text {
149
+ width: 100%;
150
+ height: 100%;
151
+ overflow: auto;
152
+ padding: var(--nice-space-4, 16px);
153
+ background: var(--nice-bg-primary, #ffffff);
154
+ }
155
+
156
+ .nice-document-viewer__text pre {
157
+ margin: 0;
158
+ font-family: 'Fira Code', monospace;
159
+ font-size: 13px;
160
+ white-space: pre-wrap;
161
+ word-wrap: break-word;
162
+ }
163
+
164
+ .nice-document-viewer__info {
165
+ display: flex;
166
+ align-items: center;
167
+ gap: var(--nice-space-4, 16px);
168
+ padding: var(--nice-space-1-5, 6px) var(--nice-space-3, 12px);
169
+ background: var(--nice-bg-primary, #ffffff);
170
+ border-top: 1px solid var(--nice-border-color, #e5e7eb);
171
+ font-size: 12px;
172
+ color: var(--nice-text-secondary, #475569);
173
+ flex-shrink: 0;
174
+ }
175
+
176
+ .nice-document-viewer__name {
177
+ font-weight: 500;
178
+ color: var(--nice-text-primary, #0f172a);
179
+ }
180
+ `,document.head.appendChild(o)}exports.NiceDocumentViewer=ke;