@micromag/screen-game-sort 0.3.736 → 0.3.738

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.
Files changed (2) hide show
  1. package/es/index.js +52 -32
  2. package/package.json +2 -2
package/es/index.js CHANGED
@@ -74,7 +74,7 @@ var defaultProps = {
74
74
  active: true,
75
75
  className: null
76
76
  };
77
- var ShareScreen = function ShareScreen(_ref) {
77
+ var zzzz = function zzzz(_ref) {
78
78
  var layout = _ref.layout,
79
79
  heading = _ref.heading,
80
80
  items = _ref.items,
@@ -141,7 +141,7 @@ var ShareScreen = function ShareScreen(_ref) {
141
141
  setSortedItems = _useState2[1];
142
142
  var sortedItemsRef = useRef(sortedItems);
143
143
  var currentItemsRef = useRef(items);
144
- var elementsRef = useRef([]);
144
+ var elementsRef = useRef({});
145
145
  var _useState3 = useState(false),
146
146
  _useState4 = _slicedToArray(_useState3, 2),
147
147
  initialSorted = _useState4[0],
@@ -194,37 +194,49 @@ var ShareScreen = function ShareScreen(_ref) {
194
194
  dragY = _ref2.dragY,
195
195
  _ref2$initial = _ref2.initial,
196
196
  initial = _ref2$initial === void 0 ? false : _ref2$initial;
197
- var heights = elementsRef.current.map(function (item) {
198
- var _item$getBoundingClie;
199
- return (item === null || item === void 0 || (_item$getBoundingClie = item.getBoundingClientRect()) === null || _item$getBoundingClie === void 0 ? void 0 : _item$getBoundingClie.height) || 0;
197
+ var refs = currentItems.map(function (it) {
198
+ return elementsRef.current[it.id] || null;
200
199
  });
200
+ var initialRefs = (items || []).map(function (it) {
201
+ return elementsRef.current[it.id] || null;
202
+ });
203
+ var initialHeights = initialRefs.map(function (it) {
204
+ var _it$getBoundingClient;
205
+ return (it === null || it === void 0 || (_it$getBoundingClient = it.getBoundingClientRect()) === null || _it$getBoundingClient === void 0 ? void 0 : _it$getBoundingClient.height) || 0;
206
+ });
207
+ var heights = refs.map(function (it) {
208
+ var _it$getBoundingClient2;
209
+ return (it === null || it === void 0 || (_it$getBoundingClient2 = it.getBoundingClientRect()) === null || _it$getBoundingClient2 === void 0 ? void 0 : _it$getBoundingClient2.height) || 0;
210
+ });
211
+ // console.log('heights', heights);
201
212
  api.start(function (itemIndex) {
202
213
  var item = (items || [])[itemIndex] || null;
203
214
  var sortedIndex = currentItems.findIndex(function (it) {
204
- return it === item;
215
+ return it.id === item.id;
205
216
  });
206
- var currentHeight = heights[itemIndex] || 0;
217
+ var currentHeight = initialHeights[itemIndex] || 0;
218
+ var currentY = initialHeights.slice(0, itemIndex).reduce(function (acc, itemHeight) {
219
+ return acc + itemHeight;
220
+ }, 0);
221
+
222
+ // This works
207
223
  if (item === dragItem) {
208
- var _currentY = heights.slice(0, itemIndex).reduce(function (acc, itemHeight) {
209
- return acc + itemHeight;
210
- }, 0);
211
- var _deltaY = dragY - _currentY;
224
+ var _deltaY = dragY - currentY;
212
225
  return {
213
226
  y: "".concat(_deltaY / currentHeight * 100, "%"),
214
227
  scale: 1.02,
215
228
  immediate: true
216
229
  };
217
230
  }
218
- if (itemIndex === sortedIndex) {
219
- return {
220
- y: "0%",
221
- scale: 1,
222
- immediate: initial
223
- };
224
- }
225
- var currentY = heights.slice(0, itemIndex).reduce(function (acc, itemHeight) {
226
- return acc + itemHeight;
227
- }, 0);
231
+
232
+ // if (itemIndex === sortedIndex) {
233
+ // return {
234
+ // y: `0%`,
235
+ // scale: 1,
236
+ // immediate: initial,
237
+ // };
238
+ // }
239
+
228
240
  var newY = heights.slice(0, sortedIndex).reduce(function (acc, itemHeight) {
229
241
  return acc + itemHeight;
230
242
  }, 0);
@@ -269,12 +281,18 @@ var ShareScreen = function ShareScreen(_ref) {
269
281
  var sortedIndex = sortedItems.findIndex(function (it) {
270
282
  return it === item;
271
283
  });
272
- var heights = sortedItems.map(function (sortedItem) {
273
- var _elementsRef$current$;
274
- var elementIndex = items.findIndex(function (it) {
275
- return it === sortedItem;
276
- });
277
- return ((_elementsRef$current$ = elementsRef.current[elementIndex]) === null || _elementsRef$current$ === void 0 || (_elementsRef$current$ = _elementsRef$current$.getBoundingClientRect()) === null || _elementsRef$current$ === void 0 ? void 0 : _elementsRef$current$.height) || 0;
284
+
285
+ // const heights = sortedItems.map((sortedItem) => {
286
+ // const elementIndex = items.findIndex((it) => it === sortedItem);
287
+ // return elementsRef.current[elementIndex]?.getBoundingClientRect()?.height || 0;
288
+ // });
289
+
290
+ var refs = sortedItems.map(function (it) {
291
+ return elementsRef.current[it.id] || null;
292
+ });
293
+ var heights = refs.map(function (it) {
294
+ var _it$getBoundingClient3;
295
+ return (it === null || it === void 0 || (_it$getBoundingClient3 = it.getBoundingClientRect()) === null || _it$getBoundingClient3 === void 0 ? void 0 : _it$getBoundingClient3.height) || 0;
278
296
  });
279
297
  var ys = heights.map(function (itemHeight, heightIndex) {
280
298
  var endY = itemHeight + heights.slice(0, heightIndex).reduce(function (acc, h) {
@@ -406,6 +424,8 @@ var ShareScreen = function ShareScreen(_ref) {
406
424
  scale = _ref6.scale;
407
425
  var item = items[itemIndex] || {};
408
426
  var _ref7 = item || {},
427
+ _ref7$id = _ref7.id,
428
+ id = _ref7$id === void 0 ? null : _ref7$id,
409
429
  _ref7$visual = _ref7.visual,
410
430
  visual = _ref7$visual === void 0 ? null : _ref7$visual,
411
431
  itemLabel = _ref7.label,
@@ -428,7 +448,7 @@ var ShareScreen = function ShareScreen(_ref) {
428
448
  clickable: dragEnabled
429
449
  }, styles.isEmpty, isEmpty), styles.valid, validated !== null && isValid), styles.invalid, validated !== null && !isValid)]),
430
450
  ref: function ref(_ref1) {
431
- elementsRef.current[itemIndex] = _ref1;
451
+ elementsRef.current[id] = _ref1;
432
452
  },
433
453
  style: _objectSpread(_objectSpread({
434
454
  transform: y.to(function (yValue) {
@@ -494,8 +514,8 @@ var ShareScreen = function ShareScreen(_ref) {
494
514
  className: styles.background
495
515
  }) : null);
496
516
  };
497
- ShareScreen.propTypes = propTypes;
498
- ShareScreen.defaultProps = defaultProps;
517
+ zzzz.propTypes = propTypes;
518
+ zzzz.defaultProps = defaultProps;
499
519
 
500
520
  // import * as transforms from './transforms/index';
501
521
 
@@ -519,7 +539,7 @@ var definition = {
519
539
  "value": "Sort game"
520
540
  }]
521
541
  }),
522
- component: ShareScreen,
542
+ component: zzzz,
523
543
  layouts: ['top', 'middle', 'bottom'],
524
544
  // transforms,
525
545
  fields: [{
@@ -752,4 +772,4 @@ var definition = {
752
772
  }]
753
773
  };
754
774
 
755
- export { ShareScreen as GameSortScreen, definition as default };
775
+ export { zzzz as GameSortScreen, definition as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/screen-game-sort",
3
- "version": "0.3.736",
3
+ "version": "0.3.738",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -87,5 +87,5 @@
87
87
  "access": "public",
88
88
  "registry": "https://registry.npmjs.org/"
89
89
  },
90
- "gitHead": "77bd0bf6941418463c0f7bbb68bf9b20379c5c55"
90
+ "gitHead": "01cf2bc1d2bf4769cb33fb817304c6103e2b60f3"
91
91
  }