code-editor.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import React from 'react';
  6. import { Field } from '@flowgram.ai/fixed-layout-editor';
  7. import { FreeFormMetaStoryBuilder, FormHeader } from '../../free-form-meta-story-builder';
  8. const CodeEditor = React.lazy(() =>
  9. import('@flowgram.ai/form-materials').then((module) => ({
  10. default: module.CodeEditor,
  11. }))
  12. );
  13. const defaultTsCode = `// Here, you can retrieve input variables from the node using 'params' and output results using 'ret'.
  14. // 'params' has been correctly injected into the environment.
  15. // Here's an example of getting the value of the parameter named 'input' from the node input:
  16. // const input = params.input;
  17. // Here's an example of outputting a 'ret' object containing multiple data types:
  18. // const ret = { "name": 'Xiaoming', "hobbies": ["Reading", "Traveling"] };
  19. async function main({ params }) {
  20. // Build the output object
  21. const ret = {
  22. key0: params.input + params.input, // Concatenate the input parameter 'input' twice
  23. key1: ["hello", "world"], // Output an array
  24. key2: { // Output an Object
  25. key21: "hi"
  26. },
  27. };
  28. return ret;
  29. }`;
  30. export const BasicStory = () => (
  31. <FreeFormMetaStoryBuilder
  32. filterStartNode
  33. filterEndNode
  34. height={600}
  35. formMeta={{
  36. render: () => (
  37. <div style={{ maxWidth: 500 }}>
  38. <FormHeader />
  39. <Field<string | undefined> name="code_editor" defaultValue={defaultTsCode}>
  40. {({ field }) => (
  41. <CodeEditor
  42. value={field.value}
  43. onChange={(value) => field.onChange(value)}
  44. languageId="typescript"
  45. />
  46. )}
  47. </Field>
  48. </div>
  49. ),
  50. }}
  51. />
  52. );