Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions for react-flow upgrade #832

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions workspace/src/app/views/shared/ReactFlow/ReactFlow.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {Edge, Node} from "react-flow-renderer-lts"
import {ReactFlowElement} from "./typing";

/** The elements separated into nodes and edges as its usual starting from react-flow-renderer v10.x */
interface ReactFlowElements {
nodes: Node[]
edges: Edge[]
}

const isNode = (element: ReactFlowElement & { source?: string }): boolean => !element.source;

/** Remove elements from another list of elements. All edges of removed nodes are also removed. */
const removeElements = (elementsToRemove: ReactFlowElement[], elements: ReactFlowElements): ReactFlowElements => {
const newElements = {...elements}
const nodesToRemove = new Set<string>()
const edgesToRemove = new Set<string>()
elementsToRemove.forEach(elem => {
if(isNode(elem)) {
nodesToRemove.add(elem.id)
} else {
edgesToRemove.add(elem.id)
}
})
if(nodesToRemove.size) {
newElements.nodes = elements.nodes.filter(node => !nodesToRemove.has(node.id))
}
if(edgesToRemove.size) {
newElements.edges = elements.edges.filter(edge =>
!edgesToRemove.has(edge.id) && !nodesToRemove.has(edge.source) && !nodesToRemove.has(edge.target))
}
return newElements
}

/** Remove a list of edges from another list of edges. */
const removeEdges = (edgesToRemove: Edge[], edges: Edge[]): Edge[] => {
const edgesToRemoveSet = new Set<string>()
edgesToRemove.forEach(edge => edgesToRemoveSet.add(edge.id))
if(edgesToRemoveSet.size) {
return edges.filter(edge => !edgesToRemoveSet.has(edge.id))
} else {
return [...edges]
}
}

const utils = {
isNode,
removeElements,
removeEdges
}

export default utils
4 changes: 4 additions & 0 deletions workspace/src/app/views/shared/ReactFlow/typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Node, Edge} from "react-flow-renderer-lts"

/** Short-cut for either Node or Edge. */
export type ReactFlowElement = Node | Edge