argument with options treat as enum

This commit is contained in:
codetypes committed 2025-03-25 22:40:40 +08:00
1 parent 49bfc76fc6
commit 767d8aba95
14 files changed
+105 -133

No files matched your search

+21 -60
View File
@@ -8,7 +8,6 @@ import {
import {
AutoComplete,
Button,
Checkbox,
Divider,
Flex,
Form,
@@ -29,9 +28,9 @@ import { useShallow } from "zustand/react/shallow";
import { ExpressionEvaluator } from "../behavior3/src/behavior3";
import { EditNode, EditTree, useWorkspace } from "../contexts/workspace-context";
import {
hasArgOptions,
ImportDef,
isBoolType,
isEnumType,
isExprType,
isFloatType,
isIntType,
@@ -141,49 +140,6 @@ const VarDefItem: FC<VarItemProps> = ({ name, onChange, onRemove, disabled, ...p
);
};
interface GroupDefItemProps {
disabled?: boolean;
value?: string[];
onChange?: (value: string[]) => void;
}
const GroupDefItem: FC<GroupDefItemProps> = ({ disabled, onChange, ...props }) => {
const [value, setValue] = useState<string[]>(props.value ?? []);
const form = useFormInstance();
const workspace = useWorkspace(
useShallow((state) => ({
groupDefs: state.groupDefs,
}))
);
return (
<Flex
style={{
display: "flex",
flexDirection: "row",
gap: 10,
rowGap: 2,
flexWrap: "wrap",
}}
>
{workspace.groupDefs.map((g) => (
<Checkbox
key={g}
disabled={disabled}
checked={value.includes(g)}
onChange={(e) => {
const arr = e.target.checked ? [...value, g] : value.filter((v) => v !== g);
setValue(arr);
onChange?.(arr);
form.submit();
}}
>
{g}
</Checkbox>
))}
</Flex>
);
};
const TreeInspector: FC = () => {
const workspace = useWorkspace(
useShallow((state) => ({
@@ -360,6 +316,7 @@ const TreeInspector: FC = () => {
mode="multiple"
suffixIcon={null}
onChange={form.submit}
placeholder={t("tree.group.placeholder")}
options={workspace.groupDefs.map((g) => ({ label: g, value: g }))}
/>
</Form.Item>
@@ -1160,29 +1117,29 @@ const NodeInspector: FC = () => {
}),
]}
>
{isStringType(type) && (
{!hasArgOptions(arg) && isStringType(type) && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} />
)}
{isJsonType(type) && (
{!hasArgOptions(arg) && isJsonType(type) && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} />
)}
{isIntType(type) && (
{!hasArgOptions(arg) && isIntType(type) && (
<InputNumber
disabled={disabled}
onBlur={form.submit}
precision={0}
/>
)}
{isFloatType(type) && (
{!hasArgOptions(arg) && isFloatType(type) && (
<InputNumber disabled={disabled} onBlur={form.submit} />
)}
{isBoolType(type) && (
{!hasArgOptions(arg) && isBoolType(type) && (
<Switch disabled={disabled} onChange={form.submit} />
)}
{isExprType(type) && (
{!hasArgOptions(arg) && isExprType(type) && (
<Input disabled={disabled} onBlur={form.submit} />
)}
{isEnumType(type) && (
{hasArgOptions(arg) && (
<Select
showSearch
disabled={disabled}
@@ -1289,21 +1246,25 @@ const NodeInspector: FC = () => {
}),
]}
>
{isStringType(type) && (
{!hasArgOptions(arg) && isStringType(type) && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} />
)}
{isJsonType(type) && (
{!hasArgOptions(arg) && isJsonType(type) && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} />
)}
{isIntType(type) && (
{!hasArgOptions(arg) && isIntType(type) && (
<InputNumber disabled={disabled} onBlur={form.submit} precision={0} />
)}
{isFloatType(type) && (
{!hasArgOptions(arg) && isFloatType(type) && (
<InputNumber disabled={disabled} onBlur={form.submit} />
)}
{isBoolType(type) && <Switch disabled={disabled} onChange={form.submit} />}
{isExprType(type) && <Input disabled={disabled} onBlur={form.submit} />}
{isEnumType(type) && (
{!hasArgOptions(arg) && isBoolType(type) && (
<Switch disabled={disabled} onChange={form.submit} />
)}
{!hasArgOptions(arg) && isExprType(type) && (
<Input disabled={disabled} onBlur={form.submit} />
)}
{hasArgOptions(arg) && (
<Select
showSearch
disabled={disabled}
@@ -1584,7 +1545,7 @@ const NodeDefInspector: FC = () => {
rules={[{ required }]}
>
<Select disabled={true}>
{["float", "int", "string", "code", "enum", "boolean"].map((value) => {
{["float", "int", "string", "code", "boolean"].map((value) => {
return (
<Select.Option key={value} value={value}>
{value}
+2 -2
View File
@@ -291,7 +291,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
const workspace = get();
let buildScript = dialog.showOpenDialogSync({
properties: ["openFile"],
defaultPath: workspace.workdir,
defaultPath: workspace.workdir.replaceAll("/", Path.sep),
filters: [{ name: "Javascript", extensions: ["js"] }],
})?.[0];
if (buildScript) {
@@ -412,7 +412,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
const workspace = get();
const scriptPath = dialog.showOpenDialogSync({
properties: ["openFile"],
defaultPath: workspace.workdir,
defaultPath: workspace.workdir.replaceAll("/", Path.sep),
filters: [{ name: "Javascript", extensions: ["js"] }],
})?.[0];
if (scriptPath) {
+2 -8
View File
@@ -1,13 +1,7 @@
import type { TreeGraphData as G6TreeGraphData } from "@antv/g6";
import { NodeDef } from "../behavior3/src/behavior3";
declare module "../behavior3/src/behavior3" {
interface NodeDef {
group?: string[];
}
}
export const VERSION = "1.8.0";
export const VERSION = "1.8.1";
export type NodeType = NodeDef["type"] | "Other" | "Error";
export type NodeArg = Exclude<NodeDef["args"], undefined>[number];
@@ -16,9 +10,9 @@ export const isIntType = (type: string) => type.startsWith("int");
export const isFloatType = (type: string) => type.startsWith("float");
export const isStringType = (type: string) => type.startsWith("string");
export const isBoolType = (type: string) => type.startsWith("bool");
export const isEnumType = (type: string) => type.startsWith("enum");
export const isExprType = (type: string) => type.startsWith("expr") || type.startsWith("code");
export const isJsonType = (type: string) => type.startsWith("json");
export const hasArgOptions = (arg: NodeArg) => arg.options !== undefined;
export interface NodeModel {
id: number;
+13 -17
View File
@@ -2,9 +2,9 @@ import * as fs from "fs";
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
import {
FileVarDecl,
hasArgOptions,
ImportDef,
isBoolType,
isEnumType,
isExprType,
isFloatType,
isIntType,
@@ -67,13 +67,7 @@ export const initWorkdir = (path: string, handler: typeof alertError) => {
nodeDefs = new NodeDefs();
for (const v of nodeDefData) {
nodeDefs.set(v.name, v);
const group = v.type.match(/\(([\w|]+)\)/)?.[1];
if (group) {
v.type = v.type.match(/^\w+/)![0] as NodeDef["type"];
v.group = group.split("|");
v.group.forEach((g) => groups.add(g));
}
v.group?.forEach((g) => groups.add(g));
}
groupDefs = Array.from(groups).sort();
};
@@ -218,15 +212,6 @@ export const checkNodeArgValue = (
}
hasError = true;
}
} else if (isEnumType(type)) {
const isEnum = !!arg.options?.find((option) => option.value === value);
const isOptional = value === undefined && isNodeArgOptional(arg);
if (!(isEnum || isOptional)) {
if (verbose) {
error(data, `'${arg.name}=${JSON.stringify(value)}' is not a one of the option values`);
}
hasError = true;
}
} else if (isExprType(type)) {
const isExpr = typeof value === "string" && value;
const isOptional = (value === undefined || value === "") && isNodeArgOptional(arg);
@@ -257,6 +242,17 @@ export const checkNodeArgValue = (
error(data, `unknown arg type '${arg.type}'`);
}
if (hasArgOptions(arg)) {
const found = !!arg.options?.find((option) => option.value === value);
const isOptional = value === undefined && isNodeArgOptional(arg);
if (!(found || isOptional)) {
if (verbose) {
error(data, `'${arg.name}=${JSON.stringify(value)}' is not a one of the option values`);
}
hasError = true;
}
}
return !hasError;
};