MCPcopy
hub / github.com/react-grid-layout/react-grid-layout / compact

Function compact

src/core/compact-compat.ts:159–207  ·  view source on GitHub ↗
(
  layout: Layout,
  compactType: CompactType,
  cols: number,
  allowOverlap?: boolean
)

Source from the content-addressed store, hash-verified

157 * @returns Compacted layout
158 */
159export function compact(
160 layout: Layout,
161 compactType: CompactType,
162 cols: number,
163 allowOverlap?: boolean
164): LayoutItem[] {
165 // Statics go in the compareWith array right away so items flow around them.
166 const compareWith = getStatics(layout);
167 // We keep track of the bottom position.
168 let b = bottom(compareWith);
169 // We go through the items by row and column (or col and row for horizontal).
170 const sorted = sortLayoutItems(layout, compactType);
171 // Holding for new items.
172 const out: LayoutItem[] = new Array(layout.length);
173
174 for (let i = 0; i < sorted.length; i++) {
175 const sortedItem = sorted[i];
176 if (sortedItem === undefined) continue;
177
178 let l = cloneLayoutItem(sortedItem);
179
180 // Don't move static elements
181 if (!l.static) {
182 l = compactItemInternal(
183 compareWith,
184 l,
185 compactType,
186 cols,
187 sorted,
188 allowOverlap,
189 b
190 );
191 b = Math.max(b, l.y + l.h);
192
193 // Add to comparison array. We only collide with items before this one.
194 // Statics are already in this array.
195 compareWith.push(l);
196 }
197
198 // Add to output array to make sure they still come out in the right order.
199 const originalIndex = layout.indexOf(sortedItem);
200 out[originalIndex] = l;
201
202 // Clear moved flag, if it exists.
203 (l as Mutable<LayoutItem>).moved = false;
204 }
205
206 return out;
207}
208
209/**
210 * Compact a single item within the layout.

Callers

nothing calls this directly

Calls 5

bottomFunction · 0.85
sortLayoutItemsFunction · 0.85
cloneLayoutItemFunction · 0.85
compactItemInternalFunction · 0.85
getStaticsFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…