node.mdx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Node
  2. Nodes are defined through [FlowNodeEntity](/api/core/flow-node-entity.html)
  3. ## Node Core API
  4. - id: `string` Node id
  5. - flowNodeType: `string` | `number` Node type
  6. - bounds: `Rectangle` Get the node's x, y, width, height, equivalent to `transform.bounds`
  7. - blocks: `FlowNodeEntity[]` Get child nodes, including collapsed child nodes
  8. - parent: `FlowNodeEntity | undefined` Get the parent node (Such as loop)
  9. - document: [WorkflowDocument](/api/core/workflow-document.html) Document link
  10. - transform: [FlowNodeTransformData](https://flowgram.ai/auto-docs/document/classes/FlowNodeTransformData.html) Get the node's transform data
  11. - renderData: [FlowNodeRenderData](https://flowgram.ai/auto-docs/document/classes/FlowNodeRenderData.html) Get the node's render data, including render status
  12. - form: [NodeFormProps](https://flowgram.ai/auto-docs/editor/interfaces/NodeFormProps.html) Get the node's form data, like [getNodeForm](/api/utils/get-node-form.html)
  13. - scope: [FlowNodeScope](https://flowgram.ai/auto-docs/editor/interfaces/FlowNodeScope) Get the node's variable public scope
  14. - privateScope: [FlowNodeScope](https://flowgram.ai/auto-docs/editor/interfaces/FlowNodeScope) Get the node's variable private scope
  15. - lines: [WorkflowNodeLinesData](https://flowgram.ai/auto-docs/free-layout-core/classes/WorkflowNodeLinesData.html) Get the node's lines data
  16. - ports: [WorkflowNodePortsData](https://flowgram.ai/auto-docs/free-layout-core/classes/WorkflowNodePortsData.html) Get the node's ports data
  17. - getNodeRegistry(): [WorkflowNodeRegistry](https://flowgram.ai/auto-docs/free-layout-core/interfaces/WorkflowNodeRegistry) Get the node's registry
  18. - getService(): Get the [IOC](/guide/concepts/ioc.html) Service, such as`node.getService(HistoryService)`
  19. - getExtInfo(): Get the node's ext info, such as `node.getExtInfo<{ test: string }>()`
  20. - updateExtInfo(): Update the node's ext info, such as `node.updateExtInfo<{ test: string }>({ test: 'test' })`
  21. - dispose(): Destroy node
  22. - toJSON(): Get the node's json data
  23. ## Node Data
  24. Can be obtained through `node.toJSON()`
  25. :::note Basic Structure:
  26. - id: `string` Unique identifier for the node, must be unique
  27. - meta: `object` Node's UI configuration information, such as `position` information for free layout
  28. - type: `string | number` Node type, corresponds to `type` in `nodeRegistries`
  29. - data: `object` Node form data, can be customized by business
  30. - blocks: `array` Node branches, using `block` is more suitable for `Gramming` free layout scenarios, used in sub-nodes of sub-canvas
  31. - edges: `array` Edge data of sub-canvas
  32. :::
  33. ```ts pure
  34. const nodeData: FlowNodeJSON = {
  35. id: 'xxxx',
  36. type: 'condition',
  37. data: {
  38. title: 'MyCondition',
  39. desc: 'xxxxx'
  40. },
  41. }
  42. ```
  43. ## Node Definition
  44. In free layout scenarios, node definition is used to declare node's initial position/size, ports, form rendering, etc. For details, see [Declare Node](/guide/getting-started/create-free-layout-simple.html#4-declare-nodes)
  45. ## Get Current Rendering Node
  46. Get node-related methods through [useNodeRender](/api/hooks/use-node-render.html)
  47. ```tsx pure
  48. function BaseNode() {
  49. const { id, type, data, updateData, node } = useNodeRender()
  50. }
  51. ```
  52. ## Get Input/Output Nodes or Lines for Current Node
  53. ```ts pure
  54. // get input nodes (calculated through connection lines)
  55. node.lines.inputNodes
  56. // get all input nodes (recursively gets all upstream nodes)
  57. node.lines.allInputNodes
  58. // get output nodes
  59. node.lines.outputNodes
  60. // get all output nodes
  61. node.lines.allOutputNodes
  62. // input lines (contains the line that isDrawing or isHidden)
  63. node.lines.inputLines
  64. // output lines (contains the line that isDrawing or isHidden)
  65. node.lines.outputLines
  66. // all availableLines (Doesn't contain the lines that isDrawing or isHidden)
  67. node.lines.availableLines
  68. ```
  69. ## Create Node
  70. - Through [WorkflowDocument](/api/core/workflow-document.html)
  71. ```ts pure
  72. const ctx = useClientContext()
  73. ctx.document.createWorkflowNode({
  74. id: 'xxx', // Must be unique within the canvas
  75. type: 'custom',
  76. meta: {
  77. /**
  78. * If not provided, defaults to creating in the center of the canvas
  79. * To get position from mouse position (e.g., creating node by clicking anywhere on canvas),
  80. * convert using `ctx.playground.config.getPosFromMouseEvent(mouseEvent)`
  81. */
  82. position: { x: 100, y: 100 } //
  83. },
  84. data: {}, // Form-related data
  85. blocks: [], // Sub-canvas nodes
  86. edges: [] // Sub-canvas edges
  87. })
  88. ```
  89. - Through WorkflowDragService, see [Free Layout Basic Usage](/examples/free-layout/free-layout-simple.html)
  90. ```ts pure
  91. const dragService = useService<WorkflowDragService>(WorkflowDragService);
  92. // mouseEvent here will automatically convert to canvas position
  93. dragService.startDragCard(nodeType, mouseEvent, {
  94. id: 'xxxx',
  95. data: {}, // Form-related data
  96. blocks: [], // Sub-canvas nodes
  97. edges: [] // Sub-canvas edges
  98. })
  99. ```
  100. ## Delete Node
  101. Delete node through `node.dispose`
  102. ```tsx pure
  103. function BaseNode({ node }) {
  104. function onClick() {
  105. node.dispose()
  106. }
  107. return (
  108. <button onClick={onClick}>Delete</button>
  109. )
  110. }
  111. ```
  112. ## Update Node Data
  113. - Get node's data through [useNodeRender](/api/hooks/use-node-render.html) or [node.form](https://flowgram.ai/auto-docs/editor/interfaces/NodeFormProps.html)
  114. ```tsx pure
  115. function BaseNode() {
  116. const { form, node } = useNodeRender();
  117. // Corresponds to node's data
  118. // 1. form.values: Corresponds to node's data
  119. // 2. form.setValueIn('title', 'xxxx'): Update data.title
  120. // 3. form.getValueIn('title'): Get data.title
  121. // 4. form.updateFormValues({ ... }) Update all form values
  122. function onChange(e) {
  123. form.setValueIn('title', e.target.value)
  124. }
  125. return <input value={form.values.title} onChange={onChange}/>
  126. }
  127. ```
  128. - Update form data through Field, see details in [Form Usage](/guide/form/form.html)
  129. ```tsx pure
  130. function FormRender() {
  131. return (
  132. <Field name="title">
  133. <Input />
  134. </Field>
  135. )
  136. }
  137. ```
  138. ## Update Node's extInfo Data
  139. extInfo is used to store UI states, if node engine is not enabled, node's data will be stored in extInfo by default
  140. ```tsx pure
  141. function BaseNode({ node }) {
  142. const times = node.getExtInfo()?.times || 0
  143. function onClick() {
  144. node.updateExtInfo({ times: times ++ })
  145. }
  146. return (
  147. <div>
  148. <span>Click Times: {times}</span>
  149. <button onClick={onClick}>Click</button>
  150. </div>
  151. )
  152. }
  153. ```