@ysgroup/ui 0.1.41 → 0.1.43

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.
@@ -143,8 +143,6 @@ function select(action: YsRowAction) {
143
143
  padding: 7px;
144
144
  border-color: color-mix(in srgb, var(--ys-color-primary) 18%, var(--ys-color-border));
145
145
  border-radius: 7px;
146
- box-shadow:
147
- 0 10px 28px color-mix(in srgb, #0f172a 16%, transparent),
148
- 0 2px 6px color-mix(in srgb, #0f172a 8%, transparent);
146
+ box-shadow: var(--ys-shadow-popper);
149
147
  }
150
148
  </style>
@@ -0,0 +1,461 @@
1
+ <!-- YsSheetViewer —— Excel / CSV 只读表格预览(xlsx、xlsm、xls、xlsb、ods、csv)。
2
+ 底部工作表页签;列名行、行号列冻结;合并单元格、列宽;数字、日期按单元格格式显示(日期默认 yyyy-mm-dd);
3
+ 行虚拟滚动,每个工作表最多显示前 10000 行、200 列,超出时提示下载完整文件。点单元格在顶部编辑栏看完整内容。
4
+ CSV 自动识别 UTF-8 / GBK 编码,按原文显示(不把 001 变成 1)。隐藏工作表不显示。
5
+ SheetJS(@e965/xlsx,约 140 KB gzip)在第一次打开表格时才加载。 -->
6
+ <script setup lang="ts">
7
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from "vue";
8
+ import YsViewerIcon from "./YsViewerIcon.vue";
9
+ import YsViewerState from "./YsViewerState.vue";
10
+ import "../preview/viewer.css";
11
+ import { fileExtension, formatFileSize } from "../filePreview";
12
+ import {
13
+ SHEET_MAX_BYTES,
14
+ SHEET_MAX_COLS,
15
+ SHEET_MAX_ROWS,
16
+ SHEET_ROW_HEIGHT,
17
+ buildSheetGrid,
18
+ columnName,
19
+ decodeCsvBytes,
20
+ encodeCell,
21
+ visibleRows,
22
+ type SheetGrid,
23
+ type SheetLike,
24
+ } from "../preview/sheetGrid";
25
+
26
+ const props = withDefaults(
27
+ defineProps<{
28
+ /** 表格内容:Blob、ArrayBuffer 或字节。 */
29
+ source: Blob | ArrayBuffer | Uint8Array | null;
30
+ /** 文件名,用来判断是不是 CSV(也可直接传 csv)。 */
31
+ fileName?: string;
32
+ /** 按 CSV 解析(自动识别 UTF-8 / GBK)。 */
33
+ csv?: boolean;
34
+ /** 每个工作表最多显示的行数。 */
35
+ maxRows?: number;
36
+ }>(),
37
+ { fileName: "", csv: false, maxRows: SHEET_MAX_ROWS },
38
+ );
39
+
40
+ const emit = defineEmits<{
41
+ loaded: [sheets: number];
42
+ error: [message: string];
43
+ }>();
44
+
45
+ type SheetEntry = { name: string; sheet: SheetLike; grid: SheetGrid | null };
46
+
47
+ const scroller = ref<HTMLElement | null>(null);
48
+ const tabs = ref<HTMLElement | null>(null);
49
+ const status = ref<"loading" | "ready" | "error">("loading");
50
+ const errorText = ref("");
51
+ const sheets = shallowRef<SheetEntry[]>([]);
52
+ const active = ref(0);
53
+ const grid = shallowRef<SheetGrid | null>(null);
54
+ const scrollTop = ref(0);
55
+ const viewportHeight = ref(0);
56
+ const selected = ref<{ r: number; c: number } | null>(null);
57
+ let token = 0;
58
+ let resizeObserver: ResizeObserver | null = null;
59
+
60
+ const RH = SHEET_ROW_HEIGHT;
61
+ const rowHeadWidth = computed(() => {
62
+ const g = grid.value;
63
+ const digits = String((g?.firstRow ?? 0) + (g?.rowCount ?? 0)).length;
64
+ return Math.max(40, digits * 8 + 20);
65
+ });
66
+ const totalWidth = computed(() => rowHeadWidth.value + (grid.value?.colWidths.reduce((a, b) => a + b, 0) ?? 0));
67
+ const range = computed(() =>
68
+ visibleRows(Math.max(0, scrollTop.value - RH), viewportHeight.value, grid.value?.rowCount ?? 0, { merges: grid.value?.merges }),
69
+ );
70
+ const windowRows = computed(() => {
71
+ const out: number[] = [];
72
+ for (let r = range.value.start; r < range.value.end; r += 1) out.push(r);
73
+ return out;
74
+ });
75
+ const truncated = computed(() => {
76
+ const g = grid.value;
77
+ if (!g) return "";
78
+ const parts: string[] = [];
79
+ if (g.totalRows > g.rowCount) parts.push(`共 ${g.totalRows.toLocaleString()} 行,只显示前 ${g.rowCount.toLocaleString()} 行`);
80
+ if (g.totalCols > g.colCount) parts.push(`共 ${g.totalCols} 列,只显示前 ${g.colCount} 列`);
81
+ return parts.length ? `${parts.join(";")}。完整内容请下载后用 Excel / WPS 查看。` : "";
82
+ });
83
+ const selectedAddress = computed(() => {
84
+ const g = grid.value;
85
+ if (!g || !selected.value) return "";
86
+ return encodeCell(g.firstRow + selected.value.r, g.firstCol + selected.value.c);
87
+ });
88
+ const selectedText = computed(() => {
89
+ const g = grid.value;
90
+ if (!g || !selected.value) return "";
91
+ return g.cells[selected.value.r]?.[selected.value.c]?.text ?? "";
92
+ });
93
+ const summary = computed(() => {
94
+ const g = grid.value;
95
+ if (!g || !g.rowCount) return "空工作表";
96
+ return `${g.totalRows.toLocaleString()} 行 × ${g.totalCols} 列`;
97
+ });
98
+
99
+ function isCsv(source: Blob | ArrayBuffer | Uint8Array): boolean {
100
+ if (props.csv) return true;
101
+ if (fileExtension(props.fileName) === "csv") return true;
102
+ return source instanceof Blob && /csv/i.test(source.type);
103
+ }
104
+
105
+ function describe(reason: unknown): string {
106
+ const message = reason instanceof Error ? reason.message : String(reason ?? "");
107
+ if (/password|encrypt/i.test(message)) return "表格已加密,暂不支持在线查看,请下载后打开。";
108
+ if (/dynamically imported module|Importing a module script failed|Loading chunk|Failed to fetch/i.test(message)) return "表格预览组件加载失败,请检查网络后重试。";
109
+ return "表格文件解析失败,请下载后用 Excel / WPS 查看。";
110
+ }
111
+
112
+ async function parse() {
113
+ const mine = ++token;
114
+ status.value = "loading";
115
+ errorText.value = "";
116
+ sheets.value = [];
117
+ grid.value = null;
118
+ selected.value = null;
119
+ active.value = 0;
120
+ const source = props.source;
121
+ if (!source) return;
122
+ try {
123
+ const size = source instanceof Blob ? source.size : source.byteLength;
124
+ if (size > SHEET_MAX_BYTES) throw new Error(`too-large:${size}`);
125
+ const [XLSX, bytes] = await Promise.all([
126
+ import("@e965/xlsx"),
127
+ source instanceof Blob ? source.arrayBuffer().then((b) => new Uint8Array(b)) : Promise.resolve(source instanceof Uint8Array ? source : new Uint8Array(source)),
128
+ ]);
129
+ if (mine !== token) return;
130
+ // cellStyles 才会读列宽(!cols);cellDates 关闭时日期按单元格格式出文字,格式 14(系统短日期)改成 yyyy-mm-dd。
131
+ const common = { sheetRows: props.maxRows, dense: true, cellText: true, cellStyles: true, cellDates: false, dateNF: "yyyy-mm-dd" } as const;
132
+ const book = isCsv(source) ? XLSX.read(decodeCsvBytes(bytes), { ...common, type: "string", raw: true }) : XLSX.read(bytes, { ...common, type: "array" });
133
+ if (mine !== token) return;
134
+ const hidden = book.Workbook?.Sheets ?? [];
135
+ const entries = book.SheetNames.map((name, i) => ({ name, sheet: book.Sheets[name] as unknown as SheetLike, grid: null, hidden: Boolean(hidden[i]?.Hidden) }))
136
+ .filter((entry) => !entry.hidden)
137
+ .map(({ name, sheet, grid: g }) => ({ name, sheet, grid: g }) as SheetEntry);
138
+ sheets.value = entries;
139
+ status.value = "ready";
140
+ emit("loaded", entries.length);
141
+ await nextTick();
142
+ activate(0);
143
+ } catch (reason) {
144
+ if (mine !== token) return;
145
+ status.value = "error";
146
+ const message = reason instanceof Error ? reason.message : "";
147
+ errorText.value = message.startsWith("too-large:")
148
+ ? `表格文件较大(${formatFileSize(Number(message.slice(10)))}),在线解析会卡住页面,请下载后用 Excel / WPS 查看。`
149
+ : describe(reason);
150
+ emit("error", errorText.value);
151
+ }
152
+ }
153
+
154
+ function activate(index: number) {
155
+ const entry = sheets.value[index];
156
+ if (!entry) return;
157
+ active.value = index;
158
+ entry.grid ??= buildSheetGrid(entry.sheet, { maxRows: props.maxRows, maxCols: SHEET_MAX_COLS });
159
+ grid.value = entry.grid;
160
+ selected.value = null;
161
+ scrollTop.value = 0;
162
+ if (scroller.value) {
163
+ scroller.value.scrollTop = 0;
164
+ scroller.value.scrollLeft = 0;
165
+ }
166
+ void nextTick(() => {
167
+ tabs.value?.querySelector<HTMLElement>(".is-active")?.scrollIntoView({ block: "nearest", inline: "nearest" });
168
+ });
169
+ }
170
+
171
+ function onScroll() {
172
+ if (scroller.value) scrollTop.value = scroller.value.scrollTop;
173
+ }
174
+
175
+ function measure() {
176
+ if (scroller.value) viewportHeight.value = scroller.value.clientHeight;
177
+ }
178
+
179
+ function rowSpanOf(r: number, span: number | undefined): number | undefined {
180
+ if (!span || span <= 1) return undefined;
181
+ return Math.min(span, range.value.end - r);
182
+ }
183
+
184
+ function select(r: number, c: number) {
185
+ selected.value = { r, c };
186
+ }
187
+
188
+ function isSelected(r: number, c: number): boolean {
189
+ return selected.value?.r === r && selected.value?.c === c;
190
+ }
191
+
192
+ onMounted(() => {
193
+ resizeObserver = new ResizeObserver(() => measure());
194
+ if (scroller.value) resizeObserver.observe(scroller.value);
195
+ measure();
196
+ });
197
+
198
+ watch(scroller, (el, old) => {
199
+ if (old) resizeObserver?.unobserve(old);
200
+ if (el) {
201
+ resizeObserver?.observe(el);
202
+ measure();
203
+ }
204
+ });
205
+
206
+ watch(
207
+ () => [props.source, props.csv, props.fileName] as const,
208
+ () => void parse(),
209
+ { immediate: true },
210
+ );
211
+
212
+ onBeforeUnmount(() => {
213
+ token += 1;
214
+ resizeObserver?.disconnect();
215
+ });
216
+ </script>
217
+
218
+ <template>
219
+ <div class="ys-viewer ys-sheet">
220
+ <YsViewerState v-if="status === 'loading'" title="正在解析表格…" />
221
+ <YsViewerState v-else-if="status === 'error'" type="error" title="无法显示此表格" :detail="errorText">
222
+ <template v-if="$slots['error-actions']" #actions><slot name="error-actions" /></template>
223
+ </YsViewerState>
224
+ <template v-else>
225
+ <div class="ys-sheet__formula" role="status">
226
+ <span class="ys-sheet__address">{{ selectedAddress || "—" }}</span>
227
+ <span class="ys-sheet__content" :title="selectedText">{{ selected ? selectedText : summary }}</span>
228
+ <slot name="toolbar" />
229
+ </div>
230
+ <div v-if="truncated" class="ys-viewer__notice">{{ truncated }}</div>
231
+ <YsViewerState v-if="sheets.length === 0" type="empty" icon="sheet" title="表格里没有可显示的工作表" />
232
+ <YsViewerState v-else-if="grid && grid.rowCount === 0" type="empty" icon="sheet" title="空工作表" />
233
+ </template>
234
+ <div v-show="status === 'ready' && grid && grid.rowCount > 0" ref="scroller" class="ys-sheet__scroller" tabindex="0" aria-label="表格内容" @scroll.passive="onScroll">
235
+ <table v-if="grid" class="ys-sheet__table" :style="{ width: `${totalWidth}px` }">
236
+ <colgroup>
237
+ <col :style="{ width: `${rowHeadWidth}px` }" />
238
+ <col v-for="(w, c) in grid.colWidths" :key="c" :style="{ width: `${w}px` }" />
239
+ </colgroup>
240
+ <thead>
241
+ <tr :style="{ height: `${RH}px` }">
242
+ <th class="ys-sheet__corner" />
243
+ <th v-for="(_, c) in grid.colWidths" :key="c" class="ys-sheet__colhead" :class="{ 'is-active': selected?.c === c }">{{ columnName(grid.firstCol + c) }}</th>
244
+ </tr>
245
+ </thead>
246
+ <tbody>
247
+ <tr v-if="range.start > 0" class="ys-sheet__spacer" :style="{ height: `${range.start * RH}px` }"><td :colspan="grid.colCount + 1" /></tr>
248
+ <tr v-for="r in windowRows" :key="r" :style="{ height: `${RH}px` }">
249
+ <th class="ys-sheet__rowhead" :class="{ 'is-active': selected?.r === r }">{{ grid.firstRow + r + 1 }}</th>
250
+ <template v-for="(cell, c) in grid.cells[r]" :key="c">
251
+ <td
252
+ v-if="cell !== undefined"
253
+ :rowspan="rowSpanOf(r, cell?.rowSpan)"
254
+ :colspan="cell?.colSpan && cell.colSpan > 1 ? cell.colSpan : undefined"
255
+ :class="[cell ? `is-${cell.align}` : '', { 'is-error': cell?.error, 'is-selected': isSelected(r, c), 'is-merged': (cell?.rowSpan ?? 1) > 1 || (cell?.colSpan ?? 1) > 1 }]"
256
+ @click="select(r, c)"
257
+ >
258
+ {{ cell?.text }}
259
+ </td>
260
+ </template>
261
+ </tr>
262
+ <tr v-if="range.end < grid.rowCount" class="ys-sheet__spacer" :style="{ height: `${(grid.rowCount - range.end) * RH}px` }"><td :colspan="grid.colCount + 1" /></tr>
263
+ </tbody>
264
+ </table>
265
+ </div>
266
+ <div v-if="status === 'ready' && sheets.length" ref="tabs" class="ys-sheet__tabs" role="tablist" aria-label="工作表">
267
+ <YsViewerIcon name="sheet" class="ys-sheet__tabs-icon" />
268
+ <button
269
+ v-for="(entry, i) in sheets"
270
+ :key="entry.name"
271
+ type="button"
272
+ role="tab"
273
+ class="ys-sheet__tab"
274
+ :class="{ 'is-active': i === active }"
275
+ :aria-selected="i === active"
276
+ :title="entry.name"
277
+ @click="activate(i)"
278
+ >
279
+ {{ entry.name }}
280
+ </button>
281
+ </div>
282
+ </div>
283
+ </template>
284
+
285
+ <style scoped>
286
+ .ys-sheet {
287
+ background: var(--ys-color-bg-elevated);
288
+ }
289
+ .ys-sheet__formula {
290
+ display: flex;
291
+ flex: none;
292
+ align-items: center;
293
+ gap: 0;
294
+ height: 32px;
295
+ border-bottom: 1px solid var(--ys-color-border);
296
+ background: var(--ys-color-bg-elevated);
297
+ font-size: 12px;
298
+ }
299
+ .ys-sheet__address {
300
+ flex: none;
301
+ width: 72px;
302
+ height: 100%;
303
+ border-right: 1px solid var(--ys-color-border);
304
+ color: var(--ys-color-text-2);
305
+ font-variant-numeric: tabular-nums;
306
+ font-weight: 600;
307
+ line-height: 32px;
308
+ text-align: center;
309
+ }
310
+ .ys-sheet__content {
311
+ flex: 1;
312
+ min-width: 0;
313
+ padding: 0 12px;
314
+ overflow: hidden;
315
+ color: var(--ys-color-text-1);
316
+ text-overflow: ellipsis;
317
+ white-space: nowrap;
318
+ }
319
+ .ys-sheet__scroller {
320
+ flex: 1;
321
+ min-height: 0;
322
+ overflow: auto;
323
+ overscroll-behavior: contain;
324
+ background: var(--ys-color-bg-elevated);
325
+ }
326
+ .ys-sheet__scroller:focus {
327
+ outline: none;
328
+ }
329
+ .ys-sheet__table {
330
+ table-layout: fixed;
331
+ border-collapse: separate;
332
+ border-spacing: 0;
333
+ color: var(--ys-color-text-1);
334
+ font-size: 12px;
335
+ font-variant-numeric: tabular-nums;
336
+ line-height: 23px;
337
+ }
338
+ .ys-sheet__table th,
339
+ .ys-sheet__table td {
340
+ box-sizing: border-box;
341
+ padding: 0 6px;
342
+ overflow: hidden;
343
+ border-right: 1px solid var(--ys-sheet-grid);
344
+ border-bottom: 1px solid var(--ys-sheet-grid);
345
+ text-overflow: ellipsis;
346
+ white-space: nowrap;
347
+ }
348
+ .ys-sheet {
349
+ --ys-sheet-grid: color-mix(in srgb, var(--ys-color-border) 75%, var(--ys-color-bg-elevated));
350
+ --ys-sheet-head: color-mix(in srgb, var(--ys-color-primary) 5%, var(--ys-color-bg));
351
+ }
352
+ .ys-sheet__table td {
353
+ cursor: cell;
354
+ }
355
+ .ys-sheet__table td.is-right {
356
+ text-align: right;
357
+ }
358
+ .ys-sheet__table td.is-center {
359
+ text-align: center;
360
+ }
361
+ .ys-sheet__table td.is-merged {
362
+ vertical-align: middle;
363
+ background: var(--ys-color-bg-elevated);
364
+ }
365
+ .ys-sheet__table td.is-error {
366
+ color: var(--ys-color-danger);
367
+ }
368
+ .ys-sheet__table td.is-selected {
369
+ box-shadow: inset 0 0 0 2px var(--ys-color-primary);
370
+ }
371
+ .ys-sheet__colhead,
372
+ .ys-sheet__rowhead,
373
+ .ys-sheet__corner {
374
+ position: sticky;
375
+ background: var(--ys-sheet-head);
376
+ color: var(--ys-color-text-3);
377
+ font-weight: 500;
378
+ text-align: center;
379
+ user-select: none;
380
+ }
381
+ .ys-sheet__colhead {
382
+ top: 0;
383
+ z-index: 2;
384
+ }
385
+ .ys-sheet__rowhead {
386
+ left: 0;
387
+ z-index: 1;
388
+ }
389
+ .ys-sheet__corner {
390
+ top: 0;
391
+ left: 0;
392
+ z-index: 3;
393
+ }
394
+ .ys-sheet__colhead.is-active,
395
+ .ys-sheet__rowhead.is-active {
396
+ background: color-mix(in srgb, var(--ys-color-primary) 14%, var(--ys-color-bg));
397
+ color: var(--ys-color-primary);
398
+ font-weight: 600;
399
+ }
400
+ .ys-sheet__spacer td {
401
+ padding: 0;
402
+ border: 0;
403
+ }
404
+ .ys-sheet__tabs {
405
+ display: flex;
406
+ flex: none;
407
+ align-items: stretch;
408
+ gap: 2px;
409
+ height: 34px;
410
+ padding: 0 8px;
411
+ overflow-x: auto;
412
+ border-top: 1px solid var(--ys-color-border);
413
+ background: var(--ys-sheet-head);
414
+ scrollbar-width: none;
415
+ }
416
+ .ys-sheet__tabs::-webkit-scrollbar {
417
+ display: none;
418
+ }
419
+ .ys-sheet__tabs-icon {
420
+ flex: none;
421
+ align-self: center;
422
+ width: 16px;
423
+ height: 16px;
424
+ margin: 0 6px 0 2px;
425
+ color: var(--ys-color-success);
426
+ }
427
+ .ys-sheet__tab {
428
+ position: relative;
429
+ flex: none;
430
+ max-width: 200px;
431
+ padding: 0 14px;
432
+ overflow: hidden;
433
+ border: 0;
434
+ background: transparent;
435
+ color: var(--ys-color-text-2);
436
+ font: inherit;
437
+ font-size: 12px;
438
+ text-overflow: ellipsis;
439
+ white-space: nowrap;
440
+ cursor: pointer;
441
+ }
442
+ .ys-sheet__tab:hover {
443
+ color: var(--ys-color-text-1);
444
+ }
445
+ .ys-sheet__tab:focus {
446
+ outline: none;
447
+ }
448
+ .ys-sheet__tab:focus-visible {
449
+ outline: 2px solid color-mix(in srgb, var(--ys-color-primary) 55%, transparent);
450
+ outline-offset: -2px;
451
+ }
452
+ .ys-sheet__tab.is-active {
453
+ background: var(--ys-color-bg-elevated);
454
+ color: var(--ys-color-primary);
455
+ font-weight: 600;
456
+ box-shadow:
457
+ inset 0 -2px 0 var(--ys-color-primary),
458
+ 1px 0 0 var(--ys-color-border),
459
+ -1px 0 0 var(--ys-color-border);
460
+ }
461
+ </style>
@@ -0,0 +1,53 @@
1
+ <!-- 查看器内部用的线性图标(24 网格、1.75 描边,随 currentColor 变色)。只在 @ysgroup/ui 内部使用。 -->
2
+ <script setup lang="ts">
3
+ import { computed } from "vue";
4
+
5
+ const PATHS = {
6
+ "chevron-left": "M15 18l-6-6 6-6",
7
+ "chevron-right": "M9 18l6-6-6-6",
8
+ plus: "M12 5v14M5 12h14",
9
+ minus: "M5 12h14",
10
+ rotate: "M20 12a8 8 0 1 1-2.34-5.66L20 8.5M20 4v4.5h-4.5",
11
+ sidebar: "M3 6a3 3 0 0 1 3-3h12a3 3 0 0 1 3 3v12a3 3 0 0 1-3 3H6a3 3 0 0 1-3-3zM9 3v18",
12
+ moon: "M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z",
13
+ maximize: "M8 3H5a2 2 0 0 0-2 2v3M21 8V5a2 2 0 0 0-2-2h-3M3 16v3a2 2 0 0 0 2 2h3M16 21h3a2 2 0 0 0 2-2v-3",
14
+ minimize: "M8 3v3a2 2 0 0 1-2 2H3M21 8h-3a2 2 0 0 1-2-2V3M3 16h3a2 2 0 0 1 2 2v3M16 21v-3a2 2 0 0 1 2-2h3",
15
+ play: "M7.5 4.8v14.4a1 1 0 0 0 1.53.85l11.5-7.2a1 1 0 0 0 0-1.7L9.03 3.95A1 1 0 0 0 7.5 4.8z",
16
+ pause: "M7 4.5h3a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1v-13a1 1 0 0 1 1-1zM14 4.5h3a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1h-3a1 1 0 0 1-1-1v-13a1 1 0 0 1 1-1z",
17
+ volume: "M11 5L6.5 9H3.5v6h3l4.5 4zM15.5 8.5a5 5 0 0 1 0 7M18.5 5.5a9 9 0 0 1 0 13",
18
+ "volume-low": "M11 5L6.5 9H3.5v6h3l4.5 4zM15.5 8.5a5 5 0 0 1 0 7",
19
+ mute: "M11 5L6.5 9H3.5v6h3l4.5 4zM22 9l-6 6M16 9l6 6",
20
+ pip: "M21 11V6a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5M14 14h7v6h-7z",
21
+ file: "M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8zM14 3v5h5",
22
+ sheet: "M4 5a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zM4 9h16M4 15h16M10 3v18",
23
+ alert: "M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 7.5v5.5M12 16.5v.01",
24
+ download: "M12 4v11M7 10.5l5 5 5-5M5 20h14",
25
+ "fit-width": "M3 12h18M7 8l-4 4 4 4M17 8l4 4-4 4",
26
+ "fit-page": "M6 3h12a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1zM9 8h6M9 12h6M9 16h4",
27
+ external: "M14 4h6v6M20 4l-9 9M18 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4",
28
+ close: "M6 6l12 12M18 6L6 18",
29
+ music: "M9 18V5l11-2v13M9 18a3 3 0 1 1-6 0 3 3 0 0 1 6 0zM20 16a3 3 0 1 1-6 0 3 3 0 0 1 6 0z",
30
+ } as const;
31
+
32
+ const FILLED = new Set(["play", "pause"]);
33
+
34
+ export type YsViewerIconName = keyof typeof PATHS;
35
+
36
+ const props = defineProps<{ name: YsViewerIconName }>();
37
+ const filled = computed(() => FILLED.has(props.name));
38
+ </script>
39
+
40
+ <template>
41
+ <svg
42
+ viewBox="0 0 24 24"
43
+ :fill="filled ? 'currentColor' : 'none'"
44
+ :stroke="filled ? 'none' : 'currentColor'"
45
+ stroke-width="1.75"
46
+ stroke-linecap="round"
47
+ stroke-linejoin="round"
48
+ aria-hidden="true"
49
+ focusable="false"
50
+ >
51
+ <path :d="PATHS[name]" />
52
+ </svg>
53
+ </template>
@@ -0,0 +1,66 @@
1
+ <!-- 查看器的统一状态面板:加载中(转圈或进度条)、出错、不支持/空。按钮放 actions 插槽。只在 @ysgroup/ui 内部使用。 -->
2
+ <script setup lang="ts">
3
+ import { computed } from "vue";
4
+ import YsViewerIcon, { type YsViewerIconName } from "./YsViewerIcon.vue";
5
+ import "../preview/viewer.css";
6
+
7
+ const props = withDefaults(
8
+ defineProps<{
9
+ type?: "loading" | "error" | "empty";
10
+ title?: string;
11
+ detail?: string;
12
+ /** 0~1 显示进度条;不传显示转圈。 */
13
+ progress?: number | null;
14
+ icon?: YsViewerIconName;
15
+ }>(),
16
+ { type: "loading", title: "", detail: "", progress: null, icon: undefined },
17
+ );
18
+
19
+ const percent = computed(() => (props.progress === null ? null : Math.round(Math.min(1, Math.max(0, props.progress)) * 100)));
20
+ const iconName = computed<YsViewerIconName>(() => props.icon ?? (props.type === "error" ? "alert" : "file"));
21
+ </script>
22
+
23
+ <template>
24
+ <div class="ys-viewer__state" :role="type === 'error' ? 'alert' : 'status'" :aria-busy="type === 'loading'">
25
+ <template v-if="type === 'loading'">
26
+ <div v-if="percent === null" class="ys-viewer__spinner" aria-hidden="true" />
27
+ <div v-else class="ys-viewer-progress" role="progressbar" :aria-valuenow="percent" aria-valuemin="0" aria-valuemax="100">
28
+ <div class="ys-viewer-progress__track"><div class="ys-viewer-progress__bar" :style="{ width: `${percent}%` }" /></div>
29
+ <span>{{ percent }}%</span>
30
+ </div>
31
+ </template>
32
+ <span v-else class="ys-viewer__state-icon" :class="{ 'is-danger': type === 'error' }"><YsViewerIcon :name="iconName" /></span>
33
+ <p v-if="title" class="ys-viewer__state-title">{{ title }}</p>
34
+ <p v-if="detail" class="ys-viewer__state-detail">{{ detail }}</p>
35
+ <div v-if="$slots.actions" class="ys-viewer__state-actions"><slot name="actions" /></div>
36
+ </div>
37
+ </template>
38
+
39
+ <style scoped>
40
+ .ys-viewer-progress {
41
+ display: flex;
42
+ align-items: center;
43
+ gap: 10px;
44
+ width: min(280px, 80%);
45
+ color: var(--ys-color-text-2);
46
+ font-size: 12px;
47
+ font-variant-numeric: tabular-nums;
48
+ }
49
+ .ys-viewer-progress__track {
50
+ flex: 1;
51
+ height: 4px;
52
+ overflow: hidden;
53
+ border-radius: 2px;
54
+ background: color-mix(in srgb, var(--ys-color-primary) 14%, var(--ys-color-border));
55
+ }
56
+ .ys-viewer-progress__bar {
57
+ height: 100%;
58
+ border-radius: inherit;
59
+ background: var(--ys-color-primary);
60
+ transition: width 0.2s ease;
61
+ }
62
+ .ys-viewer-progress span {
63
+ min-width: 34px;
64
+ text-align: right;
65
+ }
66
+ </style>