-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add input and output handles to components (#49)
* Add input & output handlers and some validation Signed-off-by: Tyler Ohlsen <[email protected]> * Clean up IDs & base classes Signed-off-by: Tyler Ohlsen <[email protected]> * Clean up isValidConnection Signed-off-by: Tyler Ohlsen <[email protected]> * Enforce baseclass in input Signed-off-by: Tyler Ohlsen <[email protected]> --------- Signed-off-by: Tyler Ohlsen <[email protected]>
- Loading branch information
Showing
14 changed files
with
245 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,11 @@ | |
} | ||
|
||
.workspace { | ||
width: 50vh; | ||
width: 80vh; | ||
height: 50vh; | ||
padding: 0; | ||
} | ||
|
||
.workspace-component { | ||
width: 300px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
public/pages/workflow_detail/workspace_component/input_handle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState, useRef, useEffect, useContext } from 'react'; | ||
import { Connection, Handle, Position } from 'reactflow'; | ||
import { EuiText } from '@elastic/eui'; | ||
import { IComponent, IComponentInput } from '../../../component_types'; | ||
import { calculateHandlePosition, isValidConnection } from './utils'; | ||
import { rfContext } from '../../../store'; | ||
|
||
interface InputHandleProps { | ||
data: IComponent; | ||
input: IComponentInput; | ||
} | ||
|
||
export function InputHandle(props: InputHandleProps) { | ||
const ref = useRef(null); | ||
const { reactFlowInstance } = useContext(rfContext); | ||
const [position, setPosition] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
setPosition(calculateHandlePosition(ref)); | ||
}, [ref]); | ||
|
||
return ( | ||
<div ref={ref}> | ||
<> | ||
<EuiText textAlign="left">{props.input.label}</EuiText> | ||
<Handle | ||
type="target" | ||
id={props.input.baseClass} | ||
position={Position.Left} | ||
isValidConnection={(connection: Connection) => | ||
// @ts-ignore | ||
isValidConnection(connection, reactFlowInstance) | ||
} | ||
style={{ | ||
height: 10, | ||
width: 10, | ||
backgroundColor: 'black', | ||
top: position, | ||
}} | ||
/> | ||
</> | ||
</div> | ||
); | ||
} |
50 changes: 50 additions & 0 deletions
50
public/pages/workflow_detail/workspace_component/output_handle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState, useRef, useEffect, useContext } from 'react'; | ||
import { Connection, Handle, Position } from 'reactflow'; | ||
import { EuiText } from '@elastic/eui'; | ||
import { IComponent, IComponentOutput } from '../../../component_types'; | ||
import { calculateHandlePosition, isValidConnection } from './utils'; | ||
import { rfContext } from '../../../store'; | ||
|
||
interface OutputHandleProps { | ||
data: IComponent; | ||
output: IComponentOutput; | ||
} | ||
|
||
export function OutputHandle(props: OutputHandleProps) { | ||
const ref = useRef(null); | ||
const { reactFlowInstance } = useContext(rfContext); | ||
const [position, setPosition] = useState<number>(0); | ||
const outputClasses = props.output.baseClasses.join('|'); | ||
|
||
useEffect(() => { | ||
setPosition(calculateHandlePosition(ref)); | ||
}, [ref]); | ||
|
||
return ( | ||
<div ref={ref}> | ||
<> | ||
<EuiText textAlign="right">{props.output.label}</EuiText> | ||
<Handle | ||
type="source" | ||
id={outputClasses} | ||
position={Position.Right} | ||
isValidConnection={(connection: Connection) => | ||
// @ts-ignore | ||
isValidConnection(connection, reactFlowInstance) | ||
} | ||
style={{ | ||
height: 10, | ||
width: 10, | ||
backgroundColor: 'black', | ||
top: position, | ||
}} | ||
/> | ||
</> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Connection, ReactFlowInstance } from 'reactflow'; | ||
import { IComponentInput } from '../../../../common'; | ||
|
||
/** | ||
* Collection of utility functions for the workspace component | ||
*/ | ||
|
||
// Uses DOM elements to calculate where the handle should be placed | ||
// vertically on the ReactFlow component. offsetTop is the offset relative to the | ||
// parent element, and clientHeight is the element height including padding. | ||
// We can combine them to get the exact amount, in pixels. | ||
export function calculateHandlePosition(ref: any): number { | ||
if (ref.current && ref.current.offsetTop && ref.current.clientHeight) { | ||
return ref.current.offsetTop + ref.current.clientHeight / 2; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
// Validates that connections can only be made when the source and target classes align, and | ||
// that multiple connections to the same target handle are not allowed unless the input configuration | ||
// for that particular component allows for it. | ||
export function isValidConnection( | ||
connection: Connection, | ||
rfInstance: ReactFlowInstance | ||
): boolean { | ||
const sourceHandle = connection.sourceHandle; | ||
const targetHandle = connection.targetHandle; | ||
const targetNodeId = connection.target; | ||
|
||
// We store the output classes in a pipe-delimited string. Converting back to a list. | ||
const sourceClasses = sourceHandle?.split('|') || []; | ||
const targetClass = targetHandle || ''; | ||
|
||
if (sourceClasses?.includes(targetClass)) { | ||
const targetNode = rfInstance.getNode(targetNodeId || ''); | ||
if (targetNode) { | ||
const inputConfig = targetNode.data.inputs.find( | ||
(input: IComponentInput) => sourceClasses.includes(input.baseClass) | ||
) as IComponentInput; | ||
const existingEdge = rfInstance | ||
.getEdges() | ||
.find( | ||
(edge) => | ||
edge.target === targetNodeId && edge.targetHandle === targetHandle | ||
); | ||
if (existingEdge && inputConfig.acceptMultiple === false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.