react-grid-layout 1.2.1 → 1.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ 1.2.2 (Mar 1, 2021)
4
+ ---
5
+
6
+ ### Bugfixes
7
+ - `onResize` as changed in 1.2.1 did not correctly save the layout. This is now fixed.
8
+ - As you might guess, we need more test coverage! PRs are very welcome, I'll buy you beers on Cashapp or Patreon or whatever you like.
9
+
3
10
  1.2.1 (Mar 1, 2021)
4
11
  ---
5
12
 
@@ -27,6 +27,14 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
27
27
 
28
28
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
29
29
 
30
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
31
+
32
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
33
+
34
+ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
35
+
36
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
37
+
30
38
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
31
39
 
32
40
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
@@ -464,45 +472,49 @@ var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
464
472
  var _this$props4 = this.props,
465
473
  cols = _this$props4.cols,
466
474
  preventCollision = _this$props4.preventCollision;
467
- var originalLayoutItem
468
- /*: ?LayoutItem*/
469
- = (0, _utils.getLayoutItem)(layout, i);
470
- if (!originalLayoutItem) return; // We're going to modify this object now, so clone it first.
471
-
472
- var l = (0, _utils.cloneLayoutItem)(originalLayoutItem); // Something like quad tree should be used
473
- // to find collisions faster
474
-
475
- var hasCollisions;
476
-
477
- if (preventCollision) {
478
- var collisions = (0, _utils.getAllCollisions)(layout, _objectSpread(_objectSpread({}, l), {}, {
479
- w: w,
480
- h: h
481
- })).filter(function (layoutItem) {
482
- return layoutItem.i !== l.i;
483
- });
484
- hasCollisions = collisions.length > 0; // If we're colliding, we need adjust the placeholder.
485
-
486
- if (hasCollisions) {
487
- // adjust w && h to maximum allowed space
488
- var leastX = Infinity,
489
- leastY = Infinity;
490
- collisions.forEach(function (layoutItem) {
491
- if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
492
- if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
475
+
476
+ var _withLayoutItem = (0, _utils.withLayoutItem)(layout, i, function (l) {
477
+ // Something like quad tree should be used
478
+ // to find collisions faster
479
+ var hasCollisions;
480
+
481
+ if (preventCollision) {
482
+ var collisions = (0, _utils.getAllCollisions)(layout, _objectSpread(_objectSpread({}, l), {}, {
483
+ w: w,
484
+ h: h
485
+ })).filter(function (layoutItem) {
486
+ return layoutItem.i !== l.i;
493
487
  });
494
- if (Number.isFinite(leastX)) l.w = leastX - l.x;
495
- if (Number.isFinite(leastY)) l.h = leastY - l.y;
488
+ hasCollisions = collisions.length > 0; // If we're colliding, we need adjust the placeholder.
489
+
490
+ if (hasCollisions) {
491
+ // adjust w && h to maximum allowed space
492
+ var leastX = Infinity,
493
+ leastY = Infinity;
494
+ collisions.forEach(function (layoutItem) {
495
+ if (layoutItem.x > l.x) leastX = Math.min(leastX, layoutItem.x);
496
+ if (layoutItem.y > l.y) leastY = Math.min(leastY, layoutItem.y);
497
+ });
498
+ if (Number.isFinite(leastX)) l.w = leastX - l.x;
499
+ if (Number.isFinite(leastY)) l.h = leastY - l.y;
500
+ }
501
+ }
502
+
503
+ if (!hasCollisions) {
504
+ // Set new width and height.
505
+ l.w = w;
506
+ l.h = h;
496
507
  }
497
- }
498
508
 
499
- if (!hasCollisions) {
500
- // Set new width and height.
501
- l.w = w;
502
- l.h = h;
503
- } // Create placeholder element (display only)
509
+ return l;
510
+ }),
511
+ _withLayoutItem2 = _slicedToArray(_withLayoutItem, 2),
512
+ newLayout = _withLayoutItem2[0],
513
+ l = _withLayoutItem2[1]; // Shouldn't ever happen, but typechecking makes it necessary
504
514
 
505
515
 
516
+ if (!l) return; // Create placeholder element (display only)
517
+
506
518
  var placeholder = {
507
519
  w: l.w,
508
520
  h: l.h,
@@ -511,10 +523,10 @@ var ReactGridLayout = /*#__PURE__*/function (_React$Component) {
511
523
  static: true,
512
524
  i: i
513
525
  };
514
- this.props.onResize(layout, oldResizeItem, l, placeholder, e, node); // Re-compact the layout and set the drag placeholder.
526
+ this.props.onResize(newLayout, oldResizeItem, l, placeholder, e, node); // Re-compact the newLayout and set the drag placeholder.
515
527
 
516
528
  this.setState({
517
- layout: (0, _utils.compact)(layout, (0, _utils.compactType)(this.props), cols),
529
+ layout: (0, _utils.compact)(newLayout, (0, _utils.compactType)(this.props), cols),
518
530
  activeDrag: placeholder
519
531
  });
520
532
  }
package/build/utils.js CHANGED
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.bottom = bottom;
7
7
  exports.cloneLayout = cloneLayout;
8
+ exports.modifyLayout = modifyLayout;
9
+ exports.withLayoutItem = withLayoutItem;
8
10
  exports.cloneLayoutItem = cloneLayoutItem;
9
11
  exports.childrenEqual = childrenEqual;
10
12
  exports.fastPositionEqual = fastPositionEqual;
@@ -79,6 +81,48 @@ function cloneLayout(layout
79
81
  }
80
82
 
81
83
  return newLayout;
84
+ } // Modify a layoutItem inside a layout. Returns a new Layout,
85
+ // does not mutate. Carries over all other LayoutItems unmodified.
86
+
87
+
88
+ function modifyLayout(layout
89
+ /*: Layout*/
90
+ , layoutItem
91
+ /*: LayoutItem*/
92
+ )
93
+ /*: Layout*/
94
+ {
95
+ var newLayout = Array(layout.length);
96
+
97
+ for (var i = 0, len = layout.length; i < len; i++) {
98
+ if (layoutItem.i === layout[i].i) {
99
+ newLayout[i] = layoutItem;
100
+ } else {
101
+ newLayout[i] = layout[i];
102
+ }
103
+ }
104
+
105
+ return newLayout;
106
+ } // Function to be called to modify a layout item.
107
+ // Does defensive clones to ensure the layout is not modified.
108
+
109
+
110
+ function withLayoutItem(layout
111
+ /*: Layout*/
112
+ , itemKey
113
+ /*: string*/
114
+ , cb
115
+ /*: LayoutItem => LayoutItem*/
116
+ )
117
+ /*: [Layout, ?LayoutItem]*/
118
+ {
119
+ var item = getLayoutItem(layout, itemKey);
120
+ if (!item) return [layout, null];
121
+ item = cb(cloneLayoutItem(item)); // defensive clone then modify
122
+ // FIXME could do this faster if we already knew the index
123
+
124
+ layout = modifyLayout(layout, item);
125
+ return [layout, item];
82
126
  } // Fast path to cloning, since this is monomorphic
83
127
 
84
128
 
@@ -727,13 +771,15 @@ function synchronizeLayoutWithChildren(initialLayout
727
771
  if (g) {
728
772
  if (!isProduction) {
729
773
  validateLayout([g], "ReactGridLayout.children");
730
- }
774
+ } // FIXME clone not really necessary here
775
+
731
776
 
732
777
  layout[i] = cloneLayoutItem(_objectSpread(_objectSpread({}, g), {}, {
733
778
  i: child.key
734
779
  }));
735
780
  } else {
736
781
  // Nothing provided: ensure this is added to the bottom
782
+ // FIXME clone not really necessary here
737
783
  layout[i] = cloneLayoutItem({
738
784
  w: 1,
739
785
  h: 1,