foldkit-data-grid 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clipboard.d.ts +44 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +109 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/columnState.d.ts +53 -0
- package/dist/columnState.d.ts.map +1 -0
- package/dist/columnState.js +201 -0
- package/dist/columnState.js.map +1 -0
- package/dist/{rows-D8siEVqF.d.mts → columns.d.ts} +15 -81
- package/dist/columns.d.ts.map +1 -0
- package/dist/columns.js +73 -0
- package/dist/columns.js.map +1 -0
- package/dist/{crud.d.mts → crud.d.ts} +3 -2
- package/dist/{crud.d.mts.map → crud.d.ts.map} +1 -1
- package/dist/{crud.mjs → crud.js} +3 -2
- package/dist/crud.js.map +1 -0
- package/dist/fill.d.ts +49 -0
- package/dist/fill.d.ts.map +1 -0
- package/dist/fill.js +113 -0
- package/dist/fill.js.map +1 -0
- package/dist/focus.d.ts +119 -0
- package/dist/focus.d.ts.map +1 -0
- package/dist/focus.js +150 -0
- package/dist/focus.js.map +1 -0
- package/dist/{index.d.mts → grid.d.ts} +13 -564
- package/dist/grid.d.ts.map +1 -0
- package/dist/{index.mjs → grid.js} +11 -1024
- package/dist/grid.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/layout.d.ts +20 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +27 -0
- package/dist/layout.js.map +1 -0
- package/dist/projection.d.ts +87 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +102 -0
- package/dist/projection.js.map +1 -0
- package/dist/rows.d.ts +70 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/{rows-Dy1HPpGX.mjs → rows.js} +3 -71
- package/dist/rows.js.map +1 -0
- package/dist/selection.d.ts +65 -0
- package/dist/selection.d.ts.map +1 -0
- package/dist/selection.js +137 -0
- package/dist/selection.js.map +1 -0
- package/dist/viewport.d.ts +74 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +116 -0
- package/dist/viewport.js.map +1 -0
- package/dist/virtual.d.ts +73 -0
- package/dist/virtual.d.ts.map +1 -0
- package/dist/virtual.js +112 -0
- package/dist/virtual.js.map +1 -0
- package/package.json +15 -15
- package/dist/crud.mjs.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/rows-D8siEVqF.d.mts.map +0 -1
- package/dist/rows-Dy1HPpGX.mjs.map +0 -1
|
@@ -1,1029 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Equal, Option, Result, Schema } from "effect";
|
|
2
|
+
import { CellEditor, Columns, editorOf } from "./columns.js";
|
|
3
3
|
import { defineTaggedUnion } from "foldkit/schema";
|
|
4
4
|
import { defineMessageUnion } from "foldkit/message";
|
|
5
5
|
import { modifyFields } from "foldkit/struct";
|
|
6
6
|
import { Bundle } from "foldkit-bundle";
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* Tab-separated text as rows of cells. Quoted cells may hold tabs, line
|
|
15
|
-
* breaks and doubled quotes; `\r\n` and `\r` end a row as `\n` does; the line
|
|
16
|
-
* break a spreadsheet adds after the last row makes no empty row.
|
|
17
|
-
*/
|
|
18
|
-
const parseTsv = (text) => {
|
|
19
|
-
const rows = [];
|
|
20
|
-
let row = [];
|
|
21
|
-
let cell = "";
|
|
22
|
-
let quoted = false;
|
|
23
|
-
let index = 0;
|
|
24
|
-
const endCell = () => {
|
|
25
|
-
row.push(cell);
|
|
26
|
-
cell = "";
|
|
27
|
-
};
|
|
28
|
-
const endRow = () => {
|
|
29
|
-
endCell();
|
|
30
|
-
rows.push(row);
|
|
31
|
-
row = [];
|
|
32
|
-
};
|
|
33
|
-
while (index < text.length) {
|
|
34
|
-
const char = text[index];
|
|
35
|
-
if (quoted) {
|
|
36
|
-
if (char === "\"" && text[index + 1] === "\"") {
|
|
37
|
-
cell += "\"";
|
|
38
|
-
index += 2;
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
if (char === "\"") quoted = false;
|
|
42
|
-
else cell += char;
|
|
43
|
-
index += 1;
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
if (char === "\"" && cell === "") quoted = true;
|
|
47
|
-
else if (char === " ") endCell();
|
|
48
|
-
else if (char === "\r" || char === "\n") {
|
|
49
|
-
endRow();
|
|
50
|
-
if (char === "\r" && text[index + 1] === "\n") index += 1;
|
|
51
|
-
} else cell += char;
|
|
52
|
-
index += 1;
|
|
53
|
-
}
|
|
54
|
-
if (cell !== "" || row.length > 0) endRow();
|
|
55
|
-
return rows;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* The text of the cells a box covers, row by row in the projection's order
|
|
59
|
-
* and column by column in display order. A row counted but not loaded copies
|
|
60
|
-
* as empty cells, so the shape is the box's.
|
|
61
|
-
*/
|
|
62
|
-
const copy = (projection, box, textOf) => {
|
|
63
|
-
const rows = [];
|
|
64
|
-
for (let index = box.rows.start; index < box.rows.end; index++) {
|
|
65
|
-
const key = projection.rows.keyAt(index);
|
|
66
|
-
rows.push(box.columns.map((column) => Option.match(key, {
|
|
67
|
-
onNone: () => "",
|
|
68
|
-
onSome: (row) => textOf({
|
|
69
|
-
row,
|
|
70
|
-
column
|
|
71
|
-
})
|
|
72
|
-
})));
|
|
73
|
-
}
|
|
74
|
-
return toTsv(rows);
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Where pasted cells land: the matrix laid from `anchor` across rows in the
|
|
78
|
-
* projection's order and columns in display order. Cells past the grid's
|
|
79
|
-
* edges are dropped, as are those landing on a column that does not edit or
|
|
80
|
-
* a row not loaded; the rest keep their place, each with `from`, the text
|
|
81
|
-
* the cell showed (the text an edit of it would begin from).
|
|
82
|
-
*/
|
|
83
|
-
const pasteAt = (projection, anchor, matrix, cells) => Option.match(projection.positionOf(anchor), {
|
|
84
|
-
onNone: () => [],
|
|
85
|
-
onSome: (start) => {
|
|
86
|
-
const landed = [];
|
|
87
|
-
matrix.forEach((texts, down) => {
|
|
88
|
-
const key = projection.rows.keyAt(start.row + down);
|
|
89
|
-
if (Option.isNone(key)) return;
|
|
90
|
-
const row = key.value;
|
|
91
|
-
texts.forEach((text, across) => {
|
|
92
|
-
const column = projection.columns[start.column + across];
|
|
93
|
-
if (column !== void 0 && cells.editable(column)) landed.push({
|
|
94
|
-
row,
|
|
95
|
-
column,
|
|
96
|
-
text,
|
|
97
|
-
from: cells.from({
|
|
98
|
-
row,
|
|
99
|
-
column
|
|
100
|
-
})
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
return landed;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
const Clipboard = {
|
|
108
|
-
toTsv,
|
|
109
|
-
parseTsv,
|
|
110
|
-
copy,
|
|
111
|
-
pasteAt
|
|
112
|
-
};
|
|
113
|
-
// src/fill.ts
|
|
114
|
-
const number = /^-?\d+(?:\.(\d+))?$/;
|
|
115
|
-
/**
|
|
116
|
-
* The texts that carry `source` on for `count` cells, the first of them next
|
|
117
|
-
* to it. Two or more numbers a constant step apart continue their step, with
|
|
118
|
-
* as many decimals as the most precise of them; anything else repeats, in
|
|
119
|
-
* order. `backward` carries the source on before its first text, as filling
|
|
120
|
-
* up or left does.
|
|
121
|
-
*/
|
|
122
|
-
const series = (source, count, backward) => {
|
|
123
|
-
const at = (index) => source[(index % source.length + source.length) % source.length];
|
|
124
|
-
const repeated = Array.from({ length: count }, (_, index) => backward ? at(-1 - index) : at(source.length + index));
|
|
125
|
-
if (source.length < 2) return repeated;
|
|
126
|
-
const parsed = source.map((text) => number.exec(text.trim()));
|
|
127
|
-
if (parsed.some((match) => match === null)) return repeated;
|
|
128
|
-
const decimals = Math.max(...parsed.map((match) => match[1]?.length ?? 0));
|
|
129
|
-
const scale = 10 ** decimals;
|
|
130
|
-
const units = source.map((text) => Math.round(Number(text.trim()) * scale));
|
|
131
|
-
const step = units[1] - units[0];
|
|
132
|
-
if (units.some((unit, index) => index > 0 && unit - units[index - 1] !== step)) return repeated;
|
|
133
|
-
const from = backward ? units[0] : units[units.length - 1];
|
|
134
|
-
return Array.from({ length: count }, (_, index) => ((from + (backward ? -step : step) * (index + 1)) / scale).toFixed(decimals));
|
|
135
|
-
};
|
|
136
|
-
/** The source's box and the box beyond it toward `to`; none when `to` is inside it. */
|
|
137
|
-
const plan = (projection, request) => Option.flatMap(projection.box(request.source.anchor, request.source.focus), (source) => Option.flatMap(projection.positionOf(request.to), (to) => {
|
|
138
|
-
const first = projection.columns.indexOf(source.columns[0]);
|
|
139
|
-
const last = first + source.columns.length - 1;
|
|
140
|
-
const below = to.row - (source.rows.end - 1);
|
|
141
|
-
const above = source.rows.start - to.row;
|
|
142
|
-
const right = to.column - last;
|
|
143
|
-
const left = first - to.column;
|
|
144
|
-
const rows = Math.max(below, above);
|
|
145
|
-
const columns = Math.max(right, left);
|
|
146
|
-
if (rows <= 0 && columns <= 0) return Option.none();
|
|
147
|
-
if (rows >= columns) {
|
|
148
|
-
const target = below > 0 ? {
|
|
149
|
-
start: source.rows.end,
|
|
150
|
-
end: to.row + 1
|
|
151
|
-
} : {
|
|
152
|
-
start: to.row,
|
|
153
|
-
end: source.rows.start
|
|
154
|
-
};
|
|
155
|
-
return Option.some({
|
|
156
|
-
source,
|
|
157
|
-
target: {
|
|
158
|
-
rows: target,
|
|
159
|
-
columns: source.columns
|
|
160
|
-
},
|
|
161
|
-
toward: below > 0 ? "down" : "up"
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
const columnsOf = (start, end) => projection.columns.slice(start, end);
|
|
165
|
-
return Option.some({
|
|
166
|
-
source,
|
|
167
|
-
target: {
|
|
168
|
-
rows: source.rows,
|
|
169
|
-
columns: right > 0 ? columnsOf(last + 1, to.column + 1) : columnsOf(to.column, first)
|
|
170
|
-
},
|
|
171
|
-
toward: right > 0 ? "right" : "left"
|
|
172
|
-
});
|
|
173
|
-
}));
|
|
174
|
-
/**
|
|
175
|
-
* The cells a fill writes: each lane of the source (a column for a fill down
|
|
176
|
-
* or up, a row for one right or left) carried on through the target by
|
|
177
|
-
* `series`. Cells on a column that does not edit, or on a row not loaded, are
|
|
178
|
-
* dropped; each kept cell has `from`, the text it showed.
|
|
179
|
-
*/
|
|
180
|
-
const cells = (projection, fill, options) => {
|
|
181
|
-
const rowsOf = (box) => Array.from({ length: box.end - box.start }, (_, offset) => box.start + offset);
|
|
182
|
-
const keyAt = (index) => projection.rows.keyAt(index);
|
|
183
|
-
const textAt = (index, column) => Option.match(keyAt(index), {
|
|
184
|
-
onNone: () => "",
|
|
185
|
-
onSome: (row) => options.from({
|
|
186
|
-
row,
|
|
187
|
-
column
|
|
188
|
-
})
|
|
189
|
-
});
|
|
190
|
-
const cell = (index, column, text) => options.editable(column) ? Option.match(keyAt(index), {
|
|
191
|
-
onNone: () => [],
|
|
192
|
-
onSome: (row) => [{
|
|
193
|
-
row,
|
|
194
|
-
column,
|
|
195
|
-
text,
|
|
196
|
-
from: options.from({
|
|
197
|
-
row,
|
|
198
|
-
column
|
|
199
|
-
})
|
|
200
|
-
}]
|
|
201
|
-
}) : [];
|
|
202
|
-
const backward = fill.toward === "up" || fill.toward === "left";
|
|
203
|
-
const away = (items) => backward ? [...items].reverse() : items;
|
|
204
|
-
if (fill.toward === "down" || fill.toward === "up") {
|
|
205
|
-
const targetRows = away(rowsOf(fill.target.rows));
|
|
206
|
-
return fill.source.columns.flatMap((column) => {
|
|
207
|
-
const texts = series(rowsOf(fill.source.rows).map((index) => textAt(index, column)), targetRows.length, backward);
|
|
208
|
-
return targetRows.flatMap((index, at) => cell(index, column, texts[at]));
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
const targetColumns = away(fill.target.columns);
|
|
212
|
-
return rowsOf(fill.source.rows).flatMap((index) => {
|
|
213
|
-
const texts = series(fill.source.columns.map((column) => textAt(index, column)), targetColumns.length, backward);
|
|
214
|
-
return targetColumns.flatMap((column, at) => cell(index, column, texts[at]));
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
const Fill = {
|
|
218
|
-
series,
|
|
219
|
-
plan,
|
|
220
|
-
cells
|
|
221
|
-
};
|
|
222
|
-
// src/layout.ts
|
|
223
|
-
const ColumnLayout = {
|
|
224
|
-
/** The layout the columns declare: definition order, each in its `pinned` region. */
|
|
225
|
-
initial: (columns) => {
|
|
226
|
-
const start = [];
|
|
227
|
-
const center = [];
|
|
228
|
-
const end = [];
|
|
229
|
-
const hidden = [];
|
|
230
|
-
const pinned = {
|
|
231
|
-
start,
|
|
232
|
-
end
|
|
233
|
-
};
|
|
234
|
-
for (const id of columns.ids) {
|
|
235
|
-
const column = columns.byId[id];
|
|
236
|
-
(column.pinned === void 0 ? center : pinned[column.pinned]).push(id);
|
|
237
|
-
if (column.hidden === true) hidden.push(id);
|
|
238
|
-
}
|
|
239
|
-
return {
|
|
240
|
-
start,
|
|
241
|
-
center,
|
|
242
|
-
end,
|
|
243
|
-
hidden
|
|
244
|
-
};
|
|
245
|
-
} };
|
|
246
|
-
// src/columnState.ts
|
|
247
|
-
const regions = [
|
|
248
|
-
"start",
|
|
249
|
-
"center",
|
|
250
|
-
"end"
|
|
251
|
-
];
|
|
252
|
-
const Saved = Schema.Struct({
|
|
253
|
-
start: Schema.Array(Schema.String),
|
|
254
|
-
center: Schema.Array(Schema.String),
|
|
255
|
-
end: Schema.Array(Schema.String),
|
|
256
|
-
hidden: Schema.Array(Schema.String),
|
|
257
|
-
widths: Schema.Array(Schema.Struct({
|
|
258
|
-
column: Schema.String,
|
|
259
|
-
width: Schema.Number
|
|
260
|
-
}))
|
|
261
|
-
});
|
|
262
|
-
const without = (ids, id) => ids.filter((each) => each !== id);
|
|
263
|
-
/**
|
|
264
|
-
* Column state for one grid's columns: its Schema, which decodes only these
|
|
265
|
-
* columns' ids, and the operations a grid's Messages run. Every operation
|
|
266
|
-
* returns the state it was given when it changes nothing, so a no-op draws
|
|
267
|
-
* nothing.
|
|
268
|
-
*/
|
|
269
|
-
const make$2 = (columns) => {
|
|
270
|
-
const Ids = Schema.Literals(columns.ids);
|
|
271
|
-
const Model = Schema.Struct({
|
|
272
|
-
start: Schema.Array(Ids),
|
|
273
|
-
center: Schema.Array(Ids),
|
|
274
|
-
end: Schema.Array(Ids),
|
|
275
|
-
hidden: Schema.Array(Ids),
|
|
276
|
-
widths: Schema.Array(Schema.Struct({
|
|
277
|
-
column: Ids,
|
|
278
|
-
width: Schema.Number
|
|
279
|
-
}))
|
|
280
|
-
});
|
|
281
|
-
const spec = (id) => columns.byId[id];
|
|
282
|
-
const isColumn = (id) => Object.hasOwn(columns.byId, id);
|
|
283
|
-
const clampWidth = (id, width) => {
|
|
284
|
-
const { minWidth = Columns.minWidth, maxWidth = Number.POSITIVE_INFINITY } = spec(id);
|
|
285
|
-
return Math.min(Math.max(width, minWidth), maxWidth);
|
|
286
|
-
};
|
|
287
|
-
const initial = () => ({
|
|
288
|
-
...ColumnLayout.initial(columns),
|
|
289
|
-
widths: []
|
|
290
|
-
});
|
|
291
|
-
/** The width each column is drawn at: its resize, else its spec's. Call once per state. */
|
|
292
|
-
const widthOf = (state) => {
|
|
293
|
-
const resized = new Map(state.widths.map((entry) => [entry.column, entry.width]));
|
|
294
|
-
return (id) => resized.get(id) ?? spec(id).width ?? Columns.defaultWidth;
|
|
295
|
-
};
|
|
296
|
-
const regionOf = (state, id) => regions.find((region) => state[region].includes(id)) ?? "center";
|
|
297
|
-
/** Resizes a resizable column, within its `minWidth` and `maxWidth`. */
|
|
298
|
-
const resize = (state, id, width) => {
|
|
299
|
-
if (spec(id).resizable === false || !Number.isFinite(width)) return state;
|
|
300
|
-
const clamped = clampWidth(id, width);
|
|
301
|
-
if (clamped === widthOf(state)(id)) return state;
|
|
302
|
-
return {
|
|
303
|
-
...state,
|
|
304
|
-
widths: [...state.widths.filter((entry) => entry.column !== id), {
|
|
305
|
-
column: id,
|
|
306
|
-
width: clamped
|
|
307
|
-
}]
|
|
308
|
-
};
|
|
309
|
-
};
|
|
310
|
-
const visibleCount = (state) => {
|
|
311
|
-
const hidden = new Set(state.hidden);
|
|
312
|
-
return regions.reduce((count, region) => count + state[region].filter((id) => !hidden.has(id)).length, 0);
|
|
313
|
-
};
|
|
314
|
-
/** Hides a hideable column; the last one shown stays, so the grid keeps a cell to focus. */
|
|
315
|
-
const hide = (state, id) => {
|
|
316
|
-
if (spec(id).hideable === false || state.hidden.includes(id) || visibleCount(state) <= 1) return state;
|
|
317
|
-
return {
|
|
318
|
-
...state,
|
|
319
|
-
hidden: [...state.hidden, id]
|
|
320
|
-
};
|
|
321
|
-
};
|
|
322
|
-
const show = (state, id) => state.hidden.includes(id) ? {
|
|
323
|
-
...state,
|
|
324
|
-
hidden: without(state.hidden, id)
|
|
325
|
-
} : state;
|
|
326
|
-
/**
|
|
327
|
-
* Moves a column to `index` among the columns of `region`, counted with it
|
|
328
|
-
* removed: a reorder within its region, or a pin or an unpin across them.
|
|
329
|
-
*/
|
|
330
|
-
const move = (state, id, region, index) => {
|
|
331
|
-
const from = regionOf(state, id);
|
|
332
|
-
const target = without(state[region], id);
|
|
333
|
-
const at = Math.min(Math.max(0, Math.trunc(index)), target.length);
|
|
334
|
-
if (from === region && state[region].indexOf(id) === at) return state;
|
|
335
|
-
const placed = [
|
|
336
|
-
...target.slice(0, at),
|
|
337
|
-
id,
|
|
338
|
-
...target.slice(at)
|
|
339
|
-
];
|
|
340
|
-
return {
|
|
341
|
-
...state,
|
|
342
|
-
[from]: without(state[from], id),
|
|
343
|
-
[region]: placed
|
|
344
|
-
};
|
|
345
|
-
};
|
|
346
|
-
/**
|
|
347
|
-
* Where a column dragged `delta` pixels toward its region's end lands, as
|
|
348
|
-
* the index `move` takes in that region: past the middle of each shown
|
|
349
|
-
* neighbour it crossed. A column that crosses none stays where it is, so
|
|
350
|
-
* hidden columns keep their places; so do a hidden column and a delta that
|
|
351
|
-
* is not a number.
|
|
352
|
-
*/
|
|
353
|
-
const dropAt = (state, id, delta) => {
|
|
354
|
-
const order = state[regionOf(state, id)];
|
|
355
|
-
const here = order.indexOf(id);
|
|
356
|
-
const hidden = new Set(state.hidden);
|
|
357
|
-
if (hidden.has(id) || !Number.isFinite(delta)) return here;
|
|
358
|
-
const width = widthOf(state);
|
|
359
|
-
const shown = [];
|
|
360
|
-
let dragged = 0;
|
|
361
|
-
let before = 0;
|
|
362
|
-
let edge = 0;
|
|
363
|
-
for (const each of order) {
|
|
364
|
-
if (hidden.has(each)) continue;
|
|
365
|
-
const middle = edge + width(each) / 2;
|
|
366
|
-
if (each === id) {
|
|
367
|
-
dragged = middle + delta;
|
|
368
|
-
before = shown.length;
|
|
369
|
-
} else shown.push({
|
|
370
|
-
id: each,
|
|
371
|
-
middle
|
|
372
|
-
});
|
|
373
|
-
edge += width(each);
|
|
374
|
-
}
|
|
375
|
-
const passed = shown.filter((other) => other.middle <= dragged).length;
|
|
376
|
-
if (passed === before) return here;
|
|
377
|
-
const others = without(order, id);
|
|
378
|
-
return passed < shown.length ? others.indexOf(shown[passed].id) : others.indexOf(shown[passed - 1].id) + 1;
|
|
379
|
-
};
|
|
380
|
-
/**
|
|
381
|
-
* A saved state read back leniently: an id these columns no longer define,
|
|
382
|
-
* or one named twice, is dropped and listed; a column the save predates
|
|
383
|
-
* takes its initial place; a width is clamped to its column's limits.
|
|
384
|
-
* Input that is not a saved state at all is the initial state, with nothing
|
|
385
|
-
* dropped, since nothing was read.
|
|
386
|
-
*/
|
|
387
|
-
const restore = (saved) => Option.match(Schema.decodeUnknownOption(Saved)(saved), {
|
|
388
|
-
onNone: () => ({
|
|
389
|
-
state: initial(),
|
|
390
|
-
dropped: []
|
|
391
|
-
}),
|
|
392
|
-
onSome: (value) => {
|
|
393
|
-
const dropped = [];
|
|
394
|
-
const saved = /* @__PURE__ */ new Set();
|
|
395
|
-
const keep = (ids) => ids.filter((id) => {
|
|
396
|
-
if (!isColumn(id) || saved.has(id)) {
|
|
397
|
-
dropped.push(id);
|
|
398
|
-
return false;
|
|
399
|
-
}
|
|
400
|
-
saved.add(id);
|
|
401
|
-
return true;
|
|
402
|
-
});
|
|
403
|
-
const placed = {
|
|
404
|
-
start: keep(value.start),
|
|
405
|
-
center: keep(value.center),
|
|
406
|
-
end: keep(value.end)
|
|
407
|
-
};
|
|
408
|
-
const fresh = initial();
|
|
409
|
-
for (const region of regions) for (const id of fresh[region]) if (!saved.has(id)) placed[region].push(id);
|
|
410
|
-
const hidden = new Set(fresh.hidden.filter((id) => !saved.has(id)));
|
|
411
|
-
for (const id of value.hidden) if (isColumn(id)) hidden.add(id);
|
|
412
|
-
const widths = /* @__PURE__ */ new Map();
|
|
413
|
-
for (const entry of value.widths) if (isColumn(entry.column) && Number.isFinite(entry.width)) widths.set(entry.column, clampWidth(entry.column, entry.width));
|
|
414
|
-
return {
|
|
415
|
-
state: {
|
|
416
|
-
...placed,
|
|
417
|
-
hidden: [...hidden],
|
|
418
|
-
widths: [...widths].map(([column, width]) => ({
|
|
419
|
-
column,
|
|
420
|
-
width
|
|
421
|
-
}))
|
|
422
|
-
},
|
|
423
|
-
dropped
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
return {
|
|
428
|
-
Model,
|
|
429
|
-
initial,
|
|
430
|
-
widthOf,
|
|
431
|
-
regionOf,
|
|
432
|
-
resize,
|
|
433
|
-
hide,
|
|
434
|
-
show,
|
|
435
|
-
move,
|
|
436
|
-
dropAt,
|
|
437
|
-
restore
|
|
438
|
-
};
|
|
439
|
-
};
|
|
440
|
-
const ColumnState = { make: make$2 };
|
|
441
|
-
// src/projection.ts
|
|
442
|
-
const CellAddress = {
|
|
443
|
-
/**
|
|
444
|
-
* An address as stored or sent: the row key as text, and the column as one
|
|
445
|
-
* of these columns' ids, so a saved address naming a removed column fails
|
|
446
|
-
* to decode instead of naming nothing.
|
|
447
|
-
*/
|
|
448
|
-
schema: (columns) => Schema.Struct({
|
|
449
|
-
row: Schema.String,
|
|
450
|
-
column: Schema.Literals(columns.ids)
|
|
451
|
-
}) };
|
|
452
|
-
const clamp = (value, last) => Math.min(Math.max(value, 0), last);
|
|
453
|
-
const GridProjection = {
|
|
454
|
-
/**
|
|
455
|
-
* Projects rows through a column layout. A layout saved before a column
|
|
456
|
-
* was added does not mention it, so the column joins the center region in
|
|
457
|
-
* definition order; an id the columns do not define, or one named twice,
|
|
458
|
-
* keeps only its first place.
|
|
459
|
-
*/
|
|
460
|
-
make: (options) => {
|
|
461
|
-
const { rows, columns, layout } = options;
|
|
462
|
-
const placed = /* @__PURE__ */ new Set();
|
|
463
|
-
const place = (ids) => ids.filter((id) => {
|
|
464
|
-
if (!Object.hasOwn(columns.byId, id) || placed.has(id)) return false;
|
|
465
|
-
placed.add(id);
|
|
466
|
-
return true;
|
|
467
|
-
});
|
|
468
|
-
const startPlaced = place(layout.start);
|
|
469
|
-
const centerPlaced = place(layout.center);
|
|
470
|
-
const endPlaced = place(layout.end);
|
|
471
|
-
centerPlaced.push(...columns.ids.filter((id) => !placed.has(id)));
|
|
472
|
-
const hidden = new Set(layout.hidden);
|
|
473
|
-
const shown = (ids) => ids.filter((id) => !hidden.has(id));
|
|
474
|
-
const start = shown(startPlaced);
|
|
475
|
-
const center = shown(centerPlaced);
|
|
476
|
-
const end = shown(endPlaced);
|
|
477
|
-
const visible = [
|
|
478
|
-
...start,
|
|
479
|
-
...center,
|
|
480
|
-
...end
|
|
481
|
-
];
|
|
482
|
-
const indexes = new Map(visible.map((id, index) => [id, index]));
|
|
483
|
-
const lastRow = addressableRows(rows.count) - 1;
|
|
484
|
-
const lastColumn = visible.length - 1;
|
|
485
|
-
const cellAt = (position) => {
|
|
486
|
-
const column = visible[position.column];
|
|
487
|
-
if (column === void 0) return Option.none();
|
|
488
|
-
return Option.map(rows.keyAt(position.row), (row) => ({
|
|
489
|
-
row,
|
|
490
|
-
column
|
|
491
|
-
}));
|
|
492
|
-
};
|
|
493
|
-
const positionOf = (address) => Option.flatMap(Option.fromUndefinedOr(indexes.get(address.column)), (column) => Option.map(rows.indexOf(address.row), (row) => ({
|
|
494
|
-
row,
|
|
495
|
-
column
|
|
496
|
-
})));
|
|
497
|
-
const within = (address, to) => Option.flatMap(positionOf(address), (position) => cellAt(to(position)));
|
|
498
|
-
return {
|
|
499
|
-
rows,
|
|
500
|
-
rowCount: rows.count,
|
|
501
|
-
columns: visible,
|
|
502
|
-
start,
|
|
503
|
-
center,
|
|
504
|
-
end,
|
|
505
|
-
columnIndex: (id) => Option.fromUndefinedOr(indexes.get(id)),
|
|
506
|
-
rowIndex: (key) => rows.indexOf(key),
|
|
507
|
-
cellAt,
|
|
508
|
-
positionOf,
|
|
509
|
-
moveBy: (address, offset) => within(address, ({ row, column }) => ({
|
|
510
|
-
row: clamp(row + Math.trunc(offset.rows ?? 0), lastRow),
|
|
511
|
-
column: clamp(column + Math.trunc(offset.columns ?? 0), lastColumn)
|
|
512
|
-
})),
|
|
513
|
-
rowStart: (address) => within(address, ({ row }) => ({
|
|
514
|
-
row,
|
|
515
|
-
column: 0
|
|
516
|
-
})),
|
|
517
|
-
rowEnd: (address) => within(address, ({ row }) => ({
|
|
518
|
-
row,
|
|
519
|
-
column: lastColumn
|
|
520
|
-
})),
|
|
521
|
-
first: () => cellAt({
|
|
522
|
-
row: 0,
|
|
523
|
-
column: 0
|
|
524
|
-
}),
|
|
525
|
-
last: () => cellAt({
|
|
526
|
-
row: lastRow,
|
|
527
|
-
column: lastColumn
|
|
528
|
-
}),
|
|
529
|
-
box: (anchor, focus) => Option.flatMap(positionOf(anchor), (from) => Option.map(positionOf(focus), (to) => ({
|
|
530
|
-
rows: {
|
|
531
|
-
start: Math.min(from.row, to.row),
|
|
532
|
-
end: Math.max(from.row, to.row) + 1
|
|
533
|
-
},
|
|
534
|
-
columns: visible.slice(Math.min(from.column, to.column), Math.max(from.column, to.column) + 1)
|
|
535
|
-
})))
|
|
536
|
-
};
|
|
537
|
-
} };
|
|
538
|
-
// src/focus.ts
|
|
539
|
-
const sameAddress = (a, b) => a.row === b.row && a.column === b.column;
|
|
540
|
-
/**
|
|
541
|
-
* The cell that holds the grid's one tab stop: the focused cell while it is
|
|
542
|
-
* shown, and otherwise the first cell. A focused cell whose column was hidden
|
|
543
|
-
* or whose row went away stays in the Model, so focus returns to it if it
|
|
544
|
-
* comes back, but the tab stop moves on until then.
|
|
545
|
-
*/
|
|
546
|
-
const tabStop = (projection, current) => Option.orElse(Option.filter(current, (address) => Option.isSome(projection.positionOf(address))), () => projection.first());
|
|
547
|
-
const arrows$1 = /* @__PURE__ */ new Map([
|
|
548
|
-
["ArrowUp", {
|
|
549
|
-
rows: -1,
|
|
550
|
-
columns: 0
|
|
551
|
-
}],
|
|
552
|
-
["ArrowDown", {
|
|
553
|
-
rows: 1,
|
|
554
|
-
columns: 0
|
|
555
|
-
}],
|
|
556
|
-
["ArrowLeft", {
|
|
557
|
-
rows: 0,
|
|
558
|
-
columns: -1
|
|
559
|
-
}],
|
|
560
|
-
["ArrowRight", {
|
|
561
|
-
rows: 0,
|
|
562
|
-
columns: 1
|
|
563
|
-
}]
|
|
564
|
-
]);
|
|
565
|
-
/**
|
|
566
|
-
* The cell a key moves focus to, or none when the key is not the grid's to
|
|
567
|
-
* handle, so the caller leaves it to the page. Arrows step one cell, Home and
|
|
568
|
-
* End go to the row's edges and, with Ctrl, to the grid's corners; PageUp and
|
|
569
|
-
* PageDown move `pageRows`. A key the grid handles always lands somewhere: at
|
|
570
|
-
* an edge, or onto a row that is counted but not loaded, focus stays where it
|
|
571
|
-
* is. Shift, Alt and Meta combinations are left alone: Shift extends a
|
|
572
|
-
* selection, which is not focus's to decide.
|
|
573
|
-
*/
|
|
574
|
-
const target = (projection, options) => {
|
|
575
|
-
const { key, modifiers } = options;
|
|
576
|
-
if (modifiers.shiftKey || modifiers.altKey || modifiers.metaKey) return Option.none();
|
|
577
|
-
return Option.flatMap(tabStop(projection, options.current), (from) => {
|
|
578
|
-
const stay = (to) => Option.some(Option.getOrElse(to, () => from));
|
|
579
|
-
if (key === "Home") return stay(modifiers.ctrlKey ? projection.first() : projection.rowStart(from));
|
|
580
|
-
if (key === "End") return stay(modifiers.ctrlKey ? projection.last() : projection.rowEnd(from));
|
|
581
|
-
if (modifiers.ctrlKey) return Option.none();
|
|
582
|
-
if (key === "PageUp") return stay(projection.moveBy(from, { rows: -options.pageRows }));
|
|
583
|
-
if (key === "PageDown") return stay(projection.moveBy(from, { rows: options.pageRows }));
|
|
584
|
-
const step = arrows$1.get(key);
|
|
585
|
-
if (step === void 0) return Option.none();
|
|
586
|
-
const flip = options.direction === "rtl" ? -1 : 1;
|
|
587
|
-
return stay(projection.moveBy(from, {
|
|
588
|
-
rows: step.rows,
|
|
589
|
-
columns: step.columns * flip
|
|
590
|
-
}));
|
|
591
|
-
});
|
|
592
|
-
};
|
|
593
|
-
/**
|
|
594
|
-
* A cell's DOM id: the grid's id, the row key and the column id, each
|
|
595
|
-
* URI-encoded and joined by `:`, which encoding never leaves in a part, so no
|
|
596
|
-
* two cells share an id. The container points at it with
|
|
597
|
-
* `aria-activedescendant`.
|
|
598
|
-
*/
|
|
599
|
-
const cellId = (grid, address) => [
|
|
600
|
-
grid,
|
|
601
|
-
address.row,
|
|
602
|
-
address.column
|
|
603
|
-
].map(encodeURIComponent).join(":");
|
|
604
|
-
const decode = Option.liftThrowable(decodeURIComponent);
|
|
605
|
-
/**
|
|
606
|
-
* The address a `cellId` names, if it is one of this grid's: its row key and
|
|
607
|
-
* its column id as written, for the grid's columns to check. An id from
|
|
608
|
-
* another grid, or with a malformed escape, is none.
|
|
609
|
-
*/
|
|
610
|
-
const cellOf = (grid, id) => Option.map(partsOf(grid, id, 3), ([row, column]) => ({
|
|
611
|
-
row,
|
|
612
|
-
column
|
|
613
|
-
}));
|
|
614
|
-
/** The parts of one of this grid's ids after the grid's own, when it has `count` parts in all. */
|
|
615
|
-
const partsOf = (grid, id, count) => {
|
|
616
|
-
const parts = id.split(":");
|
|
617
|
-
if (parts.length !== count) return Option.none();
|
|
618
|
-
return Option.flatMap(Option.all(parts.map((part) => decode(part))), ([owner, ...rest]) => owner === grid ? Option.some(rest) : Option.none());
|
|
619
|
-
};
|
|
620
|
-
/**
|
|
621
|
-
* A column header's DOM id: the grid's id and the column id, URI-encoded and
|
|
622
|
-
* joined by `:`. It has two parts where a cell's has three, so the two never
|
|
623
|
-
* meet.
|
|
624
|
-
*/
|
|
625
|
-
const headerId = (grid, column) => [grid, column].map(encodeURIComponent).join(":");
|
|
626
|
-
/** The column a `headerId` names, if it is one of this grid's, as written. */
|
|
627
|
-
const headerOf = (grid, id) => Option.map(partsOf(grid, id, 2), ([column]) => column);
|
|
628
|
-
/**
|
|
629
|
-
* Focus for one grid: which cell is current, by identity. Make it once per
|
|
630
|
-
* grid from its columns, so a focus saved in the Model decodes only against
|
|
631
|
-
* columns that exist.
|
|
632
|
-
*/
|
|
633
|
-
const make$1 = (columns) => {
|
|
634
|
-
const Address = CellAddress.schema(columns);
|
|
635
|
-
const Model = Schema.Struct({
|
|
636
|
-
/** The focused cell; stored as `null` when none is. */
|
|
637
|
-
current: Schema.OptionFromNullOr(Address),
|
|
638
|
-
/**
|
|
639
|
-
* The column header focus is on, when it is on the header row. The cell
|
|
640
|
-
* focused before stays in `current`, so going back down returns to it.
|
|
641
|
-
*/
|
|
642
|
-
header: Schema.OptionFromNullOr(Address.fields.column)
|
|
643
|
-
});
|
|
644
|
-
const Message = defineMessageUnion({
|
|
645
|
-
/** A cell became current: the user focused it, or a key moved there. */
|
|
646
|
-
Focused: { address: Address },
|
|
647
|
-
/** Focus went up to a column's header. */
|
|
648
|
-
HeaderFocused: { column: Address.fields.column }
|
|
649
|
-
});
|
|
650
|
-
return {
|
|
651
|
-
Address,
|
|
652
|
-
Model,
|
|
653
|
-
Message,
|
|
654
|
-
bundle: Bundle.make("GridFocus", {
|
|
655
|
-
Model,
|
|
656
|
-
Message,
|
|
657
|
-
init: () => ({ model: {
|
|
658
|
-
current: Option.none(),
|
|
659
|
-
header: Option.none()
|
|
660
|
-
} }),
|
|
661
|
-
update: (model, message) => ({ model: Message.match(message, {
|
|
662
|
-
Focused: ({ address }) => Option.isNone(model.header) && Option.isSome(model.current) && sameAddress(model.current.value, address) ? model : modifyFields(model, {
|
|
663
|
-
current: () => Option.some(address),
|
|
664
|
-
header: () => Option.none()
|
|
665
|
-
}),
|
|
666
|
-
HeaderFocused: ({ column }) => Option.contains(model.header, column) ? model : modifyFields(model, { header: () => Option.some(column) })
|
|
667
|
-
}) })
|
|
668
|
-
})
|
|
669
|
-
};
|
|
670
|
-
};
|
|
671
|
-
const GridFocus = {
|
|
672
|
-
make: make$1,
|
|
673
|
-
target,
|
|
674
|
-
tabStop,
|
|
675
|
-
cellId,
|
|
676
|
-
cellOf,
|
|
677
|
-
headerId,
|
|
678
|
-
headerOf
|
|
679
|
-
};
|
|
680
|
-
// src/selection.ts
|
|
681
|
-
/**
|
|
682
|
-
* Which rows are selected. `Keys` names them; `AllExcept` is every row but
|
|
683
|
-
* these, so select-all holds whatever the rows are, loaded or not, and
|
|
684
|
-
* however many there turn out to be.
|
|
685
|
-
*/
|
|
686
|
-
const RowSelection = defineTaggedUnion({
|
|
687
|
-
Keys: { keys: Schema.Array(Schema.String) },
|
|
688
|
-
AllExcept: { except: Schema.Array(Schema.String) }
|
|
689
|
-
});
|
|
690
|
-
const none = RowSelection.Keys({ keys: [] });
|
|
691
|
-
const lookups = /* @__PURE__ */ new WeakMap();
|
|
692
|
-
/** Whether a row is selected. Call it once per selection: it indexes the keys. */
|
|
693
|
-
const isSelected = (selection) => {
|
|
694
|
-
const cached = lookups.get(selection);
|
|
695
|
-
if (cached !== void 0) return cached;
|
|
696
|
-
const lookup = RowSelection.match(selection, {
|
|
697
|
-
Keys: ({ keys }) => {
|
|
698
|
-
const set = new Set(keys);
|
|
699
|
-
return (key) => set.has(key);
|
|
700
|
-
},
|
|
701
|
-
AllExcept: ({ except }) => {
|
|
702
|
-
const set = new Set(except);
|
|
703
|
-
return (key) => !set.has(key);
|
|
704
|
-
}
|
|
705
|
-
});
|
|
706
|
-
lookups.set(selection, lookup);
|
|
707
|
-
return lookup;
|
|
708
|
-
};
|
|
709
|
-
/** Selects a row, or deselects it if it is selected. */
|
|
710
|
-
const toggle = (selection, key) => {
|
|
711
|
-
const selected = isSelected(selection)(key);
|
|
712
|
-
return RowSelection.match(selection, {
|
|
713
|
-
Keys: ({ keys }) => RowSelection.Keys({ keys: selected ? keys.filter((each) => each !== key) : [...keys, key] }),
|
|
714
|
-
AllExcept: ({ except }) => RowSelection.AllExcept({ except: selected ? [...except, key] : except.filter((each) => each !== key) })
|
|
715
|
-
});
|
|
716
|
-
};
|
|
717
|
-
/** Selects every row in `keys`, keeping what was selected. */
|
|
718
|
-
const add = (selection, keys) => {
|
|
719
|
-
const selected = isSelected(selection);
|
|
720
|
-
const fresh = keys.filter((key) => !selected(key));
|
|
721
|
-
if (fresh.length === 0) return selection;
|
|
722
|
-
return RowSelection.match(selection, {
|
|
723
|
-
Keys: ({ keys: held }) => RowSelection.Keys({ keys: [...held, ...new Set(fresh)] }),
|
|
724
|
-
AllExcept: ({ except }) => {
|
|
725
|
-
const adding = new Set(fresh);
|
|
726
|
-
return RowSelection.AllExcept({ except: except.filter((key) => !adding.has(key)) });
|
|
727
|
-
}
|
|
728
|
-
});
|
|
729
|
-
};
|
|
730
|
-
/** The keys of the rows from `from` to `to`, in the projection's order, both included. */
|
|
731
|
-
const rowsBetween = (projection, from, to) => {
|
|
732
|
-
const keys = [];
|
|
733
|
-
const ends = Option.all([projection.rowIndex(from), projection.rowIndex(to)]);
|
|
734
|
-
if (Option.isNone(ends)) return keys;
|
|
735
|
-
const [a, b] = ends.value;
|
|
736
|
-
for (let index = Math.min(a, b); index <= Math.max(a, b); index++) {
|
|
737
|
-
const key = projection.rows.keyAt(index);
|
|
738
|
-
if (Option.isSome(key)) keys.push(key.value);
|
|
739
|
-
}
|
|
740
|
-
return keys;
|
|
741
|
-
};
|
|
742
|
-
/** The cells a range covers now, or none when a corner is hidden or gone. */
|
|
743
|
-
const boxOf = (projection, range) => projection.box(range.anchor, range.focus);
|
|
744
|
-
const arrows = /* @__PURE__ */ new Map([
|
|
745
|
-
["ArrowUp", {
|
|
746
|
-
rows: -1,
|
|
747
|
-
columns: 0
|
|
748
|
-
}],
|
|
749
|
-
["ArrowDown", {
|
|
750
|
-
rows: 1,
|
|
751
|
-
columns: 0
|
|
752
|
-
}],
|
|
753
|
-
["ArrowLeft", {
|
|
754
|
-
rows: 0,
|
|
755
|
-
columns: -1
|
|
756
|
-
}],
|
|
757
|
-
["ArrowRight", {
|
|
758
|
-
rows: 0,
|
|
759
|
-
columns: 1
|
|
760
|
-
}]
|
|
761
|
-
]);
|
|
762
|
-
/** Where a Shift key takes a range's far corner, or none when it is not such a key. */
|
|
763
|
-
const farCorner = (projection, focus, options) => {
|
|
764
|
-
const step = arrows.get(options.key);
|
|
765
|
-
if (step !== void 0) {
|
|
766
|
-
const flip = options.direction === "rtl" ? -1 : 1;
|
|
767
|
-
return Option.some(projection.moveBy(focus, {
|
|
768
|
-
rows: step.rows,
|
|
769
|
-
columns: step.columns * flip
|
|
770
|
-
}));
|
|
771
|
-
}
|
|
772
|
-
const moves = {
|
|
773
|
-
PageDown: () => projection.moveBy(focus, { rows: options.pageRows }),
|
|
774
|
-
PageUp: () => projection.moveBy(focus, { rows: -options.pageRows }),
|
|
775
|
-
Home: () => projection.rowStart(focus),
|
|
776
|
-
End: () => projection.rowEnd(focus)
|
|
777
|
-
};
|
|
778
|
-
return Object.hasOwn(moves, options.key) ? Option.some(moves[options.key]()) : Option.none();
|
|
779
|
-
};
|
|
780
|
-
/**
|
|
781
|
-
* The range a Shift key makes, or none when the key is not one: Shift with an
|
|
782
|
-
* arrow moves the range's far corner a cell, with PageUp or PageDown a page,
|
|
783
|
-
* with Home or End to the row's edge. The near corner stays where the range
|
|
784
|
-
* began, the focused cell when there was none. As with focus, a key the grid
|
|
785
|
-
* handles always lands: at an edge, or before a row not loaded, the range
|
|
786
|
-
* stays as it is.
|
|
787
|
-
*/
|
|
788
|
-
const extend = (projection, options) => {
|
|
789
|
-
const { modifiers } = options;
|
|
790
|
-
if (!modifiers.shiftKey || modifiers.altKey || modifiers.metaKey || modifiers.ctrlKey) return Option.none();
|
|
791
|
-
const start = Option.orElse(options.range, () => Option.map(options.current, (current) => ({
|
|
792
|
-
anchor: current,
|
|
793
|
-
focus: current
|
|
794
|
-
})));
|
|
795
|
-
return Option.flatMap(start, (range) => Option.map(farCorner(projection, range.focus, options), (to) => Option.match(to, {
|
|
796
|
-
onNone: () => range,
|
|
797
|
-
onSome: (focus) => ({
|
|
798
|
-
anchor: range.anchor,
|
|
799
|
-
focus
|
|
800
|
-
})
|
|
801
|
-
})));
|
|
802
|
-
};
|
|
803
|
-
const GridSelection = {
|
|
804
|
-
none,
|
|
805
|
-
isSelected,
|
|
806
|
-
toggle,
|
|
807
|
-
add,
|
|
808
|
-
rowsBetween,
|
|
809
|
-
boxOf,
|
|
810
|
-
extend
|
|
811
|
-
};
|
|
812
|
-
// src/viewport.ts
|
|
813
|
-
const Model = Schema.Struct({
|
|
814
|
-
top: Schema.Number,
|
|
815
|
-
left: Schema.Number,
|
|
816
|
-
width: Schema.Number,
|
|
817
|
-
height: Schema.Number
|
|
818
|
-
});
|
|
819
|
-
const Message = defineMessageUnion({
|
|
820
|
-
/** The scroll container moved or changed size; the Mount reports it. */
|
|
821
|
-
Measured: {
|
|
822
|
-
top: Schema.Number,
|
|
823
|
-
left: Schema.Number,
|
|
824
|
-
width: Schema.Number,
|
|
825
|
-
height: Schema.Number
|
|
826
|
-
},
|
|
827
|
-
/** A reveal set the scroll offsets; the container's own scroll event confirms it later. */
|
|
828
|
-
Revealed: {
|
|
829
|
-
top: Schema.Number,
|
|
830
|
-
left: Schema.Number
|
|
831
|
-
}
|
|
832
|
-
});
|
|
833
|
-
const write = (model, next) => {
|
|
834
|
-
const fields = {};
|
|
835
|
-
for (const [key, value] of next) {
|
|
836
|
-
const clean = Math.max(0, value);
|
|
837
|
-
if (Number.isFinite(value) && clean !== model[key]) fields[key] = () => clean;
|
|
838
|
-
}
|
|
839
|
-
return Object.keys(fields).length === 0 ? model : modifyFields(model, fields);
|
|
840
|
-
};
|
|
841
|
-
/**
|
|
842
|
-
* The scroll container's geometry, as the window math reads it. The grid
|
|
843
|
-
* owns no scroll position of its own: the container's is the fact, reported
|
|
844
|
-
* by the `Measure` Mount, and a reveal moves the container.
|
|
845
|
-
*/
|
|
846
|
-
const bundle = Bundle.make("GridViewport", {
|
|
847
|
-
Model,
|
|
848
|
-
Message,
|
|
849
|
-
init: () => ({ model: {
|
|
850
|
-
top: 0,
|
|
851
|
-
left: 0,
|
|
852
|
-
width: 0,
|
|
853
|
-
height: 0
|
|
854
|
-
} }),
|
|
855
|
-
update: (model, message) => ({ model: Message.match(message, {
|
|
856
|
-
Measured: ({ top, left, width, height }) => write(model, [
|
|
857
|
-
["top", top],
|
|
858
|
-
["left", left],
|
|
859
|
-
["width", width],
|
|
860
|
-
["height", height]
|
|
861
|
-
]),
|
|
862
|
-
Revealed: ({ top, left }) => write(model, [["top", top], ["left", left]])
|
|
863
|
-
}) })
|
|
864
|
-
});
|
|
865
|
-
/**
|
|
866
|
-
* Reports the element's scroll offsets and size: once when it mounts, then on
|
|
867
|
-
* every scroll and every resize. Attach it to the scroll container. Without
|
|
868
|
-
* `ResizeObserver` (a test DOM) it still reports scrolls.
|
|
869
|
-
*/
|
|
870
|
-
const Measure = Mount.defineStream("GridViewportMeasure", {
|
|
871
|
-
messages: [Message.Measured],
|
|
872
|
-
execute: ({ element }) => Stream.callback((queue) => Effect.gen(function* () {
|
|
873
|
-
const scroller = element;
|
|
874
|
-
const report = () => Queue.offerUnsafe(queue, Message.Measured({
|
|
875
|
-
top: scroller.scrollTop,
|
|
876
|
-
left: scroller.scrollLeft,
|
|
877
|
-
width: scroller.clientWidth,
|
|
878
|
-
height: scroller.clientHeight
|
|
879
|
-
}));
|
|
880
|
-
const Observer = globalThis.ResizeObserver;
|
|
881
|
-
yield* Effect.acquireRelease(Effect.sync(() => {
|
|
882
|
-
const observer = Observer === void 0 ? void 0 : new Observer(report);
|
|
883
|
-
element.addEventListener("scroll", report, { passive: true });
|
|
884
|
-
observer?.observe(element);
|
|
885
|
-
report();
|
|
886
|
-
return observer;
|
|
887
|
-
}), (observer) => Effect.sync(() => {
|
|
888
|
-
element.removeEventListener("scroll", report);
|
|
889
|
-
observer?.disconnect();
|
|
890
|
-
}));
|
|
891
|
-
}))
|
|
892
|
-
});
|
|
893
|
-
/**
|
|
894
|
-
* Scrolls the container with this DOM id to `offsets` (from
|
|
895
|
-
* `VirtualGrid.reveal`) and reports `Revealed`, so the next window is drawn
|
|
896
|
-
* from the new offsets at once rather than after the scroll event.
|
|
897
|
-
*/
|
|
898
|
-
const scrollTo = (viewportId, offsets) => ({
|
|
899
|
-
name: "GridViewport.scrollTo",
|
|
900
|
-
args: {
|
|
901
|
-
viewportId,
|
|
902
|
-
...offsets
|
|
903
|
-
},
|
|
904
|
-
effect: Effect.sync(() => {
|
|
905
|
-
const element = typeof document === "undefined" ? null : document.getElementById(viewportId);
|
|
906
|
-
if (element !== null && typeof element.scrollTo === "function") element.scrollTo({
|
|
907
|
-
top: offsets.top,
|
|
908
|
-
left: offsets.left
|
|
909
|
-
});
|
|
910
|
-
return Message.Revealed(offsets);
|
|
911
|
-
})
|
|
912
|
-
});
|
|
913
|
-
const GridViewport = {
|
|
914
|
-
Model,
|
|
915
|
-
Message,
|
|
916
|
-
bundle,
|
|
917
|
-
Measure,
|
|
918
|
-
scrollTo
|
|
919
|
-
};
|
|
920
|
-
// src/virtual.ts
|
|
921
|
-
const sum = (values) => values.reduce((total, value) => total + value, 0);
|
|
922
|
-
/** `edges[i]` is where center column `i` begins; `edges[count]` is the center's width. */
|
|
923
|
-
const edgesOf = (ids, width) => {
|
|
924
|
-
const edges = new Float64Array(ids.length + 1);
|
|
925
|
-
ids.forEach((id, index) => {
|
|
926
|
-
edges[index + 1] = edges[index] + Math.max(0, width(id));
|
|
927
|
-
});
|
|
928
|
-
return edges;
|
|
929
|
-
};
|
|
930
|
-
/** How many of `0..count-1` satisfy `holds`, which holds for a prefix of them: a binary search. */
|
|
931
|
-
const prefixWhere = (count, holds) => {
|
|
932
|
-
let low = 0;
|
|
933
|
-
let high = count;
|
|
934
|
-
while (low < high) {
|
|
935
|
-
const middle = low + high >>> 1;
|
|
936
|
-
if (holds(middle)) low = middle + 1;
|
|
937
|
-
else high = middle;
|
|
938
|
-
}
|
|
939
|
-
return low;
|
|
940
|
-
};
|
|
941
|
-
const layoutOf = (geometry, viewport) => {
|
|
942
|
-
const { projection, width, rowHeight } = geometry;
|
|
943
|
-
if (!(Number.isFinite(rowHeight) && rowHeight > 0)) throw new Error(`VirtualGrid: rowHeight must be a positive number of pixels, not ${rowHeight}.`);
|
|
944
|
-
const startWidth = sum(projection.start.map((id) => Math.max(0, width(id))));
|
|
945
|
-
const endWidth = sum(projection.end.map((id) => Math.max(0, width(id))));
|
|
946
|
-
return {
|
|
947
|
-
rowCount: addressableRows(projection.rowCount),
|
|
948
|
-
bodyHeight: Math.max(0, viewport.height - (geometry.headerHeight ?? 0)),
|
|
949
|
-
centerWidth: Math.max(0, viewport.width - startWidth - endWidth),
|
|
950
|
-
edges: edgesOf(projection.center, width),
|
|
951
|
-
startWidth,
|
|
952
|
-
endWidth,
|
|
953
|
-
center: projection.center
|
|
954
|
-
};
|
|
955
|
-
};
|
|
956
|
-
/**
|
|
957
|
-
* What to render for a viewport: the rows and center columns it shows, plus
|
|
958
|
-
* overscan, and the space the rest take. Rows are one fixed height, so the
|
|
959
|
-
* row window is arithmetic on the scroll offset and never walks the rows; the
|
|
960
|
-
* column window is a binary search over the center columns' widths. The
|
|
961
|
-
* pinned columns are always rendered, outside the horizontal window.
|
|
962
|
-
*/
|
|
963
|
-
const window = (options) => {
|
|
964
|
-
const { viewport, rowHeight } = options;
|
|
965
|
-
const layout = layoutOf(options, viewport);
|
|
966
|
-
const overRows = Math.max(0, Math.trunc(options.overscan?.rows ?? 0));
|
|
967
|
-
const overColumns = Math.max(0, Math.trunc(options.overscan?.columns ?? 0));
|
|
968
|
-
const top = Math.max(0, viewport.top);
|
|
969
|
-
const firstRow = Math.floor(top / rowHeight);
|
|
970
|
-
const pastRow = Math.ceil((top + layout.bodyHeight) / rowHeight);
|
|
971
|
-
const rowStart = Math.min(layout.rowCount, Math.max(0, firstRow - overRows));
|
|
972
|
-
const rowEnd = Math.min(layout.rowCount, Math.max(rowStart, pastRow + overRows));
|
|
973
|
-
const { edges } = layout;
|
|
974
|
-
const count = layout.center.length;
|
|
975
|
-
const totalCenter = edges[count];
|
|
976
|
-
const left = Math.max(0, viewport.left);
|
|
977
|
-
const right = left + layout.centerWidth;
|
|
978
|
-
const firstColumn = prefixWhere(count, (index) => edges[index + 1] <= left);
|
|
979
|
-
const pastColumn = Math.max(firstColumn, prefixWhere(count, (index) => edges[index] < right));
|
|
980
|
-
const columnStart = Math.max(0, firstColumn - overColumns);
|
|
981
|
-
const columnEnd = Math.min(count, pastColumn + overColumns);
|
|
982
|
-
return {
|
|
983
|
-
rows: {
|
|
984
|
-
start: rowStart,
|
|
985
|
-
end: rowEnd,
|
|
986
|
-
before: rowStart * rowHeight,
|
|
987
|
-
after: (layout.rowCount - rowEnd) * rowHeight
|
|
988
|
-
},
|
|
989
|
-
center: {
|
|
990
|
-
start: columnStart,
|
|
991
|
-
end: columnEnd,
|
|
992
|
-
before: edges[columnStart],
|
|
993
|
-
after: totalCenter - edges[columnEnd]
|
|
994
|
-
},
|
|
995
|
-
centerColumns: layout.center.slice(columnStart, columnEnd),
|
|
996
|
-
start: options.projection.start,
|
|
997
|
-
end: options.projection.end,
|
|
998
|
-
width: layout.startWidth + totalCenter + layout.endWidth,
|
|
999
|
-
height: (options.headerHeight ?? 0) + layout.rowCount * rowHeight
|
|
1000
|
-
};
|
|
1001
|
-
};
|
|
1002
|
-
/**
|
|
1003
|
-
* The scroll offsets that bring a cell fully into view, moving each axis the
|
|
1004
|
-
* least it can, or none when it is already in view. A pinned column never
|
|
1005
|
-
* scrolls, so only its row is brought in.
|
|
1006
|
-
*/
|
|
1007
|
-
const reveal = (options) => {
|
|
1008
|
-
const { viewport, position, rowHeight, projection } = options;
|
|
1009
|
-
const layout = layoutOf(options, viewport);
|
|
1010
|
-
const nearest = (offset, from, size, span) => {
|
|
1011
|
-
if (from < offset) return from;
|
|
1012
|
-
if (from + size > offset + span) return Math.max(0, from + size - span);
|
|
1013
|
-
return offset;
|
|
1014
|
-
};
|
|
1015
|
-
const top = nearest(viewport.top, position.row * rowHeight, rowHeight, layout.bodyHeight);
|
|
1016
|
-
const centerIndex = position.column - projection.start.length;
|
|
1017
|
-
const left = layout.center[centerIndex] !== void 0 ? nearest(viewport.left, layout.edges[centerIndex], layout.edges[centerIndex + 1] - layout.edges[centerIndex], layout.centerWidth) : viewport.left;
|
|
1018
|
-
return top === viewport.top && left === viewport.left ? Option.none() : Option.some({
|
|
1019
|
-
top,
|
|
1020
|
-
left
|
|
1021
|
-
});
|
|
1022
|
-
};
|
|
1023
|
-
const VirtualGrid = {
|
|
1024
|
-
window,
|
|
1025
|
-
reveal
|
|
1026
|
-
};
|
|
7
|
+
import { GridProjection } from "./projection.js";
|
|
8
|
+
import { ColumnState } from "./columnState.js";
|
|
9
|
+
import { Fill } from "./fill.js";
|
|
10
|
+
import { GridFocus } from "./focus.js";
|
|
11
|
+
import { GridSelection, RowSelection } from "./selection.js";
|
|
12
|
+
import { GridViewport } from "./viewport.js";
|
|
13
|
+
import { VirtualGrid } from "./virtual.js";
|
|
1027
14
|
// src/grid.ts
|
|
1028
15
|
const Offsets = Schema.Struct({
|
|
1029
16
|
top: Schema.Number,
|
|
@@ -1977,6 +964,6 @@ const make = (options) => {
|
|
|
1977
964
|
};
|
|
1978
965
|
};
|
|
1979
966
|
const DataGrid = { make };
|
|
1980
|
-
export {
|
|
967
|
+
export { DataGrid };
|
|
1981
968
|
|
|
1982
|
-
//# sourceMappingURL=
|
|
969
|
+
//# sourceMappingURL=grid.js.map
|