@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
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";
|
|
@@ -73,6 +110,75 @@ function ftypType(brand: string): YsSniffedType {
|
|
|
73
110
|
return { kind: "video", mime: "video/mp4" };
|
|
74
111
|
}
|
|
75
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
|
+
|
|
76
182
|
function isBinaryControl(byte: number): boolean {
|
|
77
183
|
// NUL 和除 \t \n \v \f \r ESC 以外的控制字符基本只出现在二进制文件里。
|
|
78
184
|
return byte < 0x20 && byte !== 0x09 && byte !== 0x0a && byte !== 0x0b && byte !== 0x0c && byte !== 0x0d && byte !== 0x1b;
|
|
@@ -97,6 +203,11 @@ function looksLikeText(bytes: Uint8Array, complete: boolean): boolean {
|
|
|
97
203
|
export function sniffFileHead(head: Uint8Array, size = head.length): YsSniffedType {
|
|
98
204
|
const b = head;
|
|
99
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
|
+
}
|
|
100
211
|
if (ascii(b, 0, "\x89PNG\r\n\x1a\n")) return { kind: "image", mime: "image/png" };
|
|
101
212
|
if (b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return { kind: "image", mime: "image/jpeg" };
|
|
102
213
|
if (ascii(b, 0, "GIF87a") || ascii(b, 0, "GIF89a")) return { kind: "image", mime: "image/gif" };
|
|
@@ -131,14 +242,27 @@ function typeFromBlob(blob: Blob): YsSniffedType | null {
|
|
|
131
242
|
|
|
132
243
|
/**
|
|
133
244
|
* 文件名、content_type 认不出时按内容识别:先看 Blob 自带的 type,再只读开头 SNIFF_HEAD_BYTES 字节看魔数和文本,
|
|
134
|
-
* 不读整个文件。
|
|
245
|
+
* 不读整个文件。ZIP 开头认不出是哪种 Office 文件时再读末尾 SNIFF_TAIL_BYTES 看中央目录;
|
|
246
|
+
* 旧版 Office(OLE)的目录扇区不在开头时按文件头里的位置读一个扇区。
|
|
135
247
|
*/
|
|
136
248
|
export async function sniffFileType(blob: Blob): Promise<YsSniffedType> {
|
|
137
249
|
const typed = typeFromBlob(blob);
|
|
138
250
|
if (typed) return typed;
|
|
139
251
|
if (blob.size === 0) return UNKNOWN;
|
|
140
252
|
const head = new Uint8Array(await blob.slice(0, SNIFF_HEAD_BYTES).arrayBuffer());
|
|
141
|
-
|
|
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;
|
|
142
266
|
}
|
|
143
267
|
|
|
144
268
|
/** 扩展名或 MIME 认不出时按内容判断预览方式(见 sniffFileType)。 */
|
|
@@ -149,18 +273,29 @@ export async function sniffPreviewKind(blob: Blob): Promise<YsPreviewKind> {
|
|
|
149
273
|
const TYPE_LABELS: [string[], string][] = [
|
|
150
274
|
[["pdf"], "PDF"],
|
|
151
275
|
[["doc", "docx", "wps", "odt", "rtf"], "Word"],
|
|
152
|
-
[["xls", "xlsx", "et", "ods", "csv"], "Excel"],
|
|
276
|
+
[["xls", "xlsx", "xlsm", "xlsb", "et", "ods", "csv"], "Excel"],
|
|
153
277
|
[["ppt", "pptx", "dps", "odp"], "PPT"],
|
|
154
278
|
[["zip", "rar", "7z", "gz", "tgz", "tar", "bz2", "xz"], "压缩包"],
|
|
155
279
|
[["txt", "md", "markdown", "log"], "文本"],
|
|
156
280
|
];
|
|
157
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
|
+
|
|
158
289
|
/** 文件列表「类型」列的显示文字。 */
|
|
159
290
|
export function fileTypeLabel(name: string, contentType = ""): string {
|
|
160
291
|
const ext = fileExtension(name);
|
|
161
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;
|
|
162
295
|
const kind = previewKindOf({ name, content_type: contentType });
|
|
163
296
|
if (kind === "pdf") return "PDF";
|
|
297
|
+
if (kind === "excel") return "Excel";
|
|
298
|
+
if (kind === "word") return "Word";
|
|
164
299
|
if (kind === "image") return "图片";
|
|
165
300
|
if (kind === "video") return "视频";
|
|
166
301
|
if (kind === "audio") return "音频";
|
package/src/index.ts
CHANGED
|
@@ -23,12 +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 { isTextOverflowing, vTip } from "./vTip";
|
|
26
34
|
export {
|
|
27
35
|
previewKindOf,
|
|
28
36
|
sniffPreviewKind,
|
|
29
37
|
sniffFileType,
|
|
30
38
|
sniffFileHead,
|
|
39
|
+
sniffZipEntries,
|
|
40
|
+
sniffOleDirectory,
|
|
31
41
|
SNIFF_HEAD_BYTES,
|
|
42
|
+
SNIFF_TAIL_BYTES,
|
|
43
|
+
OFFICE_MIME,
|
|
32
44
|
fileExtension,
|
|
33
45
|
fileTypeLabel,
|
|
34
46
|
formatFileSize,
|
|
@@ -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
|
+
}
|