From f58ad21a5c2d231d1e0057b9ecdd701c50a4e285 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 27 Jun 2023 19:19:17 +0800 Subject: [PATCH] Add Download Task --- components/BCheckbox.tsx | 48 +++++++++ components/BContext.tsx | 29 ++++++ components/BSelect.tsx | 17 +++- components/BTextField.tsx | 120 ++++++++++++++++++++++ components/NewTask.tsx | 175 +++++++++++++++++++++++++++++++-- routes/api/filemeta.ts | 2 +- server/cfg.ts | 13 +++ static/common.css | 26 ++++- translation/en/common.jsonc | 3 +- translation/en/task.jsonc | 6 +- translation/zh-cn/common.jsonc | 3 +- translation/zh-cn/task.jsonc | 6 +- 12 files changed, 427 insertions(+), 21 deletions(-) create mode 100644 components/BCheckbox.tsx create mode 100644 components/BContext.tsx create mode 100644 components/BTextField.tsx diff --git a/components/BCheckbox.tsx b/components/BCheckbox.tsx new file mode 100644 index 0000000..6eb3c28 --- /dev/null +++ b/components/BCheckbox.tsx @@ -0,0 +1,48 @@ +import { Component, ContextType } from "preact"; +import Checkbox from "preact-material-components/Checkbox"; +import { StateUpdater } from "preact/hooks"; +import { BCtx } from "./BContext.tsx"; + +type Props = { + id?: string; + checked: boolean; + name?: string; + description?: string; + set_value?: StateUpdater; +}; + +export default class BCheckbox extends Component { + static contextType = BCtx; + declare context: ContextType; + set_value(value: boolean) { + if (this.props.set_value) { + this.props.set_value(value); + } else if (this.context) { + this.context.set_value((v) => { + v[this.props.name || ""] = value; + return v; + }); + } + } + render() { + let label = null; + if (this.props.description) { + label = ; + } + return ( +
+ { + if (ev.target) { + const e = ev.target as HTMLInputElement; + this.set_value(e.checked); + } + }} + /> + {label} +
+ ); + } +} diff --git a/components/BContext.tsx b/components/BContext.tsx new file mode 100644 index 0000000..e8a656f --- /dev/null +++ b/components/BContext.tsx @@ -0,0 +1,29 @@ +import { Component, ComponentChild, createContext } from "preact"; +import { StateUpdater } from "preact/hooks"; + +type State = { + set_value: StateUpdater>; +}; + +type Props = { + children: ComponentChild; + set_value: StateUpdater>; +}; + +export const BCtx = createContext(null); + +export default class BContext extends Component { + constructor(props: Props) { + super(props); + this.state = { + set_value: props.set_value, + }; + } + render() { + return ( + + {this.props.children} + + ); + } +} diff --git a/components/BSelect.tsx b/components/BSelect.tsx index a6c7b69..b6df991 100644 --- a/components/BSelect.tsx +++ b/components/BSelect.tsx @@ -1,6 +1,7 @@ -import { Component } from "preact"; +import { Component, ContextType } from "preact"; import Select from "preact-material-components/Select"; import { Ref, StateUpdater, useRef } from "preact/hooks"; +import { BCtx } from "./BContext.tsx"; interface obj { toString(): string; @@ -19,7 +20,8 @@ type Props = { /**@default {false}*/ outlined?: boolean; hintText?: string; - set_value: StateUpdater; + set_value?: StateUpdater; + name?: string; }; type State = { @@ -28,6 +30,8 @@ type State = { export default class SettingsSelect extends Component, State> { + static contextType = BCtx; + declare context: ContextType; ref: Ref