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