use-editor-props.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { useMemo } from 'react';
  6. import {
  7. Field,
  8. FreeLayoutProps,
  9. PlaygroundConfigEntity,
  10. WorkflowLinesManager,
  11. } from '@flowgram.ai/free-layout-editor';
  12. import { updatePosition } from './update-position';
  13. import { NodeRender } from './node-render';
  14. import { nodeRegistries } from './node-registries';
  15. import { NodeColorMap } from './node-color';
  16. import { initialData } from './initial-data';
  17. export const useEditorProps = () =>
  18. useMemo<FreeLayoutProps>(
  19. () => ({
  20. background: false,
  21. materials: {
  22. renderDefaultNode: NodeRender,
  23. },
  24. isHideArrowLine: (ctx, line) => {
  25. if (line.from && line.to) {
  26. return true;
  27. }
  28. return false;
  29. },
  30. nodeRegistries,
  31. initialData,
  32. onInit: (ctx) => {
  33. const linesManager = ctx.get(WorkflowLinesManager);
  34. linesManager.getLineColor = (line) => {
  35. const lineColor = NodeColorMap[line.from?.id ?? line.to?.id ?? ''] ?? '#000';
  36. return lineColor;
  37. };
  38. },
  39. onAllLayersRendered: async (ctx) => {
  40. await ctx.tools.fitView(false);
  41. // disable playground operations
  42. const playgroundConfig = ctx.get(PlaygroundConfigEntity);
  43. playgroundConfig.updateConfig = () => {};
  44. // display logo container
  45. const containerDOM = window.document.querySelector('.flowgram-logo-container');
  46. if (containerDOM instanceof HTMLDivElement) {
  47. containerDOM.style.opacity = '1';
  48. }
  49. // update nodes position
  50. updatePosition(ctx);
  51. },
  52. getNodeDefaultRegistry(type) {
  53. return {
  54. type,
  55. meta: {
  56. defaultExpanded: true,
  57. },
  58. formMeta: {
  59. /**
  60. * Render form
  61. */
  62. render: () => (
  63. <>
  64. <Field<string> name="title">{({ field }) => <div>{field.value}</div>}</Field>
  65. </>
  66. ),
  67. },
  68. };
  69. },
  70. }),
  71. []
  72. );