foldkit-data-grid 0.3.2 → 0.5.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/README.md +10 -3
- 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} +31 -569
- package/dist/grid.d.ts.map +1 -0
- package/dist/grid.js +986 -0
- 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 +123 -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 +16 -16
- package/dist/crud.mjs.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -1982
- package/dist/index.mjs.map +0 -1
- package/dist/rows-D8siEVqF.d.mts.map +0 -1
- package/dist/rows-Dy1HPpGX.mjs.map +0 -1
package/dist/rows.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.js","names":[],"sources":["../src/rows.ts"],"sourcesContent":["import { Option, Schema } from 'effect'\nimport { defineTaggedUnion } from 'foldkit/schema'\n\n/**\n * How many rows a grid can address. A local array, or a server that reports\n * its total, is `Known`; a cursor that has not reached its end is `Unknown`,\n * with the rows seen so far as a lower bound.\n */\nexport const RowCount = defineTaggedUnion({\n /** Exactly `total` rows, loaded or not. */\n Known: { total: Schema.Number },\n /** At least `atLeast` rows; more may follow. */\n Unknown: { atLeast: Schema.Number },\n})\nexport type RowCount = typeof RowCount.Type\n\n/**\n * Where the rows' source stands, for a view to say: `Loading` before any\n * answer, `Refreshing` while a newer one is on its way and the rows shown are\n * the last good ones, `Failed` with what went wrong. The grid holds no\n * loading state of its own; the source's owner (Remote, a store) says this.\n */\nexport const RowStatus = defineTaggedUnion({\n Ready: {},\n Loading: {},\n Refreshing: {},\n Failed: { message: Schema.String },\n})\nexport type RowStatus = typeof RowStatus.Type\n\n/** The number of row indexes a grid may address: the total, or the lower bound. */\nexport const addressableRows = RowCount.match({\n Known: ({ total }) => total,\n Unknown: ({ atLeast }) => atLeast,\n})\n\n/**\n * Logical row space: rows by index, each with a stable key, whatever stores\n * or fetches them. Indexes follow the order the application supplies, so a\n * sorted or filtered result is the application's, not the grid's. An index\n * that is in range may still have no row yet (a page not loaded), so every\n * lookup is an `Option`.\n */\nexport interface RowModel<Row> {\n readonly count: RowCount\n rowAt(index: number): Option.Option<Row>\n keyAt(index: number): Option.Option<string>\n indexOf(key: string): Option.Option<number>\n}\n\ninterface ArrayModel {\n readonly key: (row: never) => string\n readonly model: RowModel<unknown>\n}\n\n// One model per array, so a view that keeps its rows array between renders\n// indexes the keys once. A key function made inline on every render misses.\nconst arrayModels = new WeakMap<ReadonlyArray<unknown>, ArrayModel>()\n\nconst isIndex = (index: number, count: number): boolean =>\n Number.isInteger(index) && index >= 0 && index < count\n\nconst makeArrayModel = <Row>(\n rows: ReadonlyArray<Row>,\n key: (row: Row) => string,\n): RowModel<Row> => {\n const keys = rows.map(key)\n const indexes = new Map<string, number>()\n keys.forEach((rowKey, index) => {\n const earlier = indexes.get(rowKey)\n if (earlier !== undefined) {\n throw new Error(\n `RowModel.fromArray: rows ${earlier} and ${index} share the key \"${rowKey}\"; a row key must identify one row.`,\n )\n }\n indexes.set(rowKey, index)\n })\n return {\n count: RowCount.Known({ total: rows.length }),\n rowAt: index => (isIndex(index, rows.length) ? Option.some(rows[index]!) : Option.none()),\n keyAt: index => (isIndex(index, keys.length) ? Option.some(keys[index]!) : Option.none()),\n indexOf: rowKey => Option.fromUndefinedOr(indexes.get(rowKey)),\n }\n}\n\n// One mapped model per row model and input, so a projection built from it is\n// kept across renders while neither changes.\nconst mappedModels = new WeakMap<\n RowModel<unknown>,\n { readonly input: object; readonly model: RowModel<unknown> }\n>()\n\nexport const RowModel = {\n /**\n * Rows held in memory, in the order given. Throws when two rows share a\n * key, since focus and selection would then name two rows at once.\n */\n fromArray: <Row>(rows: ReadonlyArray<Row>, key: (row: Row) => string): RowModel<Row> => {\n const cached = arrayModels.get(rows)\n if (cached !== undefined && cached.key === key) return cached.model as RowModel<Row>\n const model = makeArrayModel(rows, key)\n arrayModels.set(rows, { key, model })\n return model\n },\n\n /**\n * The rows with each one transformed as it is read: a local edit laid over\n * a row a server sent, say. `make` turns `input` into the per-row function,\n * once per `rows` and `input`, so a lookup it builds is built once; the\n * result is the same model while both are, and a new one when either\n * changes. Keys, count and order are `rows`' own, so `make`'s function must\n * keep each row's key.\n */\n map: <Row, Mapped, Input extends object>(\n rows: RowModel<Row>,\n input: Input,\n make: (input: Input) => (row: Row) => Mapped,\n ): RowModel<Mapped> => {\n const cached = mappedModels.get(rows)\n if (cached !== undefined && cached.input === input) return cached.model as RowModel<Mapped>\n const each = make(input)\n const model: RowModel<Mapped> = {\n count: rows.count,\n rowAt: index => Option.map(rows.rowAt(index), each),\n keyAt: index => rows.keyAt(index),\n indexOf: key => rows.indexOf(key),\n }\n mappedModels.set(rows, { input, model })\n return model\n },\n}\n"],"mappings":";;;;;;;;AAQA,MAAa,WAAW,kBAAkB;;CAExC,OAAO,EAAE,OAAO,OAAO,OAAO;;CAE9B,SAAS,EAAE,SAAS,OAAO,OAAO;AACpC,CAAC;;;;;;;AASD,MAAa,YAAY,kBAAkB;CACzC,OAAO,CAAC;CACR,SAAS,CAAC;CACV,YAAY,CAAC;CACb,QAAQ,EAAE,SAAS,OAAO,OAAO;AACnC,CAAC;;AAID,MAAa,kBAAkB,SAAS,MAAM;CAC5C,QAAQ,EAAE,YAAY;CACtB,UAAU,EAAE,cAAc;AAC5B,CAAC;AAuBD,MAAM,8BAAc,IAAI,QAA4C;AAEpE,MAAM,WAAW,OAAe,UAC9B,OAAO,UAAU,KAAK,KAAK,SAAS,KAAK,QAAQ;AAEnD,MAAM,kBACJ,MACA,QACkB;CAClB,MAAM,OAAO,KAAK,IAAI,GAAG;CACzB,MAAM,0BAAU,IAAI,IAAoB;CACxC,KAAK,SAAS,QAAQ,UAAU;EAC9B,MAAM,UAAU,QAAQ,IAAI,MAAM;EAClC,IAAI,YAAY,KAAA,GACd,MAAM,IAAI,MACR,4BAA4B,QAAQ,OAAO,MAAM,kBAAkB,OAAO,oCAC5E;EAEF,QAAQ,IAAI,QAAQ,KAAK;CAC3B,CAAC;CACD,OAAO;EACL,OAAO,SAAS,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC;EAC5C,QAAO,UAAU,QAAQ,OAAO,KAAK,MAAM,IAAI,OAAO,KAAK,KAAK,MAAO,IAAI,OAAO,KAAK;EACvF,QAAO,UAAU,QAAQ,OAAO,KAAK,MAAM,IAAI,OAAO,KAAK,KAAK,MAAO,IAAI,OAAO,KAAK;EACvF,UAAS,WAAU,OAAO,gBAAgB,QAAQ,IAAI,MAAM,CAAC;CAC/D;AACF;AAIA,MAAM,+BAAe,IAAI,QAGvB;AAEF,MAAa,WAAW;;;;;CAKtB,YAAiB,MAA0B,QAA6C;EACtF,MAAM,SAAS,YAAY,IAAI,IAAI;EACnC,IAAI,WAAW,KAAA,KAAa,OAAO,QAAQ,KAAK,OAAO,OAAO;EAC9D,MAAM,QAAQ,eAAe,MAAM,GAAG;EACtC,YAAY,IAAI,MAAM;GAAE;GAAK;EAAM,CAAC;EACpC,OAAO;CACT;;;;;;;;;CAUA,MACE,MACA,OACA,SACqB;EACrB,MAAM,SAAS,aAAa,IAAI,IAAI;EACpC,IAAI,WAAW,KAAA,KAAa,OAAO,UAAU,OAAO,OAAO,OAAO;EAClE,MAAM,OAAO,KAAK,KAAK;EACvB,MAAM,QAA0B;GAC9B,OAAO,KAAK;GACZ,QAAO,UAAS,OAAO,IAAI,KAAK,MAAM,KAAK,GAAG,IAAI;GAClD,QAAO,UAAS,KAAK,MAAM,KAAK;GAChC,UAAS,QAAO,KAAK,QAAQ,GAAG;EAClC;EACA,aAAa,IAAI,MAAM;GAAE;GAAO;EAAM,CAAC;EACvC,OAAO;CACT;AACF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { CellAddress, CellBox, GridProjection } from "./projection.js";
|
|
3
|
+
import { Direction } from "./focus.js";
|
|
4
|
+
import { KeyboardModifiers } from "foldkit/html";
|
|
5
|
+
/**
|
|
6
|
+
* Which rows are selected. `Keys` names them; `AllExcept` is every row but
|
|
7
|
+
* these, so select-all holds whatever the rows are, loaded or not, and
|
|
8
|
+
* however many there turn out to be.
|
|
9
|
+
*/
|
|
10
|
+
export declare const RowSelection: import("foldkit/schema").TaggedUnion<{
|
|
11
|
+
readonly Keys: {
|
|
12
|
+
keys: Schema.$Array<Schema.String>;
|
|
13
|
+
};
|
|
14
|
+
readonly AllExcept: {
|
|
15
|
+
except: Schema.$Array<Schema.String>;
|
|
16
|
+
};
|
|
17
|
+
}>;
|
|
18
|
+
export type RowSelection = typeof RowSelection.Type;
|
|
19
|
+
/** Whether a row is selected. Call it once per selection: it indexes the keys. */
|
|
20
|
+
declare const isSelected: (selection: RowSelection) => ((key: string) => boolean);
|
|
21
|
+
/** Selects a row, or deselects it if it is selected. */
|
|
22
|
+
declare const toggle: (selection: RowSelection, key: string) => RowSelection;
|
|
23
|
+
/** Selects every row in `keys`, keeping what was selected. */
|
|
24
|
+
declare const add: (selection: RowSelection, keys: ReadonlyArray<string>) => RowSelection;
|
|
25
|
+
/** The keys of the rows from `from` to `to`, in the projection's order, both included. */
|
|
26
|
+
declare const rowsBetween: <Row, Id extends string>(projection: GridProjection<Row, Id>, from: string, to: string) => ReadonlyArray<string>;
|
|
27
|
+
/** A rectangle of cells, named by its corners, so it survives a re-sort as two cells. */
|
|
28
|
+
export interface CellRange<Id extends string> {
|
|
29
|
+
readonly anchor: CellAddress<Id>;
|
|
30
|
+
readonly focus: CellAddress<Id>;
|
|
31
|
+
}
|
|
32
|
+
/** The cells a range covers now, or none when a corner is hidden or gone. */
|
|
33
|
+
declare const boxOf: <Row, Id extends string>(projection: GridProjection<Row, Id>, range: CellRange<Id>) => Option.Option<CellBox<Id>>;
|
|
34
|
+
export interface ExtendOptions<Id extends string> {
|
|
35
|
+
readonly range: Option.Option<CellRange<Id>>;
|
|
36
|
+
/** The focused cell: a range grows from it when there is none. */
|
|
37
|
+
readonly current: Option.Option<CellAddress<Id>>;
|
|
38
|
+
readonly key: string;
|
|
39
|
+
readonly modifiers: KeyboardModifiers;
|
|
40
|
+
readonly pageRows: number;
|
|
41
|
+
readonly direction?: Direction;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The range a Shift key makes, or none when the key is not one: Shift with an
|
|
45
|
+
* arrow moves the range's far corner a cell, with PageUp or PageDown a page,
|
|
46
|
+
* with Home or End to the row's edge. The near corner stays where the range
|
|
47
|
+
* began, the focused cell when there was none. As with focus, a key the grid
|
|
48
|
+
* handles always lands: at an edge, or before a row not loaded, the range
|
|
49
|
+
* stays as it is.
|
|
50
|
+
*/
|
|
51
|
+
declare const extend: <Row, Id extends string>(projection: GridProjection<Row, Id>, options: ExtendOptions<Id>) => Option.Option<CellRange<Id>>;
|
|
52
|
+
export declare const GridSelection: {
|
|
53
|
+
none: {
|
|
54
|
+
readonly _tag: "Keys";
|
|
55
|
+
readonly keys: readonly string[];
|
|
56
|
+
};
|
|
57
|
+
isSelected: typeof isSelected;
|
|
58
|
+
toggle: typeof toggle;
|
|
59
|
+
add: typeof add;
|
|
60
|
+
rowsBetween: typeof rowsBetween;
|
|
61
|
+
boxOf: typeof boxOf;
|
|
62
|
+
extend: typeof extend;
|
|
63
|
+
};
|
|
64
|
+
export {};
|
|
65
|
+
//# sourceMappingURL=selection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.d.ts","names":[],"sources":["../src/selection.ts"],"mappings":";;;;;;;;;qBAWa,uCAAY;;IACf,MAAI,OAAA,OAAA,OAAA;;;IACC,QAAM,OAAA,OAAA,OAAA;;;YAET,sBAAsB,aAAa;;cAQzC,aAAU,WAAe,mBAAiB;;cAkB1C,SAAM,WAAe,cAAY,gBAAgB;;cAajD,MAAG,WAAe,cAAY,MAAQ,0BAAwB;;cAc9D,cAAe,KAAK,mBAAiB,YAC7B,eAAe,KAAK,KAAG,cACvB,eAEX;;iBAac,UAAU;WAChB,QAAQ,YAAY;WACpB,OAAO,YAAY;;;cAIxB,QAAS,KAAK,mBAAiB,YACvB,eAAe,KAAK,KAAG,OAC5B,UAAU,QAChB,OAAO,OAAO,QAAQ;iBAER,cAAc;WACpB,OAAO,OAAO,OAAO,UAAU;;WAE/B,SAAS,OAAO,OAAO,YAAY;WACnC;WACA,WAAW;WACX;WACA,YAAY;;;;;;;;;;cAsCjB,SAAU,KAAK,mBAAiB,YACxB,eAAe,KAAK,KAAG,SAC1B,cAAc,QACtB,OAAO,OAAO,UAAU;qBAkBd"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { defineTaggedUnion } from "foldkit/schema";
|
|
3
|
+
// src/selection.ts
|
|
4
|
+
/**
|
|
5
|
+
* Which rows are selected. `Keys` names them; `AllExcept` is every row but
|
|
6
|
+
* these, so select-all holds whatever the rows are, loaded or not, and
|
|
7
|
+
* however many there turn out to be.
|
|
8
|
+
*/
|
|
9
|
+
const RowSelection = defineTaggedUnion({
|
|
10
|
+
Keys: { keys: Schema.Array(Schema.String) },
|
|
11
|
+
AllExcept: { except: Schema.Array(Schema.String) }
|
|
12
|
+
});
|
|
13
|
+
const none = RowSelection.Keys({ keys: [] });
|
|
14
|
+
const lookups = /* @__PURE__ */ new WeakMap();
|
|
15
|
+
/** Whether a row is selected. Call it once per selection: it indexes the keys. */
|
|
16
|
+
const isSelected = (selection) => {
|
|
17
|
+
const cached = lookups.get(selection);
|
|
18
|
+
if (cached !== void 0) return cached;
|
|
19
|
+
const lookup = RowSelection.match(selection, {
|
|
20
|
+
Keys: ({ keys }) => {
|
|
21
|
+
const set = new Set(keys);
|
|
22
|
+
return (key) => set.has(key);
|
|
23
|
+
},
|
|
24
|
+
AllExcept: ({ except }) => {
|
|
25
|
+
const set = new Set(except);
|
|
26
|
+
return (key) => !set.has(key);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
lookups.set(selection, lookup);
|
|
30
|
+
return lookup;
|
|
31
|
+
};
|
|
32
|
+
/** Selects a row, or deselects it if it is selected. */
|
|
33
|
+
const toggle = (selection, key) => {
|
|
34
|
+
const selected = isSelected(selection)(key);
|
|
35
|
+
return RowSelection.match(selection, {
|
|
36
|
+
Keys: ({ keys }) => RowSelection.Keys({ keys: selected ? keys.filter((each) => each !== key) : [...keys, key] }),
|
|
37
|
+
AllExcept: ({ except }) => RowSelection.AllExcept({ except: selected ? [...except, key] : except.filter((each) => each !== key) })
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
/** Selects every row in `keys`, keeping what was selected. */
|
|
41
|
+
const add = (selection, keys) => {
|
|
42
|
+
const selected = isSelected(selection);
|
|
43
|
+
const fresh = keys.filter((key) => !selected(key));
|
|
44
|
+
if (fresh.length === 0) return selection;
|
|
45
|
+
return RowSelection.match(selection, {
|
|
46
|
+
Keys: ({ keys: held }) => RowSelection.Keys({ keys: [...held, ...new Set(fresh)] }),
|
|
47
|
+
AllExcept: ({ except }) => {
|
|
48
|
+
const adding = new Set(fresh);
|
|
49
|
+
return RowSelection.AllExcept({ except: except.filter((key) => !adding.has(key)) });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
/** The keys of the rows from `from` to `to`, in the projection's order, both included. */
|
|
54
|
+
const rowsBetween = (projection, from, to) => {
|
|
55
|
+
const keys = [];
|
|
56
|
+
const ends = Option.all([projection.rowIndex(from), projection.rowIndex(to)]);
|
|
57
|
+
if (Option.isNone(ends)) return keys;
|
|
58
|
+
const [a, b] = ends.value;
|
|
59
|
+
for (let index = Math.min(a, b); index <= Math.max(a, b); index++) {
|
|
60
|
+
const key = projection.rows.keyAt(index);
|
|
61
|
+
if (Option.isSome(key)) keys.push(key.value);
|
|
62
|
+
}
|
|
63
|
+
return keys;
|
|
64
|
+
};
|
|
65
|
+
/** The cells a range covers now, or none when a corner is hidden or gone. */
|
|
66
|
+
const boxOf = (projection, range) => projection.box(range.anchor, range.focus);
|
|
67
|
+
const arrows = /* @__PURE__ */ new Map([
|
|
68
|
+
["ArrowUp", {
|
|
69
|
+
rows: -1,
|
|
70
|
+
columns: 0
|
|
71
|
+
}],
|
|
72
|
+
["ArrowDown", {
|
|
73
|
+
rows: 1,
|
|
74
|
+
columns: 0
|
|
75
|
+
}],
|
|
76
|
+
["ArrowLeft", {
|
|
77
|
+
rows: 0,
|
|
78
|
+
columns: -1
|
|
79
|
+
}],
|
|
80
|
+
["ArrowRight", {
|
|
81
|
+
rows: 0,
|
|
82
|
+
columns: 1
|
|
83
|
+
}]
|
|
84
|
+
]);
|
|
85
|
+
/** Where a Shift key takes a range's far corner, or none when it is not such a key. */
|
|
86
|
+
const farCorner = (projection, focus, options) => {
|
|
87
|
+
const step = arrows.get(options.key);
|
|
88
|
+
if (step !== void 0) {
|
|
89
|
+
const flip = options.direction === "rtl" ? -1 : 1;
|
|
90
|
+
return Option.some(projection.moveBy(focus, {
|
|
91
|
+
rows: step.rows,
|
|
92
|
+
columns: step.columns * flip
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
const moves = {
|
|
96
|
+
PageDown: () => projection.moveBy(focus, { rows: options.pageRows }),
|
|
97
|
+
PageUp: () => projection.moveBy(focus, { rows: -options.pageRows }),
|
|
98
|
+
Home: () => projection.rowStart(focus),
|
|
99
|
+
End: () => projection.rowEnd(focus)
|
|
100
|
+
};
|
|
101
|
+
return Object.hasOwn(moves, options.key) ? Option.some(moves[options.key]()) : Option.none();
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The range a Shift key makes, or none when the key is not one: Shift with an
|
|
105
|
+
* arrow moves the range's far corner a cell, with PageUp or PageDown a page,
|
|
106
|
+
* with Home or End to the row's edge. The near corner stays where the range
|
|
107
|
+
* began, the focused cell when there was none. As with focus, a key the grid
|
|
108
|
+
* handles always lands: at an edge, or before a row not loaded, the range
|
|
109
|
+
* stays as it is.
|
|
110
|
+
*/
|
|
111
|
+
const extend = (projection, options) => {
|
|
112
|
+
const { modifiers } = options;
|
|
113
|
+
if (!modifiers.shiftKey || modifiers.altKey || modifiers.metaKey || modifiers.ctrlKey) return Option.none();
|
|
114
|
+
const start = Option.orElse(options.range, () => Option.map(options.current, (current) => ({
|
|
115
|
+
anchor: current,
|
|
116
|
+
focus: current
|
|
117
|
+
})));
|
|
118
|
+
return Option.flatMap(start, (range) => Option.map(farCorner(projection, range.focus, options), (to) => Option.match(to, {
|
|
119
|
+
onNone: () => range,
|
|
120
|
+
onSome: (focus) => ({
|
|
121
|
+
anchor: range.anchor,
|
|
122
|
+
focus
|
|
123
|
+
})
|
|
124
|
+
})));
|
|
125
|
+
};
|
|
126
|
+
const GridSelection = {
|
|
127
|
+
none,
|
|
128
|
+
isSelected,
|
|
129
|
+
toggle,
|
|
130
|
+
add,
|
|
131
|
+
rowsBetween,
|
|
132
|
+
boxOf,
|
|
133
|
+
extend
|
|
134
|
+
};
|
|
135
|
+
export { GridSelection, RowSelection };
|
|
136
|
+
|
|
137
|
+
//# sourceMappingURL=selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.js","names":[],"sources":["../src/selection.ts"],"sourcesContent":["import { Option, Schema } from 'effect'\nimport { defineTaggedUnion } from 'foldkit/schema'\nimport type { KeyboardModifiers } from 'foldkit/html'\nimport type { Direction } from './focus.js'\nimport type { CellAddress, CellBox, GridProjection } from './projection.js'\n\n/**\n * Which rows are selected. `Keys` names them; `AllExcept` is every row but\n * these, so select-all holds whatever the rows are, loaded or not, and\n * however many there turn out to be.\n */\nexport const RowSelection = defineTaggedUnion({\n Keys: { keys: Schema.Array(Schema.String) },\n AllExcept: { except: Schema.Array(Schema.String) },\n})\nexport type RowSelection = typeof RowSelection.Type\n\nconst none = RowSelection.Keys({ keys: [] })\n\n// One lookup per selection value, so drawing a window of rows asks a Set.\nconst lookups = new WeakMap<RowSelection, (key: string) => boolean>()\n\n/** Whether a row is selected. Call it once per selection: it indexes the keys. */\nconst isSelected = (selection: RowSelection): ((key: string) => boolean) => {\n const cached = lookups.get(selection)\n if (cached !== undefined) return cached\n const lookup = RowSelection.match(selection, {\n Keys: ({ keys }) => {\n const set = new Set(keys)\n return (key: string) => set.has(key)\n },\n AllExcept: ({ except }) => {\n const set = new Set(except)\n return (key: string) => !set.has(key)\n },\n })\n lookups.set(selection, lookup)\n return lookup\n}\n\n/** Selects a row, or deselects it if it is selected. */\nconst toggle = (selection: RowSelection, key: string): RowSelection => {\n const selected = isSelected(selection)(key)\n return RowSelection.match(selection, {\n Keys: ({ keys }) =>\n RowSelection.Keys({ keys: selected ? keys.filter(each => each !== key) : [...keys, key] }),\n AllExcept: ({ except }) =>\n RowSelection.AllExcept({\n except: selected ? [...except, key] : except.filter(each => each !== key),\n }),\n })\n}\n\n/** Selects every row in `keys`, keeping what was selected. */\nconst add = (selection: RowSelection, keys: ReadonlyArray<string>): RowSelection => {\n const selected = isSelected(selection)\n const fresh = keys.filter(key => !selected(key))\n if (fresh.length === 0) return selection\n return RowSelection.match(selection, {\n Keys: ({ keys: held }) => RowSelection.Keys({ keys: [...held, ...new Set(fresh)] }),\n AllExcept: ({ except }) => {\n const adding = new Set(fresh)\n return RowSelection.AllExcept({ except: except.filter(key => !adding.has(key)) })\n },\n })\n}\n\n/** The keys of the rows from `from` to `to`, in the projection's order, both included. */\nconst rowsBetween = <Row, Id extends string>(\n projection: GridProjection<Row, Id>,\n from: string,\n to: string,\n): ReadonlyArray<string> => {\n const keys: Array<string> = []\n const ends = Option.all([projection.rowIndex(from), projection.rowIndex(to)])\n if (Option.isNone(ends)) return keys\n const [a, b] = ends.value\n for (let index = Math.min(a, b); index <= Math.max(a, b); index++) {\n const key = projection.rows.keyAt(index)\n if (Option.isSome(key)) keys.push(key.value)\n }\n return keys\n}\n\n/** A rectangle of cells, named by its corners, so it survives a re-sort as two cells. */\nexport interface CellRange<Id extends string> {\n readonly anchor: CellAddress<Id>\n readonly focus: CellAddress<Id>\n}\n\n/** The cells a range covers now, or none when a corner is hidden or gone. */\nconst boxOf = <Row, Id extends string>(\n projection: GridProjection<Row, Id>,\n range: CellRange<Id>,\n): Option.Option<CellBox<Id>> => projection.box(range.anchor, range.focus)\n\nexport interface ExtendOptions<Id extends string> {\n readonly range: Option.Option<CellRange<Id>>\n /** The focused cell: a range grows from it when there is none. */\n readonly current: Option.Option<CellAddress<Id>>\n readonly key: string\n readonly modifiers: KeyboardModifiers\n readonly pageRows: number\n readonly direction?: Direction\n}\n\nconst arrows: ReadonlyMap<string, { readonly rows: number; readonly columns: number }> = new Map([\n ['ArrowUp', { rows: -1, columns: 0 }],\n ['ArrowDown', { rows: 1, columns: 0 }],\n ['ArrowLeft', { rows: 0, columns: -1 }],\n ['ArrowRight', { rows: 0, columns: 1 }],\n])\n\n/** Where a Shift key takes a range's far corner, or none when it is not such a key. */\nconst farCorner = <Row, Id extends string>(\n projection: GridProjection<Row, Id>,\n focus: CellAddress<Id>,\n options: ExtendOptions<Id>,\n): Option.Option<Option.Option<CellAddress<Id>>> => {\n const step = arrows.get(options.key)\n if (step !== undefined) {\n const flip = options.direction === 'rtl' ? -1 : 1\n return Option.some(projection.moveBy(focus, { rows: step.rows, columns: step.columns * flip }))\n }\n const moves: Readonly<Record<string, () => Option.Option<CellAddress<Id>>>> = {\n PageDown: () => projection.moveBy(focus, { rows: options.pageRows }),\n PageUp: () => projection.moveBy(focus, { rows: -options.pageRows }),\n Home: () => projection.rowStart(focus),\n End: () => projection.rowEnd(focus),\n }\n return Object.hasOwn(moves, options.key) ? Option.some(moves[options.key]!()) : Option.none()\n}\n\n/**\n * The range a Shift key makes, or none when the key is not one: Shift with an\n * arrow moves the range's far corner a cell, with PageUp or PageDown a page,\n * with Home or End to the row's edge. The near corner stays where the range\n * began, the focused cell when there was none. As with focus, a key the grid\n * handles always lands: at an edge, or before a row not loaded, the range\n * stays as it is.\n */\nconst extend = <Row, Id extends string>(\n projection: GridProjection<Row, Id>,\n options: ExtendOptions<Id>,\n): Option.Option<CellRange<Id>> => {\n const { modifiers } = options\n if (!modifiers.shiftKey || modifiers.altKey || modifiers.metaKey || modifiers.ctrlKey) {\n return Option.none()\n }\n const start = Option.orElse(options.range, () =>\n Option.map(options.current, current => ({ anchor: current, focus: current })),\n )\n return Option.flatMap(start, range =>\n Option.map(farCorner(projection, range.focus, options), to =>\n Option.match(to, {\n onNone: () => range,\n onSome: focus => ({ anchor: range.anchor, focus }),\n }),\n ),\n )\n}\n\nexport const GridSelection = { none, isSelected, toggle, add, rowsBetween, boxOf, extend }\n"],"mappings":";;;;;;;;AAWA,MAAa,eAAe,kBAAkB;CAC5C,MAAM,EAAE,MAAM,OAAO,MAAM,OAAO,MAAM,EAAE;CAC1C,WAAW,EAAE,QAAQ,OAAO,MAAM,OAAO,MAAM,EAAE;AACnD,CAAC;AAGD,MAAM,OAAO,aAAa,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;AAG3C,MAAM,0BAAU,IAAI,QAAgD;;AAGpE,MAAM,cAAc,cAAwD;CAC1E,MAAM,SAAS,QAAQ,IAAI,SAAS;CACpC,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,SAAS,aAAa,MAAM,WAAW;EAC3C,OAAO,EAAE,WAAW;GAClB,MAAM,MAAM,IAAI,IAAI,IAAI;GACxB,QAAQ,QAAgB,IAAI,IAAI,GAAG;EACrC;EACA,YAAY,EAAE,aAAa;GACzB,MAAM,MAAM,IAAI,IAAI,MAAM;GAC1B,QAAQ,QAAgB,CAAC,IAAI,IAAI,GAAG;EACtC;CACF,CAAC;CACD,QAAQ,IAAI,WAAW,MAAM;CAC7B,OAAO;AACT;;AAGA,MAAM,UAAU,WAAyB,QAA8B;CACrE,MAAM,WAAW,WAAW,SAAS,CAAC,CAAC,GAAG;CAC1C,OAAO,aAAa,MAAM,WAAW;EACnC,OAAO,EAAE,WACP,aAAa,KAAK,EAAE,MAAM,WAAW,KAAK,QAAO,SAAQ,SAAS,GAAG,IAAI,CAAC,GAAG,MAAM,GAAG,EAAE,CAAC;EAC3F,YAAY,EAAE,aACZ,aAAa,UAAU,EACrB,QAAQ,WAAW,CAA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AAQ;CAAK;CAAa;CAAO;AAAO"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Bundle } from "foldkit-bundle";
|
|
3
|
+
import { Viewport } from "./virtual.js";
|
|
4
|
+
import * as Mount from "foldkit/mount";
|
|
5
|
+
import { Command } from "foldkit/command";
|
|
6
|
+
declare const Message: import("foldkit/message").MessageUnion<{
|
|
7
|
+
/** The scroll container moved or changed size; the Mount reports it. */
|
|
8
|
+
readonly Measured: {
|
|
9
|
+
top: Schema.Number;
|
|
10
|
+
left: Schema.Number;
|
|
11
|
+
width: Schema.Number;
|
|
12
|
+
height: Schema.Number;
|
|
13
|
+
};
|
|
14
|
+
/** A reveal set the scroll offsets; the container's own scroll event confirms it later. */
|
|
15
|
+
readonly Revealed: {
|
|
16
|
+
top: Schema.Number;
|
|
17
|
+
left: Schema.Number;
|
|
18
|
+
};
|
|
19
|
+
}>;
|
|
20
|
+
type Message = typeof Message.Type;
|
|
21
|
+
/**
|
|
22
|
+
* Scrolls the container with this DOM id to `offsets` (from
|
|
23
|
+
* `VirtualGrid.reveal`) and reports `Revealed`, so the next window is drawn
|
|
24
|
+
* from the new offsets at once rather than after the scroll event.
|
|
25
|
+
*/
|
|
26
|
+
declare const scrollTo: (viewportId: string, offsets: Pick<Viewport, 'top' | 'left'>) => Command<Message>;
|
|
27
|
+
export declare const GridViewport: {
|
|
28
|
+
Model: Schema.Struct<{
|
|
29
|
+
readonly top: Schema.Number;
|
|
30
|
+
readonly left: Schema.Number;
|
|
31
|
+
readonly width: Schema.Number;
|
|
32
|
+
readonly height: Schema.Number;
|
|
33
|
+
}>;
|
|
34
|
+
Message: import("foldkit/message").MessageUnion<{
|
|
35
|
+
/** The scroll container moved or changed size; the Mount reports it. */
|
|
36
|
+
readonly Measured: {
|
|
37
|
+
top: Schema.Number;
|
|
38
|
+
left: Schema.Number;
|
|
39
|
+
width: Schema.Number;
|
|
40
|
+
height: Schema.Number;
|
|
41
|
+
};
|
|
42
|
+
/** A reveal set the scroll offsets; the container's own scroll event confirms it later. */
|
|
43
|
+
readonly Revealed: {
|
|
44
|
+
top: Schema.Number;
|
|
45
|
+
left: Schema.Number;
|
|
46
|
+
};
|
|
47
|
+
}>;
|
|
48
|
+
bundle: Bundle.Bundle<"GridViewport", void, {
|
|
49
|
+
readonly top: number;
|
|
50
|
+
readonly left: number;
|
|
51
|
+
readonly width: number;
|
|
52
|
+
readonly height: number;
|
|
53
|
+
}, {
|
|
54
|
+
readonly _tag: "Measured";
|
|
55
|
+
readonly top: number;
|
|
56
|
+
readonly left: number;
|
|
57
|
+
readonly width: number;
|
|
58
|
+
readonly height: number;
|
|
59
|
+
} | {
|
|
60
|
+
readonly _tag: "Revealed";
|
|
61
|
+
readonly top: number;
|
|
62
|
+
readonly left: number;
|
|
63
|
+
}, never, never, never, import("foldkit-bundle").NoView, {}, {}>;
|
|
64
|
+
Measure: Mount.MountDefinitionNoArgs<"GridViewportMeasure", {
|
|
65
|
+
readonly _tag: "Measured";
|
|
66
|
+
readonly top: number;
|
|
67
|
+
readonly left: number;
|
|
68
|
+
readonly width: number;
|
|
69
|
+
readonly height: number;
|
|
70
|
+
}>;
|
|
71
|
+
scrollTo: typeof scrollTo;
|
|
72
|
+
};
|
|
73
|
+
export {};
|
|
74
|
+
//# sourceMappingURL=viewport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"viewport.d.ts","names":[],"sources":["../src/viewport.ts"],"mappings":";;;;;cAgBM,mCAAO;;;IAGT,KAAG,OAAA;IACH,MAAI,OAAA;IACJ,OAAK,OAAA;IACL,QAAM,OAAA;;;;IAGI,KAAG,OAAA;IAAiB,MAAI,OAAA;;;KAEjC,iBAAiB,QAAQ;;;;;;cAoGxB,WAAQ,oBACM,SACT,KAAK,8BACb,QAAQ;qBAaE;;;;;;;;;;MA5HT,KAAG,OAAA;MACH,MAAI,OAAA;MACJ,OAAK,OAAA;MACL,QAAM,OAAA;;;;MAGI,KAAG,OAAA;MAAiB,MAAI,OAAA"}
|
package/dist/viewport.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Effect, Queue, Schema, Stream } from "effect";
|
|
2
|
+
import { defineMessageUnion } from "foldkit/message";
|
|
3
|
+
import { modifyFields } from "foldkit/struct";
|
|
4
|
+
import { Bundle } from "foldkit-bundle";
|
|
5
|
+
import * as Mount from "foldkit/mount";
|
|
6
|
+
// src/viewport.ts
|
|
7
|
+
const Model = Schema.Struct({
|
|
8
|
+
top: Schema.Number,
|
|
9
|
+
left: Schema.Number,
|
|
10
|
+
width: Schema.Number,
|
|
11
|
+
height: Schema.Number
|
|
12
|
+
});
|
|
13
|
+
const Message = defineMessageUnion({
|
|
14
|
+
/** The scroll container moved or changed size; the Mount reports it. */
|
|
15
|
+
Measured: {
|
|
16
|
+
top: Schema.Number,
|
|
17
|
+
left: Schema.Number,
|
|
18
|
+
width: Schema.Number,
|
|
19
|
+
height: Schema.Number
|
|
20
|
+
},
|
|
21
|
+
/** A reveal set the scroll offsets; the container's own scroll event confirms it later. */
|
|
22
|
+
Revealed: {
|
|
23
|
+
top: Schema.Number,
|
|
24
|
+
left: Schema.Number
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const write = (model, next) => {
|
|
28
|
+
const fields = {};
|
|
29
|
+
for (const [key, value] of next) {
|
|
30
|
+
const clean = Math.max(0, value);
|
|
31
|
+
if (Number.isFinite(value) && clean !== model[key]) fields[key] = () => clean;
|
|
32
|
+
}
|
|
33
|
+
return Object.keys(fields).length === 0 ? model : modifyFields(model, fields);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The scroll container's geometry, as the window math reads it. The grid
|
|
37
|
+
* owns no scroll position of its own: the container's is the fact, reported
|
|
38
|
+
* by the `Measure` Mount, and a reveal moves the container.
|
|
39
|
+
*/
|
|
40
|
+
const bundle = Bundle.make("GridViewport", {
|
|
41
|
+
Model,
|
|
42
|
+
Message,
|
|
43
|
+
init: () => ({ model: {
|
|
44
|
+
top: 0,
|
|
45
|
+
left: 0,
|
|
46
|
+
width: 0,
|
|
47
|
+
height: 0
|
|
48
|
+
} }),
|
|
49
|
+
update: (model, message) => ({ model: Message.match(message, {
|
|
50
|
+
Measured: ({ top, left, width, height }) => write(model, [
|
|
51
|
+
["top", top],
|
|
52
|
+
["left", left],
|
|
53
|
+
["width", width],
|
|
54
|
+
["height", height]
|
|
55
|
+
]),
|
|
56
|
+
Revealed: ({ top, left }) => write(model, [["top", top], ["left", left]])
|
|
57
|
+
}) })
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* Reports the element's scroll offsets and size: once when it mounts, then on
|
|
61
|
+
* every scroll and every resize that changes them. Attach it to the scroll
|
|
62
|
+
* container. Without `ResizeObserver` (a test DOM) it still reports scrolls.
|
|
63
|
+
*/
|
|
64
|
+
const Measure = Mount.defineStream("GridViewportMeasure", {
|
|
65
|
+
messages: [Message.Measured],
|
|
66
|
+
execute: ({ element }) => Stream.callback((queue) => Effect.gen(function* () {
|
|
67
|
+
const scroller = element;
|
|
68
|
+
let last = "";
|
|
69
|
+
const report = () => {
|
|
70
|
+
const reading = Message.Measured({
|
|
71
|
+
top: scroller.scrollTop,
|
|
72
|
+
left: scroller.scrollLeft,
|
|
73
|
+
width: scroller.clientWidth,
|
|
74
|
+
height: scroller.clientHeight
|
|
75
|
+
});
|
|
76
|
+
const key = `${reading.top} ${reading.left} ${reading.width} ${reading.height}`;
|
|
77
|
+
if (key === last) return;
|
|
78
|
+
last = key;
|
|
79
|
+
Queue.offerUnsafe(queue, reading);
|
|
80
|
+
};
|
|
81
|
+
const Observer = globalThis.ResizeObserver;
|
|
82
|
+
yield* Effect.acquireRelease(Effect.sync(() => {
|
|
83
|
+
const observer = Observer === void 0 ? void 0 : new Observer(report);
|
|
84
|
+
element.addEventListener("scroll", report, { passive: true });
|
|
85
|
+
observer?.observe(element);
|
|
86
|
+
report();
|
|
87
|
+
return observer;
|
|
88
|
+
}), (observer) => Effect.sync(() => {
|
|
89
|
+
element.removeEventListener("scroll", report);
|
|
90
|
+
observer?.disconnect();
|
|
91
|
+
}));
|
|
92
|
+
}))
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* Scrolls the container with this DOM id to `offsets` (from
|
|
96
|
+
* `VirtualGrid.reveal`) and reports `Revealed`, so the next window is drawn
|
|
97
|
+
* from the new offsets at once rather than after the scroll event.
|
|
98
|
+
*/
|
|
99
|
+
const scrollTo = (viewportId, offsets) => ({
|
|
100
|
+
name: "GridViewport.scrollTo",
|
|
101
|
+
args: {
|
|
102
|
+
viewportId,
|
|
103
|
+
...offsets
|
|
104
|
+
},
|
|
105
|
+
effect: Effect.sync(() => {
|
|
106
|
+
const element = typeof document === "undefined" ? null : document.getElementById(viewportId);
|
|
107
|
+
if (element !== null && typeof element.scrollTo === "function") element.scrollTo({
|
|
108
|
+
top: offsets.top,
|
|
109
|
+
left: offsets.left
|
|
110
|
+
});
|
|
111
|
+
return Message.Revealed(offsets);
|
|
112
|
+
})
|
|
113
|
+
});
|
|
114
|
+
const GridViewport = {
|
|
115
|
+
Model,
|
|
116
|
+
Message,
|
|
117
|
+
bundle,
|
|
118
|
+
Measure,
|
|
119
|
+
scrollTo
|
|
120
|
+
};
|
|
121
|
+
export { GridViewport };
|
|
122
|
+
|
|
123
|
+
//# sourceMappingURL=viewport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"viewport.js","names":[],"sources":["../src/viewport.ts"],"sourcesContent":["import { Effect, Queue, Schema, Stream } from 'effect'\nimport type { Command } from 'foldkit/command'\nimport { defineMessageUnion } from 'foldkit/message'\nimport * as Mount from 'foldkit/mount'\nimport { modifyFields } from 'foldkit/struct'\nimport { Bundle } from 'foldkit-bundle'\nimport type { Viewport } from './virtual.js'\n\nconst Model = Schema.Struct({\n top: Schema.Number,\n left: Schema.Number,\n width: Schema.Number,\n height: Schema.Number,\n})\ntype Model = typeof Model.Type\n\nconst Message = defineMessageUnion({\n /** The scroll container moved or changed size; the Mount reports it. */\n Measured: {\n top: Schema.Number,\n left: Schema.Number,\n width: Schema.Number,\n height: Schema.Number,\n },\n /** A reveal set the scroll offsets; the container's own scroll event confirms it later. */\n Revealed: { top: Schema.Number, left: Schema.Number },\n})\ntype Message = typeof Message.Type\n\n// A detached element measures NaN, and an overscroll less than nothing; neither\n// may reach the window math. An unchanged reading returns the Model it was given.\nconst write = (model: Model, next: ReadonlyArray<readonly [keyof Model, number]>): Model => {\n const fields: Partial<Record<keyof Model, () => number>> = {}\n for (const [key, value] of next) {\n const clean = Math.max(0, value)\n if (Number.isFinite(value) && clean !== model[key]) fields[key] = () => clean\n }\n return Object.keys(fields).length === 0 ? model : modifyFields(model, fields)\n}\n\n/**\n * The scroll container's geometry, as the window math reads it. The grid\n * owns no scroll position of its own: the container's is the fact, reported\n * by the `Measure` Mount, and a reveal moves the container.\n */\nconst bundle = Bundle.make('GridViewport', {\n Model,\n Message,\n init: () => ({ model: { top: 0, left: 0, width: 0, height: 0 } }),\n update: (model: Model, message: Message) => ({\n model: Message.match(message, {\n Measured: ({ top, left, width, height }) =>\n write(model, [\n ['top', top],\n ['left', left],\n ['width', width],\n ['height', height],\n ]),\n Revealed: ({ top, left }) =>\n write(model, [\n ['top', top],\n ['left', left],\n ]),\n }),\n }),\n})\n\ninterface Scroller {\n readonly scrollTop: number\n readonly scrollLeft: number\n readonly clientWidth: number\n readonly clientHeight: number\n}\n\ntype ResizeObserverCtor = new (callback: () => void) => ResizeObserver\n\n/**\n * Reports the element's scroll offsets and size: once when it mounts, then on\n * every scroll and every resize that changes them. Attach it to the scroll\n * container. Without `ResizeObserver` (a test DOM) it still reports scrolls.\n */\nconst Measure = Mount.defineStream('GridViewportMeasure', {\n messages: [Message.Measured],\n execute: ({ element }) =>\n Stream.callback<typeof Message.Measured.Type>(queue =>\n Effect.gen(function* () {\n const scroller = element as unknown as Scroller\n // An observer's first callback repeats the size just reported, so\n // only a change is sent: the Mount answers once when it starts.\n let last = ''\n const report = () => {\n const reading = Message.Measured({\n top: scroller.scrollTop,\n left: scroller.scrollLeft,\n width: scroller.clientWidth,\n height: scroller.clientHeight,\n })\n const key = `${reading.top} ${reading.left} ${reading.width} ${reading.height}`\n if (key === last) return\n last = key\n Queue.offerUnsafe(queue, reading)\n }\n const Observer = (globalThis as { ResizeObserver?: ResizeObserverCtor }).ResizeObserver\n yield* Effect.acquireRelease(\n Effect.sync(() => {\n // Built before anything listens, so a constructor that throws leaves nothing attached.\n const observer = Observer === undefined ? undefined : new Observer(report)\n element.addEventListener('scroll', report, { passive: true })\n observer?.observe(element)\n report()\n return observer\n }),\n observer =>\n Effect.sync(() => {\n element.removeEventListener('scroll', report)\n observer?.disconnect()\n }),\n )\n }),\n ),\n})\n\n/**\n * Scrolls the container with this DOM id to `offsets` (from\n * `VirtualGrid.reveal`) and reports `Revealed`, so the next window is drawn\n * from the new offsets at once rather than after the scroll event.\n */\nconst scrollTo = (\n viewportId: string,\n offsets: Pick<Viewport, 'top' | 'left'>,\n): Command<Message> => ({\n name: 'GridViewport.scrollTo',\n args: { viewportId, ...offsets },\n effect: Effect.sync(() => {\n const element = typeof document === 'undefined' ? null : document.getElementById(viewportId)\n // A DOM with no layout (jsdom, in a test) has no scrollTo and nothing to scroll.\n if (element !== null && typeof element.scrollTo === 'function') {\n element.scrollTo({ top: offsets.top, left: offsets.left })\n }\n return Message.Revealed(offsets)\n }),\n})\n\nexport const GridViewport = { Model, Message, bundle, Measure, scrollTo }\n"],"mappings":";;;;;;AAQA,MAAM,QAAQ,OAAO,OAAO;CAC1B,KAAK,OAAO;CACZ,MAAM,OAAO;CACb,OAAO,OAAO;CACd,QAAQ,OAAO;AACjB,CAAC;AAGD,MAAM,UAAU,mBAAmB;;CAEjC,UAAU;EACR,KAAK,OAAO;EACZ,MAAM,OAAO;EACb,OAAO,OAAO;EACd,QAAQ,OAAO;CACjB;;CAEA,UAAU;EAAE,KAAK,OAAO;EAAQ,MAA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,MAAM;GAClB,OAAO;GACP,MAAM,YAAY,OAAO,OAAO;EAClC;EACA,MAAM,WAAY,WAAuD;EACzE,OAAO,OAAO,eACZ,OAAO,WAAW;GAEhB,MAAM,WAAW,aAAa,KAAA,IAAY,KAAA,IAAY,IAAI,SAAS,MAAM;GACzE,QAAQ,iBAAiB,UAAU,QAAQ,EAAE,SAAS,KAAK,CAAC;GAC5D,UAAU,QAAQ,OAAO;GACzB,OAAO;GACP,OAAO;EACT,CAAC,IACD,aACE,OAAO,WAAW;GAChB,QAAQ,oBAAoB,UAAU,MAAM;GAC5C,UAAU,WAAW;EACvB,CAAC,CACL;CACF,CAAC,CACH;AACJ,CAAC;;;;;;AAOD,MAAM,YACJ,YACA,aACsB;CACtB,MAAM;CACN,MAAM;EAAE;EAAY,GAAG;CAAQ;CAC/B,QAAQ,OAAO,WAAW;EACxB,MAAM,UAAU,OAAO,aAAa,cAAc,OAAO,SAAS,eAAe,UAAU;EAE3F,IAAI,YAAY,QAAQ,OAAO,QAAQ,aAAa,YAClD,QAAQ,SAAS;GAAE,KAAK,QAAQ;GAAK,MAAM,QAAQ;EAAK,CAAC;EAE3D,OAAO,QAAQ,SAAS,OAAO;CACjC,CAAC;AACH;AAEA,MAAa,eAAe;CAAE;CAAO;CAAS;CAAQ;CAAS;AAAS"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { CellPosition, GridProjection } from "./projection.js";
|
|
3
|
+
/** The scroll container's offsets and size, in pixels. */
|
|
4
|
+
export interface Viewport {
|
|
5
|
+
readonly top: number;
|
|
6
|
+
readonly left: number;
|
|
7
|
+
readonly width: number;
|
|
8
|
+
readonly height: number;
|
|
9
|
+
}
|
|
10
|
+
/** Rendered items `[start, end)` on one axis, with the space the rest take before and after. */
|
|
11
|
+
export interface AxisWindow {
|
|
12
|
+
readonly start: number;
|
|
13
|
+
readonly end: number;
|
|
14
|
+
readonly before: number;
|
|
15
|
+
readonly after: number;
|
|
16
|
+
}
|
|
17
|
+
export interface GridWindow<Id extends string> {
|
|
18
|
+
/** Row indexes to render; every row is `rowHeight` tall. */
|
|
19
|
+
readonly rows: AxisWindow;
|
|
20
|
+
/** Center columns to render, as indexes into `projection.center`. */
|
|
21
|
+
readonly center: AxisWindow;
|
|
22
|
+
/** The center columns in that window, in display order. */
|
|
23
|
+
readonly centerColumns: ReadonlyArray<Id>;
|
|
24
|
+
/** Pinned columns are always rendered: they never scroll horizontally. */
|
|
25
|
+
readonly start: ReadonlyArray<Id>;
|
|
26
|
+
readonly end: ReadonlyArray<Id>;
|
|
27
|
+
/** The scrollable content's full size. */
|
|
28
|
+
readonly width: number;
|
|
29
|
+
readonly height: number;
|
|
30
|
+
}
|
|
31
|
+
export interface Geometry<Row, Id extends string> {
|
|
32
|
+
readonly projection: GridProjection<Row, Id>;
|
|
33
|
+
readonly rowHeight: number;
|
|
34
|
+
/** Width of each visible column. */
|
|
35
|
+
readonly width: (column: NoInfer<Id>) => number;
|
|
36
|
+
/** Height of the header rows inside the scroll container, above the body. */
|
|
37
|
+
readonly headerHeight?: number;
|
|
38
|
+
}
|
|
39
|
+
export interface WindowOptions<Row, Id extends string> extends Geometry<Row, Id> {
|
|
40
|
+
readonly viewport: Viewport;
|
|
41
|
+
/** Extra rows and columns rendered beyond each edge, so a fast scroll shows no gap. */
|
|
42
|
+
readonly overscan?: {
|
|
43
|
+
readonly rows?: number;
|
|
44
|
+
readonly columns?: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* What to render for a viewport: the rows and center columns it shows, plus
|
|
49
|
+
* overscan, and the space the rest take. Rows are one fixed height, so the
|
|
50
|
+
* row window is arithmetic on the scroll offset and never walks the rows; the
|
|
51
|
+
* column window is a binary search over the center columns' widths. The
|
|
52
|
+
* pinned columns are always rendered, outside the horizontal window.
|
|
53
|
+
*/
|
|
54
|
+
declare const window: <Row, Id extends string>(options: WindowOptions<Row, Id>) => GridWindow<Id>;
|
|
55
|
+
export interface RevealOptions<Row, Id extends string> extends Geometry<Row, Id> {
|
|
56
|
+
readonly viewport: Viewport;
|
|
57
|
+
readonly position: CellPosition;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The scroll offsets that bring a cell fully into view, moving each axis the
|
|
61
|
+
* least it can, or none when it is already in view. A pinned column never
|
|
62
|
+
* scrolls, so only its row is brought in.
|
|
63
|
+
*/
|
|
64
|
+
declare const reveal: <Row, Id extends string>(options: RevealOptions<Row, Id>) => Option.Option<{
|
|
65
|
+
readonly top: number;
|
|
66
|
+
readonly left: number;
|
|
67
|
+
}>;
|
|
68
|
+
export declare const VirtualGrid: {
|
|
69
|
+
window: typeof window;
|
|
70
|
+
reveal: typeof reveal;
|
|
71
|
+
};
|
|
72
|
+
export {};
|
|
73
|
+
//# sourceMappingURL=virtual.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtual.d.ts","names":[],"sources":["../src/virtual.ts"],"mappings":";;;iBAKiB;WACN;WACA;WACA;WACA;;;iBAIM;WACN;WACA;WACA;WACA;;iBAGM,WAAW;;WAEjB,MAAM;;WAEN,QAAQ;;WAER,eAAe,cAAc;;WAE7B,OAAO,cAAc;WACrB,KAAK,cAAc;;WAEnB;WACA;;iBAGM,SAAS,KAAK;WACpB,YAAY,eAAe,KAAK;WAChC;;WAEA,QAAQ,QAAQ,QAAQ;;WAExB;;iBAGM,cAAc,KAAK,2BAA2B,SAAS,KAAK;WAClE,UAAU;;WAEV;aAAsB;aAAwB;;;;;;;;;;cAoEnD,SAAU,KAAK,mBAAiB,SAAW,cAAc,KAAK,QAAM,WAAW;iBA+CpE,cAAc,KAAK,2BAA2B,SAAS,KAAK;WAClE,UAAU;WACV,UAAU;;;;;;;cAQf,SAAU,KAAK,mBAAiB,SAC3B,cAAc,KAAK,QAC3B,OAAO;WAAkB;WAAsB;;qBAyBrC"}
|
package/dist/virtual.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Option } from "effect";
|
|
2
|
+
import { addressableRows } from "./rows.js";
|
|
3
|
+
// src/virtual.ts
|
|
4
|
+
const sum = (values) => values.reduce((total, value) => total + value, 0);
|
|
5
|
+
/** `edges[i]` is where center column `i` begins; `edges[count]` is the center's width. */
|
|
6
|
+
const edgesOf = (ids, width) => {
|
|
7
|
+
const edges = new Float64Array(ids.length + 1);
|
|
8
|
+
ids.forEach((id, index) => {
|
|
9
|
+
edges[index + 1] = edges[index] + Math.max(0, width(id));
|
|
10
|
+
});
|
|
11
|
+
return edges;
|
|
12
|
+
};
|
|
13
|
+
/** How many of `0..count-1` satisfy `holds`, which holds for a prefix of them: a binary search. */
|
|
14
|
+
const prefixWhere = (count, holds) => {
|
|
15
|
+
let low = 0;
|
|
16
|
+
let high = count;
|
|
17
|
+
while (low < high) {
|
|
18
|
+
const middle = low + high >>> 1;
|
|
19
|
+
if (holds(middle)) low = middle + 1;
|
|
20
|
+
else high = middle;
|
|
21
|
+
}
|
|
22
|
+
return low;
|
|
23
|
+
};
|
|
24
|
+
const layoutOf = (geometry, viewport) => {
|
|
25
|
+
const { projection, width, rowHeight } = geometry;
|
|
26
|
+
if (!(Number.isFinite(rowHeight) && rowHeight > 0)) throw new Error(`VirtualGrid: rowHeight must be a positive number of pixels, not ${rowHeight}.`);
|
|
27
|
+
const startWidth = sum(projection.start.map((id) => Math.max(0, width(id))));
|
|
28
|
+
const endWidth = sum(projection.end.map((id) => Math.max(0, width(id))));
|
|
29
|
+
return {
|
|
30
|
+
rowCount: addressableRows(projection.rowCount),
|
|
31
|
+
bodyHeight: Math.max(0, viewport.height - (geometry.headerHeight ?? 0)),
|
|
32
|
+
centerWidth: Math.max(0, viewport.width - startWidth - endWidth),
|
|
33
|
+
edges: edgesOf(projection.center, width),
|
|
34
|
+
startWidth,
|
|
35
|
+
endWidth,
|
|
36
|
+
center: projection.center
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* What to render for a viewport: the rows and center columns it shows, plus
|
|
41
|
+
* overscan, and the space the rest take. Rows are one fixed height, so the
|
|
42
|
+
* row window is arithmetic on the scroll offset and never walks the rows; the
|
|
43
|
+
* column window is a binary search over the center columns' widths. The
|
|
44
|
+
* pinned columns are always rendered, outside the horizontal window.
|
|
45
|
+
*/
|
|
46
|
+
const window = (options) => {
|
|
47
|
+
const { viewport, rowHeight } = options;
|
|
48
|
+
const layout = layoutOf(options, viewport);
|
|
49
|
+
const overRows = Math.max(0, Math.trunc(options.overscan?.rows ?? 0));
|
|
50
|
+
const overColumns = Math.max(0, Math.trunc(options.overscan?.columns ?? 0));
|
|
51
|
+
const top = Math.max(0, viewport.top);
|
|
52
|
+
const firstRow = Math.floor(top / rowHeight);
|
|
53
|
+
const pastRow = Math.ceil((top + layout.bodyHeight) / rowHeight);
|
|
54
|
+
const rowStart = Math.min(layout.rowCount, Math.max(0, firstRow - overRows));
|
|
55
|
+
const rowEnd = Math.min(layout.rowCount, Math.max(rowStart, pastRow + overRows));
|
|
56
|
+
const { edges } = layout;
|
|
57
|
+
const count = layout.center.length;
|
|
58
|
+
const totalCenter = edges[count];
|
|
59
|
+
const left = Math.max(0, viewport.left);
|
|
60
|
+
const right = left + layout.centerWidth;
|
|
61
|
+
const firstColumn = prefixWhere(count, (index) => edges[index + 1] <= left);
|
|
62
|
+
const pastColumn = Math.max(firstColumn, prefixWhere(count, (index) => edges[index] < right));
|
|
63
|
+
const columnStart = Math.max(0, firstColumn - overColumns);
|
|
64
|
+
const columnEnd = Math.min(count, pastColumn + overColumns);
|
|
65
|
+
return {
|
|
66
|
+
rows: {
|
|
67
|
+
start: rowStart,
|
|
68
|
+
end: rowEnd,
|
|
69
|
+
before: rowStart * rowHeight,
|
|
70
|
+
after: (layout.rowCount - rowEnd) * rowHeight
|
|
71
|
+
},
|
|
72
|
+
center: {
|
|
73
|
+
start: columnStart,
|
|
74
|
+
end: columnEnd,
|
|
75
|
+
before: edges[columnStart],
|
|
76
|
+
after: totalCenter - edges[columnEnd]
|
|
77
|
+
},
|
|
78
|
+
centerColumns: layout.center.slice(columnStart, columnEnd),
|
|
79
|
+
start: options.projection.start,
|
|
80
|
+
end: options.projection.end,
|
|
81
|
+
width: layout.startWidth + totalCenter + layout.endWidth,
|
|
82
|
+
height: (options.headerHeight ?? 0) + layout.rowCount * rowHeight
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* The scroll offsets that bring a cell fully into view, moving each axis the
|
|
87
|
+
* least it can, or none when it is already in view. A pinned column never
|
|
88
|
+
* scrolls, so only its row is brought in.
|
|
89
|
+
*/
|
|
90
|
+
const reveal = (options) => {
|
|
91
|
+
const { viewport, position, rowHeight, projection } = options;
|
|
92
|
+
const layout = layoutOf(options, viewport);
|
|
93
|
+
const nearest = (offset, from, size, span) => {
|
|
94
|
+
if (from < offset) return from;
|
|
95
|
+
if (from + size > offset + span) return Math.max(0, from + size - span);
|
|
96
|
+
return offset;
|
|
97
|
+
};
|
|
98
|
+
const top = nearest(viewport.top, position.row * rowHeight, rowHeight, layout.bodyHeight);
|
|
99
|
+
const centerIndex = position.column - projection.start.length;
|
|
100
|
+
const left = layout.center[centerIndex] !== void 0 ? nearest(viewport.left, layout.edges[centerIndex], layout.edges[centerIndex + 1] - layout.edges[centerIndex], layout.centerWidth) : viewport.left;
|
|
101
|
+
return top === viewport.top && left === viewport.left ? Option.none() : Option.some({
|
|
102
|
+
top,
|
|
103
|
+
left
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
const VirtualGrid = {
|
|
107
|
+
window,
|
|
108
|
+
reveal
|
|
109
|
+
};
|
|
110
|
+
export { VirtualGrid };
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=virtual.js.map
|