@particle-academy/fancy-sheets 0.4.0 → 0.4.1

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.cjs CHANGED
@@ -1601,9 +1601,10 @@ function ColumnResizeHandle({ colIndex }) {
1601
1601
  }
1602
1602
  ColumnResizeHandle.displayName = "ColumnResizeHandle";
1603
1603
  function ColumnHeaders() {
1604
- const { columnCount, rowCount, rowHeight, getColumnWidth, selection, selectRange } = useSpreadsheet();
1605
- const handleColumnClick = react.useCallback(
1604
+ const { columnCount, rowCount, rowHeight, getColumnWidth, selection, selectRange, _isDragging } = useSpreadsheet();
1605
+ const handleColumnMouseDown = react.useCallback(
1606
1606
  (colIdx, e) => {
1607
+ if (e.button !== 0) return;
1607
1608
  if (e.shiftKey) {
1608
1609
  const activeCol = parseAddress(selection.activeCell).col;
1609
1610
  const minCol = Math.min(activeCol, colIdx);
@@ -1612,9 +1613,24 @@ function ColumnHeaders() {
1612
1613
  } else {
1613
1614
  selectRange(toAddress(0, colIdx), toAddress(rowCount - 1, colIdx));
1614
1615
  }
1616
+ _isDragging.current = true;
1615
1617
  },
1616
- [rowCount, selectRange, selection.activeCell]
1618
+ [rowCount, selectRange, selection.activeCell, _isDragging]
1617
1619
  );
1620
+ const handleColumnMouseEnter = react.useCallback(
1621
+ (colIdx) => {
1622
+ if (_isDragging.current) {
1623
+ const activeCol = parseAddress(selection.activeCell).col;
1624
+ const minCol = Math.min(activeCol, colIdx);
1625
+ const maxCol = Math.max(activeCol, colIdx);
1626
+ selectRange(toAddress(0, minCol), toAddress(rowCount - 1, maxCol));
1627
+ }
1628
+ },
1629
+ [rowCount, selection.activeCell, selectRange, _isDragging]
1630
+ );
1631
+ const handleMouseUp = react.useCallback(() => {
1632
+ _isDragging.current = false;
1633
+ }, [_isDragging]);
1618
1634
  return /* @__PURE__ */ jsxRuntime.jsxs(
1619
1635
  "div",
1620
1636
  {
@@ -1634,7 +1650,9 @@ function ColumnHeaders() {
1634
1650
  {
1635
1651
  className: "relative flex shrink-0 cursor-pointer items-center justify-center border-r border-zinc-300 text-[11px] font-medium text-zinc-500 select-none hover:bg-zinc-200 dark:border-zinc-600 dark:text-zinc-400 dark:hover:bg-zinc-700",
1636
1652
  style: { width: getColumnWidth(i), minWidth: getColumnWidth(i) },
1637
- onClick: (e) => handleColumnClick(i, e),
1653
+ onMouseDown: (e) => handleColumnMouseDown(i, e),
1654
+ onMouseEnter: () => handleColumnMouseEnter(i),
1655
+ onMouseUp: handleMouseUp,
1638
1656
  children: [
1639
1657
  columnToLetter(i),
1640
1658
  /* @__PURE__ */ jsxRuntime.jsx(ColumnResizeHandle, { colIndex: i })
@@ -1648,9 +1666,10 @@ function ColumnHeaders() {
1648
1666
  }
1649
1667
  ColumnHeaders.displayName = "ColumnHeaders";
1650
1668
  function RowHeader({ rowIndex }) {
1651
- const { rowHeight, columnCount, selection, selectRange, extendSelection } = useSpreadsheet();
1652
- const handleClick = react.useCallback(
1669
+ const { rowHeight, columnCount, selection, selectRange, _isDragging } = useSpreadsheet();
1670
+ const handleMouseDown = react.useCallback(
1653
1671
  (e) => {
1672
+ if (e.button !== 0) return;
1654
1673
  if (e.shiftKey) {
1655
1674
  const activeRow = parseAddress(selection.activeCell).row;
1656
1675
  const minRow = Math.min(activeRow, rowIndex);
@@ -1659,16 +1678,30 @@ function RowHeader({ rowIndex }) {
1659
1678
  } else {
1660
1679
  selectRange(toAddress(rowIndex, 0), toAddress(rowIndex, columnCount - 1));
1661
1680
  }
1681
+ _isDragging.current = true;
1662
1682
  },
1663
- [rowIndex, columnCount, selectRange, selection.activeCell]
1683
+ [rowIndex, columnCount, selectRange, selection.activeCell, _isDragging]
1664
1684
  );
1685
+ const handleMouseEnter = react.useCallback(() => {
1686
+ if (_isDragging.current) {
1687
+ const activeRow = parseAddress(selection.activeCell).row;
1688
+ const minRow = Math.min(activeRow, rowIndex);
1689
+ const maxRow = Math.max(activeRow, rowIndex);
1690
+ selectRange(toAddress(minRow, 0), toAddress(maxRow, columnCount - 1));
1691
+ }
1692
+ }, [rowIndex, columnCount, selection.activeCell, selectRange, _isDragging]);
1693
+ const handleMouseUp = react.useCallback(() => {
1694
+ _isDragging.current = false;
1695
+ }, [_isDragging]);
1665
1696
  return /* @__PURE__ */ jsxRuntime.jsx(
1666
1697
  "div",
1667
1698
  {
1668
1699
  "data-fancy-sheets-row-header": "",
1669
1700
  className: "flex shrink-0 cursor-pointer items-center justify-center border-r border-b border-zinc-300 bg-zinc-100 text-[11px] font-medium text-zinc-500 select-none hover:bg-zinc-200 dark:border-zinc-600 dark:bg-zinc-800 dark:text-zinc-400 dark:hover:bg-zinc-700",
1670
1701
  style: { width: 48, minWidth: 48, height: rowHeight },
1671
- onClick: handleClick,
1702
+ onMouseDown: handleMouseDown,
1703
+ onMouseEnter: handleMouseEnter,
1704
+ onMouseUp: handleMouseUp,
1672
1705
  children: rowIndex + 1
1673
1706
  }
1674
1707
  );
@@ -1731,7 +1764,8 @@ var Cell = react.memo(function Cell2({ address, row, col }) {
1731
1764
  rowHeight,
1732
1765
  getColumnWidth,
1733
1766
  isCellSelected,
1734
- isCellActive
1767
+ isCellActive,
1768
+ _isDragging
1735
1769
  } = useSpreadsheet();
1736
1770
  const cell = activeSheet.cells[address];
1737
1771
  const isActive = isCellActive(address);
@@ -1741,6 +1775,7 @@ var Cell = react.memo(function Cell2({ address, row, col }) {
1741
1775
  const width = getColumnWidth(col);
1742
1776
  const handleMouseDown = react.useCallback(
1743
1777
  (e) => {
1778
+ if (e.button !== 0) return;
1744
1779
  if (e.shiftKey) {
1745
1780
  extendSelection(address);
1746
1781
  } else if (e.ctrlKey || e.metaKey) {
@@ -1748,9 +1783,18 @@ var Cell = react.memo(function Cell2({ address, row, col }) {
1748
1783
  } else {
1749
1784
  setSelection(address);
1750
1785
  }
1786
+ _isDragging.current = true;
1751
1787
  },
1752
- [address, setSelection, extendSelection, addSelection]
1788
+ [address, setSelection, extendSelection, addSelection, _isDragging]
1753
1789
  );
1790
+ const handleMouseEnter = react.useCallback(() => {
1791
+ if (_isDragging.current) {
1792
+ extendSelection(address);
1793
+ }
1794
+ }, [address, extendSelection, _isDragging]);
1795
+ const handleMouseUp = react.useCallback(() => {
1796
+ _isDragging.current = false;
1797
+ }, [_isDragging]);
1754
1798
  const handleDoubleClick = react.useCallback(() => {
1755
1799
  if (readOnly) return;
1756
1800
  startEdit();
@@ -1767,12 +1811,14 @@ var Cell = react.memo(function Cell2({ address, row, col }) {
1767
1811
  "data-active": isActive || void 0,
1768
1812
  role: "gridcell",
1769
1813
  className: reactFancy.cn(
1770
- "relative flex items-center truncate border-r border-b border-zinc-200 bg-white px-1.5 text-[13px] dark:border-zinc-700 dark:bg-zinc-900",
1814
+ "relative flex items-center truncate border-r border-b border-zinc-200 bg-white px-1.5 text-[13px] select-none dark:border-zinc-700 dark:bg-zinc-900",
1771
1815
  isActive && "ring-2 ring-inset ring-blue-500",
1772
1816
  isSelected && !isActive && "bg-blue-50 dark:bg-blue-950/40"
1773
1817
  ),
1774
1818
  style: { width, minWidth: width, height: rowHeight, ...formatStyle },
1775
1819
  onMouseDown: handleMouseDown,
1820
+ onMouseEnter: handleMouseEnter,
1821
+ onMouseUp: handleMouseUp,
1776
1822
  onDoubleClick: handleDoubleClick,
1777
1823
  children: !isEditing && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate", children: displayValue })
1778
1824
  }
@@ -1914,6 +1960,7 @@ function SpreadsheetGrid({ className }) {
1914
1960
  setCellValue,
1915
1961
  setFrozenRows,
1916
1962
  setFrozenCols,
1963
+ extendSelection,
1917
1964
  undo,
1918
1965
  redo
1919
1966
  } = useSpreadsheet();
@@ -2411,6 +2458,7 @@ function SpreadsheetRoot({
2411
2458
  (address) => state.selection.activeCell === address,
2412
2459
  [state.selection.activeCell]
2413
2460
  );
2461
+ const isDraggingRef = react.useRef(false);
2414
2462
  const ctx = react.useMemo(
2415
2463
  () => ({
2416
2464
  workbook: state.workbook,
@@ -2428,7 +2476,8 @@ function SpreadsheetRoot({
2428
2476
  canRedo: state.redoStack.length > 0,
2429
2477
  getColumnWidth,
2430
2478
  isCellSelected,
2431
- isCellActive
2479
+ isCellActive,
2480
+ _isDragging: isDraggingRef
2432
2481
  }),
2433
2482
  [state, activeSheet, columnCount, rowCount, defaultColumnWidth, rowHeight, readOnly, actions, getColumnWidth, isCellSelected, isCellActive]
2434
2483
  );