* Recursively collect the structured text of the tree items. * @param items A list of tree items * @param level The level of items that are being accounted for during this iteration * @param parentExpanded Whether the parent of the first item in param items is expanded
(
items: [number, string, boolean][],
level: number,
parentExpanded: boolean,
)
| 52 | * @param parentExpanded Whether the parent of the first item in param items is expanded |
| 53 | */ |
| 54 | private _getTreeStructure( |
| 55 | items: [number, string, boolean][], |
| 56 | level: number, |
| 57 | parentExpanded: boolean, |
| 58 | ): TextTree { |
| 59 | const result: TextTree = {}; |
| 60 | for (let i = 0; i < items.length; i++) { |
| 61 | const [itemLevel, text, expanded] = items[i]; |
| 62 | const nextItemLevel = items[i + 1]?.[0] ?? -1; |
| 63 | |
| 64 | // Return the accumulated value for the current level once we reach a shallower level item |
| 65 | if (itemLevel < level) { |
| 66 | return result; |
| 67 | } |
| 68 | // Skip deeper level items during this iteration, they will be picked up in a later iteration |
| 69 | if (itemLevel > level) { |
| 70 | continue; |
| 71 | } |
| 72 | // Only add to representation if it is visible (parent is expanded) |
| 73 | if (parentExpanded) { |
| 74 | // Collect the data under this item according to the following rules: |
| 75 | // 1. If the next item in the list is a sibling of the current item add it to the child list |
| 76 | // 2. If the next item is a child of the current item, get the sub-tree structure for the |
| 77 | // child and add it under this item |
| 78 | // 3. If the next item has a shallower level, we've reached the end of the child items for |
| 79 | // the current parent. |
| 80 | if (nextItemLevel === level) { |
| 81 | this._addChildToItem(result, {text}); |
| 82 | } else if (nextItemLevel > level) { |
| 83 | let children = this._getTreeStructure( |
| 84 | items.slice(i + 1), |
| 85 | nextItemLevel, |
| 86 | expanded, |
| 87 | )?.children; |
| 88 | let child = children ? {text, children} : {text}; |
| 89 | this._addChildToItem(result, child); |
| 90 | } else { |
| 91 | this._addChildToItem(result, {text}); |
| 92 | return result; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | private _addChildToItem(result: TextTree, child: TextTree) { |
| 100 | result.children ? result.children.push(child) : (result.children = [child]); |
no test coverage detected