vvvfs 0.2.0 → 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/index.html CHANGED
@@ -10,6 +10,7 @@
10
10
 
11
11
  <body>
12
12
  <script src="index.js"></script>
13
+ <fs-img src="test.png" alt="Test Image"></fs-img>
13
14
  </body>
14
15
 
15
16
  </html>
package/index.ts CHANGED
@@ -4,6 +4,9 @@ import { Mime } from "mime";
4
4
  import standardTypes from "mime/types/standard.js";
5
5
  import otherTypes from "mime/types/other.js";
6
6
 
7
+ // 自定义元素 <fs-img>(直接显示 vvvfs 中的图片文件)
8
+ export { FsImg } from "./Img";
9
+
7
10
  const mime = new Mime(standardTypes, otherTypes);
8
11
 
9
12
  /**
@@ -20,6 +23,7 @@ const mime = new Mime(standardTypes, otherTypes);
20
23
  * @param type 文件类型(file或dir)
21
24
  * @param file 文件对象
22
25
  * @param locked 是否锁定
26
+ * @param hidden 是否隐藏
23
27
  */
24
28
  export interface FileRecord {
25
29
  id?: number;
@@ -28,6 +32,7 @@ export interface FileRecord {
28
32
  type: string;
29
33
  file: File | null;
30
34
  locked?: boolean;
35
+ hidden?: boolean;
31
36
  }
32
37
 
33
38
  /**
@@ -309,6 +314,19 @@ class VVVFSFile {
309
314
  async isLocked() {
310
315
  return await this._vvvfs.isLocked(this._path);
311
316
  }
317
+ /**
318
+ * 设置隐藏属性
319
+ * @param hidden 是否隐藏
320
+ */
321
+ async setHide(hidden: boolean) {
322
+ return await this._vvvfs.setHide(this._path, hidden);
323
+ }
324
+ /**
325
+ * 获取隐藏属性
326
+ */
327
+ async getHide() {
328
+ return await this._vvvfs.getHide(this._path);
329
+ }
312
330
  /**
313
331
  * 保存文件
314
332
  * @param name 文件名
@@ -652,6 +670,15 @@ class VVVFS {
652
670
  }
653
671
  });
654
672
  });
673
+ this.#db.version(3).stores({
674
+ files: "++id, name, path, type, file, locked, hidden, [name+path+type]",
675
+ }).upgrade(async (tx) => {
676
+ await tx.table("files").toCollection().modify((file: FileRecord) => {
677
+ if (file.hidden === undefined) {
678
+ file.hidden = false;
679
+ }
680
+ });
681
+ });
655
682
  this.options.init ? this.init(this.options.init == true ? "root" : this.options.init) : void 0;
656
683
  } catch (error) {
657
684
  console.error("创建数据库失败", error);
@@ -681,6 +708,7 @@ class VVVFS {
681
708
  type: "dir" as const,
682
709
  file: new File([], "home"),
683
710
  locked: false,
711
+ hidden: false,
684
712
  });
685
713
  try {
686
714
  for (const file of linuxInitFiles) {
@@ -705,6 +733,9 @@ class VVVFS {
705
733
  this.#db.version(2).stores({
706
734
  files: "++id, name, path, type, file, locked, [name+path+type]",
707
735
  });
736
+ this.#db.version(3).stores({
737
+ files: "++id, name, path, type, file, locked, hidden, [name+path+type]",
738
+ });
708
739
  } catch (error) {
709
740
  console.error("重置数据库失败", error);
710
741
  throw new VVVFSError("ResetDatabase", "重置数据库失败" + error);
@@ -734,6 +765,7 @@ class VVVFS {
734
765
  type: mime.getType(targetPath) || "application/octet-stream",
735
766
  }),
736
767
  locked: false,
768
+ hidden: false,
737
769
  });
738
770
  return true;
739
771
  }, false);
@@ -759,6 +791,7 @@ class VVVFS {
759
791
  type: "dir",
760
792
  file: new File([], ""),
761
793
  locked: false,
794
+ hidden: false,
762
795
  });
763
796
  if (name === "") return true;
764
797
  } else {
@@ -771,6 +804,7 @@ class VVVFS {
771
804
  type: "dir",
772
805
  file: new File([], name),
773
806
  locked: false,
807
+ hidden: false,
774
808
  });
775
809
  return true;
776
810
  }, false);
@@ -1215,6 +1249,7 @@ class VVVFS {
1215
1249
  type: fileRecord.type,
1216
1250
  file: fileRecord.file,
1217
1251
  locked: false,
1252
+ hidden: false,
1218
1253
  });
1219
1254
  return true;
1220
1255
  }
@@ -1332,6 +1367,43 @@ class VVVFS {
1332
1367
  return await this.#isLocked(targetPath);
1333
1368
  }, false);
1334
1369
  }
1370
+ /**
1371
+ * 设置隐藏属性
1372
+ * @param path 文件路径
1373
+ * @param hidden 是否隐藏
1374
+ */
1375
+ async setHide(path: string, hidden: boolean) {
1376
+ return this.#withErrorHandling("setHide", async () => {
1377
+ const targetPath = joinPath(path);
1378
+ if (!(await this.exists(targetPath))) {
1379
+ console.warn("文件不存在");
1380
+ return false;
1381
+ }
1382
+ const { name, parent } = parsePath(targetPath);
1383
+ const fileRecord = await this.#db.files.where({ name, path: parent }).first();
1384
+ if (!fileRecord?.id) {
1385
+ console.warn("文件记录未找到");
1386
+ return false;
1387
+ }
1388
+ await this.#db.files.update(fileRecord.id, { hidden });
1389
+ return true;
1390
+ }, false);
1391
+ }
1392
+ /**
1393
+ * 获取隐藏属性
1394
+ * @param path 文件路径
1395
+ */
1396
+ async getHide(path: string) {
1397
+ return this.#withErrorHandling("getHide", async () => {
1398
+ const targetPath = joinPath(path);
1399
+ if (!(await this.exists(targetPath))) {
1400
+ return false;
1401
+ }
1402
+ const { name, parent } = parsePath(targetPath);
1403
+ const fileRecord = await this.#db.files.where({ name, path: parent }).first();
1404
+ return !!fileRecord?.hidden;
1405
+ }, false);
1406
+ }
1335
1407
  /**
1336
1408
  * 保存文件
1337
1409
  * @param path 文件路径
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvvfs",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "一个使用IndexDB实现的虚拟文件系统",
5
5
  "keywords": [
6
6
  "Virtual",
package/types/index.d.ts CHANGED
@@ -35,6 +35,7 @@ declare global {
35
35
  * @param type 文件类型(file或dir)
36
36
  * @param file 文件对象
37
37
  * @param locked 是否锁定
38
+ * @param hidden 是否隐藏
38
39
  */
39
40
  export interface FileRecord {
40
41
  id?: number;
@@ -43,6 +44,7 @@ export interface FileRecord {
43
44
  type: string;
44
45
  file: File | null;
45
46
  locked?: boolean;
47
+ hidden?: boolean;
46
48
  }
47
49
 
48
50
  /**
@@ -242,6 +244,15 @@ export class VVVFSFile {
242
244
  * 判断文件是否已锁定
243
245
  */
244
246
  isLocked(): Promise<boolean>;
247
+ /**
248
+ * 设置隐藏属性
249
+ * @param hidden 是否隐藏
250
+ */
251
+ setHide(hidden: boolean): Promise<boolean>;
252
+ /**
253
+ * 获取隐藏属性
254
+ */
255
+ getHide(): Promise<boolean>;
245
256
  /**
246
257
  * 保存文件
247
258
  * @param name 文件名
@@ -560,6 +571,17 @@ export class VVVFS {
560
571
  * @param path 文件路径
561
572
  */
562
573
  isLocked(path: string): Promise<boolean>;
574
+ /**
575
+ * 设置隐藏属性
576
+ * @param path 文件路径
577
+ * @param hidden 是否隐藏
578
+ */
579
+ setHide(path: string, hidden: boolean): Promise<boolean>;
580
+ /**
581
+ * 获取隐藏属性
582
+ * @param path 文件路径
583
+ */
584
+ getHide(path: string): Promise<boolean>;
563
585
  /**
564
586
  * 保存文件
565
587
  * @param path 文件路径
@@ -586,6 +608,75 @@ export class VVVFS {
586
608
  static registerMIMEType(mimetype: string, extensions: string[]): Mime;
587
609
  }
588
610
 
611
+ /**
612
+ * <fs-img> 元素 load 事件详情
613
+ */
614
+ export interface FsImgLoadDetail {
615
+ /**
616
+ * 图片路径
617
+ */
618
+ path: string;
619
+ /**
620
+ * 图片文件对象
621
+ */
622
+ file: File | null;
623
+ }
624
+
625
+ /**
626
+ * <fs-img> 元素 error 事件详情
627
+ */
628
+ export interface FsImgErrorDetail {
629
+ /**
630
+ * 图片路径
631
+ */
632
+ path: string;
633
+ /**
634
+ * 错误信息
635
+ */
636
+ message: string;
637
+ }
638
+
639
+ /**
640
+ * 自定义图片元素 <fs-img>
641
+ * @description 直接读取并显示存储在 vvvfs 中的图片文件
642
+ * @attributes
643
+ * - src / path:图片在 vvvfs 中的路径(src 优先,其次 path)
644
+ * - dbname:数据库名,默认 "vvvfs";未指定且存在全局 vvvfs 实例时直接使用该实例
645
+ * - alt / width / height:透传到内部 <img> 元素
646
+ * - watch:存在时,文件发生变化自动刷新
647
+ * @events
648
+ * - load:图片加载完成(detail: { path, file })
649
+ * - error:图片加载失败(detail: { path, message })
650
+ * @example <fs-img src="/home/user/Pictures/logo.png" alt="Logo" width="120"></fs-img>
651
+ */
652
+ export class FsImg extends HTMLElement {
653
+ /**
654
+ * 监听的属性列表
655
+ */
656
+ static get observedAttributes(): string[];
657
+ /**
658
+ * 构造函数
659
+ */
660
+ constructor();
661
+ /**
662
+ * 图片路径(src 优先,其次 path)
663
+ */
664
+ src: string;
665
+ /**
666
+ * 数据库名
667
+ */
668
+ dbname: string;
669
+ /**
670
+ * 手动刷新(重新读取并显示图片)
671
+ */
672
+ refresh(): Promise<boolean>;
673
+ /**
674
+ * 调度一次 watch 触发的刷新(内部使用)
675
+ * @internal
676
+ */
677
+ scheduleWatchRefresh(): void;
678
+ }
679
+
589
680
  /**
590
681
  * 解析路径
591
682
  * @param path 路径