@pretable/core 0.0.1 → 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/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/index.cjs +954 -83
- package/dist/index.d.cts +291 -51
- package/dist/index.d.ts +291 -51
- package/dist/index.mjs +954 -83
- package/package.json +14 -1
package/dist/index.mjs
CHANGED
|
@@ -19,7 +19,7 @@ function autosizeColumns(input) {
|
|
|
19
19
|
maxContentWidth = Math.max(maxContentWidth, headerWidth);
|
|
20
20
|
}
|
|
21
21
|
for (const row of input.rows) {
|
|
22
|
-
const rawValue = column.
|
|
22
|
+
const rawValue = column.value ? column.value(row) : row[column.id];
|
|
23
23
|
const text = String(rawValue ?? "");
|
|
24
24
|
if (text.length === 0) {
|
|
25
25
|
continue;
|
|
@@ -33,6 +33,197 @@ function autosizeColumns(input) {
|
|
|
33
33
|
return { widths };
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// ../grid-core/dist/evaluate-filter.js
|
|
37
|
+
var NO_OPERAND = /* @__PURE__ */ new Set([
|
|
38
|
+
"isEmpty",
|
|
39
|
+
"isNotEmpty"
|
|
40
|
+
]);
|
|
41
|
+
function isFilterActive(filter) {
|
|
42
|
+
const { operator, value } = filter;
|
|
43
|
+
if (NO_OPERAND.has(operator))
|
|
44
|
+
return true;
|
|
45
|
+
if (value === null || value === void 0)
|
|
46
|
+
return false;
|
|
47
|
+
if (typeof value === "string")
|
|
48
|
+
return value.trim() !== "";
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.length > 0;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function isEmptyCell(cell) {
|
|
54
|
+
if (cell === null || cell === void 0)
|
|
55
|
+
return true;
|
|
56
|
+
if (typeof cell === "number")
|
|
57
|
+
return Number.isNaN(cell);
|
|
58
|
+
return String(cell).trim() === "";
|
|
59
|
+
}
|
|
60
|
+
function toBooleanCell(value) {
|
|
61
|
+
if (typeof value === "boolean")
|
|
62
|
+
return value;
|
|
63
|
+
if (value === "true" || value === 1 || value === "1")
|
|
64
|
+
return true;
|
|
65
|
+
if (value === "false" || value === 0 || value === "0")
|
|
66
|
+
return false;
|
|
67
|
+
return Boolean(value);
|
|
68
|
+
}
|
|
69
|
+
var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
70
|
+
var ISO_DATETIME_RE = /^(\d{4}-\d{2}-\d{2})[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/i;
|
|
71
|
+
var MAX_ISO_YEAR = 9999;
|
|
72
|
+
var GREGORIAN_400Y_MS = 146097 * 864e5;
|
|
73
|
+
function utcMs(year, month, day) {
|
|
74
|
+
return year >= 0 && year < 100 ? Date.UTC(year + 400, month, day) - GREGORIAN_400Y_MS : Date.UTC(year, month, day);
|
|
75
|
+
}
|
|
76
|
+
function utcDayOf(ms) {
|
|
77
|
+
const d = new Date(ms);
|
|
78
|
+
if (Number.isNaN(d.getTime()))
|
|
79
|
+
return Number.NaN;
|
|
80
|
+
const year = d.getUTCFullYear();
|
|
81
|
+
if (year < 0 || year > MAX_ISO_YEAR)
|
|
82
|
+
return Number.NaN;
|
|
83
|
+
d.setUTCHours(0, 0, 0, 0);
|
|
84
|
+
return d.getTime();
|
|
85
|
+
}
|
|
86
|
+
function isoDayMs(iso) {
|
|
87
|
+
if (!ISO_DATE_RE.test(iso))
|
|
88
|
+
return Number.NaN;
|
|
89
|
+
const [y, m, d] = iso.split("-").map(Number);
|
|
90
|
+
const ms = utcMs(y, m - 1, d);
|
|
91
|
+
const back = new Date(ms);
|
|
92
|
+
return back.getUTCFullYear() === y && back.getUTCMonth() === m - 1 && back.getUTCDate() === d ? ms : Number.NaN;
|
|
93
|
+
}
|
|
94
|
+
function toDayMs(input) {
|
|
95
|
+
if (input instanceof Date)
|
|
96
|
+
return utcDayOf(input.getTime());
|
|
97
|
+
if (typeof input === "number")
|
|
98
|
+
return utcDayOf(input);
|
|
99
|
+
if (typeof input !== "string")
|
|
100
|
+
return Number.NaN;
|
|
101
|
+
const trimmed = input.trim();
|
|
102
|
+
const dateOnly = isoDayMs(trimmed);
|
|
103
|
+
if (!Number.isNaN(dateOnly))
|
|
104
|
+
return dateOnly;
|
|
105
|
+
const parts = ISO_DATETIME_RE.exec(trimmed);
|
|
106
|
+
if (!parts)
|
|
107
|
+
return Number.NaN;
|
|
108
|
+
const day = isoDayMs(parts[1]);
|
|
109
|
+
if (Number.isNaN(day))
|
|
110
|
+
return Number.NaN;
|
|
111
|
+
return parts[2] ? utcDayOf(Date.parse(trimmed.replace(" ", "T"))) : day;
|
|
112
|
+
}
|
|
113
|
+
function evaluateFilter(cell, type, operator, value) {
|
|
114
|
+
if (operator === "isEmpty")
|
|
115
|
+
return isEmptyCell(cell);
|
|
116
|
+
if (operator === "isNotEmpty")
|
|
117
|
+
return !isEmptyCell(cell);
|
|
118
|
+
switch (type) {
|
|
119
|
+
case "number": {
|
|
120
|
+
const n = typeof cell === "number" ? cell : Number(cell);
|
|
121
|
+
if (Number.isNaN(n))
|
|
122
|
+
return false;
|
|
123
|
+
switch (operator) {
|
|
124
|
+
case "equals":
|
|
125
|
+
return n === Number(value);
|
|
126
|
+
case "notEquals":
|
|
127
|
+
return n !== Number(value);
|
|
128
|
+
case "gt":
|
|
129
|
+
return n > Number(value);
|
|
130
|
+
case "gte":
|
|
131
|
+
return n >= Number(value);
|
|
132
|
+
case "lt":
|
|
133
|
+
return n < Number(value);
|
|
134
|
+
case "lte":
|
|
135
|
+
return n <= Number(value);
|
|
136
|
+
case "between": {
|
|
137
|
+
if (!Array.isArray(value))
|
|
138
|
+
return false;
|
|
139
|
+
const a = Number(value[0]);
|
|
140
|
+
const b = Number(value[1]);
|
|
141
|
+
const lo = Math.min(a, b);
|
|
142
|
+
const hi = Math.max(a, b);
|
|
143
|
+
return n >= lo && n <= hi;
|
|
144
|
+
}
|
|
145
|
+
default:
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
case "date": {
|
|
150
|
+
const c = toDayMs(cell);
|
|
151
|
+
if (Number.isNaN(c))
|
|
152
|
+
return false;
|
|
153
|
+
switch (operator) {
|
|
154
|
+
case "on":
|
|
155
|
+
return c === toDayMs(value);
|
|
156
|
+
case "before":
|
|
157
|
+
return c < toDayMs(value);
|
|
158
|
+
case "after":
|
|
159
|
+
return c > toDayMs(value);
|
|
160
|
+
case "dateBetween": {
|
|
161
|
+
if (!Array.isArray(value))
|
|
162
|
+
return false;
|
|
163
|
+
const a = toDayMs(value[0]);
|
|
164
|
+
const b = toDayMs(value[1]);
|
|
165
|
+
if (Number.isNaN(a) || Number.isNaN(b))
|
|
166
|
+
return false;
|
|
167
|
+
const lo = Math.min(a, b);
|
|
168
|
+
const hi = Math.max(a, b);
|
|
169
|
+
return c >= lo && c <= hi;
|
|
170
|
+
}
|
|
171
|
+
default:
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
case "boolean": {
|
|
176
|
+
const c = String(toBooleanCell(cell));
|
|
177
|
+
const set = Array.isArray(value) ? value.map(String) : [];
|
|
178
|
+
if (set.length === 0)
|
|
179
|
+
return true;
|
|
180
|
+
switch (operator) {
|
|
181
|
+
case "isAnyOf":
|
|
182
|
+
return set.includes(c);
|
|
183
|
+
case "isNoneOf":
|
|
184
|
+
return !set.includes(c);
|
|
185
|
+
default:
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
case "enum": {
|
|
190
|
+
const c = String(cell);
|
|
191
|
+
const set = Array.isArray(value) ? value.map(String) : [];
|
|
192
|
+
if (set.length === 0)
|
|
193
|
+
return true;
|
|
194
|
+
switch (operator) {
|
|
195
|
+
case "isAnyOf":
|
|
196
|
+
return set.includes(c);
|
|
197
|
+
case "isNoneOf":
|
|
198
|
+
return !set.includes(c);
|
|
199
|
+
default:
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
case "text":
|
|
204
|
+
default: {
|
|
205
|
+
const hay = String(cell ?? "").toLowerCase();
|
|
206
|
+
const needle = String(value ?? "").toLowerCase();
|
|
207
|
+
switch (operator) {
|
|
208
|
+
case "contains":
|
|
209
|
+
return hay.includes(needle);
|
|
210
|
+
case "notContains":
|
|
211
|
+
return !hay.includes(needle);
|
|
212
|
+
case "equals":
|
|
213
|
+
return hay === needle;
|
|
214
|
+
case "notEquals":
|
|
215
|
+
return hay !== needle;
|
|
216
|
+
case "startsWith":
|
|
217
|
+
return hay.startsWith(needle);
|
|
218
|
+
case "endsWith":
|
|
219
|
+
return hay.endsWith(needle);
|
|
220
|
+
default:
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
36
227
|
// ../grid-core/dist/derived-rows.js
|
|
37
228
|
function createSourceRows(options) {
|
|
38
229
|
return options.rows.map((row, index) => ({
|
|
@@ -54,22 +245,20 @@ function deriveVisibleRows(input) {
|
|
|
54
245
|
function resolveFilters(columns, filters) {
|
|
55
246
|
const columnMap = new Map(columns.map((c) => [c.id, c]));
|
|
56
247
|
const resolved = [];
|
|
57
|
-
for (const [columnId,
|
|
58
|
-
if (!
|
|
248
|
+
for (const [columnId, filter] of Object.entries(filters)) {
|
|
249
|
+
if (!filter || !isFilterActive(filter))
|
|
59
250
|
continue;
|
|
60
|
-
}
|
|
61
251
|
const column = columnMap.get(columnId);
|
|
62
|
-
if (!column)
|
|
252
|
+
if (!column || column.filterable === false)
|
|
63
253
|
continue;
|
|
64
|
-
}
|
|
65
|
-
resolved.push({ column, needle: rawNeedle.toLowerCase() });
|
|
254
|
+
resolved.push({ column, filter });
|
|
66
255
|
}
|
|
67
256
|
return resolved;
|
|
68
257
|
}
|
|
69
258
|
function matchesFilters(row, resolvedFilters) {
|
|
70
|
-
for (const { column,
|
|
71
|
-
const
|
|
72
|
-
if (!
|
|
259
|
+
for (const { column, filter } of resolvedFilters) {
|
|
260
|
+
const cell = readCellValue(row, column);
|
|
261
|
+
if (!evaluateFilter(cell, column.type ?? "text", filter.operator, filter.value)) {
|
|
73
262
|
return false;
|
|
74
263
|
}
|
|
75
264
|
}
|
|
@@ -80,38 +269,45 @@ var collator = new Intl.Collator(void 0, {
|
|
|
80
269
|
sensitivity: "base"
|
|
81
270
|
});
|
|
82
271
|
function sortRows(rows, columns, sort) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
272
|
+
const keys = sort.map((entry) => {
|
|
273
|
+
const column = columns.find((c) => c.id === entry.columnId);
|
|
274
|
+
if (!column)
|
|
275
|
+
return null;
|
|
276
|
+
const rawKeys = rows.map((r) => readCellValue(r.row, column));
|
|
277
|
+
const allNumeric = rawKeys.every((v) => typeof v === "number");
|
|
278
|
+
const multiplier = entry.direction === "asc" ? 1 : -1;
|
|
279
|
+
return allNumeric ? { kind: "num", keys: rawKeys, multiplier } : {
|
|
280
|
+
kind: "str",
|
|
281
|
+
keys: rawKeys.map((v) => String(v ?? "")),
|
|
282
|
+
multiplier
|
|
283
|
+
};
|
|
284
|
+
}).filter((k) => k !== null);
|
|
285
|
+
if (keys.length === 0) {
|
|
88
286
|
return [...rows];
|
|
89
287
|
}
|
|
90
|
-
const multiplier = sort.direction === "asc" ? 1 : -1;
|
|
91
|
-
const rawKeys = rows.map((entry) => readCellValue(entry.row, column));
|
|
92
|
-
const allNumeric = rawKeys.every((v) => typeof v === "number");
|
|
93
|
-
if (allNumeric) {
|
|
94
|
-
const numKeys = rawKeys;
|
|
95
|
-
const indexed2 = rows.map((_, i) => i);
|
|
96
|
-
indexed2.sort((a, b) => {
|
|
97
|
-
const diff = numKeys[a] - numKeys[b];
|
|
98
|
-
return diff !== 0 ? diff * multiplier : rows[a].sourceIndex - rows[b].sourceIndex;
|
|
99
|
-
});
|
|
100
|
-
return indexed2.map((i) => rows[i]);
|
|
101
|
-
}
|
|
102
|
-
const strKeys = rawKeys.map((v) => String(v ?? ""));
|
|
103
288
|
const indexed = rows.map((_, i) => i);
|
|
104
289
|
indexed.sort((a, b) => {
|
|
105
|
-
const
|
|
106
|
-
|
|
290
|
+
for (const key of keys) {
|
|
291
|
+
const cmp = key.kind === "num" ? key.keys[a] - key.keys[b] : collator.compare(key.keys[a], key.keys[b]);
|
|
292
|
+
if (cmp !== 0) {
|
|
293
|
+
return cmp * key.multiplier;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return rows[a].sourceIndex - rows[b].sourceIndex;
|
|
107
297
|
});
|
|
108
298
|
return indexed.map((i) => rows[i]);
|
|
109
299
|
}
|
|
110
300
|
function readCellValue(row, column) {
|
|
111
|
-
return column.
|
|
301
|
+
return column.value ? column.value(row) : row[column.id];
|
|
112
302
|
}
|
|
113
303
|
|
|
114
304
|
// ../grid-core/dist/create-grid-core.js
|
|
305
|
+
var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
|
|
306
|
+
function clampColumnWidth(width, column) {
|
|
307
|
+
const min = column.minWidthPx ?? 40;
|
|
308
|
+
const max = column.maxWidthPx ?? Infinity;
|
|
309
|
+
return Math.max(min, Math.min(max, width));
|
|
310
|
+
}
|
|
115
311
|
function applyAutosize(options, autosizeOptions) {
|
|
116
312
|
const result = autosizeColumns({
|
|
117
313
|
columns: options.columns,
|
|
@@ -130,19 +326,40 @@ function applyAutosize(options, autosizeOptions) {
|
|
|
130
326
|
});
|
|
131
327
|
return { ...options, columns: nextColumns };
|
|
132
328
|
}
|
|
329
|
+
function groupColumnsByPin(columns) {
|
|
330
|
+
const left = [];
|
|
331
|
+
const unpinned = [];
|
|
332
|
+
const right = [];
|
|
333
|
+
for (const column of columns) {
|
|
334
|
+
const region = column.pinned === "left" ? left : column.pinned === "right" ? right : unpinned;
|
|
335
|
+
if (column.id === ROW_SELECT_COLUMN_ID) {
|
|
336
|
+
region.unshift(column);
|
|
337
|
+
} else {
|
|
338
|
+
region.push(column);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return [...left, ...unpinned, ...right];
|
|
342
|
+
}
|
|
133
343
|
function createGridCore(inputOptions) {
|
|
134
344
|
const listeners = /* @__PURE__ */ new Set();
|
|
135
|
-
|
|
345
|
+
const groupedInput = {
|
|
346
|
+
...inputOptions,
|
|
347
|
+
columns: groupColumnsByPin(inputOptions.columns)
|
|
348
|
+
};
|
|
349
|
+
let options = groupedInput.autosize ? applyAutosize(groupedInput, typeof groupedInput.autosize === "object" ? groupedInput.autosize : void 0) : groupedInput;
|
|
350
|
+
let originalColumns = groupColumnsByPin(inputOptions.columns).map((c) => ({ ...c }));
|
|
136
351
|
let sourceRows = createSourceRows(options);
|
|
137
352
|
const sourceRowIndex = new Map(sourceRows.map((entry) => [entry.id, entry]));
|
|
353
|
+
const pinnedWidthColumnIds = /* @__PURE__ */ new Set();
|
|
138
354
|
let cachedSnapshot = null;
|
|
139
355
|
let cachedVisibleRows = null;
|
|
140
356
|
let cachedDerivedSort = null;
|
|
141
357
|
let cachedDerivedFilters = null;
|
|
142
|
-
let sort =
|
|
358
|
+
let sort = [];
|
|
143
359
|
let filters = {};
|
|
144
|
-
let selection = {
|
|
360
|
+
let selection = { ranges: [], anchor: null };
|
|
145
361
|
let focus = { rowId: null, columnId: null };
|
|
362
|
+
let editing = null;
|
|
146
363
|
let viewport = {
|
|
147
364
|
scrollTop: 0,
|
|
148
365
|
scrollLeft: 0,
|
|
@@ -161,22 +378,31 @@ function createGridCore(inputOptions) {
|
|
|
161
378
|
},
|
|
162
379
|
getSnapshot,
|
|
163
380
|
setSort(columnId, direction) {
|
|
164
|
-
|
|
381
|
+
const candidate = columnId && direction ? [{ columnId, direction }] : [];
|
|
382
|
+
const next = sanitizeSortEntries(candidate, options.columns);
|
|
383
|
+
if (sortsEqual(sort, next)) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
sort = next;
|
|
387
|
+
emit();
|
|
388
|
+
},
|
|
389
|
+
replaceSort(entries) {
|
|
390
|
+
const next = sanitizeSortEntries(entries, options.columns);
|
|
391
|
+
if (sortsEqual(sort, next)) {
|
|
165
392
|
return;
|
|
166
393
|
}
|
|
167
|
-
sort =
|
|
394
|
+
sort = next;
|
|
168
395
|
emit();
|
|
169
396
|
},
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (currentValue === trimmed) {
|
|
397
|
+
setColumnFilter(columnId, filter) {
|
|
398
|
+
const current = filters[columnId];
|
|
399
|
+
if (filter && isFilterActive(filter)) {
|
|
400
|
+
if (current && columnFilterEqual(current, filter)) {
|
|
175
401
|
return;
|
|
176
402
|
}
|
|
177
|
-
filters = { ...filters, [columnId]:
|
|
403
|
+
filters = { ...filters, [columnId]: filter };
|
|
178
404
|
} else {
|
|
179
|
-
if (
|
|
405
|
+
if (current === void 0) {
|
|
180
406
|
return;
|
|
181
407
|
}
|
|
182
408
|
const next = { ...filters };
|
|
@@ -194,10 +420,9 @@ function createGridCore(inputOptions) {
|
|
|
194
420
|
},
|
|
195
421
|
replaceFilters(nextFilters) {
|
|
196
422
|
const normalized = {};
|
|
197
|
-
for (const [columnId,
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
normalized[columnId] = trimmed;
|
|
423
|
+
for (const [columnId, filter] of Object.entries(nextFilters)) {
|
|
424
|
+
if (filter && isFilterActive(filter)) {
|
|
425
|
+
normalized[columnId] = filter;
|
|
201
426
|
}
|
|
202
427
|
}
|
|
203
428
|
if (filtersEqual(filters, normalized)) {
|
|
@@ -206,35 +431,270 @@ function createGridCore(inputOptions) {
|
|
|
206
431
|
filters = normalized;
|
|
207
432
|
emit();
|
|
208
433
|
},
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
if (
|
|
434
|
+
distinctColumnValues(columnId) {
|
|
435
|
+
const column = options.columns.find((c) => c.id === columnId);
|
|
436
|
+
if (!column) {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
const seen = /* @__PURE__ */ new Set();
|
|
440
|
+
for (const entry of sourceRows) {
|
|
441
|
+
const raw = column.value ? column.value(entry.row) : entry.row[columnId];
|
|
442
|
+
if (raw === null || raw === void 0) {
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
const s = String(raw);
|
|
446
|
+
if (s.trim() === "") {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
seen.add(s);
|
|
450
|
+
}
|
|
451
|
+
return [...seen].sort((a, b) => a.localeCompare(b));
|
|
452
|
+
},
|
|
453
|
+
setSelection(next) {
|
|
454
|
+
if (selectionsEqual(selection, next)) {
|
|
212
455
|
return;
|
|
213
456
|
}
|
|
214
|
-
selection =
|
|
457
|
+
selection = {
|
|
458
|
+
ranges: next.ranges.map((r) => ({ ...r })),
|
|
459
|
+
anchor: next.anchor ? { ...next.anchor } : null
|
|
460
|
+
};
|
|
215
461
|
emit();
|
|
216
462
|
},
|
|
217
|
-
|
|
218
|
-
|
|
463
|
+
selectAll() {
|
|
464
|
+
const snapshot = getSnapshot();
|
|
465
|
+
const firstRow = snapshot.visibleRows[0];
|
|
466
|
+
const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
|
|
467
|
+
const firstColumn = options.columns[0];
|
|
468
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
469
|
+
if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
|
|
219
470
|
return;
|
|
220
471
|
}
|
|
221
|
-
|
|
472
|
+
const range = {
|
|
473
|
+
startRowId: firstRow.id,
|
|
474
|
+
endRowId: lastRow.id,
|
|
475
|
+
startColumnId: firstColumn.id,
|
|
476
|
+
endColumnId: lastColumn.id
|
|
477
|
+
};
|
|
478
|
+
const anchor = {
|
|
479
|
+
rowId: firstRow.id,
|
|
480
|
+
columnId: firstColumn.id
|
|
481
|
+
};
|
|
482
|
+
const next = { ranges: [range], anchor };
|
|
483
|
+
if (selectionsEqual(selection, next)) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
selection = next;
|
|
222
487
|
emit();
|
|
223
488
|
},
|
|
224
|
-
|
|
489
|
+
clearSelection() {
|
|
490
|
+
const focusAddr = focus.rowId && focus.columnId ? { rowId: focus.rowId, columnId: focus.columnId } : null;
|
|
491
|
+
const next = focusAddr ? {
|
|
492
|
+
ranges: [
|
|
493
|
+
{
|
|
494
|
+
startRowId: focusAddr.rowId,
|
|
495
|
+
endRowId: focusAddr.rowId,
|
|
496
|
+
startColumnId: focusAddr.columnId,
|
|
497
|
+
endColumnId: focusAddr.columnId
|
|
498
|
+
}
|
|
499
|
+
],
|
|
500
|
+
anchor: focusAddr
|
|
501
|
+
} : { ranges: [], anchor: null };
|
|
502
|
+
if (selectionsEqual(selection, next)) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
selection = next;
|
|
506
|
+
emit();
|
|
507
|
+
},
|
|
508
|
+
addRange(range) {
|
|
509
|
+
selection = {
|
|
510
|
+
ranges: [...selection.ranges, { ...range }],
|
|
511
|
+
anchor: { rowId: range.startRowId, columnId: range.startColumnId }
|
|
512
|
+
};
|
|
513
|
+
emit();
|
|
514
|
+
},
|
|
515
|
+
extendRangeFromAnchor(addr) {
|
|
516
|
+
if (!selection.anchor) {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const newActive = {
|
|
520
|
+
startRowId: selection.anchor.rowId,
|
|
521
|
+
endRowId: addr.rowId,
|
|
522
|
+
startColumnId: selection.anchor.columnId,
|
|
523
|
+
endColumnId: addr.columnId
|
|
524
|
+
};
|
|
525
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
526
|
+
selection = { ranges, anchor: selection.anchor };
|
|
527
|
+
emit();
|
|
528
|
+
},
|
|
529
|
+
toggleRowSelection(rowId) {
|
|
530
|
+
const firstColumn = options.columns[0];
|
|
531
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
532
|
+
if (!firstColumn || !lastColumn) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
const fullRowRange = {
|
|
536
|
+
startRowId: rowId,
|
|
537
|
+
endRowId: rowId,
|
|
538
|
+
startColumnId: firstColumn.id,
|
|
539
|
+
endColumnId: lastColumn.id
|
|
540
|
+
};
|
|
541
|
+
const matchIndex = selection.ranges.findIndex((r) => isFullRowRange(r, rowId, firstColumn.id, lastColumn.id));
|
|
542
|
+
if (matchIndex >= 0) {
|
|
543
|
+
const ranges = selection.ranges.filter((_, i) => i !== matchIndex);
|
|
544
|
+
selection = { ranges, anchor: selection.anchor };
|
|
545
|
+
} else {
|
|
546
|
+
selection = {
|
|
547
|
+
ranges: [...selection.ranges, fullRowRange],
|
|
548
|
+
anchor: { rowId, columnId: firstColumn.id }
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
emit();
|
|
552
|
+
},
|
|
553
|
+
setSelectAllVisible(checked) {
|
|
554
|
+
const snapshot = getSnapshot();
|
|
555
|
+
const firstColumn = options.columns[0];
|
|
556
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
557
|
+
if (!firstColumn || !lastColumn) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
|
|
561
|
+
const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
|
|
562
|
+
let next;
|
|
563
|
+
if (checked) {
|
|
564
|
+
const newRanges = snapshot.visibleRows.map((row) => ({
|
|
565
|
+
startRowId: row.id,
|
|
566
|
+
endRowId: row.id,
|
|
567
|
+
startColumnId: firstColumn.id,
|
|
568
|
+
endColumnId: lastColumn.id
|
|
569
|
+
}));
|
|
570
|
+
next = {
|
|
571
|
+
ranges: [...nonRowRanges, ...newRanges],
|
|
572
|
+
anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
|
|
573
|
+
};
|
|
574
|
+
} else {
|
|
575
|
+
next = { ranges: nonRowRanges, anchor: selection.anchor };
|
|
576
|
+
}
|
|
577
|
+
if (selectionsEqual(selection, next)) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
selection = next;
|
|
581
|
+
emit();
|
|
582
|
+
},
|
|
583
|
+
setFocus(addr) {
|
|
584
|
+
const nextRowId = addr?.rowId ?? null;
|
|
585
|
+
const nextColumnId = addr?.columnId ?? null;
|
|
586
|
+
if (focus.rowId === nextRowId && focus.columnId === nextColumnId) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
focus = { rowId: nextRowId, columnId: nextColumnId };
|
|
590
|
+
emit();
|
|
591
|
+
},
|
|
592
|
+
moveFocus(direction, moveOptions = {}) {
|
|
225
593
|
const snapshot = getSnapshot();
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
594
|
+
const visibleRows = snapshot.visibleRows;
|
|
595
|
+
const columnList = options.columns;
|
|
596
|
+
if (visibleRows.length === 0 || columnList.length === 0) {
|
|
597
|
+
focus = { rowId: null, columnId: null };
|
|
229
598
|
emit();
|
|
230
599
|
return;
|
|
231
600
|
}
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
|
|
601
|
+
const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
|
|
602
|
+
const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
|
|
603
|
+
const hasRowFocus = currentRowIndex !== -1;
|
|
604
|
+
const hasColumnFocus = currentColumnIndex !== -1;
|
|
605
|
+
const baseRowIndex = hasRowFocus ? currentRowIndex : 0;
|
|
606
|
+
const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
|
|
607
|
+
let nextRowIndex = baseRowIndex;
|
|
608
|
+
let nextColumnIndex = baseColumnIndex;
|
|
609
|
+
const pageStep = computePageStep(viewport, visibleRows);
|
|
610
|
+
switch (direction) {
|
|
611
|
+
case "up":
|
|
612
|
+
if (moveOptions.jumpToEdge) {
|
|
613
|
+
nextRowIndex = 0;
|
|
614
|
+
} else if (!hasRowFocus) {
|
|
615
|
+
nextRowIndex = visibleRows.length - 1;
|
|
616
|
+
} else if (moveOptions.byPage) {
|
|
617
|
+
nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
|
|
618
|
+
} else {
|
|
619
|
+
nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
|
|
620
|
+
}
|
|
621
|
+
break;
|
|
622
|
+
case "down":
|
|
623
|
+
if (moveOptions.jumpToEdge) {
|
|
624
|
+
nextRowIndex = visibleRows.length - 1;
|
|
625
|
+
} else if (!hasRowFocus) {
|
|
626
|
+
nextRowIndex = 0;
|
|
627
|
+
} else if (moveOptions.byPage) {
|
|
628
|
+
nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
|
|
629
|
+
} else {
|
|
630
|
+
nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
|
|
631
|
+
}
|
|
632
|
+
break;
|
|
633
|
+
case "left":
|
|
634
|
+
if (moveOptions.jumpToEdge) {
|
|
635
|
+
nextColumnIndex = 0;
|
|
636
|
+
} else if (!hasColumnFocus) {
|
|
637
|
+
nextColumnIndex = columnList.length - 1;
|
|
638
|
+
} else {
|
|
639
|
+
nextColumnIndex = clamp(baseColumnIndex - 1, 0, columnList.length - 1);
|
|
640
|
+
}
|
|
641
|
+
break;
|
|
642
|
+
case "right":
|
|
643
|
+
if (moveOptions.jumpToEdge) {
|
|
644
|
+
nextColumnIndex = columnList.length - 1;
|
|
645
|
+
} else if (!hasColumnFocus) {
|
|
646
|
+
nextColumnIndex = 0;
|
|
647
|
+
} else {
|
|
648
|
+
nextColumnIndex = clamp(baseColumnIndex + 1, 0, columnList.length - 1);
|
|
649
|
+
}
|
|
650
|
+
break;
|
|
651
|
+
}
|
|
652
|
+
const nextRow = visibleRows[nextRowIndex];
|
|
653
|
+
const nextColumn = columnList[nextColumnIndex];
|
|
654
|
+
if (!nextRow || !nextColumn) {
|
|
235
655
|
return;
|
|
236
656
|
}
|
|
237
|
-
|
|
657
|
+
const nextAddr = {
|
|
658
|
+
rowId: nextRow.id,
|
|
659
|
+
columnId: nextColumn.id
|
|
660
|
+
};
|
|
661
|
+
focus = nextAddr;
|
|
662
|
+
if (moveOptions.extend) {
|
|
663
|
+
if (!selection.anchor) {
|
|
664
|
+
selection = {
|
|
665
|
+
ranges: [
|
|
666
|
+
{
|
|
667
|
+
startRowId: nextAddr.rowId,
|
|
668
|
+
endRowId: nextAddr.rowId,
|
|
669
|
+
startColumnId: nextAddr.columnId,
|
|
670
|
+
endColumnId: nextAddr.columnId
|
|
671
|
+
}
|
|
672
|
+
],
|
|
673
|
+
anchor: nextAddr
|
|
674
|
+
};
|
|
675
|
+
} else {
|
|
676
|
+
const newActive = {
|
|
677
|
+
startRowId: selection.anchor.rowId,
|
|
678
|
+
endRowId: nextAddr.rowId,
|
|
679
|
+
startColumnId: selection.anchor.columnId,
|
|
680
|
+
endColumnId: nextAddr.columnId
|
|
681
|
+
};
|
|
682
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
683
|
+
selection = { ranges, anchor: selection.anchor };
|
|
684
|
+
}
|
|
685
|
+
} else {
|
|
686
|
+
selection = {
|
|
687
|
+
ranges: [
|
|
688
|
+
{
|
|
689
|
+
startRowId: nextAddr.rowId,
|
|
690
|
+
endRowId: nextAddr.rowId,
|
|
691
|
+
startColumnId: nextAddr.columnId,
|
|
692
|
+
endColumnId: nextAddr.columnId
|
|
693
|
+
}
|
|
694
|
+
],
|
|
695
|
+
anchor: nextAddr
|
|
696
|
+
};
|
|
697
|
+
}
|
|
238
698
|
emit();
|
|
239
699
|
},
|
|
240
700
|
setViewport(nextViewport) {
|
|
@@ -252,9 +712,222 @@ function createGridCore(inputOptions) {
|
|
|
252
712
|
options = nextOptions;
|
|
253
713
|
emit();
|
|
254
714
|
},
|
|
715
|
+
setColumnWidth(columnId, width) {
|
|
716
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
720
|
+
if (idx === -1) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
const column = options.columns[idx];
|
|
724
|
+
const clamped = clampColumnWidth(width, column);
|
|
725
|
+
if (column.widthPx === clamped) {
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const nextColumns = options.columns.slice();
|
|
729
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
730
|
+
options = { ...options, columns: nextColumns };
|
|
731
|
+
pinnedWidthColumnIds.add(columnId);
|
|
732
|
+
emit();
|
|
733
|
+
},
|
|
734
|
+
moveColumn(columnId, toIndex) {
|
|
735
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const fromIndex = options.columns.findIndex((c) => c.id === columnId);
|
|
739
|
+
if (fromIndex === -1) {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
const synthAtZero = options.columns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
743
|
+
const minIndex = synthAtZero ? 1 : 0;
|
|
744
|
+
const maxIndex = options.columns.length - 1;
|
|
745
|
+
const clampedTo = Math.max(minIndex, Math.min(maxIndex, toIndex));
|
|
746
|
+
if (fromIndex === clampedTo) {
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
const nextColumns = options.columns.slice();
|
|
750
|
+
const [moved] = nextColumns.splice(fromIndex, 1);
|
|
751
|
+
if (!moved) {
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
nextColumns.splice(clampedTo, 0, moved);
|
|
755
|
+
let leftBoundary = minIndex;
|
|
756
|
+
for (let i = minIndex; i < nextColumns.length; i += 1) {
|
|
757
|
+
if (i === clampedTo) {
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
if (nextColumns[i]?.pinned === "left") {
|
|
761
|
+
leftBoundary = i + 1;
|
|
762
|
+
} else {
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
let rightBoundary = nextColumns.length;
|
|
767
|
+
for (let i = nextColumns.length - 1; i >= minIndex; i -= 1) {
|
|
768
|
+
if (i === clampedTo) {
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (nextColumns[i]?.pinned === "right") {
|
|
772
|
+
rightBoundary = i;
|
|
773
|
+
} else {
|
|
774
|
+
break;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
const nextPinned = clampedTo < leftBoundary ? "left" : clampedTo >= rightBoundary ? "right" : void 0;
|
|
778
|
+
if (nextPinned !== moved.pinned) {
|
|
779
|
+
nextColumns[clampedTo] = { ...moved, pinned: nextPinned };
|
|
780
|
+
}
|
|
781
|
+
options = { ...options, columns: nextColumns };
|
|
782
|
+
emit();
|
|
783
|
+
},
|
|
784
|
+
setColumnOrder(ids) {
|
|
785
|
+
const byId = new Map(options.columns.map((c) => [c.id, c]));
|
|
786
|
+
const taken = /* @__PURE__ */ new Set();
|
|
787
|
+
const ordered = [];
|
|
788
|
+
for (const id of ids) {
|
|
789
|
+
if (id === ROW_SELECT_COLUMN_ID || taken.has(id)) {
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
const column = byId.get(id);
|
|
793
|
+
if (!column) {
|
|
794
|
+
continue;
|
|
795
|
+
}
|
|
796
|
+
taken.add(id);
|
|
797
|
+
ordered.push(column);
|
|
798
|
+
}
|
|
799
|
+
for (const column of options.columns) {
|
|
800
|
+
if (taken.has(column.id)) {
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
taken.add(column.id);
|
|
804
|
+
ordered.push(column);
|
|
805
|
+
}
|
|
806
|
+
const nextColumns = groupColumnsByPin(ordered);
|
|
807
|
+
let changed = nextColumns.length !== options.columns.length;
|
|
808
|
+
if (!changed) {
|
|
809
|
+
for (let i = 0; i < nextColumns.length; i += 1) {
|
|
810
|
+
if (nextColumns[i].id !== options.columns[i].id) {
|
|
811
|
+
changed = true;
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
if (!changed) {
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
options = { ...options, columns: nextColumns };
|
|
820
|
+
emit();
|
|
821
|
+
},
|
|
822
|
+
setColumnPinned(columnId, pinned) {
|
|
823
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
827
|
+
if (idx === -1) {
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
const column = options.columns[idx];
|
|
831
|
+
const nextPinnedValue = pinned === "left" || pinned === "right" ? pinned : void 0;
|
|
832
|
+
if (column.pinned === nextPinnedValue) {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
const nextColumns = options.columns.slice();
|
|
836
|
+
nextColumns.splice(idx, 1);
|
|
837
|
+
const synthAtZero = nextColumns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
838
|
+
const baseStart = synthAtZero ? 1 : 0;
|
|
839
|
+
let boundary = baseStart;
|
|
840
|
+
while (boundary < nextColumns.length && nextColumns[boundary]?.pinned === "left") {
|
|
841
|
+
boundary += 1;
|
|
842
|
+
}
|
|
843
|
+
let rightBoundary = nextColumns.length;
|
|
844
|
+
while (rightBoundary > boundary && nextColumns[rightBoundary - 1]?.pinned === "right") {
|
|
845
|
+
rightBoundary -= 1;
|
|
846
|
+
}
|
|
847
|
+
const staysAtRightBoundary = nextPinnedValue === "right" || nextPinnedValue === void 0 && column.pinned === "right";
|
|
848
|
+
const insertAt = staysAtRightBoundary ? rightBoundary : boundary;
|
|
849
|
+
const nextColumn = {
|
|
850
|
+
...column,
|
|
851
|
+
pinned: nextPinnedValue
|
|
852
|
+
};
|
|
853
|
+
nextColumns.splice(insertAt, 0, nextColumn);
|
|
854
|
+
options = { ...options, columns: nextColumns };
|
|
855
|
+
emit();
|
|
856
|
+
},
|
|
857
|
+
autosizeColumn(columnId, autosizeOptions) {
|
|
858
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
862
|
+
if (idx === -1) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
const column = options.columns[idx];
|
|
866
|
+
const probeColumns = options.columns.slice();
|
|
867
|
+
probeColumns[idx] = { ...column, widthPx: void 0 };
|
|
868
|
+
const probedOptions = { ...options, columns: probeColumns };
|
|
869
|
+
const probed = applyAutosize(probedOptions, autosizeOptions);
|
|
870
|
+
const nextWidth = probed.columns[idx]?.widthPx;
|
|
871
|
+
if (nextWidth === void 0 || nextWidth === column.widthPx) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const clamped = clampColumnWidth(nextWidth, column);
|
|
875
|
+
const nextColumns = options.columns.slice();
|
|
876
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
877
|
+
options = { ...options, columns: nextColumns };
|
|
878
|
+
emit();
|
|
879
|
+
},
|
|
880
|
+
resetColumnLayout() {
|
|
881
|
+
const restored = inputOptions.autosize ? applyAutosize({
|
|
882
|
+
...inputOptions,
|
|
883
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
884
|
+
}, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : {
|
|
885
|
+
...inputOptions,
|
|
886
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
887
|
+
};
|
|
888
|
+
const current = options.columns;
|
|
889
|
+
const next = restored.columns;
|
|
890
|
+
if (current.length === next.length) {
|
|
891
|
+
let same = true;
|
|
892
|
+
for (let i = 0; i < current.length; i += 1) {
|
|
893
|
+
const c = current[i];
|
|
894
|
+
const n = next[i];
|
|
895
|
+
if (c.id !== n.id || c.widthPx !== n.widthPx || c.pinned !== n.pinned) {
|
|
896
|
+
same = false;
|
|
897
|
+
break;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
if (same) {
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
options = { ...options, columns: next };
|
|
905
|
+
emit();
|
|
906
|
+
},
|
|
907
|
+
mergeColumnsFromProps(nextColumns) {
|
|
908
|
+
const currentById = new Map(options.columns.map((c) => [c.id, c]));
|
|
909
|
+
const merged = nextColumns.map((newCol) => {
|
|
910
|
+
const existing = currentById.get(newCol.id);
|
|
911
|
+
if (existing) {
|
|
912
|
+
return {
|
|
913
|
+
...newCol,
|
|
914
|
+
widthPx: existing.widthPx ?? newCol.widthPx,
|
|
915
|
+
pinned: existing.pinned ?? newCol.pinned
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
return { ...newCol };
|
|
919
|
+
});
|
|
920
|
+
const grouped = groupColumnsByPin(merged);
|
|
921
|
+
const changed = !sameColumnLayout(options.columns, grouped);
|
|
922
|
+
originalColumns = groupColumnsByPin(nextColumns).map((c) => ({ ...c }));
|
|
923
|
+
options = { ...options, columns: grouped };
|
|
924
|
+
if (changed) {
|
|
925
|
+
emit();
|
|
926
|
+
}
|
|
927
|
+
},
|
|
255
928
|
applyTransaction(transaction) {
|
|
256
929
|
if (!options.getRowId) {
|
|
257
|
-
throw new Error("applyTransaction requires getRowId on
|
|
930
|
+
throw new Error("applyTransaction requires getRowId on PretableGridOptions");
|
|
258
931
|
}
|
|
259
932
|
const getRowId = options.getRowId;
|
|
260
933
|
if (transaction.remove) {
|
|
@@ -301,6 +974,108 @@ function createGridCore(inputOptions) {
|
|
|
301
974
|
}
|
|
302
975
|
cachedVisibleRows = null;
|
|
303
976
|
emit();
|
|
977
|
+
},
|
|
978
|
+
setRows(nextRows) {
|
|
979
|
+
options = { ...options, rows: nextRows };
|
|
980
|
+
sourceRows = createSourceRows(options);
|
|
981
|
+
sourceRowIndex.clear();
|
|
982
|
+
for (const entry of sourceRows) {
|
|
983
|
+
sourceRowIndex.set(entry.id, entry);
|
|
984
|
+
}
|
|
985
|
+
const hasRow = (id) => id != null && sourceRowIndex.has(id);
|
|
986
|
+
const keptRanges = selection.ranges.filter((range) => hasRow(range.startRowId) && hasRow(range.endRowId));
|
|
987
|
+
const anchorValid = !selection.anchor || hasRow(selection.anchor.rowId);
|
|
988
|
+
if (keptRanges.length !== selection.ranges.length || !anchorValid) {
|
|
989
|
+
selection = {
|
|
990
|
+
ranges: keptRanges,
|
|
991
|
+
anchor: anchorValid ? selection.anchor : null
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
if (focus.rowId !== null && !hasRow(focus.rowId)) {
|
|
995
|
+
focus = { rowId: null, columnId: null };
|
|
996
|
+
}
|
|
997
|
+
if (editing && !hasRow(editing.rowId)) {
|
|
998
|
+
editing = null;
|
|
999
|
+
}
|
|
1000
|
+
if (options.autosize) {
|
|
1001
|
+
const measured = applyAutosize({ ...options, columns: originalColumns }, typeof options.autosize === "object" ? options.autosize : void 0);
|
|
1002
|
+
const measuredById = new Map(measured.columns.map((column) => [column.id, column.widthPx]));
|
|
1003
|
+
options = {
|
|
1004
|
+
...options,
|
|
1005
|
+
// Keyed by id, not index — `moveColumn` can leave the live order out
|
|
1006
|
+
// of step with `originalColumns`, and the live set may carry the
|
|
1007
|
+
// synthetic row-select column that never appears in props.
|
|
1008
|
+
columns: options.columns.map((column) => {
|
|
1009
|
+
if (pinnedWidthColumnIds.has(column.id)) {
|
|
1010
|
+
return column;
|
|
1011
|
+
}
|
|
1012
|
+
const width = measuredById.get(column.id);
|
|
1013
|
+
if (width === void 0 || width === column.widthPx) {
|
|
1014
|
+
return column;
|
|
1015
|
+
}
|
|
1016
|
+
return { ...column, widthPx: width };
|
|
1017
|
+
})
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
cachedVisibleRows = null;
|
|
1021
|
+
emit();
|
|
1022
|
+
},
|
|
1023
|
+
beginEdit(addr, opts) {
|
|
1024
|
+
editing = {
|
|
1025
|
+
rowId: addr.rowId,
|
|
1026
|
+
columnId: addr.columnId,
|
|
1027
|
+
draft: opts?.draft,
|
|
1028
|
+
status: opts?.status ?? "editing"
|
|
1029
|
+
};
|
|
1030
|
+
emit();
|
|
1031
|
+
},
|
|
1032
|
+
setEditDraft(value) {
|
|
1033
|
+
if (!editing)
|
|
1034
|
+
return;
|
|
1035
|
+
editing = { ...editing, draft: value };
|
|
1036
|
+
emit();
|
|
1037
|
+
},
|
|
1038
|
+
markEditing() {
|
|
1039
|
+
if (!editing || editing.status !== "checking")
|
|
1040
|
+
return;
|
|
1041
|
+
editing = { ...editing, status: "editing", error: void 0 };
|
|
1042
|
+
emit();
|
|
1043
|
+
},
|
|
1044
|
+
markEditValidating() {
|
|
1045
|
+
if (!editing)
|
|
1046
|
+
return;
|
|
1047
|
+
editing = { ...editing, status: "validating", error: void 0 };
|
|
1048
|
+
emit();
|
|
1049
|
+
},
|
|
1050
|
+
markEditSaving() {
|
|
1051
|
+
if (!editing)
|
|
1052
|
+
return;
|
|
1053
|
+
editing = { ...editing, status: "saving", error: void 0 };
|
|
1054
|
+
emit();
|
|
1055
|
+
},
|
|
1056
|
+
markEditInvalid(message) {
|
|
1057
|
+
if (!editing)
|
|
1058
|
+
return;
|
|
1059
|
+
editing = { ...editing, status: "editing", error: message };
|
|
1060
|
+
emit();
|
|
1061
|
+
},
|
|
1062
|
+
markEditError(message) {
|
|
1063
|
+
if (!editing)
|
|
1064
|
+
return;
|
|
1065
|
+
editing = { ...editing, status: "error", error: message };
|
|
1066
|
+
emit();
|
|
1067
|
+
},
|
|
1068
|
+
commitEditSucceeded() {
|
|
1069
|
+
if (!editing)
|
|
1070
|
+
return;
|
|
1071
|
+
editing = null;
|
|
1072
|
+
emit();
|
|
1073
|
+
},
|
|
1074
|
+
cancelEdit() {
|
|
1075
|
+
if (!editing)
|
|
1076
|
+
return;
|
|
1077
|
+
editing = null;
|
|
1078
|
+
emit();
|
|
304
1079
|
}
|
|
305
1080
|
};
|
|
306
1081
|
return store;
|
|
@@ -319,11 +1094,11 @@ function createGridCore(inputOptions) {
|
|
|
319
1094
|
cachedDerivedFilters = filters;
|
|
320
1095
|
cachedSnapshot = {
|
|
321
1096
|
viewport,
|
|
322
|
-
sort,
|
|
1097
|
+
sort: sort.map((entry) => ({ ...entry })),
|
|
323
1098
|
filters: { ...filters },
|
|
324
1099
|
selection: {
|
|
325
|
-
|
|
326
|
-
|
|
1100
|
+
ranges: selection.ranges.map((r) => ({ ...r })),
|
|
1101
|
+
anchor: selection.anchor ? { ...selection.anchor } : null
|
|
327
1102
|
},
|
|
328
1103
|
focus,
|
|
329
1104
|
totalRowCount: sourceRows.length,
|
|
@@ -331,7 +1106,8 @@ function createGridCore(inputOptions) {
|
|
|
331
1106
|
visibleRange: {
|
|
332
1107
|
start: 0,
|
|
333
1108
|
end: visibleRows.length
|
|
334
|
-
}
|
|
1109
|
+
},
|
|
1110
|
+
editing: editing ? { ...editing } : null
|
|
335
1111
|
};
|
|
336
1112
|
return cachedSnapshot;
|
|
337
1113
|
}
|
|
@@ -345,6 +1121,74 @@ function createGridCore(inputOptions) {
|
|
|
345
1121
|
function clamp(value, min, max) {
|
|
346
1122
|
return Math.max(min, Math.min(max, value));
|
|
347
1123
|
}
|
|
1124
|
+
function sameColumnLayout(a, b) {
|
|
1125
|
+
if (a.length !== b.length) {
|
|
1126
|
+
return false;
|
|
1127
|
+
}
|
|
1128
|
+
return a.every((left, index) => {
|
|
1129
|
+
const right = b[index];
|
|
1130
|
+
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;
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
function computePageStep(viewport, visibleRows) {
|
|
1134
|
+
if (viewport.height <= 0 || visibleRows.length === 0) {
|
|
1135
|
+
return 1;
|
|
1136
|
+
}
|
|
1137
|
+
const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
|
|
1138
|
+
return Math.min(estimatedRowsPerPage, visibleRows.length);
|
|
1139
|
+
}
|
|
1140
|
+
function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
|
|
1141
|
+
return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
|
|
1142
|
+
}
|
|
1143
|
+
function selectionsEqual(a, b) {
|
|
1144
|
+
if (a.ranges.length !== b.ranges.length) {
|
|
1145
|
+
return false;
|
|
1146
|
+
}
|
|
1147
|
+
for (let i = 0; i < a.ranges.length; i += 1) {
|
|
1148
|
+
const ar = a.ranges[i];
|
|
1149
|
+
const br = b.ranges[i];
|
|
1150
|
+
if (ar.startRowId !== br.startRowId || ar.endRowId !== br.endRowId || ar.startColumnId !== br.startColumnId || ar.endColumnId !== br.endColumnId) {
|
|
1151
|
+
return false;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
if (a.anchor === null && b.anchor === null) {
|
|
1155
|
+
return true;
|
|
1156
|
+
}
|
|
1157
|
+
if (a.anchor === null || b.anchor === null) {
|
|
1158
|
+
return false;
|
|
1159
|
+
}
|
|
1160
|
+
return a.anchor.rowId === b.anchor.rowId && a.anchor.columnId === b.anchor.columnId;
|
|
1161
|
+
}
|
|
1162
|
+
function sanitizeSortEntries(entries, columns) {
|
|
1163
|
+
return entries.filter((entry) => {
|
|
1164
|
+
const column = columns.find((c) => c.id === entry.columnId);
|
|
1165
|
+
return column !== void 0 && column.sortable !== false;
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
function sortsEqual(a, b) {
|
|
1169
|
+
if (a.length !== b.length) {
|
|
1170
|
+
return false;
|
|
1171
|
+
}
|
|
1172
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
1173
|
+
const ae = a[i];
|
|
1174
|
+
const be = b[i];
|
|
1175
|
+
if (ae.columnId !== be.columnId || ae.direction !== be.direction) {
|
|
1176
|
+
return false;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return true;
|
|
1180
|
+
}
|
|
1181
|
+
function columnFilterEqual(a, b) {
|
|
1182
|
+
if (a.operator !== b.operator) {
|
|
1183
|
+
return false;
|
|
1184
|
+
}
|
|
1185
|
+
const av = a.value;
|
|
1186
|
+
const bv = b.value;
|
|
1187
|
+
if (Array.isArray(av) && Array.isArray(bv)) {
|
|
1188
|
+
return av.length === bv.length && av.every((v, i) => v === bv[i]);
|
|
1189
|
+
}
|
|
1190
|
+
return av === bv;
|
|
1191
|
+
}
|
|
348
1192
|
function filtersEqual(a, b) {
|
|
349
1193
|
const aKeys = Object.keys(a);
|
|
350
1194
|
const bKeys = Object.keys(b);
|
|
@@ -352,7 +1196,9 @@ function filtersEqual(a, b) {
|
|
|
352
1196
|
return false;
|
|
353
1197
|
}
|
|
354
1198
|
for (const key of aKeys) {
|
|
355
|
-
|
|
1199
|
+
const av = a[key];
|
|
1200
|
+
const bv = b[key];
|
|
1201
|
+
if (!av || !bv || !columnFilterEqual(av, bv)) {
|
|
356
1202
|
return false;
|
|
357
1203
|
}
|
|
358
1204
|
}
|
|
@@ -361,24 +1207,49 @@ function filtersEqual(a, b) {
|
|
|
361
1207
|
|
|
362
1208
|
// src/create-grid.ts
|
|
363
1209
|
function createGrid(options) {
|
|
364
|
-
const
|
|
1210
|
+
const engine = createGridCore(options);
|
|
365
1211
|
return {
|
|
366
1212
|
kind: "pretable-grid",
|
|
367
1213
|
get options() {
|
|
368
|
-
return
|
|
369
|
-
},
|
|
370
|
-
subscribe:
|
|
371
|
-
getSnapshot:
|
|
372
|
-
setSort:
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
1214
|
+
return engine.options;
|
|
1215
|
+
},
|
|
1216
|
+
subscribe: engine.subscribe,
|
|
1217
|
+
getSnapshot: engine.getSnapshot,
|
|
1218
|
+
setSort: engine.setSort,
|
|
1219
|
+
replaceSort: engine.replaceSort,
|
|
1220
|
+
setColumnFilter: engine.setColumnFilter,
|
|
1221
|
+
clearFilters: engine.clearFilters,
|
|
1222
|
+
replaceFilters: engine.replaceFilters,
|
|
1223
|
+
distinctColumnValues: engine.distinctColumnValues,
|
|
1224
|
+
setSelection: engine.setSelection,
|
|
1225
|
+
selectAll: engine.selectAll,
|
|
1226
|
+
clearSelection: engine.clearSelection,
|
|
1227
|
+
addRange: engine.addRange,
|
|
1228
|
+
extendRangeFromAnchor: engine.extendRangeFromAnchor,
|
|
1229
|
+
toggleRowSelection: engine.toggleRowSelection,
|
|
1230
|
+
setSelectAllVisible: engine.setSelectAllVisible,
|
|
1231
|
+
setFocus: engine.setFocus,
|
|
1232
|
+
moveFocus: engine.moveFocus,
|
|
1233
|
+
setViewport: engine.setViewport,
|
|
1234
|
+
autosizeColumns: engine.autosizeColumns,
|
|
1235
|
+
setColumnWidth: engine.setColumnWidth,
|
|
1236
|
+
moveColumn: engine.moveColumn,
|
|
1237
|
+
setColumnOrder: engine.setColumnOrder,
|
|
1238
|
+
setColumnPinned: engine.setColumnPinned,
|
|
1239
|
+
autosizeColumn: engine.autosizeColumn,
|
|
1240
|
+
resetColumnLayout: engine.resetColumnLayout,
|
|
1241
|
+
mergeColumnsFromProps: engine.mergeColumnsFromProps,
|
|
1242
|
+
applyTransaction: engine.applyTransaction,
|
|
1243
|
+
setRows: engine.setRows,
|
|
1244
|
+
beginEdit: engine.beginEdit,
|
|
1245
|
+
setEditDraft: engine.setEditDraft,
|
|
1246
|
+
markEditing: engine.markEditing,
|
|
1247
|
+
markEditValidating: engine.markEditValidating,
|
|
1248
|
+
markEditSaving: engine.markEditSaving,
|
|
1249
|
+
markEditInvalid: engine.markEditInvalid,
|
|
1250
|
+
markEditError: engine.markEditError,
|
|
1251
|
+
commitEditSucceeded: engine.commitEditSucceeded,
|
|
1252
|
+
cancelEdit: engine.cancelEdit
|
|
382
1253
|
};
|
|
383
1254
|
}
|
|
384
1255
|
|