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 ADDED
@@ -0,0 +1,46 @@
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 { FsMedia } from "./Media";
10
+
11
+ /**
12
+ * 自定义图片元素 <fs-img>
13
+ * @description 直接读取并显示存储在 vvvfs 中的图片文件。
14
+ * 内部使用 Shadow DOM 渲染 <img>,可通过 ::part(img) 定制样式;
15
+ * 默认插槽内容会在加载失败或未设置路径时显示。
16
+ * @attributes
17
+ * - src / path:图片在 vvvfs 中的路径(src 优先,其次 path)
18
+ * - dbname:数据库名,默认 "vvvfs";未指定且存在全局 vvvfs 实例时直接使用该实例
19
+ * - alt / width / height:透传到内部 <img> 元素
20
+ * - watch:存在时,文件内容发生变化后自动刷新
21
+ * @properties
22
+ * - data:当前显示的图片文件的 Blob 对象(未加载或加载失败时为 null)
23
+ * @events
24
+ * - load:图片加载完成(detail: { path, file })
25
+ * - error:图片加载失败(detail: { path, message })
26
+ */
27
+ export class FsImg extends FsMedia {
28
+ protected readonly mediaTag = "img";
29
+ protected readonly partName = "img";
30
+ protected readonly mediaLabel = "图片";
31
+ protected readonly acceptPrefix = "image/";
32
+ protected readonly loadEventName = "load";
33
+ protected readonly mediaAttrs = ["alt", "width", "height"] as const;
34
+ protected readonly defaultAttrs: readonly string[] = [];
35
+ protected readonly shadowCss = `
36
+ img {
37
+ display: block;
38
+ max-width: 100%;
39
+ }
40
+ `;
41
+ }
42
+
43
+ // 注册自定义元素(避免重复注册导致报错)
44
+ if (typeof customElements !== "undefined" && !customElements.get("fs-img")) {
45
+ customElements.define("fs-img", FsImg);
46
+ }