fix editing variable no not use group

This commit is contained in:
codetypes committed 2025-03-20 13:05:04 +08:00
1 parent fdc7a8d2d6
commit 8199a0d24e
4 files changed
+42 -44

No files matched your search

+7 -6
View File
@@ -1,5 +1,11 @@
{ {
"version": "1.8.0",
"name": "subtree1", "name": "subtree1",
"desc": "子树1",
"firstid": 1,
"group": [],
"import": [],
"declvar": [],
"root": { "root": {
"id": 1, "id": 1,
"name": "Sequence", "name": "Sequence",
@@ -20,10 +26,5 @@
} }
} }
] ]
}, }
"desc": "子树1",
"firstid": 1,
"group": [],
"import": [],
"declvar": []
} }
+3 -21
View File
@@ -21,21 +21,13 @@ import {
EditTree, EditTree,
useWorkspace, useWorkspace,
} from "../contexts/workspace-context"; } from "../contexts/workspace-context";
import { import { ImportDef, isExprType, NodeModel, TreeGraphData, TreeModel, VarDef } from "../misc/b3type";
ImportDef,
isExprType,
NodeModel,
TreeGraphData,
TreeModel,
VarDef,
VERSION,
} from "../misc/b3type";
import * as b3util from "../misc/b3util"; import * as b3util from "../misc/b3util";
import { message } from "../misc/hooks"; import { message } from "../misc/hooks";
import i18n from "../misc/i18n"; import i18n from "../misc/i18n";
import { Hotkey, isMacos, useKeyDown } from "../misc/keys"; import { Hotkey, isMacos, useKeyDown } from "../misc/keys";
import Path from "../misc/path"; import Path from "../misc/path";
import { mergeClassNames, readJson } from "../misc/util"; import { mergeClassNames, readJson, writeTree } from "../misc/util";
import "./register-node"; import "./register-node";
import { calcTreeDataSize } from "./register-node"; import { calcTreeDataSize } from "./register-node";
@@ -829,17 +821,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
editor.autoId = b3util.refreshTreeDataId(editor.root, editor.data.firstid); editor.autoId = b3util.refreshTreeDataId(editor.root, editor.data.firstid);
editor.data.root = b3util.createFileData(editor.root); editor.data.root = b3util.createFileData(editor.root);
editor.modifiedTime = Date.now(); editor.modifiedTime = Date.now();
fs.writeFileSync( writeTree(path, editor.data);
path,
JSON.stringify(
{
...editor.data,
version: VERSION,
},
null,
2
)
);
workspace.updateFileMeta(editor); workspace.updateFileMeta(editor);
editor.unsave = false; editor.unsave = false;
editor.graph.changeData(editor.root); editor.graph.changeData(editor.root);
+16 -15
View File
@@ -60,18 +60,17 @@ interface VarDefItem extends VarDef {
} }
interface VarDefItemProps { interface VarDefItemProps {
id: number; name: number;
disabled?: boolean; disabled?: boolean;
value?: VarDefItem; value?: VarDefItem;
onChange?: (vardef: VarDefItem) => void; onChange?: (vardef: VarDefItem) => void;
onRemove?: (index: number | number[]) => void; onRemove?: (name: number | number[]) => void;
} }
const VarDefItem: FC<VarDefItemProps> = ({ id, value, onChange, onRemove, disabled }) => { const VarDefItem: FC<VarDefItemProps> = ({ name, onChange, onRemove, disabled, ...props }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const form = useFormInstance(); const form = useFormInstance();
const [name, setName] = useState(value?.name ?? ""); const [value, setValue] = useState<VarDefItem>(props.value ?? { name: "", desc: "" });
const [desc, setDesc] = useState(value?.desc ?? "");
const { editing } = useWorkspace( const { editing } = useWorkspace(
useShallow((state) => ({ useShallow((state) => ({
editing: state.editing, editing: state.editing,
@@ -79,7 +78,7 @@ const VarDefItem: FC<VarDefItemProps> = ({ id, value, onChange, onRemove, disabl
); );
const onSubmit = () => { const onSubmit = () => {
onChange?.({ name, desc }); onChange?.(value);
form.submit(); form.submit();
}; };
@@ -108,24 +107,24 @@ const VarDefItem: FC<VarDefItemProps> = ({ id, value, onChange, onRemove, disabl
</div> </div>
<Input <Input
disabled={disabled} disabled={disabled}
value={name} value={value.name}
placeholder={t("tree.vars.name")} placeholder={t("tree.vars.name")}
onBlur={onSubmit} onBlur={onSubmit}
onChange={(e) => setName(e.target.value)} onChange={(e) => setValue({ ...value, name: e.target.value })}
/> />
<Input <Input
disabled={disabled} disabled={disabled}
value={desc} value={value.desc}
placeholder={t("tree.vars.desc")} placeholder={t("tree.vars.desc")}
onBlur={onSubmit} onBlur={onSubmit}
onChange={(e) => setDesc(e.target.value)} onChange={(e) => setValue({ ...value, desc: e.target.value })}
/> />
</Space.Compact> </Space.Compact>
{!disabled && ( {!disabled && (
<MinusCircleOutlined <MinusCircleOutlined
style={{ marginBottom: "6px" }} style={{ marginBottom: "6px" }}
onClick={() => { onClick={() => {
onRemove?.(id); onRemove?.(name);
form.submit(); form.submit();
}} }}
/> />
@@ -257,7 +256,9 @@ const TreeInspector: FC = () => {
desc: values.desc, desc: values.desc,
export: values.export, export: values.export,
firstid: Number(values.firstid), firstid: Number(values.firstid),
group: Array.from(new Set((values.group as GroupDef[]).map((v) => (v.value ? v.name : "")))) group: Array.from(
new Set(((values.group ?? []) as GroupDef[]).map((v) => (v.value ? v.name : "")))
)
.filter((v) => v) .filter((v) => v)
.sort((a, b) => a.localeCompare(b)), .sort((a, b) => a.localeCompare(b)),
declvar: (values.declvar as VarDef[]) declvar: (values.declvar as VarDef[])
@@ -369,7 +370,7 @@ const TreeInspector: FC = () => {
}, },
]} ]}
> >
<VarDefItem id={item.name} onRemove={remove} /> <VarDefItem name={item.name} onRemove={remove} />
</Form.Item> </Form.Item>
))} ))}
<Form.Item <Form.Item
@@ -405,7 +406,7 @@ const TreeInspector: FC = () => {
<div style={{ display: "flex", flexDirection: "column", rowGap: 0 }}> <div style={{ display: "flex", flexDirection: "column", rowGap: 0 }}>
{fields.map((item) => ( {fields.map((item) => (
<Form.Item key={item.key} name={item.name} style={{ marginBottom: 2 }}> <Form.Item key={item.key} name={item.name} style={{ marginBottom: 2 }}>
<VarDefItem id={item.name} disabled={true} /> <VarDefItem name={item.name} disabled={true} />
</Form.Item> </Form.Item>
))} ))}
</div> </div>
@@ -456,7 +457,7 @@ const TreeInspector: FC = () => {
<div style={{ display: "flex", flexDirection: "column", rowGap: 0 }}> <div style={{ display: "flex", flexDirection: "column", rowGap: 0 }}>
{vars.map((v) => ( {vars.map((v) => (
<Form.Item key={v.key} name={v.name} style={{ marginBottom: 2 }}> <Form.Item key={v.key} name={v.name} style={{ marginBottom: 2 }}>
<VarDefItem id={v.name} disabled={true} /> <VarDefItem name={v.name} disabled={true} />
</Form.Item> </Form.Item>
))} ))}
</div> </div>
+16 -2
View File
@@ -7,16 +7,30 @@ export const readJson = (path: string) => {
}; };
export const readTree = (path: string) => { export const readTree = (path: string) => {
const str = fs.readFileSync(path, "utf-8"); const data = readJson(path);
const data = JSON.parse(str) as TreeModel;
data.version = data.version ?? VERSION; data.version = data.version ?? VERSION;
data.firstid = data.firstid ?? 1; data.firstid = data.firstid ?? 1;
data.group = data.group || []; data.group = data.group || [];
data.import = data.import || []; data.import = data.import || [];
data.declvar = data.declvar || []; data.declvar = data.declvar || [];
data.root = data.root || {};
return data; return data;
}; };
export const writeTree = (path: string, data: TreeModel) => {
writeJson(path, {
version: VERSION,
name: data.name,
desc: data.desc,
firstid: data.firstid,
export: data.export,
group: data.group,
import: data.import,
declvar: data.declvar,
root: data.root,
});
};
export const writeJson = (path: string, data: unknown) => { export const writeJson = (path: string, data: unknown) => {
const str = JSON.stringify(data, undefined, 2); const str = JSON.stringify(data, undefined, 2);
fs.writeFileSync(path, str, "utf-8"); fs.writeFileSync(path, str, "utf-8");