mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import type { CanvasNode } from '@/types';
|
|
import type { VueFlowStore } from '@vue-flow/core';
|
|
|
|
export function useCanvasTraversal({ getIncomers, getOutgoers }: VueFlowStore) {
|
|
function sortNodesByVerticalPosition(nodes: CanvasNode[]) {
|
|
return nodes.sort((a, b) => a.position.y - b.position.y);
|
|
}
|
|
|
|
function getIncomingNodes(id: string) {
|
|
return sortNodesByVerticalPosition(getIncomers(id));
|
|
}
|
|
|
|
function getOutgoingNodes(id: string) {
|
|
return sortNodesByVerticalPosition(getOutgoers(id));
|
|
}
|
|
|
|
function getSiblingNodes(id: string) {
|
|
const incomingSiblings = getIncomers(id).flatMap((incomingNode) =>
|
|
getOutgoers(incomingNode.id),
|
|
);
|
|
const outgoingSiblings = getOutgoers(id).flatMap((outgoingNode) =>
|
|
getIncomers(outgoingNode.id),
|
|
);
|
|
|
|
return sortNodesByVerticalPosition(
|
|
[...incomingSiblings, ...outgoingSiblings].filter(
|
|
(node, index, nodes) => nodes.findIndex((n) => n.id === node.id) === index,
|
|
),
|
|
);
|
|
}
|
|
|
|
function getDownstreamNodes(id: string, visited: string[] = []): CanvasNode[] {
|
|
if (visited.includes(id)) {
|
|
return [];
|
|
}
|
|
visited.push(id);
|
|
|
|
const downstreamNodes = getOutgoers(id);
|
|
|
|
return [
|
|
...downstreamNodes,
|
|
...downstreamNodes.flatMap((node) => getDownstreamNodes(node.id, visited)),
|
|
].filter((node, index, nodes) => nodes.findIndex((n) => n.id === node.id) === index);
|
|
}
|
|
|
|
function getUpstreamNodes(id: string, visited: string[] = []): CanvasNode[] {
|
|
if (visited.includes(id)) {
|
|
return [];
|
|
}
|
|
visited.push(id);
|
|
|
|
const upstreamNodes = getIncomers(id);
|
|
|
|
return [
|
|
...upstreamNodes,
|
|
...upstreamNodes.flatMap((node) => getUpstreamNodes(node.id, visited)),
|
|
].filter((node, index, nodes) => nodes.findIndex((n) => n.id === node.id) === index);
|
|
}
|
|
|
|
return {
|
|
sortNodesByVerticalPosition,
|
|
getIncomingNodes,
|
|
getOutgoingNodes,
|
|
getSiblingNodes,
|
|
getDownstreamNodes,
|
|
getUpstreamNodes,
|
|
};
|
|
}
|