use-editor-props.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { useMemo } from 'react';
  6. import { createMinimapPlugin } from '@flowgram.ai/minimap-plugin';
  7. import { createFreeSnapPlugin } from '@flowgram.ai/free-snap-plugin';
  8. import {
  9. FreeLayoutProps,
  10. WorkflowNodeProps,
  11. WorkflowNodeRenderer,
  12. Field,
  13. useNodeRender,
  14. FlowNodeMeta,
  15. } from '@flowgram.ai/free-layout-editor';
  16. import { createContainerNodePlugin } from '@flowgram.ai/free-container-plugin';
  17. import { nodeRegistries } from '../nodes';
  18. import { initialData } from '../initial-data';
  19. export const useEditorProps = () =>
  20. useMemo<FreeLayoutProps>(
  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. * Node registries
  37. * 节点注册
  38. */
  39. nodeRegistries,
  40. /**
  41. * 节点数据转换, 由 ctx.document.fromJSON 调用
  42. * Node data transformation, called by ctx.document.fromJSON
  43. * @param node
  44. * @param json
  45. */
  46. fromNodeJSON(node, json) {
  47. return json;
  48. },
  49. /**
  50. * 节点数据转换, 由 ctx.document.toJSON 调用
  51. * Node data transformation, called by ctx.document.toJSON
  52. * @param node
  53. * @param json
  54. */
  55. toNodeJSON(node, json) {
  56. return json;
  57. },
  58. /**
  59. * Get the default node registry, which will be merged with the 'nodeRegistries'
  60. * 提供默认的节点注册,这个会和 nodeRegistries 做合并
  61. */
  62. getNodeDefaultRegistry(type) {
  63. return {
  64. type,
  65. meta: {
  66. defaultExpanded: true,
  67. },
  68. formMeta: {
  69. /**
  70. * Render form
  71. */
  72. render: () => (
  73. <>
  74. <Field<string> name="title">
  75. {({ field }) => <div className="demo-free-node-title">{field.value}</div>}
  76. </Field>
  77. <div className="demo-free-node-content">
  78. <Field<string> name="content">
  79. <input />
  80. </Field>
  81. </div>
  82. </>
  83. ),
  84. },
  85. };
  86. },
  87. materials: {
  88. /**
  89. * Render Node
  90. */
  91. renderDefaultNode: (props: WorkflowNodeProps) => {
  92. const { node, form } = useNodeRender();
  93. const meta = node.getNodeMeta<FlowNodeMeta>();
  94. return (
  95. <WorkflowNodeRenderer
  96. className="demo-free-node"
  97. node={props.node}
  98. style={meta.wrapperStyle}
  99. >
  100. {form?.render()}
  101. </WorkflowNodeRenderer>
  102. );
  103. },
  104. },
  105. /**
  106. * Content change
  107. */
  108. onContentChange(ctx, event) {
  109. console.log('Auto Save: ', event, ctx.document.toJSON());
  110. },
  111. canDeleteLine: (ctx, line) => {
  112. if (line.from?.flowNodeType === 'batch' && line.to?.flowNodeType === 'batch_function') {
  113. return false;
  114. }
  115. return true;
  116. },
  117. isHideArrowLine(ctx, line) {
  118. if (line.from?.flowNodeType === 'batch' && line.to?.flowNodeType === 'batch_function') {
  119. return true;
  120. }
  121. return false;
  122. },
  123. // /**
  124. // * Node engine enable, you can configure formMeta in the FlowNodeRegistry
  125. // */
  126. nodeEngine: {
  127. enable: true,
  128. },
  129. /**
  130. * Redo/Undo enable
  131. */
  132. history: {
  133. enable: true,
  134. enableChangeNode: true, // Listen Node engine data change
  135. },
  136. /**
  137. * Playground init
  138. */
  139. onInit: (ctx) => {},
  140. /**
  141. * Playground render
  142. */
  143. onAllLayersRendered(ctx) {
  144. // Fitview
  145. ctx.document.fitView(false);
  146. },
  147. /**
  148. * Playground dispose
  149. */
  150. onDispose() {
  151. console.log('---- Playground Dispose ----');
  152. },
  153. plugins: () => [
  154. /**
  155. * Minimap plugin
  156. * 缩略图插件
  157. */
  158. createMinimapPlugin({
  159. disableLayer: true,
  160. canvasStyle: {
  161. canvasWidth: 182,
  162. canvasHeight: 102,
  163. canvasPadding: 50,
  164. canvasBackground: 'rgba(245, 245, 245, 1)',
  165. canvasBorderRadius: 10,
  166. viewportBackground: 'rgba(235, 235, 235, 1)',
  167. viewportBorderRadius: 4,
  168. viewportBorderColor: 'rgba(201, 201, 201, 1)',
  169. viewportBorderWidth: 1,
  170. viewportBorderDashLength: 2,
  171. nodeColor: 'rgba(255, 255, 255, 1)',
  172. nodeBorderRadius: 2,
  173. nodeBorderWidth: 0.145,
  174. nodeBorderColor: 'rgba(6, 7, 9, 0.10)',
  175. overlayColor: 'rgba(255, 255, 255, 0)',
  176. },
  177. }),
  178. /**
  179. * Snap plugin
  180. * 自动对齐及辅助线插件
  181. */
  182. createFreeSnapPlugin({
  183. edgeColor: '#00B2B2',
  184. alignColor: '#00B2B2',
  185. edgeLineWidth: 1,
  186. alignLineWidth: 1,
  187. alignCrossWidth: 8,
  188. }),
  189. /**
  190. * This is used for the rendering of the loop node sub-canvas
  191. * 这个用于 loop 节点子画布的渲染
  192. */
  193. createContainerNodePlugin({}),
  194. ],
  195. }),
  196. []
  197. );