-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add input & output handlers and some validation
Signed-off-by: Tyler Ohlsen <[email protected]>
- Loading branch information
Showing
7 changed files
with
197 additions
and
52 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
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; | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
/* | ||
* 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) => | ||
isValidConnection(connection, reactFlowInstance) | ||
} | ||
style={{ | ||
height: 10, | ||
width: 10, | ||
backgroundColor: 'black', | ||
top: position, | ||
}} | ||
/> | ||
</> | ||
</div> | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
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,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, 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) => | ||
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,58 @@ | ||
/* | ||
* 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; | ||
} | ||
} | ||
|
||
export function isValidConnection( | ||
connection: Connection, | ||
rfInstance: ReactFlowInstance | ||
): boolean { | ||
const sourceHandle = connection.sourceHandle; | ||
const targetHandle = connection.targetHandle; | ||
const targetNodeId = connection.target; | ||
|
||
const inputClass = sourceHandle || ''; | ||
// We store the output classes in a pipe-delimited string. Converting back to a list. | ||
const outputClasses = targetHandle?.split('|') || []; | ||
|
||
if (outputClasses?.includes(inputClass)) { | ||
const targetNode = rfInstance.getNode(targetNodeId || ''); | ||
if (targetNode) { | ||
// We pull out the relevant IComponentInput config, and check if it allows multiple connections. | ||
// We also check the existing edges in the ReactFlow state. | ||
// If there is an existing edge, and we don't allow multiple, we don't allow this connection. | ||
// For all other scenarios, we allow the connection. | ||
const inputConfig = targetNode.data.inputs.find( | ||
(input: IComponentInput) => input.baseClass === inputClass | ||
); | ||
const existingEdge = rfInstance | ||
.getEdges() | ||
.find((edge) => edge.targetHandle === targetHandle); | ||
if (existingEdge && inputConfig.acceptMultiple === false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} |
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