feat(core): allow passing node or id to getoutgoers/getincomers
This commit is contained in:
@@ -152,23 +152,23 @@ export function parseEdge(edge: Edge, defaults: Partial<GraphEdge> = {}): GraphE
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getConnectedElements<T extends Elements = FlowElements>(
|
function getConnectedElements<T extends Elements = FlowElements>(
|
||||||
node: Node,
|
nodeOrId: Node | { id: string } | string,
|
||||||
elements: T,
|
elements: T,
|
||||||
dir: 'source' | 'target',
|
dir: 'source' | 'target',
|
||||||
): T extends FlowElements ? GraphNode[] : Node[] {
|
): T extends FlowElements ? GraphNode[] : Node[] {
|
||||||
if (!isNode(node)) {
|
const id = isString(nodeOrId) ? nodeOrId : nodeOrId.id
|
||||||
return []
|
|
||||||
}
|
|
||||||
const origin = dir === 'source' ? 'target' : 'source'
|
const origin = dir === 'source' ? 'target' : 'source'
|
||||||
const ids = elements.filter((e) => isEdge(e) && e[origin] === node.id).map((e) => isEdge(e) && e[dir])
|
const ids = elements.filter((e) => isEdge(e) && e[origin] === id).map((e) => isEdge(e) && e[dir])
|
||||||
|
|
||||||
return elements.filter((e) => ids.includes(e.id)) as T extends FlowElements ? GraphNode[] : Node[]
|
return elements.filter((e) => ids.includes(e.id)) as T extends FlowElements ? GraphNode[] : Node[]
|
||||||
}
|
}
|
||||||
export function getOutgoers<T extends Elements = FlowElements>(node: Node, elements: T) {
|
export function getOutgoers<T extends Elements = FlowElements>(nodeOrId: Node | { id: string } | string, elements: T) {
|
||||||
return getConnectedElements(node, elements, 'target')
|
return getConnectedElements(nodeOrId, elements, 'target')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getIncomers<T extends Elements = FlowElements>(node: Node, elements: T) {
|
export function getIncomers<T extends Elements = FlowElements>(nodeOrId: Node | { id: string } | string, elements: T) {
|
||||||
return getConnectedElements(node, elements, 'source')
|
return getConnectedElements(nodeOrId, elements, 'source')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEdgeId({ source, sourceHandle, target, targetHandle }: Connection) {
|
export function getEdgeId({ source, sourceHandle, target, targetHandle }: Connection) {
|
||||||
|
|||||||
Reference in New Issue
Block a user