{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-grid",
  "type": "registry:lib",
  "title": "core/grid",
  "description": "Framework-free behaviour shared by the ui-* components (grid); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-field"
  ],
  "files": [
    {
      "path": "src/core/grid.ts",
      "type": "registry:lib",
      "target": "components/ui/core/grid.ts",
      "content": "/**\n * Editable data grid behaviour (WAI-ARIA APG grid pattern), framework-free: where a key moves the\n * active body cell, which keys open a cell's editor, and how an edited value is read and saved.\n * Carbon's DataTable has no inline editing; the data tables follow the APG pattern with Carbon styling.\n */\nimport { valueText } from './field.ts';\n\n/** Editor of a column whose cells can be edited in place. */\nexport type CellEditor = 'text' | 'number' | 'select';\n\n/** A body cell: its row among the data rows (child rows of open rows don't count) and its index in that row. */\nexport interface GridCell {\n    row: number;\n    cell: number;\n}\n\n/**\n * The cell a navigation key moves to from `at`: arrows one step, Home / End the row's first / last\n * cell, with Ctrl (or Cmd) the first / last row's. `null` when `key` doesn't navigate; `at` itself\n * when the move would leave the grid (the key is still the grid's).\n */\nexport function gridMove(key: string, ctrl: boolean, at: GridCell, rowCount: number, cellCount: number): GridCell | null {\n    const inside = (target: GridCell) => (target.row >= 0 && target.row < rowCount && target.cell >= 0 && target.cell < cellCount ? target : at);\n    switch (key) {\n        case 'ArrowRight':\n            return inside({ row: at.row, cell: at.cell + 1 });\n        case 'ArrowLeft':\n            return inside({ row: at.row, cell: at.cell - 1 });\n        case 'ArrowDown':\n            return inside({ row: at.row + 1, cell: at.cell });\n        case 'ArrowUp':\n            return inside({ row: at.row - 1, cell: at.cell });\n        case 'Home':\n            return { row: ctrl ? 0 : at.row, cell: 0 };\n        case 'End':\n            return { row: ctrl ? Math.max(0, rowCount - 1) : at.row, cell: Math.max(0, cellCount - 1) };\n        default:\n            return null;\n    }\n}\n\n/** The keys of a focused editable cell: Enter / F2 open the editor, a printable character opens it with that character (not in a select). */\nexport function cellEditKey(event: { key: string; ctrlKey?: boolean; metaKey?: boolean; altKey?: boolean }, editor: CellEditor): 'edit' | 'type' | null {\n    if (event.key === 'Enter' || event.key === 'F2') return 'edit';\n    const typed = event.key.length === 1 && event.key !== ' ' && !event.ctrlKey && !event.metaKey && !event.altKey;\n    return typed && editor !== 'select' ? 'type' : null;\n}\n\n/** A cell's value as the editor's starting text (null and undefined start empty). */\nexport const cellDraft = (value: unknown) => valueText(value);\n\n/** The editor's text as the saved value: a number for a number column (unless left empty), else the text. */\nexport const parseCellValue = (editor: CellEditor, draft: string): string | number => (editor === 'number' && draft.trim() !== '' ? Number(draft) : draft);\n\n/**\n * `rows` with `key` of row `rowId` set to `value` (a new array and a new row; the others are kept),\n * or null when nothing changes (no such row, or the same value).\n */\nexport function applyCellEdit<R extends { id: string }>(rows: readonly R[], rowId: string, key: string, value: unknown): { rows: R[]; row: R; previous: unknown } | null {\n    const row = rows.find((r) => r.id === rowId);\n    if (!row) return null;\n    const previous = (row as Record<string, unknown>)[key];\n    if (previous === value) return null;\n    const updated = { ...row, [key]: value };\n    return { rows: rows.map((r) => (r.id === rowId ? updated : r)), row: updated, previous };\n}\n"
    }
  ]
}
