@pretable/core 0.0.1 → 0.0.2
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 +455 -35
- package/dist/index.d.cts +198 -47
- package/dist/index.d.ts +198 -47
- package/dist/index.mjs +455 -35
- 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;
|
|
@@ -108,10 +108,16 @@ function sortRows(rows, columns, sort) {
|
|
|
108
108
|
return indexed.map((i) => rows[i]);
|
|
109
109
|
}
|
|
110
110
|
function readCellValue(row, column) {
|
|
111
|
-
return column.
|
|
111
|
+
return column.value ? column.value(row) : row[column.id];
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
// ../grid-core/dist/create-grid-core.js
|
|
115
|
+
var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
|
|
116
|
+
function clampColumnWidth(width, column) {
|
|
117
|
+
const min = column.minWidthPx ?? 40;
|
|
118
|
+
const max = column.maxWidthPx ?? Infinity;
|
|
119
|
+
return Math.max(min, Math.min(max, width));
|
|
120
|
+
}
|
|
115
121
|
function applyAutosize(options, autosizeOptions) {
|
|
116
122
|
const result = autosizeColumns({
|
|
117
123
|
columns: options.columns,
|
|
@@ -133,6 +139,7 @@ function applyAutosize(options, autosizeOptions) {
|
|
|
133
139
|
function createGridCore(inputOptions) {
|
|
134
140
|
const listeners = /* @__PURE__ */ new Set();
|
|
135
141
|
let options = inputOptions.autosize ? applyAutosize(inputOptions, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : inputOptions;
|
|
142
|
+
let originalColumns = inputOptions.columns.map((c) => ({ ...c }));
|
|
136
143
|
let sourceRows = createSourceRows(options);
|
|
137
144
|
const sourceRowIndex = new Map(sourceRows.map((entry) => [entry.id, entry]));
|
|
138
145
|
let cachedSnapshot = null;
|
|
@@ -141,7 +148,7 @@ function createGridCore(inputOptions) {
|
|
|
141
148
|
let cachedDerivedFilters = null;
|
|
142
149
|
let sort = { columnId: null, direction: null };
|
|
143
150
|
let filters = {};
|
|
144
|
-
let selection = {
|
|
151
|
+
let selection = { ranges: [], anchor: null };
|
|
145
152
|
let focus = { rowId: null, columnId: null };
|
|
146
153
|
let viewport = {
|
|
147
154
|
scrollTop: 0,
|
|
@@ -206,35 +213,251 @@ function createGridCore(inputOptions) {
|
|
|
206
213
|
filters = normalized;
|
|
207
214
|
emit();
|
|
208
215
|
},
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
216
|
+
setSelection(next) {
|
|
217
|
+
if (selectionsEqual(selection, next)) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
selection = {
|
|
221
|
+
ranges: next.ranges.map((r) => ({ ...r })),
|
|
222
|
+
anchor: next.anchor ? { ...next.anchor } : null
|
|
223
|
+
};
|
|
224
|
+
emit();
|
|
225
|
+
},
|
|
226
|
+
selectAll() {
|
|
227
|
+
const snapshot = getSnapshot();
|
|
228
|
+
const firstRow = snapshot.visibleRows[0];
|
|
229
|
+
const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
|
|
230
|
+
const firstColumn = options.columns[0];
|
|
231
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
232
|
+
if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const range = {
|
|
236
|
+
startRowId: firstRow.id,
|
|
237
|
+
endRowId: lastRow.id,
|
|
238
|
+
startColumnId: firstColumn.id,
|
|
239
|
+
endColumnId: lastColumn.id
|
|
240
|
+
};
|
|
241
|
+
const anchor = {
|
|
242
|
+
rowId: firstRow.id,
|
|
243
|
+
columnId: firstColumn.id
|
|
244
|
+
};
|
|
245
|
+
const next = { ranges: [range], anchor };
|
|
246
|
+
if (selectionsEqual(selection, next)) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
selection = next;
|
|
250
|
+
emit();
|
|
251
|
+
},
|
|
252
|
+
clearSelection() {
|
|
253
|
+
const focusAddr = focus.rowId && focus.columnId ? { rowId: focus.rowId, columnId: focus.columnId } : null;
|
|
254
|
+
const next = focusAddr ? {
|
|
255
|
+
ranges: [
|
|
256
|
+
{
|
|
257
|
+
startRowId: focusAddr.rowId,
|
|
258
|
+
endRowId: focusAddr.rowId,
|
|
259
|
+
startColumnId: focusAddr.columnId,
|
|
260
|
+
endColumnId: focusAddr.columnId
|
|
261
|
+
}
|
|
262
|
+
],
|
|
263
|
+
anchor: focusAddr
|
|
264
|
+
} : { ranges: [], anchor: null };
|
|
265
|
+
if (selectionsEqual(selection, next)) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
selection = next;
|
|
269
|
+
emit();
|
|
270
|
+
},
|
|
271
|
+
addRange(range) {
|
|
272
|
+
selection = {
|
|
273
|
+
ranges: [...selection.ranges, { ...range }],
|
|
274
|
+
anchor: { rowId: range.startRowId, columnId: range.startColumnId }
|
|
275
|
+
};
|
|
276
|
+
emit();
|
|
277
|
+
},
|
|
278
|
+
extendRangeFromAnchor(addr) {
|
|
279
|
+
if (!selection.anchor) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const newActive = {
|
|
283
|
+
startRowId: selection.anchor.rowId,
|
|
284
|
+
endRowId: addr.rowId,
|
|
285
|
+
startColumnId: selection.anchor.columnId,
|
|
286
|
+
endColumnId: addr.columnId
|
|
287
|
+
};
|
|
288
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
289
|
+
selection = { ranges, anchor: selection.anchor };
|
|
290
|
+
emit();
|
|
291
|
+
},
|
|
292
|
+
toggleRowSelection(rowId) {
|
|
293
|
+
const firstColumn = options.columns[0];
|
|
294
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
295
|
+
if (!firstColumn || !lastColumn) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const fullRowRange = {
|
|
299
|
+
startRowId: rowId,
|
|
300
|
+
endRowId: rowId,
|
|
301
|
+
startColumnId: firstColumn.id,
|
|
302
|
+
endColumnId: lastColumn.id
|
|
303
|
+
};
|
|
304
|
+
const matchIndex = selection.ranges.findIndex((r) => isFullRowRange(r, rowId, firstColumn.id, lastColumn.id));
|
|
305
|
+
if (matchIndex >= 0) {
|
|
306
|
+
const ranges = selection.ranges.filter((_, i) => i !== matchIndex);
|
|
307
|
+
selection = { ranges, anchor: selection.anchor };
|
|
308
|
+
} else {
|
|
309
|
+
selection = {
|
|
310
|
+
ranges: [...selection.ranges, fullRowRange],
|
|
311
|
+
anchor: { rowId, columnId: firstColumn.id }
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
emit();
|
|
315
|
+
},
|
|
316
|
+
setSelectAllVisible(checked) {
|
|
317
|
+
const snapshot = getSnapshot();
|
|
318
|
+
const firstColumn = options.columns[0];
|
|
319
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
320
|
+
if (!firstColumn || !lastColumn) {
|
|
212
321
|
return;
|
|
213
322
|
}
|
|
214
|
-
|
|
323
|
+
const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
|
|
324
|
+
const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
|
|
325
|
+
let next;
|
|
326
|
+
if (checked) {
|
|
327
|
+
const newRanges = snapshot.visibleRows.map((row) => ({
|
|
328
|
+
startRowId: row.id,
|
|
329
|
+
endRowId: row.id,
|
|
330
|
+
startColumnId: firstColumn.id,
|
|
331
|
+
endColumnId: lastColumn.id
|
|
332
|
+
}));
|
|
333
|
+
next = {
|
|
334
|
+
ranges: [...nonRowRanges, ...newRanges],
|
|
335
|
+
anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
|
|
336
|
+
};
|
|
337
|
+
} else {
|
|
338
|
+
next = { ranges: nonRowRanges, anchor: selection.anchor };
|
|
339
|
+
}
|
|
340
|
+
if (selectionsEqual(selection, next)) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
selection = next;
|
|
215
344
|
emit();
|
|
216
345
|
},
|
|
217
|
-
setFocus(
|
|
218
|
-
|
|
346
|
+
setFocus(addr) {
|
|
347
|
+
const nextRowId = addr?.rowId ?? null;
|
|
348
|
+
const nextColumnId = addr?.columnId ?? null;
|
|
349
|
+
if (focus.rowId === nextRowId && focus.columnId === nextColumnId) {
|
|
219
350
|
return;
|
|
220
351
|
}
|
|
221
|
-
focus = { rowId, columnId };
|
|
352
|
+
focus = { rowId: nextRowId, columnId: nextColumnId };
|
|
222
353
|
emit();
|
|
223
354
|
},
|
|
224
|
-
moveFocus(
|
|
355
|
+
moveFocus(direction, moveOptions = {}) {
|
|
225
356
|
const snapshot = getSnapshot();
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
357
|
+
const visibleRows = snapshot.visibleRows;
|
|
358
|
+
const columnList = options.columns;
|
|
359
|
+
if (visibleRows.length === 0 || columnList.length === 0) {
|
|
360
|
+
focus = { rowId: null, columnId: null };
|
|
229
361
|
emit();
|
|
230
362
|
return;
|
|
231
363
|
}
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
|
|
364
|
+
const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
|
|
365
|
+
const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
|
|
366
|
+
const hasRowFocus = currentRowIndex !== -1;
|
|
367
|
+
const hasColumnFocus = currentColumnIndex !== -1;
|
|
368
|
+
const baseRowIndex = hasRowFocus ? currentRowIndex : 0;
|
|
369
|
+
const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
|
|
370
|
+
let nextRowIndex = baseRowIndex;
|
|
371
|
+
let nextColumnIndex = baseColumnIndex;
|
|
372
|
+
const pageStep = computePageStep(viewport, visibleRows);
|
|
373
|
+
switch (direction) {
|
|
374
|
+
case "up":
|
|
375
|
+
if (moveOptions.jumpToEdge) {
|
|
376
|
+
nextRowIndex = 0;
|
|
377
|
+
} else if (!hasRowFocus) {
|
|
378
|
+
nextRowIndex = visibleRows.length - 1;
|
|
379
|
+
} else if (moveOptions.byPage) {
|
|
380
|
+
nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
|
|
381
|
+
} else {
|
|
382
|
+
nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
case "down":
|
|
386
|
+
if (moveOptions.jumpToEdge) {
|
|
387
|
+
nextRowIndex = visibleRows.length - 1;
|
|
388
|
+
} else if (!hasRowFocus) {
|
|
389
|
+
nextRowIndex = 0;
|
|
390
|
+
} else if (moveOptions.byPage) {
|
|
391
|
+
nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
|
|
392
|
+
} else {
|
|
393
|
+
nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
|
|
394
|
+
}
|
|
395
|
+
break;
|
|
396
|
+
case "left":
|
|
397
|
+
if (moveOptions.jumpToEdge) {
|
|
398
|
+
nextColumnIndex = 0;
|
|
399
|
+
} else if (!hasColumnFocus) {
|
|
400
|
+
nextColumnIndex = columnList.length - 1;
|
|
401
|
+
} else {
|
|
402
|
+
nextColumnIndex = clamp(baseColumnIndex - 1, 0, columnList.length - 1);
|
|
403
|
+
}
|
|
404
|
+
break;
|
|
405
|
+
case "right":
|
|
406
|
+
if (moveOptions.jumpToEdge) {
|
|
407
|
+
nextColumnIndex = columnList.length - 1;
|
|
408
|
+
} else if (!hasColumnFocus) {
|
|
409
|
+
nextColumnIndex = 0;
|
|
410
|
+
} else {
|
|
411
|
+
nextColumnIndex = clamp(baseColumnIndex + 1, 0, columnList.length - 1);
|
|
412
|
+
}
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
const nextRow = visibleRows[nextRowIndex];
|
|
416
|
+
const nextColumn = columnList[nextColumnIndex];
|
|
417
|
+
if (!nextRow || !nextColumn) {
|
|
235
418
|
return;
|
|
236
419
|
}
|
|
237
|
-
|
|
420
|
+
const nextAddr = {
|
|
421
|
+
rowId: nextRow.id,
|
|
422
|
+
columnId: nextColumn.id
|
|
423
|
+
};
|
|
424
|
+
focus = nextAddr;
|
|
425
|
+
if (moveOptions.extend) {
|
|
426
|
+
if (!selection.anchor) {
|
|
427
|
+
selection = {
|
|
428
|
+
ranges: [
|
|
429
|
+
{
|
|
430
|
+
startRowId: nextAddr.rowId,
|
|
431
|
+
endRowId: nextAddr.rowId,
|
|
432
|
+
startColumnId: nextAddr.columnId,
|
|
433
|
+
endColumnId: nextAddr.columnId
|
|
434
|
+
}
|
|
435
|
+
],
|
|
436
|
+
anchor: nextAddr
|
|
437
|
+
};
|
|
438
|
+
} else {
|
|
439
|
+
const newActive = {
|
|
440
|
+
startRowId: selection.anchor.rowId,
|
|
441
|
+
endRowId: nextAddr.rowId,
|
|
442
|
+
startColumnId: selection.anchor.columnId,
|
|
443
|
+
endColumnId: nextAddr.columnId
|
|
444
|
+
};
|
|
445
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
446
|
+
selection = { ranges, anchor: selection.anchor };
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
selection = {
|
|
450
|
+
ranges: [
|
|
451
|
+
{
|
|
452
|
+
startRowId: nextAddr.rowId,
|
|
453
|
+
endRowId: nextAddr.rowId,
|
|
454
|
+
startColumnId: nextAddr.columnId,
|
|
455
|
+
endColumnId: nextAddr.columnId
|
|
456
|
+
}
|
|
457
|
+
],
|
|
458
|
+
anchor: nextAddr
|
|
459
|
+
};
|
|
460
|
+
}
|
|
238
461
|
emit();
|
|
239
462
|
},
|
|
240
463
|
setViewport(nextViewport) {
|
|
@@ -252,9 +475,165 @@ function createGridCore(inputOptions) {
|
|
|
252
475
|
options = nextOptions;
|
|
253
476
|
emit();
|
|
254
477
|
},
|
|
478
|
+
setColumnWidth(columnId, width) {
|
|
479
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
483
|
+
if (idx === -1) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const column = options.columns[idx];
|
|
487
|
+
const clamped = clampColumnWidth(width, column);
|
|
488
|
+
if (column.widthPx === clamped) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const nextColumns = options.columns.slice();
|
|
492
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
493
|
+
options = { ...options, columns: nextColumns };
|
|
494
|
+
emit();
|
|
495
|
+
},
|
|
496
|
+
moveColumn(columnId, toIndex) {
|
|
497
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const fromIndex = options.columns.findIndex((c) => c.id === columnId);
|
|
501
|
+
if (fromIndex === -1) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const synthAtZero = options.columns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
505
|
+
const minIndex = synthAtZero ? 1 : 0;
|
|
506
|
+
const maxIndex = options.columns.length - 1;
|
|
507
|
+
const clampedTo = Math.max(minIndex, Math.min(maxIndex, toIndex));
|
|
508
|
+
if (fromIndex === clampedTo) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const nextColumns = options.columns.slice();
|
|
512
|
+
const [moved] = nextColumns.splice(fromIndex, 1);
|
|
513
|
+
if (!moved) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
nextColumns.splice(clampedTo, 0, moved);
|
|
517
|
+
let boundary = synthAtZero ? 1 : 0;
|
|
518
|
+
for (let i = synthAtZero ? 1 : 0; i < nextColumns.length; i += 1) {
|
|
519
|
+
if (i === clampedTo) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (nextColumns[i]?.pinned === "left") {
|
|
523
|
+
boundary = i + 1;
|
|
524
|
+
} else {
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const landsInPinned = clampedTo < boundary;
|
|
529
|
+
const wasPinned = moved.pinned === "left";
|
|
530
|
+
const nextPinned = landsInPinned ? "left" : void 0;
|
|
531
|
+
if (nextPinned !== moved.pinned || wasPinned !== landsInPinned) {
|
|
532
|
+
nextColumns[clampedTo] = { ...moved, pinned: nextPinned };
|
|
533
|
+
}
|
|
534
|
+
options = { ...options, columns: nextColumns };
|
|
535
|
+
emit();
|
|
536
|
+
},
|
|
537
|
+
setColumnPinned(columnId, pinned) {
|
|
538
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
542
|
+
if (idx === -1) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const column = options.columns[idx];
|
|
546
|
+
const nextPinnedValue = pinned === "left" ? "left" : void 0;
|
|
547
|
+
if (column.pinned === nextPinnedValue) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const nextColumns = options.columns.slice();
|
|
551
|
+
nextColumns.splice(idx, 1);
|
|
552
|
+
const synthAtZero = nextColumns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
553
|
+
const baseStart = synthAtZero ? 1 : 0;
|
|
554
|
+
let boundary = baseStart;
|
|
555
|
+
while (boundary < nextColumns.length && nextColumns[boundary]?.pinned === "left") {
|
|
556
|
+
boundary += 1;
|
|
557
|
+
}
|
|
558
|
+
const insertAt = boundary;
|
|
559
|
+
const nextColumn = {
|
|
560
|
+
...column,
|
|
561
|
+
pinned: nextPinnedValue
|
|
562
|
+
};
|
|
563
|
+
nextColumns.splice(insertAt, 0, nextColumn);
|
|
564
|
+
options = { ...options, columns: nextColumns };
|
|
565
|
+
emit();
|
|
566
|
+
},
|
|
567
|
+
autosizeColumn(columnId, autosizeOptions) {
|
|
568
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
572
|
+
if (idx === -1) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
const column = options.columns[idx];
|
|
576
|
+
const probeColumns = options.columns.slice();
|
|
577
|
+
probeColumns[idx] = { ...column, widthPx: void 0 };
|
|
578
|
+
const probedOptions = { ...options, columns: probeColumns };
|
|
579
|
+
const probed = applyAutosize(probedOptions, autosizeOptions);
|
|
580
|
+
const nextWidth = probed.columns[idx]?.widthPx;
|
|
581
|
+
if (nextWidth === void 0 || nextWidth === column.widthPx) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
const clamped = clampColumnWidth(nextWidth, column);
|
|
585
|
+
const nextColumns = options.columns.slice();
|
|
586
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
587
|
+
options = { ...options, columns: nextColumns };
|
|
588
|
+
emit();
|
|
589
|
+
},
|
|
590
|
+
resetColumnLayout() {
|
|
591
|
+
const restored = inputOptions.autosize ? applyAutosize({
|
|
592
|
+
...inputOptions,
|
|
593
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
594
|
+
}, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : {
|
|
595
|
+
...inputOptions,
|
|
596
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
597
|
+
};
|
|
598
|
+
const current = options.columns;
|
|
599
|
+
const next = restored.columns;
|
|
600
|
+
if (current.length === next.length) {
|
|
601
|
+
let same = true;
|
|
602
|
+
for (let i = 0; i < current.length; i += 1) {
|
|
603
|
+
const c = current[i];
|
|
604
|
+
const n = next[i];
|
|
605
|
+
if (c.id !== n.id || c.widthPx !== n.widthPx || c.pinned !== n.pinned) {
|
|
606
|
+
same = false;
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (same) {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
options = { ...options, columns: next };
|
|
615
|
+
emit();
|
|
616
|
+
},
|
|
617
|
+
mergeColumnsFromProps(nextColumns) {
|
|
618
|
+
const currentById = new Map(options.columns.map((c) => [c.id, c]));
|
|
619
|
+
const merged = nextColumns.map((newCol) => {
|
|
620
|
+
const existing = currentById.get(newCol.id);
|
|
621
|
+
if (existing) {
|
|
622
|
+
return {
|
|
623
|
+
...newCol,
|
|
624
|
+
widthPx: existing.widthPx ?? newCol.widthPx,
|
|
625
|
+
pinned: existing.pinned ?? newCol.pinned
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
return { ...newCol };
|
|
629
|
+
});
|
|
630
|
+
originalColumns = nextColumns.map((c) => ({ ...c }));
|
|
631
|
+
options = { ...options, columns: merged };
|
|
632
|
+
emit();
|
|
633
|
+
},
|
|
255
634
|
applyTransaction(transaction) {
|
|
256
635
|
if (!options.getRowId) {
|
|
257
|
-
throw new Error("applyTransaction requires getRowId on
|
|
636
|
+
throw new Error("applyTransaction requires getRowId on PretableGridOptions");
|
|
258
637
|
}
|
|
259
638
|
const getRowId = options.getRowId;
|
|
260
639
|
if (transaction.remove) {
|
|
@@ -322,8 +701,8 @@ function createGridCore(inputOptions) {
|
|
|
322
701
|
sort,
|
|
323
702
|
filters: { ...filters },
|
|
324
703
|
selection: {
|
|
325
|
-
|
|
326
|
-
|
|
704
|
+
ranges: selection.ranges.map((r) => ({ ...r })),
|
|
705
|
+
anchor: selection.anchor ? { ...selection.anchor } : null
|
|
327
706
|
},
|
|
328
707
|
focus,
|
|
329
708
|
totalRowCount: sourceRows.length,
|
|
@@ -345,6 +724,35 @@ function createGridCore(inputOptions) {
|
|
|
345
724
|
function clamp(value, min, max) {
|
|
346
725
|
return Math.max(min, Math.min(max, value));
|
|
347
726
|
}
|
|
727
|
+
function computePageStep(viewport, visibleRows) {
|
|
728
|
+
if (viewport.height <= 0 || visibleRows.length === 0) {
|
|
729
|
+
return 1;
|
|
730
|
+
}
|
|
731
|
+
const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
|
|
732
|
+
return Math.min(estimatedRowsPerPage, visibleRows.length);
|
|
733
|
+
}
|
|
734
|
+
function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
|
|
735
|
+
return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
|
|
736
|
+
}
|
|
737
|
+
function selectionsEqual(a, b) {
|
|
738
|
+
if (a.ranges.length !== b.ranges.length) {
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
for (let i = 0; i < a.ranges.length; i += 1) {
|
|
742
|
+
const ar = a.ranges[i];
|
|
743
|
+
const br = b.ranges[i];
|
|
744
|
+
if (ar.startRowId !== br.startRowId || ar.endRowId !== br.endRowId || ar.startColumnId !== br.startColumnId || ar.endColumnId !== br.endColumnId) {
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (a.anchor === null && b.anchor === null) {
|
|
749
|
+
return true;
|
|
750
|
+
}
|
|
751
|
+
if (a.anchor === null || b.anchor === null) {
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
return a.anchor.rowId === b.anchor.rowId && a.anchor.columnId === b.anchor.columnId;
|
|
755
|
+
}
|
|
348
756
|
function filtersEqual(a, b) {
|
|
349
757
|
const aKeys = Object.keys(a);
|
|
350
758
|
const bKeys = Object.keys(b);
|
|
@@ -361,24 +769,36 @@ function filtersEqual(a, b) {
|
|
|
361
769
|
|
|
362
770
|
// src/create-grid.ts
|
|
363
771
|
function createGrid(options) {
|
|
364
|
-
const
|
|
772
|
+
const engine = createGridCore(options);
|
|
365
773
|
return {
|
|
366
774
|
kind: "pretable-grid",
|
|
367
775
|
get options() {
|
|
368
|
-
return
|
|
776
|
+
return engine.options;
|
|
369
777
|
},
|
|
370
|
-
subscribe:
|
|
371
|
-
getSnapshot:
|
|
372
|
-
setSort:
|
|
373
|
-
setFilter:
|
|
374
|
-
clearFilters:
|
|
375
|
-
replaceFilters:
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
778
|
+
subscribe: engine.subscribe,
|
|
779
|
+
getSnapshot: engine.getSnapshot,
|
|
780
|
+
setSort: engine.setSort,
|
|
781
|
+
setFilter: engine.setFilter,
|
|
782
|
+
clearFilters: engine.clearFilters,
|
|
783
|
+
replaceFilters: engine.replaceFilters,
|
|
784
|
+
setSelection: engine.setSelection,
|
|
785
|
+
selectAll: engine.selectAll,
|
|
786
|
+
clearSelection: engine.clearSelection,
|
|
787
|
+
addRange: engine.addRange,
|
|
788
|
+
extendRangeFromAnchor: engine.extendRangeFromAnchor,
|
|
789
|
+
toggleRowSelection: engine.toggleRowSelection,
|
|
790
|
+
setSelectAllVisible: engine.setSelectAllVisible,
|
|
791
|
+
setFocus: engine.setFocus,
|
|
792
|
+
moveFocus: engine.moveFocus,
|
|
793
|
+
setViewport: engine.setViewport,
|
|
794
|
+
autosizeColumns: engine.autosizeColumns,
|
|
795
|
+
setColumnWidth: engine.setColumnWidth,
|
|
796
|
+
moveColumn: engine.moveColumn,
|
|
797
|
+
setColumnPinned: engine.setColumnPinned,
|
|
798
|
+
autosizeColumn: engine.autosizeColumn,
|
|
799
|
+
resetColumnLayout: engine.resetColumnLayout,
|
|
800
|
+
mergeColumnsFromProps: engine.mergeColumnsFromProps,
|
|
801
|
+
applyTransaction: engine.applyTransaction
|
|
382
802
|
};
|
|
383
803
|
}
|
|
384
804
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pretable/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Framework-agnostic grid state primitives for Pretable.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/cacheplane/pretable.git",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://pretable.ai/docs/grid/api-reference",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/cacheplane/pretable/issues"
|
|
14
|
+
},
|
|
4
15
|
"type": "module",
|
|
5
16
|
"files": [
|
|
6
17
|
"dist"
|
|
@@ -26,6 +37,8 @@
|
|
|
26
37
|
"@pretable-internal/grid-core": "0.0.0"
|
|
27
38
|
},
|
|
28
39
|
"scripts": {
|
|
40
|
+
"api": "api-extractor run --local --config api-extractor.json",
|
|
41
|
+
"api:check": "api-extractor run --config api-extractor.json",
|
|
29
42
|
"build": "pnpm --filter @pretable-internal/grid-core build && tsup",
|
|
30
43
|
"lint": "eslint src --ext .ts",
|
|
31
44
|
"lint:packaging": "publint --strict && attw --pack",
|