From 54c0ff7356f17d67bd1a4382ea5df87cf0f66572 Mon Sep 17 00:00:00 2001 From: codetypes Date: Tue, 23 Jul 2024 13:19:42 +0800 Subject: [PATCH] merge check NodeData and TreeGraphData --- public/locales/en.json | 1 + public/locales/zh.json | 1 + src/components/inspector.tsx | 15 +-- src/components/menu.tsx | 23 ++-- src/misc/b3util.ts | 199 ++++++++++++++++------------------- 5 files changed, 113 insertions(+), 126 deletions(-) diff --git a/public/locales/en.json b/public/locales/en.json index f72653c..02d8d63 100644 --- a/public/locales/en.json +++ b/public/locales/en.json @@ -9,6 +9,7 @@ "buildFailed": "Build failed, more information can be found in the console.", "cancel": "Cancel", "copy": "Copy", + "console": "Console", "cut": "Cut", "createProject": "Create Project", "delete": "Delete", diff --git a/public/locales/zh.json b/public/locales/zh.json index 19c8695..d24297a 100644 --- a/public/locales/zh.json +++ b/public/locales/zh.json @@ -9,6 +9,7 @@ "buildFailed": "构建出现错误,详情请查看控制台!", "cancel": "取消", "copy": "复制", + "console": "控制台", "cut": "替换", "createProject": "新建项目", "delete": "删除", diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 42caf92..59e0233 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -1,5 +1,6 @@ import { EditNode, EditTree, useWorkspace } from "@/contexts/workspace-context"; import { NodeArg, NodeArgType, NodeModel, TreeGraphData } from "@/misc/b3type"; +import { checkOneof } from "@/misc/b3util"; import { Hotkey, isMacos } from "@/misc/keys"; import { EditOutlined } from "@ant-design/icons"; import { @@ -337,12 +338,7 @@ const NodeInspector: FC = () => { const arg = def.args?.find((a) => a.oneof && v.startsWith(a.oneof)); if (arg) { const argName = `args.${arg.name}`; - const argValue = getFieldValue(argName) ?? ""; - value = value ?? ""; - if ( - (value === "" && argValue === "") || - (value !== "" && argValue !== "") - ) { + if (!checkOneof(getFieldValue(argName), value)) { if (!isFieldValidating(argName)) { setFieldValue(`input.${i}`, value); validateFields([argName]); @@ -420,12 +416,7 @@ const NodeInspector: FC = () => { ); } const inputName = `input.${idx}`; - const inputValue = getFieldValue(inputName) ?? ""; - value = value ?? ""; - if ( - (value === "" && inputValue === "") || - (value !== "" && inputValue !== "") - ) { + if (!checkOneof(getFieldValue(inputName), value)) { if (!isFieldValidating(inputName)) { setFieldValue(`args.${v.name}`, value); validateFields([inputName]); diff --git a/src/components/menu.tsx b/src/components/menu.tsx index ea11d12..0878ac4 100644 --- a/src/components/menu.tsx +++ b/src/components/menu.tsx @@ -214,22 +214,22 @@ export const Menu: FC = () => { }, }, { - id: 'menu.edit.cut', - label: t('cut'), + id: "menu.edit.cut", + label: t("cut"), enabled: enabled, accelerator: Hotkey.Cut, click: () => { - getFocusedWebContents()?.cut() - } + getFocusedWebContents()?.cut(); + }, }, { - id: 'menu.edit.selectAll', - label: t('selectAll'), + id: "menu.edit.selectAll", + label: t("selectAll"), enabled: enabled, accelerator: Hotkey.SelectAll, click: () => { - getFocusedWebContents()?.selectAll() - } + getFocusedWebContents()?.selectAll(); + }, }, { type: "separator" }, { @@ -273,6 +273,13 @@ export const Menu: FC = () => { getFocusedWebContents()?.openDevTools(); }, }, + { + id: "menu.view.console", + label: t("console"), + click: () => { + getFocusedWebContents()?.openDevTools(); + }, + }, { type: "separator" }, { id: "menu.view.actualSize", diff --git a/src/misc/b3util.ts b/src/misc/b3util.ts index 6211b41..4b03032 100644 --- a/src/misc/b3util.ts +++ b/src/misc/b3util.ts @@ -1,5 +1,5 @@ import { useWorkspace } from "@/contexts/workspace-context"; -import { NodeArg, NodeArgType, NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type"; +import { NodeArg, NodeArgType, NodeDef, NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type"; import * as fs from "fs"; import { message } from "./hooks"; import Path from "./path"; @@ -67,10 +67,97 @@ export const isNodeEqual = (node1: NodeModel, node2: NodeModel) => { return false; }; -const error = (data: NodeModel, msg: string) => { +const error = (data: NodeModel | TreeGraphData, msg: string) => { console.error(`check ${data.id}|${data.name}: ${msg}`); }; +const checkNodeArg = ( + data: NodeModel | TreeGraphData, + conf: NodeDef, + i: number, + verbose?: boolean +) => { + let hasError = false; + const arg = conf.args![i] as NodeArg; + const type = arg.type.replace("?", "") as NodeArgType; + const value = data.args?.[arg.name]; + if (type === "float") { + const isNumber = typeof value === "number"; + const isOptional = value === undefined && arg.type === "float?"; + if (!(isNumber || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not a number`); + } + hasError = true; + } + } else if (type === "int") { + const isInt = typeof value === "number" && value === Math.floor(value); + const isOptional = value === undefined && arg.type === "int?"; + if (!(isInt || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not a int`); + } + hasError = true; + } + } else if (type === "string") { + const isString = typeof value === "string" && value; + const isOptional = (value === undefined || value === "") && arg.type === "string?"; + if (!(isString || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not a string`); + } + hasError = true; + } + } else if (type === "enum") { + const isEnum = !!arg.options?.find((option) => option.value === value); + const isOptional = value === undefined && arg.type === "enum?"; + if (!(isEnum || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not a one of the option values`); + } + hasError = true; + } + } else if (type === "code") { + const isCode = typeof value === "string" && value; + const isOptional = (value === undefined || value === "") && arg.type === "code?"; + if (!(isCode || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not a string`); + } + hasError = true; + } + } else if (type == "json") { + const isJson = value !== undefined && value !== null && value !== ""; + const isOptional = arg.type === "json?"; + if (!(isJson || isOptional)) { + if (verbose) { + error(data, `'${arg.name}=${value}' is not an invalid object`); + } + hasError = true; + } + } + if (arg.oneof !== undefined) { + const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1; + if (!checkOneof(data.args?.[arg.name], data.input?.[idx])) { + if (verbose) { + error( + data, + `only one is allowed for between argument '${arg.name}' and input '${data.input?.[idx]}'` + ); + } + hasError = true; + } + } + + return !hasError; +}; + +export const checkOneof = (argValue: unknown, inputValue: unknown) => { + argValue = argValue ?? ""; + inputValue = inputValue ?? ""; + return (argValue !== "" && inputValue === "") || (argValue === "" && inputValue !== ""); +}; + export const checkNodeData = (data: NodeModel | null | undefined) => { if (!data) { return false; @@ -120,63 +207,8 @@ export const checkNodeData = (data: NodeModel | null | undefined) => { } if (conf.args) { for (let i = 0; i < conf.args.length; i++) { - const arg = conf.args[i] as NodeArg; - const type = arg.type.replace("?", "") as NodeArgType; - const value = data.args?.[arg.name]; - if (type === "float") { - const isNumber = typeof value === "number"; - const isOptional = value === undefined && arg.type === "float?"; - if (!(isNumber || isOptional)) { - error(data, `'${arg.name}=${value}' is not a number`); - hasError = true; - } - } else if (type === "int") { - const isInt = typeof value === "number" && value === Math.floor(value); - const isOptional = value === undefined && arg.type === "int?"; - if (!(isInt || isOptional)) { - error(data, `'${arg.name}=${value}' is not a int`); - hasError = true; - } - } else if (type === "string") { - const isString = typeof value === "string" && value; - const isOptional = (value === undefined || value === "") && arg.type === "string?"; - if (!(isString || isOptional)) { - error(data, `'${arg.name}=${value}' is not a string`); - hasError = true; - } - } else if (type === "enum") { - const isEnum = !!arg.options?.find((option) => option.value === value); - const isOptional = value === undefined && arg.type === "enum?"; - if (!(isEnum || isOptional)) { - error(data, `'${arg.name}=${value}' is not a one of the option values`); - hasError = true; - } - } else if (type == "code") { - const isCode = typeof value === "string" && value; - const isOptional = (value === undefined || value === "") && arg.type === "code?"; - if (!(isCode || isOptional)) { - error(data, `'${arg.name}=${value}' is not a string`); - hasError = true; - } - } else if (type == "json") { - const isJson = value !== undefined && value !== null && value !== ""; - const isOptional = arg.type === "json?"; - if (!(isJson || isOptional)) { - error(data, `'${arg.name}=${value}' is not an invalid object`); - hasError = true; - } - } - if (arg.oneof !== undefined) { - const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1; - const inputValue = data.input?.[idx] ?? ""; - const argValue = data.args?.[arg.name] ?? ""; - if ((inputValue !== "" && argValue !== "") || (inputValue === "" && argValue === "")) { - error( - data, - `only one is allowed for between argument '${arg.name}' and input '${data.input?.[idx]}'` - ); - return false; - } + if (!checkNodeArg(data, conf, i, true)) { + hasError = true; } } } @@ -399,53 +431,8 @@ export const checkTreeData = (data: TreeGraphData) => { } if (conf.args) { for (let i = 0; i < conf.args.length; i++) { - const arg = conf.args[i] as NodeArg; - const type = arg.type.replace("?", "") as NodeArgType; - const value = data.args?.[arg.name]; - if (type === "float") { - const isNumber = typeof value === "number"; - const isOptional = value === undefined && arg.type === "float?"; - if (!(isNumber || isOptional)) { - return false; - } - } else if (type === "int") { - const isInt = typeof value === "number" && value === Math.floor(value); - const isOptional = value === undefined && arg.type === "int?"; - if (!(isInt || isOptional)) { - return false; - } - } else if (type === "string") { - const isString = typeof value === "string" && value; - const isOptional = (value === undefined || value === "") && arg.type === "string?"; - if (!(isString || isOptional)) { - return false; - } - } else if (type === "enum") { - const isEnum = !!arg.options?.find((option) => option.value === value); - const isOptional = value === undefined && arg.type === "enum?"; - if (!(isEnum || isOptional)) { - return false; - } - } else if (type === "code") { - const isCode = typeof value === "string" && value; - const isOptional = (value === undefined || value === "") && arg.type === "code?"; - if (!(isCode || isOptional)) { - return false; - } - } else if (type == "json") { - const isJson = value !== undefined && value !== null && value !== ""; - const isOptional = arg.type === "json?"; - if (!(isJson || isOptional)) { - return false; - } - } - if (arg.oneof !== undefined) { - const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1; - const inputValue = data.input?.[idx] ?? ""; - const argValue = data.args?.[arg.name] ?? ""; - if ((inputValue !== "" && argValue !== "") || (inputValue === "" && argValue === "")) { - return false; - } + if (!checkNodeArg(data, conf, i, false)) { + return false; } } }