react-grid-layout 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -405,55 +405,65 @@ export default class ReactGridLayout extends React.Component<Props, State> {
405
405
  let { layout } = this.state;
406
406
  const { cols, preventCollision, allowOverlap } = this.props;
407
407
 
408
+ let shouldMoveItem = false;
409
+ let finalLayout;
410
+ let x;
411
+ let y;
412
+
408
413
  const [newLayout, l] = withLayoutItem(layout, i, l => {
414
+ let hasCollisions;
415
+ x = l.x;
416
+ y = l.y;
417
+ if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
418
+ if (["sw", "nw", "w"].indexOf(handle) !== -1) {
419
+ x = l.x + (l.w - w);
420
+ w = l.x !== x && x < 0 ? l.w : w;
421
+ x = x < 0 ? 0 : x;
422
+ }
423
+
424
+ if (["ne", "n", "nw"].indexOf(handle) !== -1) {
425
+ y = l.y + (l.h - h);
426
+ h = l.y !== y && y < 0 ? l.h : h;
427
+ y = y < 0 ? 0 : y;
428
+ }
429
+
430
+ shouldMoveItem = true;
431
+ }
432
+
409
433
  // Something like quad tree should be used
410
434
  // to find collisions faster
411
- let hasCollisions;
412
435
  if (preventCollision && !allowOverlap) {
413
- const collisions = getAllCollisions(layout, { ...l, w, h }).filter(
414
- layoutItem => layoutItem.i !== l.i
415
- );
436
+ const collisions = getAllCollisions(layout, {
437
+ ...l,
438
+ w,
439
+ h,
440
+ x,
441
+ y
442
+ }).filter(layoutItem => layoutItem.i !== l.i);
416
443
  hasCollisions = collisions.length > 0;
417
444
 
418
445
  // If we're colliding, we need adjust the placeholder.
419
446
  if (hasCollisions) {
420
- // adjust w && h to maximum allowed space
421
- let leastX = Infinity,
422
- leastY = Infinity;
423
- collisions.forEach(layoutItem => {
424
- if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
425
- if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
426
- });
427
-
428
- if (Number.isFinite(leastX)) l.w = leastX - l.x;
429
- if (Number.isFinite(leastY)) l.h = leastY - l.y;
447
+ // Reset layoutItem dimensions if there were collisions
448
+ y = l.y;
449
+ h = l.h;
450
+ x = l.x;
451
+ w = l.w;
452
+ shouldMoveItem = false;
430
453
  }
431
454
  }
432
455
 
456
+ l.w = w;
457
+ l.h = h;
458
+
433
459
  return l;
434
460
  });
435
461
 
436
462
  // Shouldn't ever happen, but typechecking makes it necessary
437
463
  if (!l) return;
438
464
 
439
- let finalLayout;
440
- if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
441
- let x = l.x;
442
- let y = l.y;
443
- if (["sw", "nw", "w"].indexOf(handle) !== -1) {
444
- x = l.x + (l.w - w);
445
- w = l.x !== x && x < 0 ? l.w : w;
446
- x = x < 0 ? 0 : x;
447
- }
448
-
449
- if (["ne", "n", "nw"].indexOf(handle) !== -1) {
450
- y = l.y + (l.h - h);
451
- h = l.y !== y && y < 0 ? l.h : h;
452
- y = y < 0 ? 0 : y;
453
- }
454
-
455
- l.w = w;
456
- l.h = h;
465
+ finalLayout = newLayout;
466
+ if (shouldMoveItem) {
457
467
  // Move the element to the new position.
458
468
  const isUserAction = true;
459
469
  finalLayout = moveElement(
@@ -467,10 +477,6 @@ export default class ReactGridLayout extends React.Component<Props, State> {
467
477
  cols,
468
478
  allowOverlap
469
479
  );
470
- } else {
471
- l.w = w;
472
- l.h = h;
473
- finalLayout = newLayout;
474
480
  }
475
481
 
476
482
  // Create placeholder element (display only)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-grid-layout",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "A draggable and resizable grid layout with responsive breakpoints, for React.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -122,9 +122,11 @@
122
122
  }
123
123
  },
124
124
  "lint-staged": {
125
- "*.{js,jsx,css}": [
126
- "eslint --ext .js,.jsx --fix",
127
- "yarn fmt"
125
+ "*.{js,jsx}": [
126
+ "eslint --ext .js,.jsx --fix"
127
+ ],
128
+ "*": [
129
+ "prettier --ignore-unknown --write"
128
130
  ]
129
131
  }
130
132
  }