@pretable/core 0.0.2 → 0.0.3

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
@@ -35,6 +35,197 @@ function autosizeColumns(input) {
35
35
  return { widths };
36
36
  }
37
37
 
38
+ // ../grid-core/dist/evaluate-filter.js
39
+ var NO_OPERAND = /* @__PURE__ */ new Set([
40
+ "isEmpty",
41
+ "isNotEmpty"
42
+ ]);
43
+ function isFilterActive(filter) {
44
+ const { operator, value } = filter;
45
+ if (NO_OPERAND.has(operator))
46
+ return true;
47
+ if (value === null || value === void 0)
48
+ return false;
49
+ if (typeof value === "string")
50
+ return value.trim() !== "";
51
+ if (Array.isArray(value))
52
+ return value.length > 0;
53
+ return true;
54
+ }
55
+ function isEmptyCell(cell) {
56
+ if (cell === null || cell === void 0)
57
+ return true;
58
+ if (typeof cell === "number")
59
+ return Number.isNaN(cell);
60
+ return String(cell).trim() === "";
61
+ }
62
+ function toBooleanCell(value) {
63
+ if (typeof value === "boolean")
64
+ return value;
65
+ if (value === "true" || value === 1 || value === "1")
66
+ return true;
67
+ if (value === "false" || value === 0 || value === "0")
68
+ return false;
69
+ return Boolean(value);
70
+ }
71
+ var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
72
+ var ISO_DATETIME_RE = /^(\d{4}-\d{2}-\d{2})[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/i;
73
+ var MAX_ISO_YEAR = 9999;
74
+ var GREGORIAN_400Y_MS = 146097 * 864e5;
75
+ function utcMs(year, month, day) {
76
+ return year >= 0 && year < 100 ? Date.UTC(year + 400, month, day) - GREGORIAN_400Y_MS : Date.UTC(year, month, day);
77
+ }
78
+ function utcDayOf(ms) {
79
+ const d = new Date(ms);
80
+ if (Number.isNaN(d.getTime()))
81
+ return Number.NaN;
82
+ const year = d.getUTCFullYear();
83
+ if (year < 0 || year > MAX_ISO_YEAR)
84
+ return Number.NaN;
85
+ d.setUTCHours(0, 0, 0, 0);
86
+ return d.getTime();
87
+ }
88
+ function isoDayMs(iso) {
89
+ if (!ISO_DATE_RE.test(iso))
90
+ return Number.NaN;
91
+ const [y, m, d] = iso.split("-").map(Number);
92
+ const ms = utcMs(y, m - 1, d);
93
+ const back = new Date(ms);
94
+ return back.getUTCFullYear() === y && back.getUTCMonth() === m - 1 && back.getUTCDate() === d ? ms : Number.NaN;
95
+ }
96
+ function toDayMs(input) {
97
+ if (input instanceof Date)
98
+ return utcDayOf(input.getTime());
99
+ if (typeof input === "number")
100
+ return utcDayOf(input);
101
+ if (typeof input !== "string")
102
+ return Number.NaN;
103
+ const trimmed = input.trim();
104
+ const dateOnly = isoDayMs(trimmed);
105
+ if (!Number.isNaN(dateOnly))
106
+ return dateOnly;
107
+ const parts = ISO_DATETIME_RE.exec(trimmed);
108
+ if (!parts)
109
+ return Number.NaN;
110
+ const day = isoDayMs(parts[1]);
111
+ if (Number.isNaN(day))
112
+ return Number.NaN;
113
+ return parts[2] ? utcDayOf(Date.parse(trimmed.replace(" ", "T"))) : day;
114
+ }
115
+ function evaluateFilter(cell, type, operator, value) {
116
+ if (operator === "isEmpty")
117
+ return isEmptyCell(cell);
118
+ if (operator === "isNotEmpty")
119
+ return !isEmptyCell(cell);
120
+ switch (type) {
121
+ case "number": {
122
+ const n = typeof cell === "number" ? cell : Number(cell);
123
+ if (Number.isNaN(n))
124
+ return false;
125
+ switch (operator) {
126
+ case "equals":
127
+ return n === Number(value);
128
+ case "notEquals":
129
+ return n !== Number(value);
130
+ case "gt":
131
+ return n > Number(value);
132
+ case "gte":
133
+ return n >= Number(value);
134
+ case "lt":
135
+ return n < Number(value);
136
+ case "lte":
137
+ return n <= Number(value);
138
+ case "between": {
139
+ if (!Array.isArray(value))
140
+ return false;
141
+ const a = Number(value[0]);
142
+ const b = Number(value[1]);
143
+ const lo = Math.min(a, b);
144
+ const hi = Math.max(a, b);
145
+ return n >= lo && n <= hi;
146
+ }
147
+ default:
148
+ return false;
149
+ }
150
+ }
151
+ case "date": {
152
+ const c = toDayMs(cell);
153
+ if (Number.isNaN(c))
154
+ return false;
155
+ switch (operator) {
156
+ case "on":
157
+ return c === toDayMs(value);
158
+ case "before":
159
+ return c < toDayMs(value);
160
+ case "after":
161
+ return c > toDayMs(value);
162
+ case "dateBetween": {
163
+ if (!Array.isArray(value))
164
+ return false;
165
+ const a = toDayMs(value[0]);
166
+ const b = toDayMs(value[1]);
167
+ if (Number.isNaN(a) || Number.isNaN(b))
168
+ return false;
169
+ const lo = Math.min(a, b);
170
+ const hi = Math.max(a, b);
171
+ return c >= lo && c <= hi;
172
+ }
173
+ default:
174
+ return false;
175
+ }
176
+ }
177
+ case "boolean": {
178
+ const c = String(toBooleanCell(cell));
179
+ const set = Array.isArray(value) ? value.map(String) : [];
180
+ if (set.length === 0)
181
+ return true;
182
+ switch (operator) {
183
+ case "isAnyOf":
184
+ return set.includes(c);
185
+ case "isNoneOf":
186
+ return !set.includes(c);
187
+ default:
188
+ return false;
189
+ }
190
+ }
191
+ case "enum": {
192
+ const c = String(cell);
193
+ const set = Array.isArray(value) ? value.map(String) : [];
194
+ if (set.length === 0)
195
+ return true;
196
+ switch (operator) {
197
+ case "isAnyOf":
198
+ return set.includes(c);
199
+ case "isNoneOf":
200
+ return !set.includes(c);
201
+ default:
202
+ return false;
203
+ }
204
+ }
205
+ case "text":
206
+ default: {
207
+ const hay = String(cell ?? "").toLowerCase();
208
+ const needle = String(value ?? "").toLowerCase();
209
+ switch (operator) {
210
+ case "contains":
211
+ return hay.includes(needle);
212
+ case "notContains":
213
+ return !hay.includes(needle);
214
+ case "equals":
215
+ return hay === needle;
216
+ case "notEquals":
217
+ return hay !== needle;
218
+ case "startsWith":
219
+ return hay.startsWith(needle);
220
+ case "endsWith":
221
+ return hay.endsWith(needle);
222
+ default:
223
+ return false;
224
+ }
225
+ }
226
+ }
227
+ }
228
+
38
229
  // ../grid-core/dist/derived-rows.js
39
230
  function createSourceRows(options) {
40
231
  return options.rows.map((row, index) => ({
@@ -56,22 +247,20 @@ function deriveVisibleRows(input) {
56
247
  function resolveFilters(columns, filters) {
57
248
  const columnMap = new Map(columns.map((c) => [c.id, c]));
58
249
  const resolved = [];
59
- for (const [columnId, rawNeedle] of Object.entries(filters)) {
60
- if (!rawNeedle) {
250
+ for (const [columnId, filter] of Object.entries(filters)) {
251
+ if (!filter || !isFilterActive(filter))
61
252
  continue;
62
- }
63
253
  const column = columnMap.get(columnId);
64
- if (!column) {
254
+ if (!column || column.filterable === false)
65
255
  continue;
66
- }
67
- resolved.push({ column, needle: rawNeedle.toLowerCase() });
256
+ resolved.push({ column, filter });
68
257
  }
69
258
  return resolved;
70
259
  }
71
260
  function matchesFilters(row, resolvedFilters) {
72
- for (const { column, needle } of resolvedFilters) {
73
- const haystack = String(readCellValue(row, column)).toLowerCase();
74
- if (!haystack.includes(needle)) {
261
+ for (const { column, filter } of resolvedFilters) {
262
+ const cell = readCellValue(row, column);
263
+ if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
75
264
  return false;
76
265
  }
77
266
  }
@@ -82,30 +271,31 @@ var collator = new Intl.Collator(void 0, {
82
271
  sensitivity: "base"
83
272
  });
84
273
  function sortRows(rows, columns, sort) {
85
- if (!sort.columnId || !sort.direction) {
86
- return [...rows];
87
- }
88
- const column = columns.find((candidate) => candidate.id === sort.columnId);
89
- if (!column) {
274
+ const keys = sort.map((entry) => {
275
+ const column = columns.find((c) => c.id === entry.columnId);
276
+ if (!column)
277
+ return null;
278
+ const rawKeys = rows.map((r) => readCellValue(r.row, column));
279
+ const allNumeric = rawKeys.every((v) => typeof v === "number");
280
+ const multiplier = entry.direction === "asc" ? 1 : -1;
281
+ return allNumeric ? { kind: "num", keys: rawKeys, multiplier } : {
282
+ kind: "str",
283
+ keys: rawKeys.map((v) => String(v ?? "")),
284
+ multiplier
285
+ };
286
+ }).filter((k) => k !== null);
287
+ if (keys.length === 0) {
90
288
  return [...rows];
91
289
  }
92
- const multiplier = sort.direction === "asc" ? 1 : -1;
93
- const rawKeys = rows.map((entry) => readCellValue(entry.row, column));
94
- const allNumeric = rawKeys.every((v) => typeof v === "number");
95
- if (allNumeric) {
96
- const numKeys = rawKeys;
97
- const indexed2 = rows.map((_, i) => i);
98
- indexed2.sort((a, b) => {
99
- const diff = numKeys[a] - numKeys[b];
100
- return diff !== 0 ? diff * multiplier : rows[a].sourceIndex - rows[b].sourceIndex;
101
- });
102
- return indexed2.map((i) => rows[i]);
103
- }
104
- const strKeys = rawKeys.map((v) => String(v ?? ""));
105
290
  const indexed = rows.map((_, i) => i);
106
291
  indexed.sort((a, b) => {
107
- const comparison = collator.compare(strKeys[a], strKeys[b]);
108
- return comparison !== 0 ? comparison * multiplier : rows[a].sourceIndex - rows[b].sourceIndex;
292
+ for (const key of keys) {
293
+ const cmp = key.kind === "num" ? key.keys[a] - key.keys[b] : collator.compare(key.keys[a], key.keys[b]);
294
+ if (cmp !== 0) {
295
+ return cmp * key.multiplier;
296
+ }
297
+ }
298
+ return rows[a].sourceIndex - rows[b].sourceIndex;
109
299
  });
110
300
  return indexed.map((i) => rows[i]);
111
301
  }
@@ -138,20 +328,40 @@ function applyAutosize(options, autosizeOptions) {
138
328
  });
139
329
  return { ...options, columns: nextColumns };
140
330
  }
331
+ function groupColumnsByPin(columns) {
332
+ const left = [];
333
+ const unpinned = [];
334
+ const right = [];
335
+ for (const column of columns) {
336
+ const region = column.pinned === "left" ? left : column.pinned === "right" ? right : unpinned;
337
+ if (column.id === ROW_SELECT_COLUMN_ID) {
338
+ region.unshift(column);
339
+ } else {
340
+ region.push(column);
341
+ }
342
+ }
343
+ return [...left, ...unpinned, ...right];
344
+ }
141
345
  function createGridCore(inputOptions) {
142
346
  const listeners = /* @__PURE__ */ new Set();
143
- let options = inputOptions.autosize ? applyAutosize(inputOptions, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : inputOptions;
144
- let originalColumns = inputOptions.columns.map((c) => ({ ...c }));
347
+ const groupedInput = {
348
+ ...inputOptions,
349
+ columns: groupColumnsByPin(inputOptions.columns)
350
+ };
351
+ let options = groupedInput.autosize ? applyAutosize(groupedInput, typeof groupedInput.autosize === "object" ? groupedInput.autosize : void 0) : groupedInput;
352
+ let originalColumns = groupColumnsByPin(inputOptions.columns).map((c) => ({ ...c }));
145
353
  let sourceRows = createSourceRows(options);
146
354
  const sourceRowIndex = new Map(sourceRows.map((entry) => [entry.id, entry]));
355
+ const pinnedWidthColumnIds = /* @__PURE__ */ new Set();
147
356
  let cachedSnapshot = null;
148
357
  let cachedVisibleRows = null;
149
358
  let cachedDerivedSort = null;
150
359
  let cachedDerivedFilters = null;
151
- let sort = { columnId: null, direction: null };
360
+ let sort = [];
152
361
  let filters = {};
153
362
  let selection = { ranges: [], anchor: null };
154
363
  let focus = { rowId: null, columnId: null };
364
+ let editing = null;
155
365
  let viewport = {
156
366
  scrollTop: 0,
157
367
  scrollLeft: 0,
@@ -170,22 +380,31 @@ function createGridCore(inputOptions) {
170
380
  },
171
381
  getSnapshot,
172
382
  setSort(columnId, direction) {
173
- if (sort.columnId === columnId && sort.direction === direction) {
383
+ const candidate = columnId && direction ? [{ columnId, direction }] : [];
384
+ const next = sanitizeSortEntries(candidate, options.columns);
385
+ if (sortsEqual(sort, next)) {
386
+ return;
387
+ }
388
+ sort = next;
389
+ emit();
390
+ },
391
+ replaceSort(entries) {
392
+ const next = sanitizeSortEntries(entries, options.columns);
393
+ if (sortsEqual(sort, next)) {
174
394
  return;
175
395
  }
176
- sort = { columnId, direction };
396
+ sort = next;
177
397
  emit();
178
398
  },
179
- setFilter(columnId, value) {
180
- const trimmed = value.trim();
181
- const currentValue = filters[columnId];
182
- if (trimmed) {
183
- if (currentValue === trimmed) {
399
+ setColumnFilter(columnId, filter) {
400
+ const current = filters[columnId];
401
+ if (filter && isFilterActive(filter)) {
402
+ if (current && columnFilterEqual(current, filter)) {
184
403
  return;
185
404
  }
186
- filters = { ...filters, [columnId]: trimmed };
405
+ filters = { ...filters, [columnId]: filter };
187
406
  } else {
188
- if (currentValue === void 0) {
407
+ if (current === void 0) {
189
408
  return;
190
409
  }
191
410
  const next = { ...filters };
@@ -203,10 +422,9 @@ function createGridCore(inputOptions) {
203
422
  },
204
423
  replaceFilters(nextFilters) {
205
424
  const normalized = {};
206
- for (const [columnId, value] of Object.entries(nextFilters)) {
207
- const trimmed = value.trim();
208
- if (trimmed) {
209
- normalized[columnId] = trimmed;
425
+ for (const [columnId, filter] of Object.entries(nextFilters)) {
426
+ if (filter && isFilterActive(filter)) {
427
+ normalized[columnId] = filter;
210
428
  }
211
429
  }
212
430
  if (filtersEqual(filters, normalized)) {
@@ -215,6 +433,25 @@ function createGridCore(inputOptions) {
215
433
  filters = normalized;
216
434
  emit();
217
435
  },
436
+ distinctColumnValues(columnId) {
437
+ const column = options.columns.find((c) => c.id === columnId);
438
+ if (!column) {
439
+ return [];
440
+ }
441
+ const seen = /* @__PURE__ */ new Set();
442
+ for (const entry of sourceRows) {
443
+ const raw = column.value ? column.value(entry.row) : entry.row[columnId];
444
+ if (raw === null || raw === void 0) {
445
+ continue;
446
+ }
447
+ const s = String(raw);
448
+ if (s.trim() === "") {
449
+ continue;
450
+ }
451
+ seen.add(s);
452
+ }
453
+ return [...seen].sort((a, b) => a.localeCompare(b));
454
+ },
218
455
  setSelection(next) {
219
456
  if (selectionsEqual(selection, next)) {
220
457
  return;
@@ -493,6 +730,7 @@ function createGridCore(inputOptions) {
493
730
  const nextColumns = options.columns.slice();
494
731
  nextColumns[idx] = { ...column, widthPx: clamped };
495
732
  options = { ...options, columns: nextColumns };
733
+ pinnedWidthColumnIds.add(columnId);
496
734
  emit();
497
735
  },
498
736
  moveColumn(columnId, toIndex) {
@@ -516,26 +754,73 @@ function createGridCore(inputOptions) {
516
754
  return;
517
755
  }
518
756
  nextColumns.splice(clampedTo, 0, moved);
519
- let boundary = synthAtZero ? 1 : 0;
520
- for (let i = synthAtZero ? 1 : 0; i < nextColumns.length; i += 1) {
757
+ let leftBoundary = minIndex;
758
+ for (let i = minIndex; i < nextColumns.length; i += 1) {
521
759
  if (i === clampedTo) {
522
760
  continue;
523
761
  }
524
762
  if (nextColumns[i]?.pinned === "left") {
525
- boundary = i + 1;
763
+ leftBoundary = i + 1;
526
764
  } else {
527
765
  break;
528
766
  }
529
767
  }
530
- const landsInPinned = clampedTo < boundary;
531
- const wasPinned = moved.pinned === "left";
532
- const nextPinned = landsInPinned ? "left" : void 0;
533
- if (nextPinned !== moved.pinned || wasPinned !== landsInPinned) {
768
+ let rightBoundary = nextColumns.length;
769
+ for (let i = nextColumns.length - 1; i >= minIndex; i -= 1) {
770
+ if (i === clampedTo) {
771
+ continue;
772
+ }
773
+ if (nextColumns[i]?.pinned === "right") {
774
+ rightBoundary = i;
775
+ } else {
776
+ break;
777
+ }
778
+ }
779
+ const nextPinned = clampedTo < leftBoundary ? "left" : clampedTo >= rightBoundary ? "right" : void 0;
780
+ if (nextPinned !== moved.pinned) {
534
781
  nextColumns[clampedTo] = { ...moved, pinned: nextPinned };
535
782
  }
536
783
  options = { ...options, columns: nextColumns };
537
784
  emit();
538
785
  },
786
+ setColumnOrder(ids) {
787
+ const byId = new Map(options.columns.map((c) => [c.id, c]));
788
+ const taken = /* @__PURE__ */ new Set();
789
+ const ordered = [];
790
+ for (const id of ids) {
791
+ if (id === ROW_SELECT_COLUMN_ID || taken.has(id)) {
792
+ continue;
793
+ }
794
+ const column = byId.get(id);
795
+ if (!column) {
796
+ continue;
797
+ }
798
+ taken.add(id);
799
+ ordered.push(column);
800
+ }
801
+ for (const column of options.columns) {
802
+ if (taken.has(column.id)) {
803
+ continue;
804
+ }
805
+ taken.add(column.id);
806
+ ordered.push(column);
807
+ }
808
+ const nextColumns = groupColumnsByPin(ordered);
809
+ let changed = nextColumns.length !== options.columns.length;
810
+ if (!changed) {
811
+ for (let i = 0; i < nextColumns.length; i += 1) {
812
+ if (nextColumns[i].id !== options.columns[i].id) {
813
+ changed = true;
814
+ break;
815
+ }
816
+ }
817
+ }
818
+ if (!changed) {
819
+ return;
820
+ }
821
+ options = { ...options, columns: nextColumns };
822
+ emit();
823
+ },
539
824
  setColumnPinned(columnId, pinned) {
540
825
  if (columnId === ROW_SELECT_COLUMN_ID) {
541
826
  return;
@@ -545,7 +830,7 @@ function createGridCore(inputOptions) {
545
830
  return;
546
831
  }
547
832
  const column = options.columns[idx];
548
- const nextPinnedValue = pinned === "left" ? "left" : void 0;
833
+ const nextPinnedValue = pinned === "left" || pinned === "right" ? pinned : void 0;
549
834
  if (column.pinned === nextPinnedValue) {
550
835
  return;
551
836
  }
@@ -557,7 +842,12 @@ function createGridCore(inputOptions) {
557
842
  while (boundary < nextColumns.length && nextColumns[boundary]?.pinned === "left") {
558
843
  boundary += 1;
559
844
  }
560
- const insertAt = boundary;
845
+ let rightBoundary = nextColumns.length;
846
+ while (rightBoundary > boundary && nextColumns[rightBoundary - 1]?.pinned === "right") {
847
+ rightBoundary -= 1;
848
+ }
849
+ const staysAtRightBoundary = nextPinnedValue === "right" || nextPinnedValue === void 0 && column.pinned === "right";
850
+ const insertAt = staysAtRightBoundary ? rightBoundary : boundary;
561
851
  const nextColumn = {
562
852
  ...column,
563
853
  pinned: nextPinnedValue
@@ -629,9 +919,13 @@ function createGridCore(inputOptions) {
629
919
  }
630
920
  return { ...newCol };
631
921
  });
632
- originalColumns = nextColumns.map((c) => ({ ...c }));
633
- options = { ...options, columns: merged };
634
- emit();
922
+ const grouped = groupColumnsByPin(merged);
923
+ const changed = !sameColumnLayout(options.columns, grouped);
924
+ originalColumns = groupColumnsByPin(nextColumns).map((c) => ({ ...c }));
925
+ options = { ...options, columns: grouped };
926
+ if (changed) {
927
+ emit();
928
+ }
635
929
  },
636
930
  applyTransaction(transaction) {
637
931
  if (!options.getRowId) {
@@ -682,6 +976,108 @@ function createGridCore(inputOptions) {
682
976
  }
683
977
  cachedVisibleRows = null;
684
978
  emit();
979
+ },
980
+ setRows(nextRows) {
981
+ options = { ...options, rows: nextRows };
982
+ sourceRows = createSourceRows(options);
983
+ sourceRowIndex.clear();
984
+ for (const entry of sourceRows) {
985
+ sourceRowIndex.set(entry.id, entry);
986
+ }
987
+ const hasRow = (id) => id != null && sourceRowIndex.has(id);
988
+ const keptRanges = selection.ranges.filter((range) => hasRow(range.startRowId) && hasRow(range.endRowId));
989
+ const anchorValid = !selection.anchor || hasRow(selection.anchor.rowId);
990
+ if (keptRanges.length !== selection.ranges.length || !anchorValid) {
991
+ selection = {
992
+ ranges: keptRanges,
993
+ anchor: anchorValid ? selection.anchor : null
994
+ };
995
+ }
996
+ if (focus.rowId !== null && !hasRow(focus.rowId)) {
997
+ focus = { rowId: null, columnId: null };
998
+ }
999
+ if (editing && !hasRow(editing.rowId)) {
1000
+ editing = null;
1001
+ }
1002
+ if (options.autosize) {
1003
+ const measured = applyAutosize({ ...options, columns: originalColumns }, typeof options.autosize === "object" ? options.autosize : void 0);
1004
+ const measuredById = new Map(measured.columns.map((column) => [column.id, column.widthPx]));
1005
+ options = {
1006
+ ...options,
1007
+ // Keyed by id, not index — `moveColumn` can leave the live order out
1008
+ // of step with `originalColumns`, and the live set may carry the
1009
+ // synthetic row-select column that never appears in props.
1010
+ columns: options.columns.map((column) => {
1011
+ if (pinnedWidthColumnIds.has(column.id)) {
1012
+ return column;
1013
+ }
1014
+ const width = measuredById.get(column.id);
1015
+ if (width === void 0 || width === column.widthPx) {
1016
+ return column;
1017
+ }
1018
+ return { ...column, widthPx: width };
1019
+ })
1020
+ };
1021
+ }
1022
+ cachedVisibleRows = null;
1023
+ emit();
1024
+ },
1025
+ beginEdit(addr, opts) {
1026
+ editing = {
1027
+ rowId: addr.rowId,
1028
+ columnId: addr.columnId,
1029
+ draft: opts?.draft,
1030
+ status: opts?.status ?? "editing"
1031
+ };
1032
+ emit();
1033
+ },
1034
+ setEditDraft(value) {
1035
+ if (!editing)
1036
+ return;
1037
+ editing = { ...editing, draft: value };
1038
+ emit();
1039
+ },
1040
+ markEditing() {
1041
+ if (!editing || editing.status !== "checking")
1042
+ return;
1043
+ editing = { ...editing, status: "editing", error: void 0 };
1044
+ emit();
1045
+ },
1046
+ markEditValidating() {
1047
+ if (!editing)
1048
+ return;
1049
+ editing = { ...editing, status: "validating", error: void 0 };
1050
+ emit();
1051
+ },
1052
+ markEditSaving() {
1053
+ if (!editing)
1054
+ return;
1055
+ editing = { ...editing, status: "saving", error: void 0 };
1056
+ emit();
1057
+ },
1058
+ markEditInvalid(message) {
1059
+ if (!editing)
1060
+ return;
1061
+ editing = { ...editing, status: "editing", error: message };
1062
+ emit();
1063
+ },
1064
+ markEditError(message) {
1065
+ if (!editing)
1066
+ return;
1067
+ editing = { ...editing, status: "error", error: message };
1068
+ emit();
1069
+ },
1070
+ commitEditSucceeded() {
1071
+ if (!editing)
1072
+ return;
1073
+ editing = null;
1074
+ emit();
1075
+ },
1076
+ cancelEdit() {
1077
+ if (!editing)
1078
+ return;
1079
+ editing = null;
1080
+ emit();
685
1081
  }
686
1082
  };
687
1083
  return store;
@@ -700,7 +1096,7 @@ function createGridCore(inputOptions) {
700
1096
  cachedDerivedFilters = filters;
701
1097
  cachedSnapshot = {
702
1098
  viewport,
703
- sort,
1099
+ sort: sort.map((entry) => ({ ...entry })),
704
1100
  filters: { ...filters },
705
1101
  selection: {
706
1102
  ranges: selection.ranges.map((r) => ({ ...r })),
@@ -712,7 +1108,8 @@ function createGridCore(inputOptions) {
712
1108
  visibleRange: {
713
1109
  start: 0,
714
1110
  end: visibleRows.length
715
- }
1111
+ },
1112
+ editing: editing ? { ...editing } : null
716
1113
  };
717
1114
  return cachedSnapshot;
718
1115
  }
@@ -726,6 +1123,15 @@ function createGridCore(inputOptions) {
726
1123
  function clamp(value, min, max) {
727
1124
  return Math.max(min, Math.min(max, value));
728
1125
  }
1126
+ function sameColumnLayout(a, b) {
1127
+ if (a.length !== b.length) {
1128
+ return false;
1129
+ }
1130
+ return a.every((left, index) => {
1131
+ const right = b[index];
1132
+ 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;
1133
+ });
1134
+ }
729
1135
  function computePageStep(viewport, visibleRows) {
730
1136
  if (viewport.height <= 0 || visibleRows.length === 0) {
731
1137
  return 1;
@@ -755,6 +1161,36 @@ function selectionsEqual(a, b) {
755
1161
  }
756
1162
  return a.anchor.rowId === b.anchor.rowId && a.anchor.columnId === b.anchor.columnId;
757
1163
  }
1164
+ function sanitizeSortEntries(entries, columns) {
1165
+ return entries.filter((entry) => {
1166
+ const column = columns.find((c) => c.id === entry.columnId);
1167
+ return column !== void 0 && column.sortable !== false;
1168
+ });
1169
+ }
1170
+ function sortsEqual(a, b) {
1171
+ if (a.length !== b.length) {
1172
+ return false;
1173
+ }
1174
+ for (let i = 0; i < a.length; i += 1) {
1175
+ const ae = a[i];
1176
+ const be = b[i];
1177
+ if (ae.columnId !== be.columnId || ae.direction !== be.direction) {
1178
+ return false;
1179
+ }
1180
+ }
1181
+ return true;
1182
+ }
1183
+ function columnFilterEqual(a, b) {
1184
+ if (a.operator !== b.operator) {
1185
+ return false;
1186
+ }
1187
+ const av = a.value;
1188
+ const bv = b.value;
1189
+ if (Array.isArray(av) && Array.isArray(bv)) {
1190
+ return av.length === bv.length && av.every((v, i) => v === bv[i]);
1191
+ }
1192
+ return av === bv;
1193
+ }
758
1194
  function filtersEqual(a, b) {
759
1195
  const aKeys = Object.keys(a);
760
1196
  const bKeys = Object.keys(b);
@@ -762,7 +1198,9 @@ function filtersEqual(a, b) {
762
1198
  return false;
763
1199
  }
764
1200
  for (const key of aKeys) {
765
- if (a[key] !== b[key]) {
1201
+ const av = a[key];
1202
+ const bv = b[key];
1203
+ if (!av || !bv || !columnFilterEqual(av, bv)) {
766
1204
  return false;
767
1205
  }
768
1206
  }
@@ -780,9 +1218,11 @@ function createGrid(options) {
780
1218
  subscribe: engine.subscribe,
781
1219
  getSnapshot: engine.getSnapshot,
782
1220
  setSort: engine.setSort,
783
- setFilter: engine.setFilter,
1221
+ replaceSort: engine.replaceSort,
1222
+ setColumnFilter: engine.setColumnFilter,
784
1223
  clearFilters: engine.clearFilters,
785
1224
  replaceFilters: engine.replaceFilters,
1225
+ distinctColumnValues: engine.distinctColumnValues,
786
1226
  setSelection: engine.setSelection,
787
1227
  selectAll: engine.selectAll,
788
1228
  clearSelection: engine.clearSelection,
@@ -796,11 +1236,22 @@ function createGrid(options) {
796
1236
  autosizeColumns: engine.autosizeColumns,
797
1237
  setColumnWidth: engine.setColumnWidth,
798
1238
  moveColumn: engine.moveColumn,
1239
+ setColumnOrder: engine.setColumnOrder,
799
1240
  setColumnPinned: engine.setColumnPinned,
800
1241
  autosizeColumn: engine.autosizeColumn,
801
1242
  resetColumnLayout: engine.resetColumnLayout,
802
1243
  mergeColumnsFromProps: engine.mergeColumnsFromProps,
803
- applyTransaction: engine.applyTransaction
1244
+ applyTransaction: engine.applyTransaction,
1245
+ setRows: engine.setRows,
1246
+ beginEdit: engine.beginEdit,
1247
+ setEditDraft: engine.setEditDraft,
1248
+ markEditing: engine.markEditing,
1249
+ markEditValidating: engine.markEditValidating,
1250
+ markEditSaving: engine.markEditSaving,
1251
+ markEditInvalid: engine.markEditInvalid,
1252
+ markEditError: engine.markEditError,
1253
+ commitEditSucceeded: engine.commitEditSucceeded,
1254
+ cancelEdit: engine.cancelEdit
804
1255
  };
805
1256
  }
806
1257