Skip to content

Commit 1253849

Browse files
Jakub Bialasllorenspujol
authored andcommitted
chore(grid): fix enormous shink on resize if collision detected
1 parent 62651ad commit 1253849

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

projects/angular-grid-layout/src/lib/utils/grid.utils.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { compact, CompactType, getAllCollisions, LayoutItem, moveElement } from './react-grid-layout.utils';
1+
import { compact, CompactType, getFirstCollision, Layout, LayoutItem, moveElement } from './react-grid-layout.utils';
22
import { KtdDraggingData, KtdGridCfg, KtdGridCompactType, KtdGridItemRect, KtdGridLayout, KtdGridLayoutItem } from '../grid.definitions';
33
import { ktdPointerClientX, ktdPointerClientY } from './pointer.utils';
44
import { KtdDictionary } from '../../types';
@@ -154,22 +154,37 @@ export function ktdGridItemResizing(gridItemId: string, config: KtdGridCfg, comp
154154
}
155155

156156
if (config.preventCollision) {
157-
const collisions = getAllCollisions(config.layout, layoutItem);
158-
159-
// If we're colliding, we need adjust the placeholder.
160-
if (collisions.length > 0) {
161-
// adjust w && h to maximum allowed space
162-
let leastX = Infinity;
163-
let leastY = Infinity;
164-
collisions.forEach((collision) => {
165-
if (collision.id === layoutItem.id) { return; }
166-
if (collision.x > layoutItem.x) { leastX = Math.min(leastX, collision.x); }
167-
if (collision.y > layoutItem.y) { leastY = Math.min(leastY, collision.y); }
168-
});
169-
170-
if (Number.isFinite(leastX)) { layoutItem.w = leastX - layoutItem.x; }
171-
if (Number.isFinite(leastY)) { layoutItem.h = leastY - layoutItem.y; }
157+
const maxW = layoutItem.w;
158+
const maxH = layoutItem.h;
159+
160+
let colliding = hasCollision(config.layout, layoutItem);
161+
let shrunkDimension: 'w' | 'h' | undefined;
162+
163+
while (colliding) {
164+
shrunkDimension = getDimensionToShrink(layoutItem, shrunkDimension);
165+
layoutItem[shrunkDimension]--;
166+
colliding = hasCollision(config.layout, layoutItem);
167+
}
168+
169+
if (shrunkDimension === 'w') {
170+
layoutItem.h = maxH;
171+
172+
colliding = hasCollision(config.layout, layoutItem);
173+
while (colliding) {
174+
layoutItem.h--;
175+
colliding = hasCollision(config.layout, layoutItem);
176+
}
177+
}
178+
if (shrunkDimension === 'h') {
179+
layoutItem.w = maxW;
180+
181+
colliding = hasCollision(config.layout, layoutItem);
182+
while (colliding) {
183+
layoutItem.w--;
184+
colliding = hasCollision(config.layout, layoutItem);
185+
}
172186
}
187+
173188
}
174189

175190
const newLayoutItems: LayoutItem[] = config.layout.map((item) => {
@@ -186,3 +201,18 @@ export function ktdGridItemResizing(gridItemId: string, config: KtdGridCfg, comp
186201
}
187202
};
188203
}
204+
205+
function hasCollision(layout: Layout, layoutItem: LayoutItem): boolean {
206+
return !!getFirstCollision(layout, layoutItem);
207+
}
208+
209+
function getDimensionToShrink(layoutItem, lastShrunk): 'w' | 'h' {
210+
if (layoutItem.h <= 1) {
211+
return 'w';
212+
}
213+
if (layoutItem.w <= 1) {
214+
return 'h';
215+
}
216+
217+
return lastShrunk === 'w' ? 'h' : 'w';
218+
}

0 commit comments

Comments
 (0)