mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-08-21 07:10:37 +00:00
37 lines
855 B
TypeScript
37 lines
855 B
TypeScript
import { Block } from "../interfaces";
|
|
|
|
export function buildTree(id: string, blocksMap: Record<string, Block>) {
|
|
const head = blocksMap[id];
|
|
let node: Block | null = head;
|
|
while(node) {
|
|
|
|
if (node.parent) {
|
|
const parent = blocksMap[node.parent];
|
|
!parent.children && (parent.children = []);
|
|
parent.children.push(node);
|
|
}
|
|
|
|
if (node.firstChild) {
|
|
node = blocksMap[node.firstChild];
|
|
} else if (node.next) {
|
|
node = blocksMap[node.next];
|
|
} else {
|
|
while(node && node?.parent) {
|
|
const parent: Block | null = blocksMap[node.parent];
|
|
if (parent?.next) {
|
|
node = blocksMap[parent.next];
|
|
break;
|
|
} else {
|
|
node = parent;
|
|
}
|
|
}
|
|
if (node.id === head.id) {
|
|
node = null;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
return head;
|
|
}
|