diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-03-27 20:39:40 +1100 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-03-27 20:40:53 +1100 |
| commit | c16a0deca99dfa68e036c72bd4dc23441177db40 (patch) | |
| tree | b20a9aa5669afede30f0407aadc59309f063d40a /src/lib.ts | |
| parent | bdc3bf69fae2e61aed8b3e41f46d2b0675a88231 (diff) | |
Add rect-layout leaves-to-start setting
Diffstat (limited to 'src/lib.ts')
| -rw-r--r-- | src/lib.ts | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -143,7 +143,8 @@ export type LayoutOptions = { maxTileSz: number; layoutType: 'sqr' | 'rect' | 'sweep'; // The LayoutFn function to use rectMode: 'horz' | 'vert' | 'linear' | 'auto' | 'auto first-row'; - // Layout in 1 row, 1 column, 1 row or column, or multiple rows (with/without first-row-heuristic) + // Rect layout in 1 row, 1 column, 1 row or column, or multiple rows (with/without first-row-heuristic) + rectSepLeaves: boolean; // Rect layout moves leaf nodes to start sweepMode: 'left' | 'top' | 'shorter' | 'auto'; // Sweep to left, top, shorter-side, or to minimise empty space sweptNodesPrio: 'linear' | 'sqrt' | 'pow-2/3'; // Specifies allocation of space to swept-vs-remaining nodes sweepingToParent: boolean; // Allow swept nodes to occupy empty space in a parent's swept-leaves area @@ -341,6 +342,24 @@ let rectLayout: LayoutFn = function (node, pos, dims, showHeader, allowCollapse, if (newDims[0] * newDims[1] <= 0){ return false; } + // Reorder children if applicable + let oldNode: LayoutNode, oldChildIdxs: number[]; + if (opts.rectSepLeaves){ // Change 'node' to leaves-to-start version, and store old-child-order indices + oldNode = node; + let leaves: LayoutNode[] = [], nonLeaves: LayoutNode[] = []; + let leafIdxs = [] as number[], nonLeafIdxs = [] as number[]; + node.children.forEach((child, idx) => { + if (child.children.length == 0){ + leaves.push(child); + leafIdxs.push(idx); + } else { + nonLeaves.push(child); + nonLeafIdxs.push(idx); + } + }); + node = new LayoutNode(node.tolNode, leaves.concat(...nonLeaves)); + oldChildIdxs = leafIdxs.concat(...nonLeafIdxs); + } // Try finding arrangement with low empty space // Done by searching possible rows groupings, allocating within rows using dCounts, and trimming empty space let numChildren = node.children.length; @@ -519,6 +538,9 @@ let rectLayout: LayoutFn = function (node, pos, dims, showHeader, allowCollapse, } } if (usedTree == null){ // If no found layout + if (opts.rectSepLeaves){ + node = oldNode!; + } if (allowCollapse){ node.children = []; LayoutNode.updateDCounts(node, 1 - node.dCount); @@ -527,6 +549,11 @@ let rectLayout: LayoutFn = function (node, pos, dims, showHeader, allowCollapse, return false; } // Create layout + if (opts.rectSepLeaves){ // Restore old 'node', and reorder 'usedTree's children to match + node = oldNode!; + let usedChildren = [...usedTree.children]; + range(numChildren).forEach(idx => usedTree!.children[oldChildIdxs[idx]] = usedChildren[idx]); + } usedTree.copyTreeForRender(node); let usedDims: [number, number] = [dims[0] - usedEmpRight, dims[1] - usedEmpBottom]; node.assignLayoutData(pos, usedDims, {showHeader, empSpc: lowestEmpSpc}); |
