sheet-happens 0.0.21 → 0.0.23

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/dist/index.d.ts CHANGED
@@ -11,6 +11,16 @@ export interface SheetInputProps {
11
11
  onChange: (valiue: string) => void;
12
12
  style: InputStyle;
13
13
  }
14
+ interface SelectionSpan {
15
+ x1: number;
16
+ y1: number;
17
+ x2: number;
18
+ y2: number;
19
+ }
20
+ interface Selection {
21
+ span: SelectionSpan;
22
+ color: string;
23
+ }
14
24
  export interface Change {
15
25
  x: number;
16
26
  y: number;
@@ -57,13 +67,14 @@ export interface SheetProps {
57
67
  editKeys?: CellProperty<string>;
58
68
  sheetStyle?: SheetStyle;
59
69
  dontCommitEditOnSelectionChange?: boolean;
70
+ secondarySelections?: Selection[];
60
71
  inputComponent?: (x: number, y: number, props: SheetInputProps, commitEditingCell?: () => void) => ReactElement | undefined;
61
72
  onSelectionChanged?: (x1: number, y1: number, x2: number, y2: number) => void;
62
73
  onRightClick?: (e: SheetMouseEvent) => void;
63
74
  onChange?: (changes: Array<Change>) => void;
64
75
  onCellWidthChange?: (indices: number[], value: number) => void;
65
76
  onCellHeightChange?: (indices: number[], value: number) => void;
66
- onScrollChange?: (scrollX: number, scrolLY: number) => void;
77
+ onScrollChange?: (visibleRows: number[], visibleColumns: number[]) => void;
67
78
  }
68
79
  export interface Style {
69
80
  color?: string;
package/dist/index.js CHANGED
@@ -335,6 +335,26 @@ function absCoordianteToCell(absX, absY, rowSizes, columnSizes) {
335
335
  };
336
336
  }
337
337
 
338
+ function normalizeSelection(selection) {
339
+ var selx1 = selection.x1;
340
+ var selx2 = selection.x2;
341
+
342
+ if (selection.x1 > selection.x2) {
343
+ selx1 = selection.x2;
344
+ selx2 = selection.x1;
345
+ }
346
+
347
+ var sely1 = selection.y1;
348
+ var sely2 = selection.y2;
349
+
350
+ if (selection.y1 > selection.y2) {
351
+ sely1 = selection.y2;
352
+ sely2 = selection.y1;
353
+ }
354
+
355
+ return [selx1, sely1, selx2, sely2];
356
+ }
357
+
338
358
  function cellToAbsCoordinate(cellX, cellY, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle) {
339
359
  var absX = sheetStyle.rowHeaderWidth;
340
360
  var indX = columnSizes.index.findIndex(function (i) {
@@ -503,7 +523,7 @@ function findInDisplayData(displayData, start, direction) {
503
523
  return i;
504
524
  }
505
525
 
506
- function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle) {
526
+ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, secondarySelections, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle) {
507
527
  resizeCanvas(context.canvas);
508
528
  context.clearRect(0, 0, context.canvas.width, context.canvas.height);
509
529
  context.fillStyle = 'white';
@@ -514,8 +534,8 @@ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, ce
514
534
  var y = _step2.value;
515
535
  var xCoord1 = sheetStyle.rowHeaderWidth;
516
536
 
517
- for (var _iterator8 = _createForOfIteratorHelperLoose(columnSizes.index), _step8; !(_step8 = _iterator8()).done;) {
518
- var x = _step8.value;
537
+ for (var _iterator9 = _createForOfIteratorHelperLoose(columnSizes.index), _step9; !(_step9 = _iterator9()).done;) {
538
+ var x = _step9.value;
519
539
  var style = cellStyle(x, y);
520
540
 
521
541
  if (style.fillColor) {
@@ -530,21 +550,12 @@ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, ce
530
550
  }
531
551
 
532
552
  var hideKnob = false;
533
- var selx1 = selection.x1;
534
- var selx2 = selection.x2;
535
-
536
- if (selection.x1 > selection.x2) {
537
- selx1 = selection.x2;
538
- selx2 = selection.x1;
539
- }
540
-
541
- var sely1 = selection.y1;
542
- var sely2 = selection.y2;
543
553
 
544
- if (selection.y1 > selection.y2) {
545
- sely1 = selection.y2;
546
- sely2 = selection.y1;
547
- }
554
+ var _normalizeSelection = normalizeSelection(selection),
555
+ selx1 = _normalizeSelection[0],
556
+ sely1 = _normalizeSelection[1],
557
+ selx2 = _normalizeSelection[2],
558
+ sely2 = _normalizeSelection[3];
548
559
 
549
560
  var selectionActive = selx1 !== -1 && selx2 !== -1 || sely1 !== -1 && sely2 !== -1;
550
561
  var rowSelectionActive = selx1 === -1 && selx2 === -1 && sely1 !== -1 && sely2 !== -1;
@@ -727,6 +738,70 @@ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, ce
727
738
  context.stroke();
728
739
  }
729
740
 
741
+ for (var _iterator7 = _createForOfIteratorHelperLoose(secondarySelections), _step7; !(_step7 = _iterator7()).done;) {
742
+ var secondarySelection = _step7.value;
743
+
744
+ var _normalizeSelection2 = normalizeSelection(secondarySelection.span),
745
+ _selx = _normalizeSelection2[0],
746
+ _sely = _normalizeSelection2[1],
747
+ _selx2 = _normalizeSelection2[2],
748
+ _sely2 = _normalizeSelection2[3];
749
+
750
+ var secondarySelectionActive = _selx !== -1 && _selx2 !== -1 || _sely !== -1 && _sely2 !== -1;
751
+
752
+ if (!secondarySelectionActive) {
753
+ continue;
754
+ }
755
+
756
+ var rowSecondarySelectionActive = _selx === -1 && _selx2 === -1 && _sely !== -1 && _sely2 !== -1;
757
+ var colSecondarySelectionActive = _selx !== -1 && _selx2 !== -1 && _sely === -1 && _sely2 === -1;
758
+
759
+ var _p = cellToAbsCoordinate(_selx, _sely, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
760
+
761
+ var _p2 = cellToAbsCoordinate(_selx2, _sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
762
+
763
+ _p2.x += cellWidth(_selx2);
764
+ _p2.y += cellHeight(_sely2);
765
+
766
+ if (_p.x >= _p2.x) {
767
+ _p2.x = _p.x;
768
+ var _currentCol = _selx;
769
+
770
+ while (columnSizes.index.includes(_currentCol)) {
771
+ _p2.x += cellWidth(_currentCol);
772
+ _currentCol++;
773
+ }
774
+ }
775
+
776
+ if (_p.y >= _p2.y) {
777
+ _p2.y = _p.y;
778
+ var _currentRow = _sely;
779
+
780
+ while (rowSizes.index.includes(_currentRow)) {
781
+ _p2.y += cellHeight(_currentRow);
782
+ _currentRow++;
783
+ }
784
+ }
785
+
786
+ context.strokeStyle = secondarySelection.color;
787
+ context.lineWidth = 1;
788
+ context.beginPath();
789
+
790
+ if (rowSecondarySelectionActive) {
791
+ var _p1x2 = Math.max(-100, _p.x);
792
+
793
+ context.rect(_p1x2, _p.y, 100000, _p2.y - _p.y);
794
+ } else if (colSecondarySelectionActive) {
795
+ var _p1y2 = Math.max(-100, _p.y);
796
+
797
+ context.rect(_p.x, _p1y2, _p2.x - _p.x, 100000);
798
+ } else {
799
+ context.rect(_p.x, _p.y, _p2.x - _p.x, _p2.y - _p.y);
800
+ }
801
+
802
+ context.stroke();
803
+ }
804
+
730
805
  if (knobDragInProgress) {
731
806
  var kx1 = knobArea.x1;
732
807
  var kx2 = knobArea.x2;
@@ -771,14 +846,14 @@ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, ce
771
846
  context.textBaseline = 'middle';
772
847
  var yCoord = sheetStyle.columnHeaderHeight;
773
848
 
774
- for (var _iterator7 = _createForOfIteratorHelperLoose(rowSizes.index), _step7; !(_step7 = _iterator7()).done;) {
775
- var _y = _step7.value;
849
+ for (var _iterator8 = _createForOfIteratorHelperLoose(rowSizes.index), _step8; !(_step8 = _iterator8()).done;) {
850
+ var _y = _step8.value;
776
851
  var xCoord = sheetStyle.rowHeaderWidth;
777
852
 
778
853
  var _ch = cellHeight(_y);
779
854
 
780
- for (var _iterator9 = _createForOfIteratorHelperLoose(columnSizes.index), _step9; !(_step9 = _iterator9()).done;) {
781
- var _x = _step9.value;
855
+ for (var _iterator10 = _createForOfIteratorHelperLoose(columnSizes.index), _step10; !(_step10 = _iterator10()).done;) {
856
+ var _x = _step10.value;
782
857
 
783
858
  var _cellContent = displayData(_x, _y);
784
859
 
@@ -960,6 +1035,11 @@ function Sheet(props) {
960
1035
  var rowSizes = React.useMemo(function () {
961
1036
  return calculateRowsOrColsSizes(sheetStyle.freezeRows, cellHeight, sheetStyle.columnHeaderHeight, dataOffset.y, canvasHeight);
962
1037
  }, [sheetStyle, cellHeight, dataOffset.y, canvasHeight]);
1038
+ React.useEffect(function () {
1039
+ if (props.onScrollChange) {
1040
+ props.onScrollChange([].concat(rowSizes.index), [].concat(columnSizes.index));
1041
+ }
1042
+ }, [rowSizes, columnSizes, props.onScrollChange]);
963
1043
 
964
1044
  var changeSelection = function changeSelection(x1, y1, x2, y2, scrollToP2) {
965
1045
  if (scrollToP2 === void 0) {
@@ -1092,23 +1172,23 @@ function Sheet(props) {
1092
1172
  }
1093
1173
 
1094
1174
  if (selection.x2 !== -1 && selection.y2 !== -1) {
1095
- var _selx = selection.x2;
1175
+ var _selx3 = selection.x2;
1096
1176
 
1097
1177
  if (selection.x1 > selection.x2) {
1098
- _selx = selection.x1;
1178
+ _selx3 = selection.x1;
1099
1179
  }
1100
1180
 
1101
- var _sely = selection.y2;
1181
+ var _sely3 = selection.y2;
1102
1182
 
1103
1183
  if (selection.y1 > selection.y2) {
1104
- _sely = selection.y1;
1184
+ _sely3 = selection.y1;
1105
1185
  }
1106
1186
 
1107
- var _c2 = cellToAbsCoordinate(_selx, _sely, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1187
+ var _c2 = cellToAbsCoordinate(_selx3, _sely3, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1108
1188
 
1109
1189
  return {
1110
- x: _c2.x + cellWidth(_selx),
1111
- y: _c2.y + cellHeight(_sely)
1190
+ x: _c2.x + cellWidth(_selx3),
1191
+ y: _c2.y + cellHeight(_sely3)
1112
1192
  };
1113
1193
  }
1114
1194
 
@@ -1129,16 +1209,16 @@ function Sheet(props) {
1129
1209
  var yCoord = sheetStyle.columnHeaderHeight;
1130
1210
  var xCoord = sheetStyle.rowHeaderWidth;
1131
1211
 
1132
- for (var _iterator10 = _createForOfIteratorHelperLoose(columnSizes.index), _step10; !(_step10 = _iterator10()).done;) {
1133
- var x = _step10.value;
1212
+ for (var _iterator11 = _createForOfIteratorHelperLoose(columnSizes.index), _step11; !(_step11 = _iterator11()).done;) {
1213
+ var x = _step11.value;
1134
1214
  var ch = columnHeaders(x);
1135
1215
  var cellW = cellWidth(x);
1136
1216
 
1137
1217
  if (ch && typeof ch === 'object' && ch.items) {
1138
1218
  var finalStyle = void 0;
1139
1219
 
1140
- for (var _iterator12 = _createForOfIteratorHelperLoose(ch.items), _step12; !(_step12 = _iterator12()).done;) {
1141
- var obj = _step12.value;
1220
+ for (var _iterator13 = _createForOfIteratorHelperLoose(ch.items), _step13; !(_step13 = _iterator13()).done;) {
1221
+ var obj = _step13.value;
1142
1222
 
1143
1223
  if (obj.onClick) {
1144
1224
  if (!finalStyle) {
@@ -1184,12 +1264,12 @@ function Sheet(props) {
1184
1264
  xCoord += cellW;
1185
1265
  }
1186
1266
 
1187
- for (var _iterator11 = _createForOfIteratorHelperLoose(rowSizes.index), _step11; !(_step11 = _iterator11()).done;) {
1188
- var y = _step11.value;
1267
+ for (var _iterator12 = _createForOfIteratorHelperLoose(rowSizes.index), _step12; !(_step12 = _iterator12()).done;) {
1268
+ var y = _step12.value;
1189
1269
  xCoord = sheetStyle.rowHeaderWidth;
1190
1270
 
1191
- for (var _iterator13 = _createForOfIteratorHelperLoose(columnSizes.index), _step13; !(_step13 = _iterator13()).done;) {
1192
- var _x2 = _step13.value;
1271
+ for (var _iterator14 = _createForOfIteratorHelperLoose(columnSizes.index), _step14; !(_step14 = _iterator14()).done;) {
1272
+ var _x2 = _step14.value;
1193
1273
  var cellContent = displayData(_x2, y);
1194
1274
 
1195
1275
  var _cellW = cellWidth(_x2);
@@ -1205,8 +1285,8 @@ function Sheet(props) {
1205
1285
  if (typeof cellContent === 'object' && cellContent.items) {
1206
1286
  var _finalStyle = void 0;
1207
1287
 
1208
- for (var _iterator14 = _createForOfIteratorHelperLoose(cellContent.items), _step14; !(_step14 = _iterator14()).done;) {
1209
- var _obj = _step14.value;
1288
+ for (var _iterator15 = _createForOfIteratorHelperLoose(cellContent.items), _step15; !(_step15 = _iterator15()).done;) {
1289
+ var _obj = _step15.value;
1210
1290
 
1211
1291
  if (_obj.onClick) {
1212
1292
  if (!_finalStyle) {
@@ -1280,12 +1360,12 @@ function Sheet(props) {
1280
1360
  }
1281
1361
 
1282
1362
  var animationFrameId = window.requestAnimationFrame(function () {
1283
- renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle);
1363
+ renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections || [], knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle);
1284
1364
  });
1285
1365
  return function () {
1286
1366
  window.cancelAnimationFrame(animationFrameId);
1287
1367
  };
1288
- }, [canvasRef, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle]);
1368
+ }, [canvasRef, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle]);
1289
1369
 
1290
1370
  var setFocusToTextArea = function setFocusToTextArea() {
1291
1371
  if (copyPasteTextAreaRef.current) {
@@ -1345,8 +1425,8 @@ function Sheet(props) {
1345
1425
  });
1346
1426
 
1347
1427
  var findTable = function findTable(element) {
1348
- for (var _iterator15 = _createForOfIteratorHelperLoose(element.children), _step15; !(_step15 = _iterator15()).done;) {
1349
- var child = _step15.value;
1428
+ for (var _iterator16 = _createForOfIteratorHelperLoose(element.children), _step16; !(_step16 = _iterator16()).done;) {
1429
+ var child = _step16.value;
1350
1430
 
1351
1431
  if (child.nodeName === 'TABLE') {
1352
1432
  return child;
@@ -1395,17 +1475,17 @@ function Sheet(props) {
1395
1475
  return;
1396
1476
  }
1397
1477
 
1398
- for (var _iterator16 = _createForOfIteratorHelperLoose(tableNode.children), _step16; !(_step16 = _iterator16()).done;) {
1399
- var tableChild = _step16.value;
1478
+ for (var _iterator17 = _createForOfIteratorHelperLoose(tableNode.children), _step17; !(_step17 = _iterator17()).done;) {
1479
+ var tableChild = _step17.value;
1400
1480
 
1401
1481
  if (tableChild.nodeName === 'TBODY') {
1402
- for (var _iterator17 = _createForOfIteratorHelperLoose(tableChild.children), _step17; !(_step17 = _iterator17()).done;) {
1403
- var tr = _step17.value;
1482
+ for (var _iterator18 = _createForOfIteratorHelperLoose(tableChild.children), _step18; !(_step18 = _iterator18()).done;) {
1483
+ var tr = _step18.value;
1404
1484
  x = pasteLocX;
1405
1485
 
1406
1486
  if (tr.nodeName === 'TR') {
1407
- for (var _iterator18 = _createForOfIteratorHelperLoose(tr.children), _step18; !(_step18 = _iterator18()).done;) {
1408
- var td = _step18.value;
1487
+ for (var _iterator19 = _createForOfIteratorHelperLoose(tr.children), _step19; !(_step19 = _iterator19()).done;) {
1488
+ var td = _step19.value;
1409
1489
 
1410
1490
  if (td.nodeName === 'TD') {
1411
1491
  var str = '';
@@ -1626,10 +1706,6 @@ function Sheet(props) {
1626
1706
  x: cellX,
1627
1707
  y: cellY
1628
1708
  });
1629
-
1630
- if (props.onScrollChange) {
1631
- props.onScrollChange(cellX, cellY);
1632
- }
1633
1709
  }
1634
1710
 
1635
1711
  var newMaxScroll = _extends({}, maxScroll);
@@ -1673,8 +1749,8 @@ function Sheet(props) {
1673
1749
  var hitTargetKeyY = Math.floor(y / yBinSize);
1674
1750
 
1675
1751
  if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
1676
- for (var _iterator19 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step19; !(_step19 = _iterator19()).done;) {
1677
- var hitTarget = _step19.value;
1752
+ for (var _iterator20 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step20; !(_step20 = _iterator20()).done;) {
1753
+ var hitTarget = _step20.value;
1678
1754
 
1679
1755
  if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
1680
1756
  setButtonClickMouseDownCoordinates({
@@ -1982,8 +2058,8 @@ function Sheet(props) {
1982
2058
  var hitTargetKeyY = Math.floor(y / yBinSize);
1983
2059
 
1984
2060
  if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
1985
- for (var _iterator20 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step20; !(_step20 = _iterator20()).done;) {
1986
- var hitTarget = _step20.value;
2061
+ for (var _iterator21 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step21; !(_step21 = _iterator21()).done;) {
2062
+ var hitTarget = _step21.value;
1987
2063
 
1988
2064
  if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
1989
2065
  window.document.body.style.cursor = 'pointer';
@@ -1994,8 +2070,8 @@ function Sheet(props) {
1994
2070
  if (!sheetStyle.hideColumnHeaders && props.onCellWidthChange && y < sheetStyle.columnHeaderHeight) {
1995
2071
  var xx = sheetStyle.rowHeaderWidth;
1996
2072
 
1997
- for (var _iterator21 = _createForOfIteratorHelperLoose(columnSizes.index), _step21; !(_step21 = _iterator21()).done;) {
1998
- var col = _step21.value;
2073
+ for (var _iterator22 = _createForOfIteratorHelperLoose(columnSizes.index), _step22; !(_step22 = _iterator22()).done;) {
2074
+ var col = _step22.value;
1999
2075
 
2000
2076
  if (Math.abs(xx - x) < resizeColumnRowMouseThreshold) {
2001
2077
  window.document.body.style.cursor = 'col-resize';
@@ -2009,8 +2085,8 @@ function Sheet(props) {
2009
2085
  if (!sheetStyle.hideRowHeaders && props.onCellHeightChange && x < sheetStyle.rowHeaderWidth) {
2010
2086
  var yy = sheetStyle.columnHeaderHeight;
2011
2087
 
2012
- for (var _iterator22 = _createForOfIteratorHelperLoose(rowSizes.index), _step22; !(_step22 = _iterator22()).done;) {
2013
- var row = _step22.value;
2088
+ for (var _iterator23 = _createForOfIteratorHelperLoose(rowSizes.index), _step23; !(_step23 = _iterator23()).done;) {
2089
+ var row = _step23.value;
2014
2090
 
2015
2091
  if (Math.abs(yy - y) < resizeColumnRowMouseThreshold) {
2016
2092
  window.document.body.style.cursor = 'row-resize';
@@ -2117,8 +2193,8 @@ function Sheet(props) {
2117
2193
  var hitTargetKeyY = Math.floor(y / yBinSize);
2118
2194
 
2119
2195
  if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
2120
- for (var _iterator23 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step23; !(_step23 = _iterator23()).done;) {
2121
- var hitTarget = _step23.value;
2196
+ for (var _iterator24 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step24; !(_step24 = _iterator24()).done;) {
2197
+ var hitTarget = _step24.value;
2122
2198
 
2123
2199
  if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
2124
2200
  return;