define node arg type

This commit is contained in:
codetypes committed 2024-04-19 12:47:46 +08:00
1 parent 41a11dc9b5
commit 1e614a6c18
3 files changed
+18 -16

No files matched your search

+2 -4
View File
@@ -1,5 +1,5 @@
import { EditNode, EditTree, FileTreeType, useWorkspace } from "@/contexts/workspace-context"; 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 { Hotkey, isMacos } from "@/misc/keys";
import Path from "@/misc/path"; import Path from "@/misc/path";
import { AutoComplete, Divider, Flex, Form, Input, InputNumber, Select, Switch } from "antd"; import { AutoComplete, Divider, Flex, Form, Input, InputNumber, Select, Switch } from "antd";
@@ -10,8 +10,6 @@ import Markdown from "react-markdown";
interface OptionType extends DefaultOptionType {} interface OptionType extends DefaultOptionType {}
type ArgType = "string" | "int" | "float" | "boolean" | "enum" | "code";
export const Inspector: FC = () => { export const Inspector: FC = () => {
const workspace = { const workspace = {
editing: useWorkspace((state) => state.editing), editing: useWorkspace((state) => state.editing),
@@ -289,7 +287,7 @@ export const Inspector: FC = () => {
</Divider> </Divider>
{def.args.map((v) => { {def.args.map((v) => {
const required = v.type.indexOf("?") == -1; const required = v.type.indexOf("?") == -1;
const type = v.type.replace("?", "") as ArgType; const type = v.type.replace("?", "") as NodeArgType;
return ( return (
<Form.Item <Form.Item
name={`args.${v.name}`} name={`args.${v.name}`}
+2
View File
@@ -1,5 +1,7 @@
import { TreeGraphData as G6TreeGraphData } from "@antv/g6"; import { TreeGraphData as G6TreeGraphData } from "@antv/g6";
export type NodeArgType = "string" | "int" | "float" | "boolean" | "enum" | "code";
export interface NodeArgOption { export interface NodeArgOption {
name: string; name: string;
value: string | number; value: string | number;
+14 -12
View File
@@ -1,9 +1,9 @@
import { useWorkspace } from "@/contexts/workspace-context"; import { useWorkspace } from "@/contexts/workspace-context";
import { NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type"; import { NodeArgType, 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 i18n from "./i18n"; import i18n from "./i18n";
import Path from "./path";
export const isSubtreeRoot = (data: TreeGraphData) => { export const isSubtreeRoot = (data: TreeGraphData) => {
return data.path && data.id.toString() !== "1"; 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++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i]; const arg = conf.args[i];
if (arg.type.indexOf("?") === -1) { if (arg.type.indexOf("?") === -1) {
const type = arg.type as NodeArgType;
const value = data.args?.[arg.name]; const value = data.args?.[arg.name];
if (arg.type === "float") { if (type === "float") {
if (typeof value !== "number") { if (typeof value !== "number") {
console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a number`); console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a number`);
hasError = true; hasError = true;
} }
} else if (arg.type === "int") { } else if (type === "int") {
if (typeof value !== "number" || value !== Math.floor(value)) { if (typeof value !== "number" || value !== Math.floor(value)) {
console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a int`); console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a int`);
hasError = true; hasError = true;
} }
} else if (arg.type === "string") { } else if (type === "string") {
if (!value || typeof value !== "string") { if (!value || typeof value !== "string") {
console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`); console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`);
hasError = true; hasError = true;
} }
} else if (arg.type === "enum") { } else if (type === "enum") {
if (!arg.options?.find((option) => option.value === value)) { if (!arg.options?.find((option) => option.value === value)) {
console.error( console.error(
`check ${data.id}|${data.name}: '${arg.name}' must be one of option value` `check ${data.id}|${data.name}: '${arg.name}' must be one of option value`
); );
hasError = true; hasError = true;
} }
} else if (arg.type == "code") { } else if (type == "code") {
if (!value || typeof value !== "string") { if (!value || typeof value !== "string") {
console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`); console.error(`check ${data.id}|${data.name}: '${arg.name}' must be a string`);
hasError = true; hasError = true;
@@ -239,24 +240,25 @@ export const checkTreeData = (data: TreeGraphData) => {
for (let i = 0; i < conf.args.length; i++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i]; const arg = conf.args[i];
if (arg.type.indexOf("?") === -1) { if (arg.type.indexOf("?") === -1) {
const type = arg.type as NodeArgType;
const value = data.args?.[arg.name]; const value = data.args?.[arg.name];
if (arg.type === "float") { if (type === "float") {
if (typeof value !== "number") { if (typeof value !== "number") {
return false; return false;
} }
} else if (arg.type === "int") { } else if (type === "int") {
if (typeof value !== "number" || value !== Math.floor(value)) { if (typeof value !== "number" || value !== Math.floor(value)) {
return false; return false;
} }
} else if (arg.type === "string") { } else if (type === "string") {
if (!value || typeof value !== "string") { if (!value || typeof value !== "string") {
return false; return false;
} }
} else if (arg.type === "enum") { } else if (type === "enum") {
if (!arg.options?.find((option) => option.value === value)) { if (!arg.options?.find((option) => option.value === value)) {
return false; return false;
} }
} else if (arg.type == "code") { } else if (type === "code") {
if (!value || typeof value !== "string") { if (!value || typeof value !== "string") {
return false; return false;
} }