From 5ff6987a4b4f23872c8c803ea44a351224c8d96b Mon Sep 17 00:00:00 2001 From: MrKBear Date: Tue, 29 Mar 2022 15:14:31 +0800 Subject: [PATCH] Bind add behavior function --- .../BehaviorPopup/BehaviorPopup.scss | 2 +- .../Component/BehaviorPopup/BehaviorPopup.tsx | 56 ++++++++++++++----- source/Context/Status.tsx | 4 +- source/Localization/EN-US.ts | 1 + source/Localization/ZH-CN.ts | 3 +- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/source/Component/BehaviorPopup/BehaviorPopup.scss b/source/Component/BehaviorPopup/BehaviorPopup.scss index 8886442..f4039e7 100644 --- a/source/Component/BehaviorPopup/BehaviorPopup.scss +++ b/source/Component/BehaviorPopup/BehaviorPopup.scss @@ -5,7 +5,7 @@ div.behavior-popup { height: 100%; } -span.behavior-popup-select-counter { +span.behavior-popup-select-counter, div.behavior-popup-no-data { opacity: .75; } diff --git a/source/Component/BehaviorPopup/BehaviorPopup.tsx b/source/Component/BehaviorPopup/BehaviorPopup.tsx index d1aa39d..eb5691c 100644 --- a/source/Component/BehaviorPopup/BehaviorPopup.tsx +++ b/source/Component/BehaviorPopup/BehaviorPopup.tsx @@ -6,14 +6,14 @@ import { ConfirmContent } from "@Component/ConfirmPopup/ConfirmPopup"; import { BehaviorList } from "@Component/BehaviorList/BehaviorList"; import { AllBehaviorsWithCategory, ICategoryBehavior } from "@Behavior/Behavior"; import { Message } from "@Component/Message/Message"; -import { IRenderBehavior } from "@Model/Behavior"; -import { useStatus, IMixinStatusProps } from "@Context/Status"; +import { IRenderBehavior, BehaviorRecorder } from "@Model/Behavior"; +import { useStatus, IMixinStatusProps, randomColor } from "@Context/Status"; import { useSettingWithEvent, IMixinSettingProps } from "@Context/Setting"; import { ConfirmPopup } from "@Component/ConfirmPopup/ConfirmPopup"; import "./BehaviorPopup.scss"; interface IBehaviorPopupProps { - + onDismiss?: () => void; } interface IBehaviorPopupState { @@ -21,7 +21,7 @@ interface IBehaviorPopupState { focusBehavior: Set; } -class BehaviorPopup extends Popup { +class BehaviorPopup extends Popup { public minWidth: number = 400; public minHeight: number = 300; @@ -34,7 +34,9 @@ class BehaviorPopup extends Popup { } public render(): ReactNode { - return + return { + this.close(); + }}/> } } @@ -125,25 +127,53 @@ class BehaviorPopupComponent extends Component< } + private addSelectBehavior = () => { + this.state.focusBehavior.forEach((recorder) => { + if (this.props.status && recorder instanceof BehaviorRecorder) { + let newBehavior = this.props.status.model.addBehavior(recorder); + + // 初始化名字 + newBehavior.name = recorder.getTerms( + recorder.behaviorName, this.props.setting?.language + ) + " " + (recorder.nameIndex - 1).toString(); + + // 赋予一个随机颜色 + let color = randomColor(true); + newBehavior.color = `rgb(${color[0]},${color[1]},${color[2]})`; + } + }); + this.props.onDismiss ? this.props.onDismiss() : undefined; + } + public render(): ReactNode { let first: boolean = true; + let behaviorNodes = AllBehaviorsWithCategory.map((behavior) => { + let renderItem = this.renderBehaviors(behavior, first); + if (renderItem) { + first = false; + } + return renderItem; + }).filter((x) => !!x); + return - {AllBehaviorsWithCategory.map((behavior) => { - let renderItem = this.renderBehaviors(behavior, first); - if (renderItem) { - first = false; - } - return renderItem; - }).filter((x) => !!x)} + { + behaviorNodes.length ? behaviorNodes : + + } } } diff --git a/source/Context/Status.tsx b/source/Context/Status.tsx index 16f111f..6a526df 100644 --- a/source/Context/Status.tsx +++ b/source/Context/Status.tsx @@ -40,6 +40,7 @@ interface IStatusEvent { labelAttrChange: void; groupAttrChange: void; individualChange: void; + behaviorChange: void; popupChange: void; } @@ -102,6 +103,7 @@ class Status extends Emitter { // 对象变化事件 this.model.on("objectChange", () => this.emit("objectChange")); this.model.on("labelChange", () => this.emit("labelChange")); + this.model.on("behaviorChange", () => this.emit("behaviorChange")); // 弹窗事件 this.popup.on("popupChange", () => this.emit("popupChange")); @@ -277,6 +279,6 @@ const useStatus = superConnect(StatusConsumer, "status"); const useStatusWithEvent = superConnectWithEvent(StatusConsumer, "status"); export { - Status, StatusContext, useStatus, useStatusWithEvent, + Status, StatusContext, useStatus, useStatusWithEvent, randomColor, IMixinStatusProps, StatusProvider, StatusConsumer }; \ No newline at end of file diff --git a/source/Localization/EN-US.ts b/source/Localization/EN-US.ts index 81ef619..26698e4 100644 --- a/source/Localization/EN-US.ts +++ b/source/Localization/EN-US.ts @@ -55,6 +55,7 @@ const EN_US = { "Popup.Add.Behavior.Title": "Add behavior", "Popup.Add.Behavior.Action.Add": "Add all select behavior", "Popup.Add.Behavior.Select.Counter": "Selected {count} behavior", + "Popup.Add.Behavior.Select.Nodata": "Could not find behavior named \"{name}\"", "Popup.Behavior.Info.Title": "Behavior details: {behavior}", "Popup.Behavior.Info.Confirm": "OK, I know it", "Build.In.Label.Name.All.Group": "All group", diff --git a/source/Localization/ZH-CN.ts b/source/Localization/ZH-CN.ts index 7fe2ba6..95e36b5 100644 --- a/source/Localization/ZH-CN.ts +++ b/source/Localization/ZH-CN.ts @@ -54,7 +54,8 @@ const ZH_CN = { "Popup.Setting.Title": "首选项设置", "Popup.Add.Behavior.Title": "添加行为", "Popup.Add.Behavior.Action.Add": "添加全部选中行为", - "Popup.Add.Behavior.Select.Counter": "已选择 {count} 个行为", + "Popup.Add.Behavior.Select.Counter": "找不到名为 \"{name}\" 的行为", + "Popup.Add.Behavior.Select.Nodata": "Could not find behavior named \"{name}\"", "Popup.Behavior.Info.Title": "行为详情: {behavior}", "Popup.Behavior.Info.Confirm": "好的, 我知道了", "Build.In.Label.Name.All.Group": "全部群",