mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
bf45e97641
* Update types * fix circular dep * type imports * lint type imports and --fix
37 lines
868 B
TypeScript
37 lines
868 B
TypeScript
import type { NavigationNode } from "../types";
|
|
|
|
export const flattenTree = (root: NavigationNode) => {
|
|
const flattened: NavigationNode[] = [];
|
|
if (!root) {
|
|
return flattened;
|
|
}
|
|
|
|
flattened.push(root);
|
|
|
|
root.children.forEach((child) => {
|
|
flattened.push(...flattenTree(child));
|
|
});
|
|
|
|
return flattened;
|
|
};
|
|
|
|
export const ancestors = (node: NavigationNode | null) => {
|
|
const nodes: NavigationNode[] = [];
|
|
if (node) {
|
|
while (node.parent !== null) {
|
|
nodes.unshift(node.parent as NavigationNode);
|
|
node = node.parent as NavigationNode;
|
|
}
|
|
}
|
|
return nodes;
|
|
};
|
|
|
|
export const descendants = (node: NavigationNode, depth = 0) => {
|
|
const allDescendants = flattenTree(node).slice(1);
|
|
return depth === 0
|
|
? allDescendants
|
|
: allDescendants.filter(
|
|
(d) => (d.depth as number) <= (node.depth as number) + depth
|
|
);
|
|
};
|