@pretable/core 0.0.8 → 0.0.9

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.
Files changed (3) hide show
  1. package/dist/index.cjs +124 -58
  2. package/dist/index.mjs +124 -58
  3. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -608,7 +608,9 @@ function makeGroupColumn(columns, rowGroups, groupColumn) {
608
608
  // The group column shows a tree, not a value: sorting or filtering it has
609
609
  // no meaning, and both would index into a column that has no `value`.
610
610
  sortable: false,
611
- filterable: false
611
+ filterable: false,
612
+ resizable: false,
613
+ reorderable: false
612
614
  };
613
615
  return groupColumn?.pinned === "left" ? { ...column, pinned: "left" } : column;
614
616
  }
@@ -747,26 +749,31 @@ function createGridCore(inputOptions) {
747
749
  },
748
750
  setColumnFilter(columnId, filter) {
749
751
  const current = filters[columnId];
752
+ let next;
750
753
  if (filter && isFilterActive(filter)) {
751
754
  if (current && columnFilterEqual(current, filter)) {
752
755
  return;
753
756
  }
754
- filters = { ...filters, [columnId]: filter };
757
+ next = { ...filters, [columnId]: filter };
755
758
  } else {
756
759
  if (current === void 0) {
757
760
  return;
758
761
  }
759
- const next = { ...filters };
762
+ next = { ...filters };
760
763
  delete next[columnId];
761
- filters = next;
762
764
  }
765
+ const before = captureVisibleRowsForFocusReconciliation();
766
+ filters = next;
767
+ reconcileFocusAfterVisibleModelChange(before);
763
768
  emit();
764
769
  },
765
770
  clearFilters() {
766
771
  if (Object.keys(filters).length === 0) {
767
772
  return;
768
773
  }
774
+ const before = captureVisibleRowsForFocusReconciliation();
769
775
  filters = {};
776
+ reconcileFocusAfterVisibleModelChange(before);
770
777
  emit();
771
778
  },
772
779
  replaceFilters(nextFilters) {
@@ -779,7 +786,9 @@ function createGridCore(inputOptions) {
779
786
  if (filtersEqual(filters, normalized)) {
780
787
  return;
781
788
  }
789
+ const before = captureVisibleRowsForFocusReconciliation();
782
790
  filters = normalized;
791
+ reconcileFocusAfterVisibleModelChange(before);
783
792
  emit();
784
793
  },
785
794
  distinctColumnValues(columnId) {
@@ -815,8 +824,9 @@ function createGridCore(inputOptions) {
815
824
  const dataRows = getSnapshot().visibleRows.filter(isDataRow);
816
825
  const firstRow = dataRows[0];
817
826
  const lastRow = dataRows[dataRows.length - 1];
818
- const firstColumn = options.columns[0];
819
- const lastColumn = options.columns[options.columns.length - 1];
827
+ const effectiveColumns = getColumns();
828
+ const firstColumn = effectiveColumns[0];
829
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
820
830
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
821
831
  return;
822
832
  }
@@ -838,7 +848,10 @@ function createGridCore(inputOptions) {
838
848
  emit();
839
849
  },
840
850
  clearSelection() {
841
- const focusAddr = focus.rowId && focus.columnId ? { rowId: focus.rowId, columnId: focus.columnId } : null;
851
+ const focusedRowId = focus.rowId;
852
+ const focusedColumnId = focus.columnId;
853
+ const focusedRow = focusedRowId ? getSnapshot().visibleRows.find((row) => row.id === focusedRowId) : void 0;
854
+ const focusAddr = focusedRow?.kind === "group" || !focusedRowId || !focusedColumnId ? null : { rowId: focusedRowId, columnId: focusedColumnId };
842
855
  const next = focusAddr ? {
843
856
  ranges: [
844
857
  {
@@ -878,8 +891,13 @@ function createGridCore(inputOptions) {
878
891
  emit();
879
892
  },
880
893
  toggleRowSelection(rowId) {
881
- const firstColumn = options.columns[0];
882
- const lastColumn = options.columns[options.columns.length - 1];
894
+ const visibleRow = getSnapshot().visibleRows.find((row) => row.id === rowId);
895
+ if (visibleRow?.kind === "group") {
896
+ return;
897
+ }
898
+ const effectiveColumns = getColumns();
899
+ const firstColumn = effectiveColumns[0];
900
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
883
901
  if (!firstColumn || !lastColumn) {
884
902
  return;
885
903
  }
@@ -903,8 +921,9 @@ function createGridCore(inputOptions) {
903
921
  },
904
922
  setSelectAllVisible(checked) {
905
923
  const dataRows = getSnapshot().visibleRows.filter(isDataRow);
906
- const firstColumn = options.columns[0];
907
- const lastColumn = options.columns[options.columns.length - 1];
924
+ const effectiveColumns = getColumns();
925
+ const firstColumn = effectiveColumns[0];
926
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
908
927
  if (!firstColumn || !lastColumn) {
909
928
  return;
910
929
  }
@@ -1011,8 +1030,31 @@ function createGridCore(inputOptions) {
1011
1030
  columnId: nextColumn.id
1012
1031
  };
1013
1032
  focus = nextAddr;
1014
- if (moveOptions.extend) {
1015
- if (!selection.anchor) {
1033
+ if (isDataRow(nextRow)) {
1034
+ if (moveOptions.extend) {
1035
+ if (!selection.anchor) {
1036
+ selection = {
1037
+ ranges: [
1038
+ {
1039
+ startRowId: nextAddr.rowId,
1040
+ endRowId: nextAddr.rowId,
1041
+ startColumnId: nextAddr.columnId,
1042
+ endColumnId: nextAddr.columnId
1043
+ }
1044
+ ],
1045
+ anchor: nextAddr
1046
+ };
1047
+ } else {
1048
+ const newActive = {
1049
+ startRowId: selection.anchor.rowId,
1050
+ endRowId: nextAddr.rowId,
1051
+ startColumnId: selection.anchor.columnId,
1052
+ endColumnId: nextAddr.columnId
1053
+ };
1054
+ const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
1055
+ selection = { ranges, anchor: selection.anchor };
1056
+ }
1057
+ } else {
1016
1058
  selection = {
1017
1059
  ranges: [
1018
1060
  {
@@ -1024,28 +1066,7 @@ function createGridCore(inputOptions) {
1024
1066
  ],
1025
1067
  anchor: nextAddr
1026
1068
  };
1027
- } else {
1028
- const newActive = {
1029
- startRowId: selection.anchor.rowId,
1030
- endRowId: nextAddr.rowId,
1031
- startColumnId: selection.anchor.columnId,
1032
- endColumnId: nextAddr.columnId
1033
- };
1034
- const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
1035
- selection = { ranges, anchor: selection.anchor };
1036
1069
  }
1037
- } else {
1038
- selection = {
1039
- ranges: [
1040
- {
1041
- startRowId: nextAddr.rowId,
1042
- endRowId: nextAddr.rowId,
1043
- startColumnId: nextAddr.columnId,
1044
- endColumnId: nextAddr.columnId
1045
- }
1046
- ],
1047
- anchor: nextAddr
1048
- };
1049
1070
  }
1050
1071
  emit();
1051
1072
  },
@@ -1270,10 +1291,22 @@ function createGridCore(inputOptions) {
1270
1291
  return { ...newCol };
1271
1292
  });
1272
1293
  const grouped = groupColumnsByPin(merged);
1273
- const changed = !sameColumnLayout(options.columns, grouped);
1294
+ const layoutChanged = !sameColumnLayout(options.columns, grouped);
1295
+ const groupingSemanticsChanged = !sameColumnGroupingSemantics(options.columns, grouped);
1296
+ const before = groupingSemanticsChanged ? captureVisibleRowsForFocusReconciliation() : null;
1274
1297
  originalColumns = groupColumnsByPin(nextColumns).map((c) => ({ ...c }));
1275
1298
  options = { ...options, columns: grouped };
1276
- if (changed) {
1299
+ const nextRowGroups = sanitizeRowGroups(rowGroups, grouped);
1300
+ const sanitizedGroupingChanged = !stringListsEqual(rowGroups, nextRowGroups);
1301
+ if (sanitizedGroupingChanged) {
1302
+ rowGroups = nextRowGroups;
1303
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1304
+ }
1305
+ if (groupingSemanticsChanged) {
1306
+ cachedVisibleRows = null;
1307
+ reconcileFocusAfterVisibleModelChange(before);
1308
+ }
1309
+ if (layoutChanged || groupingSemanticsChanged || sanitizedGroupingChanged) {
1277
1310
  emit();
1278
1311
  }
1279
1312
  },
@@ -1282,6 +1315,7 @@ function createGridCore(inputOptions) {
1282
1315
  throw new Error("applyTransaction requires getRowId on PretableGridOptions");
1283
1316
  }
1284
1317
  const getRowId = options.getRowId;
1318
+ const before = captureVisibleRowsForFocusReconciliation();
1285
1319
  if (transaction.remove) {
1286
1320
  const removeSet = new Set(transaction.remove);
1287
1321
  sourceRows = sourceRows.filter((entry) => {
@@ -1325,9 +1359,11 @@ function createGridCore(inputOptions) {
1325
1359
  }
1326
1360
  }
1327
1361
  cachedVisibleRows = null;
1362
+ reconcileFocusAfterVisibleModelChange(before);
1328
1363
  emit();
1329
1364
  },
1330
1365
  setRows(nextRows) {
1366
+ const before = captureVisibleRowsForFocusReconciliation();
1331
1367
  options = { ...options, rows: nextRows };
1332
1368
  sourceRows = createSourceRows(options);
1333
1369
  sourceRowIndex.clear();
@@ -1343,9 +1379,6 @@ function createGridCore(inputOptions) {
1343
1379
  anchor: anchorValid ? selection.anchor : null
1344
1380
  };
1345
1381
  }
1346
- if (focus.rowId !== null && !hasRow(focus.rowId)) {
1347
- focus = { rowId: null, columnId: null };
1348
- }
1349
1382
  if (editing && !hasRow(editing.rowId)) {
1350
1383
  editing = null;
1351
1384
  }
@@ -1370,6 +1403,7 @@ function createGridCore(inputOptions) {
1370
1403
  };
1371
1404
  }
1372
1405
  cachedVisibleRows = null;
1406
+ reconcileFocusAfterVisibleModelChange(before);
1373
1407
  emit();
1374
1408
  },
1375
1409
  setRowGroups(columnIds) {
@@ -1377,8 +1411,10 @@ function createGridCore(inputOptions) {
1377
1411
  if (stringListsEqual(rowGroups, next)) {
1378
1412
  return;
1379
1413
  }
1414
+ const before = captureVisibleRowsForFocusReconciliation();
1380
1415
  rowGroups = next;
1381
1416
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1417
+ reconcileFocusAfterVisibleModelChange(before);
1382
1418
  emit();
1383
1419
  },
1384
1420
  toggleGroup(groupId) {
@@ -1388,7 +1424,7 @@ function createGridCore(inputOptions) {
1388
1424
  if (isGroupExpanded(groupId) === expanded) {
1389
1425
  return;
1390
1426
  }
1391
- const before = getSnapshot().visibleRows;
1427
+ const before = captureVisibleRowsForFocusReconciliation();
1392
1428
  if (expanded === groupsDefaultExpanded) {
1393
1429
  const next = new Set(groupExpansionOverrides);
1394
1430
  next.delete(groupId);
@@ -1396,7 +1432,9 @@ function createGridCore(inputOptions) {
1396
1432
  } else {
1397
1433
  groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1398
1434
  }
1399
- reanchorFocusAfterCollapse(before);
1435
+ reconcileFocusAfterVisibleModelChange(before, {
1436
+ preferAncestor: !expanded
1437
+ });
1400
1438
  emit();
1401
1439
  },
1402
1440
  expandAll() {
@@ -1471,32 +1509,51 @@ function createGridCore(inputOptions) {
1471
1509
  if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1472
1510
  return;
1473
1511
  }
1474
- const before = getSnapshot().visibleRows;
1512
+ const before = captureVisibleRowsForFocusReconciliation();
1475
1513
  groupsDefaultExpanded = expanded;
1476
1514
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1477
- reanchorFocusAfterCollapse(before);
1515
+ reconcileFocusAfterVisibleModelChange(before, {
1516
+ preferAncestor: !expanded
1517
+ });
1478
1518
  emit();
1479
1519
  }
1480
- function reanchorFocusAfterCollapse(before) {
1481
- if (focus.rowId === null)
1482
- return;
1483
- const fromIndex = before.findIndex((entry) => entry.id === focus.rowId);
1484
- if (fromIndex === -1)
1520
+ function captureVisibleRowsForFocusReconciliation() {
1521
+ return focus.rowId === null && focus.columnId === null ? null : getSnapshot().visibleRows;
1522
+ }
1523
+ function reconcileFocusAfterVisibleModelChange(before, options2 = {}) {
1524
+ if (before === null || focus.rowId === null && focus.columnId === null) {
1485
1525
  return;
1526
+ }
1527
+ const oldRowId = focus.rowId;
1528
+ const oldIndex = oldRowId === null ? -1 : before.findIndex((entry) => entry.id === oldRowId);
1486
1529
  cachedSnapshot = null;
1487
- const after = getSnapshot().visibleRows;
1488
- if (after.some((entry) => entry.id === focus.rowId))
1530
+ const afterRows = getSnapshot().visibleRows;
1531
+ const afterColumns = getColumns();
1532
+ if (afterRows.length === 0 || afterColumns.length === 0) {
1533
+ focus = { rowId: null, columnId: null };
1534
+ cachedSnapshot = null;
1489
1535
  return;
1490
- const surviving = new Set(after.map((entry) => entry.id));
1491
- let nextRowId = null;
1492
- for (let i = fromIndex - 1; i >= 0; i -= 1) {
1493
- const candidate = before[i];
1494
- if (candidate.kind === "group" && surviving.has(candidate.id)) {
1495
- nextRowId = candidate.id;
1496
- break;
1536
+ }
1537
+ const survivingRowIds = new Set(afterRows.map((entry) => entry.id));
1538
+ let nextRowId = oldRowId !== null && survivingRowIds.has(oldRowId) ? oldRowId : null;
1539
+ if (nextRowId === null && options2.preferAncestor === true && oldIndex !== -1) {
1540
+ const oldDepth = before[oldIndex].depth;
1541
+ for (let i = oldIndex - 1; i >= 0; i -= 1) {
1542
+ const candidate = before[i];
1543
+ if (candidate.kind === "group" && candidate.depth < oldDepth && survivingRowIds.has(candidate.id)) {
1544
+ nextRowId = candidate.id;
1545
+ break;
1546
+ }
1497
1547
  }
1498
1548
  }
1499
- focus = nextRowId === null ? { rowId: null, columnId: null } : { rowId: nextRowId, columnId: focus.columnId };
1549
+ if (nextRowId === null) {
1550
+ const nextIndex = oldIndex === -1 ? 0 : clamp(oldIndex, 0, afterRows.length - 1);
1551
+ nextRowId = afterRows[nextIndex].id;
1552
+ }
1553
+ const focusedColumnSurvives = focus.columnId !== null && afterColumns.some((column) => column.id === focus.columnId);
1554
+ const groupColumn = rowGroups.length > 0 ? afterColumns.find((column) => column.id === GROUP_COLUMN_ID) : void 0;
1555
+ const nextColumnId = focusedColumnSurvives ? focus.columnId : groupColumn?.id ?? afterColumns[0].id;
1556
+ focus = { rowId: nextRowId, columnId: nextColumnId };
1500
1557
  cachedSnapshot = null;
1501
1558
  }
1502
1559
  function preserveAggregateIdentity(rows) {
@@ -1599,6 +1656,15 @@ function sameColumnLayout(a, b) {
1599
1656
  return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
1600
1657
  });
1601
1658
  }
1659
+ function sameColumnGroupingSemantics(a, b) {
1660
+ if (a.length !== b.length) {
1661
+ return false;
1662
+ }
1663
+ return a.every((left, index) => {
1664
+ const right = b[index];
1665
+ return left.id === right.id && left.value === right.value && left.aggregate === right.aggregate;
1666
+ });
1667
+ }
1602
1668
  function computePageStep(viewport, rowCount) {
1603
1669
  if (viewport.height <= 0 || rowCount === 0) {
1604
1670
  return 1;
package/dist/index.mjs CHANGED
@@ -606,7 +606,9 @@ function makeGroupColumn(columns, rowGroups, groupColumn) {
606
606
  // The group column shows a tree, not a value: sorting or filtering it has
607
607
  // no meaning, and both would index into a column that has no `value`.
608
608
  sortable: false,
609
- filterable: false
609
+ filterable: false,
610
+ resizable: false,
611
+ reorderable: false
610
612
  };
611
613
  return groupColumn?.pinned === "left" ? { ...column, pinned: "left" } : column;
612
614
  }
@@ -745,26 +747,31 @@ function createGridCore(inputOptions) {
745
747
  },
746
748
  setColumnFilter(columnId, filter) {
747
749
  const current = filters[columnId];
750
+ let next;
748
751
  if (filter && isFilterActive(filter)) {
749
752
  if (current && columnFilterEqual(current, filter)) {
750
753
  return;
751
754
  }
752
- filters = { ...filters, [columnId]: filter };
755
+ next = { ...filters, [columnId]: filter };
753
756
  } else {
754
757
  if (current === void 0) {
755
758
  return;
756
759
  }
757
- const next = { ...filters };
760
+ next = { ...filters };
758
761
  delete next[columnId];
759
- filters = next;
760
762
  }
763
+ const before = captureVisibleRowsForFocusReconciliation();
764
+ filters = next;
765
+ reconcileFocusAfterVisibleModelChange(before);
761
766
  emit();
762
767
  },
763
768
  clearFilters() {
764
769
  if (Object.keys(filters).length === 0) {
765
770
  return;
766
771
  }
772
+ const before = captureVisibleRowsForFocusReconciliation();
767
773
  filters = {};
774
+ reconcileFocusAfterVisibleModelChange(before);
768
775
  emit();
769
776
  },
770
777
  replaceFilters(nextFilters) {
@@ -777,7 +784,9 @@ function createGridCore(inputOptions) {
777
784
  if (filtersEqual(filters, normalized)) {
778
785
  return;
779
786
  }
787
+ const before = captureVisibleRowsForFocusReconciliation();
780
788
  filters = normalized;
789
+ reconcileFocusAfterVisibleModelChange(before);
781
790
  emit();
782
791
  },
783
792
  distinctColumnValues(columnId) {
@@ -813,8 +822,9 @@ function createGridCore(inputOptions) {
813
822
  const dataRows = getSnapshot().visibleRows.filter(isDataRow);
814
823
  const firstRow = dataRows[0];
815
824
  const lastRow = dataRows[dataRows.length - 1];
816
- const firstColumn = options.columns[0];
817
- const lastColumn = options.columns[options.columns.length - 1];
825
+ const effectiveColumns = getColumns();
826
+ const firstColumn = effectiveColumns[0];
827
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
818
828
  if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
819
829
  return;
820
830
  }
@@ -836,7 +846,10 @@ function createGridCore(inputOptions) {
836
846
  emit();
837
847
  },
838
848
  clearSelection() {
839
- const focusAddr = focus.rowId && focus.columnId ? { rowId: focus.rowId, columnId: focus.columnId } : null;
849
+ const focusedRowId = focus.rowId;
850
+ const focusedColumnId = focus.columnId;
851
+ const focusedRow = focusedRowId ? getSnapshot().visibleRows.find((row) => row.id === focusedRowId) : void 0;
852
+ const focusAddr = focusedRow?.kind === "group" || !focusedRowId || !focusedColumnId ? null : { rowId: focusedRowId, columnId: focusedColumnId };
840
853
  const next = focusAddr ? {
841
854
  ranges: [
842
855
  {
@@ -876,8 +889,13 @@ function createGridCore(inputOptions) {
876
889
  emit();
877
890
  },
878
891
  toggleRowSelection(rowId) {
879
- const firstColumn = options.columns[0];
880
- const lastColumn = options.columns[options.columns.length - 1];
892
+ const visibleRow = getSnapshot().visibleRows.find((row) => row.id === rowId);
893
+ if (visibleRow?.kind === "group") {
894
+ return;
895
+ }
896
+ const effectiveColumns = getColumns();
897
+ const firstColumn = effectiveColumns[0];
898
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
881
899
  if (!firstColumn || !lastColumn) {
882
900
  return;
883
901
  }
@@ -901,8 +919,9 @@ function createGridCore(inputOptions) {
901
919
  },
902
920
  setSelectAllVisible(checked) {
903
921
  const dataRows = getSnapshot().visibleRows.filter(isDataRow);
904
- const firstColumn = options.columns[0];
905
- const lastColumn = options.columns[options.columns.length - 1];
922
+ const effectiveColumns = getColumns();
923
+ const firstColumn = effectiveColumns[0];
924
+ const lastColumn = effectiveColumns[effectiveColumns.length - 1];
906
925
  if (!firstColumn || !lastColumn) {
907
926
  return;
908
927
  }
@@ -1009,8 +1028,31 @@ function createGridCore(inputOptions) {
1009
1028
  columnId: nextColumn.id
1010
1029
  };
1011
1030
  focus = nextAddr;
1012
- if (moveOptions.extend) {
1013
- if (!selection.anchor) {
1031
+ if (isDataRow(nextRow)) {
1032
+ if (moveOptions.extend) {
1033
+ if (!selection.anchor) {
1034
+ selection = {
1035
+ ranges: [
1036
+ {
1037
+ startRowId: nextAddr.rowId,
1038
+ endRowId: nextAddr.rowId,
1039
+ startColumnId: nextAddr.columnId,
1040
+ endColumnId: nextAddr.columnId
1041
+ }
1042
+ ],
1043
+ anchor: nextAddr
1044
+ };
1045
+ } else {
1046
+ const newActive = {
1047
+ startRowId: selection.anchor.rowId,
1048
+ endRowId: nextAddr.rowId,
1049
+ startColumnId: selection.anchor.columnId,
1050
+ endColumnId: nextAddr.columnId
1051
+ };
1052
+ const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
1053
+ selection = { ranges, anchor: selection.anchor };
1054
+ }
1055
+ } else {
1014
1056
  selection = {
1015
1057
  ranges: [
1016
1058
  {
@@ -1022,28 +1064,7 @@ function createGridCore(inputOptions) {
1022
1064
  ],
1023
1065
  anchor: nextAddr
1024
1066
  };
1025
- } else {
1026
- const newActive = {
1027
- startRowId: selection.anchor.rowId,
1028
- endRowId: nextAddr.rowId,
1029
- startColumnId: selection.anchor.columnId,
1030
- endColumnId: nextAddr.columnId
1031
- };
1032
- const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
1033
- selection = { ranges, anchor: selection.anchor };
1034
1067
  }
1035
- } else {
1036
- selection = {
1037
- ranges: [
1038
- {
1039
- startRowId: nextAddr.rowId,
1040
- endRowId: nextAddr.rowId,
1041
- startColumnId: nextAddr.columnId,
1042
- endColumnId: nextAddr.columnId
1043
- }
1044
- ],
1045
- anchor: nextAddr
1046
- };
1047
1068
  }
1048
1069
  emit();
1049
1070
  },
@@ -1268,10 +1289,22 @@ function createGridCore(inputOptions) {
1268
1289
  return { ...newCol };
1269
1290
  });
1270
1291
  const grouped = groupColumnsByPin(merged);
1271
- const changed = !sameColumnLayout(options.columns, grouped);
1292
+ const layoutChanged = !sameColumnLayout(options.columns, grouped);
1293
+ const groupingSemanticsChanged = !sameColumnGroupingSemantics(options.columns, grouped);
1294
+ const before = groupingSemanticsChanged ? captureVisibleRowsForFocusReconciliation() : null;
1272
1295
  originalColumns = groupColumnsByPin(nextColumns).map((c) => ({ ...c }));
1273
1296
  options = { ...options, columns: grouped };
1274
- if (changed) {
1297
+ const nextRowGroups = sanitizeRowGroups(rowGroups, grouped);
1298
+ const sanitizedGroupingChanged = !stringListsEqual(rowGroups, nextRowGroups);
1299
+ if (sanitizedGroupingChanged) {
1300
+ rowGroups = nextRowGroups;
1301
+ groupExpansionOverrides = /* @__PURE__ */ new Set();
1302
+ }
1303
+ if (groupingSemanticsChanged) {
1304
+ cachedVisibleRows = null;
1305
+ reconcileFocusAfterVisibleModelChange(before);
1306
+ }
1307
+ if (layoutChanged || groupingSemanticsChanged || sanitizedGroupingChanged) {
1275
1308
  emit();
1276
1309
  }
1277
1310
  },
@@ -1280,6 +1313,7 @@ function createGridCore(inputOptions) {
1280
1313
  throw new Error("applyTransaction requires getRowId on PretableGridOptions");
1281
1314
  }
1282
1315
  const getRowId = options.getRowId;
1316
+ const before = captureVisibleRowsForFocusReconciliation();
1283
1317
  if (transaction.remove) {
1284
1318
  const removeSet = new Set(transaction.remove);
1285
1319
  sourceRows = sourceRows.filter((entry) => {
@@ -1323,9 +1357,11 @@ function createGridCore(inputOptions) {
1323
1357
  }
1324
1358
  }
1325
1359
  cachedVisibleRows = null;
1360
+ reconcileFocusAfterVisibleModelChange(before);
1326
1361
  emit();
1327
1362
  },
1328
1363
  setRows(nextRows) {
1364
+ const before = captureVisibleRowsForFocusReconciliation();
1329
1365
  options = { ...options, rows: nextRows };
1330
1366
  sourceRows = createSourceRows(options);
1331
1367
  sourceRowIndex.clear();
@@ -1341,9 +1377,6 @@ function createGridCore(inputOptions) {
1341
1377
  anchor: anchorValid ? selection.anchor : null
1342
1378
  };
1343
1379
  }
1344
- if (focus.rowId !== null && !hasRow(focus.rowId)) {
1345
- focus = { rowId: null, columnId: null };
1346
- }
1347
1380
  if (editing && !hasRow(editing.rowId)) {
1348
1381
  editing = null;
1349
1382
  }
@@ -1368,6 +1401,7 @@ function createGridCore(inputOptions) {
1368
1401
  };
1369
1402
  }
1370
1403
  cachedVisibleRows = null;
1404
+ reconcileFocusAfterVisibleModelChange(before);
1371
1405
  emit();
1372
1406
  },
1373
1407
  setRowGroups(columnIds) {
@@ -1375,8 +1409,10 @@ function createGridCore(inputOptions) {
1375
1409
  if (stringListsEqual(rowGroups, next)) {
1376
1410
  return;
1377
1411
  }
1412
+ const before = captureVisibleRowsForFocusReconciliation();
1378
1413
  rowGroups = next;
1379
1414
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1415
+ reconcileFocusAfterVisibleModelChange(before);
1380
1416
  emit();
1381
1417
  },
1382
1418
  toggleGroup(groupId) {
@@ -1386,7 +1422,7 @@ function createGridCore(inputOptions) {
1386
1422
  if (isGroupExpanded(groupId) === expanded) {
1387
1423
  return;
1388
1424
  }
1389
- const before = getSnapshot().visibleRows;
1425
+ const before = captureVisibleRowsForFocusReconciliation();
1390
1426
  if (expanded === groupsDefaultExpanded) {
1391
1427
  const next = new Set(groupExpansionOverrides);
1392
1428
  next.delete(groupId);
@@ -1394,7 +1430,9 @@ function createGridCore(inputOptions) {
1394
1430
  } else {
1395
1431
  groupExpansionOverrides = addGroupExpansionOverride(groupExpansionOverrides, groupId, groupExpansionOverrideLimit);
1396
1432
  }
1397
- reanchorFocusAfterCollapse(before);
1433
+ reconcileFocusAfterVisibleModelChange(before, {
1434
+ preferAncestor: !expanded
1435
+ });
1398
1436
  emit();
1399
1437
  },
1400
1438
  expandAll() {
@@ -1469,32 +1507,51 @@ function createGridCore(inputOptions) {
1469
1507
  if (groupsDefaultExpanded === expanded && groupExpansionOverrides.size === 0) {
1470
1508
  return;
1471
1509
  }
1472
- const before = getSnapshot().visibleRows;
1510
+ const before = captureVisibleRowsForFocusReconciliation();
1473
1511
  groupsDefaultExpanded = expanded;
1474
1512
  groupExpansionOverrides = /* @__PURE__ */ new Set();
1475
- reanchorFocusAfterCollapse(before);
1513
+ reconcileFocusAfterVisibleModelChange(before, {
1514
+ preferAncestor: !expanded
1515
+ });
1476
1516
  emit();
1477
1517
  }
1478
- function reanchorFocusAfterCollapse(before) {
1479
- if (focus.rowId === null)
1480
- return;
1481
- const fromIndex = before.findIndex((entry) => entry.id === focus.rowId);
1482
- if (fromIndex === -1)
1518
+ function captureVisibleRowsForFocusReconciliation() {
1519
+ return focus.rowId === null && focus.columnId === null ? null : getSnapshot().visibleRows;
1520
+ }
1521
+ function reconcileFocusAfterVisibleModelChange(before, options2 = {}) {
1522
+ if (before === null || focus.rowId === null && focus.columnId === null) {
1483
1523
  return;
1524
+ }
1525
+ const oldRowId = focus.rowId;
1526
+ const oldIndex = oldRowId === null ? -1 : before.findIndex((entry) => entry.id === oldRowId);
1484
1527
  cachedSnapshot = null;
1485
- const after = getSnapshot().visibleRows;
1486
- if (after.some((entry) => entry.id === focus.rowId))
1528
+ const afterRows = getSnapshot().visibleRows;
1529
+ const afterColumns = getColumns();
1530
+ if (afterRows.length === 0 || afterColumns.length === 0) {
1531
+ focus = { rowId: null, columnId: null };
1532
+ cachedSnapshot = null;
1487
1533
  return;
1488
- const surviving = new Set(after.map((entry) => entry.id));
1489
- let nextRowId = null;
1490
- for (let i = fromIndex - 1; i >= 0; i -= 1) {
1491
- const candidate = before[i];
1492
- if (candidate.kind === "group" && surviving.has(candidate.id)) {
1493
- nextRowId = candidate.id;
1494
- break;
1534
+ }
1535
+ const survivingRowIds = new Set(afterRows.map((entry) => entry.id));
1536
+ let nextRowId = oldRowId !== null && survivingRowIds.has(oldRowId) ? oldRowId : null;
1537
+ if (nextRowId === null && options2.preferAncestor === true && oldIndex !== -1) {
1538
+ const oldDepth = before[oldIndex].depth;
1539
+ for (let i = oldIndex - 1; i >= 0; i -= 1) {
1540
+ const candidate = before[i];
1541
+ if (candidate.kind === "group" && candidate.depth < oldDepth && survivingRowIds.has(candidate.id)) {
1542
+ nextRowId = candidate.id;
1543
+ break;
1544
+ }
1495
1545
  }
1496
1546
  }
1497
- focus = nextRowId === null ? { rowId: null, columnId: null } : { rowId: nextRowId, columnId: focus.columnId };
1547
+ if (nextRowId === null) {
1548
+ const nextIndex = oldIndex === -1 ? 0 : clamp(oldIndex, 0, afterRows.length - 1);
1549
+ nextRowId = afterRows[nextIndex].id;
1550
+ }
1551
+ const focusedColumnSurvives = focus.columnId !== null && afterColumns.some((column) => column.id === focus.columnId);
1552
+ const groupColumn = rowGroups.length > 0 ? afterColumns.find((column) => column.id === GROUP_COLUMN_ID) : void 0;
1553
+ const nextColumnId = focusedColumnSurvives ? focus.columnId : groupColumn?.id ?? afterColumns[0].id;
1554
+ focus = { rowId: nextRowId, columnId: nextColumnId };
1498
1555
  cachedSnapshot = null;
1499
1556
  }
1500
1557
  function preserveAggregateIdentity(rows) {
@@ -1597,6 +1654,15 @@ function sameColumnLayout(a, b) {
1597
1654
  return left.id === right.id && left.header === right.header && left.widthPx === right.widthPx && left.minWidthPx === right.minWidthPx && left.maxWidthPx === right.maxWidthPx && left.pinned === right.pinned && left.sortable === right.sortable && left.filterable === right.filterable && left.resizable === right.resizable && left.reorderable === right.reorderable && left.wrap === right.wrap;
1598
1655
  });
1599
1656
  }
1657
+ function sameColumnGroupingSemantics(a, b) {
1658
+ if (a.length !== b.length) {
1659
+ return false;
1660
+ }
1661
+ return a.every((left, index) => {
1662
+ const right = b[index];
1663
+ return left.id === right.id && left.value === right.value && left.aggregate === right.aggregate;
1664
+ });
1665
+ }
1600
1666
  function computePageStep(viewport, rowCount) {
1601
1667
  if (viewport.height <= 0 || rowCount === 0) {
1602
1668
  return 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretable/core",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Framework-agnostic grid state primitives for Pretable.",
5
5
  "license": "MIT",
6
6
  "repository": {