Yiwei Mao 7c6c7ab7a2
feat(variable): global variable + variable panel plugin in demo (#435)
* feat: global variable panel

* feat(variable): on any change to on list or any var change

* feat: fix layout add variable panel
2025-07-01 11:30:22 +00:00

41 lines
895 B
TypeScript

import { useEffect } from 'react';
import { JsonSchemaEditor, JsonSchemaUtils } from '@flowgram.ai/form-materials';
import {
BaseVariableField,
GlobalScope,
useRefresh,
useService,
} from '@flowgram.ai/fixed-layout-editor';
export function GlobalVariableEditor() {
const globalScope = useService(GlobalScope);
const refresh = useRefresh();
const globalVar = globalScope.getVar() as BaseVariableField;
useEffect(() => {
const disposable = globalScope.output.onVariableListChange(() => {
refresh();
});
return () => {
disposable.dispose();
};
}, []);
if (!globalVar) {
return;
}
const value = globalVar.type ? JsonSchemaUtils.astToSchema(globalVar.type) : { type: 'object' };
return (
<JsonSchemaEditor
value={value}
onChange={(_schema) => globalVar.updateType(JsonSchemaUtils.schemaToAST(_schema))}
/>
);
}