viewdoc 0.1.1 → 0.2.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/README.md +6 -3
- package/dist/DocxViewer-VQN3BESX.js +8 -0
- package/dist/DocxViewer-VQN3BESX.js.map +1 -0
- package/dist/PdfViewer-NVAQBI2Y.js +8 -0
- package/dist/PdfViewer-NVAQBI2Y.js.map +1 -0
- package/dist/XlsxViewer-SQMAF5UA.js +8 -0
- package/dist/XlsxViewer-SQMAF5UA.js.map +1 -0
- package/dist/chunk-4VVVAGYE.js +110 -0
- package/dist/chunk-4VVVAGYE.js.map +1 -0
- package/dist/chunk-KRN2POC4.js +174 -0
- package/dist/chunk-KRN2POC4.js.map +1 -0
- package/dist/chunk-LE4YTMPN.js +135 -0
- package/dist/chunk-LE4YTMPN.js.map +1 -0
- package/dist/chunk-OA35SYM6.js +432 -0
- package/dist/chunk-OA35SYM6.js.map +1 -0
- package/dist/docx.cjs +566 -0
- package/dist/docx.cjs.map +1 -0
- package/dist/docx.d.cts +43 -0
- package/dist/docx.d.ts +43 -0
- package/dist/docx.js +8 -0
- package/dist/docx.js.map +1 -0
- package/dist/index.cjs +235 -116
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -150
- package/dist/index.d.ts +9 -150
- package/dist/index.js +21 -828
- package/dist/index.js.map +1 -1
- package/dist/pdf.cjs +631 -0
- package/dist/pdf.cjs.map +1 -0
- package/dist/pdf.d.cts +45 -0
- package/dist/pdf.d.ts +45 -0
- package/dist/pdf.js +8 -0
- package/dist/pdf.js.map +1 -0
- package/dist/useDraggable-QvFR-4DD.d.cts +33 -0
- package/dist/useDraggable-QvFR-4DD.d.ts +33 -0
- package/dist/xlsx.cjs +591 -0
- package/dist/xlsx.cjs.map +1 -0
- package/dist/xlsx.d.cts +43 -0
- package/dist/xlsx.d.ts +43 -0
- package/dist/xlsx.js +8 -0
- package/dist/xlsx.js.map +1 -0
- package/package.json +16 -1
- package/dist/styles.d.cts +0 -2
- package/dist/styles.d.ts +0 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ import 'viewdoc/styles.css'
|
|
|
14
14
|
<DocViewer uri="https://example.com/report.pdf" />
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
That's it — `DocViewer` detects the file type from the URL and renders the right viewer.
|
|
17
|
+
That's it — `DocViewer` detects the file type from the URL and renders the right viewer. It loads whichever parser it needs (PDF/DOCX/XLSX) lazily at runtime — importing `viewdoc` for image-only use costs ~70KB gzipped, not the full bundle.
|
|
18
18
|
|
|
19
19
|
## Install
|
|
20
20
|
|
|
@@ -67,10 +67,13 @@ If detection fails and no `type` is given, `DocViewer` renders a `fallback` node
|
|
|
67
67
|
|
|
68
68
|
### Individual viewers
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
`ImageViewer` is cheap (no extra dependencies) and lives on the main entry. `PdfViewer`, `DocxViewer` and `XlsxViewer` each pull in a sizeable parser (`pdfjs-dist`, `mammoth`, `xlsx` respectively), so they live on their own subpaths — this keeps `viewdoc`'s main entry small for apps that only need images, and `DocViewer` loads whichever one it needs lazily at runtime instead of bundling all three upfront.
|
|
71
71
|
|
|
72
72
|
```tsx
|
|
73
|
-
import { ImageViewer
|
|
73
|
+
import { ImageViewer } from 'viewdoc'
|
|
74
|
+
import { PdfViewer } from 'viewdoc/pdf'
|
|
75
|
+
import { DocxViewer } from 'viewdoc/docx'
|
|
76
|
+
import { XlsxViewer } from 'viewdoc/xlsx'
|
|
74
77
|
|
|
75
78
|
<ImageViewer uri="https://example.com/photo.jpg" />
|
|
76
79
|
<PdfViewer uri="https://example.com/report.pdf" />
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ViewerShell,
|
|
3
|
+
useZoomPan
|
|
4
|
+
} from "./chunk-OA35SYM6.js";
|
|
5
|
+
|
|
6
|
+
// src/viewers/useDocxHtml.ts
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
import mammoth from "mammoth";
|
|
9
|
+
function useDocxHtml(uri) {
|
|
10
|
+
const [html, setHtml] = useState(null);
|
|
11
|
+
const [error, setError] = useState(null);
|
|
12
|
+
const [loading, setLoading] = useState(true);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
let cancelled = false;
|
|
15
|
+
setLoading(true);
|
|
16
|
+
setError(null);
|
|
17
|
+
setHtml(null);
|
|
18
|
+
fetch(uri).then((res) => {
|
|
19
|
+
if (!res.ok) throw new Error(`Failed to fetch document: ${res.status} ${res.statusText}`);
|
|
20
|
+
return res.arrayBuffer();
|
|
21
|
+
}).then((arrayBuffer) => mammoth.convertToHtml({ arrayBuffer })).then((result) => {
|
|
22
|
+
if (cancelled) return;
|
|
23
|
+
setHtml(result.value);
|
|
24
|
+
setLoading(false);
|
|
25
|
+
}).catch((err) => {
|
|
26
|
+
if (!cancelled) {
|
|
27
|
+
setError(err);
|
|
28
|
+
setLoading(false);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return () => {
|
|
32
|
+
cancelled = true;
|
|
33
|
+
};
|
|
34
|
+
}, [uri]);
|
|
35
|
+
return { html, error, loading };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/viewers/DocxViewer.tsx
|
|
39
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
40
|
+
function defaultDownload(uri, fileName) {
|
|
41
|
+
const link = document.createElement("a");
|
|
42
|
+
link.href = uri;
|
|
43
|
+
link.download = fileName ?? "";
|
|
44
|
+
link.rel = "noopener";
|
|
45
|
+
document.body.appendChild(link);
|
|
46
|
+
link.click();
|
|
47
|
+
document.body.removeChild(link);
|
|
48
|
+
}
|
|
49
|
+
function DocxViewer({
|
|
50
|
+
uri,
|
|
51
|
+
fileName,
|
|
52
|
+
className,
|
|
53
|
+
style,
|
|
54
|
+
width,
|
|
55
|
+
height,
|
|
56
|
+
minScale = 0.5,
|
|
57
|
+
maxScale = 3,
|
|
58
|
+
zoomStep,
|
|
59
|
+
enableZoomControls = true,
|
|
60
|
+
enableWheelZoom = true,
|
|
61
|
+
enableFullscreen = true,
|
|
62
|
+
enableDownload = true,
|
|
63
|
+
onDownload,
|
|
64
|
+
theme,
|
|
65
|
+
floating = true,
|
|
66
|
+
windowDraggable = true,
|
|
67
|
+
defaultPosition
|
|
68
|
+
}) {
|
|
69
|
+
const zoomPan = useZoomPan({
|
|
70
|
+
minScale,
|
|
71
|
+
maxScale,
|
|
72
|
+
zoomStep,
|
|
73
|
+
enablePan: false,
|
|
74
|
+
enableDragScroll: true,
|
|
75
|
+
enableWheelZoom
|
|
76
|
+
});
|
|
77
|
+
const { html, error, loading } = useDocxHtml(uri);
|
|
78
|
+
const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : void 0;
|
|
79
|
+
const wrapperStyle = floating ? { ...style } : { display: "flex", alignItems: "center", justifyContent: "center", width: "100%", height: "100%", ...style };
|
|
80
|
+
return /* @__PURE__ */ jsx("div", { className, style: wrapperStyle, children: /* @__PURE__ */ jsxs(
|
|
81
|
+
ViewerShell,
|
|
82
|
+
{
|
|
83
|
+
zoomPan,
|
|
84
|
+
onDownload: handleDownload,
|
|
85
|
+
enableZoomControls,
|
|
86
|
+
enableFullscreen,
|
|
87
|
+
enablePan: false,
|
|
88
|
+
theme,
|
|
89
|
+
width,
|
|
90
|
+
height,
|
|
91
|
+
floating,
|
|
92
|
+
draggable: windowDraggable,
|
|
93
|
+
defaultPosition,
|
|
94
|
+
transformOrigin: "top center",
|
|
95
|
+
children: [
|
|
96
|
+
loading && /* @__PURE__ */ jsx("span", { className: "vd-doc-status", children: "Loading document\u2026" }),
|
|
97
|
+
error && /* @__PURE__ */ jsxs("span", { className: "vd-doc-status", children: [
|
|
98
|
+
"Failed to load document: ",
|
|
99
|
+
error.message
|
|
100
|
+
] }),
|
|
101
|
+
html && /* @__PURE__ */ jsx("div", { className: "vd-docx-page", dangerouslySetInnerHTML: { __html: html } })
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
) });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export {
|
|
108
|
+
DocxViewer
|
|
109
|
+
};
|
|
110
|
+
//# sourceMappingURL=chunk-4VVVAGYE.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/viewers/useDocxHtml.ts","../src/viewers/DocxViewer.tsx"],"sourcesContent":["import { useEffect, useState } from 'react'\nimport mammoth from 'mammoth'\n\nexport interface UseDocxHtmlResult {\n html: string | null\n error: Error | null\n loading: boolean\n}\n\nexport function useDocxHtml(uri: string): UseDocxHtmlResult {\n const [html, setHtml] = useState<string | null>(null)\n const [error, setError] = useState<Error | null>(null)\n const [loading, setLoading] = useState(true)\n\n useEffect(() => {\n let cancelled = false\n setLoading(true)\n setError(null)\n setHtml(null)\n\n fetch(uri)\n .then((res) => {\n if (!res.ok) throw new Error(`Failed to fetch document: ${res.status} ${res.statusText}`)\n return res.arrayBuffer()\n })\n .then((arrayBuffer) => mammoth.convertToHtml({ arrayBuffer }))\n .then((result) => {\n if (cancelled) return\n setHtml(result.value)\n setLoading(false)\n })\n .catch((err: Error) => {\n if (!cancelled) {\n setError(err)\n setLoading(false)\n }\n })\n\n return () => {\n cancelled = true\n }\n }, [uri])\n\n return { html, error, loading }\n}\n","import type { CSSProperties } from 'react'\nimport { ViewerShell } from '../core/ViewerShell'\nimport type { ViewerTheme } from '../core/theme'\nimport type { Position } from '../core/useDraggable'\nimport { useZoomPan } from '../core/useZoomPan'\nimport { useDocxHtml } from './useDocxHtml'\n\nexport interface DocxViewerProps {\n /** URL of the .docx file to display. */\n uri: string\n /** Used as the suggested filename on download. */\n fileName?: string\n className?: string\n style?: CSSProperties\n\n /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */\n width?: number | string\n /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */\n height?: number | string\n\n /** Minimum zoom scale. Default: 0.5 */\n minScale?: number\n /** Maximum zoom scale. Default: 3 */\n maxScale?: number\n /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */\n zoomStep?: number\n\n /** Show zoom in/out and reset-to-100% controls. Default: true. */\n enableZoomControls?: boolean\n /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */\n enableWheelZoom?: boolean\n /** Show the fullscreen toggle button. Default: true. */\n enableFullscreen?: boolean\n /** Show the download button. Default: true. */\n enableDownload?: boolean\n /** Called when the download button is clicked. If omitted, downloads `uri` directly. */\n onDownload?: () => void\n /** Colors/radii to override the default look (toolbar background, text color, etc). */\n theme?: ViewerTheme\n\n /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */\n floating?: boolean\n /** When floating, allow dragging the window by its toolbar. Default: true. */\n windowDraggable?: boolean\n /** Initial position when floating. Default: centered in the viewport. */\n defaultPosition?: Position\n}\n\nfunction defaultDownload(uri: string, fileName?: string) {\n const link = document.createElement('a')\n link.href = uri\n link.download = fileName ?? ''\n link.rel = 'noopener'\n document.body.appendChild(link)\n link.click()\n document.body.removeChild(link)\n}\n\nexport function DocxViewer({\n uri,\n fileName,\n className,\n style,\n width,\n height,\n minScale = 0.5,\n maxScale = 3,\n zoomStep,\n enableZoomControls = true,\n enableWheelZoom = true,\n enableFullscreen = true,\n enableDownload = true,\n onDownload,\n theme,\n floating = true,\n windowDraggable = true,\n defaultPosition,\n}: DocxViewerProps) {\n const zoomPan = useZoomPan({\n minScale,\n maxScale,\n zoomStep,\n enablePan: false,\n enableDragScroll: true,\n enableWheelZoom,\n })\n const { html, error, loading } = useDocxHtml(uri)\n\n const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : undefined\n\n const wrapperStyle: CSSProperties = floating\n ? { ...style }\n : { display: 'flex', alignItems: 'center', justifyContent: 'center', width: '100%', height: '100%', ...style }\n\n return (\n <div className={className} style={wrapperStyle}>\n <ViewerShell\n zoomPan={zoomPan}\n onDownload={handleDownload}\n enableZoomControls={enableZoomControls}\n enableFullscreen={enableFullscreen}\n enablePan={false}\n theme={theme}\n width={width}\n height={height}\n floating={floating}\n draggable={windowDraggable}\n defaultPosition={defaultPosition}\n transformOrigin=\"top center\"\n >\n {loading && <span className=\"vd-doc-status\">Loading document…</span>}\n {error && <span className=\"vd-doc-status\">Failed to load document: {error.message}</span>}\n {html && <div className=\"vd-docx-page\" dangerouslySetInnerHTML={{ __html: html }} />}\n </ViewerShell>\n </div>\n )\n}\n"],"mappings":";;;;;;AAAA,SAAS,WAAW,gBAAgB;AACpC,OAAO,aAAa;AAQb,SAAS,YAAY,KAAgC;AAC1D,QAAM,CAAC,MAAM,OAAO,IAAI,SAAwB,IAAI;AACpD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AACrD,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,IAAI;AAE3C,YAAU,MAAM;AACd,QAAI,YAAY;AAChB,eAAW,IAAI;AACf,aAAS,IAAI;AACb,YAAQ,IAAI;AAEZ,UAAM,GAAG,EACN,KAAK,CAAC,QAAQ;AACb,UAAI,CAAC,IAAI,GAAI,OAAM,IAAI,MAAM,6BAA6B,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AACxF,aAAO,IAAI,YAAY;AAAA,IACzB,CAAC,EACA,KAAK,CAAC,gBAAgB,QAAQ,cAAc,EAAE,YAAY,CAAC,CAAC,EAC5D,KAAK,CAAC,WAAW;AAChB,UAAI,UAAW;AACf,cAAQ,OAAO,KAAK;AACpB,iBAAW,KAAK;AAAA,IAClB,CAAC,EACA,MAAM,CAAC,QAAe;AACrB,UAAI,CAAC,WAAW;A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X;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,WAAW;AAAA,MACX;AAAA,MACA,iBAAgB;AAAA,MAEf;AAAA,mBAAW,oBAAC,UAAK,WAAU,iBAAgB,oCAAiB;AAAA,QAC5D,SAAS,qBAAC,UAAK,WAAU,iBAAgB;AAAA;AAAA,UAA0B,MAAM;AAAA,WAAQ;AAAA,QACjF,QAAQ,oBAAC,SAAI,WAAU,gBAAe,yBAAyB,EAAE,QAAQ,KAAK,GAAG;AAAA;AAAA;AAAA,EACpF,GACF;AAEJ;","names":[]}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ViewerShell,
|
|
3
|
+
useZoomPan
|
|
4
|
+
} from "./chunk-OA35SYM6.js";
|
|
5
|
+
|
|
6
|
+
// src/viewers/PdfPage.tsx
|
|
7
|
+
import { useEffect, useRef } from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
function PdfPage({ doc, pageNumber, renderScale = 1.5 }) {
|
|
10
|
+
const canvasRef = useRef(null);
|
|
11
|
+
const renderTaskRef = useRef(null);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let cancelled = false;
|
|
14
|
+
doc.getPage(pageNumber).then((page) => {
|
|
15
|
+
if (cancelled) return;
|
|
16
|
+
const viewport = page.getViewport({ scale: renderScale });
|
|
17
|
+
const canvas = canvasRef.current;
|
|
18
|
+
if (!canvas) return;
|
|
19
|
+
const context = canvas.getContext("2d");
|
|
20
|
+
if (!context) return;
|
|
21
|
+
canvas.width = viewport.width;
|
|
22
|
+
canvas.height = viewport.height;
|
|
23
|
+
canvas.style.width = `${viewport.width / renderScale}px`;
|
|
24
|
+
canvas.style.height = `${viewport.height / renderScale}px`;
|
|
25
|
+
const renderTask = page.render({ canvasContext: context, viewport, canvas });
|
|
26
|
+
renderTaskRef.current = renderTask;
|
|
27
|
+
renderTask.promise.catch(() => {
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
return () => {
|
|
31
|
+
cancelled = true;
|
|
32
|
+
renderTaskRef.current?.cancel();
|
|
33
|
+
};
|
|
34
|
+
}, [doc, pageNumber, renderScale]);
|
|
35
|
+
return /* @__PURE__ */ jsx("canvas", { ref: canvasRef, className: "vd-pdf-page", "data-page-number": pageNumber });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/viewers/usePdfDocument.ts
|
|
39
|
+
import { useEffect as useEffect2, useState } from "react";
|
|
40
|
+
import * as pdfjsLib from "pdfjs-dist";
|
|
41
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL("pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url).toString();
|
|
42
|
+
function usePdfDocument(uri) {
|
|
43
|
+
const [doc, setDoc] = useState(null);
|
|
44
|
+
const [error, setError] = useState(null);
|
|
45
|
+
const [loading, setLoading] = useState(true);
|
|
46
|
+
useEffect2(() => {
|
|
47
|
+
let cancelled = false;
|
|
48
|
+
setLoading(true);
|
|
49
|
+
setError(null);
|
|
50
|
+
setDoc(null);
|
|
51
|
+
const loadingTask = pdfjsLib.getDocument({ url: uri });
|
|
52
|
+
loadingTask.promise.then((pdf) => {
|
|
53
|
+
if (cancelled) return;
|
|
54
|
+
setDoc(pdf);
|
|
55
|
+
setLoading(false);
|
|
56
|
+
}).catch((err) => {
|
|
57
|
+
if (!cancelled) {
|
|
58
|
+
setError(err);
|
|
59
|
+
setLoading(false);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return () => {
|
|
63
|
+
cancelled = true;
|
|
64
|
+
loadingTask.destroy();
|
|
65
|
+
};
|
|
66
|
+
}, [uri]);
|
|
67
|
+
return { doc, numPages: doc?.numPages ?? 0, error, loading };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/viewers/useVisiblePage.ts
|
|
71
|
+
import { useEffect as useEffect3, useState as useState2 } from "react";
|
|
72
|
+
function useVisiblePage(containerRef, numPages) {
|
|
73
|
+
const [currentPage, setCurrentPage] = useState2(1);
|
|
74
|
+
useEffect3(() => {
|
|
75
|
+
const root = containerRef.current;
|
|
76
|
+
if (!root || numPages === 0) return;
|
|
77
|
+
const observer = new IntersectionObserver(
|
|
78
|
+
(entries) => {
|
|
79
|
+
const mostVisible = entries.filter((e) => e.isIntersecting).sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
|
|
80
|
+
if (mostVisible) {
|
|
81
|
+
const page = Number(mostVisible.target.dataset.pageNumber);
|
|
82
|
+
if (page) setCurrentPage(page);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{ root, threshold: [0.25, 0.5, 0.75] }
|
|
86
|
+
);
|
|
87
|
+
const pages = root.querySelectorAll("[data-page-number]");
|
|
88
|
+
pages.forEach((el) => observer.observe(el));
|
|
89
|
+
return () => observer.disconnect();
|
|
90
|
+
}, [containerRef, numPages]);
|
|
91
|
+
return currentPage;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/viewers/PdfViewer.tsx
|
|
95
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
96
|
+
function defaultDownload(uri, fileName) {
|
|
97
|
+
const link = document.createElement("a");
|
|
98
|
+
link.href = uri;
|
|
99
|
+
link.download = fileName ?? "";
|
|
100
|
+
link.rel = "noopener";
|
|
101
|
+
document.body.appendChild(link);
|
|
102
|
+
link.click();
|
|
103
|
+
document.body.removeChild(link);
|
|
104
|
+
}
|
|
105
|
+
function PdfViewer({
|
|
106
|
+
uri,
|
|
107
|
+
fileName,
|
|
108
|
+
className,
|
|
109
|
+
style,
|
|
110
|
+
width,
|
|
111
|
+
height,
|
|
112
|
+
minScale = 0.25,
|
|
113
|
+
maxScale = 5,
|
|
114
|
+
zoomStep,
|
|
115
|
+
renderScale,
|
|
116
|
+
enableZoomControls = true,
|
|
117
|
+
enableWheelZoom = true,
|
|
118
|
+
enableFullscreen = true,
|
|
119
|
+
enableDownload = true,
|
|
120
|
+
onDownload,
|
|
121
|
+
theme,
|
|
122
|
+
floating = true,
|
|
123
|
+
windowDraggable = true,
|
|
124
|
+
defaultPosition
|
|
125
|
+
}) {
|
|
126
|
+
const zoomPan = useZoomPan({
|
|
127
|
+
minScale,
|
|
128
|
+
maxScale,
|
|
129
|
+
zoomStep,
|
|
130
|
+
enablePan: false,
|
|
131
|
+
enableDragScroll: true,
|
|
132
|
+
enableWheelZoom
|
|
133
|
+
});
|
|
134
|
+
const { doc, numPages, error, loading } = usePdfDocument(uri);
|
|
135
|
+
const currentPage = useVisiblePage(zoomPan.containerRef, numPages);
|
|
136
|
+
const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : void 0;
|
|
137
|
+
const wrapperStyle = floating ? { ...style } : { display: "flex", alignItems: "center", justifyContent: "center", width: "100%", height: "100%", ...style };
|
|
138
|
+
return /* @__PURE__ */ jsx2("div", { className, style: wrapperStyle, children: /* @__PURE__ */ jsxs(
|
|
139
|
+
ViewerShell,
|
|
140
|
+
{
|
|
141
|
+
zoomPan,
|
|
142
|
+
onDownload: handleDownload,
|
|
143
|
+
enableZoomControls,
|
|
144
|
+
enableFullscreen,
|
|
145
|
+
enablePan: false,
|
|
146
|
+
theme,
|
|
147
|
+
width,
|
|
148
|
+
height,
|
|
149
|
+
floating,
|
|
150
|
+
draggable: windowDraggable,
|
|
151
|
+
defaultPosition,
|
|
152
|
+
transformOrigin: "top center",
|
|
153
|
+
toolbarExtra: numPages > 0 ? /* @__PURE__ */ jsxs("span", { className: "vd-pdf-page-indicator", children: [
|
|
154
|
+
"Page ",
|
|
155
|
+
currentPage,
|
|
156
|
+
" / ",
|
|
157
|
+
numPages
|
|
158
|
+
] }) : void 0,
|
|
159
|
+
children: [
|
|
160
|
+
loading && /* @__PURE__ */ jsx2("span", { className: "vd-pdf-page-indicator", children: "Loading PDF\u2026" }),
|
|
161
|
+
error && /* @__PURE__ */ jsxs("span", { className: "vd-pdf-page-indicator", children: [
|
|
162
|
+
"Failed to load PDF: ",
|
|
163
|
+
error.message
|
|
164
|
+
] }),
|
|
165
|
+
doc && /* @__PURE__ */ jsx2("div", { className: "vd-pdf-pages", children: Array.from({ length: numPages }, (_, i) => /* @__PURE__ */ jsx2(PdfPage, { doc, pageNumber: i + 1, renderScale }, i + 1)) })
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
) });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export {
|
|
172
|
+
PdfViewer
|
|
173
|
+
};
|
|
174
|
+
//# sourceMappingURL=chunk-KRN2POC4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/viewers/PdfPage.tsx","../src/viewers/usePdfDocument.ts","../src/viewers/useVisiblePage.ts","../src/viewers/PdfViewer.tsx"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport type { PDFDocumentProxy } from 'pdfjs-dist'\n\nexport interface PdfPageProps {\n doc: PDFDocumentProxy\n pageNumber: number\n /** Render resolution multiplier. Higher = crisper at deep zoom, more memory. Default: 1.5 */\n renderScale?: number\n}\n\nexport function PdfPage({ doc, pageNumber, renderScale = 1.5 }: PdfPageProps) {\n const canvasRef = useRef<HTMLCanvasElement>(null)\n const renderTaskRef = useRef<{ cancel: () => void } | null>(null)\n\n useEffect(() => {\n let cancelled = false\n\n doc.getPage(pageNumber).then((page) => {\n if (cancelled) return\n const viewport = page.getViewport({ scale: renderScale })\n const canvas = canvasRef.current\n if (!canvas) return\n const context = canvas.getContext('2d')\n if (!context) return\n\n canvas.width = viewport.width\n canvas.height = viewport.height\n canvas.style.width = `${viewport.width / renderScale}px`\n canvas.style.height = `${viewport.height / renderScale}px`\n\n const renderTask = page.render({ canvasContext: context, viewport, canvas })\n renderTaskRef.current = renderTask\n renderTask.promise.catch(() => {\n // ignore cancellation errors from rapid re-renders\n })\n })\n\n return () => {\n cancelled = true\n renderTaskRef.current?.cancel()\n }\n }, [doc, pageNumber, renderScale])\n\n return <canvas ref={canvasRef} className=\"vd-pdf-page\" data-page-number={pageNumber} />\n}\n","import { useEffect, useState } from 'react'\nimport * as pdfjsLib from 'pdfjs-dist'\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist'\n\npdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString()\n\nexport interface UsePdfDocumentResult {\n doc: PDFDocumentProxy | null\n numPages: number\n error: Error | null\n loading: boolean\n}\n\nexport function usePdfDocument(uri: string): UsePdfDocumentResult {\n const [doc, setDoc] = useState<PDFDocumentProxy | null>(null)\n const [error, setError] = useState<Error | null>(null)\n const [loading, setLoading] = useState(true)\n\n useEffect(() => {\n let cancelled = false\n setLoading(true)\n setError(null)\n setDoc(null)\n\n const loadingTask: PDFDocumentLoadingTask = pdfjsLib.getDocument({ url: uri })\n loadingTask.promise\n .then((pdf) => {\n if (cancelled) return\n setDoc(pdf)\n setLoading(false)\n })\n .catch((err: Error) => {\n if (!cancelled) {\n setError(err)\n setLoading(false)\n }\n })\n\n return () => {\n cancelled = true\n loadingTask.destroy()\n }\n }, [uri])\n\n return { doc, numPages: doc?.numPages ?? 0, error, loading }\n}\n","import { useEffect, useState } from 'react'\n\n/** Tracks which page (by data-page-number) is most visible inside the given scroll container. */\nexport function useVisiblePage(containerRef: React.RefObject<HTMLElement>, numPages: number): number {\n const [currentPage, setCurrentPage] = useState(1)\n\n useEffect(() => {\n const root = containerRef.current\n if (!root || numPages === 0) return\n\n const observer = new IntersectionObserver(\n (entries) => {\n const mostVisible = entries\n .filter((e) => e.isIntersecting)\n .sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0]\n if (mostVisible) {\n const page = Number((mostVisible.target as HTMLElement).dataset.pageNumber)\n if (page) setCurrentPage(page)\n }\n },\n { root, threshold: [0.25, 0.5, 0.75] }\n )\n\n const pages = root.querySelectorAll('[data-page-number]')\n pages.forEach((el) => observer.observe(el))\n\n return () => observer.disconnect()\n }, [containerRef, numPages])\n\n return currentPage\n}\n","import type { CSSProperties } from 'react'\nimport { ViewerShell } from '../core/ViewerShell'\nimport type { ViewerTheme } from '../core/theme'\nimport type { Position } from '../core/useDraggable'\nimport { useZoomPan } from '../core/useZoomPan'\nimport { PdfPage } from './PdfPage'\nimport { usePdfDocument } from './usePdfDocument'\nimport { useVisiblePage } from './useVisiblePage'\n\nexport interface PdfViewerProps {\n /** URL or data URI of the PDF to display. */\n uri: string\n /** Used as the suggested filename on download. */\n fileName?: string\n className?: string\n style?: CSSProperties\n\n /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */\n width?: number | string\n /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */\n height?: number | string\n\n /** Minimum zoom scale. Default: 0.25 */\n minScale?: number\n /** Maximum zoom scale. Default: 5 */\n maxScale?: number\n /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */\n zoomStep?: number\n /** Canvas render resolution multiplier (crispness at high zoom). Default: 1.5 */\n renderScale?: number\n\n /** Show zoom in/out and reset-to-100% controls. Default: true. */\n enableZoomControls?: boolean\n /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */\n enableWheelZoom?: boolean\n /** Show the fullscreen toggle button. Default: true. */\n enableFullscreen?: boolean\n /** Show the download button. Default: true. */\n enableDownload?: boolean\n /** Called when the download button is clicked. If omitted, downloads `uri` directly. */\n onDownload?: () => void\n /** Colors/radii to override the default look (toolbar background, text color, etc). */\n theme?: ViewerTheme\n\n /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */\n floating?: boolean\n /** When floating, allow dragging the window by its toolbar. Default: true. */\n windowDraggable?: boolean\n /** Initial position when floating. Default: centered in the viewport. */\n defaultPosition?: Position\n}\n\nfunction defaultDownload(uri: string, fileName?: string) {\n const link = document.createElement('a')\n link.href = uri\n link.download = fileName ?? ''\n link.rel = 'noopener'\n document.body.appendChild(link)\n link.click()\n document.body.removeChild(link)\n}\n\nexport function PdfViewer({\n uri,\n fileName,\n className,\n style,\n width,\n height,\n minScale = 0.25,\n maxScale = 5,\n zoomStep,\n renderScale,\n enableZoomControls = true,\n enableWheelZoom = true,\n enableFullscreen = true,\n enableDownload = true,\n onDownload,\n theme,\n floating = true,\n windowDraggable = true,\n defaultPosition,\n}: PdfViewerProps) {\n const zoomPan = useZoomPan({\n minScale,\n maxScale,\n zoomStep,\n enablePan: false,\n enableDragScroll: true,\n enableWheelZoom,\n })\n const { doc, numPages, error, loading } = usePdfDocument(uri)\n const currentPage = useVisiblePage(zoomPan.containerRef, numPages)\n\n const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : undefined\n\n const wrapperStyle: CSSProperties = floating\n ? { ...style }\n : { display: 'flex', alignItems: 'center', justifyContent: 'center', width: '100%', height: '100%', ...style }\n\n return (\n <div className={className} style={wrapperStyle}>\n <ViewerShell\n zoomPan={zoomPan}\n onDownload={handleDownload}\n enableZoomControls={enableZoomControls}\n enableFullscreen={enableFullscreen}\n enablePan={false}\n theme={theme}\n width={width}\n height={height}\n floating={floating}\n draggable={windowDraggable}\n defaultPosition={defaultPosition}\n transformOrigin=\"top center\"\n toolbarExtra={\n numPages > 0 ? (\n <span className=\"vd-pdf-page-indicator\">\n Page {currentPage} / {numPages}\n </span>\n ) : undefined\n }\n >\n {loading && <span className=\"vd-pdf-page-indicator\">Loading PDF…</span>}\n {error && <span className=\"vd-pdf-page-indicator\">Failed to load PDF: {error.message}</span>}\n {doc && (\n <div className=\"vd-pdf-pages\">\n {Array.from({ length: numPages }, (_, i) => (\n <PdfPage key={i + 1} doc={doc} pageNumber={i + 1} renderScale={renderScale} />\n ))}\n </div>\n )}\n </ViewerShell>\n </div>\n )\n}\n"],"mappings":";;;;;;AAAA,SAAS,WAAW,cAAc;AA2CzB;AAjCF,SAAS,QAAQ,EAAE,KAAK,YAAY,cAAc,IAAI,GAAiB;AAC5E,QAAM,YAAY,OAA0B,IAAI;AAChD,QAAM,gBAAgB,OAAsC,IAAI;AAEhE,YAAU,MAAM;AACd,QAAI,YAA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K,WAAU,yBAAwB,+BAAY;AAAA,QAC/D,SAAS,qBAAC,UAAK,WAAU,yBAAwB;AAAA;AAAA,UAAqB,MAAM;AAAA,WAAQ;AAAA,QACpF,OACC,gBAAAA,KAAC,SAAI,WAAU,gBACZ,gBAAM,KAAK,EAAE,QAAQ,SAAS,GAAG,CAAC,GAAG,MACpC,gBAAAA,KAAC,WAAoB,KAAU,YAAY,IAAI,GAAG,eAApC,IAAI,CAA0D,CAC7E,GACH;AAAA;AAAA;AAAA,EAEJ,GACF;AAEJ;","names":["useEffect","useEffect","useState","jsx"]}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ViewerShell,
|
|
3
|
+
useZoomPan
|
|
4
|
+
} from "./chunk-OA35SYM6.js";
|
|
5
|
+
|
|
6
|
+
// src/viewers/XlsxViewer.tsx
|
|
7
|
+
import { useMemo, useState as useState2 } from "react";
|
|
8
|
+
import * as XLSX2 from "xlsx";
|
|
9
|
+
|
|
10
|
+
// src/viewers/useXlsxWorkbook.ts
|
|
11
|
+
import { useEffect, useState } from "react";
|
|
12
|
+
import * as XLSX from "xlsx";
|
|
13
|
+
function useXlsxWorkbook(uri) {
|
|
14
|
+
const [workbook, setWorkbook] = useState(null);
|
|
15
|
+
const [error, setError] = useState(null);
|
|
16
|
+
const [loading, setLoading] = useState(true);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let cancelled = false;
|
|
19
|
+
setLoading(true);
|
|
20
|
+
setError(null);
|
|
21
|
+
setWorkbook(null);
|
|
22
|
+
fetch(uri).then((res) => {
|
|
23
|
+
if (!res.ok) throw new Error(`Failed to fetch spreadsheet: ${res.status} ${res.statusText}`);
|
|
24
|
+
return res.arrayBuffer();
|
|
25
|
+
}).then((arrayBuffer) => {
|
|
26
|
+
if (cancelled) return;
|
|
27
|
+
const wb = XLSX.read(arrayBuffer, { type: "array" });
|
|
28
|
+
setWorkbook(wb);
|
|
29
|
+
setLoading(false);
|
|
30
|
+
}).catch((err) => {
|
|
31
|
+
if (!cancelled) {
|
|
32
|
+
setError(err);
|
|
33
|
+
setLoading(false);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return () => {
|
|
37
|
+
cancelled = true;
|
|
38
|
+
};
|
|
39
|
+
}, [uri]);
|
|
40
|
+
return { workbook, error, loading };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/viewers/XlsxViewer.tsx
|
|
44
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
45
|
+
function defaultDownload(uri, fileName) {
|
|
46
|
+
const link = document.createElement("a");
|
|
47
|
+
link.href = uri;
|
|
48
|
+
link.download = fileName ?? "";
|
|
49
|
+
link.rel = "noopener";
|
|
50
|
+
document.body.appendChild(link);
|
|
51
|
+
link.click();
|
|
52
|
+
document.body.removeChild(link);
|
|
53
|
+
}
|
|
54
|
+
function XlsxViewer({
|
|
55
|
+
uri,
|
|
56
|
+
fileName,
|
|
57
|
+
className,
|
|
58
|
+
style,
|
|
59
|
+
width,
|
|
60
|
+
height,
|
|
61
|
+
minScale = 0.5,
|
|
62
|
+
maxScale = 3,
|
|
63
|
+
zoomStep,
|
|
64
|
+
enableZoomControls = true,
|
|
65
|
+
enableWheelZoom = true,
|
|
66
|
+
enableFullscreen = true,
|
|
67
|
+
enableDownload = true,
|
|
68
|
+
onDownload,
|
|
69
|
+
theme,
|
|
70
|
+
floating = true,
|
|
71
|
+
windowDraggable = true,
|
|
72
|
+
defaultPosition
|
|
73
|
+
}) {
|
|
74
|
+
const zoomPan = useZoomPan({
|
|
75
|
+
minScale,
|
|
76
|
+
maxScale,
|
|
77
|
+
zoomStep,
|
|
78
|
+
enablePan: false,
|
|
79
|
+
enableDragScroll: true,
|
|
80
|
+
enableWheelZoom
|
|
81
|
+
});
|
|
82
|
+
const { workbook, error, loading } = useXlsxWorkbook(uri);
|
|
83
|
+
const [activeSheet, setActiveSheet] = useState2(0);
|
|
84
|
+
const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : void 0;
|
|
85
|
+
const tableHtml = useMemo(() => {
|
|
86
|
+
if (!workbook) return null;
|
|
87
|
+
const sheetName = workbook.SheetNames[activeSheet];
|
|
88
|
+
const sheet = workbook.Sheets[sheetName];
|
|
89
|
+
if (!sheet) return null;
|
|
90
|
+
return XLSX2.utils.sheet_to_html(sheet, { editable: false });
|
|
91
|
+
}, [workbook, activeSheet]);
|
|
92
|
+
const wrapperStyle = floating ? { ...style } : { display: "flex", alignItems: "center", justifyContent: "center", width: "100%", height: "100%", ...style };
|
|
93
|
+
return /* @__PURE__ */ jsx("div", { className, style: wrapperStyle, children: /* @__PURE__ */ jsxs(
|
|
94
|
+
ViewerShell,
|
|
95
|
+
{
|
|
96
|
+
zoomPan,
|
|
97
|
+
onDownload: handleDownload,
|
|
98
|
+
enableZoomControls,
|
|
99
|
+
enableFullscreen,
|
|
100
|
+
enablePan: false,
|
|
101
|
+
theme,
|
|
102
|
+
width,
|
|
103
|
+
height,
|
|
104
|
+
floating,
|
|
105
|
+
draggable: windowDraggable,
|
|
106
|
+
defaultPosition,
|
|
107
|
+
transformOrigin: "top left",
|
|
108
|
+
children: [
|
|
109
|
+
loading && /* @__PURE__ */ jsx("span", { className: "vd-doc-status", children: "Loading spreadsheet\u2026" }),
|
|
110
|
+
error && /* @__PURE__ */ jsxs("span", { className: "vd-doc-status", children: [
|
|
111
|
+
"Failed to load spreadsheet: ",
|
|
112
|
+
error.message
|
|
113
|
+
] }),
|
|
114
|
+
workbook && /* @__PURE__ */ jsxs("div", { className: "vd-xlsx", children: [
|
|
115
|
+
workbook.SheetNames.length > 1 && /* @__PURE__ */ jsx("div", { className: "vd-xlsx-tabs", children: workbook.SheetNames.map((name, i) => /* @__PURE__ */ jsx(
|
|
116
|
+
"button",
|
|
117
|
+
{
|
|
118
|
+
type: "button",
|
|
119
|
+
className: `vd-xlsx-tab${i === activeSheet ? " vd-xlsx-tab--active" : ""}`,
|
|
120
|
+
onClick: () => setActiveSheet(i),
|
|
121
|
+
children: name
|
|
122
|
+
},
|
|
123
|
+
name
|
|
124
|
+
)) }),
|
|
125
|
+
tableHtml && /* @__PURE__ */ jsx("div", { className: "vd-xlsx-table", dangerouslySetInnerHTML: { __html: tableHtml } })
|
|
126
|
+
] })
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
) });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export {
|
|
133
|
+
XlsxViewer
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=chunk-LE4YTMPN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/viewers/XlsxViewer.tsx","../src/viewers/useXlsxWorkbook.ts"],"sourcesContent":["import { useMemo, useState, type CSSProperties } from 'react'\nimport * as XLSX from 'xlsx'\nimport { ViewerShell } from '../core/ViewerShell'\nimport type { ViewerTheme } from '../core/theme'\nimport type { Position } from '../core/useDraggable'\nimport { useZoomPan } from '../core/useZoomPan'\nimport { useXlsxWorkbook } from './useXlsxWorkbook'\n\nexport interface XlsxViewerProps {\n /** URL of the .xlsx/.xls (legacy binary supported)/.csv file to display. */\n uri: string\n /** Used as the suggested filename on download. */\n fileName?: string\n className?: string\n style?: CSSProperties\n\n /** Width of the viewer. Accepts any CSS size (e.g. 1200, '1200px', '100%'). Default: 1200. */\n width?: number | string\n /** Height of the viewer. Accepts any CSS size (e.g. 700, '700px', '100vh'). Default: 700. */\n height?: number | string\n\n /** Minimum zoom scale. Default: 0.5 */\n minScale?: number\n /** Maximum zoom scale. Default: 3 */\n maxScale?: number\n /** Zoom increment per step (buttons / Ctrl+scroll). Default: 0.25 */\n zoomStep?: number\n\n /** Show zoom in/out and reset-to-100% controls. Default: true. */\n enableZoomControls?: boolean\n /** Allow Ctrl/Cmd + scroll wheel to zoom. Default: true. */\n enableWheelZoom?: boolean\n /** Show the fullscreen toggle button. Default: true. */\n enableFullscreen?: boolean\n /** Show the download button. Default: true. */\n enableDownload?: boolean\n /** Called when the download button is clicked. If omitted, downloads `uri` directly. */\n onDownload?: () => void\n /** Colors/radii to override the default look (toolbar background, text color, etc). */\n theme?: ViewerTheme\n\n /** Render as a floating window (position: fixed) that can be dragged, instead of filling the parent. Default: true. */\n floating?: boolean\n /** When floating, allow dragging the window by its toolbar. Default: true. */\n windowDraggable?: boolean\n /** Initial position when floating. Default: centered in the viewport. */\n defaultPosition?: Position\n}\n\nfunction defaultDownload(uri: string, fileName?: string) {\n const link = document.createElement('a')\n link.href = uri\n link.download = fileName ?? ''\n link.rel = 'noopener'\n document.body.appendChild(link)\n link.click()\n document.body.removeChild(link)\n}\n\nexport function XlsxViewer({\n uri,\n fileName,\n className,\n style,\n width,\n height,\n minScale = 0.5,\n maxScale = 3,\n zoomStep,\n enableZoomControls = true,\n enableWheelZoom = true,\n enableFullscreen = true,\n enableDownload = true,\n onDownload,\n theme,\n floating = true,\n windowDraggable = true,\n defaultPosition,\n}: XlsxViewerProps) {\n const zoomPan = useZoomPan({\n minScale,\n maxScale,\n zoomStep,\n enablePan: false,\n enableDragScroll: true,\n enableWheelZoom,\n })\n const { workbook, error, loading } = useXlsxWorkbook(uri)\n const [activeSheet, setActiveSheet] = useState(0)\n\n const handleDownload = enableDownload ? onDownload ?? (() => defaultDownload(uri, fileName)) : undefined\n\n const tableHtml = useMemo(() => {\n if (!workbook) return null\n const sheetName = workbook.SheetNames[activeSheet]\n const sheet = workbook.Sheets[sheetName]\n if (!sheet) return null\n return XLSX.utils.sheet_to_html(sheet, { editable: false })\n }, [workbook, activeSheet])\n\n const wrapperStyle: CSSProperties = floating\n ? { ...style }\n : { display: 'flex', alignItems: 'center', justifyContent: 'center', width: '100%', height: '100%', ...style }\n\n return (\n <div className={className} style={wrapperStyle}>\n <ViewerShell\n zoomPan={zoomPan}\n onDownload={handleDownload}\n enableZoomControls={enableZoomControls}\n enableFullscreen={enableFullscreen}\n enablePan={false}\n theme={theme}\n width={width}\n height={height}\n floating={floating}\n draggable={windowDraggable}\n defaultPosition={defaultPosition}\n transformOrigin=\"top left\"\n >\n {loading && <span className=\"vd-doc-status\">Loading spreadsheet…</span>}\n {error && <span className=\"vd-doc-status\">Failed to load spreadsheet: {error.message}</span>}\n {workbook && (\n <div className=\"vd-xlsx\">\n {workbook.SheetNames.length > 1 && (\n <div className=\"vd-xlsx-tabs\">\n {workbook.SheetNames.map((name, i) => (\n <button\n key={name}\n type=\"button\"\n className={`vd-xlsx-tab${i === activeSheet ? ' vd-xlsx-tab--active' : ''}`}\n onClick={() => setActiveSheet(i)}\n >\n {name}\n </button>\n ))}\n </div>\n )}\n {tableHtml && <div className=\"vd-xlsx-table\" dangerouslySetInnerHTML={{ __html: tableHtml }} />}\n </div>\n )}\n </ViewerShell>\n </div>\n )\n}\n","import { useEffect, useState } from 'react'\nimport * as XLSX from 'xlsx'\n\nexport interface UseXlsxWorkbookResult {\n workbook: XLSX.WorkBook | null\n error: Error | null\n loading: boolean\n}\n\nexport function useXlsxWorkbook(uri: string): UseXlsxWorkbookResult {\n const [workbook, setWorkbook] = useState<XLSX.WorkBook | null>(null)\n const [error, setError] = useState<Error | null>(null)\n const [loading, setLoading] = useState(true)\n\n useEffect(() => {\n let cancelled = false\n setLoading(true)\n setError(null)\n setWorkbook(null)\n\n fetch(uri)\n .then((res) => {\n if (!res.ok) throw new Error(`Failed to fetch spreadsheet: ${res.status} ${res.statusText}`)\n return res.arrayBuffer()\n })\n .then((arrayBuffer) => {\n if (cancelled) return\n const wb = XLSX.read(arrayBuffer, { type: 'array' })\n setWorkbook(wb)\n setLoading(false)\n })\n .catch((err: Error) => {\n if (!cancelled) {\n setError(err)\n setLoading(false)\n }\n })\n\n return () => {\n cancelled = true\n }\n }, [uri])\n\n return { workbook, error, loading }\n}\n"],"mappings":";;;;;;AAAA,SAAS,SAAS,YAAAA,iBAAoC;AACtD,YAAYC,WAAU;;;ACDtB,SAAS,WAAW,gBAAgB;AACpC,YAAY,UAAU;AAQf,SAAS,gBAAgB,KAAoC;AAClE,QAAM,CAAC,UAAU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