From 255aea37526fea990905e6b8d0cecf60db2653a7 Mon Sep 17 00:00:00 2001 From: Linho Date: Fri, 25 Apr 2025 12:54:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/types.ts | 32 +++++++++++++++++++++++ src/typesn.ts | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/typesn.ts diff --git a/src/types.ts b/src/types.ts index 986b390..0032918 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,3 +7,35 @@ type NormalizeObj = { [K in keyof T]-?: NormalizeProp } +type TextBox = { + type?: "text" | "number"; + placeholder: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; +}; + +type Caption = { + text: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; + color?: "normal" | "dimmed" | "highlighted"; +} + +type TextField = { + type: 'textfield'; + key: string; + caption: string | Caption; + textbox: TextBox; +}; + +type MultiTextField = { + type: 'multiTextfield'; + key: string; + array: (TextBox & { index: number } | Caption)[] +}; + +type SwitchField = { + type: 'switch'; + key: string; + caption: string | Caption; +} \ No newline at end of file diff --git a/src/typesn.ts b/src/typesn.ts new file mode 100644 index 0000000..3fd5dd9 --- /dev/null +++ b/src/typesn.ts @@ -0,0 +1,71 @@ +import { InternalDatum } from "./consts"; + +type Selector<_T, _U> = any; +type Loose = T | U; +type Strict = T; + +type GraphTypeObj = { + value: string; + label: string; + isDefault?: boolean; +}; + +type GraphTypeMap = Map; + +type TextBox = { + type: Selection<"text" | "number">; + placeholder: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; +}; + + +type Caption = { + text: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; + color?: "normal" | "dimmed" | "highlighted"; +} + +type TextField = { + key: string; + caption: string | Caption; +}; + +function normalizeTextField(field: TextField) { + function normalizeCaption(caption: Caption) { + return { + ...caption, + style: caption.style ?? "normal", + size: caption.size ?? "normal", + color: caption.color ?? "normal", + }; + } + type StrictCaption = ReturnType; + return { + ...field, + caption: normalizeCaption( + typeof field.caption === "string" + ? { text: field.caption } + : field.caption + ) as StrictCaption, + }; +} + +type MultiTextField = { + key: string; +}; + +type Field = {}; + +type FnTypeObj = { + value: string; + label: string; + isDefault?: boolean; + allowedGraphTypes: GraphTypeObj[]; + requiredFields: Field[]; + optionalFields: Field[]; + extraProcess?: (item: InternalDatum) => void; +}; + +type FnTypeMap = Map; From 4cff4ea6f3555ffb9654d2123a55940c0b43235f Mon Sep 17 00:00:00 2001 From: Linho Date: Fri, 25 Apr 2025 18:20:06 +0800 Subject: [PATCH 2/2] Update types.ts --- src/types.ts | 158 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 29 deletions(-) diff --git a/src/types.ts b/src/types.ts index 0032918..e890504 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,41 +1,141 @@ +import { InternalDatum } from "./consts"; -type NormalizeProp = - (T extends object ? true : false) extends false ? T : // 全原始类型 - T extends object ? NormalizeObj : never // 含对象则归一为对象 +type NormalizeProp = (T extends object ? true : false) extends false + ? T // 全原始类型 + : T extends object + ? NormalizeObj + : never; // 含对象则归一为对象 type NormalizeObj = { - [K in keyof T]-?: NormalizeProp + [K in keyof T]-?: NormalizeProp; +}; + +namespace FieldType { + type TextBox = { + type?: "text" | "number"; + placeholder: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; + }; + + type Caption = { + text: string; + style?: "normal" | "math" | "monospace"; + size?: "normal" | "small" | "large"; + color?: "normal" | "dimmed" | "highlighted"; + }; + + export type Text = { + type: "textfield"; + key: string; + caption: string | Caption; + textbox: TextBox; + }; + + export type MultiText = { + type: "multiTextfield"; + key: string; + array: ((TextBox & { index: number }) | Caption)[]; + }; + + export type Switch = { + type: "switchfield"; + key: string; + caption: string | Caption; + }; + + export type MultiLine = { + type: "multilinefield"; + key: string; + array: ((TextBox & { index: number }) | Caption)[]; + }; } -type TextBox = { - type?: "text" | "number"; - placeholder: string; - style?: "normal" | "math" | "monospace"; - size?: "normal" | "small" | "large"; +type Field = + | FieldType.Text + | FieldType.MultiText + | FieldType.Switch + | FieldType.MultiLine; + +type GraphTypeObj = { + value: string; + label: string; + isDefault?: boolean; }; -type Caption = { - text: string; - style?: "normal" | "math" | "monospace"; - size?: "normal" | "small" | "large"; - color?: "normal" | "dimmed" | "highlighted"; -} +type GraphTypeMap = Map; -type TextField = { - type: 'textfield'; - key: string; - caption: string | Caption; - textbox: TextBox; +type FnTypeObj = { + value: string; + label: string; + isDefault?: boolean; + allowedGraphTypes: GraphTypeObj[]; + requiredFields: Field[]; + optionalFields?: Field[]; + extraProcess?: (item: InternalDatum) => void; }; -type MultiTextField = { - type: 'multiTextfield'; - key: string; - array: (TextBox & { index: number } | Caption)[] +type StrictFnTypeObj = NormalizeObj & { + allowedGraphTypeMap: GraphTypeMap; }; -type SwitchField = { - type: 'switch'; - key: string; - caption: string | Caption; -} \ No newline at end of file +type FnTypeMap = Map; + +const fnTypeObj: FnTypeObj[] = [ + { + isDefault: true, + value: "linear", + label: "fnType.linear", + allowedGraphTypes: [ + { value: "polyline", label: "graphType.polyline" }, + { value: "interval", label: "graphType.interval", isDefault: true }, + { value: "scatter", label: "graphType.scatter" }, + ], + requiredFields: [ + { + type: "textfield", + key: "fn", + caption: { + text: "y=", + style: "math", + }, + textbox: { + placeholder: "f(x)", + style: "math", + }, + }, + ], + optionalFields: [ + { + type: "multiTextfield", + key: "range", + array: [ + { + text: "x ∈ [", + style: "math", + }, + { + type: "number", + placeholder: "min(x)", + style: "math", + index: 0, + }, + { + text: ",", + style: "math", + }, + { + type: "number", + placeholder: "max(x)", + style: "math", + index: 0, + }, + { + text: "]", + style: "math", + }, + ], + }, + ], + }, +];