@ghyassd/office-preview 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,392 @@
1
+ import { ref as h, computed as x, watch as I, onMounted as we, onBeforeUnmount as me, openBlock as a, createElementBlock as o, toDisplayString as b, Fragment as _, createElementVNode as u, renderList as M, normalizeClass as he, createCommentVNode as B, normalizeStyle as X, nextTick as ye, createBlock as Y, renderSlot as ge } from "vue";
2
+ function ke(i) {
3
+ return new Worker(
4
+ "/assets/xlsxParser.worker-CUZcso8W.js",
5
+ {
6
+ name: i == null ? void 0 : i.name
7
+ }
8
+ );
9
+ }
10
+ const H = (i, l) => {
11
+ const g = i.__vccOpts || i;
12
+ for (const [v, c] of l)
13
+ g[v] = c;
14
+ return g;
15
+ }, _e = { class: "excel-preview" }, be = {
16
+ key: 0,
17
+ class: "state loading"
18
+ }, xe = {
19
+ key: 1,
20
+ class: "state error"
21
+ }, Ce = { class: "toolbar" }, Pe = ["src"], Re = { class: "toolbar" }, Se = {
22
+ key: 0,
23
+ class: "tabs"
24
+ }, $e = ["onClick"], We = { class: "toolbar-actions" }, Me = { class: "stat" }, Be = {
25
+ key: 0,
26
+ class: "stat warn"
27
+ }, Ee = { class: "grid" }, qe = {
28
+ key: 0,
29
+ class: "spacer"
30
+ }, Ue = ["colspan"], je = { class: "row-head" }, Oe = ["title"], Ae = {
31
+ key: 1,
32
+ class: "spacer"
33
+ }, Le = ["colspan"], Ie = { class: "row-head" }, Ne = ["rowspan", "colspan", "title"], L = 31, ee = 18, Te = {
34
+ __name: "ExcelPreview",
35
+ props: {
36
+ file: { type: Object, required: !0 },
37
+ adapter: { type: Object, required: !0 },
38
+ maxRows: { type: Number, default: 0 },
39
+ maxCols: { type: Number, default: 0 }
40
+ },
41
+ setup(i) {
42
+ const l = i, g = h(!1), v = h(""), c = h([]), d = h(0), w = h(!1), k = h(""), C = h(0), $ = h(0), m = h(null), f = h(null);
43
+ let s = null, U = 0, W = 0;
44
+ const p = x(() => c.value[d.value] || null), E = x(() => {
45
+ var e;
46
+ return !((e = p.value) != null && e.hasMerges);
47
+ }), j = x(() => {
48
+ var e;
49
+ return ((e = p.value) == null ? void 0 : e.totalRows) || 0;
50
+ }), ae = x(() => {
51
+ const e = Math.ceil($.value / L);
52
+ return Math.max(e + ee * 2, 60);
53
+ }), q = x(() => {
54
+ if (!E.value) return 0;
55
+ const e = Math.floor(C.value / L) - ee;
56
+ return Math.max(0, Math.min(e, Math.max(0, j.value - 1)));
57
+ }), V = x(() => E.value ? Math.min(j.value, q.value + ae.value) : j.value), se = x(() => p.value ? E.value ? p.value.rows.slice(q.value, V.value) : p.value.rows : []), D = x(() => E.value ? q.value * L : 0), G = x(() => E.value ? (j.value - V.value) * L : 0);
58
+ function oe(e) {
59
+ let t = e + 1, r = "";
60
+ for (; t > 0; ) {
61
+ const n = (t - 1) % 26;
62
+ r = String.fromCharCode(65 + n) + r, t = Math.floor((t - 1) / 26);
63
+ }
64
+ return r;
65
+ }
66
+ function Z(e) {
67
+ var t;
68
+ return (((t = p.value) == null ? void 0 : t.rowBase) || 0) + e + 1;
69
+ }
70
+ function le(e) {
71
+ var P, A;
72
+ const t = p.value;
73
+ if (!t) return [];
74
+ const r = t.rows[e] || [], n = ((P = t.mergeStartsByRow) == null ? void 0 : P[e]) || null, y = ((A = t.mergeSkipByRow) == null ? void 0 : A[e]) || null, O = [];
75
+ for (let R = 0; R < t.cols; R += 1) {
76
+ if (y && y[R]) continue;
77
+ const S = n == null ? void 0 : n[R];
78
+ O.push({
79
+ col: R,
80
+ value: r[R] ?? "",
81
+ rowspan: (S == null ? void 0 : S.rowspan) || 1,
82
+ colspan: (S == null ? void 0 : S.colspan) || 1
83
+ });
84
+ }
85
+ return O;
86
+ }
87
+ function ne() {
88
+ if (f.value || typeof Worker > "u") return f.value;
89
+ const e = new ke();
90
+ return e.onmessage = (t) => {
91
+ const r = t.data || {};
92
+ if (!s || s.requestId !== r.requestId) return;
93
+ const { resolve: n, reject: y } = s;
94
+ s = null, r.ok ? n(r.sheets || []) : y(new Error(r.message || "Failed to parse workbook"));
95
+ }, e.onerror = () => {
96
+ s && (s.reject(new Error("Worker crashed")), s = null);
97
+ }, f.value = e, e;
98
+ }
99
+ function ue(e) {
100
+ const t = ne();
101
+ if (!t) return Promise.reject(new Error("Worker is unavailable"));
102
+ s && (s.reject(new Error("Cancelled")), s = null);
103
+ const r = ++U;
104
+ return new Promise((n, y) => {
105
+ s = { requestId: r, resolve: n, reject: y }, t.postMessage({
106
+ requestId: r,
107
+ buffer: e,
108
+ maxRows: l.maxRows,
109
+ maxCols: l.maxCols
110
+ });
111
+ });
112
+ }
113
+ async function J(e) {
114
+ const t = await import("./xlsx-BopDBbWb.js"), r = t.read(e, { type: "array" });
115
+ return r.SheetNames.map((n) => {
116
+ const y = r.Sheets[n], O = y["!ref"] || "A1:A1", P = t.utils.decode_range(O), A = P.s.r, R = P.s.c, S = P.e.r - P.s.r + 1, F = P.e.c - P.s.c + 1, K = t.utils.sheet_to_json(y, { header: 1, raw: !1, defval: "", blankrows: !0 }), Q = l.maxCols > 0 ? Math.min(F, l.maxCols) : F, pe = (l.maxRows > 0 ? K.slice(0, l.maxRows) : K).map((z) => Array.isArray(z) ? l.maxCols > 0 ? z.slice(0, Q) : z : []);
117
+ return {
118
+ name: n,
119
+ rows: pe,
120
+ cols: Q,
121
+ totalRows: S,
122
+ totalCols: F,
123
+ rowBase: A,
124
+ colBase: R,
125
+ hasMerges: !1,
126
+ mergeStartsByRow: {},
127
+ mergeSkipByRow: {}
128
+ };
129
+ }).filter((n) => n.cols > 0);
130
+ }
131
+ async function ie(e) {
132
+ if (typeof Worker > "u") return J(e);
133
+ try {
134
+ return await ue(e);
135
+ } catch (t) {
136
+ if (t.message === "Cancelled") throw t;
137
+ return J(e);
138
+ }
139
+ }
140
+ async function ce(e) {
141
+ var t;
142
+ return (t = l.adapter) != null && t.getPdfUrl ? await l.adapter.getPdfUrl(e) : (e == null ? void 0 : e.pdfUrl) || "";
143
+ }
144
+ async function de(e) {
145
+ var r;
146
+ if ((r = l.adapter) != null && r.getArrayBuffer) return await l.adapter.getArrayBuffer(e);
147
+ if (!(e != null && e.url)) throw new Error("adapter.getArrayBuffer or file.url is required");
148
+ const t = await fetch(e.url);
149
+ if (!t.ok) throw new Error(`Failed to fetch file: ${t.status}`);
150
+ return await t.arrayBuffer();
151
+ }
152
+ function N() {
153
+ m.value && ($.value = m.value.clientHeight || 0);
154
+ }
155
+ function ve(e) {
156
+ C.value = e.target.scrollTop || 0;
157
+ }
158
+ async function T() {
159
+ await ye(), m.value && (m.value.scrollTop = 0, m.value.scrollLeft = 0), C.value = 0, N();
160
+ }
161
+ async function fe() {
162
+ const e = ++W;
163
+ g.value = !0, v.value = "", c.value = [], d.value = 0, w.value = !1, k.value = await ce(l.file);
164
+ try {
165
+ const t = await de(l.file);
166
+ if (e !== W) return;
167
+ const r = await ie(t);
168
+ if (e !== W) return;
169
+ if (!r.length) throw new Error("Workbook has no readable sheet");
170
+ c.value = r;
171
+ } catch (t) {
172
+ if (e !== W) return;
173
+ k.value ? w.value = !0 : v.value = (t == null ? void 0 : t.message) || "Failed to load spreadsheet preview";
174
+ } finally {
175
+ e === W && (g.value = !1), e === W && await T();
176
+ }
177
+ }
178
+ return I(() => l.file, fe, { immediate: !0 }), I(() => d.value, T), I(() => w.value, T), we(() => window.addEventListener("resize", N)), me(() => {
179
+ window.removeEventListener("resize", N), W += 1, s && (s.reject(new Error("Cancelled")), s = null), f.value && (f.value.terminate(), f.value = null);
180
+ }), (e, t) => (a(), o("div", _e, [
181
+ g.value ? (a(), o("div", be, "Loading spreadsheet...")) : v.value ? (a(), o("div", xe, b(v.value), 1)) : w.value && k.value ? (a(), o(_, { key: 2 }, [
182
+ u("div", Ce, [
183
+ t[2] || (t[2] = u("div", null, null, -1)),
184
+ u("button", {
185
+ class: "btn",
186
+ onClick: t[0] || (t[0] = (r) => w.value = !1)
187
+ }, "Table Mode")
188
+ ]),
189
+ u("iframe", {
190
+ class: "pdf",
191
+ src: k.value
192
+ }, null, 8, Pe)
193
+ ], 64)) : p.value ? (a(), o(_, { key: 3 }, [
194
+ u("div", Re, [
195
+ c.value.length > 1 ? (a(), o("div", Se, [
196
+ (a(!0), o(_, null, M(c.value, (r, n) => (a(), o("button", {
197
+ key: r.name,
198
+ class: he(["tab", { active: n === d.value }]),
199
+ onClick: (y) => d.value = n
200
+ }, b(r.name), 11, $e))), 128))
201
+ ])) : B("", !0),
202
+ u("div", We, [
203
+ u("span", Me, b(p.value.totalRows) + " rows x " + b(p.value.totalCols) + " cols", 1),
204
+ p.value.hasMerges ? (a(), o("span", Be, "Merged mode")) : B("", !0),
205
+ k.value ? (a(), o("button", {
206
+ key: 1,
207
+ class: "btn",
208
+ onClick: t[1] || (t[1] = (r) => w.value = !0)
209
+ }, "PDF Mode")) : B("", !0)
210
+ ])
211
+ ]),
212
+ u("div", {
213
+ ref_key: "gridRef",
214
+ ref: m,
215
+ class: "grid-wrap",
216
+ onScroll: ve
217
+ }, [
218
+ u("table", Ee, [
219
+ u("thead", null, [
220
+ u("tr", null, [
221
+ t[3] || (t[3] = u("th", { class: "row-head" }, null, -1)),
222
+ (a(!0), o(_, null, M(p.value.cols, (r) => (a(), o("th", { key: r }, b(oe((p.value.colBase || 0) + r - 1)), 1))), 128))
223
+ ])
224
+ ]),
225
+ u("tbody", null, [
226
+ E.value ? (a(), o(_, { key: 0 }, [
227
+ D.value > 0 ? (a(), o("tr", qe, [
228
+ u("td", {
229
+ colspan: p.value.cols + 1,
230
+ style: X({ height: `${D.value}px` })
231
+ }, null, 12, Ue)
232
+ ])) : B("", !0),
233
+ (a(!0), o(_, null, M(se.value, (r, n) => (a(), o("tr", {
234
+ key: q.value + n
235
+ }, [
236
+ u("th", je, b(Z(q.value + n)), 1),
237
+ (a(!0), o(_, null, M(p.value.cols, (y) => (a(), o("td", {
238
+ key: y,
239
+ title: String((r || [])[y - 1] ?? "")
240
+ }, b((r || [])[y - 1] ?? ""), 9, Oe))), 128))
241
+ ]))), 128)),
242
+ G.value > 0 ? (a(), o("tr", Ae, [
243
+ u("td", {
244
+ colspan: p.value.cols + 1,
245
+ style: X({ height: `${G.value}px` })
246
+ }, null, 12, Le)
247
+ ])) : B("", !0)
248
+ ], 64)) : (a(!0), o(_, { key: 1 }, M(p.value.totalRows, (r) => (a(), o("tr", {
249
+ key: `m-${r}`
250
+ }, [
251
+ u("th", Ie, b(Z(r - 1)), 1),
252
+ (a(!0), o(_, null, M(le(r - 1), (n) => (a(), o("td", {
253
+ key: n.col,
254
+ rowspan: n.rowspan > 1 ? n.rowspan : void 0,
255
+ colspan: n.colspan > 1 ? n.colspan : void 0,
256
+ title: String(n.value ?? "")
257
+ }, b(n.value ?? ""), 9, Ne))), 128))
258
+ ]))), 128))
259
+ ])
260
+ ])
261
+ ], 544)
262
+ ], 64)) : B("", !0)
263
+ ]));
264
+ }
265
+ }, te = /* @__PURE__ */ H(Te, [["__scopeId", "data-v-d1a0b8a0"]]), Fe = { class: "word-preview" }, ze = {
266
+ key: 0,
267
+ class: "state loading"
268
+ }, He = {
269
+ key: 1,
270
+ class: "state error"
271
+ }, Ve = { class: "toolbar" }, De = ["src"], Ge = {
272
+ key: 0,
273
+ class: "toolbar"
274
+ }, Ze = { class: "pages" }, Je = { class: "page-no" }, Ke = ["src", "alt"], Qe = {
275
+ __name: "WordPreview",
276
+ props: {
277
+ file: { type: Object, required: !0 },
278
+ adapter: { type: Object, required: !0 }
279
+ },
280
+ setup(i) {
281
+ const l = i, g = h(!1), v = h(""), c = h([]), d = h(""), w = h(!1);
282
+ let k = 0;
283
+ async function C(m) {
284
+ var f;
285
+ return (f = l.adapter) != null && f.getPdfUrl ? await l.adapter.getPdfUrl(m) : (m == null ? void 0 : m.pdfUrl) || "";
286
+ }
287
+ async function $() {
288
+ var f;
289
+ const m = ++k;
290
+ g.value = !0, v.value = "", c.value = [], d.value = "", w.value = !1;
291
+ try {
292
+ if (!((f = l.adapter) != null && f.getPageImages))
293
+ throw new Error("adapter.getPageImages is required for Word preview");
294
+ const s = await l.adapter.getPageImages(l.file);
295
+ if (m !== k) return;
296
+ c.value = Array.isArray(s == null ? void 0 : s.images) ? s.images : [], d.value = (s == null ? void 0 : s.fallbackPdfUrl) || await C(l.file), !c.value.length && d.value && (w.value = !0);
297
+ } catch (s) {
298
+ if (m !== k) return;
299
+ try {
300
+ if (d.value = await C(l.file), d.value)
301
+ w.value = !0;
302
+ else
303
+ throw s;
304
+ } catch {
305
+ v.value = (s == null ? void 0 : s.message) || "Failed to load Word preview";
306
+ }
307
+ } finally {
308
+ m === k && (g.value = !1);
309
+ }
310
+ }
311
+ return I(() => l.file, $, { immediate: !0 }), (m, f) => (a(), o("div", Fe, [
312
+ g.value ? (a(), o("div", ze, "Converting document...")) : v.value ? (a(), o("div", He, b(v.value), 1)) : w.value && d.value ? (a(), o(_, { key: 2 }, [
313
+ u("div", Ve, [
314
+ u("button", {
315
+ class: "btn",
316
+ onClick: f[0] || (f[0] = (s) => w.value = !1)
317
+ }, "Image Mode")
318
+ ]),
319
+ u("iframe", {
320
+ class: "pdf",
321
+ src: d.value
322
+ }, null, 8, De)
323
+ ], 64)) : (a(), o(_, { key: 3 }, [
324
+ d.value ? (a(), o("div", Ge, [
325
+ u("button", {
326
+ class: "btn",
327
+ onClick: f[1] || (f[1] = (s) => w.value = !0)
328
+ }, "PDF Mode")
329
+ ])) : B("", !0),
330
+ u("div", Ze, [
331
+ (a(!0), o(_, null, M(c.value, (s, U) => (a(), o("div", {
332
+ key: s,
333
+ class: "page"
334
+ }, [
335
+ u("div", Je, "Page " + b(U + 1), 1),
336
+ u("img", {
337
+ src: s,
338
+ alt: `page-${U + 1}`,
339
+ loading: "lazy"
340
+ }, null, 8, Ke)
341
+ ]))), 128))
342
+ ])
343
+ ], 64))
344
+ ]));
345
+ }
346
+ }, re = /* @__PURE__ */ H(Qe, [["__scopeId", "data-v-f010849e"]]), Xe = { class: "office-preview" }, Ye = {
347
+ __name: "OfficePreview",
348
+ props: {
349
+ file: { type: Object, required: !0 },
350
+ adapter: { type: Object, required: !0 },
351
+ kind: { type: String, default: "" },
352
+ maxRows: { type: Number, default: 0 },
353
+ maxCols: { type: Number, default: 0 }
354
+ },
355
+ setup(i) {
356
+ const l = i, g = x(() => {
357
+ var w, k, C, $;
358
+ const v = (l.kind || "").toLowerCase();
359
+ if (v === "word" || v === "excel") return v;
360
+ const c = (((w = l.file) == null ? void 0 : w.mimeType) || ((k = l.file) == null ? void 0 : k.mime_type) || "").toLowerCase();
361
+ if (c.includes("spreadsheetml") || c === "application/vnd.ms-excel") return "excel";
362
+ if (c.includes("wordprocessingml") || c === "application/msword") return "word";
363
+ const d = (((C = l.file) == null ? void 0 : C.name) || (($ = l.file) == null ? void 0 : $.original_name) || "").toLowerCase();
364
+ return d.endsWith(".xlsx") || d.endsWith(".xls") ? "excel" : d.endsWith(".docx") || d.endsWith(".doc") ? "word" : "";
365
+ });
366
+ return (v, c) => (a(), o("div", Xe, [
367
+ g.value === "excel" ? (a(), Y(te, {
368
+ key: 0,
369
+ file: i.file,
370
+ adapter: i.adapter,
371
+ "max-rows": i.maxRows,
372
+ "max-cols": i.maxCols
373
+ }, null, 8, ["file", "adapter", "max-rows", "max-cols"])) : g.value === "word" ? (a(), Y(re, {
374
+ key: 1,
375
+ file: i.file,
376
+ adapter: i.adapter
377
+ }, null, 8, ["file", "adapter"])) : ge(v.$slots, "unsupported", { key: 2 }, () => [
378
+ c[0] || (c[0] = u("div", { class: "unsupported" }, "Unsupported file type", -1))
379
+ ], !0)
380
+ ]));
381
+ }
382
+ }, et = /* @__PURE__ */ H(Ye, [["__scopeId", "data-v-64b59989"]]), rt = {
383
+ install(i) {
384
+ i.component("OfficePreview", et), i.component("WordPreview", re), i.component("ExcelPreview", te);
385
+ }
386
+ };
387
+ export {
388
+ te as ExcelPreview,
389
+ et as OfficePreview,
390
+ re as WordPreview,
391
+ rt as default
392
+ };