From 0eb0b6f7f560ae44c9d56eeae25257b55d655008 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Wed, 23 Feb 2022 17:51:20 +0800 Subject: [PATCH] Test Consumer Provider --- source/Component/HeaderBar/HeaderBar.scss | 0 source/Component/HeaderBar/HeaderBar.tsx | 44 +++++++++++++++++ source/Context/Setting.tsx | 58 +++++++++++++++++++++++ source/Page/SimulatorWeb/SimulatorWeb.tsx | 21 ++++++-- tsconfig.json | 7 +++ 5 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 source/Component/HeaderBar/HeaderBar.scss create mode 100644 source/Component/HeaderBar/HeaderBar.tsx create mode 100644 source/Context/Setting.tsx diff --git a/source/Component/HeaderBar/HeaderBar.scss b/source/Component/HeaderBar/HeaderBar.scss new file mode 100644 index 0000000..e69de29 diff --git a/source/Component/HeaderBar/HeaderBar.tsx b/source/Component/HeaderBar/HeaderBar.tsx new file mode 100644 index 0000000..708516f --- /dev/null +++ b/source/Component/HeaderBar/HeaderBar.tsx @@ -0,0 +1,44 @@ +import { Component, ReactNode } from "react"; +import { useSetting, IMixinSettingProps, Themes } from "@Context/Setting"; + +interface IHeaderBarProps {} + +/** + * 头部信息栏 + */ +@useSetting +class HeaderBar extends Component { + + private handelClick = () => { + if (this.props.setting) { + this.props.setting.setProps("themes", + this.props.setting.themes === Themes.dark ? Themes.light : Themes.dark + ); + } + } + + private changeListener = () => { + this.forceUpdate(); + } + + public componentDidMount() { + console.log("mount"); + if (this.props.setting) { + this.props.setting.on("change", this.changeListener); + } + } + + public componentWillUnmount() { + console.log("die"); + if (this.props.setting) { + this.props.setting.off("change", this.changeListener); + } + } + + public render(): ReactNode { + return
{this.props.setting?.themes}
+ } +} + +export default HeaderBar; +export { HeaderBar }; \ No newline at end of file diff --git a/source/Context/Setting.tsx b/source/Context/Setting.tsx new file mode 100644 index 0000000..170471f --- /dev/null +++ b/source/Context/Setting.tsx @@ -0,0 +1,58 @@ +import { createContext, Component, FunctionComponent } from "react"; +import { Emitter } from "@Model/Emitter"; + +/** + * 主题模式 + */ +enum Themes { + light = 1, + dark = 2 +} + +class Setting extends Emitter< + Setting & {change: keyof Setting} +> { + + /** + * 主题 + */ + public themes: Themes = Themes.light; + + /** + * 设置参数 + */ + public setProps

(key: P, value: Setting[P]) { + this[key] = value as any; + this.emit("change", key); + this.emit(key as any, value as any); + } +} + +interface IMixinSettingProps { + setting?: Setting; +} + +const SettingContext = createContext(new Setting()); + +SettingContext.displayName = "Setting"; +const SettingProvider = SettingContext.Provider; +const SettingConsumer = SettingContext.Consumer; + +type RenderComponent = (new (...p: any) => Component) | FunctionComponent; + +/** + * 修饰器 + */ +function useSetting(components: R): R { + return ((props: any) => { + const C = components; + return + {(setting: Setting) => } + + }) as any; +} + +export { + Themes, Setting, SettingContext, useSetting, + IMixinSettingProps, SettingProvider, SettingConsumer +}; \ No newline at end of file diff --git a/source/Page/SimulatorWeb/SimulatorWeb.tsx b/source/Page/SimulatorWeb/SimulatorWeb.tsx index 5f80868..af5232d 100644 --- a/source/Page/SimulatorWeb/SimulatorWeb.tsx +++ b/source/Page/SimulatorWeb/SimulatorWeb.tsx @@ -1,13 +1,28 @@ -import { Component, ReactNode, createRef } from "react"; +import { Component, ReactNode } from "react"; +import { SettingProvider, Setting } from "@Context/Setting"; +import { HeaderBar } from "@Component/HeaderBar/HeaderBar"; import { Entry } from "../Entry/Entry"; import "./SimulatorWeb.scss"; class SimulatorWeb extends Component { - private canvasContRef = createRef(); + /** + * 全局设置 + */ + private setting: Setting; + + public constructor(props: any) { + super(props); + + // TODO: 这里要读取设置 + this.setting = new Setting(); + (window as any).setting = (this.setting as any); + } public render(): ReactNode { - return

Web
+ return + + } } diff --git a/tsconfig.json b/tsconfig.json index b0d5304..0d39049 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compileOnSave": true, "compilerOptions": { + "experimentalDecorators": true, "alwaysStrict": true, "strict": true, "sourceMap": true, @@ -23,6 +24,12 @@ ], "@GLRender/*": [ "./source/GLRender/*" + ], + "@Context/*": [ + "./source/Context/*" + ], + "@Component/*": [ + "./source/Component/*" ] } },