@pretable/core 0.7.0 → 0.9.0

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
@@ -917,6 +917,72 @@ function isIndexedRowSelected(selection, ref, snapshot) {
917
917
  return false;
918
918
  return semanticRowSelected(rowSelectionProgram(selection, snapshot), ref.rowId);
919
919
  }
920
+ function createIndexedRowSelection(requested, snapshot) {
921
+ let selection = createEmptyIndexedSelection();
922
+ if (requested.kind === "all") {
923
+ selection = selectAllVisibleRows(selection, snapshot);
924
+ } else {
925
+ for (const rowId of new Set(requested.rowIds)) {
926
+ selection = toggleIndexedRowSelection(selection, rowId, snapshot);
927
+ }
928
+ for (const range of requested.ranges ?? []) {
929
+ selection = selectIndexedRowRange(selection, range.startRowId, range.endRowId, snapshot);
930
+ }
931
+ }
932
+ for (const rowId of new Set(requested.excludedRowIds)) {
933
+ if (!isIndexedRowSelected(selection, dataRef(rowId), snapshot))
934
+ continue;
935
+ selection = toggleIndexedRowSelection(selection, rowId, snapshot);
936
+ }
937
+ return selection.rows;
938
+ }
939
+ function describeRowSelection(rows) {
940
+ const excluded = /* @__PURE__ */ new Set();
941
+ for (const range of rows.excludedRanges ?? []) {
942
+ excluded.add(range.startRowId);
943
+ excluded.add(range.endRowId);
944
+ }
945
+ const excludedRowIds = excluded.size === 0 ? {} : { excludedRowIds: Object.freeze([...excluded]) };
946
+ if (rows.kind === "all") {
947
+ return Object.freeze({ kind: "all", ...excludedRowIds });
948
+ }
949
+ const ranges = [...rows.ranges ?? []].map((range) => Object.freeze({ startRowId: range.startRowId, endRowId: range.endRowId }));
950
+ return Object.freeze({
951
+ kind: "explicit",
952
+ rowIds: Object.freeze([...rows.rowIds]),
953
+ ...ranges.length === 0 ? {} : { ranges: Object.freeze(ranges) },
954
+ ...excludedRowIds
955
+ });
956
+ }
957
+ function sameIndexedRowSelectionValue(left, right) {
958
+ if (left === right)
959
+ return true;
960
+ if (left.kind !== right.kind)
961
+ return false;
962
+ if (!sameRowRangeIndex(left.excludedRanges, right.excludedRanges))
963
+ return false;
964
+ if (left.kind !== "explicit" || right.kind !== "explicit")
965
+ return true;
966
+ if (left.rowIds.size !== right.rowIds.size)
967
+ return false;
968
+ for (const rowId of left.rowIds) {
969
+ if (!right.rowIds.has(rowId))
970
+ return false;
971
+ }
972
+ return sameRowRangeIndex(left.ranges, right.ranges);
973
+ }
974
+ function sameRowRangeIndex(left, right) {
975
+ if (left === right)
976
+ return true;
977
+ if ((left?.size ?? 0) !== (right?.size ?? 0))
978
+ return false;
979
+ const leftRanges = [...left ?? []];
980
+ const rightRanges = [...right ?? []];
981
+ return leftRanges.every((range, index) => {
982
+ const other = rightRanges[index];
983
+ return other !== void 0 && sameRowId(range.startRowId, other.startRowId) && sameRowId(range.endRowId, other.endRowId);
984
+ });
985
+ }
920
986
  function getIndexedSelectionSummary(selection, snapshot) {
921
987
  const visibleCount2 = snapshot.visibleDataRowCount;
922
988
  const selectedCount = projectionFor(rowSelectionProgram(selection, snapshot), snapshot).runs?.selectedCount ?? 0;
@@ -926,6 +992,79 @@ function getIndexedSelectionSummary(selection, snapshot) {
926
992
  visibleCount: visibleCount2
927
993
  });
928
994
  }
995
+ function datasetPosition(snapshot, rowId, loadedWindow) {
996
+ const rank = snapshot.dataIndexOf(dataRef(rowId));
997
+ if (rank < 0)
998
+ return -1;
999
+ return (loadedWindow?.start ?? 0) + rank;
1000
+ }
1001
+ function spanReadableInWindow(span, window) {
1002
+ return window.datasetKey !== void 0 && span.datasetKey === window.datasetKey;
1003
+ }
1004
+ function windowCarriesSpans(window) {
1005
+ return window !== null && window.datasetKey !== void 0;
1006
+ }
1007
+ function endpointPositions(range, snapshot, loadedWindow) {
1008
+ const liveStart = datasetPosition(snapshot, range.start.rowId, loadedWindow);
1009
+ const liveEnd = datasetPosition(snapshot, range.end.rowId, loadedWindow);
1010
+ const bothLive = liveStart >= 0 && liveEnd >= 0;
1011
+ if (bothLive)
1012
+ return { start: liveStart, end: liveEnd, bothLive };
1013
+ if (loadedWindow === null)
1014
+ return { start: void 0, end: void 0, bothLive };
1015
+ const remembered = range.datasetRowSpan !== void 0 && spanReadableInWindow(range.datasetRowSpan, loadedWindow) ? range.datasetRowSpan : void 0;
1016
+ return {
1017
+ start: liveStart >= 0 ? liveStart : remembered?.start,
1018
+ end: liveEnd >= 0 ? liveEnd : remembered?.end,
1019
+ bothLive
1020
+ };
1021
+ }
1022
+ function rangeDatasetSpan(range, snapshot, loadedWindow) {
1023
+ const { start, end, bothLive } = endpointPositions(range, snapshot, loadedWindow);
1024
+ const low = start ?? end;
1025
+ const high = end ?? start;
1026
+ if (low === void 0 || high === void 0)
1027
+ return void 0;
1028
+ return {
1029
+ lo: Math.min(low, high),
1030
+ hi: Math.max(low, high),
1031
+ // Both endpoints present is not on its own enough to call the extent
1032
+ // proven. Under a window that CAN carry spans, a range without one has no
1033
+ // positional identity: reconciliation stripped it, which it only does
1034
+ // when it could not locate the range in the current population. The rows
1035
+ // it still names really are selected — so it paints — but the extent it
1036
+ // reports is a remnant, not what the user chose, and saying otherwise is
1037
+ // the same "silent under-count wearing a verified flag" this whole
1038
+ // design exists to remove.
1039
+ verified: bothLive && (!windowCarriesSpans(loadedWindow) || range.datasetRowSpan !== void 0)
1040
+ };
1041
+ }
1042
+ function getIndexedCellSelectionSummary(selection, snapshot, loadedWindow) {
1043
+ const window = loadedWindow ?? null;
1044
+ const spans = [];
1045
+ let verified = true;
1046
+ for (const range of selection.ranges) {
1047
+ const span = rangeDatasetSpan(range, snapshot, window);
1048
+ if (span === void 0) {
1049
+ verified = false;
1050
+ continue;
1051
+ }
1052
+ if (!span.verified)
1053
+ verified = false;
1054
+ spans.push(span);
1055
+ }
1056
+ spans.sort((left, right) => left.lo - right.lo);
1057
+ let rowCount = 0;
1058
+ let covered = null;
1059
+ for (const span of spans) {
1060
+ const lo = covered === null || span.lo > covered ? span.lo : covered + 1;
1061
+ if (span.hi >= lo)
1062
+ rowCount += span.hi - lo + 1;
1063
+ if (covered === null || span.hi > covered)
1064
+ covered = span.hi;
1065
+ }
1066
+ return Object.freeze({ rowCount, verified });
1067
+ }
929
1068
  function visibleAddress(address, snapshot) {
930
1069
  return isVisibleData(snapshot, address.rowId);
931
1070
  }
@@ -975,7 +1114,84 @@ function projectIndexedSelection(selection, previousSnapshot, snapshot, sequence
975
1114
  const rows = cloneRowsWithProgram(selection.rows, nextProgram);
976
1115
  return Object.freeze({ ...selection, rows });
977
1116
  }
978
- function reconcileIndexedSelection(selection, snapshot) {
1117
+ function provenDeletedRow(rowId, loadedWindow, previous) {
1118
+ if (previous === void 0 || previous.window == null)
1119
+ return false;
1120
+ const previousRank = previous.snapshot.dataIndexOf(dataRef(rowId));
1121
+ if (previousRank < 0)
1122
+ return false;
1123
+ const previousAbsolute = previous.window.start + previousRank;
1124
+ return previousAbsolute >= loadedWindow.start && previousAbsolute < loadedWindow.start + loadedWindow.length;
1125
+ }
1126
+ function sameSpan(left, right) {
1127
+ if (left === right)
1128
+ return true;
1129
+ if (left === void 0 || right === void 0)
1130
+ return false;
1131
+ return left.start === right.start && left.end === right.end && left.datasetKey === right.datasetKey;
1132
+ }
1133
+ function withoutDatasetRowSpan(range) {
1134
+ if (range.datasetRowSpan === void 0)
1135
+ return range;
1136
+ return Object.freeze({ start: range.start, end: range.end });
1137
+ }
1138
+ function stampDatasetRowSpan(range, snapshot, loadedWindow) {
1139
+ if (loadedWindow === null)
1140
+ return range;
1141
+ if (!windowCarriesSpans(loadedWindow))
1142
+ return withoutDatasetRowSpan(range);
1143
+ const { start, end } = endpointPositions(range, snapshot, loadedWindow);
1144
+ if (start === void 0 || end === void 0) {
1145
+ return range.datasetRowSpan === void 0 || spanReadableInWindow(range.datasetRowSpan, loadedWindow) ? range : withoutDatasetRowSpan(range);
1146
+ }
1147
+ const next = Object.freeze({
1148
+ start,
1149
+ end,
1150
+ datasetKey: loadedWindow.datasetKey
1151
+ });
1152
+ if (sameSpan(range.datasetRowSpan, next))
1153
+ return range;
1154
+ return Object.freeze({ ...range, datasetRowSpan: next });
1155
+ }
1156
+ function adoptIndexedCellRangeSpans(ranges, replaced, snapshot, loadedWindow) {
1157
+ if (!windowCarriesSpans(loadedWindow))
1158
+ return ranges.map((range) => stampDatasetRowSpan(range, snapshot, loadedWindow));
1159
+ const remembered = /* @__PURE__ */ new Map();
1160
+ for (const range of replaced) {
1161
+ const span = range.datasetRowSpan;
1162
+ if (span === void 0)
1163
+ continue;
1164
+ if (!spanReadableInWindow(span, loadedWindow))
1165
+ continue;
1166
+ if (!remembered.has(range.start.rowId))
1167
+ remembered.set(range.start.rowId, span.start);
1168
+ if (!remembered.has(range.end.rowId))
1169
+ remembered.set(range.end.rowId, span.end);
1170
+ }
1171
+ return ranges.map((range) => {
1172
+ const own = endpointPositions(range, snapshot, loadedWindow);
1173
+ const start = own.start ?? remembered.get(range.start.rowId);
1174
+ const end = own.end ?? remembered.get(range.end.rowId);
1175
+ if (start === void 0 || end === void 0)
1176
+ return stampDatasetRowSpan(range, snapshot, loadedWindow);
1177
+ const next = Object.freeze({
1178
+ start,
1179
+ end,
1180
+ datasetKey: loadedWindow.datasetKey
1181
+ });
1182
+ if (sameSpan(range.datasetRowSpan, next))
1183
+ return range;
1184
+ return Object.freeze({ ...range, datasetRowSpan: next });
1185
+ });
1186
+ }
1187
+ function reconcileIndexedSelection(selection, snapshot, eviction) {
1188
+ const suppliedWindow = eviction?.window ?? null;
1189
+ const previous = eviction?.previous;
1190
+ const populationChanged = suppliedWindow !== null && previous?.window != null && // Value comparison, not `spanReadableInWindow`: two keyless windows are
1191
+ // not evidence of a change. A keyless consumer gets no span trust at all
1192
+ // (see `windowCarriesSpans`), so there is nothing here to invalidate.
1193
+ previous.window.datasetKey !== suppliedWindow.datasetKey;
1194
+ const retentionWindow = populationChanged ? null : suppliedWindow;
979
1195
  let changed = false;
980
1196
  rowSelectionProgram(selection, snapshot);
981
1197
  const ranges = [];
@@ -983,11 +1199,41 @@ function reconcileIndexedSelection(selection, snapshot) {
983
1199
  const startVisible = visibleAddress(range.start, snapshot);
984
1200
  const endVisible = visibleAddress(range.end, snapshot);
985
1201
  if (startVisible && endVisible) {
986
- ranges.push(range);
1202
+ const stamped = stampDatasetRowSpan(range, snapshot, suppliedWindow);
1203
+ if (stamped !== range)
1204
+ changed = true;
1205
+ ranges.push(stamped);
987
1206
  } else if (startVisible || endVisible) {
988
1207
  const survivor = startVisible ? range.start : range.end;
989
- ranges.push(Object.freeze({ start: survivor, end: survivor }));
990
- changed = true;
1208
+ const absentee = startVisible ? range.end : range.start;
1209
+ if (retentionWindow === null || provenDeletedRow(absentee.rowId, retentionWindow, previous)) {
1210
+ ranges.push(stampDatasetRowSpan(
1211
+ Object.freeze({ start: survivor, end: survivor }),
1212
+ snapshot,
1213
+ // `retentionWindow`, not `suppliedWindow`. On a population change
1214
+ // this is null, so the collapsed range gets NO span — which is
1215
+ // the whole point: it is a remnant of a selection the engine
1216
+ // could not locate in the new population, and a span would let
1217
+ // the summary call its one surviving row a proven extent. See
1218
+ // `rangeDatasetSpan`, which reads a missing span under a
1219
+ // span-carrying window as exactly that doubt.
1220
+ retentionWindow
1221
+ ));
1222
+ changed = true;
1223
+ } else {
1224
+ const retained = stampDatasetRowSpan(range, snapshot, retentionWindow);
1225
+ if (retained !== range)
1226
+ changed = true;
1227
+ ranges.push(retained);
1228
+ }
1229
+ } else if (retentionWindow != null) {
1230
+ const startDeleted = provenDeletedRow(range.start.rowId, retentionWindow, previous);
1231
+ const endDeleted = provenDeletedRow(range.end.rowId, retentionWindow, previous);
1232
+ if (startDeleted || endDeleted) {
1233
+ changed = true;
1234
+ } else {
1235
+ ranges.push(range);
1236
+ }
991
1237
  } else {
992
1238
  changed = true;
993
1239
  }
@@ -1069,7 +1315,7 @@ function orderPinnedColumns(columns) {
1069
1315
  ...columns.filter((column) => column.pinned === "right")
1070
1316
  ];
1071
1317
  }
1072
- function copySelection(selection) {
1318
+ function copySelection(selection, context) {
1073
1319
  const rows = selection.rows.kind === "explicit" ? Object.freeze({
1074
1320
  kind: "explicit",
1075
1321
  rowIds: toImmutableIndexedSet(selection.rows.rowIds),
@@ -1082,10 +1328,15 @@ function copySelection(selection) {
1082
1328
  preserveIndexedRowSelectionProgram(selection.rows, rows);
1083
1329
  return Object.freeze({
1084
1330
  rows,
1085
- ranges: Object.freeze(selection.ranges.map((range) => Object.freeze({
1331
+ ranges: Object.freeze(adoptIndexedCellRangeSpans(selection.ranges.map((range) => Object.freeze({
1086
1332
  start: Object.freeze({ ...range.start }),
1087
- end: Object.freeze({ ...range.end })
1088
- }))),
1333
+ end: Object.freeze({ ...range.end }),
1334
+ // Copied, not rebuilt from ids: this is the only public write
1335
+ // path, so dropping the span here un-counts every retained range
1336
+ // in the selection — including the ones the gesture never
1337
+ // touched.
1338
+ ...range.datasetRowSpan === void 0 ? {} : { datasetRowSpan: Object.freeze({ ...range.datasetRowSpan }) }
1339
+ })), context.replaced, context.snapshot, context.loadedWindow)),
1089
1340
  anchor: selection.anchor === null ? null : Object.freeze({ ...selection.anchor })
1090
1341
  });
1091
1342
  }
@@ -1119,6 +1370,13 @@ function sameRowRanges(left, right) {
1119
1370
  return false;
1120
1371
  }
1121
1372
  }
1373
+ function sameDatasetRowSpan(left, right) {
1374
+ if (left === right)
1375
+ return true;
1376
+ if (left === void 0 || right === void 0)
1377
+ return false;
1378
+ return left.start === right.start && left.end === right.end && left.datasetKey === right.datasetKey;
1379
+ }
1122
1380
  function sameSelection(left, right) {
1123
1381
  if (left.rows.kind !== right.rows.kind)
1124
1382
  return false;
@@ -1137,7 +1395,14 @@ function sameSelection(left, right) {
1137
1395
  for (let index = 0; index < left.ranges.length; index += 1) {
1138
1396
  const leftRange = left.ranges[index];
1139
1397
  const rightRange = right.ranges[index];
1140
- if (!sameValueZero(leftRange.start.rowId, rightRange.start.rowId) || leftRange.start.columnId !== rightRange.start.columnId || !sameValueZero(leftRange.end.rowId, rightRange.end.rowId) || leftRange.end.columnId !== rightRange.end.columnId) {
1398
+ if (!sameValueZero(leftRange.start.rowId, rightRange.start.rowId) || leftRange.start.columnId !== rightRange.start.columnId || !sameValueZero(leftRange.end.rowId, rightRange.end.rowId) || leftRange.end.columnId !== rightRange.end.columnId || // The span is part of a range's identity, not decoration on it: while
1399
+ // the rows are evicted it is the ONLY thing that says how large the
1400
+ // selection is, and two ranges over the same ids with different spans
1401
+ // select different numbers of rows. Comparing it is safe precisely
1402
+ // because `copySelection` recovers a missing span from the selection
1403
+ // being replaced — so a controlled `state` echo that drops the field
1404
+ // still compares equal, and the effect stays the no-op it has to be.
1405
+ !sameDatasetRowSpan(leftRange.datasetRowSpan, rightRange.datasetRowSpan)) {
1141
1406
  return false;
1142
1407
  }
1143
1408
  }
@@ -1158,7 +1423,7 @@ function createGridUiCore(options) {
1158
1423
  let draining = false;
1159
1424
  let projecting = false;
1160
1425
  let activeProjectionToken;
1161
- let observedSnapshot;
1426
+ let observed;
1162
1427
  let state = Object.freeze({
1163
1428
  viewport: options.viewport ? normalizeViewport(options.viewport) : EMPTY_VIEWPORT,
1164
1429
  focus: Object.freeze({ ref: null, columnId: null }),
@@ -1228,11 +1493,12 @@ function createGridUiCore(options) {
1228
1493
  const observationError = (message, cause) => cause instanceof PretableGridUiError && cause.code === "row-model-observation-failed" ? cause : new PretableGridUiError("row-model-observation-failed", message, cause);
1229
1494
  const snapshotForInteraction = () => {
1230
1495
  try {
1231
- return observedSnapshot ?? options.rowModel.getState().snapshot;
1496
+ return observed?.snapshot ?? options.rowModel.getState().snapshot;
1232
1497
  } catch (cause) {
1233
1498
  throw new PretableGridUiError("row-model-observation-failed", "The row-model snapshot could not be read for a UI interaction.", cause);
1234
1499
  }
1235
1500
  };
1501
+ const interactionContext = () => observed ?? { snapshot: snapshotForInteraction(), window: null };
1236
1502
  const navigationColumnIds = () => {
1237
1503
  if (cachedNavigationLayout !== state.columnLayout) {
1238
1504
  cachedNavigationLayout = state.columnLayout;
@@ -1245,7 +1511,7 @@ function createGridUiCore(options) {
1245
1511
  return;
1246
1512
  disposed = true;
1247
1513
  activeProjectionToken = void 0;
1248
- observedSnapshot = void 0;
1514
+ observed = void 0;
1249
1515
  releaseIndexedRowSelectionProgram(state.selection.rows);
1250
1516
  queuedActions.length = 0;
1251
1517
  const captured = Array.from(listeners);
@@ -1313,7 +1579,12 @@ function createGridUiCore(options) {
1313
1579
  setSelection(selection) {
1314
1580
  let next;
1315
1581
  try {
1316
- next = copySelection(selection);
1582
+ const context = interactionContext();
1583
+ next = copySelection(selection, {
1584
+ replaced: state.selection.ranges,
1585
+ snapshot: context.snapshot,
1586
+ loadedWindow: context.window
1587
+ });
1317
1588
  } catch (cause) {
1318
1589
  throw new PretableGridUiError("invalid-ui-state", "The supplied selection could not be captured atomically.", cause);
1319
1590
  }
@@ -1323,6 +1594,22 @@ function createGridUiCore(options) {
1323
1594
  publish({ ...state, selection: next });
1324
1595
  });
1325
1596
  },
1597
+ setRowSelection(rows) {
1598
+ command(() => {
1599
+ let nextRows;
1600
+ try {
1601
+ nextRows = createIndexedRowSelection(rows, snapshotForInteraction());
1602
+ } catch (cause) {
1603
+ throw observationError("The supplied row selection could not be applied atomically.", cause);
1604
+ }
1605
+ if (sameIndexedRowSelectionValue(state.selection.rows, nextRows))
1606
+ return;
1607
+ publish({
1608
+ ...state,
1609
+ selection: Object.freeze({ ...state.selection, rows: nextRows })
1610
+ });
1611
+ });
1612
+ },
1326
1613
  toggleRowSelection(rowId) {
1327
1614
  command(() => {
1328
1615
  let next;
@@ -1363,6 +1650,14 @@ function createGridUiCore(options) {
1363
1650
  throw observationError("Indexed row selection summary could not be observed atomically.", cause);
1364
1651
  }
1365
1652
  },
1653
+ getCellSelectionSummary() {
1654
+ try {
1655
+ const context = interactionContext();
1656
+ return getIndexedCellSelectionSummary(state.selection, context.snapshot, context.window);
1657
+ } catch (cause) {
1658
+ throw observationError("Indexed cell selection summary could not be observed atomically.", cause);
1659
+ }
1660
+ },
1366
1661
  selectAllVisibleRows() {
1367
1662
  command(() => {
1368
1663
  let next;
@@ -1393,7 +1688,7 @@ function createGridUiCore(options) {
1393
1688
  status: "editing"
1394
1689
  });
1395
1690
  command(() => {
1396
- const snapshot = observedSnapshot;
1691
+ const snapshot = observed?.snapshot;
1397
1692
  let visible = false;
1398
1693
  try {
1399
1694
  visible = snapshot !== void 0 && snapshot.indexOf({ kind: "data", rowId: input.rowId }) >= 0;
@@ -1537,7 +1832,8 @@ function createGridUiCore(options) {
1537
1832
  return;
1538
1833
  }
1539
1834
  const focus = reconcileIndexedFocus(state.focus, snapshot);
1540
- const selection = reconcileIndexedSelection(observedSnapshot === void 0 ? state.selection : projectIndexedSelection(state.selection, observedSnapshot, snapshot, options.rowModel.changesSince(observedSnapshot.revision)), snapshot);
1835
+ const selectionWindow = options.getSelectionWindow?.() ?? null;
1836
+ const selection = reconcileIndexedSelection(observed === void 0 ? state.selection : projectIndexedSelection(state.selection, observed.snapshot, snapshot, options.rowModel.changesSince(observed.snapshot.revision)), snapshot, { window: selectionWindow, previous: observed });
1541
1837
  let editing = state.editing;
1542
1838
  if (editing !== null && snapshot.indexOf({ kind: "data", rowId: editing.rowId }) < 0) {
1543
1839
  editing = null;
@@ -1552,7 +1848,7 @@ function createGridUiCore(options) {
1552
1848
  }
1553
1849
  if (disposed || activeProjectionToken !== token)
1554
1850
  return;
1555
- observedSnapshot = snapshot;
1851
+ observed = { snapshot, window: selectionWindow };
1556
1852
  committed = true;
1557
1853
  publish({
1558
1854
  ...state,
@@ -8395,4 +8691,5 @@ exports.PretableUnsupportedRowUpdateError = PretableUnsupportedRowUpdateError;
8395
8691
  exports.createColumnHelper = createColumnHelper2;
8396
8692
  exports.createGrid = createGrid;
8397
8693
  exports.createLocalRowModel = createLocalRowModel2;
8694
+ exports.describeRowSelection = describeRowSelection;
8398
8695
  exports.numberFormats = numberFormats;