@gridkitjs/core 0.0.0-dev-20261107203826 → 0.0.0-dev-20261108230616

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;
@@ -217,8 +225,10 @@ interface GridController<Row, Node = string> {
217
225
  /** Collapses every group currently shown — see `collapseAllGroups`. */
218
226
  readonly collapseAllGroups: () => void;
219
227
  /**
220
- * Replaces the page and page size as given; the view clamps a page past
221
- * the last one. An equal pagination reports nothing.
228
+ * Replaces the page and page size. A controlled pagination asks for them
229
+ * as given and the view clamps a page past the last one; an uncontrolled
230
+ * one stores and reports that page clamped. An equal pagination reports
231
+ * nothing.
222
232
  */
223
233
  readonly setPagination: (pagination: PaginationState) => void;
224
234
  /**
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,17 +1871,36 @@ function createGridController(initial) {
1850
1871
  );
1851
1872
  }
1852
1873
  }
1853
- function refresh() {
1854
- let next = resolveSlices();
1874
+ function refresh(keepPage = false) {
1875
+ const next = followPagingInputs(resolveSlices(), keepPage);
1876
+ state = SLICE_KEYS.every((key) => next[key] === state[key]) ? state : next;
1877
+ storeShownPage();
1878
+ }
1879
+ function followPagingInputs(next, keepPage) {
1855
1880
  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
- }
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
+ }
1891
+ function storeShownPage() {
1892
+ const stored = storedPagination(state.pagination);
1893
+ if (stored !== state.pagination) {
1894
+ own.pagination = stored;
1895
+ state = { ...state, pagination: stored };
1862
1896
  }
1863
- state = SLICE_KEYS.every((key) => next[key] === state[key]) ? state : next;
1897
+ }
1898
+ function storedPagination(pagination) {
1899
+ if (options.paginated !== true || isControlled("pagination")) {
1900
+ return pagination;
1901
+ }
1902
+ const { pageCount, pageIndex } = pageOf(pagination);
1903
+ return pageCount === 0 || pageIndex === pagination.pageIndex ? pagination : { ...pagination, pageIndex };
1864
1904
  }
1865
1905
  function notify() {
1866
1906
  for (const listener of [...listeners]) {
@@ -1874,14 +1914,38 @@ function createGridController(initial) {
1874
1914
  if (!isControlled(key)) {
1875
1915
  own[key] = next;
1876
1916
  }
1877
- if (heldReports !== void 0) {
1878
- heldReports.push(report);
1917
+ if (mode.kind === "transaction") {
1918
+ mode.held.push(report);
1879
1919
  return;
1880
1920
  }
1881
1921
  refresh();
1882
1922
  report();
1883
1923
  notify();
1884
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
+ }
1885
1949
  function getView() {
1886
1950
  return resolveView({
1887
1951
  rows: options.dataSource ?? NO_ROWS,
@@ -1951,14 +2015,21 @@ function createGridController(initial) {
1951
2015
  options.onGroupExpansionChange?.({ groupId, expansion: next });
1952
2016
  });
1953
2017
  }
1954
- function commitPagination(next) {
2018
+ function commitPagination(asked) {
2019
+ let next = asked;
2020
+ if (mode.kind === "transaction") {
2021
+ mode.pageAsked = true;
2022
+ } else {
2023
+ next = storedPagination(asked);
2024
+ }
1955
2025
  if (samePagination(state.pagination, next)) {
1956
2026
  return;
1957
2027
  }
1958
2028
  commit("pagination", next, () => {
2029
+ const pagination = isControlled("pagination") ? next : own.pagination;
1959
2030
  options.onPaginationChange?.({
1960
- pagination: next,
1961
- pageCount: pageOf(next).pageCount
2031
+ pagination,
2032
+ pageCount: pageOf(pagination).pageCount
1962
2033
  });
1963
2034
  });
1964
2035
  }
@@ -1979,7 +2050,8 @@ function createGridController(initial) {
1979
2050
  getState: () => state,
1980
2051
  getOptions: () => options,
1981
2052
  getView,
1982
- commit
2053
+ commit,
2054
+ transaction
1983
2055
  });
1984
2056
  const controller = {
1985
2057
  ...selection,
@@ -1992,23 +2064,9 @@ function createGridController(initial) {
1992
2064
  getState: () => state,
1993
2065
  getView,
1994
2066
  restoreState(saved) {
1995
- const reports = [];
1996
- heldReports = reports;
1997
- try {
2067
+ transaction(() => {
1998
2068
  restoreSlices(saved);
1999
- } finally {
2000
- heldReports = void 0;
2001
- }
2002
- if (saved.pagination !== void 0) {
2003
- pagingInputs = pagingInputsOf(resolveSlices(), options);
2004
- }
2005
- refresh();
2006
- for (const report of reports) {
2007
- report();
2008
- }
2009
- if (reports.length > 0) {
2010
- notify();
2011
- }
2069
+ });
2012
2070
  },
2013
2071
  subscribe(listener) {
2014
2072
  listeners.add(listener);