( layout: Layout, item: LayoutItem, moveToCoord: number, axis: "x" | "y", hasStatics?: boolean )
| 36 | * @param hasStatics - Whether layout contains static items (disables early break optimization) |
| 37 | */ |
| 38 | export function resolveCompactionCollision( |
| 39 | layout: Layout, |
| 40 | item: LayoutItem, |
| 41 | moveToCoord: number, |
| 42 | axis: "x" | "y", |
| 43 | hasStatics?: boolean |
| 44 | ): void { |
| 45 | const sizeProp = axis === "x" ? "w" : "h"; |
| 46 | |
| 47 | // Temporarily increment position to check for collisions |
| 48 | (item as Mutable<LayoutItem>)[axis] += 1; |
| 49 | |
| 50 | const itemIndex = layout.findIndex(l => l.i === item.i); |
| 51 | |
| 52 | // Calculate hasStatics once if not provided (for backwards compat) |
| 53 | const layoutHasStatics = hasStatics ?? getStatics(layout).length > 0; |
| 54 | |
| 55 | for (let i = itemIndex + 1; i < layout.length; i++) { |
| 56 | const otherItem = layout[i]; |
| 57 | if (otherItem === undefined) continue; |
| 58 | if (otherItem.static) continue; |
| 59 | // Optimization: break early if past this element, but only if no statics |
| 60 | // are present. Static items can be scattered throughout the layout, |
| 61 | // so we can't assume sort order guarantees no more collisions. |
| 62 | if (!layoutHasStatics && otherItem.y > item.y + item.h) break; |
| 63 | |
| 64 | if (collides(item, otherItem)) { |
| 65 | resolveCompactionCollision( |
| 66 | layout, |
| 67 | otherItem, |
| 68 | moveToCoord + item[sizeProp], |
| 69 | axis, |
| 70 | layoutHasStatics |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | (item as Mutable<LayoutItem>)[axis] = moveToCoord; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Compact a single item vertically (move up). |
no test coverage detected
searching dependent graphs…