sheet-happens 0.0.6 → 0.0.10

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.
@@ -589,6 +589,8 @@ function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, ce
589
589
  }
590
590
 
591
591
  function Sheet(props) {
592
+ var _props$inputComponent;
593
+
592
594
  var canvasRef = useRef(null);
593
595
  var overlayRef = useRef(null);
594
596
  var copyPasteTextAreaRef = useRef(null);
@@ -831,74 +833,138 @@ function Sheet(props) {
831
833
 
832
834
  resizeCanvas(canvas);
833
835
  var yCoord = columnHeaderHeight;
836
+ var xCoord = rowHeaderWidth;
837
+
838
+ for (var _iterator10 = _createForOfIteratorHelperLoose(columnSizes.index), _step10; !(_step10 = _iterator10()).done;) {
839
+ var x = _step10.value;
840
+ var ch = columnHeaders(x);
841
+ var cellW = cellWidth(x);
842
+
843
+ if (ch && typeof ch === 'object' && ch.items) {
844
+ var finalStyle = void 0;
845
+
846
+ for (var _iterator12 = _createForOfIteratorHelperLoose(ch.items), _step12; !(_step12 = _iterator12()).done;) {
847
+ var obj = _step12.value;
848
+
849
+ if (obj.onClick) {
850
+ if (!finalStyle) {
851
+ finalStyle = createStyleObject(columnHeaderStyle(x), defaultCellStyle);
852
+ }
853
+
854
+ var w = obj.content instanceof HTMLImageElement ? obj.width || cellW : 0;
855
+ var absX1 = applyAlignment(xCoord, cellW, finalStyle, w, obj.horiozntalAlign) + obj.x;
856
+ var absY1 = columnHeaderHeight * 0.5 + obj.y;
857
+ var absX2 = absX1 + (obj.width || 0);
858
+ var absY2 = absY1 + (obj.height || 0);
859
+ var hitTarget = {
860
+ x: absX1,
861
+ y: absY1,
862
+ w: obj.width,
863
+ h: obj.height,
864
+ onClick: obj.onClick
865
+ };
866
+ var x1key = Math.floor(absX1 / xBinSize);
867
+ var x2key = Math.floor(absX2 / xBinSize);
868
+ var y1key = Math.floor(absY1 / yBinSize);
869
+ var y2key = Math.floor(absY2 / yBinSize);
870
+
871
+ for (var xkey = x1key; xkey <= x2key; xkey++) {
872
+ if (!hitM[xkey]) {
873
+ hitM[xkey] = {};
874
+ }
875
+
876
+ var xbin = hitM[xkey];
877
+
878
+ for (var ykey = y1key; ykey <= y2key; ykey++) {
879
+ if (!xbin[ykey]) {
880
+ xbin[ykey] = [];
881
+ }
834
882
 
835
- for (var _iterator10 = _createForOfIteratorHelperLoose(rowSizes.index), _step10; !(_step10 = _iterator10()).done;) {
836
- var y = _step10.value;
837
- var xCoord = rowHeaderWidth;
883
+ xbin[ykey].push(hitTarget);
884
+ }
885
+ }
886
+ }
887
+ }
888
+ }
889
+
890
+ xCoord += cellW;
891
+ }
892
+
893
+ for (var _iterator11 = _createForOfIteratorHelperLoose(rowSizes.index), _step11; !(_step11 = _iterator11()).done;) {
894
+ var y = _step11.value;
895
+ xCoord = rowHeaderWidth;
896
+
897
+ for (var _iterator13 = _createForOfIteratorHelperLoose(columnSizes.index), _step13; !(_step13 = _iterator13()).done;) {
898
+ var _x2 = _step13.value;
899
+ var cellContent = displayData(_x2, y);
838
900
 
839
- for (var _iterator11 = _createForOfIteratorHelperLoose(columnSizes.index), _step11; !(_step11 = _iterator11()).done;) {
840
- var x = _step11.value;
841
- var cellContent = displayData(x, y);
842
- var cellW = cellWidth(x);
901
+ var _cellW = cellWidth(_x2);
843
902
 
844
903
  if (cellContent === null || cellContent === undefined) {
845
- xCoord += cellW;
904
+ xCoord += _cellW;
846
905
  continue;
847
906
  }
848
907
 
849
908
  var xx = xCoord;
850
909
  var yy = yCoord + cellHeight(y) * 0.5;
851
910
 
852
- if (typeof cellContent === 'object') {
853
- var finalStyle = void 0;
911
+ if (typeof cellContent === 'object' && cellContent.items) {
912
+ var _finalStyle = void 0;
854
913
 
855
- for (var _iterator12 = _createForOfIteratorHelperLoose(cellContent.items), _step12; !(_step12 = _iterator12()).done;) {
856
- var obj = _step12.value;
914
+ for (var _iterator14 = _createForOfIteratorHelperLoose(cellContent.items), _step14; !(_step14 = _iterator14()).done;) {
915
+ var _obj = _step14.value;
857
916
 
858
- if (obj.onClick) {
859
- if (!finalStyle) {
860
- finalStyle = createStyleObject(cellStyle(x, y), defaultCellStyle);
917
+ if (_obj.onClick) {
918
+ if (!_finalStyle) {
919
+ _finalStyle = createStyleObject(cellStyle(_x2, y), defaultCellStyle);
861
920
  }
862
921
 
863
- var w = obj.content instanceof HTMLImageElement ? obj.width || cellW : 0;
864
- var absX1 = applyAlignment(xx, cellW, finalStyle, w, obj.horiozntalAlign) + obj.x;
865
- var absY1 = yy + obj.y;
866
- var absX2 = absX1 + (obj.width || 0);
867
- var absY2 = absY1 + (obj.height || 0);
868
- var hitTarget = {
869
- cellX: x,
870
- cellY: y,
871
- x: absX1,
872
- y: absY1,
873
- w: obj.width,
874
- h: obj.height,
875
- onClick: obj.onClick
922
+ var _w = _obj.content instanceof HTMLImageElement ? _obj.width || _cellW : 0;
923
+
924
+ var _absX = applyAlignment(xx, _cellW, _finalStyle, _w, _obj.horiozntalAlign) + _obj.x;
925
+
926
+ var _absY = yy + _obj.y;
927
+
928
+ var _absX2 = _absX + (_obj.width || 0);
929
+
930
+ var _absY2 = _absY + (_obj.height || 0);
931
+
932
+ var _hitTarget = {
933
+ x: _absX,
934
+ y: _absY,
935
+ w: _obj.width,
936
+ h: _obj.height,
937
+ onClick: _obj.onClick
876
938
  };
877
- var x1key = Math.floor(absX1 / xBinSize);
878
- var x2key = Math.floor(absX2 / xBinSize);
879
- var y1key = Math.floor(absY1 / yBinSize);
880
- var y2key = Math.floor(absY2 / yBinSize);
881
-
882
- for (var xkey = x1key; xkey <= x2key; xkey++) {
883
- if (!hitM[xkey]) {
884
- hitM[xkey] = {};
939
+
940
+ var _x1key = Math.floor(_absX / xBinSize);
941
+
942
+ var _x2key = Math.floor(_absX2 / xBinSize);
943
+
944
+ var _y1key = Math.floor(_absY / yBinSize);
945
+
946
+ var _y2key = Math.floor(_absY2 / yBinSize);
947
+
948
+ for (var _xkey = _x1key; _xkey <= _x2key; _xkey++) {
949
+ if (!hitM[_xkey]) {
950
+ hitM[_xkey] = {};
885
951
  }
886
952
 
887
- var xbin = hitM[xkey];
953
+ var _xbin = hitM[_xkey];
888
954
 
889
- for (var ykey = y1key; ykey <= y2key; ykey++) {
890
- if (!xbin[ykey]) {
891
- xbin[ykey] = [];
955
+ for (var _ykey = _y1key; _ykey <= _y2key; _ykey++) {
956
+ if (!_xbin[_ykey]) {
957
+ _xbin[_ykey] = [];
892
958
  }
893
959
 
894
- xbin[ykey].push(hitTarget);
960
+ _xbin[_ykey].push(_hitTarget);
895
961
  }
896
962
  }
897
963
  }
898
964
  }
899
965
  }
900
966
 
901
- xCoord += cellW;
967
+ xCoord += _cellW;
902
968
  }
903
969
 
904
970
  yCoord += cellHeight(y);
@@ -982,8 +1048,8 @@ function Sheet(props) {
982
1048
  });
983
1049
 
984
1050
  var findTable = function findTable(element) {
985
- for (var _iterator13 = _createForOfIteratorHelperLoose(element.children), _step13; !(_step13 = _iterator13()).done;) {
986
- var child = _step13.value;
1051
+ for (var _iterator15 = _createForOfIteratorHelperLoose(element.children), _step15; !(_step15 = _iterator15()).done;) {
1052
+ var child = _step15.value;
987
1053
 
988
1054
  if (child.nodeName === 'TABLE') {
989
1055
  return child;
@@ -1032,23 +1098,39 @@ function Sheet(props) {
1032
1098
  return;
1033
1099
  }
1034
1100
 
1035
- for (var _iterator14 = _createForOfIteratorHelperLoose(tableNode.children), _step14; !(_step14 = _iterator14()).done;) {
1036
- var tableChild = _step14.value;
1101
+ for (var _iterator16 = _createForOfIteratorHelperLoose(tableNode.children), _step16; !(_step16 = _iterator16()).done;) {
1102
+ var tableChild = _step16.value;
1037
1103
 
1038
1104
  if (tableChild.nodeName === 'TBODY') {
1039
- for (var _iterator15 = _createForOfIteratorHelperLoose(tableChild.children), _step15; !(_step15 = _iterator15()).done;) {
1040
- var tr = _step15.value;
1105
+ for (var _iterator17 = _createForOfIteratorHelperLoose(tableChild.children), _step17; !(_step17 = _iterator17()).done;) {
1106
+ var tr = _step17.value;
1041
1107
  x = pasteLocX;
1042
1108
 
1043
1109
  if (tr.nodeName === 'TR') {
1044
- for (var _iterator16 = _createForOfIteratorHelperLoose(tr.children), _step16; !(_step16 = _iterator16()).done;) {
1045
- var td = _step16.value;
1110
+ for (var _iterator18 = _createForOfIteratorHelperLoose(tr.children), _step18; !(_step18 = _iterator18()).done;) {
1111
+ var td = _step18.value;
1046
1112
 
1047
1113
  if (td.nodeName === 'TD') {
1114
+ var str = '';
1115
+
1116
+ if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1117
+ var p = td.children[0];
1118
+
1119
+ if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1120
+ str = p.children[0].innerHTML;
1121
+ } else {
1122
+ str = p.innerHTML;
1123
+ }
1124
+ } else {
1125
+ str = td.innerHTML;
1126
+ }
1127
+
1128
+ str = str.replaceAll('\n', '');
1129
+ str = str.replaceAll(/\s\s+/g, ' ');
1048
1130
  changes.push({
1049
1131
  y: y,
1050
1132
  x: x,
1051
- value: td.innerHTML
1133
+ value: str
1052
1134
  });
1053
1135
  x++;
1054
1136
  }
@@ -1167,12 +1249,12 @@ function Sheet(props) {
1167
1249
  }
1168
1250
  };
1169
1251
 
1170
- var commitEditingCell = function commitEditingCell() {
1252
+ var commitEditingCell = function commitEditingCell(value) {
1171
1253
  if (props.onChange) {
1172
1254
  props.onChange([{
1173
1255
  x: editCell.x,
1174
1256
  y: editCell.y,
1175
- value: editValue
1257
+ value: value != null ? value : editValue
1176
1258
  }]);
1177
1259
  }
1178
1260
 
@@ -1243,6 +1325,7 @@ function Sheet(props) {
1243
1325
  return;
1244
1326
  }
1245
1327
 
1328
+ setSelectionInProgress(false);
1246
1329
  var rect = e.target.getBoundingClientRect();
1247
1330
  var x = e.clientX - rect.left;
1248
1331
  var y = e.clientY - rect.top;
@@ -1255,8 +1338,8 @@ function Sheet(props) {
1255
1338
  var hitTargetKeyY = Math.floor(y / yBinSize);
1256
1339
 
1257
1340
  if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
1258
- for (var _iterator17 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step17; !(_step17 = _iterator17()).done;) {
1259
- var hitTarget = _step17.value;
1341
+ for (var _iterator19 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step19; !(_step19 = _iterator19()).done;) {
1342
+ var hitTarget = _step19.value;
1260
1343
 
1261
1344
  if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
1262
1345
  setButtonClickMouseDownCoordinates({
@@ -1270,42 +1353,38 @@ function Sheet(props) {
1270
1353
  }
1271
1354
 
1272
1355
  if (y < columnHeaderHeight) {
1273
- var xx = rowHeaderWidth;
1274
-
1275
- for (var _iterator18 = _createForOfIteratorHelperLoose(columnSizes.index), _step18; !(_step18 = _iterator18()).done;) {
1276
- var col = _step18.value;
1356
+ for (var colIdx = 0; colIdx < columnSizes.index.length; colIdx++) {
1357
+ var start = columnSizes.start[colIdx];
1358
+ var end = columnSizes.end[colIdx];
1359
+ var index = columnSizes.index[colIdx];
1277
1360
 
1278
- if (Math.abs(xx - x) < resizeColumnRowMouseThreshold) {
1361
+ if (Math.abs(start - x) < resizeColumnRowMouseThreshold || Math.abs(end - x) < resizeColumnRowMouseThreshold) {
1279
1362
  window.document.body.style.cursor = 'col-resize';
1280
1363
  setColumnResize({
1281
- startX: xx,
1282
- oldWidth: cellWidth(col - 1),
1283
- colIdx: col - 1
1364
+ startX: end,
1365
+ oldWidth: cellWidth(index),
1366
+ colIdx: index
1284
1367
  });
1285
1368
  return;
1286
1369
  }
1287
-
1288
- xx += cellWidth(col);
1289
1370
  }
1290
1371
  }
1291
1372
 
1292
1373
  if (x < rowHeaderWidth) {
1293
- var yy = columnHeaderHeight;
1294
-
1295
- for (var _iterator19 = _createForOfIteratorHelperLoose(rowSizes.index), _step19; !(_step19 = _iterator19()).done;) {
1296
- var row = _step19.value;
1374
+ for (var rowIdx = 0; rowIdx < rowSizes.index.length; rowIdx++) {
1375
+ var _start = rowSizes.start[rowIdx];
1376
+ var _end = rowSizes.end[rowIdx];
1377
+ var _index = rowSizes.index[rowIdx];
1297
1378
 
1298
- if (Math.abs(yy - y) < resizeColumnRowMouseThreshold) {
1379
+ if (Math.abs(_start - y) < resizeColumnRowMouseThreshold || Math.abs(_end - y) < resizeColumnRowMouseThreshold) {
1299
1380
  window.document.body.style.cursor = 'row-resize';
1300
1381
  setRowResize({
1301
- startY: yy,
1302
- oldHeight: cellHeight(row - 1),
1303
- rowIdx: row - 1
1382
+ startY: _end,
1383
+ oldHeight: cellHeight(_index),
1384
+ rowIdx: _index
1304
1385
  });
1305
1386
  return;
1306
1387
  }
1307
-
1308
- yy += cellHeight(row);
1309
1388
  }
1310
1389
  }
1311
1390
 
@@ -1333,7 +1412,7 @@ function Sheet(props) {
1333
1412
  var scrollToP2 = true;
1334
1413
 
1335
1414
  if (x < rowHeaderWidth) {
1336
- sel2.x = 100;
1415
+ sel2.x = dataOffset.x + 100;
1337
1416
  scrollToP2 = false;
1338
1417
  setRowSelectionInProgress(true);
1339
1418
  } else {
@@ -1341,7 +1420,7 @@ function Sheet(props) {
1341
1420
  }
1342
1421
 
1343
1422
  if (y < columnHeaderHeight) {
1344
- sel2.y = 100;
1423
+ sel2.y = dataOffset.y + 100;
1345
1424
  scrollToP2 = false;
1346
1425
  setColumnSelectionInProgress(true);
1347
1426
  } else {
@@ -1430,12 +1509,12 @@ function Sheet(props) {
1430
1509
 
1431
1510
  var srcX = sx1;
1432
1511
 
1433
- for (var _x2 = fx1; _x2 <= fx2; _x2++) {
1512
+ for (var _x3 = fx1; _x3 <= fx2; _x3++) {
1434
1513
  for (var _y2 = fy1; _y2 <= fy2; _y2++) {
1435
1514
  var _value = sourceData(srcX, _y2);
1436
1515
 
1437
1516
  changes.push({
1438
- x: _x2,
1517
+ x: _x3,
1439
1518
  y: _y2,
1440
1519
  value: _value
1441
1520
  });
@@ -1470,14 +1549,14 @@ function Sheet(props) {
1470
1549
 
1471
1550
  var rect = e.target.getBoundingClientRect();
1472
1551
 
1473
- var _x3 = e.clientX - rect.left;
1552
+ var _x4 = e.clientX - rect.left;
1474
1553
 
1475
1554
  var _y3 = e.clientY - rect.top;
1476
1555
 
1477
1556
  var hitTarget = buttonClickMouseDownCoordinates.hitTarget;
1478
1557
 
1479
- if (hitTarget.x <= _x3 && _x3 <= hitTarget.x + hitTarget.w && hitTarget.y <= _y3 && _y3 <= hitTarget.y + hitTarget.h) {
1480
- hitTarget.onClick();
1558
+ if (hitTarget.x <= _x4 && _x4 <= hitTarget.x + hitTarget.w && hitTarget.y <= _y3 && _y3 <= hitTarget.y + hitTarget.h) {
1559
+ hitTarget.onClick(e);
1481
1560
  }
1482
1561
 
1483
1562
  setButtonClickMouseDownCoordinates({
@@ -1599,7 +1678,14 @@ function Sheet(props) {
1599
1678
  y2 = selection.y1;
1600
1679
  }
1601
1680
 
1602
- if (Math.abs(cell.x - (x1 + x2) * 0.5) < Math.abs(cell.y - (y1 + y2) * 0.5)) {
1681
+ var xCellDiff = 0;
1682
+ if (cell.x < x1) xCellDiff = cell.x - x1;
1683
+ if (cell.x > x2) xCellDiff = x2 - cell.x;
1684
+ var yCellDiff = 0;
1685
+ if (cell.y < y1) yCellDiff = cell.y - y1;
1686
+ if (cell.y > y2) yCellDiff = y2 - cell.y;
1687
+
1688
+ if (xCellDiff > yCellDiff) {
1603
1689
  if (cell.y < y1) {
1604
1690
  y1 = cell.y;
1605
1691
  } else {
@@ -1623,7 +1709,9 @@ function Sheet(props) {
1623
1709
  };
1624
1710
 
1625
1711
  var onDoubleClick = function onDoubleClick(e) {
1626
- if (!e.target || !(e.target instanceof Element)) {
1712
+ e.preventDefault();
1713
+
1714
+ if (!e.target || !(e.target instanceof Element) || shiftKeyDown) {
1627
1715
  return;
1628
1716
  }
1629
1717
 
@@ -1810,9 +1898,7 @@ function Sheet(props) {
1810
1898
  };
1811
1899
 
1812
1900
  var onContextMenu = function onContextMenu(e) {
1813
- if (!props.onRightClick) {
1814
- return;
1815
- }
1901
+ var _props$onRightClick;
1816
1902
 
1817
1903
  if (!e.target || !(e.target instanceof Element)) {
1818
1904
  return;
@@ -1822,17 +1908,41 @@ function Sheet(props) {
1822
1908
  var x = e.clientX - rect.left;
1823
1909
  var y = e.clientY - rect.top;
1824
1910
  var cell = absCoordianteToCell(x, y, rowSizes, columnSizes);
1911
+ var cellX = cell.x;
1912
+ var cellY = cell.y;
1913
+ var x1 = selection.x1,
1914
+ x2 = selection.x2,
1915
+ y1 = selection.y1,
1916
+ y2 = selection.y2;
1917
+
1918
+ if (x1 > x2) {
1919
+ var _ref = [x2, x1];
1920
+ x1 = _ref[0];
1921
+ x2 = _ref[1];
1922
+ }
1825
1923
 
1826
- if (y > columnHeaderHeight && x > rowHeaderWidth) {
1827
- onMouseMove(e);
1924
+ if (y1 > y2) {
1925
+ var _ref2 = [y2, y1];
1926
+ y1 = _ref2[0];
1927
+ y2 = _ref2[1];
1928
+ }
1828
1929
 
1829
- var ev = _extends({}, e, {
1830
- cellX: cell.x,
1831
- cellY: cell.y
1832
- });
1930
+ if (!(y > columnHeaderHeight && x > rowHeaderWidth)) {
1931
+ return;
1932
+ }
1833
1933
 
1834
- props.onRightClick(ev);
1934
+ if (!(cellX >= x1 && cellX <= x2) || !(cellY >= y1 && cellY <= y2)) {
1935
+ changeSelection(cellX, cellY, cellX, cellY);
1835
1936
  }
1937
+
1938
+ onMouseMove(e);
1939
+
1940
+ var ev = _extends({}, e, {
1941
+ cellX: cellX,
1942
+ cellY: cellY
1943
+ });
1944
+
1945
+ (_props$onRightClick = props.onRightClick) === null || _props$onRightClick === void 0 ? void 0 : _props$onRightClick.call(props, ev);
1836
1946
  };
1837
1947
 
1838
1948
  var editMode = editCell.x !== -1 && editCell.y !== -1;
@@ -1854,6 +1964,27 @@ function Sheet(props) {
1854
1964
  editTextTextAlign = style.textAlign || defaultCellStyle.textAlign || 'left';
1855
1965
  }
1856
1966
 
1967
+ var inputProps = {
1968
+ value: editValue,
1969
+ autoFocus: true,
1970
+ onKeyDown: onKeyDown,
1971
+ style: {
1972
+ position: 'absolute',
1973
+ top: editTextPosition.y,
1974
+ left: editTextPosition.x,
1975
+ width: editTextWidth,
1976
+ height: editTextHeight,
1977
+ outline: 'none',
1978
+ border: 'none',
1979
+ textAlign: editTextTextAlign,
1980
+ color: 'black',
1981
+ fontSize: defaultCellStyle.fontSize,
1982
+ fontFamily: 'sans-serif'
1983
+ }
1984
+ };
1985
+ var input = (_props$inputComponent = props.inputComponent) === null || _props$inputComponent === void 0 ? void 0 : _props$inputComponent.call(props, editCell.x, editCell.y, _extends({}, inputProps, {
1986
+ onChange: setEditValue
1987
+ }), commitEditingCell);
1857
1988
  return React.createElement("div", {
1858
1989
  style: {
1859
1990
  position: 'relative',
@@ -1918,31 +2049,15 @@ function Sheet(props) {
1918
2049
  tabIndex: 0,
1919
2050
  onKeyDown: onGridKeyDown,
1920
2051
  onKeyUp: onGridKeyUp
1921
- }), editMode && React.createElement("input", {
2052
+ }), editMode && (input != null ? input : React.createElement("input", Object.assign({}, inputProps, {
1922
2053
  type: "text",
1923
2054
  onFocus: function onFocus(e) {
1924
2055
  return e.target.select();
1925
2056
  },
1926
- autoFocus: true,
1927
- onKeyDown: onKeyDown,
1928
- value: editValue,
1929
2057
  onChange: function onChange(e) {
1930
2058
  return setEditValue(e.target.value);
1931
- },
1932
- style: {
1933
- position: 'absolute',
1934
- top: editTextPosition.y,
1935
- left: editTextPosition.x,
1936
- width: editTextWidth,
1937
- height: editTextHeight,
1938
- outline: 'none',
1939
- border: 'none',
1940
- textAlign: editTextTextAlign,
1941
- color: 'black',
1942
- fontSize: defaultCellStyle.fontSize,
1943
- fontFamily: 'sans-serif'
1944
2059
  }
1945
- }));
2060
+ }))));
1946
2061
  }
1947
2062
 
1948
2063
  export default Sheet;