From 1e614a6c1878502fd584ef1b4e5f6463692396bf Mon Sep 17 00:00:00 2001 From: codetypes Date: Fri, 19 Apr 2024 12:47:46 +0800 Subject: [PATCH] define node arg type --- src/components/inspector.tsx | 6 ++---- src/misc/b3type.ts | 2 ++ src/misc/b3util.ts | 26 ++++++++++++++------------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 3152aab..13d6852 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -1,5 +1,5 @@ import { EditNode, EditTree, FileTreeType, useWorkspace } from "@/contexts/workspace-context"; -import { NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type"; +import { NodeArgType, NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type"; import { Hotkey, isMacos } from "@/misc/keys"; import Path from "@/misc/path"; import { AutoComplete, Divider, Flex, Form, Input, InputNumber, Select, Switch } from "antd"; @@ -10,8 +10,6 @@ import Markdown from "react-markdown"; interface OptionType extends DefaultOptionType {} -type ArgType = "string" | "int" | "float" | "boolean" | "enum" | "code"; - export const Inspector: FC = () => { const workspace = { editing: useWorkspace((state) => state.editing), @@ -289,7 +287,7 @@ export const Inspector: FC = () => { {def.args.map((v) => { const required = v.type.indexOf("?") == -1; - const type = v.type.replace("?", "") as ArgType; + const type = v.type.replace("?", "") as NodeArgType; return ( { return data.path && data.id.toString() !== "1"; @@ -94,30 +94,31 @@ export const checkNodeData = (data: NodeModel | null | undefined) => { for (let i = 0; i < conf.args.length; i++) { const arg = conf.args[i]; if (arg.type.indexOf("?") === -1) { + const type = arg.type as NodeArgType; const value = data.args?.[arg.name]; - if (arg.type === "float") { + if (type === "float") { if (typeof value !== "number") { console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a number`); hasError = true; } - } else if (arg.type === "int") { + } else if (type === "int") { if (typeof value !== "number" || value !== Math.floor(value)) { console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a int`); hasError = true; } - } else if (arg.type === "string") { + } else if (type === "string") { if (!value || typeof value !== "string") { console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`); hasError = true; } - } else if (arg.type === "enum") { + } else if (type === "enum") { if (!arg.options?.find((option) => option.value === value)) { console.error( `check ${data.id}|${data.name}: '${arg.name}' must be one of option value` ); hasError = true; } - } else if (arg.type == "code") { + } else if (type == "code") { if (!value || typeof value !== "string") { console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`); hasError = true; @@ -239,24 +240,25 @@ export const checkTreeData = (data: TreeGraphData) => { for (let i = 0; i < conf.args.length; i++) { const arg = conf.args[i]; if (arg.type.indexOf("?") === -1) { + const type = arg.type as NodeArgType; const value = data.args?.[arg.name]; - if (arg.type === "float") { + if (type === "float") { if (typeof value !== "number") { return false; } - } else if (arg.type === "int") { + } else if (type === "int") { if (typeof value !== "number" || value !== Math.floor(value)) { return false; } - } else if (arg.type === "string") { + } else if (type === "string") { if (!value || typeof value !== "string") { return false; } - } else if (arg.type === "enum") { + } else if (type === "enum") { if (!arg.options?.find((option) => option.value === value)) { return false; } - } else if (arg.type == "code") { + } else if (type === "code") { if (!value || typeof value !== "string") { return false; }