@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
91 lines (90 loc) • 3.37 kB
JavaScript
export function makeVirtualTree(paths, nodeToIndex, itemHeight, nonAdjacentPathTrees) {
const root = [];
const lookup = new Map();
for (const [rawPath, node] of paths) {
const path = typeof rawPath === "string" ? [] : rawPath;
if (nonAdjacentPathTrees)
path.pop();
const joinPath = path.join("#");
if (node.kind === "branch") {
const rowIndex = nodeToIndex.get(node);
const parentIndex = node.parent.kind === "root" ? 0 : nodeToIndex.get(node.parent);
const posinset = rowIndex - parentIndex + 1;
const setSize = node.parent.children.size;
const branchNode = {
kind: "branch",
path: joinPath,
branch: node,
children: [],
rowIndex,
attrs: {
style: {
position: "absolute",
height: itemHeight,
width: "100%",
boxSizing: "border-box",
top: 0,
left: 0,
transform: `translate3d(0px, ${(posinset - 1) * itemHeight}px, 0px)`,
},
"data-ln-row-index": rowIndex,
"aria-posinset": posinset,
"aria-setsize": setSize,
},
};
lookup.set(joinPath, branchNode);
if (path.length === 0) {
root.push(branchNode);
}
else {
const parentPath = path.slice(0, -1).join("#");
const parent = lookup.get(parentPath);
if (parent) {
parent.children.push(branchNode);
}
else {
root.push(branchNode); // fallback
}
}
}
else {
const rowIndex = nodeToIndex.get(node);
const parentIndex = node.parent.kind === "root" ? 0 : nodeToIndex.get(node.parent);
const posinset = rowIndex - parentIndex + 1;
const setSize = node.parent.children.size;
const leafNode = {
kind: "leaf",
leaf: node,
rowIndex,
attrs: {
style: {
position: "absolute",
height: itemHeight,
boxSizing: "border-box",
width: "100%",
top: 0,
left: 0,
transform: `translate3d(0px, ${(posinset - 1) * itemHeight}px, 0px)`,
},
"data-ln-row-index": rowIndex,
"aria-posinset": posinset,
"aria-setsize": setSize,
},
};
if (path.length === 0) {
root.push(leafNode); // top-level leaf
}
else {
const parentPath = path.join("#");
const parent = lookup.get(parentPath);
if (parent) {
parent.children.push(leafNode);
}
else {
root.push(leafNode); // fallback
}
}
}
}
return root;
}