@gridkitjs/core 0.0.0-dev-20261107212842 → 0.0.0-dev-20261108231045

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -88,6 +88,8 @@ interface GridController<Row, Node = string> {
88
88
  * Subscribers are called once, after every slice is in place, and a
89
89
  * restored pagination is kept even when the same call changed the sort,
90
90
  * filter or grouping that would otherwise send it back to the first page.
91
+ * A slice that throws stops the restore there, after the slices before it
92
+ * are shown, reported and announced as above.
91
93
  */
92
94
  readonly restoreState: (saved: Partial<GridState<Row>> | Partial<SerializedGridState<Row>>) => void;
93
95
  /**
@@ -155,12 +157,18 @@ interface GridController<Row, Node = string> {
155
157
  /**
156
158
  * Applies one click or key press to a row: `"replace"`, `"toggle"` or
157
159
  * `"range"` from the last row selected by anything but a range, across
158
- * the data rows on the current page.
160
+ * the data rows on the current page. That anchor goes when the selection is
161
+ * cleared or replaced, or its row leaves the page; a range without one
162
+ * selects just this row and anchors there.
159
163
  */
160
164
  readonly selectRow: (rowId: string, intent: SelectIntent) => void;
161
165
  /** Selects every data row on the current page, when `selectable.rows` is `"multiple"`. */
162
166
  readonly selectAllRows: () => void;
163
- /** Lets go of every row, column and cell selection at once, as Escape does. */
167
+ /**
168
+ * Lets go of every row, column and cell selection at once, as Escape does:
169
+ * one change, so each selection that changes fires its callback and then
170
+ * subscribers are called once.
171
+ */
164
172
  readonly clearSelection: () => void;
165
173
  /** Replaces the selected column ids as given — see `setRowSelection`. */
166
174
  readonly setColumnSelection: (selection: SelectionState) => void;
package/dist/index.js CHANGED
@@ -1596,6 +1596,21 @@ function indexRows(rows) {
1596
1596
  function distinct(ids) {
1597
1597
  return [...new Set(ids)];
1598
1598
  }
1599
+ function click(selection, orderedIds, anchor, id, intent, mode) {
1600
+ const live = anchor !== null && orderedIds.includes(anchor) ? anchor : null;
1601
+ const keepsAnchor = mode === false || intent === "range" && live !== null;
1602
+ return {
1603
+ selection: applySelectionIntent(
1604
+ selection,
1605
+ orderedIds,
1606
+ live,
1607
+ id,
1608
+ intent,
1609
+ mode
1610
+ ),
1611
+ anchor: keepsAnchor ? anchor : id
1612
+ };
1613
+ }
1599
1614
  function createSelectionActions(grid) {
1600
1615
  let rowAnchor = null;
1601
1616
  let columnAnchor = null;
@@ -1659,6 +1674,7 @@ function createSelectionActions(grid) {
1659
1674
  }
1660
1675
  return {
1661
1676
  setRowSelection(selection) {
1677
+ rowAnchor = null;
1662
1678
  if (!sameItems(grid.getState().rowSelection, selection)) {
1663
1679
  commitRows(selection);
1664
1680
  }
@@ -1679,7 +1695,7 @@ function createSelectionActions(grid) {
1679
1695
  }
1680
1696
  },
1681
1697
  selectRow(rowId, intent) {
1682
- const next = applySelectionIntent(
1698
+ const next = click(
1683
1699
  grid.getState().rowSelection,
1684
1700
  rowIds(),
1685
1701
  rowAnchor,
@@ -1687,10 +1703,8 @@ function createSelectionActions(grid) {
1687
1703
  intent,
1688
1704
  modes().rows
1689
1705
  );
1690
- if (intent !== "range") {
1691
- rowAnchor = rowId;
1692
- }
1693
- commitRows(next);
1706
+ rowAnchor = next.anchor;
1707
+ commitRows(next.selection);
1694
1708
  },
1695
1709
  selectAllRows() {
1696
1710
  commitRows(
@@ -1698,17 +1712,22 @@ function createSelectionActions(grid) {
1698
1712
  );
1699
1713
  },
1700
1714
  clearSelection() {
1701
- commitRows(clearSelection(grid.getState().rowSelection));
1702
- commitColumns(clearSelection(grid.getState().columnSelection));
1703
- commitCell(null);
1715
+ rowAnchor = null;
1716
+ columnAnchor = null;
1717
+ grid.transaction(() => {
1718
+ commitRows(clearSelection(grid.getState().rowSelection));
1719
+ commitColumns(clearSelection(grid.getState().columnSelection));
1720
+ commitCell(null);
1721
+ });
1704
1722
  },
1705
1723
  setColumnSelection(selection) {
1724
+ columnAnchor = null;
1706
1725
  if (!sameItems(grid.getState().columnSelection, selection)) {
1707
1726
  commitColumns(selection);
1708
1727
  }
1709
1728
  },
1710
1729
  selectColumn(columnId, intent) {
1711
- const next = applySelectionIntent(
1730
+ const next = click(
1712
1731
  grid.getState().columnSelection,
1713
1732
  columnIds(),
1714
1733
  columnAnchor,
@@ -1716,10 +1735,8 @@ function createSelectionActions(grid) {
1716
1735
  intent,
1717
1736
  modes().columns
1718
1737
  );
1719
- if (intent !== "range") {
1720
- columnAnchor = columnId;
1721
- }
1722
- commitColumns(next);
1738
+ columnAnchor = next.anchor;
1739
+ commitColumns(next.selection);
1723
1740
  },
1724
1741
  setCellSelection(selection) {
1725
1742
  if (!isSameCell(grid.getState().cellSelection, selection)) {
@@ -1793,6 +1810,10 @@ function definedColumns(columns, rows) {
1793
1810
  }
1794
1811
  return NO_COLUMNS;
1795
1812
  }
1813
+ var IDLE = { kind: "idle" };
1814
+ function samePagingInputs(a, b) {
1815
+ return a.filter === b.filter && a.columnSort === b.columnSort && a.groupBy === b.groupBy && a.paginationUnit === b.paginationUnit;
1816
+ }
1796
1817
  function pagingInputsOf(state, options) {
1797
1818
  return {
1798
1819
  filter: state.filter,
@@ -1808,7 +1829,7 @@ function createGridController(initial) {
1808
1829
  const resolveColumns2 = memoizeLast(definedColumns);
1809
1830
  const listeners = /* @__PURE__ */ new Set();
1810
1831
  const reportedConflicts = /* @__PURE__ */ new Set();
1811
- let heldReports;
1832
+ let mode = IDLE;
1812
1833
  let state = resolveSlices();
1813
1834
  let pagingInputs = pagingInputsOf(state, options);
1814
1835
  reportConflicts();
@@ -1850,19 +1871,23 @@ function createGridController(initial) {
1850
1871
  );
1851
1872
  }
1852
1873
  }
1853
- function refresh() {
1854
- let next = resolveSlices();
1855
- const inputs = pagingInputsOf(next, options);
1856
- if (inputs.filter !== pagingInputs.filter || inputs.columnSort !== pagingInputs.columnSort || inputs.groupBy !== pagingInputs.groupBy || inputs.paginationUnit !== pagingInputs.paginationUnit) {
1857
- pagingInputs = inputs;
1858
- if (!isControlled("pagination") && own.pagination.pageIndex !== 0) {
1859
- own.pagination = { ...own.pagination, pageIndex: 0 };
1860
- next = { ...next, pagination: own.pagination };
1861
- }
1862
- }
1874
+ function refresh(keepPage = false) {
1875
+ const next = followPagingInputs(resolveSlices(), keepPage);
1863
1876
  state = SLICE_KEYS.every((key) => next[key] === state[key]) ? state : next;
1864
1877
  storeShownPage();
1865
1878
  }
1879
+ function followPagingInputs(next, keepPage) {
1880
+ const inputs = pagingInputsOf(next, options);
1881
+ if (samePagingInputs(inputs, pagingInputs)) {
1882
+ return next;
1883
+ }
1884
+ pagingInputs = inputs;
1885
+ if (keepPage || isControlled("pagination") || own.pagination.pageIndex === 0) {
1886
+ return next;
1887
+ }
1888
+ own.pagination = { ...own.pagination, pageIndex: 0 };
1889
+ return { ...next, pagination: own.pagination };
1890
+ }
1866
1891
  function storeShownPage() {
1867
1892
  const stored = storedPagination(state.pagination);
1868
1893
  if (stored !== state.pagination) {
@@ -1889,14 +1914,38 @@ function createGridController(initial) {
1889
1914
  if (!isControlled(key)) {
1890
1915
  own[key] = next;
1891
1916
  }
1892
- if (heldReports !== void 0) {
1893
- heldReports.push(report);
1917
+ if (mode.kind === "transaction") {
1918
+ mode.held.push(report);
1894
1919
  return;
1895
1920
  }
1896
1921
  refresh();
1897
1922
  report();
1898
1923
  notify();
1899
1924
  }
1925
+ function transaction(run) {
1926
+ if (mode.kind === "transaction") {
1927
+ run();
1928
+ return;
1929
+ }
1930
+ const current = {
1931
+ kind: "transaction",
1932
+ held: [],
1933
+ pageAsked: false
1934
+ };
1935
+ mode = current;
1936
+ try {
1937
+ run();
1938
+ } finally {
1939
+ mode = IDLE;
1940
+ refresh(current.pageAsked);
1941
+ for (const report of current.held) {
1942
+ report();
1943
+ }
1944
+ if (current.held.length > 0) {
1945
+ notify();
1946
+ }
1947
+ }
1948
+ }
1900
1949
  function getView() {
1901
1950
  return resolveView({
1902
1951
  rows: options.dataSource ?? NO_ROWS,
@@ -1967,7 +2016,12 @@ function createGridController(initial) {
1967
2016
  });
1968
2017
  }
1969
2018
  function commitPagination(asked) {
1970
- const next = heldReports === void 0 ? storedPagination(asked) : asked;
2019
+ let next = asked;
2020
+ if (mode.kind === "transaction") {
2021
+ mode.pageAsked = true;
2022
+ } else {
2023
+ next = storedPagination(asked);
2024
+ }
1971
2025
  if (samePagination(state.pagination, next)) {
1972
2026
  return;
1973
2027
  }
@@ -1996,7 +2050,8 @@ function createGridController(initial) {
1996
2050
  getState: () => state,
1997
2051
  getOptions: () => options,
1998
2052
  getView,
1999
- commit
2053
+ commit,
2054
+ transaction
2000
2055
  });
2001
2056
  const controller = {
2002
2057
  ...selection,
@@ -2009,23 +2064,9 @@ function createGridController(initial) {
2009
2064
  getState: () => state,
2010
2065
  getView,
2011
2066
  restoreState(saved) {
2012
- const reports = [];
2013
- heldReports = reports;
2014
- try {
2067
+ transaction(() => {
2015
2068
  restoreSlices(saved);
2016
- } finally {
2017
- heldReports = void 0;
2018
- }
2019
- if (saved.pagination !== void 0) {
2020
- pagingInputs = pagingInputsOf(resolveSlices(), options);
2021
- }
2022
- refresh();
2023
- for (const report of reports) {
2024
- report();
2025
- }
2026
- if (reports.length > 0) {
2027
- notify();
2028
- }
2069
+ });
2029
2070
  },
2030
2071
  subscribe(listener) {
2031
2072
  listeners.add(listener);