use-editor-props.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { useMemo } from 'react';
  2. import { defaultFixedSemiMaterials } from '@flowgram.ai/fixed-semi-materials';
  3. import { createMinimapPlugin } from '@flowgram.ai/minimap-plugin';
  4. import {
  5. type FixedLayoutProps,
  6. FlowDocumentJSON,
  7. FlowNodeRegistry,
  8. FlowTextKey,
  9. Field,
  10. FlowRendererKey,
  11. } from '@flowgram.ai/fixed-layout-editor';
  12. import { BaseNode } from '../components/base-node'
  13. import { BranchAdder } from '../components/branch-adder'
  14. import { NodeAdder } from '../components/node-adder';
  15. /** semi materials */
  16. export function useEditorProps(
  17. initialData: FlowDocumentJSON, // 初始化数据
  18. nodeRegistries: FlowNodeRegistry[], // 节点定义
  19. ): FixedLayoutProps {
  20. return useMemo<FixedLayoutProps>(
  21. () => ({
  22. /**
  23. * Whether to enable the background
  24. */
  25. background: true,
  26. /**
  27. * Whether it is read-only or not, the node cannot be dragged in read-only mode
  28. */
  29. readonly: false,
  30. /**
  31. * Initial data
  32. * 初始化数据
  33. */
  34. initialData,
  35. /**
  36. * 画布节点定义
  37. */
  38. nodeRegistries,
  39. /**
  40. * Get the default node registry, which will be merged with the 'nodeRegistries'
  41. * 提供默认的节点注册,这个会和 nodeRegistries 做合并
  42. */
  43. getNodeDefaultRegistry(type) {
  44. return {
  45. type,
  46. meta: {
  47. defaultExpanded: true,
  48. },
  49. formMeta: {
  50. /**
  51. * Render form
  52. */
  53. render: () => <>
  54. <Field<string> name="title">
  55. {({ field }) => <div className="demo-fixed-node-title">{field.value}</div>}
  56. </Field>
  57. <div className="demo-fixed-node-content">
  58. <Field<string> name="content">
  59. <input />
  60. </Field>
  61. </div>
  62. </>
  63. }
  64. };
  65. },
  66. /**
  67. * Materials, components can be customized based on the key
  68. * @see https://github.com/coze-dev/flowgram.ai/blob/main/packages/materials/fixed-semi-materials/src/components/index.tsx
  69. * 可以通过 key 自定义 UI 组件
  70. */
  71. materials: {
  72. renderNodes: {
  73. ...defaultFixedSemiMaterials,
  74. /**
  75. * Components can be customized based on key business-side requirements.
  76. * 这里可以根据 key 业务侧定制组件
  77. */
  78. [FlowRendererKey.ADDER]: NodeAdder,
  79. [FlowRendererKey.BRANCH_ADDER]: BranchAdder,
  80. // [FlowRendererKey.DRAG_NODE]: DragNode,
  81. },
  82. renderDefaultNode: BaseNode, // 节点渲染
  83. renderTexts: {
  84. [FlowTextKey.LOOP_END_TEXT]: 'loop end',
  85. [FlowTextKey.LOOP_TRAVERSE_TEXT]: 'looping',
  86. },
  87. },
  88. /**
  89. * Node engine enable, you can configure formMeta in the FlowNodeRegistry
  90. */
  91. nodeEngine: {
  92. enable: true,
  93. },
  94. history: {
  95. enable: true,
  96. enableChangeNode: true, // Listen Node engine data change
  97. onApply(ctx, opt) {
  98. // Listen change to trigger auto save
  99. // console.log('auto save: ', ctx.document.toJSON(), opt);
  100. },
  101. },
  102. /**
  103. * 画布初始化
  104. */
  105. onInit: ctx => {
  106. /**
  107. * Data can also be dynamically loaded via fromJSON
  108. * 也可以通过 fromJSON 动态加载数据
  109. */
  110. // ctx.document.fromJSON(initialData)
  111. console.log('---- Playground Init ----');
  112. },
  113. /**
  114. * 画布销毁
  115. */
  116. onDispose: () => {
  117. console.log('---- Playground Dispose ----');
  118. },
  119. plugins: () => [
  120. /**
  121. * Minimap plugin
  122. * 缩略图插件
  123. */
  124. createMinimapPlugin({
  125. disableLayer: true,
  126. enableDisplayAllNodes: true,
  127. canvasStyle: {
  128. canvasWidth: 182,
  129. canvasHeight: 102,
  130. canvasPadding: 50,
  131. canvasBackground: 'rgba(245, 245, 245, 1)',
  132. canvasBorderRadius: 10,
  133. viewportBackground: 'rgba(235, 235, 235, 1)',
  134. viewportBorderRadius: 4,
  135. viewportBorderColor: 'rgba(201, 201, 201, 1)',
  136. viewportBorderWidth: 1,
  137. viewportBorderDashLength: 2,
  138. nodeColor: 'rgba(255, 255, 255, 1)',
  139. nodeBorderRadius: 2,
  140. nodeBorderWidth: 0.145,
  141. nodeBorderColor: 'rgba(6, 7, 9, 0.10)',
  142. overlayColor: 'rgba(255, 255, 255, 0)',
  143. },
  144. inactiveDebounceTime: 1,
  145. }),
  146. ],
  147. }),
  148. [],
  149. );
  150. }