variable-panel-plugin.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { ASTFactory, definePluginCreator, GlobalScope } from '@flowgram.ai/free-layout-editor';
  6. import { JsonSchemaUtils } from '@flowgram.ai/form-materials';
  7. import iconVariable from '../../assets/icon-variable.png';
  8. import { VariablePanelLayer } from './variable-panel-layer';
  9. const fetchMockVariableFromRemote = async () => {
  10. await new Promise((resolve) => setTimeout(resolve, 1000));
  11. return {
  12. type: 'object',
  13. properties: {
  14. userId: { type: 'string' },
  15. },
  16. };
  17. };
  18. export const createVariablePanelPlugin = definePluginCreator({
  19. onInit(ctx) {
  20. ctx.playground.registerLayer(VariablePanelLayer);
  21. // Fetch Global Variable
  22. fetchMockVariableFromRemote().then((v) => {
  23. ctx.get(GlobalScope).setVar(
  24. ASTFactory.createVariableDeclaration({
  25. key: 'global',
  26. meta: {
  27. title: 'Global',
  28. icon: iconVariable,
  29. },
  30. type: JsonSchemaUtils.schemaToAST(v),
  31. })
  32. );
  33. });
  34. },
  35. });