@work-zhanguo/light-file-preview 0.0.7 → 0.0.8

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 CHANGED
@@ -8,10 +8,11 @@
8
8
 
9
9
  用于业务系统里的附件预览,支持传入文件路径、远程地址和本地文件,接入方式尽量保持简单。
10
10
 
11
- 当前版本:`0.0.7`
11
+ 当前版本:`0.0.8`
12
12
 
13
13
  最近更新:
14
14
 
15
+ - `0.0.8`:优化 `xlsx` 预览,补充工作表页签、首行首列编号区、冻结观感、单元格 hover / 选中高亮,并补充组件属性说明
15
16
  - `0.0.7`:修复 Vue2 `vue-cli / webpack4` 项目引入 PDF 预览时报 `import.meta.url`、`process.getBuiltinModule` 解析错误
16
17
  - `0.0.6`:修复 Vue2 `vue-cli / webpack4` 项目解析产物时报 `Unexpected token`,降低构建目标到 `es2018`
17
18
  - `0.0.5`:修复 Vue2 `vue-cli / webpack4` 项目子路径入口解析失败,补充 `vue2.js` 和 `style.css` 兼容入口
@@ -81,6 +82,28 @@ import LightFilePreview from '@work-zhanguo/light-file-preview/vue2';
81
82
  import '@work-zhanguo/light-file-preview/style.css';
82
83
  ```
83
84
 
85
+ ## 组件属性
86
+
87
+ | 属性 | 类型 | 默认值 | 说明 |
88
+ | --- | --- | --- | --- |
89
+ | `source` | `string \| File \| Blob` | - | 必填。支持远程 URL、本地 `File`、`Blob`。 |
90
+ | `fileName` | `string` | - | 自定义文件名,远程地址没有扩展名时建议传。 |
91
+ | `mode` | `'inline' \| 'dialog'` | `'inline'` | 直接预览或弹窗预览。 |
92
+ | `visible` | `boolean` | `true` | 弹窗模式下控制显示状态,可配合 `v-model:visible`。 |
93
+ | `loadingText` | `string` | `'文件加载中...'` | 加载提示文案。 |
94
+ | `textEncoding` | `string` | `'utf-8'` | 文本类文件读取编码。 |
95
+ | `maxTextBytes` | `number` | `2097152` | 文本预览最大读取字节数,默认 2MB。 |
96
+ | `maxSheetRows` | `number` | `200` | 表格预览最大展示行数。 |
97
+ | `maxSheetCols` | `number` | `50` | 表格预览最大展示列数。 |
98
+ | `pdfScale` | `number` | `1.35` | PDF 渲染缩放比例。 |
99
+ | `showToolbar` | `boolean` | `true` | 是否显示顶部工具栏。 |
100
+ | `dialogTitle` | `string` | `'文件预览'` | 预览标题兜底文案。 |
101
+
102
+ 组件事件:
103
+
104
+ - `update:visible`:弹窗关闭时回传状态
105
+ - `error`:解析失败时返回错误对象
106
+
84
107
  ## 远程地址预览
85
108
 
86
109
  通过 `window.open` 传入网络地址,直接打开新页面预览。建议同时传 `name`,这样扩展名识别更稳定。
@@ -0,0 +1,315 @@
1
+ import { defineComponent as xe, ref as C, computed as Se, onMounted as $e, onBeforeUnmount as Re, openBlock as u, createElementBlock as c, createElementVNode as p, Fragment as I, renderList as N, normalizeClass as x, toDisplayString as v, createCommentVNode as W, createTextVNode as ne, normalizeStyle as le } from "vue";
2
+ import { r as ke } from "./text-Bqxn_tMI.js";
3
+ const Ve = { class: "lfp-document lfp-sheet" }, Ie = { class: "lfp-sheet__toolbar" }, Ne = {
4
+ key: 0,
5
+ class: "lfp-sheet__tabs",
6
+ role: "tablist",
7
+ "aria-label": "工作表切换"
8
+ }, De = ["onClick"], Fe = { class: "lfp-sheet__tab-label" }, ze = { class: "lfp-sheet__tab-meta" }, Be = {
9
+ key: 1,
10
+ class: "lfp-sheet__summary"
11
+ }, Ae = { class: "lfp-sheet__summary-item" }, We = { class: "lfp-sheet__summary-item" }, Te = {
12
+ key: 0,
13
+ class: "lfp-hint"
14
+ }, Ee = {
15
+ key: 1,
16
+ class: "lfp-sheet__table-wrap"
17
+ }, je = { class: "lfp-sheet__table" }, He = { class: "lfp-sheet__header-row" }, Ke = ["rowspan", "colspan", "onMouseenter", "onFocus", "onClick"], Le = { class: "lfp-sheet__cell-text" }, Me = {
18
+ key: 2,
19
+ class: "lfp-sheet__empty"
20
+ }, qe = /* @__PURE__ */ xe({
21
+ __name: "SheetRenderer",
22
+ props: {
23
+ source: {},
24
+ maxRows: {},
25
+ maxCols: {}
26
+ },
27
+ emits: ["loading", "error"],
28
+ setup(re, { emit: ie }) {
29
+ const D = re, F = ie, T = new AbortController(), S = C(0), $ = C([]), m = C(""), z = C(null), B = C(null), i = Se(() => $.value[S.value]);
30
+ function ae(e) {
31
+ let o = e + 1, s = "";
32
+ for (; o > 0; ) {
33
+ const t = (o - 1) % 26;
34
+ s = String.fromCharCode(65 + t) + s, o = Math.floor((o - 1) / 26);
35
+ }
36
+ return s;
37
+ }
38
+ function y(e, o) {
39
+ z.value = e, B.value = o;
40
+ }
41
+ function ue(e) {
42
+ m.value = e;
43
+ }
44
+ function E(e, o) {
45
+ return e != null && (e === z.value || e === j()) || o != null && (o === B.value || o === H());
46
+ }
47
+ function j() {
48
+ if (!m.value)
49
+ return null;
50
+ const [e] = m.value.split(":");
51
+ return Number.isFinite(Number(e)) ? Number(e) : null;
52
+ }
53
+ function H() {
54
+ if (!m.value)
55
+ return null;
56
+ const [, e] = m.value.split(":");
57
+ return Number.isFinite(Number(e)) ? Number(e) : null;
58
+ }
59
+ function ce(e) {
60
+ if (e) {
61
+ if (["thin", "hair"].includes(e) || ["medium", "dashed", "dotted", "dashDot", "dashDotDot"].includes(e))
62
+ return "1px solid";
63
+ if (["thick", "double", "mediumDashed", "mediumDashDot", "mediumDashDotDot"].includes(e))
64
+ return "2px solid";
65
+ }
66
+ }
67
+ function R(e, o, s) {
68
+ var n;
69
+ if (!s)
70
+ return;
71
+ const t = ce(s.style), l = (n = k(s.color)) != null ? n : "rgba(31, 44, 61, 0.12)";
72
+ t && (o === "left" ? e.borderLeft = `${t} ${l}` : o === "right" ? e.borderRight = `${t} ${l}` : o === "top" ? e.borderTop = `${t} ${l}` : o === "bottom" && (e.borderBottom = `${t} ${l}`));
73
+ }
74
+ function k(e) {
75
+ if (!e || typeof e != "object")
76
+ return;
77
+ const o = "rgb" in e && typeof e.rgb == "string" ? e.rgb : "argb" in e && typeof e.argb == "string" ? e.argb : void 0;
78
+ if (!o)
79
+ return;
80
+ const s = o.replace(/^#/, "");
81
+ if (/^[0-9a-fA-F]{8}$/.test(s))
82
+ return `#${s.slice(2)}`;
83
+ if (/^[0-9a-fA-F]{6}$/.test(s))
84
+ return `#${s}`;
85
+ }
86
+ function fe(e) {
87
+ if (!e)
88
+ return;
89
+ const o = typeof e.hpx == "number" ? e.hpx : typeof e.hpt == "number" ? e.hpt * 1.3334 : void 0;
90
+ return o ? { height: `${Math.max(o, 24)}px` } : void 0;
91
+ }
92
+ function K(e) {
93
+ if (e) {
94
+ if (typeof e.wpx == "number")
95
+ return `${Math.max(e.wpx, 72)}px`;
96
+ if (typeof e.width == "number")
97
+ return `${Math.max(e.width * 8, 72)}px`;
98
+ }
99
+ }
100
+ function de(e) {
101
+ if (!e)
102
+ return {};
103
+ const o = {};
104
+ return e.horizontal && (o.textAlign = e.horizontal), e.vertical && (o.verticalAlign = e.vertical === "center" ? "middle" : e.vertical), e.wrapText && (o.whiteSpace = "pre-wrap", o.wordBreak = "break-word"), o;
105
+ }
106
+ function pe(e, o, s) {
107
+ var d;
108
+ const t = {};
109
+ if (s ? t.minWidth = s : o && (t.minWidth = o, t.width = o), !(e != null && e.s) || typeof e.s != "object")
110
+ return t;
111
+ const l = e.s.fill, n = e.s.font, r = e.s.border, f = (d = k(l == null ? void 0 : l.fgColor)) != null ? d : k(l == null ? void 0 : l.bgColor), h = k(n == null ? void 0 : n.color);
112
+ return f && (l == null ? void 0 : l.patternType) !== "none" && (t.backgroundColor = f), h && (t.color = h), n != null && n.bold && (t.fontWeight = "600"), n != null && n.italic && (t.fontStyle = "italic"), n != null && n.underline && (t.textDecoration = "underline"), typeof (n == null ? void 0 : n.sz) == "number" && (t.fontSize = `${n.sz}px`), typeof (n == null ? void 0 : n.name) == "string" && n.name.trim() && (t.fontFamily = n.name), Object.assign(t, de(e.s.alignment)), R(t, "left", r == null ? void 0 : r.left), R(t, "right", r == null ? void 0 : r.right), R(t, "top", r == null ? void 0 : r.top), R(t, "bottom", r == null ? void 0 : r.bottom), t;
113
+ }
114
+ function ve(e, o) {
115
+ var s, t;
116
+ return o ? typeof o.w == "string" ? o.w : (s = e.utils) != null && s.format_cell ? String(e.utils.format_cell(o, (t = o.v) != null ? t : "")) : o.v == null ? "" : String(o.v) : "";
117
+ }
118
+ function he(e, o, s) {
119
+ const t = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Map();
120
+ for (const n of e) {
121
+ const r = o.filter((d) => d >= n.s.r && d <= n.e.r), f = s.filter((d) => d >= n.s.c && d <= n.e.c);
122
+ if (!r.length || !f.length)
123
+ continue;
124
+ const h = `${n.s.r}:${n.s.c}`;
125
+ l.set(h, {
126
+ rowSpan: r.length,
127
+ colSpan: f.length,
128
+ colsInRange: f
129
+ });
130
+ for (const d of r)
131
+ for (const V of f)
132
+ d === n.s.r && V === n.s.c || t.add(`${d}:${V}`);
133
+ }
134
+ return { coveredCells: t, mergeStarts: l };
135
+ }
136
+ function be(e, o, s) {
137
+ var L, M, O, U, q, G, J;
138
+ const t = s["!ref"];
139
+ if (!t)
140
+ return {
141
+ name: o,
142
+ rows: [],
143
+ totalRows: 0,
144
+ totalCols: 0,
145
+ visibleRows: 0,
146
+ visibleCols: 0,
147
+ visibleRowIndexes: [],
148
+ visibleColIndexes: [],
149
+ firstVisibleRow: 0,
150
+ firstVisibleCol: 0
151
+ };
152
+ const l = e.utils.decode_range(t), n = [], r = [];
153
+ for (let a = l.s.r; a <= l.e.r; a += 1)
154
+ (M = (L = s["!rows"]) == null ? void 0 : L[a]) != null && M.hidden || n.push(a);
155
+ for (let a = l.s.c; a <= l.e.c; a += 1)
156
+ (U = (O = s["!cols"]) == null ? void 0 : O[a]) != null && U.hidden || r.push(a);
157
+ const f = n.slice(0, D.maxRows), h = r.slice(0, D.maxCols), d = (q = s["!merges"]) != null ? q : [], { coveredCells: V, mergeStarts: _e } = he(d, f, h), ye = f.map((a) => {
158
+ var Q, Y, Z, X, ee;
159
+ const ge = (Q = s["!rows"]) == null ? void 0 : Q[a], we = fe(ge), P = [];
160
+ for (const g of h) {
161
+ const A = `${a}:${g}`;
162
+ if (V.has(A))
163
+ continue;
164
+ const Ce = e.utils.encode_cell({ r: a, c: g }), te = s[Ce], b = _e.get(A), oe = ((Y = b == null ? void 0 : b.colsInRange) != null ? Y : [g]).map((_) => {
165
+ var w;
166
+ return K((w = s["!cols"]) == null ? void 0 : w[_]);
167
+ }).filter(Boolean).reduce((_, w) => {
168
+ if (!w)
169
+ return _;
170
+ const se = Number.parseFloat(w);
171
+ return Number.isFinite(se) ? (_ != null ? _ : 0) + se : _;
172
+ }, null);
173
+ P.push({
174
+ key: A,
175
+ value: ve(e, te),
176
+ rowIndex: a,
177
+ colIndex: g,
178
+ rowSpan: (Z = b == null ? void 0 : b.rowSpan) != null ? Z : 1,
179
+ colSpan: (X = b == null ? void 0 : b.colSpan) != null ? X : 1,
180
+ style: pe(
181
+ te,
182
+ K((ee = s["!cols"]) == null ? void 0 : ee[g]),
183
+ oe ? `${oe}px` : void 0
184
+ )
185
+ });
186
+ }
187
+ return {
188
+ key: `${o}-${a}`,
189
+ rowIndex: a,
190
+ cells: P,
191
+ style: we
192
+ };
193
+ });
194
+ return {
195
+ name: o,
196
+ rows: ye,
197
+ totalRows: n.length,
198
+ totalCols: r.length,
199
+ visibleRows: f.length,
200
+ visibleCols: h.length,
201
+ visibleRowIndexes: f,
202
+ visibleColIndexes: h,
203
+ firstVisibleRow: (G = f[0]) != null ? G : 0,
204
+ firstVisibleCol: (J = h[0]) != null ? J : 0
205
+ };
206
+ }
207
+ async function me() {
208
+ F("loading", !0);
209
+ try {
210
+ const e = await ke(D.source, T.signal), o = await import("./xlsx-j9Zzywas.js"), s = o.read(e, {
211
+ type: "array",
212
+ cellStyles: !0,
213
+ cellNF: !0,
214
+ cellText: !0
215
+ });
216
+ $.value = s.SheetNames.map(
217
+ (t) => be(o, t, s.Sheets[t])
218
+ ), S.value = 0, m.value = "", y(null, null);
219
+ } catch (e) {
220
+ F("error", e instanceof Error ? e : new Error("表格预览失败"));
221
+ } finally {
222
+ F("loading", !1);
223
+ }
224
+ }
225
+ return $e(() => {
226
+ me();
227
+ }), Re(() => {
228
+ T.abort();
229
+ }), (e, o) => (u(), c("div", Ve, [
230
+ p("div", Ie, [
231
+ $.value.length > 1 ? (u(), c("div", Ne, [
232
+ (u(!0), c(I, null, N($.value, (s, t) => (u(), c("button", {
233
+ key: s.name,
234
+ class: x(["lfp-sheet__tab", { "is-active": t === S.value }]),
235
+ type: "button",
236
+ onClick: (l) => S.value = t
237
+ }, [
238
+ p("span", Fe, v(s.name), 1),
239
+ p("span", ze, v(s.totalRows) + " × " + v(s.totalCols), 1)
240
+ ], 10, De))), 128))
241
+ ])) : W("", !0),
242
+ i.value ? (u(), c("div", Be, [
243
+ p("span", Ae, [
244
+ o[2] || (o[2] = ne(" 当前工作表 ", -1)),
245
+ p("strong", null, v(i.value.name), 1)
246
+ ]),
247
+ p("span", We, [
248
+ o[3] || (o[3] = ne(" 总览 ", -1)),
249
+ p("strong", null, v(i.value.totalRows) + " 行 / " + v(i.value.totalCols) + " 列", 1)
250
+ ])
251
+ ])) : W("", !0)
252
+ ]),
253
+ i.value ? (u(), c("p", Te, " 共 " + v(i.value.totalRows) + " 行," + v(i.value.totalCols) + " 列。当前展示 " + v(i.value.visibleRows) + " 行、" + v(i.value.visibleCols) + " 列。 ", 1)) : W("", !0),
254
+ i.value ? (u(), c("div", Ee, [
255
+ p("table", je, [
256
+ p("tbody", null, [
257
+ p("tr", He, [
258
+ o[4] || (o[4] = p("th", {
259
+ class: "lfp-sheet__corner-header",
260
+ scope: "col",
261
+ "aria-label": "表格左上角"
262
+ }, null, -1)),
263
+ (u(!0), c(I, null, N(i.value.visibleColIndexes, (s) => (u(), c("th", {
264
+ key: `col-header-${s}`,
265
+ class: x(["lfp-sheet__col-header", {
266
+ "is-first-data-col": s === i.value.firstVisibleCol,
267
+ "is-related": E(null, s)
268
+ }]),
269
+ scope: "col"
270
+ }, v(ae(s)), 3))), 128))
271
+ ]),
272
+ (u(!0), c(I, null, N(i.value.rows, (s) => (u(), c("tr", {
273
+ key: s.key,
274
+ class: x(["lfp-sheet__row", { "is-first-row": s.rowIndex === i.value.firstVisibleRow }]),
275
+ style: le(s.style)
276
+ }, [
277
+ p("th", {
278
+ class: x(["lfp-sheet__row-header", {
279
+ "is-first-data-row": s.rowIndex === i.value.firstVisibleRow,
280
+ "is-related": E(s.rowIndex, null)
281
+ }]),
282
+ scope: "row"
283
+ }, v(s.rowIndex + 1), 3),
284
+ (u(!0), c(I, null, N(s.cells, (t) => (u(), c("td", {
285
+ key: t.key,
286
+ class: x({
287
+ "is-first-row": t.rowIndex === i.value.firstVisibleRow,
288
+ "is-first-col": t.colIndex === i.value.firstVisibleCol,
289
+ "is-corner-cell": t.rowIndex === i.value.firstVisibleRow && t.colIndex === i.value.firstVisibleCol,
290
+ "is-selected": m.value === t.key,
291
+ "is-row-active": z.value === t.rowIndex || j() === t.rowIndex,
292
+ "is-col-active": B.value === t.colIndex || H() === t.colIndex
293
+ }),
294
+ rowspan: t.rowSpan,
295
+ colspan: t.colSpan,
296
+ style: le(t.style),
297
+ tabindex: "0",
298
+ onMouseenter: (l) => y(t.rowIndex, t.colIndex),
299
+ onMouseleave: o[0] || (o[0] = (l) => y(null, null)),
300
+ onFocus: (l) => y(t.rowIndex, t.colIndex),
301
+ onBlur: o[1] || (o[1] = (l) => y(null, null)),
302
+ onClick: (l) => ue(t.key)
303
+ }, [
304
+ p("span", Le, v(t.value), 1)
305
+ ], 46, Ke))), 128))
306
+ ], 6))), 128))
307
+ ])
308
+ ])
309
+ ])) : (u(), c("p", Me, "当前工作表没有可展示的数据。"))
310
+ ]));
311
+ }
312
+ });
313
+ export {
314
+ qe as default
315
+ };
@@ -1,5 +1,5 @@
1
1
  import { defineComponent as o, openBlock as l, createElementBlock as a, createElementVNode as t, toDisplayString as i, unref as r } from "vue";
2
- import { d as s } from "./index-BNN8UXSN.js";
2
+ import { d as s } from "./index-B8Ff5iZD.js";
3
3
  const d = { class: "lfp-empty" }, p = { class: "lfp-hint" }, c = /* @__PURE__ */ o({
4
4
  __name: "UnsupportedRenderer",
5
5
  props: {
@@ -77,7 +77,7 @@ const ee = ["aria-modal"], te = {
77
77
  },
78
78
  emits: ["update:visible", "error"],
79
79
  setup(e, { emit: t }) {
80
- const o = h(() => import("./PdfRenderer-Due2M5cA.js")), c = h(() => import("./TextRenderer-KUhpnf63.js")), i = h(() => import("./DocxRenderer-DogNhBLd.js")), p = h(() => import("./SheetRenderer-D4Y1dYZX.js")), d = h(() => import("./UnsupportedRenderer-8vWx7yB8.js")), l = e, D = t, f = U(null), j = U(!1), L = U(""), n = U(I(l.source, l.fileName)), m = w(() => Q(n.value.name)), N = w(() => l.fileName || n.value.name || l.dialogTitle), b = w(() => l.mode === "dialog"), O = w(() => b.value ? l.visible : !0), T = w(() => m.value === "image" ? "img" : m.value === "video" ? "video" : m.value === "audio" ? "audio" : null);
80
+ const o = h(() => import("./PdfRenderer-Due2M5cA.js")), c = h(() => import("./TextRenderer-KUhpnf63.js")), i = h(() => import("./DocxRenderer-DogNhBLd.js")), p = h(() => import("./SheetRenderer-Dea6-NBg.js")), d = h(() => import("./UnsupportedRenderer-DDB3UZdt.js")), l = e, D = t, f = U(null), j = U(!1), L = U(""), n = U(I(l.source, l.fileName)), m = w(() => Q(n.value.name)), N = w(() => l.fileName || n.value.name || l.dialogTitle), b = w(() => l.mode === "dialog"), O = w(() => b.value ? l.visible : !0), T = w(() => m.value === "image" ? "img" : m.value === "video" ? "video" : m.value === "audio" ? "audio" : null);
81
81
  Z(
82
82
  () => [l.source, l.fileName],
83
83
  () => {
@@ -1,4 +1,4 @@
1
- import { _ as i, i as r, o, r as l } from "./index-BNN8UXSN.js";
1
+ import { _ as i, i as r, o, r as l } from "./index-B8Ff5iZD.js";
2
2
  export {
3
3
  i as FilePreview,
4
4
  r as default,