From 903561a9074da0632e1e001db3879cc45f974493 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 5 Sep 2021 21:46:40 +0700 Subject: vfs: refactor example to use TypeScript loaded with type="module" --- index.html | 1 + vfs/example.d.ts | 1 + vfs/example.html | 144 ------------------------------------------------- vfs/example.ts | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ vfs/index.html | 11 ++++ vfs/vfs.d.ts | 10 ++-- vfs/vfs.ts | 10 ++-- 7 files changed, 182 insertions(+), 154 deletions(-) create mode 100644 vfs/example.d.ts delete mode 100644 vfs/example.html create mode 100644 vfs/example.ts create mode 100644 vfs/index.html diff --git a/index.html b/index.html index da342af..f0be1a4 100644 --- a/index.html +++ b/index.html @@ -7,5 +7,6 @@

Editor

Input

+

VFS

diff --git a/vfs/example.d.ts b/vfs/example.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/vfs/example.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/vfs/example.html b/vfs/example.html deleted file mode 100644 index 4e28bf8..0000000 --- a/vfs/example.html +++ /dev/null @@ -1,144 +0,0 @@ - - - - - WUI - Virtual File System (vfs) - - -
- - - - - - diff --git a/vfs/example.ts b/vfs/example.ts new file mode 100644 index 0000000..bc8d37d --- /dev/null +++ b/vfs/example.ts @@ -0,0 +1,159 @@ +import { WuiVfs, WuiVfsNodeInterface } from "./vfs.js" +import { WuiResponseInterface } from "../response.js" + +interface PathNodeInterface { + [key: string]: WuiVfsNodeInterface +} + +let dummyfs: PathNodeInterface = { + "/": { + name: "/", + path: "/", + is_dir: true, + content: "", + childs: [ + { + name: "Dir 1", + path: "/Dir 1", + is_dir: true, + content: "", + childs: [ + { + name: "File 1.1", + path: "/Dir 1/File 1.1", + is_dir: false, + content: "This is the content of File 1.1", + }, + { + name: `File 1.2`, + path: "/Dir 1/File 1.2", + is_dir: false, + content: "This is the content of File 1.2", + }, + ], + }, + { + name: "Dir 2", + path: "/Dir 2", + is_dir: true, + content: "", + childs: [ + { + name: "File 2.1", + path: "/Dir 2/File 2.1", + is_dir: false, + content: "This is the content of File 2.1", + }, + { + name: "File 2.2", + path: "/Dir 2/File 2.2", + is_dir: false, + content: "This is the content of File 2.2", + }, + ], + }, + ], + }, + "/Dir 1": { + name: "Dir 1", + path: "/Dir 1", + is_dir: true, + content: "", + childs: [ + { + name: "File 1.1", + path: "/Dir 1/File 1.1", + is_dir: false, + content: "This is the content of File 1.1", + }, + { + name: "File 1.2", + path: "/Dir 1/File 1.2", + is_dir: false, + content: "This is the content of File 1.2", + }, + ], + }, + "/Dir 2": { + name: "Dir 2", + path: "/Dir 2", + is_dir: true, + content: "", + childs: [ + { + name: "File 2.1", + path: "/Dir 2/File 2.1", + is_dir: false, + content: "This is the content of File 2.1", + }, + { + name: "File 2.2", + path: "/Dir 2/File 2.2", + is_dir: false, + content: "This is the content of File 2.2", + }, + ], + } +} + +async function main() { + let opts = { + id: "vfs", + Open: Open, + OpenNode: OpenNode, + } + + let wui_vfs = new WuiVfs(opts) + wui_vfs.OpenDir("/") +} + +async function Open(path: string, is_dir: boolean): Promise { + console.log("Open:", path, is_dir) + let res: WuiResponseInterface = { + code: 200, + message: "", + } + + if (is_dir) { + res.data = dummyfs[path] + return res + } + + res.data = { + name: "", + path: path, + content: "", + } + + switch (path) { + case "/Dir 1/File 1.1": + res.data.name = "File 1.1" + res.data.content = "This is the content of " + res.data.name + break + case "/Dir 1/File 1.2": + res.data.name = "File 1.2" + res.data.content = "This is the content of " + res.data.name + break + case "/Dir 2/File 2.1": + res.data.name = "File 2.1" + res.data.content = "This is the content of " + res.data.name + break + case "/Dir 2/File 2.2": + res.data.name = "File 2.1" + res.data.content = "This is the content of " + res.data.name + break + default: + res.code = 404 + res.message = "path not found" + } + + console.log("Open:", res) + + return res +} + +async function OpenNode(node: WuiVfsNodeInterface): Promise { + return await Open(node.path, node.is_dir) +} + +main() diff --git a/vfs/index.html b/vfs/index.html new file mode 100644 index 0000000..10a69f2 --- /dev/null +++ b/vfs/index.html @@ -0,0 +1,11 @@ + + + + + WUI - Virtual File System (vfs) + + +
+ + + diff --git a/vfs/vfs.d.ts b/vfs/vfs.d.ts index 177fcfd..5e89b33 100644 --- a/vfs/vfs.d.ts +++ b/vfs/vfs.d.ts @@ -3,11 +3,11 @@ export interface WuiVfsNodeInterface { name: string; path: string; is_dir: boolean; - content_type: string; - mod_time: number; - size: number; - mode: string; - childs: WuiVfsNodeInterface[]; + content_type?: string; + mod_time?: number; + size?: number; + mode?: string; + childs?: WuiVfsNodeInterface[]; content: string; } export interface WuiVfsOptions { diff --git a/vfs/vfs.ts b/vfs/vfs.ts index b1b0881..c6a5a8d 100644 --- a/vfs/vfs.ts +++ b/vfs/vfs.ts @@ -8,11 +8,11 @@ export interface WuiVfsNodeInterface { name: string path: string is_dir: boolean - content_type: string - mod_time: number - size: number - mode: string - childs: WuiVfsNodeInterface[] + content_type?: string + mod_time?: number + size?: number + mode?: string + childs?: WuiVfsNodeInterface[] content: string // If its not empty, it MUST be encoded in base64. } -- cgit v1.3