From 0f40bc7c43ef5d05b4a02960296aa3f1fdbbdc0a Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 26 May 2023 08:39:21 +0800 Subject: [PATCH] Update --- components/GlobalContext.tsx | 23 ++++++++++++++++++++ components/Menu.tsx | 36 +++++++++++++++++++++++++++++++ components/StyleSheet.tsx | 21 ++++++++++++++++++ deno.json | 3 ++- deno.lock | 27 ++++++++++++++++++++++++ fetch_static_files.ts | 41 ++++++++++++++++++++++++++++++++++++ fresh.gen.ts | 2 ++ import_map.json | 3 ++- routes/index.tsx | 17 +++++++++++++++ static/.gitignore | 1 + 10 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 components/GlobalContext.tsx create mode 100644 components/Menu.tsx create mode 100644 components/StyleSheet.tsx create mode 100644 fetch_static_files.ts create mode 100644 routes/index.tsx create mode 100644 static/.gitignore diff --git a/components/GlobalContext.tsx b/components/GlobalContext.tsx new file mode 100644 index 0000000..c9a4a89 --- /dev/null +++ b/components/GlobalContext.tsx @@ -0,0 +1,23 @@ +import { Component, ComponentChild, createContext } from "preact"; + +export const GlobalCtx = createContext(null); + +type State = { + stylesheets: Set; +}; + +type Props = { children: ComponentChild }; + +export default class GlobalContext extends Component { + constructor(props: Props) { + super(props); + this.state = { stylesheets: new Set() }; + } + render() { + return ( + + {this.props.children} + + ); + } +} diff --git a/components/Menu.tsx b/components/Menu.tsx new file mode 100644 index 0000000..583a84c --- /dev/null +++ b/components/Menu.tsx @@ -0,0 +1,36 @@ +import { Head } from "$fresh/runtime.ts"; +import { Component } from "preact"; +import { ContextType } from "preact"; +import TopAppBar from "preact-material-components/TopAppBar"; +import StyleSheet from "./StyleSheet.tsx"; +import { GlobalCtx } from "./GlobalContext.tsx"; + +export default class Menu extends Component { + static contextType = GlobalCtx; + declare context: ContextType; + render() { + return ( +
+ + + + + + + {}}> + + + menu + + EH Downloader + + + + more_vert + + + +
+ ); + } +} diff --git a/components/StyleSheet.tsx b/components/StyleSheet.tsx new file mode 100644 index 0000000..23a00ff --- /dev/null +++ b/components/StyleSheet.tsx @@ -0,0 +1,21 @@ +import { asset } from "$fresh/runtime.ts"; +import { Component, ContextType, RenderableProps } from "preact"; +import { GlobalCtx } from "./GlobalContext.tsx"; + +export type StyleSheetType = { + href: string; +}; + +export default class StyleSheet extends Component { + static contextType = GlobalCtx; + declare context: ContextType; + render() { + const href = this.props.href; + if (this.context) { + const sheets = this.context.stylesheets; + if (sheets.has(href)) return null; + sheets.add(href); + } + return ; + } +} diff --git a/deno.json b/deno.json index b05ce05..a182dea 100644 --- a/deno.json +++ b/deno.json @@ -5,7 +5,8 @@ "test": "deno test --allow-read=./ --allow-net --allow-write=./ --allow-run=tasklist.exe --unstable", "run": "deno run --allow-read=./ --allow-write=./ --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable", "compile": "deno compile --allow-read=./ --allow-write=./ --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable", - "compile_full": "deno compile --allow-read --allow-write --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable" + "compile_full": "deno compile --allow-read --allow-write --allow-run=tasklist.exe --allow-env=DENO_DEPLOYMENT_ID --allow-net --unstable", + "fetch": "deno run --allow-read=./ --allow-write=./ --allow-net fetch_static_files.ts" }, "fmt": { "indentWidth": 4, diff --git a/deno.lock b/deno.lock index 9dce48c..260ad25 100644 --- a/deno.lock +++ b/deno.lock @@ -194,18 +194,45 @@ "https://deno.land/x/zipjs@v2.7.14/lib/z-worker-inline.js": "df83d91413a2e79f76924f39f26f59e6efbe8f5487d3a91b7e92b6d64236927c", "https://deno.land/x/zipjs@v2.7.14/lib/zip-fs.js": "a733360302f5fbec9cc01543cb9fcfe7bae3f35a50d0006626ce42fe8183b63f", "https://esm.sh/*preact-render-to-string@5.2.6": "88ec8d8706b6a3f1e0fdad3862a2690dcd9b350d87bdc8e7bd0e27fbc0f7d29e", + "https://esm.sh/preact-material-components@1.6.1/TopAppBar": "e418485020aecb866d9cc44216cd52c5ac1ff120bd1ad422b318428c274b3474", "https://esm.sh/preact@10.13.1": "ae382301328ab874e2c42bee76e261bb8d094673fe76ca7eb71917636d43d8ad", "https://esm.sh/preact@10.13.1/hooks": "1813f80c6d648f8137a62569e35972b0716f92539f192d6db66d45b7aafea43a", "https://esm.sh/preact@10.13.1/jsx-runtime": "0caf2b2eade93af59e89abc31816b6cb2829a9b8a82b958c0ebc1d8bec241e2a", "https://esm.sh/stable/preact@10.13.1/deno/hooks.js": "f25445c4f9fa6742119c41db4a85a2f75ee6ea941c506ae8ad3b1568c7692c61", "https://esm.sh/stable/preact@10.13.1/deno/jsx-runtime.js": "7c8e1b8f272996846cbac0837dcb71f6f8cfc82611b3f7819501d07c37383dc0", "https://esm.sh/stable/preact@10.13.1/deno/preact.mjs": "9b73545225d0ed274c89f39aee524a2857c81a73060e80c2c4bdc2a6de7bec26", + "https://esm.sh/stable/preact@10.14.1/deno/preact.mjs": "f51b1a99f29fa9879d70dc283bb8f42ce9beef99aa93dd6ca969f297e101e964", "https://esm.sh/twind@0.16.19": "e6db0352eb940ca8cdbf129fe4520809916d002b8ccc2e2fc7f8551138f7a161", "https://esm.sh/twind@0.16.19/sheets": "3472c3c28394aa25c026ff81a393361e71aca014b3b8db5d6c67396e60f87cc9", "https://esm.sh/v118/preact@10.13.1/hooks/src/index.d.ts": "5c29febb624fc25d71cb0e125848c9b711e233337a08f7eacfade38fd4c14cc3", "https://esm.sh/v118/preact@10.13.1/jsx-runtime/src/index.d.ts": "e153460ed2b3fe2ad8b93696ecd48fbf73cd628b0b0ea6692b71804a3af69dfd", "https://esm.sh/v118/preact@10.13.1/src/index.d.ts": "65398710de6aa0a07412da79784e05e6e96763f51c7c91b77344d2d0af06385c", "https://esm.sh/v118/preact@10.13.1/src/jsx.d.ts": "fde41cfb4944bcc5005e653c989ef8f85deb1cbb5364a1726654993937cd08d0", + "https://esm.sh/v118/preact@10.14.1/src/index.d.ts": "65398710de6aa0a07412da79784e05e6e96763f51c7c91b77344d2d0af06385c", + "https://esm.sh/v118/preact@10.14.1/src/jsx.d.ts": "de467cd980f79a13bf3026146afed7173a3634e7d6415d349184663c67174c0c", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/classCallCheck.js": "678d4a1fc837f06947628e73a957e1f017fe30c77e8500008ec9a5977563c4f9", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/createClass.js": "4205e44f1238343af0a0e0ebc92cfb043382600f54c3e53aa4e6801c9bec7a44", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/get.js": "2021a8fa9d0d4880ece83c1d93266ac1d7414430f4f4671691a336ef3aa25dbe", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/getPrototypeOf.js": "41e694838de6926a04a013646f2b3915afa24a57a9301f3e226eadc78fa32e76", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/inherits.js": "09429b866edd8b9dd67dab992f9ad8865c19e15ffecde5ff632e4929755e2807", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/interopRequireDefault.js": "273ebefd452416883e9e517a620b46197b732a9ee384dfcb2ef33f731b7293d4", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/possibleConstructorReturn.js": "c48587b6a4194dac1f823e4706c67a286187cec9f29d35aac06df940443baa51", + "https://esm.sh/v122/@babel/runtime@7.21.5/deno/helpers/typeof.js": "ea8e5723c88f83ac7cfbb5728d38e20449f7387093b2825e6c8c81fdc50a7f32", + "https://esm.sh/v122/@material/base@0.39.0/deno/component.js": "031f373ae497c52e9b7050165e31dc6e708aebe2b2df027b25c897ce4b7cd43a", + "https://esm.sh/v122/@material/base@0.39.0/deno/foundation.js": "4edd0737b3601305ffe599556e98e4861542210744da83af19194c8c4f50c0e0", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/adapter.js": "29b56c7005653c22ce0d167db65b802e14903e2ba67164824d0be6364309117f", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/constants.js": "f8a80510d9c102aacc87c0fdcdd665118685d53f18948f7eabe275b2c85512a8", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/foundation.js": "691312af51a78412d5bea70fee6d85cd3c2e6beb6e39a5ad878496d874b9633a", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/index.js": "379c671f75219f71074e9d5fc1d790325d4ef469e0268d509147afaab67c6e8a", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/ripple.mjs": "a915efddd4cf20bc2bbb187fc44655b1d9919d563be67053fa9b71b78cd05df5", + "https://esm.sh/v122/@material/ripple@0.39.3/deno/util.js": "4df787664fc517d4545bb7d502f85de3b34cfa613e34edd6b4630ea21e673cf2", + "https://esm.sh/v122/@material/top-app-bar@0.39.3/deno/top-app-bar.mjs": "e7de012030af2ce0baac3cff6c96adc206f763cea07924a8c714fd4a83b6a18f", + "https://esm.sh/v122/bind-decorator@1.0.11/deno/bind-decorator.mjs": "21a126bdebaca6e38120139d903c3485c4f57e15487646666e79c1b5c15d0e44", + "https://esm.sh/v122/preact-material-components@1.6.1/Base/MaterialComponent.d.ts": "af619a28f2d2f76113e29e952308a2ce46577438f7ed40531458b880ff9a141f", + "https://esm.sh/v122/preact-material-components@1.6.1/Base/types.d.ts": "4ec93636595145b9c5cdd628111f28e66148821c23c8ef6273f84bb84a6b8eb8", + "https://esm.sh/v122/preact-material-components@1.6.1/TopAppBar/index.d.ts": "d921bd9336541339b0fff19e0769cea149b798bc172d261a7b7a1595f6b90266", + "https://esm.sh/v122/preact-material-components@1.6.1/deno/Base/MaterialComponent.js": "7dca052dee2ab5010232055eac4a3159c8455e9b2857870525f7002cb50160bd", + "https://esm.sh/v122/preact-material-components@1.6.1/deno/TopAppBar.js": "ab7ec4fa168d7867961b9fd818e10d138ffb075c20ea4b11f6d231318d897145", "https://esm.sh/v124/csstype@3.1.2/index.d.ts": "4c68749a564a6facdf675416d75789ee5a557afda8960e0803cf6711fa569288", "https://esm.sh/v124/preact-render-to-string@5.2.6/X-ZS8q/deno/preact-render-to-string.mjs": "672116885c5e5072207c527a0ec663f5bc52774a0868ec487985109520382a55", "https://esm.sh/v124/preact-render-to-string@5.2.6/X-ZS8q/src/index.d.ts": "b1d73703252c8570fdf2952475805f5808ba3511fefbd93a3e7bd8406de7dcd0", diff --git a/fetch_static_files.ts b/fetch_static_files.ts new file mode 100644 index 0000000..c5228a2 --- /dev/null +++ b/fetch_static_files.ts @@ -0,0 +1,41 @@ +import { dirname, join } from "std/path/mod.ts"; +import { sure_dir } from "./utils.ts"; + +const map = JSON.parse(await Deno.readTextFile("./import_map.json")).imports; +const LIST: string[] = ["preact-material-components/TopAppBar/style.css"]; + +function get_url(i: string) { + for (const v of Object.getOwnPropertyNames(map)) { + if (v.endsWith("/") && i.startsWith(v)) { + return i.replace(v, map[v]); + } + } + for (const v of Object.getOwnPropertyNames(map)) { + if (i.startsWith(v)) { + return i.replace(v, map[v]); + } + } + return i; +} + +async function fetch_file(u: string | URL, p: string) { + await sure_dir(dirname(p)); + const f = await Deno.open(p, { create: true, write: true, truncate: true }); + try { + const r = await fetch(u); + if (!r.body) throw Error("No body."); + await r.body.pipeTo(f.writable); + } finally { + try { + f.close(); + } catch (_) { + null; + } + } +} + +for (const i of LIST) { + const u = get_url(i); + const p = join("./static", i); + await fetch_file(u, p); +} diff --git a/fresh.gen.ts b/fresh.gen.ts index 2fbb485..f25276f 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -4,10 +4,12 @@ import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/api/task/list.ts"; +import * as $1 from "./routes/index.tsx"; const manifest = { routes: { "./routes/api/task/list.ts": $0, + "./routes/index.tsx": $1, }, islands: {}, baseUrl: import.meta.url, diff --git a/import_map.json b/import_map.json index 488bd19..64e3199 100644 --- a/import_map.json +++ b/import_map.json @@ -12,6 +12,7 @@ "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3", "twind": "https://esm.sh/twind@0.16.19", "twind/": "https://esm.sh/twind@0.16.19/", - "$std/": "https://deno.land/std@0.187.0/" + "$std/": "https://deno.land/std@0.187.0/", + "preact-material-components/": "https://esm.sh/preact-material-components@1.6.1/" } } diff --git a/routes/index.tsx b/routes/index.tsx new file mode 100644 index 0000000..ca34e02 --- /dev/null +++ b/routes/index.tsx @@ -0,0 +1,17 @@ +import { Head } from "$fresh/runtime.ts"; +import GlobalContext from "../components/GlobalContext.tsx"; +import Menu from "../components/Menu.tsx"; +import StyleSheet from "../components/StyleSheet.tsx"; + +export default function Index() { + return ( + + + + EH Downloader + + + + + ); +} diff --git a/static/.gitignore b/static/.gitignore new file mode 100644 index 0000000..ccc9229 --- /dev/null +++ b/static/.gitignore @@ -0,0 +1 @@ +preact-material-components/