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

Function compactItemInternal

src/core/compact-compat.ts:77–142  ·  view source on GitHub ↗

* Compact a single item in the layout. * * This is the exact algorithm from lib/utils.js compactItem.

(
  compareWith: Layout,
  l: LayoutItem,
  compactType: CompactType,
  cols: number,
  fullLayout: Layout,
  allowOverlap: boolean | undefined,
  b: number | undefined
)

Source from the content-addressed store, hash-verified

75 * This is the exact algorithm from lib/utils.js compactItem.
76 */
77function compactItemInternal(
78 compareWith: Layout,
79 l: LayoutItem,
80 compactType: CompactType,
81 cols: number,
82 fullLayout: Layout,
83 allowOverlap: boolean | undefined,
84 b: number | undefined
85): LayoutItem {
86 const compactV = compactType === "vertical";
87 const compactH = compactType === "horizontal";
88
89 // When allowOverlap is true, skip all compaction movement.
90 // Items stay where they are placed.
91 if (!allowOverlap) {
92 if (compactV) {
93 // Bottom 'y' possible is the bottom of the layout.
94 // This allows you to do nice stuff like specify {y: Infinity}
95 if (typeof b === "number") {
96 (l as Mutable<LayoutItem>).y = Math.min(b, l.y);
97 } else {
98 (l as Mutable<LayoutItem>).y = Math.min(bottom(compareWith), l.y);
99 }
100 // Move the element up as far as it can go without colliding.
101 while (l.y > 0 && !getFirstCollision(compareWith, l)) {
102 (l as Mutable<LayoutItem>).y--;
103 }
104 } else if (compactH) {
105 // Move the element left as far as it can go without colliding.
106 while (l.x > 0 && !getFirstCollision(compareWith, l)) {
107 (l as Mutable<LayoutItem>).x--;
108 }
109 }
110 }
111
112 // Move it down/right, and keep moving if it's colliding.
113 let collision: LayoutItem | undefined;
114 // When allowOverlap is true, skip collision resolution entirely.
115 while (
116 (collision = getFirstCollision(compareWith, l)) !== undefined &&
117 !allowOverlap
118 ) {
119 if (compactH) {
120 resolveCompactionCollision(fullLayout, l, collision.x + collision.w, "x");
121 } else {
122 resolveCompactionCollision(fullLayout, l, collision.y + collision.h, "y");
123 }
124
125 // Since we can't grow without bounds horizontally, if we've overflown,
126 // let's move it down and try again.
127 if (compactH && l.x + l.w > cols) {
128 (l as Mutable<LayoutItem>).x = cols - l.w;
129 (l as Mutable<LayoutItem>).y++;
130 // Also move element as left as we can
131 while (l.x > 0 && !getFirstCollision(compareWith, l)) {
132 (l as Mutable<LayoutItem>).x--;
133 }
134 }

Callers 2

compactFunction · 0.85
compactItemFunction · 0.85

Calls 3

bottomFunction · 0.85
getFirstCollisionFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…