document-viewer-vue3 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,97 @@
1
+ import { n as e, t } from "./jszip.min-BuCM2Hjj.js";
2
+ //#region src/engines/presentation.ts
3
+ var n = /* @__PURE__ */ e(t(), 1), r = (e, t) => Array.from(e.querySelectorAll("*")).filter((e) => e.localName === t), i = (e, t) => r(e, t)[0], a = (e, t) => {
4
+ let n = new DOMParser().parseFromString(e, "application/xml");
5
+ if (n.querySelector("parsererror")) throw Error(`${t} XML 解析失败。`);
6
+ return n;
7
+ }, o = (e, t) => {
8
+ if (t.startsWith("/")) return t.slice(1);
9
+ let n = e.split("/");
10
+ n.pop();
11
+ for (let e of t.replace(/\\/g, "/").split("/")) e && e !== "." && (e === ".." ? n.pop() : n.push(e));
12
+ return n.join("/");
13
+ }, s = (e, t) => {
14
+ let n = /* @__PURE__ */ new Map();
15
+ for (let i of r(e, "Relationship")) {
16
+ let e = i.getAttribute("Id"), r = i.getAttribute("Target");
17
+ e && r && i.getAttribute("TargetMode") !== "External" && n.set(e, o(t, r));
18
+ }
19
+ return n;
20
+ }, c = (e) => r(e, "p").length ? r(e, "p").map((e) => r(e, "t").map((e) => e.textContent || "").join("")).filter(Boolean).join("\n").trim() : r(e, "t").map((e) => e.textContent || "").join("").trim(), l = (e, t = "") => {
21
+ let n = i(e, "srgbClr")?.getAttribute("val");
22
+ return n ? `#${n}` : t;
23
+ }, u = (e, t, n) => {
24
+ let r = i(e, "xfrm"), a = r && i(r, "off"), o = r && i(r, "ext");
25
+ if (!a || !o) return null;
26
+ let s = Number(a.getAttribute("x") || 0), c = Number(a.getAttribute("y") || 0), l = Number(o.getAttribute("cx") || 0), u = Number(o.getAttribute("cy") || 0);
27
+ return !l || !u ? null : {
28
+ left: `${s / t * 100}%`,
29
+ top: `${c / n * 100}%`,
30
+ width: `${l / t * 100}%`,
31
+ height: `${u / n * 100}%`
32
+ };
33
+ }, d = (e) => {
34
+ let t = e.split(".").pop()?.toLowerCase();
35
+ return t === "jpg" || t === "jpeg" ? "image/jpeg" : t === "gif" ? "image/gif" : t === "svg" ? "image/svg+xml" : t === "webp" ? "image/webp" : t === "bmp" ? "image/bmp" : "image/png";
36
+ }, f = (e, t) => t ? (Object.assign(e.style, t), !0) : !1, p = (e, t, n, r) => {
37
+ let a = c(e), o = i(e, "spPr") || e, s = u(o, n, r);
38
+ if (!s) return;
39
+ let d = t.ownerDocument.createElement("div");
40
+ d.className = "document-slide__shape", d.textContent = a, f(d, s);
41
+ let p = i(e, "pPr"), m = i(e, "rPr") || i(e, "defRPr"), h = Number(m?.getAttribute("sz") || 1800) / 100, g = p?.getAttribute("algn");
42
+ d.style.fontSize = `clamp(8px, ${Math.max(1, h / 7.2)}cqi, ${h * 4 / 3}px)`, d.style.fontWeight = m?.getAttribute("b") === "1" ? "700" : "400", d.style.fontStyle = m?.getAttribute("i") === "1" ? "italic" : "normal", d.style.color = l(m || e, "#1f2937"), d.style.textAlign = g === "ctr" ? "center" : g === "r" ? "right" : "left";
43
+ let _ = l(o);
44
+ _ && (d.style.background = _);
45
+ let v = i(o, "ln"), y = v && l(v);
46
+ y && (d.style.border = `1px solid ${y}`), i(o, "prstGeom")?.getAttribute("prst") === "ellipse" && (d.style.borderRadius = "50%"), t.appendChild(d);
47
+ }, m = async (e, t, n, r, a, o, s, c) => {
48
+ let l = u(i(e, "spPr") || e, o, s), p = i(e, "blip"), m = p?.getAttribute("r:embed") || p?.getAttributeNS("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), h = m ? r.get(m) : void 0;
49
+ if (!l || !h) return;
50
+ let g = a.file(h);
51
+ if (!g) return;
52
+ let _ = await g.async("uint8array"), v = new Uint8Array(_).buffer, y = URL.createObjectURL(new Blob([v], { type: d(h) }));
53
+ c.push(y);
54
+ let b = t.ownerDocument.createElement("img");
55
+ b.className = "document-slide__picture", b.alt = i(e, "cNvPr")?.getAttribute("descr") || "", b.src = y, f(b, l), t.appendChild(b);
56
+ }, h = async ({ buffer: e, container: t, signal: o, onProgress: c }) => {
57
+ c({
58
+ current: 0,
59
+ total: 1,
60
+ label: "正在解析 PowerPoint 文档"
61
+ });
62
+ let u = await n.default.loadAsync(e), d = "ppt/presentation.xml", f = u.file(d), h = u.file("ppt/_rels/presentation.xml.rels");
63
+ if (!f || !h) throw Error("文件不是有效的 PowerPoint OpenXML 文档。");
64
+ let g = a(await f.async("text"), "presentation"), _ = s(a(await h.async("text"), "presentation relationships"), d), v = i(g, "sldSz"), y = Number(v?.getAttribute("cx") || 12192e3), b = Number(v?.getAttribute("cy") || 6858e3), x = r(g, "sldId").map((e) => e.getAttribute("r:id") || e.getAttributeNS("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id")).map((e) => e ? _.get(e) : void 0).filter((e) => !!e);
65
+ if (!x.length) throw Error("PowerPoint 文档中没有可预览的幻灯片。");
66
+ t.classList.add("document-renderer", "document-renderer--presentation");
67
+ let S = [];
68
+ for (let e = 0; e < x.length; e += 1) {
69
+ if (o.aborted) throw new DOMException("Document load aborted", "AbortError");
70
+ let n = x[e], r = u.file(n);
71
+ if (!r) continue;
72
+ c({
73
+ current: e,
74
+ total: x.length,
75
+ label: `正在渲染幻灯片 ${e + 1}/${x.length}`
76
+ });
77
+ let d = a(await r.async("text"), `slide ${e + 1}`), f = n.replace(/([^/]+)$/, "_rels/$1.rels"), h = u.file(f), g = h ? s(a(await h.async("text"), `slide ${e + 1} relationships`), n) : /* @__PURE__ */ new Map(), _ = t.ownerDocument.createElement("section");
78
+ _.className = "document-slide", _.style.aspectRatio = `${y} / ${b}`, _.dataset.slide = String(e + 1);
79
+ let v = i(d, "bg");
80
+ _.style.background = v ? l(v, "#fff") : "#fff";
81
+ let C = i(d, "spTree");
82
+ if (C) for (let e of Array.from(C.children)) e.localName === "sp" || e.localName === "graphicFrame" ? p(e, _, y, b) : e.localName === "pic" && await m(e, _, n, g, u, y, b, S);
83
+ t.appendChild(_);
84
+ }
85
+ return c({
86
+ current: x.length,
87
+ total: x.length,
88
+ label: "PowerPoint 已就绪"
89
+ }), {
90
+ pageCount: x.length,
91
+ destroy() {
92
+ S.forEach((e) => URL.revokeObjectURL(e)), t.replaceChildren(), t.classList.remove("document-renderer", "document-renderer--presentation");
93
+ }
94
+ };
95
+ };
96
+ //#endregion
97
+ export { h as renderPresentationDocument };