@ztwoint/z-ui 0.1.148 → 0.1.149
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.
|
@@ -1,69 +1,73 @@
|
|
|
1
|
-
import { useRef as c, useState as
|
|
2
|
-
function
|
|
3
|
-
if (typeof
|
|
1
|
+
import { useRef as c, useState as U, useCallback as h, useEffect as B } from "react";
|
|
2
|
+
function W(r) {
|
|
3
|
+
if (typeof r == "string")
|
|
4
4
|
try {
|
|
5
|
-
const n = new URL(
|
|
5
|
+
const n = new URL(r, "http://localhost").pathname.split("/").pop();
|
|
6
6
|
if (n && n.endsWith(".pdf")) return decodeURIComponent(n);
|
|
7
7
|
} catch {
|
|
8
8
|
}
|
|
9
|
-
return
|
|
9
|
+
return r instanceof File ? r.name : "PDF Preview";
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
const { src:
|
|
13
|
-
i.current =
|
|
14
|
-
const f = c(
|
|
15
|
-
f.current =
|
|
16
|
-
const d = c(
|
|
17
|
-
d.current =
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
function z(r) {
|
|
12
|
+
const { src: a, open: n, onOpenChange: u, onError: C, onLoad: w, downloadFilename: L } = r, i = c(a);
|
|
13
|
+
i.current = a;
|
|
14
|
+
const f = c(w);
|
|
15
|
+
f.current = w;
|
|
16
|
+
const d = c(C);
|
|
17
|
+
d.current = C;
|
|
18
|
+
const P = c(L);
|
|
19
|
+
P.current = L;
|
|
20
|
+
const [O, S] = U(!1), [j, p] = U("idle"), [D, m] = U(null), [T, g] = U(null), b = c(null), v = c(null), y = n !== void 0, F = y ? n : O, o = h(() => {
|
|
21
|
+
b.current && (URL.revokeObjectURL(b.current), b.current = null);
|
|
22
|
+
}, []), R = h(async () => {
|
|
23
|
+
var s, E, k;
|
|
24
|
+
(s = v.current) == null || s.abort();
|
|
23
25
|
const e = new AbortController();
|
|
24
|
-
|
|
26
|
+
v.current = e, p("loading"), g(null), m(null), o();
|
|
25
27
|
try {
|
|
26
|
-
let
|
|
27
|
-
if (typeof i.current == "function" ?
|
|
28
|
-
if (typeof
|
|
29
|
-
|
|
28
|
+
let t;
|
|
29
|
+
if (typeof i.current == "function" ? t = await i.current(e.signal) : t = i.current, e.signal.aborted) return;
|
|
30
|
+
if (typeof t == "string")
|
|
31
|
+
m(t);
|
|
30
32
|
else {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
+
const l = URL.createObjectURL(t);
|
|
34
|
+
b.current = l;
|
|
35
|
+
const I = P.current || (t instanceof File ? t.name : null);
|
|
36
|
+
m(I ? `${l}#${encodeURIComponent(I)}` : l);
|
|
33
37
|
}
|
|
34
38
|
p("ready"), (E = f.current) == null || E.call(f);
|
|
35
|
-
} catch (
|
|
39
|
+
} catch (t) {
|
|
36
40
|
if (e.signal.aborted) return;
|
|
37
|
-
const
|
|
38
|
-
message:
|
|
39
|
-
cause:
|
|
41
|
+
const l = {
|
|
42
|
+
message: t instanceof Error ? t.message : "Failed to load PDF",
|
|
43
|
+
cause: t
|
|
40
44
|
};
|
|
41
|
-
g(
|
|
45
|
+
g(l), p("error"), (k = d.current) == null || k.call(d, l);
|
|
42
46
|
}
|
|
43
|
-
}, [o]),
|
|
47
|
+
}, [o]), $ = h(
|
|
44
48
|
(e) => {
|
|
45
|
-
var
|
|
46
|
-
y ||
|
|
49
|
+
var s;
|
|
50
|
+
y || S(e), u == null || u(e), e || ((s = v.current) == null || s.abort(), p("idle"), m(null), g(null), o());
|
|
47
51
|
},
|
|
48
|
-
[y,
|
|
52
|
+
[y, u, o]
|
|
49
53
|
);
|
|
50
|
-
|
|
54
|
+
B(() => (F && R(), () => {
|
|
51
55
|
var e;
|
|
52
|
-
(e =
|
|
53
|
-
}), [
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
}, [
|
|
56
|
+
(e = v.current) == null || e.abort(), o();
|
|
57
|
+
}), [F, R, o]);
|
|
58
|
+
const x = h(() => {
|
|
59
|
+
R();
|
|
60
|
+
}, [R]), A = W(a);
|
|
57
61
|
return {
|
|
58
|
-
open:
|
|
59
|
-
handleOpenChange:
|
|
60
|
-
status:
|
|
61
|
-
resolvedUrl:
|
|
62
|
-
error:
|
|
63
|
-
retry:
|
|
64
|
-
resolvedTitle:
|
|
62
|
+
open: F,
|
|
63
|
+
handleOpenChange: $,
|
|
64
|
+
status: j,
|
|
65
|
+
resolvedUrl: D,
|
|
66
|
+
error: T,
|
|
67
|
+
retry: x,
|
|
68
|
+
resolvedTitle: A
|
|
65
69
|
};
|
|
66
70
|
}
|
|
67
71
|
export {
|
|
68
|
-
|
|
72
|
+
z as usePdfPreview
|
|
69
73
|
};
|
|
@@ -1,71 +1,76 @@
|
|
|
1
1
|
import { jsxs as P, jsx as o } from "react/jsx-runtime";
|
|
2
|
-
import * as
|
|
3
|
-
import { cn as
|
|
4
|
-
import { Z2Dialog as
|
|
5
|
-
import { PDF_PREVIEW_DEFAULTS as
|
|
6
|
-
import { usePdfPreview as
|
|
7
|
-
import { PdfToolbar as
|
|
8
|
-
import { PdfViewer as
|
|
9
|
-
import { PdfLoadingState as
|
|
2
|
+
import * as y from "react";
|
|
3
|
+
import { cn as _ } from "../../lib/utils.js";
|
|
4
|
+
import { Z2Dialog as j, Z2DialogTrigger as L, Z2DialogContent as O, Z2DialogTitle as U } from "../dialog/dialog.js";
|
|
5
|
+
import { PDF_PREVIEW_DEFAULTS as c, pdfDialogContentVariants as $ } from "./pdf-preview.const.js";
|
|
6
|
+
import { usePdfPreview as z } from "./pdf-preview.hook.js";
|
|
7
|
+
import { PdfToolbar as A } from "./components/pdf-toolbar.js";
|
|
8
|
+
import { PdfViewer as F } from "./components/pdf-viewer.js";
|
|
9
|
+
import { PdfLoadingState as I } from "./components/pdf-loading-state.js";
|
|
10
10
|
import { PdfErrorState as q } from "./components/pdf-error-state.js";
|
|
11
11
|
const B = ({
|
|
12
|
-
src:
|
|
13
|
-
title:
|
|
14
|
-
size: D =
|
|
12
|
+
src: w,
|
|
13
|
+
title: u,
|
|
14
|
+
size: D = c.size,
|
|
15
15
|
open: C,
|
|
16
16
|
onOpenChange: E,
|
|
17
17
|
disabled: t = !1,
|
|
18
|
-
showToolbar:
|
|
19
|
-
showDownload: T =
|
|
20
|
-
downloadFilename:
|
|
21
|
-
onDownload:
|
|
18
|
+
showToolbar: g = c.showToolbar,
|
|
19
|
+
showDownload: T = c.showDownload,
|
|
20
|
+
downloadFilename: f,
|
|
21
|
+
onDownload: d,
|
|
22
22
|
loadingContent: b,
|
|
23
|
-
errorContent:
|
|
24
|
-
renderPdf:
|
|
23
|
+
errorContent: i,
|
|
24
|
+
renderPdf: h,
|
|
25
25
|
className: N,
|
|
26
26
|
viewerClassName: Z,
|
|
27
27
|
onError: k,
|
|
28
28
|
onLoad: x,
|
|
29
|
-
children:
|
|
29
|
+
children: v
|
|
30
30
|
}) => {
|
|
31
|
-
const { open:
|
|
32
|
-
src:
|
|
31
|
+
const { open: l, handleOpenChange: s, status: a, resolvedUrl: r, error: p, retry: R, resolvedTitle: S } = z({
|
|
32
|
+
src: w,
|
|
33
33
|
open: C,
|
|
34
34
|
onOpenChange: E,
|
|
35
35
|
onError: k,
|
|
36
|
-
onLoad: x
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
onLoad: x,
|
|
37
|
+
downloadFilename: (() => {
|
|
38
|
+
const e = f || u;
|
|
39
|
+
if (e)
|
|
40
|
+
return e.endsWith(".pdf") ? e : `${e}.pdf`;
|
|
41
|
+
})()
|
|
42
|
+
}), n = u || S, V = y.useCallback(() => {
|
|
43
|
+
if (!r)
|
|
39
44
|
return;
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
}, [
|
|
46
|
-
return
|
|
47
|
-
t &&
|
|
48
|
-
}, [t,
|
|
49
|
-
/* @__PURE__ */ P(
|
|
50
|
-
|
|
45
|
+
d && d();
|
|
46
|
+
const e = document.createElement("a");
|
|
47
|
+
e.href = r;
|
|
48
|
+
const m = f || n || "document.pdf";
|
|
49
|
+
e.download = m.endsWith(".pdf") ? m : `${m}.pdf`, e.target = "_blank", e.rel = "noopener noreferrer", document.body.appendChild(e), e.click(), document.body.removeChild(e);
|
|
50
|
+
}, [d, r, f, n]), W = () => a === "idle" || a === "loading" ? b || /* @__PURE__ */ o(I, {}) : a === "error" && p ? i ? typeof i == "function" ? i(p) : i : /* @__PURE__ */ o(q, { error: p, onRetry: R }) : a === "ready" && r ? h ? h(r) : /* @__PURE__ */ o(F, { url: r, title: n, className: Z }) : null;
|
|
51
|
+
return y.useEffect(() => {
|
|
52
|
+
t && l && s(!1);
|
|
53
|
+
}, [t, l, s]), // Z2Dialog wraps Radix Dialog — manages focus trapping, escape key, and overlay click-to-close
|
|
54
|
+
/* @__PURE__ */ P(j, { open: t ? !1 : l, onOpenChange: t ? void 0 : s, children: [
|
|
55
|
+
v && /* @__PURE__ */ o(L, { asChild: !0, disabled: t, "data-slot": "pdf-preview-trigger", children: v }),
|
|
51
56
|
/* @__PURE__ */ P(
|
|
52
|
-
|
|
57
|
+
O,
|
|
53
58
|
{
|
|
54
59
|
"data-slot": "pdf-preview-content",
|
|
55
|
-
className:
|
|
60
|
+
className: _($({ size: D }), N),
|
|
56
61
|
"aria-describedby": void 0,
|
|
57
62
|
children: [
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
g && /* @__PURE__ */ o(
|
|
64
|
+
A,
|
|
60
65
|
{
|
|
61
|
-
title:
|
|
66
|
+
title: n,
|
|
62
67
|
showDownload: T,
|
|
63
68
|
onDownload: V,
|
|
64
|
-
downloadDisabled: !
|
|
69
|
+
downloadDisabled: !r
|
|
65
70
|
}
|
|
66
71
|
),
|
|
67
|
-
!
|
|
68
|
-
|
|
72
|
+
!g && /* @__PURE__ */ o(U, { className: "sr-only", children: n }),
|
|
73
|
+
W()
|
|
69
74
|
]
|
|
70
75
|
}
|
|
71
76
|
)
|
|
@@ -69,6 +69,9 @@ export interface UsePdfPreviewProps {
|
|
|
69
69
|
onOpenChange?: (open: boolean) => void;
|
|
70
70
|
onError?: (error: PdfPreviewError) => void;
|
|
71
71
|
onLoad?: () => void;
|
|
72
|
+
/** Filename appended as a URL fragment to blob URLs so the browser's
|
|
73
|
+
* built-in PDF viewer uses it for "Save As" instead of the blob UUID. */
|
|
74
|
+
downloadFilename?: string;
|
|
72
75
|
}
|
|
73
76
|
/** Return value of usePdfPreview — provides state and handlers to the main component. */
|
|
74
77
|
export interface UsePdfPreviewReturn {
|
|
@@ -69,6 +69,9 @@ export interface UsePdfPreviewProps {
|
|
|
69
69
|
onOpenChange?: (open: boolean) => void;
|
|
70
70
|
onError?: (error: PdfPreviewError) => void;
|
|
71
71
|
onLoad?: () => void;
|
|
72
|
+
/** Filename appended as a URL fragment to blob URLs so the browser's
|
|
73
|
+
* built-in PDF viewer uses it for "Save As" instead of the blob UUID. */
|
|
74
|
+
downloadFilename?: string;
|
|
72
75
|
}
|
|
73
76
|
/** Return value of usePdfPreview — provides state and handlers to the main component. */
|
|
74
77
|
export interface UsePdfPreviewReturn {
|