viewdoc 0.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.
@@ -0,0 +1,316 @@
1
+ import * as react from 'react';
2
+ import { ReactNode, CSSProperties } from 'react';
3
+
4
+ type DocViewerFileType = 'image' | 'pdf' | 'docx' | 'xlsx';
5
+ /**
6
+ * Extracts the file extension from a URL's pathname (ignoring query strings/fragments,
7
+ * which matters for presigned URLs like S3's that carry dots in their query params).
8
+ */
9
+ declare function getUrlExtension(uri: string): string | null;
10
+ /** Maps a URL (or explicit extension/mime hint) to the viewer type DocViewer should render. */
11
+ declare function detectFileType(uri: string, hint?: string): DocViewerFileType | null;
12
+
13
+ interface ViewerTheme {
14
+ /** Background color of the viewer canvas area. */
15
+ background?: string;
16
+ /** Background color of the toolbar strip. */
17
+ toolbarBackground?: string;
18
+ /** Border color under the toolbar. */
19
+ toolbarBorderColor?: string;
20
+ /** Hover background for toolbar buttons. */
21
+ toolbarButtonHoverBackground?: string;
22
+ /** Text/icon color used across the toolbar. */
23
+ textColor?: string;
24
+ /** Corner radius of the outer viewer container. */
25
+ borderRadius?: string;
26
+ /** Corner radius of individual toolbar buttons. */
27
+ toolbarButtonRadius?: string;
28
+ }
29
+
30
+ interface Position {
31
+ x: number;
32
+ y: number;
33
+ }
34
+ interface UseDraggableOptions {
35
+ defaultPosition?: Position;
36
+ /** Allow dragging via the returned handle props. Default: true. */
37
+ draggable?: boolean;
38
+ }
39
+ interface DraggableControls {
40
+ position: Position;
41
+ onDragHandlePointerDown: (e: React.PointerEvent) => void;
42
+ }
43
+ declare function useDraggable(options?: UseDraggableOptions): DraggableControls;
44
+
45
+ interface DocViewerProps {
46
+ /** URL of the file to display. */
47
+ uri: string;
48
+ /** Used as alt text / suggested download filename, and (if no `type` is given) as a fallback source for extension detection. */
49
+ fileName?: string;
50
+ /**
51
+ * Force the viewer type instead of auto-detecting from the URL. Use this when the URL has no
52
+ * reliable extension (e.g. a presigned URL pointing at an opaque key), or to pass a MIME type
53
+ * (e.g. 'application/pdf', 'image/png').
54
+ */
55
+ type?: DocViewerFileType | string;
56
+ /** Rendered when the file type can't be detected and no `type` override was given. */
57
+ fallback?: ReactNode;
58
+ className?: string;
59
+ style?: CSSProperties;
60
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
61
+ width?: number | string;
62
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
63
+ height?: number | string;
64
+ minScale?: number;
65
+ maxScale?: number;
66
+ zoomStep?: number;
67
+ enableZoomControls?: boolean;
68
+ enableWheelZoom?: boolean;
69
+ enableFullscreen?: boolean;
70
+ enableDownload?: boolean;
71
+ onDownload?: () => void;
72
+ theme?: ViewerTheme;
73
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */
74
+ floating?: boolean;
75
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
76
+ windowDraggable?: boolean;
77
+ /** Initial position when floating. Default: centered in the viewport. */
78
+ defaultPosition?: Position;
79
+ }
80
+ declare function DocViewer({ uri, fileName, type, fallback, ...rest }: DocViewerProps): react.JSX.Element;
81
+
82
+ interface ImageViewerProps {
83
+ /** URL or data URI of the image to display. */
84
+ uri: string;
85
+ /** Used as alt text and as the suggested filename on download. */
86
+ fileName?: string;
87
+ className?: string;
88
+ style?: CSSProperties;
89
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
90
+ width?: number | string;
91
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
92
+ height?: number | string;
93
+ /** Minimum zoom scale. Default: 0.1 */
94
+ minScale?: number;
95
+ /** Maximum zoom scale. Default: 8 */
96
+ maxScale?: number;
97
+ /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */
98
+ zoomStep?: number;
99
+ /** Show zoom in/out and reset-to-100% controls. Default: true. */
100
+ enableZoomControls?: boolean;
101
+ /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */
102
+ enableWheelZoom?: boolean;
103
+ /** Allow click-drag-release panning of the image. Default: true. */
104
+ enablePan?: boolean;
105
+ /** Show the fullscreen toggle button. Default: true. */
106
+ enableFullscreen?: boolean;
107
+ /** Show the download button. Default: true. */
108
+ enableDownload?: boolean;
109
+ /** Called when the download button is clicked. If omitted, downloads `uri` directly. */
110
+ onDownload?: () => void;
111
+ /** Colors/radii to override the default look (toolbar background, text color, etc). */
112
+ theme?: ViewerTheme;
113
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */
114
+ floating?: boolean;
115
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
116
+ windowDraggable?: boolean;
117
+ /** Initial position when floating. Default: centered in the viewport. */
118
+ defaultPosition?: Position;
119
+ }
120
+ declare function ImageViewer({ uri, fileName, className, style, width, height, minScale, maxScale, zoomStep, enableZoomControls, enableWheelZoom, enablePan, enableFullscreen, enableDownload, onDownload, theme, floating, windowDraggable, defaultPosition, }: ImageViewerProps): react.JSX.Element;
121
+
122
+ interface PdfViewerProps {
123
+ /** URL or data URI of the PDF to display. */
124
+ uri: string;
125
+ /** Used as the suggested filename on download. */
126
+ fileName?: string;
127
+ className?: string;
128
+ style?: CSSProperties;
129
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
130
+ width?: number | string;
131
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
132
+ height?: number | string;
133
+ /** Minimum zoom scale. Default: 0.25 */
134
+ minScale?: number;
135
+ /** Maximum zoom scale. Default: 5 */
136
+ maxScale?: number;
137
+ /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */
138
+ zoomStep?: number;
139
+ /** Canvas render resolution multiplier (crispness at high zoom). Default: 1.5 */
140
+ renderScale?: number;
141
+ /** Show zoom in/out and reset-to-100% controls. Default: true. */
142
+ enableZoomControls?: boolean;
143
+ /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */
144
+ enableWheelZoom?: boolean;
145
+ /** Show the fullscreen toggle button. Default: true. */
146
+ enableFullscreen?: boolean;
147
+ /** Show the download button. Default: true. */
148
+ enableDownload?: boolean;
149
+ /** Called when the download button is clicked. If omitted, downloads `uri` directly. */
150
+ onDownload?: () => void;
151
+ /** Colors/radii to override the default look (toolbar background, text color, etc). */
152
+ theme?: ViewerTheme;
153
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */
154
+ floating?: boolean;
155
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
156
+ windowDraggable?: boolean;
157
+ /** Initial position when floating. Default: centered in the viewport. */
158
+ defaultPosition?: Position;
159
+ }
160
+ declare function PdfViewer({ uri, fileName, className, style, width, height, minScale, maxScale, zoomStep, renderScale, enableZoomControls, enableWheelZoom, enableFullscreen, enableDownload, onDownload, theme, floating, windowDraggable, defaultPosition, }: PdfViewerProps): react.JSX.Element;
161
+
162
+ interface DocxViewerProps {
163
+ /** URL of the .docx file to display. */
164
+ uri: string;
165
+ /** Used as the suggested filename on download. */
166
+ fileName?: string;
167
+ className?: string;
168
+ style?: CSSProperties;
169
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
170
+ width?: number | string;
171
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
172
+ height?: number | string;
173
+ /** Minimum zoom scale. Default: 0.5 */
174
+ minScale?: number;
175
+ /** Maximum zoom scale. Default: 3 */
176
+ maxScale?: number;
177
+ /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */
178
+ zoomStep?: number;
179
+ /** Show zoom in/out and reset-to-100% controls. Default: true. */
180
+ enableZoomControls?: boolean;
181
+ /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */
182
+ enableWheelZoom?: boolean;
183
+ /** Show the fullscreen toggle button. Default: true. */
184
+ enableFullscreen?: boolean;
185
+ /** Show the download button. Default: true. */
186
+ enableDownload?: boolean;
187
+ /** Called when the download button is clicked. If omitted, downloads `uri` directly. */
188
+ onDownload?: () => void;
189
+ /** Colors/radii to override the default look (toolbar background, text color, etc). */
190
+ theme?: ViewerTheme;
191
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */
192
+ floating?: boolean;
193
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
194
+ windowDraggable?: boolean;
195
+ /** Initial position when floating. Default: centered in the viewport. */
196
+ defaultPosition?: Position;
197
+ }
198
+ declare function DocxViewer({ uri, fileName, className, style, width, height, minScale, maxScale, zoomStep, enableZoomControls, enableWheelZoom, enableFullscreen, enableDownload, onDownload, theme, floating, windowDraggable, defaultPosition, }: DocxViewerProps): react.JSX.Element;
199
+
200
+ interface XlsxViewerProps {
201
+ /** URL of the .xlsx/.xls (legacy binary supported)/.csv file to display. */
202
+ uri: string;
203
+ /** Used as the suggested filename on download. */
204
+ fileName?: string;
205
+ className?: string;
206
+ style?: CSSProperties;
207
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
208
+ width?: number | string;
209
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
210
+ height?: number | string;
211
+ /** Minimum zoom scale. Default: 0.5 */
212
+ minScale?: number;
213
+ /** Maximum zoom scale. Default: 3 */
214
+ maxScale?: number;
215
+ /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */
216
+ zoomStep?: number;
217
+ /** Show zoom in/out and reset-to-100% controls. Default: true. */
218
+ enableZoomControls?: boolean;
219
+ /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */
220
+ enableWheelZoom?: boolean;
221
+ /** Show the fullscreen toggle button. Default: true. */
222
+ enableFullscreen?: boolean;
223
+ /** Show the download button. Default: true. */
224
+ enableDownload?: boolean;
225
+ /** Called when the download button is clicked. If omitted, downloads `uri` directly. */
226
+ onDownload?: () => void;
227
+ /** Colors/radii to override the default look (toolbar background, text color, etc). */
228
+ theme?: ViewerTheme;
229
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */
230
+ floating?: boolean;
231
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
232
+ windowDraggable?: boolean;
233
+ /** Initial position when floating. Default: centered in the viewport. */
234
+ defaultPosition?: Position;
235
+ }
236
+ declare function XlsxViewer({ uri, fileName, className, style, width, height, minScale, maxScale, zoomStep, enableZoomControls, enableWheelZoom, enableFullscreen, enableDownload, onDownload, theme, floating, windowDraggable, defaultPosition, }: XlsxViewerProps): react.JSX.Element;
237
+
238
+ interface ZoomPanState {
239
+ scale: number;
240
+ translateX: number;
241
+ translateY: number;
242
+ }
243
+ interface ZoomPanOptions {
244
+ minScale?: number;
245
+ maxScale?: number;
246
+ zoomStep?: number;
247
+ /** Allow click-drag-release panning by transforming the content. Default: true. */
248
+ enablePan?: boolean;
249
+ /** Allow Ctrl/Cmd + scroll wheel zoom. Default: true. */
250
+ enableWheelZoom?: boolean;
251
+ /** Allow click-drag-release scrolling of a natively-scrollable container (used instead of enablePan for scrollable content like PDF pages). Default: false. */
252
+ enableDragScroll?: boolean;
253
+ }
254
+ interface ZoomPanControls extends ZoomPanState {
255
+ containerRef: React.RefObject<HTMLDivElement>;
256
+ zoomIn: () => void;
257
+ zoomOut: () => void;
258
+ resetZoom: () => void;
259
+ setScale: (scale: number) => void;
260
+ handlers: {
261
+ onWheel: (e: React.WheelEvent) => void;
262
+ onPointerDown: (e: React.PointerEvent) => void;
263
+ onPointerMove: (e: React.PointerEvent) => void;
264
+ onPointerUp: (e: React.PointerEvent) => void;
265
+ };
266
+ }
267
+ declare function useZoomPan(options?: ZoomPanOptions): ZoomPanControls;
268
+
269
+ interface ViewerShellProps {
270
+ zoomPan: ZoomPanControls;
271
+ onDownload?: () => void;
272
+ toolbarExtra?: ReactNode;
273
+ children: ReactNode;
274
+ /** Show the zoom in/out/reset-to-100% controls. Default: true. */
275
+ enableZoomControls?: boolean;
276
+ /** Show the fullscreen toggle button. Default: true. */
277
+ enableFullscreen?: boolean;
278
+ /** Allow click-drag-release panning of the content. Default: true. */
279
+ enablePan?: boolean;
280
+ /** Colors/radii to override the default look. Any field left out keeps the default. */
281
+ theme?: ViewerTheme;
282
+ /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */
283
+ width?: number | string;
284
+ /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */
285
+ height?: number | string;
286
+ /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent container. Default: true. */
287
+ floating?: boolean;
288
+ /** When floating, allow dragging the window by its toolbar. Default: true. */
289
+ draggable?: boolean;
290
+ /** Initial position when floating. Default: centered in the viewport. */
291
+ defaultPosition?: Position;
292
+ /** CSS transform-origin for the zoomed content. Default: 'center center'. */
293
+ transformOrigin?: string;
294
+ }
295
+ declare function ViewerShell({ zoomPan, onDownload, toolbarExtra, children, enableZoomControls, enableFullscreen, enablePan, theme, width, height, floating, draggable, defaultPosition, transformOrigin, }: ViewerShellProps): react.JSX.Element;
296
+
297
+ interface ToolbarProps {
298
+ scale: number;
299
+ onZoomIn: () => void;
300
+ onZoomOut: () => void;
301
+ onReset: () => void;
302
+ onFullscreen?: () => void;
303
+ onDownload?: () => void;
304
+ /** Whether the viewer is currently in fullscreen mode (controls which icon is shown). */
305
+ isFullscreen?: boolean;
306
+ /** Show zoom in/out/reset controls. Default: true. */
307
+ showZoomControls?: boolean;
308
+ /** Icon shown at the start of the toolbar; pointer-down on it starts dragging. */
309
+ dragHandle?: ReactNode;
310
+ /** Pointer-down handler attached to the drag handle icon. */
311
+ onDragHandlePointerDown?: (e: React.PointerEvent) => void;
312
+ extra?: ReactNode;
313
+ }
314
+ declare function Toolbar({ scale, onZoomIn, onZoomOut, onReset, onFullscreen, onDownload, isFullscreen, showZoomControls, dragHandle, onDragHandlePointerDown, extra, }: ToolbarProps): react.JSX.Element;
315
+
316
+ export { DocViewer, type DocViewerFileType, type DocViewerProps, DocxViewer, type DocxViewerProps, type DraggableControls, ImageViewer, type ImageViewerProps, PdfViewer, type PdfViewerProps, type Position, Toolbar, type UseDraggableOptions, ViewerShell, type ViewerTheme, XlsxViewer, type XlsxViewerProps, type ZoomPanControls, type ZoomPanOptions, type ZoomPanState, detectFileType, getUrlExtension, useDraggable, useZoomPan };