diff --git a/source/Context/Context.tsx b/source/Context/Context.tsx new file mode 100644 index 0000000..c5cd4c7 --- /dev/null +++ b/source/Context/Context.tsx @@ -0,0 +1,59 @@ +import { Emitter, EventType } from "@Model/Emitter"; +import { Component, FunctionComponent, ReactNode, Consumer } from "react"; + +type RenderComponent = (new (...p: any) => Component) | FunctionComponent; + +function superConnect, E extends Record>( + consumer: Consumer +) { + return (...events: Array) => { + return (components: R): R => { + const C = components as any; + return class extends Component { + + private status: C | undefined; + private isEventMount: boolean = false; + + private handelChange = () => { + this.forceUpdate(); + } + + private mountEvent() { + if (this.status && !this.isEventMount) { + this.isEventMount = true; + console.log("Component dep event mount: " + events.join(", ")); + for (let i = 0; i < events.length; i++) { + this.status.on(events[i], this.handelChange); + } + } + } + + private unmountEvent() { + if (this.status) { + for (let i = 0; i < events.length; i++) { + this.status.off(events[i], this.handelChange); + } + } + } + + public render(): ReactNode { + const Consumer = consumer; + return + {(status: C) => { + this.status = status; + this.mountEvent(); + return ; + }} + + } + + public componentWillUnmount() { + this.unmountEvent(); + } + + } as any; + } + } +} + +export { superConnect }; \ No newline at end of file diff --git a/source/Context/Status.tsx b/source/Context/Status.tsx index 3e0ce63..c9b6b2f 100644 --- a/source/Context/Status.tsx +++ b/source/Context/Status.tsx @@ -1,4 +1,4 @@ -import { createContext, Component, FunctionComponent, useState, useEffect, ReactNode } from "react"; +import { createContext, Component, FunctionComponent, ReactNode } from "react"; import { Emitter } from "@Model/Emitter"; import { Model, ObjectID } from "@Model/Model"; import { Label } from "@Model/Label"; @@ -9,6 +9,7 @@ import { AbstractRenderer } from "@Model/Renderer"; import { ClassicRenderer, MouseMod } from "@GLRender/ClassicRenderer"; import { Setting } from "./Setting"; import { I18N } from "@Component/Localization/Localization"; +import { superConnect } from "./Context"; function randomColor(unNormal: boolean = false) { const color = [ @@ -272,53 +273,7 @@ function useStatus(components: R): R { }) as any; } -function useStatusWithEvent(...events: Array) { - return (components: R): R => { - const C = components as any; - return class extends Component { - - private status: Status | undefined; - private isEventMount: boolean = false; - - private handelChange = () => { - this.forceUpdate(); - } - - private mountEvent() { - if (this.status && !this.isEventMount) { - this.isEventMount = true; - console.log("Component dep event mount: " + events.join(", ")); - for (let i = 0; i < events.length; i++) { - this.status.on(events[i], this.handelChange); - } - } - } - - private unmountEvent() { - if (this.status) { - for (let i = 0; i < events.length; i++) { - this.status.off(events[i], this.handelChange); - } - } - } - - public render(): ReactNode { - return - {(status: Status) => { - this.status = status; - this.mountEvent(); - return ; - }} - - } - - public componentWillUnmount() { - this.unmountEvent(); - } - - } as any; - } -} +const useStatusWithEvent = superConnect(StatusConsumer); export { Status, StatusContext, useStatus, useStatusWithEvent, diff --git a/source/Model/Label.ts b/source/Model/Label.ts index 81c1d2b..3cdf609 100644 --- a/source/Model/Label.ts +++ b/source/Model/Label.ts @@ -46,7 +46,7 @@ class Label { * 判断是否为相同标签 */ public equal(label: Label): boolean { - if (this.isDeleted() || label.isDeleted()) return false; + // if (this.isDeleted() || label.isDeleted()) return false; return this === label || this.id === label.id; }