foldkit-data-grid 0.2.3 → 0.3.1

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.
@@ -1,5 +1,4 @@
1
1
  import { Option, Schema } from "effect";
2
- //#region src/columns.d.ts
3
2
  /** What a column shows and where it starts out. Its id is its key in `Columns.define`. */
4
3
  interface ColumnSpec<Row, Value> {
5
4
  readonly header: string;
@@ -97,8 +96,6 @@ declare const Columns: {
97
96
  */
98
97
  define: <Row>() => <Specs extends Record<string, ColumnSpec<Row, unknown>>>(specs: Specs) => NoInfer<Columns<Row, Specs>>;
99
98
  };
100
- //#endregion
101
- //#region src/rows.d.ts
102
99
  /**
103
100
  * How many rows a grid can address. A local array, or a server that reports
104
101
  * its total, is `Known`; a cursor that has not reached its end is `Unknown`,
@@ -167,6 +164,5 @@ declare const RowModel: {
167
164
  */
168
165
  map: <Row, Mapped, Input extends object>(rows: RowModel<Row>, input: Input, make: (input: Input) => (row: Row) => Mapped) => RowModel<Mapped>;
169
166
  };
170
- //#endregion
171
167
  export { CellEditor as a, ColumnId as c, EditValue as d, EditableId as f, addressableRows as i, ColumnSpec as l, RowModel as n, Column as o, RowStatus as r, ColumnEdit as s, RowCount as t, Columns as u };
172
- //# sourceMappingURL=rows-TYJdJ7tK.d.mts.map
168
+ //# sourceMappingURL=rows-D8siEVqF.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rows-D8siEVqF.d.mts","names":[],"sources":["../src/columns.ts","../src/rows.ts"],"mappings":";;UAGiB,WAAW,KAAK;WACtB;WACA,QAAQ,KAAK,QAAQ;;WAErB;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;;;;;WAOA,OAAO,WAAW;;UAGZ,WAAW,KAAK;;WAEtB,SAAS,KAAK;;;;;;;WAOd,SAAS,OAAO,MAAM;;;;;;cAOpB,qCAAU;;;;;;IAKX,SAAO,OAAA,OAAA,OAAA;;;;;KAIP,oBAAoB,WAAW;;KAsC/B,WAAW,YACpB,WAAW,QAAQ,MAAM;WAAsB;IAAiB,mBAC3D;;KAII,UAAU,QAAQ;WACnB;aAAiB,QAAQ,OAAO,YAAY;;IAEnD;UAGa,OAAO,KAAK,mBAAmB,eAAe,WAAW,KAAK;WACpE,IAAI;;KAGV,QAAQ,QAAQ,aAAa,wBAAwB,SAAS;KAEvD,SAAS,KAAK,UAAU,qBAAqB,eAAe;;;;;;UAOvD,QAAQ,KAAK,cAAc,eAAe,WAAW;;WAE3D,KAAK,oBAAoB;WACzB,kBACG,YAAY,iBAAiB,OAAO,KAAK,IAAI,QAAQ,MAAM;;;;;WAM9D,QAAQ;;cAON;;EAEX;;EAEA;;EAEA,SAAM;;;;;;EAMN,SACG,WACA,cAAc,eAAe,WAAW,gBAAc,OAC9C,UACN,QAAQ,QAAQ,KAAK;;;;;;;cC9If,mCAAQ;;;IAEV,OAAK,OAAA;;;;IAEH,SAAO,OAAA;;;KAER,kBAAkB,SAAS;;;;;;;cAQ1B,oCAAS;;;;;IAIV,SAAO,OAAA;;;KAEP,mBAAmB,UAAU;;cAG5B,kBAAe;;;;;;;;;;;;;;UAYX,SAAS;WACf,OAAO;EAChB,MAAM,gBAAgB,OAAO,OAAO;EACpC,MAAM,gBAAgB,OAAO;EAC7B,QAAQ,cAAc,OAAO;;cA6ClB;;;;;EAKX,YAAY,KAAG,MAAQ,cAAc,MAAI,MAAQ,KAAK,mBAAiB,SAAS;;;;;;;;;EAgBhF,MAAM,KAAK,QAAQ,sBAAoB,MAC/B,SAAS,MAAI,OACZ,OAAK,OACL,OAAO,WAAW,KAAK,QAAQ,WACrC,SAAS"}
@@ -1,6 +1,6 @@
1
1
  import { Option, Schema, SchemaAST } from "effect";
2
2
  import { defineTaggedUnion } from "foldkit/schema";
3
- //#region src/columns.ts
3
+ // src/columns.ts
4
4
  /**
5
5
  * How a cell's edit is drawn, read from its column's schema: a choice of the
6
6
  * literals its text may be, a number typed, or text.
@@ -68,8 +68,7 @@ const Columns = {
68
68
  });
69
69
  }
70
70
  };
71
- //#endregion
72
- //#region src/rows.ts
71
+ // src/rows.ts
73
72
  /**
74
73
  * How many rows a grid can address. A local array, or a server that reports
75
74
  * its total, is `Known`; a cursor that has not reached its end is `Unknown`,
@@ -156,7 +155,6 @@ const RowModel = {
156
155
  return model;
157
156
  }
158
157
  };
159
- //#endregion
160
158
  export { CellEditor as a, addressableRows as i, RowModel as n, Columns as o, RowStatus as r, editorOf as s, RowCount as t };
161
159
 
162
- //# sourceMappingURL=rows-BFm_vy9f.mjs.map
160
+ //# sourceMappingURL=rows-Dy1HPpGX.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"rows-BFm_vy9f.mjs","names":[],"sources":["../src/columns.ts","../src/rows.ts"],"sourcesContent":["import { Schema, SchemaAST } from 'effect'\nimport { defineTaggedUnion } from 'foldkit/schema'\n/** What a column shows and where it starts out. Its id is its key in `Columns.define`. */\nexport interface ColumnSpec<Row, Value> {\n readonly header: string\n readonly value: (row: Row) => Value\n /** The region the column starts in; `ColumnLayout.initial` reads it. */\n readonly pinned?: 'start' | 'end'\n /** Whether the column starts hidden; `ColumnLayout.initial` reads it. */\n readonly hidden?: boolean\n /** Its width in pixels until resized. Default `Columns.defaultWidth`. */\n readonly width?: number\n /** The narrowest a resize makes it. Default `Columns.minWidth`. */\n readonly minWidth?: number\n /** The widest a resize makes it. Default no limit. */\n readonly maxWidth?: number\n /** Whether it can be resized. Default `true`. */\n readonly resizable?: boolean\n /** Whether it can be hidden. Default `true`. */\n readonly hideable?: boolean\n /**\n * Makes the column's cells editable as text. The grid holds the draft,\n * commits it only when the column's schema decodes it, and reports the\n * committed text as an `Edited` OutMessage; `matchEdit` reads its value\n * back, and writing it is the application's.\n */\n readonly edit?: ColumnEdit<Row, unknown>\n}\n\nexport interface ColumnEdit<Row, Value = string> {\n /** The text an edit of a row's cell begins with. Default the value as a string. */\n readonly draft?: (row: Row) => string\n /**\n * What the text means: from the text typed to the column's value. A draft\n * is committed only when it decodes, and the failure's message is the\n * cell's error, so a check's own `message` is what a person reads. Default\n * any text, as itself.\n */\n readonly schema?: Schema.Codec<Value, string>\n}\n\n/**\n * How a cell's edit is drawn, read from its column's schema: a choice of the\n * literals its text may be, a number typed, or text.\n */\nexport const CellEditor = defineTaggedUnion({\n Text: {},\n /** The schema decodes to a number: typed with a decimal keypad on touch. */\n Number: {},\n /** The schema's text is one of these, offered in order. */\n Choice: { options: Schema.Array(Schema.String) },\n /** The schema decodes to a date: a native date field over the draft text. */\n Date: {},\n})\nexport type CellEditor = typeof CellEditor.Type\n\n/**\n * Whether a schema decodes to a date, found as Effect's own tools find it:\n * by the declaration's representation id. Only Date-ness is read here, never\n * the text format: committing decodes the draft through the schema, and a\n * refusal is the cell's error.\n */\nconst DATE_ID = 'effect/schema/Date'\n\nconst isDate = (ast: SchemaAST.AST): boolean => {\n if (!SchemaAST.isDeclaration(ast)) return false\n const representation: unknown = ast.annotations?.['representation']\n return (\n typeof representation === 'object' &&\n representation !== null &&\n 'id' in representation &&\n representation.id === DATE_ID\n )\n}\n\n/** A column's editor, from its schema; a column with none edits text. */\nexport const editorOf = (schema: Schema.Top | undefined): CellEditor => {\n if (schema === undefined) return CellEditor.Text()\n const encoded = SchemaAST.toEncoded(schema.ast)\n if (SchemaAST.isUnion(encoded)) {\n const options = encoded.types.flatMap(member =>\n SchemaAST.isLiteral(member) && typeof member.literal === 'string' ? [member.literal] : [],\n )\n // Every member a text literal: anything else typed would be refused.\n if (options.length === encoded.types.length) return CellEditor.Choice({ options })\n }\n const type = SchemaAST.toType(schema.ast)\n if (isDate(type)) return CellEditor.Date()\n return SchemaAST.isNumber(type) ? CellEditor.Number() : CellEditor.Text()\n}\n\n/** The ids of the columns that edit. */\nexport type EditableId<Specs> = {\n [K in keyof Specs]: Specs[K] extends { readonly edit: object } ? K : never\n}[keyof Specs] &\n string\n\n/** What an editable column's text decodes to: its schema's value, or the text. */\nexport type EditValue<Spec> = Spec extends {\n readonly edit: { readonly schema: Schema.Codec<infer Value, string> }\n}\n ? Value\n : string\n\nexport interface Column<Row, Id extends string, Value> extends ColumnSpec<Row, Value> {\n readonly id: Id\n}\n\ntype ValueOf<Spec> = Spec extends ColumnSpec<never, infer Value> ? Value : never\n\nexport type ColumnId<C> = C extends Columns<never, infer Specs> ? keyof Specs & string : never\n\n/**\n * A grid's columns, keyed by stable id. Order, visibility and pinning live in\n * a `ColumnLayout`, so every reference to a column (focus, a selected range,\n * a saved width) names its id and survives a reorder.\n */\nexport interface Columns<Row, Specs extends Record<string, ColumnSpec<Row, unknown>>> {\n /** In definition order: the order `ColumnLayout.initial` starts from. */\n readonly ids: ReadonlyArray<keyof Specs & string>\n readonly byId: {\n readonly [Id in keyof Specs & string]: Column<Row, Id, ValueOf<Specs[Id]>>\n }\n /**\n * Type-only: the specs as written, so a grid made from these columns infers\n * each one exactly, its edit's schema included. Never set.\n */\n readonly Specs?: Specs\n}\n\n// Integer-like keys are enumerated before every other key, whatever the order\n// they were written in, so `{ name, 2024 }` would put 2024 first.\nconst arrayIndex = /^(0|[1-9][0-9]*)$/\n\nexport const Columns = {\n /** The width of a column that names none. */\n defaultWidth: 120,\n /** The narrowest a column with no `minWidth` resizes to. */\n minWidth: 40,\n /** A value as a cell shows it: nothing for `null` or `undefined`. */\n textOf: (value: unknown): string => (value === null || value === undefined ? '' : String(value)),\n /**\n * Declares a grid's columns over `Row`. Call it twice, once with the row\n * type and once with the specs, so the ids and values are inferred:\n * `Columns.define<Product>()({ sku: { header: 'SKU', value: p => p.sku } })`.\n */\n define:\n <Row>() =>\n <Specs extends Record<string, ColumnSpec<Row, unknown>>>(\n specs: Specs,\n ): NoInfer<Columns<Row, Specs>> => {\n // A literal `__proto__:` key sets the prototype instead of naming a\n // column, so the column would vanish from `Object.keys`.\n if (Object.getPrototypeOf(specs) !== Object.prototype || Object.hasOwn(specs, '__proto__')) {\n throw new Error('Columns.define: \"__proto__\" cannot name a column.')\n }\n const ids = Object.keys(specs)\n const byId: Record<string, Column<Row, string, unknown>> = {}\n for (const id of ids) {\n if (arrayIndex.test(id)) {\n throw new Error(\n `Columns.define: the column id \"${id}\" is an array index, which JavaScript orders before every other key. Give it a name that is not a number.`,\n )\n }\n byId[id] = Object.freeze({ ...specs[id]!, id })\n }\n return Object.freeze({\n ids: Object.freeze(ids),\n byId: Object.freeze(byId),\n }) as Columns<Row, Specs>\n },\n}\n","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":";;;;;;;AA6CA,MAAa,aAAa,kBAAkB;CAC1C,MAAM,CAAC;;CAEP,QAAQ,CAAC;;CAET,QAAQ,EAAE,SAAS,OAAO,MAAM,OAAO,MAAM,EAAE;;CAE/C,MAAM,CAAC;AACT,CAAC;;;;;;;AASD,MAAM,UAAU;AAEhB,MAAM,UAAU,QAAgC;CAC9C,IAAI,CAAC,UAAU,cAAc,GAAG,GAAG,OAAO;CAC1C,MAAM,iBAA0B,IAAI,cAAc;CAClD,OACE,OAAO,mBAAmB,YAC1B,mBAAmB,QACnB,QAAQ,kBACR,eAAe,OAAO;AAE1B;;AAGA,MAAa,YAAY,WAA+C;CACtE,IAAI,WAAW,KAAA,GAAW,OAAO,WAAW,KAAK;CACjD,MAAM,UAAU,UAAU,UAAU,OAAO,GAAG;CAC9C,IAAI,UAAU,QAAQ,OAAO,GAAG;EAC9B,MAAM,UAAU,QAAQ,MAAM,SAAQ,WACpC,UAAU,UAAU,MAAM,KAAK,OAAO,OAAO,YAAY,WAAW,CAAC,OAAO,OAAO,IAAI,CAAC,CAC1F;EAEA,IAAI,QAAQ,WAAW,QAAQ,MAAM,QAAQ,OAAO,WAAW,OAAO,EAAE,QAAQ,CAAC;CACnF;CACA,MAAM,OAAO,UAAU,OAAO,OAAO,GAAG;CACxC,IAAI,OAAO,IAAI,GAAG,OAAO,WAAW,KAAK;CACzC,OAAO,UAAU,SAAS,IAAI,IAAI,WAAW,OAAO,IAAI,WAAW,KAAK;AAC1E;AA2CA,MAAM,aAAa;AAEnB,MAAa,UAAU;;CAErB,cAAc;;CAEd,UAAU;;CAEV,SAAS,UAA4B,UAAU,QAAQ,UAAU,KAAA,IAAY,K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AQ,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"}
1
+ {"version":3,"file":"rows-Dy1HPpGX.mjs","names":[],"sources":["../src/columns.ts","../src/rows.ts"],"sourcesContent":["import { Schema, SchemaAST } from 'effect'\nimport { defineTaggedUnion } from 'foldkit/schema'\n/** What a column shows and where it starts out. Its id is its key in `Columns.define`. */\nexport interface ColumnSpec<Row, Value> {\n readonly header: string\n readonly value: (row: Row) => Value\n /** The region the column starts in; `ColumnLayout.initial` reads it. */\n readonly pinned?: 'start' | 'end'\n /** Whether the column starts hidden; `ColumnLayout.initial` reads it. */\n readonly hidden?: boolean\n /** Its width in pixels until resized. Default `Columns.defaultWidth`. */\n readonly width?: number\n /** The narrowest a resize makes it. Default `Columns.minWidth`. */\n readonly minWidth?: number\n /** The widest a resize makes it. Default no limit. */\n readonly maxWidth?: number\n /** Whether it can be resized. Default `true`. */\n readonly resizable?: boolean\n /** Whether it can be hidden. Default `true`. */\n readonly hideable?: boolean\n /**\n * Makes the column's cells editable as text. The grid holds the draft,\n * commits it only when the column's schema decodes it, and reports the\n * committed text as an `Edited` OutMessage; `matchEdit` reads its value\n * back, and writing it is the application's.\n */\n readonly edit?: ColumnEdit<Row, unknown>\n}\n\nexport interface ColumnEdit<Row, Value = string> {\n /** The text an edit of a row's cell begins with. Default the value as a string. */\n readonly draft?: (row: Row) => string\n /**\n * What the text means: from the text typed to the column's value. A draft\n * is committed only when it decodes, and the failure's message is the\n * cell's error, so a check's own `message` is what a person reads. Default\n * any text, as itself.\n */\n readonly schema?: Schema.Codec<Value, string>\n}\n\n/**\n * How a cell's edit is drawn, read from its column's schema: a choice of the\n * literals its text may be, a number typed, or text.\n */\nexport const CellEditor = defineTaggedUnion({\n Text: {},\n /** The schema decodes to a number: typed with a decimal keypad on touch. */\n Number: {},\n /** The schema's text is one of these, offered in order. */\n Choice: { options: Schema.Array(Schema.String) },\n /** The schema decodes to a date: a native date field over the draft text. */\n Date: {},\n})\nexport type CellEditor = typeof CellEditor.Type\n\n/**\n * Whether a schema decodes to a date, found as Effect's own tools find it:\n * by the declaration's representation id. Only Date-ness is read here, never\n * the text format: committing decodes the draft through the schema, and a\n * refusal is the cell's error.\n */\nconst DATE_ID = 'effect/schema/Date'\n\nconst isDate = (ast: SchemaAST.AST): boolean => {\n if (!SchemaAST.isDeclaration(ast)) return false\n const representation: unknown = ast.annotations?.['representation']\n return (\n typeof representation === 'object' &&\n representation !== null &&\n 'id' in representation &&\n representation.id === DATE_ID\n )\n}\n\n/** A column's editor, from its schema; a column with none edits text. */\nexport const editorOf = (schema: Schema.Top | undefined): CellEditor => {\n if (schema === undefined) return CellEditor.Text()\n const encoded = SchemaAST.toEncoded(schema.ast)\n if (SchemaAST.isUnion(encoded)) {\n const options = encoded.types.flatMap(member =>\n SchemaAST.isLiteral(member) && typeof member.literal === 'string' ? [member.literal] : [],\n )\n // Every member a text literal: anything else typed would be refused.\n if (options.length === encoded.types.length) return CellEditor.Choice({ options })\n }\n const type = SchemaAST.toType(schema.ast)\n if (isDate(type)) return CellEditor.Date()\n return SchemaAST.isNumber(type) ? CellEditor.Number() : CellEditor.Text()\n}\n\n/** The ids of the columns that edit. */\nexport type EditableId<Specs> = {\n [K in keyof Specs]: Specs[K] extends { readonly edit: object } ? K : never\n}[keyof Specs] &\n string\n\n/** What an editable column's text decodes to: its schema's value, or the text. */\nexport type EditValue<Spec> = Spec extends {\n readonly edit: { readonly schema: Schema.Codec<infer Value, string> }\n}\n ? Value\n : string\n\nexport interface Column<Row, Id extends string, Value> extends ColumnSpec<Row, Value> {\n readonly id: Id\n}\n\ntype ValueOf<Spec> = Spec extends ColumnSpec<never, infer Value> ? Value : never\n\nexport type ColumnId<C> = C extends Columns<never, infer Specs> ? keyof Specs & string : never\n\n/**\n * A grid's columns, keyed by stable id. Order, visibility and pinning live in\n * a `ColumnLayout`, so every reference to a column (focus, a selected range,\n * a saved width) names its id and survives a reorder.\n */\nexport interface Columns<Row, Specs extends Record<string, ColumnSpec<Row, unknown>>> {\n /** In definition order: the order `ColumnLayout.initial` starts from. */\n readonly ids: ReadonlyArray<keyof Specs & string>\n readonly byId: {\n readonly [Id in keyof Specs & string]: Column<Row, Id, ValueOf<Specs[Id]>>\n }\n /**\n * Type-only: the specs as written, so a grid made from these columns infers\n * each one exactly, its edit's schema included. Never set.\n */\n readonly Specs?: Specs\n}\n\n// Integer-like keys are enumerated before every other key, whatever the order\n// they were written in, so `{ name, 2024 }` would put 2024 first.\nconst arrayIndex = /^(0|[1-9][0-9]*)$/\n\nexport const Columns = {\n /** The width of a column that names none. */\n defaultWidth: 120,\n /** The narrowest a column with no `minWidth` resizes to. */\n minWidth: 40,\n /** A value as a cell shows it: nothing for `null` or `undefined`. */\n textOf: (value: unknown): string => (value === null || value === undefined ? '' : String(value)),\n /**\n * Declares a grid's columns over `Row`. Call it twice, once with the row\n * type and once with the specs, so the ids and values are inferred:\n * `Columns.define<Product>()({ sku: { header: 'SKU', value: p => p.sku } })`.\n */\n define:\n <Row>() =>\n <Specs extends Record<string, ColumnSpec<Row, unknown>>>(\n specs: Specs,\n ): NoInfer<Columns<Row, Specs>> => {\n // A literal `__proto__:` key sets the prototype instead of naming a\n // column, so the column would vanish from `Object.keys`.\n if (Object.getPrototypeOf(specs) !== Object.prototype || Object.hasOwn(specs, '__proto__')) {\n throw new Error('Columns.define: \"__proto__\" cannot name a column.')\n }\n const ids = Object.keys(specs)\n const byId: Record<string, Column<Row, string, unknown>> = {}\n for (const id of ids) {\n if (arrayIndex.test(id)) {\n throw new Error(\n `Columns.define: the column id \"${id}\" is an array index, which JavaScript orders before every other key. Give it a name that is not a number.`,\n )\n }\n byId[id] = Object.freeze({ ...specs[id]!, id })\n }\n return Object.freeze({\n ids: Object.freeze(ids),\n byId: Object.freeze(byId),\n }) as Columns<Row, Specs>\n },\n}\n","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":";;;;;;;AA6CA,MAAa,aAAa,kBAAkB;CAC1C,MAAM,CAAC;;CAEP,QAAQ,CAAC;;CAET,QAAQ,EAAE,SAAS,OAAO,MAAM,OAAO,MAAM,EAAE;;CAE/C,MAAM,CAAC;AACT,CAAC;;;;;;;AASD,MAAM,UAAU;AAEhB,MAAM,UAAU,QAAgC;CAC9C,IAAI,CAAC,UAAU,cAAc,GAAG,GAAG,OAAO;CAC1C,MAAM,iBAA0B,IAAI,cAAc;CAClD,OACE,OAAO,mBAAmB,YAC1B,mBAAmB,QACnB,QAAQ,kBACR,eAAe,OAAO;A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AG;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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foldkit-data-grid",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "description": "A Foldkit-native data grid: the grid owns interaction state and geometry, the application owns the records.",
5
5
  "keywords": [
6
6
  "foldkit",
@@ -48,8 +48,8 @@
48
48
  "peerDependencies": {
49
49
  "effect": "^4.0.0",
50
50
  "foldkit": "^0.169.0",
51
- "foldkit-crud": "0.8.3",
52
- "foldkit-remote": "0.18.1"
51
+ "foldkit-crud": "0.10.0",
52
+ "foldkit-remote": "0.20.0"
53
53
  },
54
54
  "peerDependenciesMeta": {
55
55
  "foldkit-crud": {
@@ -64,9 +64,9 @@
64
64
  "foldkit": "0.169.0",
65
65
  "tsdown": "^0.23.1",
66
66
  "typescript": "7.0.2",
67
- "foldkit-crud": "0.8.3",
68
- "foldkit-entity": "0.10.0",
69
- "foldkit-remote": "0.18.1"
67
+ "foldkit-crud": "0.10.0",
68
+ "foldkit-remote": "0.20.0",
69
+ "foldkit-entity": "0.12.0"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsdown",
@@ -1 +0,0 @@
1
- {"version":3,"file":"rows-TYJdJ7tK.d.mts","names":[],"s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