@ysgroup/ui 0.1.42 → 0.1.44
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/package.json +7 -4
- package/src/components/YsAppToolbar.vue +11 -3
- package/src/components/YsDocViewer.vue +218 -0
- package/src/components/YsFilePreview.vue +215 -158
- package/src/components/YsMediaPlayer.vue +720 -0
- package/src/components/YsPdfViewer.vue +752 -0
- package/src/components/YsRowActionMenu.vue +1 -3
- package/src/components/YsSettingsCenterDialog.vue +13 -0
- package/src/components/YsSheetViewer.vue +461 -0
- package/src/components/YsViewerIcon.vue +53 -0
- package/src/components/YsViewerState.vue +66 -0
- package/src/filePreview.ts +143 -8
- package/src/index.ts +12 -0
- package/src/preview/docx.ts +27 -0
- package/src/preview/media.ts +97 -0
- package/src/preview/pdfAssets.ts +381 -0
- package/src/preview/pdfLayout.ts +125 -0
- package/src/preview/pdfjs.ts +38 -0
- package/src/preview/sheetGrid.ts +205 -0
- package/src/preview/viewer.css +255 -0
- package/src/vTip.ts +114 -0
- package/src/vendor/docx-preview.LICENSE.txt +206 -0
- package/src/vendor/docx-preview.d.mts +25 -0
- package/src/vendor/docx-preview.mjs +42 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/* 表格预览(YsSheetViewer)的纯函数:把 SheetJS 读出的工作表转成可渲染的网格(显示文字、对齐、列宽、合并单元格),
|
|
2
|
+
* 以及虚拟滚动的可见行区间、CSV 编码识别。不依赖 SheetJS,便于单测。 */
|
|
3
|
+
|
|
4
|
+
/** 每个工作表最多显示的行数、列数;超出只显示前面部分并提示下载。 */
|
|
5
|
+
export const SHEET_MAX_ROWS = 10_000;
|
|
6
|
+
export const SHEET_MAX_COLS = 200;
|
|
7
|
+
/** 超过这么大的表格文件不在浏览器里解析(会卡住页面),提示下载。 */
|
|
8
|
+
export const SHEET_MAX_BYTES = 50 * 1024 * 1024;
|
|
9
|
+
/** 行高、列宽默认值(CSS 像素)。 */
|
|
10
|
+
export const SHEET_ROW_HEIGHT = 24;
|
|
11
|
+
export const SHEET_DEFAULT_COL_WIDTH = 88;
|
|
12
|
+
export const SHEET_MIN_COL_WIDTH = 36;
|
|
13
|
+
export const SHEET_MAX_COL_WIDTH = 480;
|
|
14
|
+
|
|
15
|
+
/** SheetJS 单元格(只用到的字段)。t:s 文本、n 数字、b 布尔、d 日期、e 错误、z 空。 */
|
|
16
|
+
export type SheetCellLike = { t?: string; v?: unknown; w?: string };
|
|
17
|
+
export type SheetColLike = { wpx?: number; wch?: number; width?: number; hidden?: boolean };
|
|
18
|
+
export type SheetRange = { s: { r: number; c: number }; e: { r: number; c: number } };
|
|
19
|
+
/** SheetJS 工作表(稀疏模式按 A1 键取,密集模式在 !data 里)。 */
|
|
20
|
+
export type SheetLike = {
|
|
21
|
+
"!ref"?: string;
|
|
22
|
+
"!fullref"?: string;
|
|
23
|
+
"!merges"?: SheetRange[];
|
|
24
|
+
"!cols"?: (SheetColLike | undefined)[];
|
|
25
|
+
"!data"?: (SheetCellLike | undefined)[][];
|
|
26
|
+
[address: string]: unknown;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type GridCell = {
|
|
30
|
+
text: string;
|
|
31
|
+
align: "left" | "right" | "center";
|
|
32
|
+
/** 合并单元格的跨行、跨列(只有左上角那格有)。 */
|
|
33
|
+
rowSpan?: number;
|
|
34
|
+
colSpan?: number;
|
|
35
|
+
error?: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type SheetGrid = {
|
|
39
|
+
/** 显示的行列数(已按上限截断)。 */
|
|
40
|
+
rowCount: number;
|
|
41
|
+
colCount: number;
|
|
42
|
+
/** 工作表实际的行列数。 */
|
|
43
|
+
totalRows: number;
|
|
44
|
+
totalCols: number;
|
|
45
|
+
/** 行列起点(工作表不一定从 A1 开始),用于显示行号、列名。 */
|
|
46
|
+
firstRow: number;
|
|
47
|
+
firstCol: number;
|
|
48
|
+
colWidths: number[];
|
|
49
|
+
/** cells[r][c];null 为空格,undefined 表示被合并单元格盖住、不渲染。 */
|
|
50
|
+
cells: (GridCell | null | undefined)[][];
|
|
51
|
+
merges: { r: number; c: number; rowSpan: number; colSpan: number }[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** 列号(0 开始)转列名:0 → A,25 → Z,26 → AA。 */
|
|
55
|
+
export function columnName(index: number): string {
|
|
56
|
+
let n = Math.max(0, Math.floor(index)) + 1;
|
|
57
|
+
let name = "";
|
|
58
|
+
while (n > 0) {
|
|
59
|
+
const rem = (n - 1) % 26;
|
|
60
|
+
name = String.fromCharCode(65 + rem) + name;
|
|
61
|
+
n = Math.floor((n - 1) / 26);
|
|
62
|
+
}
|
|
63
|
+
return name;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 单元格地址 → 行列(0 开始):B3 → { r: 2, c: 1 }。 */
|
|
67
|
+
export function decodeCell(address: string): { r: number; c: number } | null {
|
|
68
|
+
const match = /^\$?([A-Z]+)\$?(\d+)$/i.exec(String(address).trim());
|
|
69
|
+
if (!match) return null;
|
|
70
|
+
let c = 0;
|
|
71
|
+
for (const ch of match[1].toUpperCase()) c = c * 26 + (ch.charCodeAt(0) - 64);
|
|
72
|
+
return { r: Number(match[2]) - 1, c: c - 1 };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 行列(0 开始)→ 单元格地址。 */
|
|
76
|
+
export function encodeCell(r: number, c: number): string {
|
|
77
|
+
return `${columnName(c)}${r + 1}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 区域 → 起止行列:A1:C10;单个格子也可以。 */
|
|
81
|
+
export function decodeRange(ref: string | undefined): SheetRange | null {
|
|
82
|
+
if (!ref) return null;
|
|
83
|
+
const [a, b = a] = String(ref).split(":");
|
|
84
|
+
const s = decodeCell(a);
|
|
85
|
+
const e = decodeCell(b);
|
|
86
|
+
if (!s || !e) return null;
|
|
87
|
+
return {
|
|
88
|
+
s: { r: Math.min(s.r, e.r), c: Math.min(s.c, e.c) },
|
|
89
|
+
e: { r: Math.max(s.r, e.r), c: Math.max(s.c, e.c) },
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Excel 列宽 → CSS 像素。wpx 优先,其次 wch(字符数,按 7px 一字符加 5px 边距),限制在合理范围。 */
|
|
94
|
+
export function columnWidthPx(col: SheetColLike | undefined): number {
|
|
95
|
+
let px = SHEET_DEFAULT_COL_WIDTH;
|
|
96
|
+
if (col?.wpx) px = col.wpx;
|
|
97
|
+
else if (col?.wch) px = col.wch * 7 + 5;
|
|
98
|
+
else if (col?.width) px = col.width * 7 + 5;
|
|
99
|
+
return Math.round(Math.min(SHEET_MAX_COL_WIDTH, Math.max(SHEET_MIN_COL_WIDTH, px)));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 单元格显示文字与对齐:有格式化文字(w,按单元格数字/日期格式)就用它;数字、日期右对齐,布尔、错误居中。 */
|
|
103
|
+
export function cellDisplay(cell: SheetCellLike | undefined): GridCell | null {
|
|
104
|
+
if (!cell || cell.t === "z") return null;
|
|
105
|
+
const raw = cell.v;
|
|
106
|
+
let text = typeof cell.w === "string" ? cell.w : "";
|
|
107
|
+
if (!text && raw !== undefined && raw !== null) {
|
|
108
|
+
if (raw instanceof Date) text = raw.toISOString().slice(0, 10);
|
|
109
|
+
else if (cell.t === "b") text = raw ? "TRUE" : "FALSE";
|
|
110
|
+
else text = String(raw);
|
|
111
|
+
}
|
|
112
|
+
if (text === "") return null;
|
|
113
|
+
if (cell.t === "n" || cell.t === "d") return { text, align: "right" };
|
|
114
|
+
if (cell.t === "b") return { text, align: "center" };
|
|
115
|
+
if (cell.t === "e") return { text, align: "center", error: true };
|
|
116
|
+
return { text, align: "left" };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function readCell(sheet: SheetLike, r: number, c: number): SheetCellLike | undefined {
|
|
120
|
+
const dense = sheet["!data"];
|
|
121
|
+
if (dense) return dense[r]?.[c];
|
|
122
|
+
return sheet[encodeCell(r, c)] as SheetCellLike | undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 工作表 → 网格。超过 maxRows/maxCols 的部分不显示(totalRows/totalCols 仍是实际大小,界面据此提示)。
|
|
127
|
+
* 用 SheetJS 的 sheetRows 只读了前若干行时,实际范围在 !fullref 里。
|
|
128
|
+
*/
|
|
129
|
+
export function buildSheetGrid(sheet: SheetLike, limits: { maxRows?: number; maxCols?: number } = {}): SheetGrid {
|
|
130
|
+
const maxRows = limits.maxRows ?? SHEET_MAX_ROWS;
|
|
131
|
+
const maxCols = limits.maxCols ?? SHEET_MAX_COLS;
|
|
132
|
+
const range = decodeRange(sheet["!ref"]);
|
|
133
|
+
const full = decodeRange(sheet["!fullref"]) ?? range;
|
|
134
|
+
if (!range || !full) {
|
|
135
|
+
return { rowCount: 0, colCount: 0, totalRows: 0, totalCols: 0, firstRow: 0, firstCol: 0, colWidths: [], cells: [], merges: [] };
|
|
136
|
+
}
|
|
137
|
+
const firstRow = range.s.r;
|
|
138
|
+
const firstCol = range.s.c;
|
|
139
|
+
const totalRows = full.e.r - firstRow + 1;
|
|
140
|
+
const totalCols = full.e.c - firstCol + 1;
|
|
141
|
+
const rowCount = Math.min(maxRows, range.e.r - firstRow + 1);
|
|
142
|
+
const colCount = Math.min(maxCols, range.e.c - firstCol + 1);
|
|
143
|
+
|
|
144
|
+
const cols = sheet["!cols"] ?? [];
|
|
145
|
+
const colWidths = Array.from({ length: colCount }, (_, i) => columnWidthPx(cols[firstCol + i]));
|
|
146
|
+
|
|
147
|
+
const cells: (GridCell | null | undefined)[][] = Array.from({ length: rowCount }, (_, r) =>
|
|
148
|
+
Array.from({ length: colCount }, (_, c) => cellDisplay(readCell(sheet, firstRow + r, firstCol + c))),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const merges: SheetGrid["merges"] = [];
|
|
152
|
+
for (const merge of sheet["!merges"] ?? []) {
|
|
153
|
+
const r0 = merge.s.r - firstRow;
|
|
154
|
+
const c0 = merge.s.c - firstCol;
|
|
155
|
+
if (r0 < 0 || c0 < 0 || r0 >= rowCount || c0 >= colCount) continue;
|
|
156
|
+
const r1 = Math.min(rowCount - 1, merge.e.r - firstRow);
|
|
157
|
+
const c1 = Math.min(colCount - 1, merge.e.c - firstCol);
|
|
158
|
+
const rowSpan = r1 - r0 + 1;
|
|
159
|
+
const colSpan = c1 - c0 + 1;
|
|
160
|
+
if (rowSpan === 1 && colSpan === 1) continue;
|
|
161
|
+
merges.push({ r: r0, c: c0, rowSpan, colSpan });
|
|
162
|
+
const origin = cells[r0][c0] ?? { text: "", align: "left" as const };
|
|
163
|
+
// 合并区域的文字默认居中(Excel 常见的「合并后居中」),数字仍右对齐。
|
|
164
|
+
cells[r0][c0] = { ...origin, align: origin.align === "right" ? "right" : "center", rowSpan, colSpan };
|
|
165
|
+
for (let r = r0; r <= r1; r += 1) {
|
|
166
|
+
for (let c = c0; c <= c1; c += 1) if (r !== r0 || c !== c0) cells[r][c] = undefined;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return { rowCount, colCount, totalRows, totalCols, firstRow, firstCol, colWidths, cells, merges };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 虚拟滚动:给出滚动位置和视口高度,求要渲染的行区间 [start, end)(0 开始)。
|
|
175
|
+
* 上下各多渲染 overscan 行;起点落在合并单元格中间时往上扩到合并区域的第一行,合并格才不会被截掉。
|
|
176
|
+
*/
|
|
177
|
+
export function visibleRows(
|
|
178
|
+
scrollTop: number,
|
|
179
|
+
viewportHeight: number,
|
|
180
|
+
rowCount: number,
|
|
181
|
+
options: { rowHeight?: number; overscan?: number; merges?: SheetGrid["merges"] } = {},
|
|
182
|
+
): { start: number; end: number } {
|
|
183
|
+
const rowHeight = options.rowHeight ?? SHEET_ROW_HEIGHT;
|
|
184
|
+
const overscan = options.overscan ?? 8;
|
|
185
|
+
if (rowCount <= 0) return { start: 0, end: 0 };
|
|
186
|
+
let start = Math.max(0, Math.floor(Math.max(0, scrollTop) / rowHeight) - overscan);
|
|
187
|
+
const end = Math.min(rowCount, Math.ceil((Math.max(0, scrollTop) + Math.max(0, viewportHeight)) / rowHeight) + overscan);
|
|
188
|
+
for (const merge of options.merges ?? []) {
|
|
189
|
+
if (merge.r < start && merge.r + merge.rowSpan > start) start = merge.r;
|
|
190
|
+
}
|
|
191
|
+
return { start: Math.min(start, end), end };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* CSV 字节 → 文字:有 UTF-8 BOM 或能按 UTF-8 严格解码就用 UTF-8,否则按 GB18030(兼容 GBK,Excel 中文版另存的 CSV 常见)。
|
|
196
|
+
*/
|
|
197
|
+
export function decodeCsvBytes(bytes: Uint8Array): string {
|
|
198
|
+
if (bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) return new TextDecoder("utf-8").decode(bytes.subarray(3));
|
|
199
|
+
if (bytes[0] === 0xff && bytes[1] === 0xfe) return new TextDecoder("utf-16le").decode(bytes.subarray(2));
|
|
200
|
+
try {
|
|
201
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
202
|
+
} catch {
|
|
203
|
+
return new TextDecoder("gb18030").decode(bytes);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/* 文件查看器共用外观(YsFilePreview、YsPdfViewer、YsDocViewer、YsSheetViewer、YsMediaPlayer):
|
|
2
|
+
* 工具条、图标按钮、页码输入、状态(加载/出错/不支持)、转圈。颜色只用 --ys-* 令牌,明暗自动适配。 */
|
|
3
|
+
|
|
4
|
+
.ys-viewer {
|
|
5
|
+
position: relative;
|
|
6
|
+
display: flex;
|
|
7
|
+
flex-direction: column;
|
|
8
|
+
min-width: 0;
|
|
9
|
+
height: 100%;
|
|
10
|
+
min-height: 0;
|
|
11
|
+
container-type: inline-size;
|
|
12
|
+
background: var(--ys-color-stage);
|
|
13
|
+
color: var(--ys-color-text-1);
|
|
14
|
+
font-family: var(--ys-font-family);
|
|
15
|
+
font-size: 13px;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.ys-viewer:focus,
|
|
19
|
+
.ys-viewer:focus-visible {
|
|
20
|
+
outline: none;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/* ---------- 工具条 ---------- */
|
|
24
|
+
.ys-viewer__bar {
|
|
25
|
+
display: flex;
|
|
26
|
+
flex: none;
|
|
27
|
+
align-items: center;
|
|
28
|
+
gap: 2px;
|
|
29
|
+
min-height: 40px;
|
|
30
|
+
padding: 0 8px;
|
|
31
|
+
overflow-x: auto;
|
|
32
|
+
border-bottom: 1px solid var(--ys-color-border);
|
|
33
|
+
background: var(--ys-color-bg-elevated);
|
|
34
|
+
scrollbar-width: none;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.ys-viewer__bar::-webkit-scrollbar {
|
|
38
|
+
display: none;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.ys-viewer__spacer {
|
|
42
|
+
flex: 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.ys-viewer__sep {
|
|
46
|
+
flex: none;
|
|
47
|
+
width: 1px;
|
|
48
|
+
height: 16px;
|
|
49
|
+
margin: 0 6px;
|
|
50
|
+
background: var(--ys-color-border);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.ys-viewer__btn {
|
|
54
|
+
display: inline-flex;
|
|
55
|
+
flex: none;
|
|
56
|
+
align-items: center;
|
|
57
|
+
justify-content: center;
|
|
58
|
+
gap: 4px;
|
|
59
|
+
min-width: 28px;
|
|
60
|
+
height: 28px;
|
|
61
|
+
padding: 0 6px;
|
|
62
|
+
border: 0;
|
|
63
|
+
border-radius: var(--ys-radius-s);
|
|
64
|
+
background: transparent;
|
|
65
|
+
color: var(--ys-color-text-2);
|
|
66
|
+
font: inherit;
|
|
67
|
+
font-size: 12px;
|
|
68
|
+
font-variant-numeric: tabular-nums;
|
|
69
|
+
white-space: nowrap;
|
|
70
|
+
cursor: pointer;
|
|
71
|
+
transition:
|
|
72
|
+
background-color 0.15s,
|
|
73
|
+
color 0.15s;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.ys-viewer__btn:hover:not(:disabled) {
|
|
77
|
+
background: var(--ys-color-bg-hover);
|
|
78
|
+
color: var(--ys-color-text-1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.ys-viewer__btn.is-active {
|
|
82
|
+
background: color-mix(in srgb, var(--ys-color-primary) 12%, transparent);
|
|
83
|
+
color: var(--ys-color-primary);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.ys-viewer__btn:disabled {
|
|
87
|
+
cursor: not-allowed;
|
|
88
|
+
opacity: 0.4;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.ys-viewer__btn:focus {
|
|
92
|
+
outline: none;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.ys-viewer__btn:focus-visible {
|
|
96
|
+
outline: 2px solid color-mix(in srgb, var(--ys-color-primary) 55%, transparent);
|
|
97
|
+
outline-offset: -2px;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.ys-viewer__btn svg {
|
|
101
|
+
flex: none;
|
|
102
|
+
width: 16px;
|
|
103
|
+
height: 16px;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.ys-viewer__pager {
|
|
107
|
+
display: inline-flex;
|
|
108
|
+
flex: none;
|
|
109
|
+
align-items: center;
|
|
110
|
+
gap: 6px;
|
|
111
|
+
margin: 0 2px;
|
|
112
|
+
color: var(--ys-color-text-3);
|
|
113
|
+
font-size: 12px;
|
|
114
|
+
font-variant-numeric: tabular-nums;
|
|
115
|
+
white-space: nowrap;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.ys-viewer__input {
|
|
119
|
+
box-sizing: border-box;
|
|
120
|
+
width: 48px;
|
|
121
|
+
height: 26px;
|
|
122
|
+
padding: 0 4px;
|
|
123
|
+
border: 1px solid var(--ys-color-border);
|
|
124
|
+
border-radius: var(--ys-radius-s);
|
|
125
|
+
background: var(--ys-color-bg);
|
|
126
|
+
color: var(--ys-color-text-1);
|
|
127
|
+
font: inherit;
|
|
128
|
+
font-size: 12px;
|
|
129
|
+
font-variant-numeric: tabular-nums;
|
|
130
|
+
text-align: center;
|
|
131
|
+
transition: border-color 0.15s;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.ys-viewer__input:focus {
|
|
135
|
+
border-color: var(--ys-color-primary);
|
|
136
|
+
outline: none;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.ys-viewer__notice {
|
|
140
|
+
display: flex;
|
|
141
|
+
flex: none;
|
|
142
|
+
align-items: center;
|
|
143
|
+
gap: 8px;
|
|
144
|
+
padding: 6px 12px;
|
|
145
|
+
border-bottom: 1px solid color-mix(in srgb, var(--ys-color-warning) 30%, var(--ys-color-border));
|
|
146
|
+
background: color-mix(in srgb, var(--ys-color-warning) 10%, var(--ys-color-bg-elevated));
|
|
147
|
+
color: var(--ys-color-text-2);
|
|
148
|
+
font-size: 12px;
|
|
149
|
+
line-height: 18px;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.ys-viewer__notice::before {
|
|
153
|
+
content: "";
|
|
154
|
+
flex: none;
|
|
155
|
+
width: 6px;
|
|
156
|
+
height: 6px;
|
|
157
|
+
border-radius: 50%;
|
|
158
|
+
background: var(--ys-color-warning);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* ---------- 状态:加载、出错、不支持 ---------- */
|
|
162
|
+
.ys-viewer__state {
|
|
163
|
+
display: flex;
|
|
164
|
+
flex: 1;
|
|
165
|
+
flex-direction: column;
|
|
166
|
+
align-items: center;
|
|
167
|
+
justify-content: center;
|
|
168
|
+
gap: 10px;
|
|
169
|
+
min-height: 220px;
|
|
170
|
+
padding: 24px;
|
|
171
|
+
color: var(--ys-color-text-3);
|
|
172
|
+
font-size: 13px;
|
|
173
|
+
line-height: 1.6;
|
|
174
|
+
text-align: center;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.ys-viewer__state-icon {
|
|
178
|
+
display: inline-flex;
|
|
179
|
+
align-items: center;
|
|
180
|
+
justify-content: center;
|
|
181
|
+
width: 56px;
|
|
182
|
+
height: 56px;
|
|
183
|
+
margin-bottom: 4px;
|
|
184
|
+
border: 1px solid var(--ys-color-border);
|
|
185
|
+
border-radius: 14px;
|
|
186
|
+
background: var(--ys-color-bg-elevated);
|
|
187
|
+
color: var(--ys-color-text-3);
|
|
188
|
+
box-shadow: var(--ys-shadow-popper);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
.ys-viewer__state-icon svg {
|
|
192
|
+
width: 26px;
|
|
193
|
+
height: 26px;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
.ys-viewer__state-icon.is-danger {
|
|
197
|
+
color: var(--ys-color-danger);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.ys-viewer__state-title {
|
|
201
|
+
margin: 0;
|
|
202
|
+
color: var(--ys-color-text-1);
|
|
203
|
+
font-size: 15px;
|
|
204
|
+
font-weight: 600;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
.ys-viewer__state-detail {
|
|
208
|
+
max-width: 420px;
|
|
209
|
+
margin: 0;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.ys-viewer__state-actions {
|
|
213
|
+
display: flex;
|
|
214
|
+
gap: 8px;
|
|
215
|
+
margin-top: 6px;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.ys-viewer__spinner {
|
|
219
|
+
width: 28px;
|
|
220
|
+
height: 28px;
|
|
221
|
+
border: 2.5px solid color-mix(in srgb, var(--ys-color-primary) 18%, transparent);
|
|
222
|
+
border-top-color: var(--ys-color-primary);
|
|
223
|
+
border-radius: 50%;
|
|
224
|
+
animation: ys-viewer-spin 0.8s linear infinite;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
@keyframes ys-viewer-spin {
|
|
228
|
+
to {
|
|
229
|
+
transform: rotate(360deg);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
@media (prefers-reduced-motion: reduce) {
|
|
234
|
+
.ys-viewer__spinner {
|
|
235
|
+
animation-duration: 2.4s;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/* 窄容器(手机或小窗口)里隐藏次要按钮。 */
|
|
240
|
+
@container (max-width: 560px) {
|
|
241
|
+
.ys-viewer__wide-only {
|
|
242
|
+
display: none !important;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* 工具条下拉菜单(缩放档位、倍速):当前项主色加粗。 */
|
|
247
|
+
.ys-viewer-menu .el-dropdown-menu__item {
|
|
248
|
+
min-width: 96px;
|
|
249
|
+
font-variant-numeric: tabular-nums;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.ys-viewer-menu .el-dropdown-menu__item.is-current {
|
|
253
|
+
color: var(--ys-color-primary);
|
|
254
|
+
font-weight: 600;
|
|
255
|
+
}
|
package/src/vTip.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/* v-tip 悬停提示:`v-tip="文本"`,鼠标移上或键盘聚焦立即显示,移开/失焦/滚动即隐藏,代替浏览器原生 title(要等约 1 秒)。
|
|
2
|
+
* 全页共用一个浮层(.ys-tip),外观由 @ysgroup/theme 的 --ys-tooltip-* 令牌决定,与 el-tooltip 一致;文本为空不显示。
|
|
3
|
+
* 注册:app.directive("tip", vTip)。(模板类型提示需要时在业务仓自行声明 GlobalDirectives,这里不声明,免得与业务仓已有的同名声明冲突。)
|
|
4
|
+
* 默认在元素上方,放不下时放下方;超长文字自动换行(最宽 360px),\n 换行。
|
|
5
|
+
* `v-tip.overflow="文本"`:只在文字被截断(出现省略号)时才显示,适合配合 text-overflow: ellipsis 的单行文字。 */
|
|
6
|
+
import type { Directive } from "vue";
|
|
7
|
+
|
|
8
|
+
let layer: HTMLDivElement | null = null;
|
|
9
|
+
let owner: HTMLElement | null = null;
|
|
10
|
+
const texts = new WeakMap<HTMLElement, string>();
|
|
11
|
+
const overflowOnly = new WeakSet<HTMLElement>();
|
|
12
|
+
|
|
13
|
+
/** 元素内容是否被截断(横向或纵向放不下)。 */
|
|
14
|
+
export function isTextOverflowing(el: HTMLElement): boolean {
|
|
15
|
+
return (
|
|
16
|
+
el.scrollWidth > el.clientWidth + 1 || el.scrollHeight > el.clientHeight + 1
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
let listening = false;
|
|
20
|
+
|
|
21
|
+
function ensureLayer(): HTMLDivElement {
|
|
22
|
+
if (layer && layer.isConnected) return layer;
|
|
23
|
+
layer = document.createElement("div");
|
|
24
|
+
layer.className = "ys-tip";
|
|
25
|
+
layer.id = "ys-tip";
|
|
26
|
+
layer.setAttribute("role", "tooltip");
|
|
27
|
+
layer.style.display = "none";
|
|
28
|
+
document.body.appendChild(layer);
|
|
29
|
+
return layer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 计算浮层位置(纯函数):水平居中对齐目标并限制在视口内,默认在上方,上方放不下时放到下方。 */
|
|
33
|
+
export function placeTip(
|
|
34
|
+
target: Pick<DOMRect, "left" | "top" | "bottom" | "width">,
|
|
35
|
+
tip: { width: number; height: number },
|
|
36
|
+
viewport: { width: number; height: number },
|
|
37
|
+
gap = 6,
|
|
38
|
+
): { left: number; top: number; placement: "top" | "bottom" } {
|
|
39
|
+
const left = Math.min(
|
|
40
|
+
Math.max(4, target.left + target.width / 2 - tip.width / 2),
|
|
41
|
+
Math.max(4, viewport.width - tip.width - 4),
|
|
42
|
+
);
|
|
43
|
+
const above = target.top - tip.height - gap;
|
|
44
|
+
if (above >= 4 || target.bottom + gap + tip.height > viewport.height)
|
|
45
|
+
return { left, top: Math.max(4, above), placement: "top" };
|
|
46
|
+
return { left, top: target.bottom + gap, placement: "bottom" };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function show(el: HTMLElement) {
|
|
50
|
+
const text = texts.get(el);
|
|
51
|
+
if (!text) return;
|
|
52
|
+
if (overflowOnly.has(el) && !isTextOverflowing(el)) return;
|
|
53
|
+
owner = el;
|
|
54
|
+
const tip = ensureLayer();
|
|
55
|
+
tip.textContent = text;
|
|
56
|
+
tip.style.display = "block";
|
|
57
|
+
tip.style.left = "0px";
|
|
58
|
+
tip.style.top = "0px";
|
|
59
|
+
const pos = placeTip(
|
|
60
|
+
el.getBoundingClientRect(),
|
|
61
|
+
{ width: tip.offsetWidth, height: tip.offsetHeight },
|
|
62
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
63
|
+
);
|
|
64
|
+
tip.style.left = `${pos.left}px`;
|
|
65
|
+
tip.style.top = `${pos.top}px`;
|
|
66
|
+
tip.dataset.placement = pos.placement;
|
|
67
|
+
el.setAttribute("aria-describedby", tip.id);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function hide() {
|
|
71
|
+
owner?.removeAttribute("aria-describedby");
|
|
72
|
+
owner = null;
|
|
73
|
+
if (layer) layer.style.display = "none";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function onEnter(event: Event) {
|
|
77
|
+
show(event.currentTarget as HTMLElement);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function listen() {
|
|
81
|
+
if (listening) return;
|
|
82
|
+
listening = true;
|
|
83
|
+
window.addEventListener("scroll", hide, true);
|
|
84
|
+
window.addEventListener("pointerdown", hide, true);
|
|
85
|
+
window.addEventListener("blur", hide);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const vTip: Directive<HTMLElement, string | null | undefined> = {
|
|
89
|
+
mounted(el, binding) {
|
|
90
|
+
texts.set(el, String(binding.value ?? ""));
|
|
91
|
+
if (binding.modifiers.overflow) overflowOnly.add(el);
|
|
92
|
+
el.addEventListener("mouseenter", onEnter);
|
|
93
|
+
el.addEventListener("focusin", onEnter);
|
|
94
|
+
el.addEventListener("mouseleave", hide);
|
|
95
|
+
el.addEventListener("focusout", hide);
|
|
96
|
+
listen();
|
|
97
|
+
},
|
|
98
|
+
updated(el, binding) {
|
|
99
|
+
texts.set(el, String(binding.value ?? ""));
|
|
100
|
+
if (binding.modifiers.overflow) overflowOnly.add(el);
|
|
101
|
+
else overflowOnly.delete(el);
|
|
102
|
+
if (owner === el) {
|
|
103
|
+
if (texts.get(el)) show(el);
|
|
104
|
+
else hide();
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
beforeUnmount(el) {
|
|
108
|
+
el.removeEventListener("mouseenter", onEnter);
|
|
109
|
+
el.removeEventListener("focusin", onEnter);
|
|
110
|
+
el.removeEventListener("mouseleave", hide);
|
|
111
|
+
el.removeEventListener("focusout", hide);
|
|
112
|
+
if (owner === el) hide();
|
|
113
|
+
},
|
|
114
|
+
};
|