@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.
- package/package.json +7 -4
- package/src/components/YsCountBadge.vue +4 -1
- package/src/components/YsDocViewer.vue +218 -0
- package/src/components/YsFilePreview.vue +227 -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/YsSheetViewer.vue +461 -0
- package/src/components/YsViewerIcon.vue +53 -0
- package/src/components/YsViewerState.vue +66 -0
- package/src/filePreview.ts +244 -10
- package/src/index.ts +16 -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 +93 -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
package/src/filePreview.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* 文件预览的辅助函数:判断能怎么预览、文件类型标签、大小格式化、把 Blob 存成下载。
|
|
2
2
|
* 与 YsFilePreview 配合使用,也可单独用在文件列表里。 */
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/** 预览方式。excel:xlsx/xlsm/xls/xlsb/ods/csv 表格;word:docx(旧版 .doc 不支持,为 none)。 */
|
|
5
|
+
export type YsPreviewKind = "pdf" | "image" | "text" | "video" | "audio" | "excel" | "word" | "none";
|
|
5
6
|
|
|
6
7
|
export type YsPreviewFile = {
|
|
7
8
|
/** 文件名(含扩展名),用于判断类型和下载命名。 */
|
|
@@ -14,11 +15,40 @@ export type YsPreviewFile = {
|
|
|
14
15
|
|
|
15
16
|
const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "gif", "webp", "bmp", "svg", "avif", "ico"]);
|
|
16
17
|
const TEXT_EXT = new Set([
|
|
17
|
-
"txt", "md", "markdown", "
|
|
18
|
+
"txt", "md", "markdown", "tsv", "log", "json", "xml", "yaml", "yml", "ini", "toml", "conf",
|
|
18
19
|
"py", "js", "ts", "vue", "html", "css", "sql", "sh", "java", "go", "c", "h", "cpp",
|
|
19
20
|
]);
|
|
20
|
-
const VIDEO_EXT = new Set(["mp4", "webm", "ogv", "mov"]);
|
|
21
|
-
const AUDIO_EXT = new Set(["mp3", "wav", "ogg", "m4a", "aac", "flac"]);
|
|
21
|
+
const VIDEO_EXT = new Set(["mp4", "webm", "ogv", "mov", "m4v"]);
|
|
22
|
+
const AUDIO_EXT = new Set(["mp3", "wav", "ogg", "oga", "m4a", "aac", "flac", "opus"]);
|
|
23
|
+
const EXCEL_EXT = new Set(["xlsx", "xlsm", "xls", "xlsb", "ods", "csv"]);
|
|
24
|
+
const WORD_EXT = new Set(["docx"]);
|
|
25
|
+
|
|
26
|
+
/** Office 文件的 MIME。 */
|
|
27
|
+
export const OFFICE_MIME = {
|
|
28
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
29
|
+
xls: "application/vnd.ms-excel",
|
|
30
|
+
xlsb: "application/vnd.ms-excel.sheet.binary.macroenabled.12",
|
|
31
|
+
ods: "application/vnd.oasis.opendocument.spreadsheet",
|
|
32
|
+
csv: "text/csv",
|
|
33
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
34
|
+
doc: "application/msword",
|
|
35
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
36
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
39
|
+
function isExcelMime(mime: string): boolean {
|
|
40
|
+
return (
|
|
41
|
+
mime.includes("spreadsheetml") ||
|
|
42
|
+
mime.startsWith("application/vnd.ms-excel") ||
|
|
43
|
+
mime === OFFICE_MIME.ods ||
|
|
44
|
+
mime === "text/csv" ||
|
|
45
|
+
mime === "application/csv"
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isWordMime(mime: string): boolean {
|
|
50
|
+
return mime.includes("wordprocessingml.document");
|
|
51
|
+
}
|
|
22
52
|
|
|
23
53
|
/** 文本预览最多读这么多字节,再大只显示开头,避免卡住页面。 */
|
|
24
54
|
export const TEXT_PREVIEW_LIMIT = 2 * 1024 * 1024;
|
|
@@ -32,8 +62,15 @@ export function fileExtension(name: string): string {
|
|
|
32
62
|
/** 按扩展名和 MIME 判断预览方式;判断不了返回 none(只能下载)。 */
|
|
33
63
|
export function previewKindOf(file: Pick<YsPreviewFile, "name" | "content_type">): YsPreviewKind {
|
|
34
64
|
const ext = fileExtension(file.name);
|
|
35
|
-
const mime = String(file.content_type || "").toLowerCase();
|
|
65
|
+
const mime = String(file.content_type || "").toLowerCase().split(";")[0].trim();
|
|
36
66
|
if (ext === "pdf" || mime === "application/pdf") return "pdf";
|
|
67
|
+
if (EXCEL_EXT.has(ext)) return "excel";
|
|
68
|
+
if (WORD_EXT.has(ext)) return "word";
|
|
69
|
+
// 文本类扩展名(如 .txt 带着 text/csv)仍按文本预览。
|
|
70
|
+
if (!TEXT_EXT.has(ext)) {
|
|
71
|
+
if (isExcelMime(mime)) return "excel";
|
|
72
|
+
if (isWordMime(mime)) return "word";
|
|
73
|
+
}
|
|
37
74
|
if (IMAGE_EXT.has(ext) || mime.startsWith("image/")) return "image";
|
|
38
75
|
if (VIDEO_EXT.has(ext) || mime.startsWith("video/")) return "video";
|
|
39
76
|
if (AUDIO_EXT.has(ext) || mime.startsWith("audio/")) return "audio";
|
|
@@ -41,27 +78,224 @@ export function previewKindOf(file: Pick<YsPreviewFile, "name" | "content_type">
|
|
|
41
78
|
return "none";
|
|
42
79
|
}
|
|
43
80
|
|
|
44
|
-
/**
|
|
81
|
+
/** 按内容识别出的类型:kind 决定怎么预览,mime 用来给 Blob 补上类型(新标签页打开、浏览器阅读器要用)。 */
|
|
82
|
+
export type YsSniffedType = { kind: YsPreviewKind; mime: string };
|
|
83
|
+
|
|
84
|
+
const UNKNOWN: YsSniffedType = { kind: "none", mime: "" };
|
|
85
|
+
/** 识别要读的文件头字节数:魔数只看前几十字节,文本看这一段能否按 UTF-8 严格解码。 */
|
|
86
|
+
export const SNIFF_HEAD_BYTES = 4096;
|
|
87
|
+
/** PDF 规范允许 %PDF- 前面有少量字节,在开头这么多字节内找。 */
|
|
88
|
+
const PDF_SEARCH_BYTES = 1024;
|
|
89
|
+
/** BMP 的 DIB 头长度只有这几种,用来排除以 BM 开头的文本。 */
|
|
90
|
+
const BMP_DIB_SIZES = new Set([12, 40, 52, 56, 64, 108, 124]);
|
|
91
|
+
|
|
92
|
+
function ascii(bytes: Uint8Array, start: number, text: string): boolean {
|
|
93
|
+
if (bytes.length < start + text.length) return false;
|
|
94
|
+
for (let i = 0; i < text.length; i += 1) if (bytes[start + i] !== text.charCodeAt(i)) return false;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function hasPdfMarker(bytes: Uint8Array): boolean {
|
|
99
|
+
const end = Math.min(bytes.length, PDF_SEARCH_BYTES) - 5;
|
|
100
|
+
for (let i = 0; i <= end; i += 1) if (bytes[i] === 0x25 && ascii(bytes, i, "%PDF-")) return true;
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function ftypType(brand: string): YsSniffedType {
|
|
105
|
+
if (brand === "avif" || brand === "avis") return { kind: "image", mime: "image/avif" };
|
|
106
|
+
if (brand === "qt ") return { kind: "video", mime: "video/quicktime" };
|
|
107
|
+
if (brand.startsWith("M4A") || brand.startsWith("M4B")) return { kind: "audio", mime: "audio/mp4" };
|
|
108
|
+
// HEIC 浏览器大多不能显示,按未知处理(提示下载)。
|
|
109
|
+
if (brand.startsWith("hei") || brand.startsWith("hev") || brand === "mif1" || brand === "msf1") return UNKNOWN;
|
|
110
|
+
return { kind: "video", mime: "video/mp4" };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ZIP_TYPE: YsSniffedType = { kind: "none", mime: "application/zip" };
|
|
114
|
+
const OLE_TYPE: YsSniffedType = { kind: "none", mime: "application/x-ole-storage" };
|
|
115
|
+
/** 按内容识别 ZIP(docx/xlsx)时读文件末尾这么多字节找中央目录里的条目名。 */
|
|
116
|
+
export const SNIFF_TAIL_BYTES = 256 * 1024;
|
|
117
|
+
|
|
118
|
+
function indexOfAscii(bytes: Uint8Array, text: string, from = 0): number {
|
|
119
|
+
const first = text.charCodeAt(0);
|
|
120
|
+
const last = bytes.length - text.length;
|
|
121
|
+
for (let i = from; i <= last; i += 1) if (bytes[i] === first && ascii(bytes, i, text)) return i;
|
|
122
|
+
return -1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function utf16le(text: string): Uint8Array {
|
|
126
|
+
const out = new Uint8Array(text.length * 2);
|
|
127
|
+
for (let i = 0; i < text.length; i += 1) out[i * 2] = text.charCodeAt(i);
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function indexOfBytes(bytes: Uint8Array, needle: Uint8Array): number {
|
|
132
|
+
const last = bytes.length - needle.length;
|
|
133
|
+
outer: for (let i = 0; i <= last; i += 1) {
|
|
134
|
+
for (let j = 0; j < needle.length; j += 1) if (bytes[i + j] !== needle[j]) continue outer;
|
|
135
|
+
return i;
|
|
136
|
+
}
|
|
137
|
+
return -1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 按 ZIP 里的条目名识别 Office Open XML / ODF(纯函数)。bytes 可以是文件开头(本地文件头)或末尾(中央目录)。
|
|
142
|
+
* word/document.xml → docx;xl/workbook.xml(.bin) → xlsx(xlsb);ppt/presentation.xml → pptx(不支持预览);
|
|
143
|
+
* ODF 表格看开头的 mimetype 条目。认不出返回 null。
|
|
144
|
+
*/
|
|
145
|
+
export function sniffZipEntries(bytes: Uint8Array): YsSniffedType | null {
|
|
146
|
+
if (indexOfAscii(bytes, "mimetypeapplication/vnd.oasis.opendocument.spreadsheet") >= 0)
|
|
147
|
+
return { kind: "excel", mime: OFFICE_MIME.ods };
|
|
148
|
+
if (indexOfAscii(bytes, "word/document.xml") >= 0) return { kind: "word", mime: OFFICE_MIME.docx };
|
|
149
|
+
if (indexOfAscii(bytes, "xl/workbook.bin") >= 0) return { kind: "excel", mime: OFFICE_MIME.xlsb };
|
|
150
|
+
if (indexOfAscii(bytes, "xl/workbook.xml") >= 0) return { kind: "excel", mime: OFFICE_MIME.xlsx };
|
|
151
|
+
if (indexOfAscii(bytes, "ppt/presentation.xml") >= 0) return { kind: "none", mime: OFFICE_MIME.pptx };
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 按 OLE 复合文档(旧版 .xls/.doc/.ppt)目录里的流名识别(纯函数)。bytes 为目录扇区内容。
|
|
157
|
+
* Workbook/Book → xls(可预览);WordDocument → doc(不支持预览,提示下载);PowerPoint Document → ppt。
|
|
158
|
+
*/
|
|
159
|
+
export function sniffOleDirectory(bytes: Uint8Array): YsSniffedType | null {
|
|
160
|
+
if (indexOfBytes(bytes, utf16le("WordDocument")) >= 0) return { kind: "none", mime: OFFICE_MIME.doc };
|
|
161
|
+
if (indexOfBytes(bytes, utf16le("Workbook")) >= 0 || indexOfBytes(bytes, utf16le("Book\0")) >= 0)
|
|
162
|
+
return { kind: "excel", mime: OFFICE_MIME.xls };
|
|
163
|
+
if (indexOfBytes(bytes, utf16le("PowerPoint Document")) >= 0) return { kind: "none", mime: OFFICE_MIME.ppt };
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** OLE 复合文档第一个目录扇区在文件里的偏移;不是 OLE 时返回 -1。 */
|
|
168
|
+
export function oleDirectoryOffset(head: Uint8Array): number {
|
|
169
|
+
if (!isOle(head) || head.length < 0x34) return -1;
|
|
170
|
+
const view = new DataView(head.buffer, head.byteOffset, head.byteLength);
|
|
171
|
+
const sectorShift = view.getUint16(0x1e, true);
|
|
172
|
+
if (sectorShift < 7 || sectorShift > 16) return -1;
|
|
173
|
+
const firstDirSector = view.getUint32(0x30, true);
|
|
174
|
+
if (firstDirSector >= 0xfffffffa) return -1;
|
|
175
|
+
return (firstDirSector + 1) * (1 << sectorShift);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function isOle(b: Uint8Array): boolean {
|
|
179
|
+
return b[0] === 0xd0 && b[1] === 0xcf && b[2] === 0x11 && b[3] === 0xe0 && b[4] === 0xa1 && b[5] === 0xb1 && b[6] === 0x1a && b[7] === 0xe1;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isBinaryControl(byte: number): boolean {
|
|
183
|
+
// NUL 和除 \t \n \v \f \r ESC 以外的控制字符基本只出现在二进制文件里。
|
|
184
|
+
return byte < 0x20 && byte !== 0x09 && byte !== 0x0a && byte !== 0x0b && byte !== 0x0c && byte !== 0x0d && byte !== 0x1b;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function looksLikeText(bytes: Uint8Array, complete: boolean): boolean {
|
|
188
|
+
if (bytes.length === 0 || bytes.some(isBinaryControl)) return false;
|
|
189
|
+
try {
|
|
190
|
+
// 只读了开头时用 stream,末尾被截断的多字节字符不算错。
|
|
191
|
+
new TextDecoder("utf-8", { fatal: true }).decode(bytes, { stream: !complete });
|
|
192
|
+
return true;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 按文件头识别类型(纯函数)。head 是文件开头的字节(读 SNIFF_HEAD_BYTES 个即可),size 是整个文件的字节数。
|
|
200
|
+
* 认得 PDF、PNG、JPEG、GIF、WebP、BMP、AVIF,MP4/MOV/M4A(ftyp)、WebM/MKV、MP3、AAC、OGG、WAV、FLAC;
|
|
201
|
+
* 都不是时,文件不超过 TEXT_PREVIEW_LIMIT、开头能按 UTF-8 严格解码且没有二进制控制字符就当文本。
|
|
202
|
+
*/
|
|
203
|
+
export function sniffFileHead(head: Uint8Array, size = head.length): YsSniffedType {
|
|
204
|
+
const b = head;
|
|
205
|
+
if (hasPdfMarker(b)) return { kind: "pdf", mime: "application/pdf" };
|
|
206
|
+
if (ascii(b, 0, "PK\x03\x04")) return sniffZipEntries(b) ?? ZIP_TYPE;
|
|
207
|
+
if (isOle(b)) {
|
|
208
|
+
const offset = oleDirectoryOffset(b);
|
|
209
|
+
return (offset >= 0 && offset < b.length ? sniffOleDirectory(b.subarray(offset)) : null) ?? OLE_TYPE;
|
|
210
|
+
}
|
|
211
|
+
if (ascii(b, 0, "\x89PNG\r\n\x1a\n")) return { kind: "image", mime: "image/png" };
|
|
212
|
+
if (b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return { kind: "image", mime: "image/jpeg" };
|
|
213
|
+
if (ascii(b, 0, "GIF87a") || ascii(b, 0, "GIF89a")) return { kind: "image", mime: "image/gif" };
|
|
214
|
+
if (ascii(b, 0, "RIFF") && ascii(b, 8, "WEBP")) return { kind: "image", mime: "image/webp" };
|
|
215
|
+
if (ascii(b, 0, "RIFF") && ascii(b, 8, "WAVE")) return { kind: "audio", mime: "audio/wav" };
|
|
216
|
+
if (ascii(b, 0, "BM") && b.length >= 18) {
|
|
217
|
+
const view = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
218
|
+
if (view.getUint32(6, true) === 0 && BMP_DIB_SIZES.has(view.getUint32(14, true)))
|
|
219
|
+
return { kind: "image", mime: "image/bmp" };
|
|
220
|
+
}
|
|
221
|
+
if (b.length >= 12 && ascii(b, 4, "ftyp")) return ftypType(String.fromCharCode(b[8], b[9], b[10], b[11]));
|
|
222
|
+
if (b[0] === 0x1a && b[1] === 0x45 && b[2] === 0xdf && b[3] === 0xa3) return { kind: "video", mime: "video/webm" };
|
|
223
|
+
if (ascii(b, 0, "OggS")) return { kind: "audio", mime: "audio/ogg" };
|
|
224
|
+
if (ascii(b, 0, "fLaC")) return { kind: "audio", mime: "audio/flac" };
|
|
225
|
+
if (ascii(b, 0, "ID3")) return { kind: "audio", mime: "audio/mpeg" };
|
|
226
|
+
// 没有 ID3 的 MP3(Layer III 帧同步 FF E2/E3/F2/F3/FA/FB)和 AAC(ADTS,FF F1/F9)。
|
|
227
|
+
// 只认这几种,避免把 UTF-16 的 FF FE 当成音频。
|
|
228
|
+
if (b.length >= 3 && b[0] === 0xff && b[2] >> 4 !== 0x0f) {
|
|
229
|
+
if ((b[1] & 0xe6) === 0xe2 && (b[1] & 0x18) !== 0x08) return { kind: "audio", mime: "audio/mpeg" };
|
|
230
|
+
if ((b[1] & 0xf6) === 0xf0) return { kind: "audio", mime: "audio/aac" };
|
|
231
|
+
}
|
|
232
|
+
if (size <= TEXT_PREVIEW_LIMIT && looksLikeText(b, b.length >= size))
|
|
233
|
+
return { kind: "text", mime: "text/plain;charset=utf-8" };
|
|
234
|
+
return UNKNOWN;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Blob 自带的 type 能确定预览方式时直接用;空或 application/octet-stream 这类兜底类型返回 null。 */
|
|
238
|
+
function typeFromBlob(blob: Blob): YsSniffedType | null {
|
|
239
|
+
const kind = previewKindOf({ name: "", content_type: blob.type });
|
|
240
|
+
return kind === "none" ? null : { kind, mime: blob.type };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 文件名、content_type 认不出时按内容识别:先看 Blob 自带的 type,再只读开头 SNIFF_HEAD_BYTES 字节看魔数和文本,
|
|
245
|
+
* 不读整个文件。ZIP 开头认不出是哪种 Office 文件时再读末尾 SNIFF_TAIL_BYTES 看中央目录;
|
|
246
|
+
* 旧版 Office(OLE)的目录扇区不在开头时按文件头里的位置读一个扇区。
|
|
247
|
+
*/
|
|
248
|
+
export async function sniffFileType(blob: Blob): Promise<YsSniffedType> {
|
|
249
|
+
const typed = typeFromBlob(blob);
|
|
250
|
+
if (typed) return typed;
|
|
251
|
+
if (blob.size === 0) return UNKNOWN;
|
|
252
|
+
const head = new Uint8Array(await blob.slice(0, SNIFF_HEAD_BYTES).arrayBuffer());
|
|
253
|
+
const result = sniffFileHead(head, blob.size);
|
|
254
|
+
if (result === ZIP_TYPE && blob.size > head.length) {
|
|
255
|
+
const tail = new Uint8Array(await blob.slice(Math.max(head.length, blob.size - SNIFF_TAIL_BYTES)).arrayBuffer());
|
|
256
|
+
return sniffZipEntries(tail) ?? ZIP_TYPE;
|
|
257
|
+
}
|
|
258
|
+
if (result === OLE_TYPE) {
|
|
259
|
+
const offset = oleDirectoryOffset(head);
|
|
260
|
+
if (offset >= head.length && offset < blob.size) {
|
|
261
|
+
const dir = new Uint8Array(await blob.slice(offset, offset + SNIFF_HEAD_BYTES).arrayBuffer());
|
|
262
|
+
return sniffOleDirectory(dir) ?? OLE_TYPE;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** 扩展名或 MIME 认不出时按内容判断预览方式(见 sniffFileType)。 */
|
|
45
269
|
export async function sniffPreviewKind(blob: Blob): Promise<YsPreviewKind> {
|
|
46
|
-
|
|
47
|
-
const text = String.fromCharCode(...head);
|
|
48
|
-
return text.startsWith("%PDF") ? "pdf" : "none";
|
|
270
|
+
return (await sniffFileType(blob)).kind;
|
|
49
271
|
}
|
|
50
272
|
|
|
51
273
|
const TYPE_LABELS: [string[], string][] = [
|
|
52
274
|
[["pdf"], "PDF"],
|
|
53
275
|
[["doc", "docx", "wps", "odt", "rtf"], "Word"],
|
|
54
|
-
[["xls", "xlsx", "et", "ods", "csv"], "Excel"],
|
|
276
|
+
[["xls", "xlsx", "xlsm", "xlsb", "et", "ods", "csv"], "Excel"],
|
|
55
277
|
[["ppt", "pptx", "dps", "odp"], "PPT"],
|
|
56
278
|
[["zip", "rar", "7z", "gz", "tgz", "tar", "bz2", "xz"], "压缩包"],
|
|
57
279
|
[["txt", "md", "markdown", "log"], "文本"],
|
|
58
280
|
];
|
|
59
281
|
|
|
282
|
+
const MIME_LABELS: [(mime: string) => boolean, string][] = [
|
|
283
|
+
[(m) => m === OFFICE_MIME.doc || isWordMime(m), "Word"],
|
|
284
|
+
[isExcelMime, "Excel"],
|
|
285
|
+
[(m) => m === OFFICE_MIME.ppt || m.includes("presentationml"), "PPT"],
|
|
286
|
+
[(m) => m === "application/zip" || m === "application/x-zip-compressed", "压缩包"],
|
|
287
|
+
];
|
|
288
|
+
|
|
60
289
|
/** 文件列表「类型」列的显示文字。 */
|
|
61
290
|
export function fileTypeLabel(name: string, contentType = ""): string {
|
|
62
291
|
const ext = fileExtension(name);
|
|
63
292
|
for (const [exts, label] of TYPE_LABELS) if (exts.includes(ext)) return label;
|
|
293
|
+
const mime = String(contentType || "").toLowerCase().split(";")[0].trim();
|
|
294
|
+
if (!ext) for (const [match, label] of MIME_LABELS) if (match(mime)) return label;
|
|
64
295
|
const kind = previewKindOf({ name, content_type: contentType });
|
|
296
|
+
if (kind === "pdf") return "PDF";
|
|
297
|
+
if (kind === "excel") return "Excel";
|
|
298
|
+
if (kind === "word") return "Word";
|
|
65
299
|
if (kind === "image") return "图片";
|
|
66
300
|
if (kind === "video") return "视频";
|
|
67
301
|
if (kind === "audio") return "音频";
|
package/src/index.ts
CHANGED
|
@@ -23,9 +23,24 @@ export { default as YsBoolMark } from "./components/YsBoolMark.vue";
|
|
|
23
23
|
export { default as YsSegmented } from "./components/YsSegmented.vue";
|
|
24
24
|
export { default as YsCountBadge } from "./components/YsCountBadge.vue";
|
|
25
25
|
export { default as YsFilePreview } from "./components/YsFilePreview.vue";
|
|
26
|
+
// 单独的查看器,业务页可直接内嵌(重型依赖都在打开时才按需加载)。
|
|
27
|
+
export { default as YsPdfViewer } from "./components/YsPdfViewer.vue";
|
|
28
|
+
export { default as YsSheetViewer } from "./components/YsSheetViewer.vue";
|
|
29
|
+
export { default as YsDocViewer } from "./components/YsDocViewer.vue";
|
|
30
|
+
export { default as YsMediaPlayer } from "./components/YsMediaPlayer.vue";
|
|
31
|
+
export { formatMediaTime, MEDIA_SPEEDS } from "./preview/media";
|
|
32
|
+
export type { PdfZoomMode } from "./preview/pdfLayout";
|
|
33
|
+
export { vTip } from "./vTip";
|
|
26
34
|
export {
|
|
27
35
|
previewKindOf,
|
|
28
36
|
sniffPreviewKind,
|
|
37
|
+
sniffFileType,
|
|
38
|
+
sniffFileHead,
|
|
39
|
+
sniffZipEntries,
|
|
40
|
+
sniffOleDirectory,
|
|
41
|
+
SNIFF_HEAD_BYTES,
|
|
42
|
+
SNIFF_TAIL_BYTES,
|
|
43
|
+
OFFICE_MIME,
|
|
29
44
|
fileExtension,
|
|
30
45
|
fileTypeLabel,
|
|
31
46
|
formatFileSize,
|
|
@@ -33,6 +48,7 @@ export {
|
|
|
33
48
|
TEXT_PREVIEW_LIMIT,
|
|
34
49
|
type YsPreviewFile,
|
|
35
50
|
type YsPreviewKind,
|
|
51
|
+
type YsSniffedType,
|
|
36
52
|
} from "./filePreview";
|
|
37
53
|
export { enumOptions, enumLabel, type EnumOption } from "./tokens-runtime";
|
|
38
54
|
export {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* Word 预览(YsDocViewer)的辅助函数:链接安全处理、适合宽度的缩放比例。不依赖 docx-preview,便于单测。 */
|
|
2
|
+
import { clampZoom } from "./pdfLayout";
|
|
3
|
+
|
|
4
|
+
/** 文档里允许保留的链接:http(s)、mailto、页内锚点。 */
|
|
5
|
+
export function isSafeDocLink(href: string): boolean {
|
|
6
|
+
return /^(https?:|mailto:|#)/i.test(String(href ?? "").trim());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** 只保留安全的链接,外部链接在新标签页打开;javascript:、file: 等去掉 href(文字保留)。 */
|
|
10
|
+
export function sanitizeLinks(container: ParentNode): void {
|
|
11
|
+
container.querySelectorAll("a[href]").forEach((link) => {
|
|
12
|
+
const href = link.getAttribute("href") ?? "";
|
|
13
|
+
if (!isSafeDocLink(href)) {
|
|
14
|
+
link.removeAttribute("href");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (href.trim().startsWith("#")) return;
|
|
18
|
+
link.setAttribute("target", "_blank");
|
|
19
|
+
link.setAttribute("rel", "noopener noreferrer");
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 适合宽度:页面比可用宽度宽时缩小,宽屏不放大(最大 100%)。 */
|
|
24
|
+
export function fitDocZoom(pageWidth: number, boxWidth: number, padding = 24): number {
|
|
25
|
+
if (!pageWidth || !boxWidth) return 1;
|
|
26
|
+
return clampZoom(Math.min(1, (boxWidth - padding * 2) / pageWidth));
|
|
27
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* 播放器(YsMediaPlayer)用到的纯函数:时间格式化、快捷键、缓冲进度、倍速档位。 */
|
|
2
|
+
|
|
3
|
+
/** 倍速档位(0.5~2×)。 */
|
|
4
|
+
export const MEDIA_SPEEDS = [0.5, 0.75, 1, 1.25, 1.5, 2] as const;
|
|
5
|
+
/** ← → 一次快退/快进的秒数。 */
|
|
6
|
+
export const MEDIA_SEEK_STEP = 5;
|
|
7
|
+
/** ↑ ↓ 一次调整的音量。 */
|
|
8
|
+
export const MEDIA_VOLUME_STEP = 0.1;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 秒数转「分:秒」,满一小时转「时:分:秒」。reference 传总时长时,按总时长决定是否带小时,
|
|
12
|
+
* 当前时间和总时长位数一致,进度变化时文字不跳动。非法值显示 0:00。
|
|
13
|
+
*/
|
|
14
|
+
export function formatMediaTime(seconds: number, reference = seconds): string {
|
|
15
|
+
const value = Number.isFinite(seconds) && seconds > 0 ? Math.floor(seconds) : 0;
|
|
16
|
+
const withHours = (Number.isFinite(reference) ? reference : 0) >= 3600 || value >= 3600;
|
|
17
|
+
const h = Math.floor(value / 3600);
|
|
18
|
+
const m = Math.floor((value % 3600) / 60);
|
|
19
|
+
const s = value % 60;
|
|
20
|
+
const ss = String(s).padStart(2, "0");
|
|
21
|
+
return withHours ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${m}:${ss}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MediaKeyAction =
|
|
25
|
+
| { type: "toggle" }
|
|
26
|
+
| { type: "seek"; delta: number }
|
|
27
|
+
| { type: "volume"; delta: number }
|
|
28
|
+
| { type: "mute" }
|
|
29
|
+
| { type: "fullscreen" };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 键盘快捷键(播放器获得焦点时):空格/K 播放暂停,← → 快退快进 5 秒,↑ ↓ 音量 ±10%,F 全屏,M 静音。
|
|
33
|
+
* 带 Ctrl/⌘/Alt 的组合键不处理,留给浏览器;音频没有全屏。
|
|
34
|
+
*/
|
|
35
|
+
export function mediaKeyAction(
|
|
36
|
+
event: Pick<KeyboardEvent, "key"> & Partial<Pick<KeyboardEvent, "ctrlKey" | "metaKey" | "altKey">>,
|
|
37
|
+
options: { video?: boolean } = {},
|
|
38
|
+
): MediaKeyAction | null {
|
|
39
|
+
if (event.ctrlKey || event.metaKey || event.altKey) return null;
|
|
40
|
+
switch (event.key) {
|
|
41
|
+
case " ":
|
|
42
|
+
case "Spacebar":
|
|
43
|
+
case "k":
|
|
44
|
+
case "K":
|
|
45
|
+
return { type: "toggle" };
|
|
46
|
+
case "ArrowLeft":
|
|
47
|
+
return { type: "seek", delta: -MEDIA_SEEK_STEP };
|
|
48
|
+
case "ArrowRight":
|
|
49
|
+
return { type: "seek", delta: MEDIA_SEEK_STEP };
|
|
50
|
+
case "ArrowUp":
|
|
51
|
+
return { type: "volume", delta: MEDIA_VOLUME_STEP };
|
|
52
|
+
case "ArrowDown":
|
|
53
|
+
return { type: "volume", delta: -MEDIA_VOLUME_STEP };
|
|
54
|
+
case "m":
|
|
55
|
+
case "M":
|
|
56
|
+
return { type: "mute" };
|
|
57
|
+
case "f":
|
|
58
|
+
case "F":
|
|
59
|
+
return options.video === false ? null : { type: "fullscreen" };
|
|
60
|
+
default:
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 音量限制在 0~1,保留两位小数(避免 0.1 累加出 0.30000000000000004)。 */
|
|
66
|
+
export function clampVolume(value: number): number {
|
|
67
|
+
if (!Number.isFinite(value)) return 1;
|
|
68
|
+
return Math.round(Math.min(1, Math.max(0, value)) * 100) / 100;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 跳转位置限制在 0~时长之间;时长未知时只限制不小于 0。 */
|
|
72
|
+
export function clampTime(value: number, duration: number): number {
|
|
73
|
+
const time = Number.isFinite(value) ? Math.max(0, value) : 0;
|
|
74
|
+
return Number.isFinite(duration) && duration > 0 ? Math.min(duration, time) : time;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Ranges = Pick<TimeRanges, "length" | "start" | "end">;
|
|
78
|
+
|
|
79
|
+
/** 当前播放位置所在的已缓冲区段的末尾(秒);不在任何区段里时返回当前位置。 */
|
|
80
|
+
export function bufferedEnd(ranges: Ranges | null | undefined, time: number): number {
|
|
81
|
+
if (!ranges) return time;
|
|
82
|
+
for (let i = 0; i < ranges.length; i += 1) {
|
|
83
|
+
if (ranges.start(i) <= time + 0.5 && time <= ranges.end(i)) return ranges.end(i);
|
|
84
|
+
}
|
|
85
|
+
return time;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 指针在进度条上的位置换成 0~1 的比例。 */
|
|
89
|
+
export function pointerRatio(clientX: number, rect: Pick<DOMRect, "left" | "width">): number {
|
|
90
|
+
if (!rect.width) return 0;
|
|
91
|
+
return Math.min(1, Math.max(0, (clientX - rect.left) / rect.width));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 倍速显示:1 → 「1×」,1.25 → 「1.25×」。 */
|
|
95
|
+
export function formatSpeed(rate: number): string {
|
|
96
|
+
return `${Number(rate.toFixed(2))}×`;
|
|
97
|
+
}
|