global-variable-editor.tsx 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { useEffect } from 'react';
  6. import { JsonSchemaEditor, JsonSchemaUtils } from '@flowgram.ai/form-materials';
  7. import {
  8. BaseVariableField,
  9. GlobalScope,
  10. useRefresh,
  11. useService,
  12. } from '@flowgram.ai/fixed-layout-editor';
  13. export function GlobalVariableEditor() {
  14. const globalScope = useService(GlobalScope);
  15. const refresh = useRefresh();
  16. const globalVar = globalScope.getVar() as BaseVariableField;
  17. useEffect(() => {
  18. const disposable = globalScope.output.onVariableListChange(() => {
  19. refresh();
  20. });
  21. return () => {
  22. disposable.dispose();
  23. };
  24. }, []);
  25. if (!globalVar) {
  26. return;
  27. }
  28. const value = globalVar.type ? JsonSchemaUtils.astToSchema(globalVar.type) : { type: 'object' };
  29. return (
  30. <JsonSchemaEditor
  31. value={value}
  32. onChange={(_schema) => globalVar.updateType(JsonSchemaUtils.schemaToAST(_schema))}
  33. />
  34. );
  35. }