@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brian Love
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @pretable/core
|
|
2
|
+
|
|
3
|
+
The headless engine for [pretable](https://pretable.dev/). Drives sort, filter, selection, focus, viewport, and streaming-transaction state for any table-shaped UI.
|
|
4
|
+
|
|
5
|
+
## When to reach for this
|
|
6
|
+
|
|
7
|
+
Most users want **[`@pretable/react`](../react)** instead. It bundles `@pretable/core` with a React surface that handles rendering, layout, and keyboard interaction.
|
|
8
|
+
|
|
9
|
+
`@pretable/core` is for users building their own UI from scratch — for example, plain DOM, a non-React framework, or a custom canvas/webgl renderer. Headless usage is supported (the `createGrid` factory returns a fully-typed `PretableGrid` handle), but **dedicated docs, examples, and demos for headless mode are forthcoming**. If headless is what you're after, the type definitions and [`core.api.md`](./core.api.md) are the source of truth today.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @pretable/core
|
|
15
|
+
# or pnpm add @pretable/core, yarn add @pretable/core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Minimal example
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createGrid } from "@pretable/core";
|
|
22
|
+
|
|
23
|
+
const grid = createGrid({
|
|
24
|
+
columns: [
|
|
25
|
+
{ id: "name", header: "Name" },
|
|
26
|
+
{ id: "age", header: "Age", sortable: true },
|
|
27
|
+
],
|
|
28
|
+
rows: [
|
|
29
|
+
{ id: "1", name: "Ada", age: 36 },
|
|
30
|
+
{ id: "2", name: "Grace", age: 85 },
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
grid.subscribe(() => {
|
|
35
|
+
const { visibleRows, sort } = grid.getSnapshot();
|
|
36
|
+
console.log("rows:", visibleRows.length, "sort:", sort);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
grid.setSort("age", "desc");
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Full public surface
|
|
43
|
+
|
|
44
|
+
See **[`core.api.md`](./core.api.md)** for every exported type, interface, and function with their full signatures. The file is generated by [API Extractor](https://api-extractor.com/) and committed to the repo; CI fails if it drifts.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT — see [LICENSE](../../LICENSE).
|
package/dist/index.cjs
CHANGED
|
@@ -21,7 +21,7 @@ function autosizeColumns(input) {
|
|
|
21
21
|
maxContentWidth = Math.max(maxContentWidth, headerWidth);
|
|
22
22
|
}
|
|
23
23
|
for (const row of input.rows) {
|
|
24
|
-
const rawValue = column.
|
|
24
|
+
const rawValue = column.value ? column.value(row) : row[column.id];
|
|
25
25
|
const text = String(rawValue ?? "");
|
|
26
26
|
if (text.length === 0) {
|
|
27
27
|
continue;
|
|
@@ -110,10 +110,16 @@ function sortRows(rows, columns, sort) {
|
|
|
110
110
|
return indexed.map((i) => rows[i]);
|
|
111
111
|
}
|
|
112
112
|
function readCellValue(row, column) {
|
|
113
|
-
return column.
|
|
113
|
+
return column.value ? column.value(row) : row[column.id];
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
// ../grid-core/dist/create-grid-core.js
|
|
117
|
+
var ROW_SELECT_COLUMN_ID = "__pretable_row_select__";
|
|
118
|
+
function clampColumnWidth(width, column) {
|
|
119
|
+
const min = column.minWidthPx ?? 40;
|
|
120
|
+
const max = column.maxWidthPx ?? Infinity;
|
|
121
|
+
return Math.max(min, Math.min(max, width));
|
|
122
|
+
}
|
|
117
123
|
function applyAutosize(options, autosizeOptions) {
|
|
118
124
|
const result = autosizeColumns({
|
|
119
125
|
columns: options.columns,
|
|
@@ -135,6 +141,7 @@ function applyAutosize(options, autosizeOptions) {
|
|
|
135
141
|
function createGridCore(inputOptions) {
|
|
136
142
|
const listeners = /* @__PURE__ */ new Set();
|
|
137
143
|
let options = inputOptions.autosize ? applyAutosize(inputOptions, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : inputOptions;
|
|
144
|
+
let originalColumns = inputOptions.columns.map((c) => ({ ...c }));
|
|
138
145
|
let sourceRows = createSourceRows(options);
|
|
139
146
|
const sourceRowIndex = new Map(sourceRows.map((entry) => [entry.id, entry]));
|
|
140
147
|
let cachedSnapshot = null;
|
|
@@ -143,7 +150,7 @@ function createGridCore(inputOptions) {
|
|
|
143
150
|
let cachedDerivedFilters = null;
|
|
144
151
|
let sort = { columnId: null, direction: null };
|
|
145
152
|
let filters = {};
|
|
146
|
-
let selection = {
|
|
153
|
+
let selection = { ranges: [], anchor: null };
|
|
147
154
|
let focus = { rowId: null, columnId: null };
|
|
148
155
|
let viewport = {
|
|
149
156
|
scrollTop: 0,
|
|
@@ -208,35 +215,251 @@ function createGridCore(inputOptions) {
|
|
|
208
215
|
filters = normalized;
|
|
209
216
|
emit();
|
|
210
217
|
},
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
218
|
+
setSelection(next) {
|
|
219
|
+
if (selectionsEqual(selection, next)) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
selection = {
|
|
223
|
+
ranges: next.ranges.map((r) => ({ ...r })),
|
|
224
|
+
anchor: next.anchor ? { ...next.anchor } : null
|
|
225
|
+
};
|
|
226
|
+
emit();
|
|
227
|
+
},
|
|
228
|
+
selectAll() {
|
|
229
|
+
const snapshot = getSnapshot();
|
|
230
|
+
const firstRow = snapshot.visibleRows[0];
|
|
231
|
+
const lastRow = snapshot.visibleRows[snapshot.visibleRows.length - 1];
|
|
232
|
+
const firstColumn = options.columns[0];
|
|
233
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
234
|
+
if (!firstRow || !lastRow || !firstColumn || !lastColumn) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const range = {
|
|
238
|
+
startRowId: firstRow.id,
|
|
239
|
+
endRowId: lastRow.id,
|
|
240
|
+
startColumnId: firstColumn.id,
|
|
241
|
+
endColumnId: lastColumn.id
|
|
242
|
+
};
|
|
243
|
+
const anchor = {
|
|
244
|
+
rowId: firstRow.id,
|
|
245
|
+
columnId: firstColumn.id
|
|
246
|
+
};
|
|
247
|
+
const next = { ranges: [range], anchor };
|
|
248
|
+
if (selectionsEqual(selection, next)) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
selection = next;
|
|
252
|
+
emit();
|
|
253
|
+
},
|
|
254
|
+
clearSelection() {
|
|
255
|
+
const focusAddr = focus.rowId && focus.columnId ? { rowId: focus.rowId, columnId: focus.columnId } : null;
|
|
256
|
+
const next = focusAddr ? {
|
|
257
|
+
ranges: [
|
|
258
|
+
{
|
|
259
|
+
startRowId: focusAddr.rowId,
|
|
260
|
+
endRowId: focusAddr.rowId,
|
|
261
|
+
startColumnId: focusAddr.columnId,
|
|
262
|
+
endColumnId: focusAddr.columnId
|
|
263
|
+
}
|
|
264
|
+
],
|
|
265
|
+
anchor: focusAddr
|
|
266
|
+
} : { ranges: [], anchor: null };
|
|
267
|
+
if (selectionsEqual(selection, next)) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
selection = next;
|
|
271
|
+
emit();
|
|
272
|
+
},
|
|
273
|
+
addRange(range) {
|
|
274
|
+
selection = {
|
|
275
|
+
ranges: [...selection.ranges, { ...range }],
|
|
276
|
+
anchor: { rowId: range.startRowId, columnId: range.startColumnId }
|
|
277
|
+
};
|
|
278
|
+
emit();
|
|
279
|
+
},
|
|
280
|
+
extendRangeFromAnchor(addr) {
|
|
281
|
+
if (!selection.anchor) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const newActive = {
|
|
285
|
+
startRowId: selection.anchor.rowId,
|
|
286
|
+
endRowId: addr.rowId,
|
|
287
|
+
startColumnId: selection.anchor.columnId,
|
|
288
|
+
endColumnId: addr.columnId
|
|
289
|
+
};
|
|
290
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
291
|
+
selection = { ranges, anchor: selection.anchor };
|
|
292
|
+
emit();
|
|
293
|
+
},
|
|
294
|
+
toggleRowSelection(rowId) {
|
|
295
|
+
const firstColumn = options.columns[0];
|
|
296
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
297
|
+
if (!firstColumn || !lastColumn) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const fullRowRange = {
|
|
301
|
+
startRowId: rowId,
|
|
302
|
+
endRowId: rowId,
|
|
303
|
+
startColumnId: firstColumn.id,
|
|
304
|
+
endColumnId: lastColumn.id
|
|
305
|
+
};
|
|
306
|
+
const matchIndex = selection.ranges.findIndex((r) => isFullRowRange(r, rowId, firstColumn.id, lastColumn.id));
|
|
307
|
+
if (matchIndex >= 0) {
|
|
308
|
+
const ranges = selection.ranges.filter((_, i) => i !== matchIndex);
|
|
309
|
+
selection = { ranges, anchor: selection.anchor };
|
|
310
|
+
} else {
|
|
311
|
+
selection = {
|
|
312
|
+
ranges: [...selection.ranges, fullRowRange],
|
|
313
|
+
anchor: { rowId, columnId: firstColumn.id }
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
emit();
|
|
317
|
+
},
|
|
318
|
+
setSelectAllVisible(checked) {
|
|
319
|
+
const snapshot = getSnapshot();
|
|
320
|
+
const firstColumn = options.columns[0];
|
|
321
|
+
const lastColumn = options.columns[options.columns.length - 1];
|
|
322
|
+
if (!firstColumn || !lastColumn) {
|
|
214
323
|
return;
|
|
215
324
|
}
|
|
216
|
-
|
|
325
|
+
const visibleIds = new Set(snapshot.visibleRows.map((r) => r.id));
|
|
326
|
+
const nonRowRanges = selection.ranges.filter((r) => !isFullRowRange(r, r.startRowId, firstColumn.id, lastColumn.id) || !visibleIds.has(r.startRowId));
|
|
327
|
+
let next;
|
|
328
|
+
if (checked) {
|
|
329
|
+
const newRanges = snapshot.visibleRows.map((row) => ({
|
|
330
|
+
startRowId: row.id,
|
|
331
|
+
endRowId: row.id,
|
|
332
|
+
startColumnId: firstColumn.id,
|
|
333
|
+
endColumnId: lastColumn.id
|
|
334
|
+
}));
|
|
335
|
+
next = {
|
|
336
|
+
ranges: [...nonRowRanges, ...newRanges],
|
|
337
|
+
anchor: snapshot.visibleRows[0] ? { rowId: snapshot.visibleRows[0].id, columnId: firstColumn.id } : selection.anchor
|
|
338
|
+
};
|
|
339
|
+
} else {
|
|
340
|
+
next = { ranges: nonRowRanges, anchor: selection.anchor };
|
|
341
|
+
}
|
|
342
|
+
if (selectionsEqual(selection, next)) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
selection = next;
|
|
217
346
|
emit();
|
|
218
347
|
},
|
|
219
|
-
setFocus(
|
|
220
|
-
|
|
348
|
+
setFocus(addr) {
|
|
349
|
+
const nextRowId = addr?.rowId ?? null;
|
|
350
|
+
const nextColumnId = addr?.columnId ?? null;
|
|
351
|
+
if (focus.rowId === nextRowId && focus.columnId === nextColumnId) {
|
|
221
352
|
return;
|
|
222
353
|
}
|
|
223
|
-
focus = { rowId, columnId };
|
|
354
|
+
focus = { rowId: nextRowId, columnId: nextColumnId };
|
|
224
355
|
emit();
|
|
225
356
|
},
|
|
226
|
-
moveFocus(
|
|
357
|
+
moveFocus(direction, moveOptions = {}) {
|
|
227
358
|
const snapshot = getSnapshot();
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
359
|
+
const visibleRows = snapshot.visibleRows;
|
|
360
|
+
const columnList = options.columns;
|
|
361
|
+
if (visibleRows.length === 0 || columnList.length === 0) {
|
|
362
|
+
focus = { rowId: null, columnId: null };
|
|
231
363
|
emit();
|
|
232
364
|
return;
|
|
233
365
|
}
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
|
|
366
|
+
const currentRowIndex = focus.rowId ? visibleRows.findIndex((r) => r.id === focus.rowId) : -1;
|
|
367
|
+
const currentColumnIndex = focus.columnId ? columnList.findIndex((c) => c.id === focus.columnId) : -1;
|
|
368
|
+
const hasRowFocus = currentRowIndex !== -1;
|
|
369
|
+
const hasColumnFocus = currentColumnIndex !== -1;
|
|
370
|
+
const baseRowIndex = hasRowFocus ? currentRowIndex : 0;
|
|
371
|
+
const baseColumnIndex = hasColumnFocus ? currentColumnIndex : 0;
|
|
372
|
+
let nextRowIndex = baseRowIndex;
|
|
373
|
+
let nextColumnIndex = baseColumnIndex;
|
|
374
|
+
const pageStep = computePageStep(viewport, visibleRows);
|
|
375
|
+
switch (direction) {
|
|
376
|
+
case "up":
|
|
377
|
+
if (moveOptions.jumpToEdge) {
|
|
378
|
+
nextRowIndex = 0;
|
|
379
|
+
} else if (!hasRowFocus) {
|
|
380
|
+
nextRowIndex = visibleRows.length - 1;
|
|
381
|
+
} else if (moveOptions.byPage) {
|
|
382
|
+
nextRowIndex = clamp(baseRowIndex - pageStep, 0, visibleRows.length - 1);
|
|
383
|
+
} else {
|
|
384
|
+
nextRowIndex = clamp(baseRowIndex - 1, 0, visibleRows.length - 1);
|
|
385
|
+
}
|
|
386
|
+
break;
|
|
387
|
+
case "down":
|
|
388
|
+
if (moveOptions.jumpToEdge) {
|
|
389
|
+
nextRowIndex = visibleRows.length - 1;
|
|
390
|
+
} else if (!hasRowFocus) {
|
|
391
|
+
nextRowIndex = 0;
|
|
392
|
+
} else if (moveOptions.byPage) {
|
|
393
|
+
nextRowIndex = clamp(baseRowIndex + pageStep, 0, visibleRows.length - 1);
|
|
394
|
+
} else {
|
|
395
|
+
nextRowIndex = clamp(baseRowIndex + 1, 0, visibleRows.length - 1);
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
case "left":
|
|
399
|
+
if (moveOptions.jumpToEdge) {
|
|
400
|
+
nextColumnIndex = 0;
|
|
401
|
+
} else if (!hasColumnFocus) {
|
|
402
|
+
nextColumnIndex = columnList.length - 1;
|
|
403
|
+
} else {
|
|
404
|
+
nextColumnIndex = clamp(baseColumnIndex - 1, 0, columnList.length - 1);
|
|
405
|
+
}
|
|
406
|
+
break;
|
|
407
|
+
case "right":
|
|
408
|
+
if (moveOptions.jumpToEdge) {
|
|
409
|
+
nextColumnIndex = columnList.length - 1;
|
|
410
|
+
} else if (!hasColumnFocus) {
|
|
411
|
+
nextColumnIndex = 0;
|
|
412
|
+
} else {
|
|
413
|
+
nextColumnIndex = clamp(baseColumnIndex + 1, 0, columnList.length - 1);
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
const nextRow = visibleRows[nextRowIndex];
|
|
418
|
+
const nextColumn = columnList[nextColumnIndex];
|
|
419
|
+
if (!nextRow || !nextColumn) {
|
|
237
420
|
return;
|
|
238
421
|
}
|
|
239
|
-
|
|
422
|
+
const nextAddr = {
|
|
423
|
+
rowId: nextRow.id,
|
|
424
|
+
columnId: nextColumn.id
|
|
425
|
+
};
|
|
426
|
+
focus = nextAddr;
|
|
427
|
+
if (moveOptions.extend) {
|
|
428
|
+
if (!selection.anchor) {
|
|
429
|
+
selection = {
|
|
430
|
+
ranges: [
|
|
431
|
+
{
|
|
432
|
+
startRowId: nextAddr.rowId,
|
|
433
|
+
endRowId: nextAddr.rowId,
|
|
434
|
+
startColumnId: nextAddr.columnId,
|
|
435
|
+
endColumnId: nextAddr.columnId
|
|
436
|
+
}
|
|
437
|
+
],
|
|
438
|
+
anchor: nextAddr
|
|
439
|
+
};
|
|
440
|
+
} else {
|
|
441
|
+
const newActive = {
|
|
442
|
+
startRowId: selection.anchor.rowId,
|
|
443
|
+
endRowId: nextAddr.rowId,
|
|
444
|
+
startColumnId: selection.anchor.columnId,
|
|
445
|
+
endColumnId: nextAddr.columnId
|
|
446
|
+
};
|
|
447
|
+
const ranges = selection.ranges.length === 0 ? [newActive] : [...selection.ranges.slice(0, -1), newActive];
|
|
448
|
+
selection = { ranges, anchor: selection.anchor };
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
selection = {
|
|
452
|
+
ranges: [
|
|
453
|
+
{
|
|
454
|
+
startRowId: nextAddr.rowId,
|
|
455
|
+
endRowId: nextAddr.rowId,
|
|
456
|
+
startColumnId: nextAddr.columnId,
|
|
457
|
+
endColumnId: nextAddr.columnId
|
|
458
|
+
}
|
|
459
|
+
],
|
|
460
|
+
anchor: nextAddr
|
|
461
|
+
};
|
|
462
|
+
}
|
|
240
463
|
emit();
|
|
241
464
|
},
|
|
242
465
|
setViewport(nextViewport) {
|
|
@@ -254,9 +477,165 @@ function createGridCore(inputOptions) {
|
|
|
254
477
|
options = nextOptions;
|
|
255
478
|
emit();
|
|
256
479
|
},
|
|
480
|
+
setColumnWidth(columnId, width) {
|
|
481
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
485
|
+
if (idx === -1) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const column = options.columns[idx];
|
|
489
|
+
const clamped = clampColumnWidth(width, column);
|
|
490
|
+
if (column.widthPx === clamped) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const nextColumns = options.columns.slice();
|
|
494
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
495
|
+
options = { ...options, columns: nextColumns };
|
|
496
|
+
emit();
|
|
497
|
+
},
|
|
498
|
+
moveColumn(columnId, toIndex) {
|
|
499
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const fromIndex = options.columns.findIndex((c) => c.id === columnId);
|
|
503
|
+
if (fromIndex === -1) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const synthAtZero = options.columns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
507
|
+
const minIndex = synthAtZero ? 1 : 0;
|
|
508
|
+
const maxIndex = options.columns.length - 1;
|
|
509
|
+
const clampedTo = Math.max(minIndex, Math.min(maxIndex, toIndex));
|
|
510
|
+
if (fromIndex === clampedTo) {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const nextColumns = options.columns.slice();
|
|
514
|
+
const [moved] = nextColumns.splice(fromIndex, 1);
|
|
515
|
+
if (!moved) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
nextColumns.splice(clampedTo, 0, moved);
|
|
519
|
+
let boundary = synthAtZero ? 1 : 0;
|
|
520
|
+
for (let i = synthAtZero ? 1 : 0; i < nextColumns.length; i += 1) {
|
|
521
|
+
if (i === clampedTo) {
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (nextColumns[i]?.pinned === "left") {
|
|
525
|
+
boundary = i + 1;
|
|
526
|
+
} else {
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
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) {
|
|
534
|
+
nextColumns[clampedTo] = { ...moved, pinned: nextPinned };
|
|
535
|
+
}
|
|
536
|
+
options = { ...options, columns: nextColumns };
|
|
537
|
+
emit();
|
|
538
|
+
},
|
|
539
|
+
setColumnPinned(columnId, pinned) {
|
|
540
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
544
|
+
if (idx === -1) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const column = options.columns[idx];
|
|
548
|
+
const nextPinnedValue = pinned === "left" ? "left" : void 0;
|
|
549
|
+
if (column.pinned === nextPinnedValue) {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const nextColumns = options.columns.slice();
|
|
553
|
+
nextColumns.splice(idx, 1);
|
|
554
|
+
const synthAtZero = nextColumns[0]?.id === ROW_SELECT_COLUMN_ID;
|
|
555
|
+
const baseStart = synthAtZero ? 1 : 0;
|
|
556
|
+
let boundary = baseStart;
|
|
557
|
+
while (boundary < nextColumns.length && nextColumns[boundary]?.pinned === "left") {
|
|
558
|
+
boundary += 1;
|
|
559
|
+
}
|
|
560
|
+
const insertAt = boundary;
|
|
561
|
+
const nextColumn = {
|
|
562
|
+
...column,
|
|
563
|
+
pinned: nextPinnedValue
|
|
564
|
+
};
|
|
565
|
+
nextColumns.splice(insertAt, 0, nextColumn);
|
|
566
|
+
options = { ...options, columns: nextColumns };
|
|
567
|
+
emit();
|
|
568
|
+
},
|
|
569
|
+
autosizeColumn(columnId, autosizeOptions) {
|
|
570
|
+
if (columnId === ROW_SELECT_COLUMN_ID) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const idx = options.columns.findIndex((c) => c.id === columnId);
|
|
574
|
+
if (idx === -1) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const column = options.columns[idx];
|
|
578
|
+
const probeColumns = options.columns.slice();
|
|
579
|
+
probeColumns[idx] = { ...column, widthPx: void 0 };
|
|
580
|
+
const probedOptions = { ...options, columns: probeColumns };
|
|
581
|
+
const probed = applyAutosize(probedOptions, autosizeOptions);
|
|
582
|
+
const nextWidth = probed.columns[idx]?.widthPx;
|
|
583
|
+
if (nextWidth === void 0 || nextWidth === column.widthPx) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const clamped = clampColumnWidth(nextWidth, column);
|
|
587
|
+
const nextColumns = options.columns.slice();
|
|
588
|
+
nextColumns[idx] = { ...column, widthPx: clamped };
|
|
589
|
+
options = { ...options, columns: nextColumns };
|
|
590
|
+
emit();
|
|
591
|
+
},
|
|
592
|
+
resetColumnLayout() {
|
|
593
|
+
const restored = inputOptions.autosize ? applyAutosize({
|
|
594
|
+
...inputOptions,
|
|
595
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
596
|
+
}, typeof inputOptions.autosize === "object" ? inputOptions.autosize : void 0) : {
|
|
597
|
+
...inputOptions,
|
|
598
|
+
columns: originalColumns.map((c) => ({ ...c }))
|
|
599
|
+
};
|
|
600
|
+
const current = options.columns;
|
|
601
|
+
const next = restored.columns;
|
|
602
|
+
if (current.length === next.length) {
|
|
603
|
+
let same = true;
|
|
604
|
+
for (let i = 0; i < current.length; i += 1) {
|
|
605
|
+
const c = current[i];
|
|
606
|
+
const n = next[i];
|
|
607
|
+
if (c.id !== n.id || c.widthPx !== n.widthPx || c.pinned !== n.pinned) {
|
|
608
|
+
same = false;
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (same) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
options = { ...options, columns: next };
|
|
617
|
+
emit();
|
|
618
|
+
},
|
|
619
|
+
mergeColumnsFromProps(nextColumns) {
|
|
620
|
+
const currentById = new Map(options.columns.map((c) => [c.id, c]));
|
|
621
|
+
const merged = nextColumns.map((newCol) => {
|
|
622
|
+
const existing = currentById.get(newCol.id);
|
|
623
|
+
if (existing) {
|
|
624
|
+
return {
|
|
625
|
+
...newCol,
|
|
626
|
+
widthPx: existing.widthPx ?? newCol.widthPx,
|
|
627
|
+
pinned: existing.pinned ?? newCol.pinned
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
return { ...newCol };
|
|
631
|
+
});
|
|
632
|
+
originalColumns = nextColumns.map((c) => ({ ...c }));
|
|
633
|
+
options = { ...options, columns: merged };
|
|
634
|
+
emit();
|
|
635
|
+
},
|
|
257
636
|
applyTransaction(transaction) {
|
|
258
637
|
if (!options.getRowId) {
|
|
259
|
-
throw new Error("applyTransaction requires getRowId on
|
|
638
|
+
throw new Error("applyTransaction requires getRowId on PretableGridOptions");
|
|
260
639
|
}
|
|
261
640
|
const getRowId = options.getRowId;
|
|
262
641
|
if (transaction.remove) {
|
|
@@ -324,8 +703,8 @@ function createGridCore(inputOptions) {
|
|
|
324
703
|
sort,
|
|
325
704
|
filters: { ...filters },
|
|
326
705
|
selection: {
|
|
327
|
-
|
|
328
|
-
|
|
706
|
+
ranges: selection.ranges.map((r) => ({ ...r })),
|
|
707
|
+
anchor: selection.anchor ? { ...selection.anchor } : null
|
|
329
708
|
},
|
|
330
709
|
focus,
|
|
331
710
|
totalRowCount: sourceRows.length,
|
|
@@ -347,6 +726,35 @@ function createGridCore(inputOptions) {
|
|
|
347
726
|
function clamp(value, min, max) {
|
|
348
727
|
return Math.max(min, Math.min(max, value));
|
|
349
728
|
}
|
|
729
|
+
function computePageStep(viewport, visibleRows) {
|
|
730
|
+
if (viewport.height <= 0 || visibleRows.length === 0) {
|
|
731
|
+
return 1;
|
|
732
|
+
}
|
|
733
|
+
const estimatedRowsPerPage = Math.max(1, Math.floor(viewport.height * 0.8 / 32));
|
|
734
|
+
return Math.min(estimatedRowsPerPage, visibleRows.length);
|
|
735
|
+
}
|
|
736
|
+
function isFullRowRange(range, rowId, firstColumnId, lastColumnId) {
|
|
737
|
+
return range.startRowId === rowId && range.endRowId === rowId && range.startColumnId === firstColumnId && range.endColumnId === lastColumnId;
|
|
738
|
+
}
|
|
739
|
+
function selectionsEqual(a, b) {
|
|
740
|
+
if (a.ranges.length !== b.ranges.length) {
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
for (let i = 0; i < a.ranges.length; i += 1) {
|
|
744
|
+
const ar = a.ranges[i];
|
|
745
|
+
const br = b.ranges[i];
|
|
746
|
+
if (ar.startRowId !== br.startRowId || ar.endRowId !== br.endRowId || ar.startColumnId !== br.startColumnId || ar.endColumnId !== br.endColumnId) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (a.anchor === null && b.anchor === null) {
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
if (a.anchor === null || b.anchor === null) {
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
return a.anchor.rowId === b.anchor.rowId && a.anchor.columnId === b.anchor.columnId;
|
|
757
|
+
}
|
|
350
758
|
function filtersEqual(a, b) {
|
|
351
759
|
const aKeys = Object.keys(a);
|
|
352
760
|
const bKeys = Object.keys(b);
|
|
@@ -363,24 +771,36 @@ function filtersEqual(a, b) {
|
|
|
363
771
|
|
|
364
772
|
// src/create-grid.ts
|
|
365
773
|
function createGrid(options) {
|
|
366
|
-
const
|
|
774
|
+
const engine = createGridCore(options);
|
|
367
775
|
return {
|
|
368
776
|
kind: "pretable-grid",
|
|
369
777
|
get options() {
|
|
370
|
-
return
|
|
778
|
+
return engine.options;
|
|
371
779
|
},
|
|
372
|
-
subscribe:
|
|
373
|
-
getSnapshot:
|
|
374
|
-
setSort:
|
|
375
|
-
setFilter:
|
|
376
|
-
clearFilters:
|
|
377
|
-
replaceFilters:
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
780
|
+
subscribe: engine.subscribe,
|
|
781
|
+
getSnapshot: engine.getSnapshot,
|
|
782
|
+
setSort: engine.setSort,
|
|
783
|
+
setFilter: engine.setFilter,
|
|
784
|
+
clearFilters: engine.clearFilters,
|
|
785
|
+
replaceFilters: engine.replaceFilters,
|
|
786
|
+
setSelection: engine.setSelection,
|
|
787
|
+
selectAll: engine.selectAll,
|
|
788
|
+
clearSelection: engine.clearSelection,
|
|
789
|
+
addRange: engine.addRange,
|
|
790
|
+
extendRangeFromAnchor: engine.extendRangeFromAnchor,
|
|
791
|
+
toggleRowSelection: engine.toggleRowSelection,
|
|
792
|
+
setSelectAllVisible: engine.setSelectAllVisible,
|
|
793
|
+
setFocus: engine.setFocus,
|
|
794
|
+
moveFocus: engine.moveFocus,
|
|
795
|
+
setViewport: engine.setViewport,
|
|
796
|
+
autosizeColumns: engine.autosizeColumns,
|
|
797
|
+
setColumnWidth: engine.setColumnWidth,
|
|
798
|
+
moveColumn: engine.moveColumn,
|
|
799
|
+
setColumnPinned: engine.setColumnPinned,
|
|
800
|
+
autosizeColumn: engine.autosizeColumn,
|
|
801
|
+
resetColumnLayout: engine.resetColumnLayout,
|
|
802
|
+
mergeColumnsFromProps: engine.mergeColumnsFromProps,
|
|
803
|
+
applyTransaction: engine.applyTransaction
|
|
384
804
|
};
|
|
385
805
|
}
|
|
386
806
|
|