merge check NodeData and TreeGraphData

This commit is contained in:
codetypes committed 2024-07-23 13:19:42 +08:00
1 parent 6d6fdf8c07
commit 54c0ff7356
5 files changed
+111 -124

No files matched your search

+1
View File
@@ -9,6 +9,7 @@
"buildFailed": "Build failed, more information can be found in the console.", "buildFailed": "Build failed, more information can be found in the console.",
"cancel": "Cancel", "cancel": "Cancel",
"copy": "Copy", "copy": "Copy",
"console": "Console",
"cut": "Cut", "cut": "Cut",
"createProject": "Create Project", "createProject": "Create Project",
"delete": "Delete", "delete": "Delete",
+1
View File
@@ -9,6 +9,7 @@
"buildFailed": "构建出现错误,详情请查看控制台!", "buildFailed": "构建出现错误,详情请查看控制台!",
"cancel": "取消", "cancel": "取消",
"copy": "复制", "copy": "复制",
"console": "控制台",
"cut": "替换", "cut": "替换",
"createProject": "新建项目", "createProject": "新建项目",
"delete": "删除", "delete": "删除",
+3 -12
View File
@@ -1,5 +1,6 @@
import { EditNode, EditTree, useWorkspace } from "@/contexts/workspace-context"; import { EditNode, EditTree, useWorkspace } from "@/contexts/workspace-context";
import { NodeArg, NodeArgType, NodeModel, TreeGraphData } from "@/misc/b3type"; import { NodeArg, NodeArgType, NodeModel, TreeGraphData } from "@/misc/b3type";
import { checkOneof } from "@/misc/b3util";
import { Hotkey, isMacos } from "@/misc/keys"; import { Hotkey, isMacos } from "@/misc/keys";
import { EditOutlined } from "@ant-design/icons"; import { EditOutlined } from "@ant-design/icons";
import { import {
@@ -337,12 +338,7 @@ const NodeInspector: FC = () => {
const arg = def.args?.find((a) => a.oneof && v.startsWith(a.oneof)); const arg = def.args?.find((a) => a.oneof && v.startsWith(a.oneof));
if (arg) { if (arg) {
const argName = `args.${arg.name}`; const argName = `args.${arg.name}`;
const argValue = getFieldValue(argName) ?? ""; if (!checkOneof(getFieldValue(argName), value)) {
value = value ?? "";
if (
(value === "" && argValue === "") ||
(value !== "" && argValue !== "")
) {
if (!isFieldValidating(argName)) { if (!isFieldValidating(argName)) {
setFieldValue(`input.${i}`, value); setFieldValue(`input.${i}`, value);
validateFields([argName]); validateFields([argName]);
@@ -420,12 +416,7 @@ const NodeInspector: FC = () => {
); );
} }
const inputName = `input.${idx}`; const inputName = `input.${idx}`;
const inputValue = getFieldValue(inputName) ?? ""; if (!checkOneof(getFieldValue(inputName), value)) {
value = value ?? "";
if (
(value === "" && inputValue === "") ||
(value !== "" && inputValue !== "")
) {
if (!isFieldValidating(inputName)) { if (!isFieldValidating(inputName)) {
setFieldValue(`args.${v.name}`, value); setFieldValue(`args.${v.name}`, value);
validateFields([inputName]); validateFields([inputName]);
+15 -8
View File
@@ -214,22 +214,22 @@ export const Menu: FC<LayoutProps> = () => {
}, },
}, },
{ {
id: 'menu.edit.cut', id: "menu.edit.cut",
label: t('cut'), label: t("cut"),
enabled: enabled, enabled: enabled,
accelerator: Hotkey.Cut, accelerator: Hotkey.Cut,
click: () => { click: () => {
getFocusedWebContents()?.cut() getFocusedWebContents()?.cut();
} },
}, },
{ {
id: 'menu.edit.selectAll', id: "menu.edit.selectAll",
label: t('selectAll'), label: t("selectAll"),
enabled: enabled, enabled: enabled,
accelerator: Hotkey.SelectAll, accelerator: Hotkey.SelectAll,
click: () => { click: () => {
getFocusedWebContents()?.selectAll() getFocusedWebContents()?.selectAll();
} },
}, },
{ type: "separator" }, { type: "separator" },
{ {
@@ -273,6 +273,13 @@ export const Menu: FC<LayoutProps> = () => {
getFocusedWebContents()?.openDevTools(); getFocusedWebContents()?.openDevTools();
}, },
}, },
{
id: "menu.view.console",
label: t("console"),
click: () => {
getFocusedWebContents()?.openDevTools();
},
},
{ type: "separator" }, { type: "separator" },
{ {
id: "menu.view.actualSize", id: "menu.view.actualSize",
+91 -104
View File
@@ -1,5 +1,5 @@
import { useWorkspace } from "@/contexts/workspace-context"; 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 * as fs from "fs";
import { message } from "./hooks"; import { message } from "./hooks";
import Path from "./path"; import Path from "./path";
@@ -67,10 +67,97 @@ export const isNodeEqual = (node1: NodeModel, node2: NodeModel) => {
return false; return false;
}; };
const error = (data: NodeModel, msg: string) => { const error = (data: NodeModel | TreeGraphData, msg: string) => {
console.error(`check ${data.id}|${data.name}: ${msg}`); 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) => { export const checkNodeData = (data: NodeModel | null | undefined) => {
if (!data) { if (!data) {
return false; return false;
@@ -120,64 +207,9 @@ export const checkNodeData = (data: NodeModel | null | undefined) => {
} }
if (conf.args) { if (conf.args) {
for (let i = 0; i < conf.args.length; i++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i] as NodeArg; if (!checkNodeArg(data, conf, i, true)) {
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; 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;
}
}
} }
} }
@@ -399,54 +431,9 @@ export const checkTreeData = (data: TreeGraphData) => {
} }
if (conf.args) { if (conf.args) {
for (let i = 0; i < conf.args.length; i++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i] as NodeArg; if (!checkNodeArg(data, conf, i, false)) {
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; 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;
}
}
} }
} }