@jalez/react-flow-automated-layout
Version:
A React library for automated layout of nested node graphs with parent-child relationships using React Flow
55 lines (54 loc) • 2.35 kB
TypeScript
import { Node } from '@xyflow/react';
/**
* Tree node representing a node in the parent-child hierarchy
*/
export interface TreeNode {
id: string;
node: Node;
children: TreeNode[];
depth: number;
}
/**
* Builds a tree structure from parent-child relationships in the node maps.
* The tree only includes parent nodes (nodes that have children).
* The tree starts with root parent nodes (parent nodes without parents) at depth 0.
*
* @param nodeParentIdMapWithChildIdSet Map of parent IDs to their set of child IDs
* @param nodeIdWithNode Map of node IDs to node objects
* @param noParentKey Key used to represent nodes without a parent
* @returns An array of TreeNode objects representing root parent nodes, each containing its hierarchy
*/
export declare const buildNodeTree: (nodeParentIdMapWithChildIdSet: Map<string, Set<string>>, nodeIdWithNode: Map<string, Node>, noParentKey?: string) => TreeNode[];
/**
* Gets all nodes at the specified depth in the tree
*
* @param tree The tree structure to search
* @param targetDepth The depth to find nodes at
* @returns Array of nodes at the specified depth
*/
export declare const getNodesAtDepth: (tree: TreeNode[], targetDepth: number) => Node[];
/**
* Finds the maximum depth in the tree
*
* @param tree The tree structure to analyze
* @returns The maximum depth in the tree
*/
export declare const getMaxTreeDepth: (tree: TreeNode[]) => number;
/**
* Get the path from a node to the root (including the node itself)
*/
export declare function getAncestorPath(nodeId: string, nodeIdWithNode: Map<string, Node>, noParentKey?: string): string[];
/**
* Returns the first child under the given ancestor on the path to the node.
* If the node itself is the direct child under the ancestor, returns the node id.
* Returns null if the ancestor is not on the node's ancestor path.
*/
export declare function getFirstChildUnderAncestor(nodeId: string, ancestorId: string, nodeIdWithNode: Map<string, Node>, noParentKey?: string): string | null;
/**
* Enhanced LCA function that returns both the LCA and the immediate children
*/
export declare function findLCAWithChildren(sourceNodeId: string, targetNodeId: string, nodeIdWithNode: Map<string, Node>, noParentKey?: string): {
lca: string | null;
sourceChild: string | null;
targetChild: string | null;
};