import type { Readable } from 'svelte/store'; import type { BodyCell, BodyCellAttributes } from '../bodyCells.js'; import type { BodyRow, BodyRowAttributes } from '../bodyRows.js'; import type { DataColumn, FlatColumn } from '../columns.js'; import type { PluginInitTableState, TableAttributes, TableBodyAttributes, TableHeadAttributes } from '../createViewModel.js'; import type { HeaderCell, HeaderCellAttributes } from '../headerCells.js'; import type { HeaderRow, HeaderRowAttributes } from '../headerRows.js'; /** * A table plugin factory function. * Receives initialization options and returns a plugin instance. * * @template Item - The type of data items in the table. * @template PluginState - The state exposed by the plugin. * @template ColumnOptions - Per-column configuration options. * @template TablePropSet - Props added to table components. * @template TableAttributeSet - Attributes added to table components. */ export type TablePlugin = (_init: TablePluginInit) => TablePluginInstance; /** * Initialization options passed to a table plugin. * * @template Item - The type of data items in the table. * @template ColumnOptions - Per-column configuration options. */ export type TablePluginInit = { /** The name/key of this plugin in the plugins object. */ pluginName: string; /** The table state during plugin initialization. */ tableState: PluginInitTableState; /** Column options keyed by column ID. */ columnOptions: Record; }; /** * A plugin instance returned by a TablePlugin factory. * Contains state, transformation functions, and component hooks. * * @template Item - The type of data items in the table. * @template PluginState - The state exposed by the plugin. * @template ColumnOptions - Per-column configuration options. * @template TablePropSet - Props added to table components. * @template TableAttributeSet - Attributes added to table components. */ export type TablePluginInstance = { pluginState: PluginState; transformFlatColumnsFn?: Readable>; deriveFlatColumns?: DeriveFlatColumnsFn; deriveRows?: DeriveRowsFn; derivePageRows?: DeriveRowsFn; deriveTableAttrs?: DeriveFn>; deriveTableHeadAttrs?: DeriveFn>; deriveTableBodyAttrs?: DeriveFn>; columnOptions?: ColumnOptions; hooks?: TableHooks; }; /** * A record of table plugins, keyed by plugin name. * Used as a type constraint for the plugins parameter. */ export type AnyPlugins = Record>; /** * A record of plugin instances, keyed by plugin name. * Used internally after plugins are initialized. */ export type AnyPluginInstances = Record>; /** * A synchronous function that transforms flat columns. * * @template Item - The type of data items in the table. */ export type TransformFlatColumnsFn = (_flatColumns: DataColumn[]) => DataColumn[]; /** * A reactive function that derives flat columns from a store. * * @template Item - The type of data items in the table. */ export type DeriveFlatColumnsFn = >(_flatColumns: Readable) => Readable; /** * A reactive function that derives rows from a store. * * @template Item - The type of data items in the table. */ export type DeriveRowsFn = >(_rows: Readable) => Readable; /** * A generic reactive derivation function. * * @template T - The type being derived. */ export type DeriveFn = (_obj: Readable) => Readable; /** * Maps component keys to their corresponding component types. * * @template Item - The type of data items in the table. * @template Plugins - The plugins used by the table. */ export type Components = { 'thead.tr': HeaderRow; 'thead.tr.th': HeaderCell; 'tbody.tr': BodyRow; 'tbody.tr.td': BodyCell; }; /** * Maps component keys to their corresponding attribute types. * * @template Item - The type of data items in the table. * @template Plugins - The plugins used by the table. */ export type AttributesForKey = { 'thead.tr': HeaderRowAttributes; 'thead.tr.th': HeaderCellAttributes; 'tbody.tr': BodyRowAttributes; 'tbody.tr.td': BodyCellAttributes; }; /** * Valid keys for table components: header rows, header cells, body rows, body cells. */ export type ComponentKeys = keyof Components; type TablePropSet = { [K in ComponentKeys]: PropSet[K]; }; /** * Creates a new table prop set type, filtering out undefined component props. * * @template PropSet - The prop set definition. */ export type NewTablePropSet = { [K in ComponentKeys]: unknown extends PropSet[K] ? never : PropSet[K]; }; /** * A table prop set with any types. Used as a type constraint. */ export type AnyTablePropSet = TablePropSet; /** * Internal type for mapping component keys to attribute sets. * @internal */ type TableAttributeSet = { [K in ComponentKeys]: AttributeSet[K]; }; /** * Creates a new table attribute set type, filtering out undefined component attributes. * * @template AttributeSet - The attribute set definition. */ export type NewTableAttributeSet = { [K in ComponentKeys]: unknown extends AttributeSet[K] ? never : AttributeSet[K]; }; /** * A table attribute set with any types. Used as a type constraint. */ export type AnyTableAttributeSet = TableAttributeSet; /** * Hooks for attaching props and attributes to table components. * Each hook receives a component and returns props/attrs stores. * * @template Item - The type of data items in the table. * @template PropSet - The prop set type. * @template AttributeSet - The attribute set type. */ export type TableHooks = { [ComponentKey in keyof Components]?: (_component: Components[ComponentKey]) => ElementHook; }; /** * Return type for component hooks. * Contains optional readable stores for props and attributes. * * @template Props - The props type. * @template Attributes - The attributes type. */ export type ElementHook = { /** Reactive props store. */ props?: Readable; /** Reactive attributes store. */ attrs?: Readable; }; /** * Extracts the plugin state types from a plugins record. * * @template Plugins - The plugins record type. */ export type PluginStates = { [K in keyof Plugins]: ReturnType['pluginState']; }; /** * Internal type for extracting prop sets from plugins. * @internal */ type TablePropSetForPluginKey = { [K in keyof Plugins]: Plugins[K] extends TablePlugin ? TablePropSet : never; }; /** * Combines prop sets from all plugins into a single type. * Props are grouped by component key, then by plugin key. * * @template Plugins - The plugins record type. */ export type PluginTablePropSet = { [ComponentKey in ComponentKeys]: { [PluginKey in keyof Plugins]: TablePropSetForPluginKey[PluginKey][ComponentKey]; }; }; /** * Extracts column configuration types from all plugins. * Used to type the `plugins` option in column definitions. * * @template Plugins - The plugins record type. */ export type PluginColumnConfigs = Partial<{ [K in keyof Plugins]: ReturnType['columnOptions']; }>; export {};