port.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Port
  2. - [WorkflowNodePortsData](https://github.com/bytedance/flowgram.ai/blob/main/packages/canvas-engine/free-layout-core/src/entity-datas/workflow-node-ports-data.ts) manages all port information for nodes
  3. - [WorkflowPortEntity](https://github.com/bytedance/flowgram.ai/blob/main/packages/canvas-engine/free-layout-core/src/entities/workflow-port-entity.ts) port instance
  4. - [WorkflowPortRender](https://github.com/bytedance/flowgram.ai/blob/main/packages/plugins/free-lines-plugin/src/components/workflow-port-render/index.tsx) port rendering component
  5. ## Define Ports
  6. - Static Ports
  7. Add `defaultPorts` to node declaration, such as `{ type: 'input' }`, which will add an input port to the left side of the node
  8. ```ts pure title="node-registries.ts"
  9. {
  10. type: 'start',
  11. meta: {
  12. defaultPorts: [{ type: 'output' }, { type: 'input'}]
  13. },
  14. }
  15. ```
  16. - Dynamic Ports
  17. Add `dynamicPorts` to node declaration, when set to true it will look for DOM elements with `data-port-id` and `data-port-type` attributes as ports
  18. ```ts pure title="node-registries.ts"
  19. {
  20. type: 'condition',
  21. meta: {
  22. defaultPorts: [{ type: 'input'}]
  23. dynamicPort: true
  24. },
  25. }
  26. ```
  27. ```tsx pure
  28. /**
  29. * Dynamic ports are found through querySelectorAll('[data-port-id]')
  30. */
  31. function BaseNode() {
  32. return (
  33. <div>
  34. <div data-port-id="condition-if-0" data-port-type="output"></div>
  35. <div data-port-id="condition-if-1" data-port-type="output"></div>
  36. {/* others */}
  37. </div>
  38. )
  39. }
  40. ```
  41. ## Port Rendering
  42. Ports are ultimately rendered through the `WorkflowPortRender` component, supporting custom styles, or businesses can reimplement this component based on the source code, see [Free Layout Best Practices - Node Rendering](https://github.com/bytedance/flowgram.ai/blob/main/apps/demo-free-layout/src/components/base-node/node-wrapper.tsx)
  43. ### Custom Port Colors
  44. You can customize port colors by passing color props to `WorkflowPortRender`:
  45. - `primaryColor` - Active state color (when linked or hovered)
  46. - `secondaryColor` - Default state color
  47. - `errorColor` - Error state color
  48. - `backgroundColor` - Background color
  49. ```tsx pure
  50. import { WorkflowPortRender, useNodeRender } from '@flowgram.ai/free-layout-editor';
  51. function BaseNode() {
  52. const { ports } = useNodeRender();
  53. return (
  54. <div>
  55. <div data-port-id="condition-if-0" data-port-type="output"></div>
  56. <div data-port-id="condition-if-1" data-port-type="output"></div>
  57. {ports.map((p) => (
  58. <WorkflowPortRender
  59. key={p.id}
  60. entity={p}
  61. className="xxx"
  62. style={{ /* custom style */}}
  63. // Custom port colors
  64. primaryColor="#4d53e8" // Active state color (linked/hovered)
  65. secondaryColor="#9197f1" // Default state color
  66. errorColor="#ff4444" // Error state color
  67. backgroundColor="#ffffff" // Background color
  68. />
  69. ))}
  70. </div>
  71. )
  72. }
  73. ```
  74. ## Get Port Data
  75. ```ts pure
  76. const ports = node.getData(WorkflowNodePortsData)
  77. console.log(ports.inputPorts) // Get all input ports of the current node
  78. console.log(ports.outputPorts) // Get all output ports of the current node
  79. console.log(ports.inputPorts.map(port => port.availableLines)) // Find connected lines through ports
  80. ports.updateDynamicPorts() // When dynamic ports modify DOM structure or position, you can manually refresh port positions through this method (DOM rendering has delay, best to execute in useEffect or setTimeout)
  81. ```