vvvfs 0.2.1 → 0.2.3
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/Img.ts +46 -0
- package/Media.ts +773 -0
- package/README.md +114 -0
- package/dist/vvvfs.min.js +34 -3
- package/index.html +1 -0
- package/index.ts +4 -0
- package/package.json +1 -1
- package/types/index.d.ts +177 -0
- package//345/205/261/345/210/233/350/276/211/347/205/214/357/274/210/351/224/241/350/201/214/345/244/247/344/271/213/346/255/214/357/274/211.flac +0 -0
package/Media.ts
ADDED
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Media.ts
|
|
3
|
+
* @description 定义自定义 HTML 元素 <fs-audio> 与 <fs-video>,可直接播放存储在 vvvfs 中的音频 / 视频文件。
|
|
4
|
+
* 同时导出 <fs-img> / <fs-audio> / <fs-video> 共用的抽象基类 FsMedia。
|
|
5
|
+
* @author IFTC
|
|
6
|
+
* @example
|
|
7
|
+
* <fs-audio src="/home/user/Music/demo.mp3"></fs-audio>
|
|
8
|
+
* <fs-video src="/home/user/Videos/demo.mp4" width="320"></fs-video>
|
|
9
|
+
*/
|
|
10
|
+
import type { VVVFS, VVVFSOptions } from "./index";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 监听的属性列表
|
|
14
|
+
*/
|
|
15
|
+
const OBSERVED_ATTRIBUTES = [
|
|
16
|
+
"src",
|
|
17
|
+
"path",
|
|
18
|
+
"dbname",
|
|
19
|
+
"watch",
|
|
20
|
+
"controls",
|
|
21
|
+
"autoplay",
|
|
22
|
+
"loop",
|
|
23
|
+
"muted",
|
|
24
|
+
"preload",
|
|
25
|
+
"width",
|
|
26
|
+
"height",
|
|
27
|
+
"alt",
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 默认数据库名
|
|
32
|
+
*/
|
|
33
|
+
const DEFAULT_DB_NAME = "vvvfs";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 会改变文件内容的操作类型(其余操作不会触发刷新)
|
|
37
|
+
*/
|
|
38
|
+
const WATCH_MUTATIONS = new Set([
|
|
39
|
+
"create",
|
|
40
|
+
"write",
|
|
41
|
+
"append",
|
|
42
|
+
"rename",
|
|
43
|
+
"delete",
|
|
44
|
+
"move",
|
|
45
|
+
"copy",
|
|
46
|
+
"downloadFile",
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 刷新重试间隔(毫秒)
|
|
51
|
+
* @description vvvfs 的 watch 在操作提交前触发,立即刷新会读到旧内容,
|
|
52
|
+
* 因此延迟一段时间后重试,直到读到的文件发生变化。
|
|
53
|
+
*/
|
|
54
|
+
const WATCH_RETRY_DELAY = 50;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 刷新最大重试次数
|
|
58
|
+
*/
|
|
59
|
+
const WATCH_MAX_ATTEMPTS = 10;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 各元素共用的 Shadow DOM 基础样式
|
|
63
|
+
*/
|
|
64
|
+
const SHADOW_BASE_STYLE = `
|
|
65
|
+
:host {
|
|
66
|
+
display: inline-block;
|
|
67
|
+
}
|
|
68
|
+
[hidden] {
|
|
69
|
+
display: none;
|
|
70
|
+
}
|
|
71
|
+
slot {
|
|
72
|
+
display: none;
|
|
73
|
+
}
|
|
74
|
+
:host([data-state="error"]) slot,
|
|
75
|
+
:host([data-state="idle"]) slot {
|
|
76
|
+
display: block;
|
|
77
|
+
}
|
|
78
|
+
`;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* VVVFS 构造函数类型
|
|
82
|
+
*/
|
|
83
|
+
type VVVFSConstructor = new (name?: string, options?: VVVFSOptions) => VVVFS;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 媒体元素类型(图片 / 音频 / 视频)
|
|
87
|
+
*/
|
|
88
|
+
type MediaElement = HTMLImageElement | HTMLMediaElement;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 全局作用域(VVVFS 库会向 globalThis 挂载 VVVFS / vvvfs)
|
|
92
|
+
*/
|
|
93
|
+
const globalScope = globalThis as typeof globalThis & {
|
|
94
|
+
VVVFS?: VVVFSConstructor;
|
|
95
|
+
vvvfs?: VVVFS;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 已创建的 VVVFS 实例缓存(按数据库名共享,避免重复创建)
|
|
100
|
+
*/
|
|
101
|
+
const instanceCache = new Map<string, VVVFS>();
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 每个 (数据库名, 路径) 对应的元素集合,用于共享同一个 watch 监听器
|
|
105
|
+
*/
|
|
106
|
+
const watcherSets = new Map<string, Set<FsMedia>>();
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 生成监听键
|
|
110
|
+
* @param dbname 数据库名
|
|
111
|
+
* @param path 文件路径
|
|
112
|
+
*/
|
|
113
|
+
function watchKey(dbname: string, path: string): string {
|
|
114
|
+
return dbname + "\n" + path;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 注册共享监听器
|
|
119
|
+
* @description VVVFS 未提供取消监听的 API,因此同一个 (数据库名, 路径) 只注册一次,
|
|
120
|
+
* 集合清空后监听器闭包仍保留在 VVVFS 内部(每个路径仅一个闭包,开销极小)。
|
|
121
|
+
* @param el 目标元素
|
|
122
|
+
* @param fs VVVFS 实例
|
|
123
|
+
* @param dbname 数据库名
|
|
124
|
+
* @param path 文件路径
|
|
125
|
+
*/
|
|
126
|
+
function registerWatcher(el: FsMedia, fs: VVVFS, dbname: string, path: string): void {
|
|
127
|
+
const key = watchKey(dbname, path);
|
|
128
|
+
let set = watcherSets.get(key);
|
|
129
|
+
if (!set) {
|
|
130
|
+
set = new Set<FsMedia>();
|
|
131
|
+
watcherSets.set(key, set);
|
|
132
|
+
fs.watch(path, (type) => {
|
|
133
|
+
// 只有修改类操作才需要刷新
|
|
134
|
+
if (!WATCH_MUTATIONS.has(type)) return false;
|
|
135
|
+
const current = watcherSets.get(key);
|
|
136
|
+
if (!current || current.size === 0) return false;
|
|
137
|
+
for (const target of current) {
|
|
138
|
+
if (target.isConnected) target.scheduleWatchRefresh();
|
|
139
|
+
}
|
|
140
|
+
// 返回 false,避免取消触发监听的文件操作
|
|
141
|
+
return false;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
set.add(el);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 将元素从共享监听集合中移除
|
|
149
|
+
* @param el 目标元素
|
|
150
|
+
* @param dbname 数据库名
|
|
151
|
+
* @param path 文件路径
|
|
152
|
+
*/
|
|
153
|
+
function unregisterWatcher(el: FsMedia, dbname: string, path: string): void {
|
|
154
|
+
const key = watchKey(dbname, path);
|
|
155
|
+
const set = watcherSets.get(key);
|
|
156
|
+
if (!set) return;
|
|
157
|
+
set.delete(el);
|
|
158
|
+
if (set.size === 0) watcherSets.delete(key);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* load 事件详情
|
|
163
|
+
*/
|
|
164
|
+
export interface FsMediaLoadDetail {
|
|
165
|
+
/** 媒体文件路径 */
|
|
166
|
+
path: string;
|
|
167
|
+
/** 媒体文件对象 */
|
|
168
|
+
file: File | null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* error 事件详情
|
|
173
|
+
*/
|
|
174
|
+
export interface FsMediaErrorDetail {
|
|
175
|
+
/** 媒体文件路径 */
|
|
176
|
+
path: string;
|
|
177
|
+
/** 错误信息 */
|
|
178
|
+
message: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 媒体元素基类
|
|
183
|
+
* @description <fs-img> / <fs-audio> / <fs-video> 共用,封装文件读取、ObjectURL、
|
|
184
|
+
* watch 自动刷新、Shadow DOM 渲染与 load / error 事件。
|
|
185
|
+
*/
|
|
186
|
+
export abstract class FsMedia extends HTMLElement {
|
|
187
|
+
/**
|
|
188
|
+
* 监听的属性列表
|
|
189
|
+
*/
|
|
190
|
+
static get observedAttributes(): string[] {
|
|
191
|
+
return OBSERVED_ATTRIBUTES;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 内部媒体标签名(img / audio / video)
|
|
196
|
+
*/
|
|
197
|
+
protected abstract readonly mediaTag: string;
|
|
198
|
+
/**
|
|
199
|
+
* 暴露给 ::part() 的名称
|
|
200
|
+
*/
|
|
201
|
+
protected abstract readonly partName: string;
|
|
202
|
+
/**
|
|
203
|
+
* 媒体类型名称(用于错误提示)
|
|
204
|
+
*/
|
|
205
|
+
protected abstract readonly mediaLabel: string;
|
|
206
|
+
/**
|
|
207
|
+
* 接受的 MIME 前缀(image/ 、audio/ 、video/)
|
|
208
|
+
*/
|
|
209
|
+
protected abstract readonly acceptPrefix: string;
|
|
210
|
+
/**
|
|
211
|
+
* 媒体加载完成对应的事件名(img 为 load,音视频为 loadeddata)
|
|
212
|
+
*/
|
|
213
|
+
protected abstract readonly loadEventName: string;
|
|
214
|
+
/**
|
|
215
|
+
* Shadow DOM 附加样式
|
|
216
|
+
*/
|
|
217
|
+
protected abstract readonly shadowCss: string;
|
|
218
|
+
/**
|
|
219
|
+
* 需要从宿主同步到媒体元素的属性
|
|
220
|
+
*/
|
|
221
|
+
protected abstract readonly mediaAttrs: readonly string[];
|
|
222
|
+
/**
|
|
223
|
+
* 默认开启的属性(仅在初始化时应用,如 controls)
|
|
224
|
+
*/
|
|
225
|
+
protected readonly defaultAttrs: readonly string[] = [];
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 是否已完成首次属性同步(之后严格遵循宿主属性)
|
|
229
|
+
*/
|
|
230
|
+
#attrsInitialized = false;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Shadow Root
|
|
234
|
+
*/
|
|
235
|
+
#shadow: ShadowRoot | null = null;
|
|
236
|
+
/**
|
|
237
|
+
* 默认插槽(加载失败 / 无路径时的回退内容)
|
|
238
|
+
*/
|
|
239
|
+
#slot: HTMLSlotElement | null = null;
|
|
240
|
+
/**
|
|
241
|
+
* 内部媒体元素
|
|
242
|
+
*/
|
|
243
|
+
#media: MediaElement | null = null;
|
|
244
|
+
/**
|
|
245
|
+
* 当前媒体的 ObjectURL
|
|
246
|
+
*/
|
|
247
|
+
#objectURL: string | null = null;
|
|
248
|
+
/**
|
|
249
|
+
* 当前使用的 VVVFS 实例
|
|
250
|
+
*/
|
|
251
|
+
#fs: VVVFS | null = null;
|
|
252
|
+
/**
|
|
253
|
+
* 当前展示的文件对象
|
|
254
|
+
*/
|
|
255
|
+
#currentFile: File | null = null;
|
|
256
|
+
/**
|
|
257
|
+
* 加载令牌(用于丢弃过期的异步结果)
|
|
258
|
+
*/
|
|
259
|
+
#token = 0;
|
|
260
|
+
/**
|
|
261
|
+
* 是否正在执行由自身触发的读取(避免 watch 递归)
|
|
262
|
+
*/
|
|
263
|
+
#skipWatch = false;
|
|
264
|
+
/**
|
|
265
|
+
* watch 刷新重试定时器
|
|
266
|
+
*/
|
|
267
|
+
#watchTimer: number | null = null;
|
|
268
|
+
/**
|
|
269
|
+
* 当前注册的监听信息
|
|
270
|
+
*/
|
|
271
|
+
#watchDbname: string | null = null;
|
|
272
|
+
#watchPath: string | null = null;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 媒体路径(src 优先,其次 path)
|
|
276
|
+
*/
|
|
277
|
+
get src(): string {
|
|
278
|
+
return this.getAttribute("src") ?? this.getAttribute("path") ?? "";
|
|
279
|
+
}
|
|
280
|
+
set src(value: string) {
|
|
281
|
+
this.setAttribute("src", value);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 数据库名
|
|
286
|
+
*/
|
|
287
|
+
get dbname(): string {
|
|
288
|
+
return this.getAttribute("dbname") || DEFAULT_DB_NAME;
|
|
289
|
+
}
|
|
290
|
+
set dbname(value: string) {
|
|
291
|
+
this.setAttribute("dbname", value);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 是否监听文件变化
|
|
296
|
+
* @description watch="true" 时启用监听,watch="false" 时停止监听;
|
|
297
|
+
* 属性存在且值不为 "false" 时均视为启用(兼容 `<fs-img watch>` 的写法)
|
|
298
|
+
*/
|
|
299
|
+
get watch(): boolean {
|
|
300
|
+
const value = this.getAttribute("watch");
|
|
301
|
+
if (value === null) return false;
|
|
302
|
+
return value.toLowerCase() !== "false";
|
|
303
|
+
}
|
|
304
|
+
set watch(value: boolean) {
|
|
305
|
+
this.setAttribute("watch", value ? "true" : "false");
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 当前显示的媒体文件对象(未加载或加载失败时为 null)
|
|
310
|
+
*/
|
|
311
|
+
get data(): Blob | null {
|
|
312
|
+
return this.#currentFile;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 元素挂载时
|
|
317
|
+
*/
|
|
318
|
+
connectedCallback(): void {
|
|
319
|
+
this.#ensureShadow();
|
|
320
|
+
this.#ensureMedia();
|
|
321
|
+
this.#syncAttrs();
|
|
322
|
+
this.#syncWatch();
|
|
323
|
+
void this.refresh();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 元素卸载时
|
|
328
|
+
*/
|
|
329
|
+
disconnectedCallback(): void {
|
|
330
|
+
// 使所有进行中的异步加载失效
|
|
331
|
+
this.#token++;
|
|
332
|
+
this.#clearWatchTimer();
|
|
333
|
+
this.#unregisterWatch();
|
|
334
|
+
this.#revokeObjectURL();
|
|
335
|
+
this.#setState("idle");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 属性变化时
|
|
340
|
+
* @param name 属性名
|
|
341
|
+
*/
|
|
342
|
+
attributeChangedCallback(name: string): void {
|
|
343
|
+
if (!this.isConnected) return;
|
|
344
|
+
if (this.mediaAttrs.includes(name)) {
|
|
345
|
+
this.#syncAttrs();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (name === "watch") {
|
|
349
|
+
this.#syncWatch();
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (name === "dbname") {
|
|
353
|
+
this.#fs = null;
|
|
354
|
+
}
|
|
355
|
+
this.#syncWatch();
|
|
356
|
+
void this.refresh();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* 手动刷新:重新读取并显示媒体
|
|
361
|
+
* @returns 是否成功显示
|
|
362
|
+
*/
|
|
363
|
+
refresh(): Promise<boolean> {
|
|
364
|
+
// 自身读取触发的 watch 不递归刷新
|
|
365
|
+
if (this.#skipWatch) return Promise.resolve(false);
|
|
366
|
+
return this.#reload();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 调度一次 watch 触发的刷新(内部使用)
|
|
371
|
+
* @description 延迟重试,直到读到的文件内容发生变化,
|
|
372
|
+
* 以避开 vvvfs watch 在操作提交前触发导致的旧内容问题。
|
|
373
|
+
*/
|
|
374
|
+
scheduleWatchRefresh(): void {
|
|
375
|
+
if (this.#watchTimer !== null || !this.isConnected || !this.watch) return;
|
|
376
|
+
let attempts = 0;
|
|
377
|
+
const tick = async () => {
|
|
378
|
+
this.#watchTimer = null;
|
|
379
|
+
if (!this.isConnected || !this.watch) return;
|
|
380
|
+
const changed = await this.#refreshForWatch();
|
|
381
|
+
attempts++;
|
|
382
|
+
if (!changed && attempts < WATCH_MAX_ATTEMPTS) {
|
|
383
|
+
this.#watchTimer = window.setTimeout(tick, WATCH_RETRY_DELAY);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
this.#watchTimer = window.setTimeout(tick, WATCH_RETRY_DELAY);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 执行加载
|
|
391
|
+
*/
|
|
392
|
+
async #reload(): Promise<boolean> {
|
|
393
|
+
const token = ++this.#token;
|
|
394
|
+
const src = this.src;
|
|
395
|
+
this.#setState("loading");
|
|
396
|
+
|
|
397
|
+
if (!src) {
|
|
398
|
+
this.#revokeObjectURL();
|
|
399
|
+
this.#clearMedia();
|
|
400
|
+
this.#setState("idle");
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const fs = this.#resolveFS();
|
|
405
|
+
if (!fs) return false;
|
|
406
|
+
this.#syncWatch(fs);
|
|
407
|
+
|
|
408
|
+
let file: File | null = null;
|
|
409
|
+
this.#skipWatch = true;
|
|
410
|
+
try {
|
|
411
|
+
file = await fs.read(src);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
if (token === this.#token && this.isConnected) {
|
|
414
|
+
this.#fail(error instanceof Error ? error.message : String(error));
|
|
415
|
+
}
|
|
416
|
+
return false;
|
|
417
|
+
} finally {
|
|
418
|
+
this.#skipWatch = false;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (token !== this.#token || !this.isConnected) return false;
|
|
422
|
+
|
|
423
|
+
if (!file) {
|
|
424
|
+
this.#fail(`文件不存在: ${src}`);
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return this.#display(file);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* watch 触发的刷新:仅当文件内容变化时才重新展示
|
|
433
|
+
* @returns 内容是否发生变化(或无需比较)
|
|
434
|
+
*/
|
|
435
|
+
async #refreshForWatch(): Promise<boolean> {
|
|
436
|
+
const src = this.src;
|
|
437
|
+
if (!src) return true;
|
|
438
|
+
const fs = this.#fs ?? this.#resolveFS();
|
|
439
|
+
if (!fs) return true;
|
|
440
|
+
|
|
441
|
+
let file: File | null = null;
|
|
442
|
+
this.#skipWatch = true;
|
|
443
|
+
try {
|
|
444
|
+
file = await fs.read(src);
|
|
445
|
+
} catch {
|
|
446
|
+
return true;
|
|
447
|
+
} finally {
|
|
448
|
+
this.#skipWatch = false;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (!this.isConnected) return true;
|
|
452
|
+
if (this.#isSameFile(file, this.#currentFile)) return false;
|
|
453
|
+
|
|
454
|
+
if (!file) {
|
|
455
|
+
this.#fail(`文件不存在: ${src}`);
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
// 无论是否为对应类型的媒体,内容已变化,处理完毕即停止重试
|
|
459
|
+
this.#display(file);
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* 判断两个文件对象内容是否一致
|
|
465
|
+
* @param a 文件 A
|
|
466
|
+
* @param b 文件 B
|
|
467
|
+
*/
|
|
468
|
+
#isSameFile(a: File | null, b: File | null): boolean {
|
|
469
|
+
if (!a || !b) return a === b;
|
|
470
|
+
return (
|
|
471
|
+
a.size === b.size &&
|
|
472
|
+
a.type === b.type &&
|
|
473
|
+
a.name === b.name &&
|
|
474
|
+
a.lastModified === b.lastModified
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* 解析要使用的 VVVFS 实例
|
|
480
|
+
*/
|
|
481
|
+
#resolveFS(): VVVFS | null {
|
|
482
|
+
const dbname = this.dbname;
|
|
483
|
+
// 默认数据库名时优先复用全局 vvvfs 实例
|
|
484
|
+
if (dbname === DEFAULT_DB_NAME && globalScope.vvvfs) {
|
|
485
|
+
this.#fs = globalScope.vvvfs;
|
|
486
|
+
return this.#fs;
|
|
487
|
+
}
|
|
488
|
+
if (!instanceCache.has(dbname)) {
|
|
489
|
+
if (!globalScope.VVVFS) {
|
|
490
|
+
this.#fail("VVVFS 尚未加载,无法创建文件系统实例");
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
try {
|
|
494
|
+
instanceCache.set(dbname, new globalScope.VVVFS(dbname));
|
|
495
|
+
} catch (error) {
|
|
496
|
+
this.#fail(
|
|
497
|
+
"创建 VVVFS 实例失败: " +
|
|
498
|
+
(error instanceof Error ? error.message : String(error)),
|
|
499
|
+
);
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
this.#fs = instanceCache.get(dbname)!;
|
|
504
|
+
return this.#fs;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* 展示文件
|
|
509
|
+
* @param file 媒体文件
|
|
510
|
+
* @returns 是否成功展示
|
|
511
|
+
*/
|
|
512
|
+
#display(file: File): boolean {
|
|
513
|
+
if (file.type && !file.type.startsWith(this.acceptPrefix)) {
|
|
514
|
+
this.#fail(`文件不是${this.mediaLabel}: ${this.src}(${file.type})`);
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
const media = this.#ensureMedia();
|
|
518
|
+
media.removeAttribute("hidden");
|
|
519
|
+
this.#currentFile = file;
|
|
520
|
+
const url = URL.createObjectURL(file);
|
|
521
|
+
this.#revokeObjectURL();
|
|
522
|
+
this.#objectURL = url;
|
|
523
|
+
media.src = url;
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* 加载失败处理
|
|
529
|
+
* @param message 错误信息
|
|
530
|
+
*/
|
|
531
|
+
#fail(message: string): void {
|
|
532
|
+
this.#revokeObjectURL();
|
|
533
|
+
this.#clearMedia();
|
|
534
|
+
this.#setState("error");
|
|
535
|
+
this.dispatchEvent(
|
|
536
|
+
new CustomEvent<FsMediaErrorDetail>("error", {
|
|
537
|
+
detail: { path: this.src, message },
|
|
538
|
+
}),
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* 确保 Shadow Root 已创建
|
|
544
|
+
*/
|
|
545
|
+
#ensureShadow(): ShadowRoot {
|
|
546
|
+
if (!this.#shadow) {
|
|
547
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
548
|
+
const style = document.createElement("style");
|
|
549
|
+
style.textContent = SHADOW_BASE_STYLE + this.shadowCss;
|
|
550
|
+
this.#shadow.appendChild(style);
|
|
551
|
+
this.#slot = document.createElement("slot");
|
|
552
|
+
this.#shadow.appendChild(this.#slot);
|
|
553
|
+
}
|
|
554
|
+
return this.#shadow;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* 获取(或创建)内部媒体元素
|
|
559
|
+
*/
|
|
560
|
+
#ensureMedia(): MediaElement {
|
|
561
|
+
const shadow = this.#ensureShadow();
|
|
562
|
+
if (!this.#media) {
|
|
563
|
+
const media = document.createElement(this.mediaTag) as MediaElement;
|
|
564
|
+
// 暴露为 part,便于外部通过 ::part() 定制样式
|
|
565
|
+
media.setAttribute("part", this.partName);
|
|
566
|
+
// 无内容时先隐藏
|
|
567
|
+
media.toggleAttribute("hidden", true);
|
|
568
|
+
media.addEventListener(this.loadEventName, () => {
|
|
569
|
+
if (!this.isConnected) return;
|
|
570
|
+
this.#setState("loaded");
|
|
571
|
+
this.dispatchEvent(
|
|
572
|
+
new CustomEvent<FsMediaLoadDetail>("load", {
|
|
573
|
+
detail: { path: this.src, file: this.#currentFile },
|
|
574
|
+
}),
|
|
575
|
+
);
|
|
576
|
+
});
|
|
577
|
+
media.addEventListener("error", () => {
|
|
578
|
+
if (!this.isConnected) return;
|
|
579
|
+
this.#revokeObjectURL();
|
|
580
|
+
this.#setState("error");
|
|
581
|
+
this.dispatchEvent(
|
|
582
|
+
new CustomEvent<FsMediaErrorDetail>("error", {
|
|
583
|
+
detail: { path: this.src, message: "媒体加载失败" },
|
|
584
|
+
}),
|
|
585
|
+
);
|
|
586
|
+
});
|
|
587
|
+
this.#media = media;
|
|
588
|
+
}
|
|
589
|
+
if (this.#media.parentNode === null) {
|
|
590
|
+
shadow.insertBefore(this.#media, this.#slot);
|
|
591
|
+
}
|
|
592
|
+
return this.#media;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* 同步 mediaAttrs / defaultAttrs 到内部媒体元素
|
|
597
|
+
*/
|
|
598
|
+
#syncAttrs(): void {
|
|
599
|
+
const media = this.#media;
|
|
600
|
+
if (!media) return;
|
|
601
|
+
for (const name of this.mediaAttrs) {
|
|
602
|
+
const value = this.getAttribute(name);
|
|
603
|
+
if (value !== null) media.setAttribute(name, value);
|
|
604
|
+
else if (!this.#attrsInitialized && this.defaultAttrs.includes(name)) {
|
|
605
|
+
media.setAttribute(name, "");
|
|
606
|
+
} else media.removeAttribute(name);
|
|
607
|
+
}
|
|
608
|
+
this.#attrsInitialized = true;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* 同步 watch 监听(路径 / 数据库名 / watch 属性变化时调用)
|
|
613
|
+
* @param fs 已解析的 VVVFS 实例(可选)
|
|
614
|
+
*/
|
|
615
|
+
#syncWatch(fs?: VVVFS | null): void {
|
|
616
|
+
const dbname = this.dbname;
|
|
617
|
+
const path = this.src;
|
|
618
|
+
const watching = this.watch && path.length > 0;
|
|
619
|
+
|
|
620
|
+
// 目标未变化时无需处理
|
|
621
|
+
if (
|
|
622
|
+
watching &&
|
|
623
|
+
this.#watchPath === path &&
|
|
624
|
+
this.#watchDbname === dbname
|
|
625
|
+
) {
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// 先注销旧监听
|
|
630
|
+
if (this.#watchPath !== null && this.#watchDbname !== null) {
|
|
631
|
+
unregisterWatcher(this, this.#watchDbname, this.#watchPath);
|
|
632
|
+
this.#watchDbname = null;
|
|
633
|
+
this.#watchPath = null;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
if (watching) {
|
|
637
|
+
const target = fs ?? this.#fs;
|
|
638
|
+
if (target) {
|
|
639
|
+
registerWatcher(this, target, dbname, path);
|
|
640
|
+
this.#watchDbname = dbname;
|
|
641
|
+
this.#watchPath = path;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* 注销 watch 监听
|
|
648
|
+
*/
|
|
649
|
+
#unregisterWatch(): void {
|
|
650
|
+
if (this.#watchPath !== null && this.#watchDbname !== null) {
|
|
651
|
+
unregisterWatcher(this, this.#watchDbname, this.#watchPath);
|
|
652
|
+
}
|
|
653
|
+
this.#watchDbname = null;
|
|
654
|
+
this.#watchPath = null;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* 清除 watch 刷新重试定时器
|
|
659
|
+
*/
|
|
660
|
+
#clearWatchTimer(): void {
|
|
661
|
+
if (this.#watchTimer !== null) {
|
|
662
|
+
window.clearTimeout(this.#watchTimer);
|
|
663
|
+
this.#watchTimer = null;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* 清空内部媒体元素的 src
|
|
669
|
+
*/
|
|
670
|
+
#clearMedia(): void {
|
|
671
|
+
this.#currentFile = null;
|
|
672
|
+
if (this.#media) {
|
|
673
|
+
this.#media.removeAttribute("src");
|
|
674
|
+
this.#media.toggleAttribute("hidden", true);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* 释放 ObjectURL
|
|
680
|
+
*/
|
|
681
|
+
#revokeObjectURL(): void {
|
|
682
|
+
if (this.#objectURL) {
|
|
683
|
+
URL.revokeObjectURL(this.#objectURL);
|
|
684
|
+
this.#objectURL = null;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* 设置状态(写入 data-state 属性,便于 CSS 定制)
|
|
690
|
+
* @param state 状态
|
|
691
|
+
*/
|
|
692
|
+
#setState(state: "idle" | "loading" | "loaded" | "error"): void {
|
|
693
|
+
if (state === "idle") this.removeAttribute("data-state");
|
|
694
|
+
else this.setAttribute("data-state", state);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* 自定义音频元素 <fs-audio>
|
|
700
|
+
* @description 直接读取并播放存储在 vvvfs 中的音频文件
|
|
701
|
+
* @attributes
|
|
702
|
+
* - src / path:音频在 vvvfs 中的路径(src 优先,其次 path)
|
|
703
|
+
* - dbname:数据库名,默认 "vvvfs";未指定且存在全局 vvvfs 实例时直接使用该实例
|
|
704
|
+
* - controls:是否显示控制条(默认显示,可用 removeAttribute("controls") 关闭)
|
|
705
|
+
* - autoplay / loop / muted / preload:透传到内部 <audio> 元素
|
|
706
|
+
* - watch:存在时,文件内容发生变化后自动刷新
|
|
707
|
+
* @events
|
|
708
|
+
* - load:音频数据加载完成(detail: { path, file })
|
|
709
|
+
* - error:音频加载失败(detail: { path, message })
|
|
710
|
+
* @example <fs-audio src="/home/user/Music/demo.mp3"></fs-audio>
|
|
711
|
+
*/
|
|
712
|
+
export class FsAudio extends FsMedia {
|
|
713
|
+
protected readonly mediaTag = "audio";
|
|
714
|
+
protected readonly partName = "audio";
|
|
715
|
+
protected readonly mediaLabel = "音频";
|
|
716
|
+
protected readonly acceptPrefix = "audio/";
|
|
717
|
+
protected readonly loadEventName = "loadeddata";
|
|
718
|
+
protected readonly mediaAttrs = ["controls", "autoplay", "loop", "muted", "preload"] as const;
|
|
719
|
+
protected readonly defaultAttrs = ["controls"] as const;
|
|
720
|
+
protected readonly shadowCss = `
|
|
721
|
+
audio {
|
|
722
|
+
display: block;
|
|
723
|
+
max-width: 100%;
|
|
724
|
+
}
|
|
725
|
+
`;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* 自定义视频元素 <fs-video>
|
|
730
|
+
* @description 直接读取并播放存储在 vvvfs 中的视频文件
|
|
731
|
+
* @attributes
|
|
732
|
+
* - src / path:视频在 vvvfs 中的路径(src 优先,其次 path)
|
|
733
|
+
* - dbname:数据库名,默认 "vvvfs";未指定且存在全局 vvvfs 实例时直接使用该实例
|
|
734
|
+
* - controls:是否显示控制条(默认显示,可用 removeAttribute("controls") 关闭)
|
|
735
|
+
* - autoplay / loop / muted / preload / width / height:透传到内部 <video> 元素
|
|
736
|
+
* - watch:存在时,文件内容发生变化后自动刷新
|
|
737
|
+
* @events
|
|
738
|
+
* - load:视频数据加载完成(detail: { path, file })
|
|
739
|
+
* - error:视频加载失败(detail: { path, message })
|
|
740
|
+
* @example <fs-video src="/home/user/Videos/demo.mp4" width="320"></fs-video>
|
|
741
|
+
*/
|
|
742
|
+
export class FsVideo extends FsMedia {
|
|
743
|
+
protected readonly mediaTag = "video";
|
|
744
|
+
protected readonly partName = "video";
|
|
745
|
+
protected readonly mediaLabel = "视频";
|
|
746
|
+
protected readonly acceptPrefix = "video/";
|
|
747
|
+
protected readonly loadEventName = "loadeddata";
|
|
748
|
+
protected readonly mediaAttrs = [
|
|
749
|
+
"controls",
|
|
750
|
+
"autoplay",
|
|
751
|
+
"loop",
|
|
752
|
+
"muted",
|
|
753
|
+
"preload",
|
|
754
|
+
"width",
|
|
755
|
+
"height",
|
|
756
|
+
] as const;
|
|
757
|
+
protected readonly defaultAttrs = ["controls"] as const;
|
|
758
|
+
protected readonly shadowCss = `
|
|
759
|
+
video {
|
|
760
|
+
display: block;
|
|
761
|
+
max-width: 100%;
|
|
762
|
+
height: auto;
|
|
763
|
+
}
|
|
764
|
+
`;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// 注册自定义元素(避免重复注册导致报错)
|
|
768
|
+
if (typeof customElements !== "undefined" && !customElements.get("fs-audio")) {
|
|
769
|
+
customElements.define("fs-audio", FsAudio);
|
|
770
|
+
}
|
|
771
|
+
if (typeof customElements !== "undefined" && !customElements.get("fs-video")) {
|
|
772
|
+
customElements.define("fs-video", FsVideo);
|
|
773
|
+
}
|