-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tyler Ohlsen <[email protected]>
- Loading branch information
Showing
8 changed files
with
297 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './workspace'; |
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,10 @@ | ||
.reactflow-parent-wrapper { | ||
display: flex; | ||
flex-grow: 1; | ||
height: 100%; | ||
} | ||
|
||
.reactflow-parent-wrapper .reactflow-wrapper { | ||
flex-grow: 1; | ||
height: 100%; | ||
} |
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,141 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useRef, useContext, useCallback, useEffect } from 'react'; | ||
import ReactFlow, { | ||
Controls, | ||
Background, | ||
useNodesState, | ||
useEdgesState, | ||
addEdge, | ||
} from 'reactflow'; | ||
import { useSelector } from 'react-redux'; | ||
import { EuiFlexItem, EuiFlexGroup } from '@elastic/eui'; | ||
import { AppState, rfContext } from '../../store'; | ||
|
||
// styling | ||
import 'reactflow/dist/style.css'; | ||
import './reactflow-styles.scss'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface WorkspaceProps {} | ||
|
||
export function Workspace(props: WorkspaceProps) { | ||
const reactFlowWrapper = useRef(null); | ||
const { reactFlowInstance, setReactFlowInstance } = useContext(rfContext); | ||
|
||
// Fetching workspace state to populate the initial nodes/edges | ||
const storedComponents = useSelector( | ||
(state: AppState) => state.workspace.components | ||
); | ||
const storedEdges = useSelector((state: AppState) => state.workspace.edges); | ||
const [nodes, setNodes, onNodesChange] = useNodesState(storedComponents); | ||
const [edges, setEdges, onEdgesChange] = useEdgesState(storedEdges); | ||
|
||
const onConnect = useCallback( | ||
(params) => { | ||
setEdges((eds) => addEdge(params, eds)); | ||
}, | ||
// TODO: add customized logic to prevent connections based on the node's | ||
// allowed inputs. If allowed, update that node state as well with the added | ||
// connection details. | ||
[setEdges] | ||
); | ||
|
||
const onDragOver = useCallback((event) => { | ||
event.preventDefault(); | ||
event.dataTransfer.dropEffect = 'move'; | ||
}, []); | ||
|
||
const onDrop = useCallback( | ||
(event) => { | ||
event.preventDefault(); | ||
// Get the node info from the event metadata | ||
const nodeData = event.dataTransfer.getData('application/reactflow'); | ||
|
||
// check if the dropped element is valid | ||
if (typeof nodeData === 'undefined' || !nodeData) { | ||
return; | ||
} | ||
|
||
// Fetch bounds based on the ref'd div component, adjust as needed. | ||
// TODO: remove hardcoded bounds and fetch from a constant somewhere | ||
// @ts-ignore | ||
const reactFlowBounds = reactFlowWrapper.current.getBoundingClientRect(); | ||
// @ts-ignore | ||
const position = reactFlowInstance.project({ | ||
x: event.clientX - reactFlowBounds.left - 80, | ||
y: event.clientY - reactFlowBounds.top - 90, | ||
}); | ||
|
||
// TODO: remove hardcoded values when more component info is passed in the event. | ||
// Only keep the calculated 'positioning' field. | ||
const newNode = { | ||
// TODO: generate ID based on the node data maybe | ||
id: Date.now().toFixed(), | ||
type: nodeData.type, | ||
position, | ||
data: { label: nodeData.label }, | ||
style: { | ||
background: 'white', | ||
}, | ||
}; | ||
|
||
// TODO: add logic to add node into the redux datastore | ||
|
||
setNodes((nds) => nds.concat(newNode)); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[reactFlowInstance] | ||
); | ||
|
||
// Initialization hook | ||
useEffect(() => { | ||
// TODO: fetch the nodes/edges dynamically (loading existing flow, | ||
// creating fresh from template, creating blank template, etc.) | ||
// Will involve populating and/or fetching from redux store | ||
}, []); | ||
|
||
return ( | ||
<EuiFlexItem grow={true}> | ||
<EuiFlexGroup | ||
direction="column" | ||
gutterSize="m" | ||
justifyContent="spaceBetween" | ||
> | ||
<EuiFlexItem | ||
style={{ | ||
borderStyle: 'groove', | ||
borderColor: 'gray', | ||
borderWidth: '1px', | ||
}} | ||
> | ||
{/** | ||
* We have these wrapper divs & reactFlowWrapper ref to control and calculate the | ||
* ReactFlow bounds when calculating node positioning. | ||
*/} | ||
<div className="reactflow-parent-wrapper"> | ||
<div className="reactflow-wrapper" ref={reactFlowWrapper}> | ||
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
onConnect={onConnect} | ||
onInit={setReactFlowInstance} | ||
onDrop={onDrop} | ||
onDragOver={onDragOver} | ||
fitView | ||
> | ||
<Controls /> | ||
<Background /> | ||
</ReactFlow> | ||
</div> | ||
</div> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</EuiFlexItem> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './react_flow_context_provider'; |
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,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { createContext, useState } from 'react'; | ||
// import { useDispatch } from 'react-redux'; | ||
|
||
const initialValues = { | ||
reactFlowInstance: null, | ||
setReactFlowInstance: () => {}, | ||
deleteNode: (nodeId: string) => {}, | ||
deleteEdge: (edgeId: string) => {}, | ||
}; | ||
|
||
export const rfContext = createContext(initialValues); | ||
|
||
/** | ||
* This returns a provider from the rfContext context created above. The initial | ||
* values are set so any nested components can use useContext to access these | ||
* values. | ||
* | ||
* This is how we can manage ReactFlow context consistently across the various | ||
* nested child components. | ||
*/ | ||
export function ReactFlowContextProvider({ children }: any) { | ||
// const dispatch = useDispatch(); | ||
const [reactFlowInstance, setReactFlowInstance] = useState(null); | ||
|
||
const deleteNode = (nodeId: string) => { | ||
// reactFlowInstance.setNodes(...) | ||
}; | ||
|
||
const deleteEdge = (edgeId: string) => { | ||
// reactFlowInstance.setEdges(...) | ||
}; | ||
|
||
return ( | ||
<rfContext.Provider | ||
value={{ | ||
reactFlowInstance, | ||
// @ts-ignore | ||
setReactFlowInstance, | ||
deleteNode, | ||
deleteEdge, | ||
}} | ||
> | ||
{children} | ||
</rfContext.Provider> | ||
); | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
export * from './store'; | ||
export * from './reducers'; | ||
export * from './context'; |
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