sheet-happens 0.0.43 → 0.0.45

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.
@@ -1,4 +1,4 @@
1
- import React, { useState, useMemo, useRef, useCallback, useLayoutEffect, useEffect, forwardRef, useImperativeHandle } from 'react';
1
+ import React, { useState, useMemo, useRef, useCallback, useLayoutEffect, forwardRef, useImperativeHandle } from 'react';
2
2
  import useResizeObserver from 'use-resize-observer';
3
3
 
4
4
  function _extends() {
@@ -66,13 +66,13 @@ var styles = {"sheetscroll":"_PxIi8"};
66
66
  var INITIAL_MAX_SCROLL = [2000, 1000];
67
67
  var ORIGIN = [0, 0];
68
68
  var ONE_ONE = [1, 1];
69
+ var NEG_NEG = [-1, -1];
69
70
  var NO_CELL = [-1, -1];
70
71
  var NO_SELECTION = [NO_CELL, NO_CELL];
71
72
  var NO_SELECTIONS = [];
72
73
  var NO_CLICKABLES = [];
73
74
  var NO_STYLE = {};
74
- var MAX_SEARCHABLE_INDEX = 65536;
75
- var MAX_XY = [MAX_SEARCHABLE_INDEX, MAX_SEARCHABLE_INDEX];
75
+ var MAX_SEARCHABLE_INDEX = 100000;
76
76
  var COLORS = {
77
77
  selectionBorder: '#1a66ff',
78
78
  selectionBackground: '#d8e6ff80',
@@ -142,9 +142,6 @@ var ARROW_KEYS = {
142
142
  ArrowDown: 'down'
143
143
  };
144
144
 
145
- var clamp = function clamp(x, min, max) {
146
- return Math.max(Math.min(max, x), min);
147
- };
148
145
  var seq = function seq(n, s, d) {
149
146
  if (s === void 0) {
150
147
  s = 0;
@@ -185,13 +182,6 @@ var maxXY = function maxXY(a, b) {
185
182
  var minXY = function minXY(a, b) {
186
183
  return [Math.min(a[0], b[0]), Math.min(a[1], b[1])];
187
184
  };
188
- var clampXY = function clampXY(p, min, max) {
189
- if (max === void 0) {
190
- max = [Infinity, Infinity];
191
- }
192
-
193
- return [clamp(p[0], min[0], max[0]), clamp(p[1], min[1], max[1])];
194
- };
195
185
  var getDirectionStep = function getDirectionStep(direction) {
196
186
  if (direction === 'left') return [-1, 0];
197
187
  if (direction === 'right') return [1, 0];
@@ -432,6 +422,52 @@ var createCellProp = function createCellProp(cellProp, defaultValue) {
432
422
  };
433
423
  }
434
424
  };
425
+ var createRowOrColumnStyledProp = function createRowOrColumnStyledProp(rowColProp, defaultValue) {
426
+ if (Array.isArray(rowColProp)) {
427
+ return function (rowOrColIndex) {
428
+ if (rowOrColIndex >= 0 && rowOrColIndex < rowColProp.length) {
429
+ return rowColProp[rowOrColIndex];
430
+ } else {
431
+ return defaultValue;
432
+ }
433
+ };
434
+ } else if (typeof rowColProp === 'function') {
435
+ return rowColProp;
436
+ } else if (rowColProp !== null && rowColProp !== undefined) {
437
+ return function () {
438
+ return rowColProp;
439
+ };
440
+ } else {
441
+ return function () {
442
+ return defaultValue;
443
+ };
444
+ }
445
+ };
446
+ var createCellStyledProp = function createCellStyledProp(cellProp, defaultValue) {
447
+ if (Array.isArray(cellProp)) {
448
+ return function (x, y) {
449
+ if (y >= 0 && y < cellProp.length) {
450
+ if (x >= 0 && x < cellProp[y].length) {
451
+ return cellProp[y][x];
452
+ } else {
453
+ return defaultValue;
454
+ }
455
+ } else {
456
+ return defaultValue;
457
+ }
458
+ };
459
+ } else if (typeof cellProp === 'function') {
460
+ return cellProp;
461
+ } else if (cellProp !== null && cellProp !== undefined) {
462
+ return function () {
463
+ return cellProp;
464
+ };
465
+ } else {
466
+ return function () {
467
+ return defaultValue;
468
+ };
469
+ }
470
+ };
435
471
  var findApproxMaxEditDataIndex = function findApproxMaxEditDataIndex(editData) {
436
472
  var x = 0;
437
473
  var y = 0;
@@ -497,8 +533,13 @@ var findApproxMaxEditDataIndex = function findApproxMaxEditDataIndex(editData) {
497
533
  };
498
534
  var findInDisplayData = function findInDisplayData(displayData, start, direction) {
499
535
  var step = getDirectionStep(direction);
500
- var cell = clampXY(start, ORIGIN, MAX_XY);
501
- var first = displayData.apply(void 0, addXY(cell, step));
536
+ var cell = maxXY(start, ORIGIN);
537
+
538
+ var _addXY = addXY(cell, step),
539
+ x = _addXY[0],
540
+ y = _addXY[1];
541
+
542
+ var first = displayData(x, y, DEFAULT_CELL_STYLE);
502
543
  var firstFilled = first !== '' && first !== null && first !== undefined;
503
544
 
504
545
  if (!firstFilled) {
@@ -508,9 +549,11 @@ var findInDisplayData = function findInDisplayData(displayData, start, direction
508
549
  var _cell = cell,
509
550
  cellX = _cell[0],
510
551
  cellY = _cell[1];
552
+ var maxX = cellX + MAX_SEARCHABLE_INDEX - 1;
553
+ var maxY = cellY + MAX_SEARCHABLE_INDEX - 1;
511
554
 
512
- while (cellX <= MAX_SEARCHABLE_INDEX && cellY <= MAX_SEARCHABLE_INDEX && cellX >= 0 && cellY >= 0) {
513
- var data = displayData(cellX, cellY);
555
+ while (cellX < maxX && cellY < maxY && cellX >= 0 && cellY >= 0) {
556
+ var data = displayData(cellX, cellY, DEFAULT_CELL_STYLE);
514
557
 
515
558
  if (firstFilled && (data === '' || data === null || data === undefined)) {
516
559
  return subXY(cell, step);
@@ -529,7 +572,7 @@ var findInDisplayData = function findInDisplayData(displayData, start, direction
529
572
  return maxXY(cell, [0, 0]);
530
573
  };
531
574
 
532
- var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, cellReadOnly, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, getAutoSizeWidth, onEdit, onCommit, onKnobAreaChange, onDragIndicesChange, onDragOffsetChange, onDropTargetChange, onSelectionChange, onInvalidateColumn, onInvalidateRow, onChange, onColumnOrderChange, onRowOrderChange, onCellWidthChange, onCellHeightChange, onRightClick, dontCommitEditOnSelectionChange, dontChangeSelectionOnOrderChange) {
575
+ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, cellReadOnly, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, getAutoSizeWidth, onEdit, onCommit, onKnobAreaChange, onDragIndicesChange, onDragOffsetChange, onDropTargetChange, onSelectionChange, onFocusChange, onInvalidateColumn, onInvalidateRow, onChange, onColumnOrderChange, onRowOrderChange, onCellWidthChange, onCellHeightChange, onRightClick, dontCommitEditOnSelectionChange, dontChangeSelectionOnOrderChange) {
533
576
  var _useState = useState(null),
534
577
  hitTarget = _useState[0],
535
578
  setHitTarget = _useState[1];
@@ -670,6 +713,7 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
670
713
  columns = _ref$current$visibleC.columns,
671
714
  rows = _ref$current$visibleC.rows,
672
715
  knobPosition = _ref$current.knobPosition;
716
+ onFocusChange === null || onFocusChange === void 0 ? void 0 : onFocusChange(true);
673
717
  if (e.button !== 0) return;
674
718
  (_e$target2 = e.target) === null || _e$target2 === void 0 ? void 0 : (_e$target2$setPointer = _e$target2.setPointerCapture) === null || _e$target2$setPointer === void 0 ? void 0 : _e$target2$setPointer.call(_e$target2, e.pointerId);
675
719
  var xy = getMousePosition(e);
@@ -936,6 +980,7 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
936
980
  pixelToRow = _ref$current2$cellLay.pixelToRow,
937
981
  getIndentX = _ref$current2$cellLay.getIndentX,
938
982
  getIndentY = _ref$current2$cellLay.getIndentY;
983
+ onFocusChange === null || onFocusChange === void 0 ? void 0 : onFocusChange(true);
939
984
 
940
985
  if (knobArea && draggingKnob) {
941
986
  var changes = parseKnobOperation(knobArea, selection, sourceData, editData, cellReadOnly);
@@ -1171,7 +1216,9 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
1171
1216
 
1172
1217
  var newWidth = Math.max(size + x - anchor + scroll - currentScroll, SIZES.minimumWidth * indices.length);
1173
1218
  onInvalidateColumn === null || onInvalidateColumn === void 0 ? void 0 : onInvalidateColumn(indices[0] - 1);
1174
- onCellWidthChange(indices, newWidth / indices.length);
1219
+ onCellWidthChange(indices, indices.map(function (_) {
1220
+ return Math.round(newWidth / indices.length);
1221
+ }));
1175
1222
  }
1176
1223
 
1177
1224
  return;
@@ -1189,7 +1236,9 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
1189
1236
 
1190
1237
  var newHeight = Math.max(_size4 + y - _anchor + _scroll4 - _currentScroll, SIZES.minimumHeight * _indices5.length);
1191
1238
  onInvalidateRow === null || onInvalidateRow === void 0 ? void 0 : onInvalidateRow(_indices5[0] - 1);
1192
- onCellHeightChange(_indices5, newHeight / _indices5.length);
1239
+ onCellHeightChange(_indices5, _indices5.map(function (_) {
1240
+ return newHeight / _indices5.length;
1241
+ }));
1193
1242
  }
1194
1243
 
1195
1244
  return;
@@ -1292,15 +1341,18 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
1292
1341
  selection = _ref$current4.selection,
1293
1342
  _ref$current4$cellLay = _ref$current4.cellLayout,
1294
1343
  pixelToCell = _ref$current4$cellLay.pixelToCell,
1295
- columnToPixel = _ref$current4$cellLay.columnToPixel;
1344
+ columnToPixel = _ref$current4$cellLay.columnToPixel,
1345
+ getIndentY = _ref$current4$cellLay.getIndentY;
1296
1346
  e.preventDefault();
1297
1347
  if (e.shiftKey) return;
1298
1348
  var xy = getMousePosition(e);
1299
1349
  if (!xy) return;
1300
- var x = xy[0];
1350
+ var x = xy[0],
1351
+ y = xy[1];
1352
+ var indentY = getIndentY();
1301
1353
  var columns = visibleCells.columns;
1302
1354
 
1303
- if (onCellWidthChange) {
1355
+ if (onCellWidthChange && y < indentY) {
1304
1356
  var autosized = [];
1305
1357
 
1306
1358
  for (var _iterator10 = _createForOfIteratorHelperLoose(columns), _step10; !(_step10 = _iterator10()).done;) {
@@ -1324,9 +1376,11 @@ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editD
1324
1376
  for (var _i = 0, _autosized = autosized; _i < _autosized.length; _i++) {
1325
1377
  var column = _autosized[_i];
1326
1378
  onInvalidateColumn === null || onInvalidateColumn === void 0 ? void 0 : onInvalidateColumn(column - 1);
1327
- onCellWidthChange([column], getAutoSizeWidth(column));
1328
1379
  }
1329
1380
 
1381
+ onCellWidthChange(autosized, autosized.map(function (column) {
1382
+ return getAutoSizeWidth(column);
1383
+ }));
1330
1384
  if (autosized.length) return;
1331
1385
  }
1332
1386
 
@@ -1494,6 +1548,162 @@ var parseKnobOperation = function parseKnobOperation(knobArea, selection, source
1494
1548
  return changes;
1495
1549
  };
1496
1550
 
1551
+ var useKeyboard = function useKeyboard(arrowKeyCommitMode, overlayRef, cellReadOnly, displayData, editCell, editMode, focused, rawSelection, selection, onEdit, onCommit, onCancel, onSelectionChange, onFocusChange, onClipboardCopy, onChange) {
1552
+ var onInputKeyDown = function onInputKeyDown(e) {
1553
+ if (e.key === 'Escape') {
1554
+ onCancel === null || onCancel === void 0 ? void 0 : onCancel();
1555
+ return;
1556
+ }
1557
+
1558
+ var direction = e.key === 'Enter' ? 'down' : e.key === 'Tab' ? 'right' : arrowKeyCommitMode ? ARROW_KEYS[e.key] : null;
1559
+
1560
+ if (direction) {
1561
+ e.preventDefault();
1562
+ var step = getDirectionStep(direction);
1563
+ var head = maxXY(addXY(editCell, step), ORIGIN);
1564
+ onCommit === null || onCommit === void 0 ? void 0 : onCommit();
1565
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([head, head]);
1566
+ }
1567
+ };
1568
+
1569
+ var onGridKeyDown = function onGridKeyDown(e) {
1570
+ if (editMode && arrowKeyCommitMode && e.key in ARROW_KEYS) {
1571
+ onCommit === null || onCommit === void 0 ? void 0 : onCommit();
1572
+ return;
1573
+ }
1574
+
1575
+ if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'v') {
1576
+ return;
1577
+ }
1578
+
1579
+ if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'c') {
1580
+ onClipboardCopy === null || onClipboardCopy === void 0 ? void 0 : onClipboardCopy(false);
1581
+ return;
1582
+ }
1583
+
1584
+ if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'x') {
1585
+ onClipboardCopy === null || onClipboardCopy === void 0 ? void 0 : onClipboardCopy(true);
1586
+ return;
1587
+ }
1588
+
1589
+ if (e.key === 'Backspace' || e.key === 'Delete') {
1590
+ var _normalizeSelection = normalizeSelection(selection),
1591
+ _normalizeSelection$ = _normalizeSelection[0],
1592
+ x1 = _normalizeSelection$[0],
1593
+ y1 = _normalizeSelection$[1],
1594
+ _normalizeSelection$2 = _normalizeSelection[1],
1595
+ x2 = _normalizeSelection$2[0],
1596
+ y2 = _normalizeSelection$2[1];
1597
+
1598
+ if (isRowSelection(selection)) {
1599
+ x1 = 0;
1600
+ x2 = MAX_SEARCHABLE_INDEX;
1601
+ }
1602
+
1603
+ if (isColumnSelection(selection)) {
1604
+ y1 = 0;
1605
+ y2 = MAX_SEARCHABLE_INDEX;
1606
+ }
1607
+
1608
+ var changes = [];
1609
+
1610
+ for (var y = y1; y <= y2; y++) {
1611
+ for (var x = x1; x <= x2; x++) {
1612
+ if (!cellReadOnly(x, y)) {
1613
+ changes.push({
1614
+ x: x,
1615
+ y: y,
1616
+ value: null
1617
+ });
1618
+ }
1619
+ }
1620
+ }
1621
+
1622
+ onChange === null || onChange === void 0 ? void 0 : onChange(changes);
1623
+ return;
1624
+ }
1625
+
1626
+ if (isEmptySelection(selection)) {
1627
+ return;
1628
+ }
1629
+
1630
+ if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90 || e.key === 'Enter' || e.key === '-' || e.key === '.' || e.key === ',') {
1631
+ var cell = selection[0];
1632
+ var cellX = cell[0],
1633
+ cellY = cell[1];
1634
+
1635
+ if (cellReadOnly(cellX, cellY)) {
1636
+ return;
1637
+ }
1638
+
1639
+ onEdit === null || onEdit === void 0 ? void 0 : onEdit(cell, e.key !== 'Enter');
1640
+ return;
1641
+ }
1642
+
1643
+ e.preventDefault();
1644
+
1645
+ if (e.key in ARROW_KEYS) {
1646
+ var anchor = rawSelection[0],
1647
+ head = rawSelection[1];
1648
+ var direction = ARROW_KEYS[e.key];
1649
+ var step = getDirectionStep(direction);
1650
+ var shift = e.shiftKey;
1651
+
1652
+ if (e.metaKey || e.ctrlKey) {
1653
+ head = findInDisplayData(displayData, head, direction);
1654
+ } else {
1655
+ var limit = shift ? isRowSelection(selection) ? [-1, 0] : isColumnSelection(selection) ? [0, -1] : NEG_NEG : ORIGIN;
1656
+ head = maxXY(addXY(head, step), limit);
1657
+ }
1658
+
1659
+ if (!shift) {
1660
+ anchor = head;
1661
+ }
1662
+
1663
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([anchor, head], true, true);
1664
+ return;
1665
+ }
1666
+ };
1667
+
1668
+ var onGridFocus = function onGridFocus() {
1669
+ onFocusChange === null || onFocusChange === void 0 ? void 0 : onFocusChange(true);
1670
+ };
1671
+
1672
+ var onGridBlur = function onGridBlur() {
1673
+ onFocusChange === null || onFocusChange === void 0 ? void 0 : onFocusChange(false);
1674
+ };
1675
+
1676
+ useLayoutEffect(function () {
1677
+ var overlay = overlayRef.current;
1678
+
1679
+ if (!overlay) {
1680
+ return;
1681
+ }
1682
+
1683
+ if (editMode || !focused) {
1684
+ return;
1685
+ }
1686
+
1687
+ if (document.activeElement === overlay) {
1688
+ return;
1689
+ }
1690
+
1691
+ var activeTagName = document.activeElement.tagName.toLowerCase();
1692
+
1693
+ if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1694
+ overlay.focus({
1695
+ preventScroll: true
1696
+ });
1697
+ }
1698
+ }, [editMode, focused]);
1699
+ return {
1700
+ onInputKeyDown: onInputKeyDown,
1701
+ onGridFocus: onGridFocus,
1702
+ onGridBlur: onGridBlur,
1703
+ onGridKeyDown: onGridKeyDown
1704
+ };
1705
+ };
1706
+
1497
1707
  var useScroll = function useScroll(offset, maxScroll, cellLayout, onOffsetChange, onMaxScrollChange) {
1498
1708
  return useCallback(function (e) {
1499
1709
  if (!e.target || !(e.target instanceof Element)) {
@@ -1651,42 +1861,6 @@ var scrollToCell = function scrollToCell(element, cell, view, freeze, offset, ma
1651
1861
  }
1652
1862
  };
1653
1863
 
1654
- var resolveSheetStyle = function resolveSheetStyle(sheetStyle) {
1655
- var _sheetStyle$shadowBlu, _sheetStyle$shadowOpa, _sheetStyle$shadowCol;
1656
-
1657
- return {
1658
- freezeColumns: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeColumns) || 0,
1659
- freezeRows: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeRows) || 0,
1660
- hideColumnHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideColumnHeaders) || false,
1661
- hideRowHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideRowHeaders) || false,
1662
- hideGridlines: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideGridlines) || false,
1663
- hideScrollBars: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideScrollBars) || false,
1664
- columnHeaderHeight: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideColumnHeaders ? 1 : SIZES.headerHeight,
1665
- rowHeaderWidth: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideRowHeaders ? 1 : SIZES.headerWidth,
1666
- shadowBlur: (_sheetStyle$shadowBlu = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowBlur) != null ? _sheetStyle$shadowBlu : SIZES.shadowBlur,
1667
- shadowOpacity: (_sheetStyle$shadowOpa = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowOpacity) != null ? _sheetStyle$shadowOpa : SIZES.shadowOpacity,
1668
- shadowColor: (_sheetStyle$shadowCol = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowColor) != null ? _sheetStyle$shadowCol : COLORS.shadowColor
1669
- };
1670
- };
1671
- var resolveCellStyle = function resolveCellStyle(optionalStyle, defaultStyle) {
1672
- return _extends({}, defaultStyle, optionalStyle);
1673
- };
1674
- var applyAlignment = function applyAlignment(start, cellSize, style, imageWidth, alignment) {
1675
- if (alignment === void 0) {
1676
- alignment = style.textAlign;
1677
- }
1678
-
1679
- if (alignment === 'left') {
1680
- return start + style.marginLeft;
1681
- } else if (alignment === 'center') {
1682
- return start + cellSize * 0.5 - imageWidth / 2;
1683
- } else if (alignment === 'right') {
1684
- return start + (cellSize - style.marginRight - imageWidth);
1685
- }
1686
-
1687
- return start;
1688
- };
1689
-
1690
1864
  var useAutoSizeColumn = function useAutoSizeColumn(rows, displayData, cellStyle, columnHeaders, columnHeaderStyle, canvasWidth) {
1691
1865
  var context = useMemo(function () {
1692
1866
  return document.createElement('canvas').getContext('2d');
@@ -1695,9 +1869,8 @@ var useAutoSizeColumn = function useAutoSizeColumn(rows, displayData, cellStyle,
1695
1869
  if (!context) return 0;
1696
1870
 
1697
1871
  var getWidth = function getWidth(cellContent, style) {
1698
- var finalStyle = resolveCellStyle(style, DEFAULT_CELL_STYLE);
1699
- context.font = finalStyle.weight + ' ' + finalStyle.fontSize + 'px ' + finalStyle.fontFamily;
1700
- var inlineMargin = finalStyle.marginLeft + finalStyle.marginRight;
1872
+ context.font = style.weight + ' ' + style.fontSize + 'px ' + style.fontFamily;
1873
+ var inlineMargin = style.marginLeft + style.marginRight;
1701
1874
 
1702
1875
  if (typeof cellContent === 'string' || typeof cellContent === 'number') {
1703
1876
  var _context$measureText = context.measureText(cellContent.toString()),
@@ -1722,7 +1895,7 @@ var useAutoSizeColumn = function useAutoSizeColumn(rows, displayData, cellStyle,
1722
1895
  }
1723
1896
 
1724
1897
  if (obj.horizontalAlign === 'right') {
1725
- extraWidth += _width;
1898
+ extraWidth += style.textAlign === 'right' ? _width * 2 : _width;
1726
1899
  } else {
1727
1900
  _maxWidth = Math.max(_maxWidth, _width);
1728
1901
  }
@@ -1735,19 +1908,23 @@ var useAutoSizeColumn = function useAutoSizeColumn(rows, displayData, cellStyle,
1735
1908
  };
1736
1909
 
1737
1910
  var maxWidth = SIZES.minimumWidth;
1738
- var headerContent = columnHeaders(x);
1911
+
1912
+ var headerStyle = _extends({}, DEFAULT_COLUMN_HEADER_STYLE, columnHeaderStyle(x));
1913
+
1914
+ var headerContent = columnHeaders(x, headerStyle);
1739
1915
 
1740
1916
  if (headerContent) {
1741
- var headerStyle = columnHeaderStyle(x);
1742
1917
  maxWidth = Math.max(maxWidth, getWidth(headerContent, headerStyle));
1743
1918
  }
1744
1919
 
1745
1920
  for (var _iterator2 = _createForOfIteratorHelperLoose(rows), _step2; !(_step2 = _iterator2()).done;) {
1746
1921
  var y = _step2.value;
1747
- var cellContent = displayData(x, y);
1922
+
1923
+ var style = _extends({}, DEFAULT_CELL_STYLE, cellStyle(x, y));
1924
+
1925
+ var cellContent = displayData(x, y, style);
1748
1926
 
1749
1927
  if (cellContent != null) {
1750
- var style = cellStyle(x, y);
1751
1928
  maxWidth = Math.max(maxWidth, getWidth(cellContent, style));
1752
1929
  }
1753
1930
  }
@@ -1757,90 +1934,349 @@ var useAutoSizeColumn = function useAutoSizeColumn(rows, displayData, cellStyle,
1757
1934
  return getAutoSizeWidth;
1758
1935
  };
1759
1936
 
1760
- var useClipboardCopy = function useClipboardCopy(textAreaRef, selection, editMode, editData) {
1937
+ // A type of promise-like that resolves synchronously and supports only one observer
1938
+
1939
+ const _iteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator"))) : "@@iterator";
1940
+
1941
+ const _asyncIteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.asyncIterator || (Symbol.asyncIterator = Symbol("Symbol.asyncIterator"))) : "@@asyncIterator";
1942
+
1943
+ // Asynchronously call a function and send errors to recovery continuation
1944
+ function _catch(body, recover) {
1945
+ try {
1946
+ var result = body();
1947
+ } catch(e) {
1948
+ return recover(e);
1949
+ }
1950
+ if (result && result.then) {
1951
+ return result.then(void 0, recover);
1952
+ }
1953
+ return result;
1954
+ }
1955
+
1956
+ var EMPTY_TABLE = {
1957
+ rows: []
1958
+ };
1959
+ var useClipboardTable = function useClipboardTable() {
1960
+ var _useState = useState(),
1961
+ peek = _useState[0],
1962
+ setPeek = _useState[1];
1963
+
1761
1964
  useLayoutEffect(function () {
1762
- var textArea = textAreaRef.current;
1763
- if (!textArea) return;
1764
- if (editMode) return;
1765
- if (isEmptySelection(selection)) return;
1766
- var v = formatSelectionAsTSV(selection, editData);
1965
+ var softRefresh = function softRefresh() {
1966
+ return Promise.resolve(_catch(function () {
1967
+ return Promise.resolve(navigator.permissions.query({
1968
+ name: 'clipboard-read'
1969
+ })).then(function (status) {
1970
+ if (status.state !== 'granted') return;
1971
+ hardRefresh();
1972
+ });
1973
+ }, function () {}));
1974
+ };
1975
+
1976
+ var hardRefresh = function hardRefresh() {
1977
+ try {
1978
+ var _temp3 = _catch(function () {
1979
+ return Promise.resolve(navigator.clipboard.read()).then(function (items) {
1980
+ var item = items[0];
1981
+
1982
+ var _temp = function () {
1983
+ if (item) {
1984
+ return Promise.resolve(parseClipboardTable(item)).then(function (peek) {
1985
+ setPeek(peek);
1986
+ });
1987
+ } else {
1988
+ setPeek(EMPTY_TABLE);
1989
+ }
1990
+ }();
1991
+
1992
+ if (_temp && _temp.then) return _temp.then(function () {});
1993
+ });
1994
+ }, function () {});
1767
1995
 
1768
- if (v.match(/^[\t\n]*$/)) {
1769
- v = ' ' + v;
1996
+ return Promise.resolve(_temp3 && _temp3.then ? _temp3.then(function () {}) : void 0);
1997
+ } catch (e) {
1998
+ return Promise.reject(e);
1999
+ }
2000
+ };
2001
+
2002
+ var delayedRefresh = function delayedRefresh() {
2003
+ return setTimeout(hardRefresh);
2004
+ };
2005
+
2006
+ window.document.addEventListener('cut', delayedRefresh);
2007
+ window.document.addEventListener('copy', delayedRefresh);
2008
+ window.document.addEventListener('focus', softRefresh);
2009
+ softRefresh();
2010
+ return function () {
2011
+ window.document.removeEventListener('cut', delayedRefresh);
2012
+ window.document.removeEventListener('copy', delayedRefresh);
2013
+ window.document.removeEventListener('focus', softRefresh);
2014
+ };
2015
+ }, []);
2016
+ var canPaste = useCallback(function () {
2017
+ var _peek$rows;
2018
+
2019
+ return !!(peek == null || (_peek$rows = peek.rows) !== null && _peek$rows !== void 0 && _peek$rows.length);
2020
+ }, [peek]);
2021
+ return {
2022
+ peek: peek,
2023
+ canPaste: canPaste,
2024
+ copyTable: copyClipboardTable,
2025
+ pasteTable: pasteClipboardTable
2026
+ };
2027
+ };
2028
+ var useClipboardAPI = function useClipboardAPI(selection, editData, cellReadOnly, addListener, onSelectionChange, onChange, onCopy, onPaste) {
2029
+ var _useClipboardTable = useClipboardTable(),
2030
+ canPaste = _useClipboardTable.canPaste,
2031
+ copyTable = _useClipboardTable.copyTable,
2032
+ pasteTable = _useClipboardTable.pasteTable;
2033
+
2034
+ var pasteIntoSelection = useCallback(function (selection, table) {
2035
+ try {
2036
+ var rows = table.rows,
2037
+ payload = table.payload;
2038
+
2039
+ var _normalizeSelection = normalizeSelection(selection),
2040
+ min = _normalizeSelection[0];
2041
+
2042
+ var minX = min[0],
2043
+ minY = min[1];
2044
+ var left = Math.max(0, minX);
2045
+ var top = Math.max(0, minY);
2046
+ var width = rows.reduce(function (a, b) {
2047
+ return Math.max(a, b.length);
2048
+ }, 0);
2049
+ var height = rows.length;
2050
+ var newSelection = [min, addXY(min, [width - 1, height - 1])];
2051
+ return Promise.resolve(onPaste === null || onPaste === void 0 ? void 0 : onPaste(newSelection, rows, payload)).then(function (shouldPaste) {
2052
+ if (shouldPaste !== false) {
2053
+ var changes = rows.flatMap(function (row, j) {
2054
+ return row.map(function (value, i) {
2055
+ var x = left + i;
2056
+ var y = top + j;
2057
+ return !(cellReadOnly !== null && cellReadOnly !== void 0 && cellReadOnly(x, y)) ? {
2058
+ x: x,
2059
+ y: y,
2060
+ value: value
2061
+ } : null;
2062
+ });
2063
+ }).filter(function (change) {
2064
+ return !!change;
2065
+ });
2066
+ onChange === null || onChange === void 0 ? void 0 : onChange(changes);
2067
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(newSelection);
2068
+ }
2069
+ });
2070
+ } catch (e) {
2071
+ return Promise.reject(e);
2072
+ }
2073
+ }, [onChange, onSelectionChange, cellReadOnly]);
2074
+ var copySelection = useCallback(function (selection, cut) {
2075
+ if (cut === void 0) {
2076
+ cut = false;
1770
2077
  }
1771
2078
 
1772
- textArea.value = v;
1773
- }, [selection, editMode, editData, textAreaRef]);
1774
- useLayoutEffect(function () {
1775
- var textArea = textAreaRef.current;
1776
- if (!textArea) return;
2079
+ try {
2080
+ var _payload$cut;
1777
2081
 
1778
- var focus = function focus() {
1779
- textArea.focus({
1780
- preventScroll: true
2082
+ var rows = formatSelectionAsRows(selection, editData);
2083
+ var payload = onCopy === null || onCopy === void 0 ? void 0 : onCopy(selection, rows, cut);
2084
+ copyTable(rows, payload);
2085
+
2086
+ if ((_payload$cut = payload === null || payload === void 0 ? void 0 : payload.cut) != null ? _payload$cut : cut) {
2087
+ var changes = [];
2088
+ forSelectionRows(selection)(function (y) {
2089
+ forSelectionColumns(selection)(function (x) {
2090
+ if (!(cellReadOnly !== null && cellReadOnly !== void 0 && cellReadOnly(x, y))) {
2091
+ var change = {
2092
+ x: x,
2093
+ y: y,
2094
+ value: ''
2095
+ };
2096
+ changes.push(change);
2097
+ }
2098
+ });
2099
+ });
2100
+ onChange === null || onChange === void 0 ? void 0 : onChange(changes);
2101
+ }
2102
+
2103
+ return Promise.resolve();
2104
+ } catch (e) {
2105
+ return Promise.reject(e);
2106
+ }
2107
+ }, [onCopy, onChange, cellReadOnly]);
2108
+ var pasteSelection = useCallback(function (selection) {
2109
+ try {
2110
+ return Promise.resolve(pasteTable()).then(function (table) {
2111
+ if (table) pasteIntoSelection(selection, table);
1781
2112
  });
1782
- textArea.select();
1783
- };
2113
+ } catch (e) {
2114
+ return Promise.reject(e);
2115
+ }
2116
+ }, [pasteIntoSelection]);
1784
2117
 
1785
- if (editMode) return;
1786
- if (document.activeElement === textArea) return;
1787
- var activeTagName = document.activeElement.tagName.toLowerCase();
2118
+ var onClipboardCopy = function onClipboardCopy(cut) {
2119
+ if (isEmptySelection(selection)) return;
2120
+ return copySelection(selection, cut);
2121
+ };
1788
2122
 
1789
- if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1790
- focus();
2123
+ var onClipboardPaste = function onClipboardPaste(e) {
2124
+ try {
2125
+ e.preventDefault();
2126
+ var clipboardData = e.clipboardData || window.clipboardData;
2127
+ return Promise.resolve(parseClipboardTable(clipboardData)).then(function (table) {
2128
+ if (table) pasteIntoSelection(selection, table);
2129
+ });
2130
+ } catch (e) {
2131
+ return Promise.reject(e);
1791
2132
  }
2133
+ };
2134
+
2135
+ useLayoutEffect(function () {
2136
+ if (!addListener) return;
2137
+ window.document.addEventListener('paste', onClipboardPaste);
2138
+ return function () {
2139
+ window.document.removeEventListener('paste', onClipboardPaste);
2140
+ };
1792
2141
  });
2142
+ var clipboardApi = useMemo(function () {
2143
+ return {
2144
+ copySelection: copySelection,
2145
+ pasteSelection: pasteSelection,
2146
+ canPasteSelection: canPaste
2147
+ };
2148
+ }, [copySelection, pasteSelection, canPaste]);
2149
+ return {
2150
+ clipboardApi: clipboardApi,
2151
+ onClipboardCopy: onClipboardCopy,
2152
+ onClipboardPaste: onClipboardPaste
2153
+ };
2154
+ };
2155
+
2156
+ var copyClipboardTable = function copyClipboardTable(rows, payload) {
2157
+ try {
2158
+ var text = formatRowsAsTSV(rows);
2159
+ var html = formatRowsAsHTML(rows, payload != null ? payload : undefined);
2160
+ return Promise.resolve(navigator.clipboard.write([new ClipboardItem({
2161
+ 'text/html': new Blob([html], {
2162
+ type: 'text/html'
2163
+ }),
2164
+ 'text/plain': new Blob([text], {
2165
+ type: 'text/plain'
2166
+ })
2167
+ })])).then(function () {
2168
+ var event = new Event('copy');
2169
+ document.dispatchEvent(event);
2170
+ });
2171
+ } catch (e) {
2172
+ return Promise.reject(e);
2173
+ }
1793
2174
  };
1794
- var useClipboardPaste = function useClipboardPaste(textAreaRef, selection, onSelectionChange, onChange, isReadOnly) {
1795
- useEffect(function () {
1796
- var onPaste = function onPaste(e) {
1797
- var textArea = textAreaRef.current;
1798
- if (!textArea) return;
1799
- if (e.target !== textArea) return;
1800
- e.preventDefault();
1801
- var clipboardData = e.clipboardData || window.clipboardData;
1802
- var types = clipboardData.types;
1803
- var parsed;
1804
-
1805
- if (types.includes('text/html')) {
1806
- var pastedHtml = clipboardData.getData('text/html');
1807
- parsed = parsePastedHtml(selection, pastedHtml, isReadOnly);
1808
- } else if (types.includes('text/plain')) {
1809
- var text = clipboardData.getData('text/plain');
1810
- parsed = parsePastedText(selection, text, isReadOnly);
1811
- }
1812
2175
 
1813
- if (!parsed) return;
1814
- var _parsed = parsed,
1815
- s = _parsed.selection,
1816
- changes = _parsed.changes;
1817
- onChange === null || onChange === void 0 ? void 0 : onChange(changes);
1818
- onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(s);
2176
+ var pasteClipboardTable = function pasteClipboardTable() {
2177
+ try {
2178
+ return Promise.resolve(navigator.clipboard.read()).then(function (items) {
2179
+ var item = items[0];
2180
+ if (!item) return;
2181
+ return parseClipboardTable(item);
2182
+ });
2183
+ } catch (e) {
2184
+ return Promise.reject(e);
2185
+ }
2186
+ };
2187
+
2188
+ var parseClipboardTable = function parseClipboardTable(item) {
2189
+ try {
2190
+ var _temp7 = function _temp7() {
2191
+ return rows ? {
2192
+ rows: rows,
2193
+ payload: payload
2194
+ } : {
2195
+ rows: []
2196
+ };
1819
2197
  };
1820
2198
 
1821
- window.document.addEventListener('paste', onPaste);
1822
- return function () {
1823
- window.document.removeEventListener('paste', onPaste);
2199
+ var has = function has(type) {
2200
+ return item.types.includes(type);
2201
+ };
2202
+
2203
+ var get = function get(type) {
2204
+ if ('getData' in item) return item.getData(type);else if ('getType' in item) return item.getType(type).then(function (blob) {
2205
+ return blob.text();
2206
+ });
2207
+ return '';
1824
2208
  };
1825
- }, [textAreaRef, selection]);
2209
+
2210
+ var rows, payload;
2211
+
2212
+ var _temp8 = function () {
2213
+ if (has('text/html')) {
2214
+ return Promise.resolve(get('text/html')).then(function (pastedHtml) {
2215
+ var _parsePastedHtml = parsePastedHtml(pastedHtml);
2216
+
2217
+ rows = _parsePastedHtml.rows;
2218
+ payload = _parsePastedHtml.payload;
2219
+ });
2220
+ } else {
2221
+ var _temp9 = function () {
2222
+ if (has('text/plain')) {
2223
+ return Promise.resolve(get('text/plain')).then(function (text) {
2224
+ rows = parsePastedText(text);
2225
+ });
2226
+ }
2227
+ }();
2228
+
2229
+ if (_temp9 && _temp9.then) return _temp9.then(function () {});
2230
+ }
2231
+ }();
2232
+
2233
+ return Promise.resolve(_temp8 && _temp8.then ? _temp8.then(_temp7) : _temp7(_temp8));
2234
+ } catch (e) {
2235
+ return Promise.reject(e);
2236
+ }
1826
2237
  };
1827
2238
 
1828
- var formatTSV = function formatTSV(rows) {
2239
+ var formatRowsAsTSV = function formatRowsAsTSV(rows) {
1829
2240
  return rows.map(function (row) {
1830
2241
  return row.join('\t');
1831
2242
  }).join('\n');
1832
2243
  };
1833
2244
 
1834
- var formatSelectionAsTSV = function formatSelectionAsTSV(selection, editData) {
1835
- if (isEmptySelection(selection)) return '';
2245
+ var formatRowsAsHTML = function formatRowsAsHTML(rows, payload) {
2246
+ var trs = rows.map(function (row) {
2247
+ var tds = row.map(formatTextAsHTML).map(function (cell) {
2248
+ return "<td>" + cell + "</td>";
2249
+ });
2250
+ return tds.join('');
2251
+ }).map(function (row) {
2252
+ return "<tr>" + row + "</tr>";
2253
+ }).join('\n');
2254
+ var table = "<table>" + trs + "</table>";
1836
2255
 
1837
- var _normalizeSelection = normalizeSelection(selection),
1838
- _normalizeSelection$ = _normalizeSelection[0],
1839
- minX = _normalizeSelection$[0],
1840
- minY = _normalizeSelection$[1],
1841
- _normalizeSelection$2 = _normalizeSelection[1],
1842
- maxX = _normalizeSelection$2[0],
1843
- maxY = _normalizeSelection$2[1];
2256
+ if (payload) {
2257
+ var extra = "<SheetHappens payload=\"" + formatTextAsHTML(JSON.stringify(payload)) + "\"></SheetHappens>";
2258
+ return extra + table;
2259
+ }
2260
+
2261
+ return table;
2262
+ };
2263
+
2264
+ var formatTextAsHTML = function formatTextAsHTML(s) {
2265
+ return s.replace(/[&"'<>]/g, function (i) {
2266
+ return "&#" + i.charCodeAt(0) + ";";
2267
+ });
2268
+ };
2269
+
2270
+ var formatSelectionAsRows = function formatSelectionAsRows(selection, editData) {
2271
+ if (isEmptySelection(selection)) return [];
2272
+
2273
+ var _normalizeSelection2 = normalizeSelection(selection),
2274
+ _normalizeSelection2$ = _normalizeSelection2[0],
2275
+ minX = _normalizeSelection2$[0],
2276
+ minY = _normalizeSelection2$[1],
2277
+ _normalizeSelection2$2 = _normalizeSelection2[1],
2278
+ maxX = _normalizeSelection2$2[0],
2279
+ maxY = _normalizeSelection2$2[1];
1844
2280
 
1845
2281
  if (isMaybeRowSelection(selection)) {
1846
2282
  var _findApproxMaxEditDat = findApproxMaxEditDataIndex(editData),
@@ -1874,132 +2310,94 @@ var formatSelectionAsTSV = function formatSelectionAsTSV(selection, editData) {
1874
2310
  rows.push(row);
1875
2311
  }
1876
2312
 
1877
- return formatTSV(rows);
2313
+ return rows;
1878
2314
  };
1879
2315
 
1880
- var findTable = function findTable(element) {
2316
+ var findTag = function findTag(element, tagName) {
1881
2317
  for (var _iterator = _createForOfIteratorHelperLoose(element.children), _step; !(_step = _iterator()).done;) {
1882
2318
  var child = _step.value;
1883
2319
 
1884
- if (child.nodeName === 'TABLE') {
2320
+ if (child.nodeName === tagName) {
1885
2321
  return child;
1886
2322
  }
1887
2323
 
1888
- var maybeTable = findTable(child);
2324
+ var maybeTag = findTag(child, tagName);
1889
2325
 
1890
- if (maybeTable) {
1891
- return maybeTable;
2326
+ if (maybeTag) {
2327
+ return maybeTag;
1892
2328
  }
1893
2329
  }
1894
2330
  };
1895
2331
 
1896
- var parsePastedHtml = function parsePastedHtml(selection, html, isReadOnly) {
2332
+ var parsePastedHtml = function parsePastedHtml(html) {
1897
2333
  var div = document.createElement('div');
1898
2334
  div.innerHTML = html.trim();
2335
+ var rows = [];
2336
+ var payload = undefined;
2337
+ var sheetNode = findTag(div, 'SHEETHAPPENS');
1899
2338
 
1900
- var _normalizeSelection2 = normalizeSelection(selection),
1901
- _normalizeSelection2$ = _normalizeSelection2[0],
1902
- minX = _normalizeSelection2$[0],
1903
- minY = _normalizeSelection2$[1];
1904
-
1905
- var left = isMaybeRowSelection(selection) ? 0 : minX;
1906
- var top = isMaybeColumnSelection(selection) ? 0 : minY;
1907
- var changes = [];
1908
- var tableNode = findTable(div);
2339
+ if (sheetNode) {
2340
+ var json = sheetNode.getAttribute('payload');
1909
2341
 
1910
- if (!tableNode) {
1911
- return null;
2342
+ try {
2343
+ payload = JSON.parse(json);
2344
+ } catch (e) {}
1912
2345
  }
1913
2346
 
1914
- var right = left;
1915
- var bottom = top;
1916
- var y = top;
2347
+ var tableNode = findTag(div, 'TABLE');
2348
+
2349
+ if (tableNode) {
2350
+ for (var _iterator2 = _createForOfIteratorHelperLoose(tableNode.children), _step2; !(_step2 = _iterator2()).done;) {
2351
+ var tableChild = _step2.value;
1917
2352
 
1918
- for (var _iterator2 = _createForOfIteratorHelperLoose(tableNode.children), _step2; !(_step2 = _iterator2()).done;) {
1919
- var tableChild = _step2.value;
2353
+ if (tableChild.nodeName === 'TBODY') {
2354
+ for (var _iterator3 = _createForOfIteratorHelperLoose(tableChild.children), _step3; !(_step3 = _iterator3()).done;) {
2355
+ var tr = _step3.value;
1920
2356
 
1921
- if (tableChild.nodeName === 'TBODY') {
1922
- for (var _iterator3 = _createForOfIteratorHelperLoose(tableChild.children), _step3; !(_step3 = _iterator3()).done;) {
1923
- var tr = _step3.value;
1924
- var x = left;
2357
+ if (tr.nodeName === 'TR') {
2358
+ var row = [];
1925
2359
 
1926
- if (tr.nodeName === 'TR') {
1927
- for (var _iterator4 = _createForOfIteratorHelperLoose(tr.children), _step4; !(_step4 = _iterator4()).done;) {
1928
- var td = _step4.value;
2360
+ for (var _iterator4 = _createForOfIteratorHelperLoose(tr.children), _step4; !(_step4 = _iterator4()).done;) {
2361
+ var td = _step4.value;
1929
2362
 
1930
- if (td.nodeName === 'TD') {
1931
- var str = '';
2363
+ if (td.nodeName === 'TD') {
2364
+ var str = '';
1932
2365
 
1933
- if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1934
- var p = td.children[0];
2366
+ if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
2367
+ var p = td.children[0];
1935
2368
 
1936
- if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1937
- str = p.children[0].textContent.trim();
2369
+ if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
2370
+ str = p.children[0].textContent.trim();
2371
+ } else {
2372
+ str = p.textContent.trim();
2373
+ }
1938
2374
  } else {
1939
- str = p.textContent.trim();
2375
+ str = td.textContent.trim();
1940
2376
  }
1941
- } else {
1942
- str = td.textContent.trim();
1943
- }
1944
2377
 
1945
- str = str.replaceAll('\n', '');
1946
- str = str.replaceAll(/\s\s+/g, ' ');
1947
- if (!(isReadOnly !== null && isReadOnly !== void 0 && isReadOnly(x, y))) changes.push({
1948
- x: x,
1949
- y: y,
1950
- value: str
1951
- });
1952
- x++;
2378
+ str = str.replaceAll('\n', '');
2379
+ str = str.replaceAll(/\s\s+/g, ' ');
2380
+ row.push(str);
2381
+ }
1953
2382
  }
1954
- }
1955
2383
 
1956
- y++;
2384
+ rows.push(row);
2385
+ }
1957
2386
  }
1958
-
1959
- right = Math.max(right, x - 1);
1960
2387
  }
1961
2388
  }
1962
2389
  }
1963
2390
 
1964
- bottom = Math.max(top, y - 1);
1965
2391
  return {
1966
- selection: [[left, top], [right, bottom]],
1967
- changes: changes
2392
+ rows: rows,
2393
+ payload: payload
1968
2394
  };
1969
2395
  };
1970
2396
 
1971
- var parsePastedText = function parsePastedText(selection, text, isReadOnly) {
1972
- var _normalizeSelection3 = normalizeSelection(selection),
1973
- _normalizeSelection3$ = _normalizeSelection3[0],
1974
- minX = _normalizeSelection3$[0],
1975
- minY = _normalizeSelection3$[1];
1976
-
1977
- var left = isMaybeRowSelection(selection) ? 0 : minX;
1978
- var top = isMaybeColumnSelection(selection) ? 0 : minY;
1979
- var rows = text.split(/\r?\n/);
1980
- var right = left;
1981
- var bottom = top + rows.length - 1;
1982
- var changes = [];
1983
-
1984
- for (var y = 0; y < rows.length; y++) {
1985
- var cols = rows[y].split('\t');
1986
- right = Math.max(right, left + cols.length - 1);
1987
-
1988
- for (var x = 0; x < cols.length; x++) {
1989
- var X = left + x;
1990
- var Y = top + y;
1991
- if (!(isReadOnly !== null && isReadOnly !== void 0 && isReadOnly(X, Y))) changes.push({
1992
- x: X,
1993
- y: Y,
1994
- value: cols[x]
1995
- });
1996
- }
1997
- }
1998
-
1999
- return {
2000
- selection: [[left, top], [right, bottom]],
2001
- changes: changes
2002
- };
2397
+ var parsePastedText = function parsePastedText(text) {
2398
+ return text.split(/\r?\n/).map(function (line) {
2399
+ return line.split('\t');
2400
+ });
2003
2401
  };
2004
2402
 
2005
2403
  var INITIAL_SIZE = 256;
@@ -2382,7 +2780,40 @@ var makeIntMap = function makeIntMap(initialSize) {
2382
2780
  };
2383
2781
  };
2384
2782
 
2385
- var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragIndices, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset) {
2783
+ var resolveSheetStyle = function resolveSheetStyle(sheetStyle) {
2784
+ var _sheetStyle$shadowBlu, _sheetStyle$shadowOpa, _sheetStyle$shadowCol;
2785
+
2786
+ return {
2787
+ freezeColumns: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeColumns) || 0,
2788
+ freezeRows: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeRows) || 0,
2789
+ hideColumnHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideColumnHeaders) || false,
2790
+ hideRowHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideRowHeaders) || false,
2791
+ hideGridlines: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideGridlines) || false,
2792
+ hideScrollBars: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideScrollBars) || false,
2793
+ columnHeaderHeight: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideColumnHeaders ? 1 : SIZES.headerHeight,
2794
+ rowHeaderWidth: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideRowHeaders ? 1 : SIZES.headerWidth,
2795
+ shadowBlur: (_sheetStyle$shadowBlu = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowBlur) != null ? _sheetStyle$shadowBlu : SIZES.shadowBlur,
2796
+ shadowOpacity: (_sheetStyle$shadowOpa = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowOpacity) != null ? _sheetStyle$shadowOpa : SIZES.shadowOpacity,
2797
+ shadowColor: (_sheetStyle$shadowCol = sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.shadowColor) != null ? _sheetStyle$shadowCol : COLORS.shadowColor
2798
+ };
2799
+ };
2800
+ var applyAlignment = function applyAlignment(start, cellSize, style, imageWidth, alignment) {
2801
+ if (alignment === void 0) {
2802
+ alignment = style.textAlign;
2803
+ }
2804
+
2805
+ if (alignment === 'left') {
2806
+ return start + style.marginLeft;
2807
+ } else if (alignment === 'center') {
2808
+ return start + cellSize * 0.5 - imageWidth / 2;
2809
+ } else if (alignment === 'right') {
2810
+ return start + (cellSize - style.marginRight - imageWidth);
2811
+ }
2812
+
2813
+ return start;
2814
+ };
2815
+
2816
+ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, isFocused, knobPosition, knobArea, dragIndices, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset) {
2386
2817
  var canvas = context.canvas;
2387
2818
  var width = canvas.width,
2388
2819
  height = canvas.height;
@@ -2544,11 +2975,13 @@ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetS
2544
2975
  var isGroupSelected = rowSelectionActive && isInRowGroup;
2545
2976
  var style = isSelfSelected ? HEADER_SELECTED_STYLE : isGroupSelected ? HEADER_GROUP_SELECTED_STYLE : isActive ? HEADER_ACTIVE_STYLE : NO_STYLE;
2546
2977
 
2978
+ var resolvedStyle = _extends({}, DEFAULT_COLUMN_HEADER_STYLE, style);
2979
+
2547
2980
  var _top2 = rowToPixel(row);
2548
2981
 
2549
2982
  var _bottom2 = rowToPixel(row, 1);
2550
2983
 
2551
- clickables.push.apply(clickables, renderCell(context, content, style, DEFAULT_COLUMN_HEADER_STYLE, 0, _top2, rowHeaderWidth, _bottom2 - _top2));
2984
+ clickables.push.apply(clickables, renderCell(context, content, resolvedStyle, 0, _top2, rowHeaderWidth, _bottom2 - _top2));
2552
2985
  }
2553
2986
  }
2554
2987
 
@@ -2561,8 +2994,6 @@ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetS
2561
2994
 
2562
2995
  var column = _step5.value;
2563
2996
 
2564
- var _content = (_columnHeaders = columnHeaders(column)) != null ? _columnHeaders : excelHeaderString(column + 1);
2565
-
2566
2997
  var _isActive = isInRange(column, minX, maxX);
2567
2998
 
2568
2999
  var _groupKey = columnGroupKeys(column);
@@ -2572,19 +3003,22 @@ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetS
2572
3003
  var selectedStyle = isSelected ? HEADER_SELECTED_STYLE : NO_STYLE;
2573
3004
  var activeStyle = _isActive ? HEADER_ACTIVE_STYLE : NO_STYLE;
2574
3005
 
2575
- var _style = _extends({}, columnHeaderStyle(column), activeStyle, selectedStyle);
3006
+ var _style = _extends({}, DEFAULT_COLUMN_HEADER_STYLE, columnHeaderStyle(column), activeStyle, selectedStyle);
2576
3007
 
2577
3008
  var _left2 = columnToPixel(column);
2578
3009
 
2579
3010
  var _right2 = columnToPixel(column, 1);
2580
3011
 
2581
- clickables.push.apply(clickables, renderCell(context, _content, _style, DEFAULT_COLUMN_HEADER_STYLE, _left2, 0, _right2 - _left2, columnHeaderHeight));
3012
+ var _content = (_columnHeaders = columnHeaders(column, _style)) != null ? _columnHeaders : excelHeaderString(column + 1);
3013
+
3014
+ clickables.push.apply(clickables, renderCell(context, _content, _style, _left2, 0, _right2 - _left2, columnHeaderHeight));
2582
3015
  }
2583
3016
  }
2584
3017
 
2585
3018
  if (selectionActive) {
2586
3019
  context.strokeStyle = COLORS.selectionBorder;
2587
3020
  context.lineWidth = 2;
3021
+ context.globalAlpha = isFocused ? 1 : 0.5;
2588
3022
  var _selected$7 = selected[0],
2589
3023
  _left3 = _selected$7[0],
2590
3024
  _top3 = _selected$7[1],
@@ -2592,6 +3026,7 @@ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetS
2592
3026
  _right3 = _selected$8[0],
2593
3027
  _bottom3 = _selected$8[1];
2594
3028
  context.strokeRect(_left3, _top3, _right3 - _left3 - 1, _bottom3 - _top3 - 1);
3029
+ context.globalAlpha = 1;
2595
3030
  }
2596
3031
 
2597
3032
  for (var _iterator6 = _createForOfIteratorHelperLoose(secondarySelections), _step6; !(_step6 = _iterator6()).done;) {
@@ -2741,43 +3176,42 @@ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetS
2741
3176
 
2742
3177
  var _bottom10 = rowToPixel(_y, 1);
2743
3178
 
2744
- var cellContent = displayData(_x, _y);
3179
+ var _style2 = _extends({}, DEFAULT_CELL_STYLE, cellStyle(_x, _y));
2745
3180
 
2746
- if (cellContent !== null && cellContent !== undefined) {
2747
- var _style2 = cellStyle(_x, _y);
3181
+ var cellContent = displayData(_x, _y, _style2);
2748
3182
 
2749
- clickables.push.apply(clickables, renderCell(context, cellContent, _style2, DEFAULT_CELL_STYLE, _left9, _top9, _right10 - _left9, _bottom10 - _top9));
3183
+ if (cellContent !== null && cellContent !== undefined) {
3184
+ clickables.push.apply(clickables, renderCell(context, cellContent, _style2, _left9, _top9, _right10 - _left9, _bottom10 - _top9));
2750
3185
  }
2751
3186
  }
2752
3187
  }
2753
3188
 
2754
3189
  return clickables;
2755
3190
  };
2756
- var renderCell = function renderCell(context, cellContent, style, defaultCellStyle, xCoord, yCoord, cellWidth, cellHeight) {
3191
+ var renderCell = function renderCell(context, cellContent, style, xCoord, yCoord, cellWidth, cellHeight) {
2757
3192
  var clickables = [];
2758
3193
 
2759
3194
  if (cellContent === null) {
2760
3195
  return clickables;
2761
3196
  }
2762
3197
 
2763
- var finalStyle = resolveCellStyle(style, defaultCellStyle);
2764
- context.fillStyle = finalStyle.color;
2765
- context.font = finalStyle.weight + ' ' + finalStyle.fontSize + 'px ' + finalStyle.fontFamily;
2766
- context.textAlign = finalStyle.textAlign;
3198
+ context.fillStyle = style.color;
3199
+ context.font = style.weight + ' ' + style.fontSize + 'px ' + style.fontFamily;
3200
+ context.textAlign = style.textAlign;
2767
3201
  var yy = Math.floor(yCoord + cellHeight * 0.5);
2768
3202
  context.save();
2769
3203
  context.beginPath();
2770
3204
  context.rect(xCoord, yCoord, cellWidth, cellHeight);
2771
3205
  context.clip();
2772
3206
 
2773
- if (finalStyle.backgroundColor !== '') {
2774
- context.fillStyle = finalStyle.backgroundColor;
3207
+ if (style.backgroundColor !== '') {
3208
+ context.fillStyle = style.backgroundColor;
2775
3209
  context.fillRect(xCoord, yCoord, cellWidth, cellHeight);
2776
- context.fillStyle = finalStyle.color;
3210
+ context.fillStyle = style.color;
2777
3211
  }
2778
3212
 
2779
3213
  if (typeof cellContent === 'string' || typeof cellContent === 'number') {
2780
- var xx = applyAlignment(xCoord, cellWidth, finalStyle, 0);
3214
+ var xx = applyAlignment(xCoord, cellWidth, style, 0);
2781
3215
  var text = '' + cellContent;
2782
3216
  context.fillText(text, xx, yy);
2783
3217
  } else if (typeof cellContent === 'object') {
@@ -2791,7 +3225,7 @@ var renderCell = function renderCell(context, cellContent, style, defaultCellSty
2791
3225
  if (obj.content instanceof HTMLImageElement) {
2792
3226
  w = obj.width || cellWidth;
2793
3227
  h = obj.height || cellHeight;
2794
- var finalX = applyAlignment(xCoord, cellWidth, finalStyle, w, obj.horizontalAlign);
3228
+ var finalX = applyAlignment(xCoord, cellWidth, style, w, obj.horizontalAlign);
2795
3229
  x = finalX + obj.x;
2796
3230
  y = yy + obj.y;
2797
3231
  context.drawImage(obj.content, x, y, w, h);
@@ -2800,7 +3234,7 @@ var renderCell = function renderCell(context, cellContent, style, defaultCellSty
2800
3234
  context.textAlign = obj.horizontalAlign;
2801
3235
  }
2802
3236
 
2803
- var _finalX = applyAlignment(xCoord, cellWidth, finalStyle, 0, obj.horizontalAlign);
3237
+ var _finalX = applyAlignment(xCoord, cellWidth, style, 0, obj.horizontalAlign);
2804
3238
 
2805
3239
  var _text = '' + obj.content;
2806
3240
 
@@ -3026,8 +3460,8 @@ var Sheet = forwardRef(function (props, ref) {
3026
3460
  var selectionProp = (_props$selection = props.selection) != null ? _props$selection : NO_SELECTION;
3027
3461
 
3028
3462
  var _useState3 = useState(selectionProp),
3029
- selection = _useState3[0],
3030
- setSelection = _useState3[1];
3463
+ rawSelection = _useState3[0],
3464
+ setRawSelection = _useState3[1];
3031
3465
 
3032
3466
  var _useState4 = useState(null),
3033
3467
  knobArea = _useState4[0],
@@ -3049,22 +3483,26 @@ var Sheet = forwardRef(function (props, ref) {
3049
3483
  editCell = _useState8[0],
3050
3484
  setEditCell = _useState8[1];
3051
3485
 
3052
- var _useState9 = useState(selectionProp),
3053
- lastSelectionProp = _useState9[0],
3054
- setLastSelectionProp = _useState9[1];
3486
+ var _useState9 = useState(!!props.autoFocus),
3487
+ focused = _useState9[0],
3488
+ setFocused = _useState9[1];
3489
+
3490
+ var _useState10 = useState(selectionProp),
3491
+ lastSelectionProp = _useState10[0],
3492
+ setLastSelectionProp = _useState10[1];
3055
3493
 
3056
3494
  if (lastSelectionProp !== selectionProp) {
3057
3495
  setLastSelectionProp(selectionProp);
3058
- setSelection(selectionProp);
3496
+ setRawSelection(selectionProp);
3059
3497
  }
3060
3498
 
3061
- var _useState10 = useState(''),
3062
- editValue = _useState10[0],
3063
- setEditValue = _useState10[1];
3499
+ var _useState11 = useState(''),
3500
+ editValue = _useState11[0],
3501
+ setEditValue = _useState11[1];
3064
3502
 
3065
- var _useState11 = useState(false),
3066
- arrowKeyCommitMode = _useState11[0],
3067
- setArrowKeyCommitMode = _useState11[1];
3503
+ var _useState12 = useState(false),
3504
+ arrowKeyCommitMode = _useState12[0],
3505
+ setArrowKeyCommitMode = _useState12[1];
3068
3506
 
3069
3507
  var _useResizeObserver = useResizeObserver({
3070
3508
  ref: overlayRef
@@ -3081,7 +3519,7 @@ var Sheet = forwardRef(function (props, ref) {
3081
3519
  return createRowOrColumnProp(props.cellHeight, 22);
3082
3520
  }, [props.cellHeight]);
3083
3521
  var columnHeaders = useMemo(function () {
3084
- return createRowOrColumnProp(props.columnHeaders, null);
3522
+ return createRowOrColumnStyledProp(props.columnHeaders, null);
3085
3523
  }, [props.columnHeaders]);
3086
3524
  var columnHeaderStyle = useMemo(function () {
3087
3525
  return createRowOrColumnProp(props.columnHeaderStyle, {});
@@ -3111,7 +3549,7 @@ var Sheet = forwardRef(function (props, ref) {
3111
3549
  return createCellProp(props.sourceData, null);
3112
3550
  }, [props.sourceData]);
3113
3551
  var displayData = useMemo(function () {
3114
- return createCellProp(props.displayData, '');
3552
+ return createCellStyledProp(props.displayData, '');
3115
3553
  }, [props.displayData]);
3116
3554
  var editData = useMemo(function () {
3117
3555
  return createCellProp(props.editData, '');
@@ -3126,6 +3564,9 @@ var Sheet = forwardRef(function (props, ref) {
3126
3564
  return resolveSheetStyle(props.sheetStyle);
3127
3565
  }, [props.sheetStyle]);
3128
3566
  var secondarySelections = (_props$secondarySelec = props.secondarySelections) != null ? _props$secondarySelec : NO_SELECTIONS;
3567
+ var selection = useMemo(function () {
3568
+ return validateSelection(rawSelection);
3569
+ }, [rawSelection]);
3129
3570
  var selectedColumnGroups = useMemo(function () {
3130
3571
  return props.columnGroupKeys ? new Set(mapSelectionColumns(selection)(function (x) {
3131
3572
  return columnGroupKeys(x);
@@ -3204,7 +3645,7 @@ var Sheet = forwardRef(function (props, ref) {
3204
3645
  }
3205
3646
 
3206
3647
  if (!isSameSelection(selection, newSelection)) {
3207
- setSelection(validateSelection(newSelection));
3648
+ setRawSelection(newSelection);
3208
3649
  }
3209
3650
 
3210
3651
  var overlay = overlayRef.current;
@@ -3215,7 +3656,7 @@ var Sheet = forwardRef(function (props, ref) {
3215
3656
  }
3216
3657
 
3217
3658
  if (props.onSelectionChanged) {
3218
- var _normalizeSelection = normalizeSelection(newSelection),
3659
+ var _normalizeSelection = normalizeSelection(validateSelection(newSelection)),
3219
3660
  _normalizeSelection$ = _normalizeSelection[0],
3220
3661
  minX = _normalizeSelection$[0],
3221
3662
  minY = _normalizeSelection$[1],
@@ -3227,6 +3668,11 @@ var Sheet = forwardRef(function (props, ref) {
3227
3668
  }
3228
3669
  };
3229
3670
 
3671
+ var cancelEditingCell = function cancelEditingCell() {
3672
+ setEditCell(NO_CELL);
3673
+ setFocused(true);
3674
+ };
3675
+
3230
3676
  var commitEditingCell = function commitEditingCell(value) {
3231
3677
  if (props.onChange) {
3232
3678
  var cellX = editCell[0],
@@ -3239,6 +3685,7 @@ var Sheet = forwardRef(function (props, ref) {
3239
3685
  }
3240
3686
 
3241
3687
  setEditCell(NO_CELL);
3688
+ setFocused(true);
3242
3689
  };
3243
3690
 
3244
3691
  var startEditingCell = function startEditingCell(editCell, arrowKeyCommitMode) {
@@ -3276,16 +3723,25 @@ var Sheet = forwardRef(function (props, ref) {
3276
3723
  setDataOffset(clipDataOffset(view, dataOffset, freeze, [maxColumns, maxRows], cellLayout));
3277
3724
  }, [maxRows, maxColumns]);
3278
3725
  var hitmapRef = useRef(NO_CLICKABLES);
3279
- var textAreaRef = useRef(null);
3280
- useClipboardCopy(textAreaRef, selection, editMode, editData);
3281
- useClipboardPaste(textAreaRef, selection, changeSelection, props.onChange, cellReadOnly);
3726
+ var isFocused = focused || editMode;
3727
+
3728
+ var _useClipboardAPI = useClipboardAPI(selection, editData, cellReadOnly, isFocused && !editMode, changeSelection, props.onChange, props.onCopy, props.onPaste),
3729
+ clipboardApi = _useClipboardAPI.clipboardApi,
3730
+ onClipboardCopy = _useClipboardAPI.onClipboardCopy;
3731
+
3282
3732
  var onScroll = useScroll(dataOffset, maxScroll, cellLayout, setDataOffset, setMaxScroll);
3283
3733
  var getAutoSizeWidth = useAutoSizeColumn(visibleCells.rows, displayData, cellStyle, columnHeaders, columnHeaderStyle, canvasWidth);
3284
3734
 
3285
- var _useMouse = useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, cellReadOnly, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, getAutoSizeWidth, startEditingCell, commitEditingCell, setKnobArea, setDragIndices, setDragOffset, setDropTarget, changeSelection, props.cacheLayout ? columnLayout.clearAfter : undefined, props.cacheLayout ? rowLayout.clearAfter : undefined, props.onChange, props.onColumnOrderChange, props.onRowOrderChange, props.onCellWidthChange, props.onCellHeightChange, props.onRightClick, props.dontCommitEditOnSelectionChange, props.dontChangeSelectionOnOrderChange),
3735
+ var _useMouse = useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, cellReadOnly, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, getAutoSizeWidth, startEditingCell, commitEditingCell, setKnobArea, setDragIndices, setDragOffset, setDropTarget, changeSelection, setFocused, props.cacheLayout ? columnLayout.clearAfter : undefined, props.cacheLayout ? rowLayout.clearAfter : undefined, props.onChange, props.onColumnOrderChange, props.onRowOrderChange, props.onCellWidthChange, props.onCellHeightChange, props.onRightClick, props.dontCommitEditOnSelectionChange, props.dontChangeSelectionOnOrderChange),
3286
3736
  mouseHandlers = _useMouse.mouseHandlers,
3287
3737
  knobPosition = _useMouse.knobPosition;
3288
3738
 
3739
+ var _useKeyboard = useKeyboard(arrowKeyCommitMode, overlayRef, cellReadOnly, displayData, editCell, editMode, focused, rawSelection, selection, startEditingCell, commitEditingCell, cancelEditingCell, changeSelection, setFocused, onClipboardCopy, props.onChange),
3740
+ onInputKeyDown = _useKeyboard.onInputKeyDown,
3741
+ onGridKeyDown = _useKeyboard.onGridKeyDown,
3742
+ onGridFocus = _useKeyboard.onGridFocus,
3743
+ onGridBlur = _useKeyboard.onGridBlur;
3744
+
3289
3745
  useLayoutEffect(function () {
3290
3746
  var canvas = canvasRef.current;
3291
3747
 
@@ -3300,130 +3756,16 @@ var Sheet = forwardRef(function (props, ref) {
3300
3756
  }
3301
3757
 
3302
3758
  var animationFrameId = window.requestAnimationFrame(function () {
3303
- hitmapRef.current = renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragIndices, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset);
3759
+ hitmapRef.current = renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, isFocused, knobPosition, knobArea, dragIndices, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset);
3304
3760
  });
3305
3761
  return function () {
3306
3762
  window.cancelAnimationFrame(animationFrameId);
3307
3763
  };
3308
- }, [cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset]);
3309
-
3310
- var onKeyDown = function onKeyDown(e) {
3311
- if (e.key === 'Escape') {
3312
- setEditCell(NO_CELL);
3313
- return;
3314
- }
3315
-
3316
- var direction = e.key === 'Enter' ? 'down' : e.key === 'Tab' ? 'right' : arrowKeyCommitMode ? ARROW_KEYS[e.key] : null;
3317
-
3318
- if (direction) {
3319
- e.preventDefault();
3320
- var step = getDirectionStep(direction);
3321
- var head = maxXY(addXY(editCell, step), ORIGIN);
3322
- commitEditingCell();
3323
- changeSelection([head, head]);
3324
- }
3325
- };
3326
-
3327
- var onGridKeyDown = function onGridKeyDown(e) {
3328
- if (editMode && arrowKeyCommitMode && e.key in ARROW_KEYS) {
3329
- commitEditingCell();
3330
- return;
3331
- }
3332
-
3333
- if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'v') {
3334
- return;
3335
- }
3336
-
3337
- if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'c') {
3338
- var textArea = textAreaRef.current;
3339
- textArea === null || textArea === void 0 ? void 0 : textArea.select();
3340
- return;
3341
- }
3342
-
3343
- if (e.key === 'Backspace' || e.key === 'Delete') {
3344
- var _normalizeSelection2 = normalizeSelection(selection),
3345
- _normalizeSelection2$ = _normalizeSelection2[0],
3346
- x1 = _normalizeSelection2$[0],
3347
- y1 = _normalizeSelection2$[1],
3348
- _normalizeSelection2$2 = _normalizeSelection2[1],
3349
- x2 = _normalizeSelection2$2[0],
3350
- y2 = _normalizeSelection2$2[1];
3351
-
3352
- if (isRowSelection(selection)) {
3353
- x1 = 0;
3354
- x2 = MAX_SEARCHABLE_INDEX;
3355
- }
3356
-
3357
- if (isColumnSelection(selection)) {
3358
- y1 = 0;
3359
- y2 = MAX_SEARCHABLE_INDEX;
3360
- }
3361
-
3362
- var changes = [];
3363
-
3364
- for (var y = y1; y <= y2; y++) {
3365
- for (var x = x1; x <= x2; x++) {
3366
- if (!cellReadOnly(x, y)) {
3367
- changes.push({
3368
- x: x,
3369
- y: y,
3370
- value: null
3371
- });
3372
- }
3373
- }
3374
- }
3375
-
3376
- if (props.onChange) {
3377
- props.onChange(changes);
3378
- }
3379
-
3380
- return;
3381
- }
3382
-
3383
- if (isEmptySelection(selection)) {
3384
- return;
3385
- }
3386
-
3387
- if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90 || e.key === 'Enter' || e.key === '-' || e.key === '.' || e.key === ',') {
3388
- var cell = selection[0];
3389
- var cellX = cell[0],
3390
- cellY = cell[1];
3391
-
3392
- if (cellReadOnly(cellX, cellY)) {
3393
- e.preventDefault();
3394
- return;
3395
- }
3396
-
3397
- startEditingCell(cell, e.key !== 'Enter');
3398
- return;
3399
- }
3400
-
3401
- if (e.key in ARROW_KEYS) {
3402
- var anchor = selection[0],
3403
- head = selection[1];
3404
- var direction = ARROW_KEYS[e.key];
3405
- var step = getDirectionStep(direction);
3406
-
3407
- if (e.metaKey || e.ctrlKey) {
3408
- head = findInDisplayData(displayData, head, direction);
3409
- } else {
3410
- head = maxXY(addXY(head, step), ORIGIN);
3411
- }
3412
-
3413
- if (!e.shiftKey) {
3414
- anchor = head;
3415
- }
3764
+ }, [cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, isFocused, knobPosition, knobArea, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, columnGroupKeys, rowGroupKeys, selectedColumnGroups, selectedRowGroups, dataOffset]);
3416
3765
 
3417
- changeSelection([anchor, head], true, true);
3418
- return;
3419
- }
3420
-
3421
- e.preventDefault();
3422
- };
3423
-
3424
- var _useState12 = useState(''),
3425
- lastEditKey = _useState12[0],
3426
- setLastEditKey = _useState12[1];
3766
+ var _useState13 = useState(''),
3767
+ lastEditKey = _useState13[0],
3768
+ setLastEditKey = _useState13[1];
3427
3769
 
3428
3770
  var editTextPosition = ORIGIN;
3429
3771
  var editTextWidth = 0;
@@ -3451,7 +3793,7 @@ var Sheet = forwardRef(function (props, ref) {
3451
3793
  var inputProps = {
3452
3794
  value: editValue,
3453
3795
  autoFocus: true,
3454
- onKeyDown: onKeyDown,
3796
+ onKeyDown: onInputKeyDown,
3455
3797
  style: {
3456
3798
  position: 'absolute',
3457
3799
  left: textX,
@@ -3478,6 +3820,7 @@ var Sheet = forwardRef(function (props, ref) {
3478
3820
  top: 0,
3479
3821
  left: 0,
3480
3822
  overflow: 'scroll',
3823
+ outline: 0,
3481
3824
  borderBottom: '1px solid #ddd'
3482
3825
  };
3483
3826
  var canvasStyles = {
@@ -3513,10 +3856,10 @@ var Sheet = forwardRef(function (props, ref) {
3513
3856
  });
3514
3857
  }, [props.renderOutside, visibleCells, cellLayout, selection, editMode]);
3515
3858
  useImperativeHandle(ref, function () {
3516
- return _extends({}, cellLayout, {
3859
+ return _extends({}, cellLayout, clipboardApi, {
3517
3860
  startEditingCell: startEditingCell
3518
3861
  });
3519
- }, [cellLayout, startEditingCell]);
3862
+ }, [cellLayout, clipboardApi, startEditingCell]);
3520
3863
  return React.createElement("div", {
3521
3864
  style: {
3522
3865
  position: 'relative',
@@ -3529,6 +3872,10 @@ var Sheet = forwardRef(function (props, ref) {
3529
3872
  }), React.createElement("div", Object.assign({
3530
3873
  ref: overlayRef
3531
3874
  }, mouseHandlers, {
3875
+ onKeyDown: onGridKeyDown,
3876
+ onFocus: onGridFocus,
3877
+ onBlur: onGridBlur,
3878
+ tabIndex: 0,
3532
3879
  onScroll: onScroll,
3533
3880
  className: overlayDivClassName,
3534
3881
  style: overlayDivStyles
@@ -3565,26 +3912,7 @@ var Sheet = forwardRef(function (props, ref) {
3565
3912
  height: '100%',
3566
3913
  pointerEvents: 'none'
3567
3914
  }
3568
- }, renderedOutside) : null, React.createElement("textarea", {
3569
- style: {
3570
- position: 'absolute',
3571
- top: 0,
3572
- left: 0,
3573
- width: 1,
3574
- height: 1,
3575
- opacity: 0.01
3576
- },
3577
- ref: textAreaRef,
3578
- autoComplete: "off",
3579
- autoCorrect: "off",
3580
- autoCapitalize: "off",
3581
- spellCheck: "false",
3582
- onFocus: function onFocus(e) {
3583
- return e.target.select();
3584
- },
3585
- tabIndex: 0,
3586
- onKeyDown: onGridKeyDown
3587
- }), editMode && (input !== undefined ? input : React.createElement("input", Object.assign({}, inputProps, {
3915
+ }, renderedOutside) : null, editMode && (input !== undefined ? input : React.createElement("input", Object.assign({}, inputProps, {
3588
3916
  type: "text",
3589
3917
  onFocus: function onFocus(e) {
3590
3918
  return e.target.select();
@@ -3596,4 +3924,5 @@ var Sheet = forwardRef(function (props, ref) {
3596
3924
  });
3597
3925
 
3598
3926
  export default Sheet;
3927
+ export { useClipboardTable };
3599
3928
  //# sourceMappingURL=index.modern.js.map