@macrostrat/data-sheet 4.0.0-dev3 → 4.0.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.
Files changed (212) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +44 -0
  3. package/dist/actions/builtin.cjs +36 -5
  4. package/dist/actions/builtin.cjs.map +1 -1
  5. package/dist/actions/builtin.d.ts +3 -2
  6. package/dist/actions/builtin.js +36 -5
  7. package/dist/actions/builtin.js.map +1 -1
  8. package/dist/actions/column-controls.cjs +149 -0
  9. package/dist/actions/column-controls.cjs.map +1 -0
  10. package/dist/actions/column-controls.d.ts +11 -0
  11. package/dist/actions/column-controls.js +147 -0
  12. package/dist/actions/column-controls.js.map +1 -0
  13. package/dist/actions/column-filter.cjs +68 -0
  14. package/dist/actions/column-filter.cjs.map +1 -0
  15. package/dist/actions/column-filter.d.ts +11 -0
  16. package/dist/actions/column-filter.js +66 -0
  17. package/dist/actions/column-filter.js.map +1 -0
  18. package/dist/actions/edit-actions.cjs +42 -0
  19. package/dist/actions/edit-actions.cjs.map +1 -0
  20. package/dist/actions/edit-actions.d.ts +14 -0
  21. package/dist/actions/edit-actions.js +42 -0
  22. package/dist/actions/edit-actions.js.map +1 -0
  23. package/dist/actions/filters.cjs +39 -23
  24. package/dist/actions/filters.cjs.map +1 -1
  25. package/dist/actions/filters.d.ts +6 -3
  26. package/dist/actions/filters.js +41 -25
  27. package/dist/actions/filters.js.map +1 -1
  28. package/dist/actions/index.d.ts +4 -0
  29. package/dist/actions/selection.cjs +51 -32
  30. package/dist/actions/selection.cjs.map +1 -1
  31. package/dist/actions/selection.d.ts +3 -7
  32. package/dist/actions/selection.js +51 -32
  33. package/dist/actions/selection.js.map +1 -1
  34. package/dist/actions/table-controls.cjs +41 -0
  35. package/dist/actions/table-controls.cjs.map +1 -0
  36. package/dist/actions/table-controls.d.ts +4 -0
  37. package/dist/actions/table-controls.js +39 -0
  38. package/dist/actions/table-controls.js.map +1 -0
  39. package/dist/actions/toolbar.cjs +69 -10
  40. package/dist/actions/toolbar.cjs.map +1 -1
  41. package/dist/actions/toolbar.d.ts +9 -6
  42. package/dist/actions/toolbar.js +69 -10
  43. package/dist/actions/toolbar.js.map +1 -1
  44. package/dist/actions/toolbar.module.sass.cjs +12 -7
  45. package/dist/actions/toolbar.module.sass.cjs.map +1 -1
  46. package/dist/actions/toolbar.module.sass.js +12 -7
  47. package/dist/actions/toolbar.module.sass.js.map +1 -1
  48. package/dist/actions/types.d.ts +49 -4
  49. package/dist/cell-renderer.cjs +150 -14
  50. package/dist/cell-renderer.cjs.map +1 -1
  51. package/dist/cell-renderer.d.ts +1 -1
  52. package/dist/cell-renderer.js +151 -15
  53. package/dist/cell-renderer.js.map +1 -1
  54. package/dist/components/cell-detail.cjs +24 -0
  55. package/dist/components/cell-detail.cjs.map +1 -0
  56. package/dist/components/cell-detail.d.ts +12 -0
  57. package/dist/components/cell-detail.js +24 -0
  58. package/dist/components/cell-detail.js.map +1 -0
  59. package/dist/components/editor-popup.cjs +28 -24
  60. package/dist/components/editor-popup.cjs.map +1 -1
  61. package/dist/components/editor-popup.js +29 -25
  62. package/dist/components/editor-popup.js.map +1 -1
  63. package/dist/components/index.d.ts +2 -2
  64. package/dist/components/text-areas.cjs +51 -7
  65. package/dist/components/text-areas.cjs.map +1 -1
  66. package/dist/components/text-areas.d.ts +4 -1
  67. package/dist/components/text-areas.js +51 -7
  68. package/dist/components/text-areas.js.map +1 -1
  69. package/dist/components/view-info.cjs +46 -9
  70. package/dist/components/view-info.cjs.map +1 -1
  71. package/dist/components/view-info.d.ts +5 -1
  72. package/dist/components/view-info.js +47 -10
  73. package/dist/components/view-info.js.map +1 -1
  74. package/dist/core.cjs +167 -73
  75. package/dist/core.cjs.map +1 -1
  76. package/dist/core.d.ts +53 -6
  77. package/dist/core.js +169 -75
  78. package/dist/core.js.map +1 -1
  79. package/dist/data-sheet.css +72 -123
  80. package/dist/filters/operators.cjs +91 -0
  81. package/dist/filters/operators.cjs.map +1 -0
  82. package/dist/filters/operators.d.ts +17 -0
  83. package/dist/filters/operators.js +91 -0
  84. package/dist/filters/operators.js.map +1 -0
  85. package/dist/index.cjs +38 -10
  86. package/dist/index.cjs.map +1 -1
  87. package/dist/index.d.ts +2 -0
  88. package/dist/index.js +44 -16
  89. package/dist/index.js.map +1 -1
  90. package/dist/main.module.sass.cjs +13 -12
  91. package/dist/main.module.sass.cjs.map +1 -1
  92. package/dist/main.module.sass.js +13 -12
  93. package/dist/main.module.sass.js.map +1 -1
  94. package/dist/postgrest-table/data-loaders.cjs +255 -146
  95. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  96. package/dist/postgrest-table/data-loaders.d.ts +144 -46
  97. package/dist/postgrest-table/data-loaders.js +259 -150
  98. package/dist/postgrest-table/data-loaders.js.map +1 -1
  99. package/dist/postgrest-table/index.cjs +58 -169
  100. package/dist/postgrest-table/index.cjs.map +1 -1
  101. package/dist/postgrest-table/index.d.ts +5 -2
  102. package/dist/postgrest-table/index.js +63 -174
  103. package/dist/postgrest-table/index.js.map +1 -1
  104. package/dist/postgrest-table/loading-utils.cjs +0 -4
  105. package/dist/postgrest-table/loading-utils.cjs.map +1 -1
  106. package/dist/postgrest-table/loading-utils.js +1 -5
  107. package/dist/postgrest-table/loading-utils.js.map +1 -1
  108. package/dist/postgrest-table/main.module.sass.cjs +14 -13
  109. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  110. package/dist/postgrest-table/main.module.sass.js +14 -13
  111. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  112. package/dist/provider.cjs +68 -11
  113. package/dist/provider.cjs.map +1 -1
  114. package/dist/provider.d.ts +25 -0
  115. package/dist/provider.js +68 -11
  116. package/dist/provider.js.map +1 -1
  117. package/dist/renderers/column-header-cell.cjs +95 -75
  118. package/dist/renderers/column-header-cell.cjs.map +1 -1
  119. package/dist/renderers/column-header-cell.d.ts +3 -18
  120. package/dist/renderers/column-header-cell.js +98 -78
  121. package/dist/renderers/column-header-cell.js.map +1 -1
  122. package/dist/renderers/index.d.ts +0 -1
  123. package/dist/types.cjs +2 -0
  124. package/dist/types.cjs.map +1 -1
  125. package/dist/types.d.ts +84 -1
  126. package/dist/types.js +2 -0
  127. package/dist/types.js.map +1 -1
  128. package/dist/utils/column-spec.cjs.map +1 -1
  129. package/dist/utils/column-spec.d.ts +101 -1
  130. package/dist/utils/column-spec.js.map +1 -1
  131. package/dist/utils/debug-overlay.cjs +24 -0
  132. package/dist/utils/debug-overlay.cjs.map +1 -0
  133. package/dist/utils/debug-overlay.d.ts +4 -0
  134. package/dist/utils/debug-overlay.js +24 -0
  135. package/dist/utils/debug-overlay.js.map +1 -0
  136. package/dist/utils/index.cjs.map +1 -1
  137. package/dist/utils/index.d.ts +2 -0
  138. package/dist/utils/index.js.map +1 -1
  139. package/dist/utils/key-handling.cjs +52 -6
  140. package/dist/utils/key-handling.cjs.map +1 -1
  141. package/dist/utils/key-handling.js +52 -6
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/utils/validation.cjs +41 -0
  144. package/dist/utils/validation.cjs.map +1 -0
  145. package/dist/utils/validation.d.ts +26 -0
  146. package/dist/utils/validation.js +41 -0
  147. package/dist/utils/validation.js.map +1 -0
  148. package/dist/zustand-store.cjs +119 -61
  149. package/dist/zustand-store.cjs.map +1 -1
  150. package/dist/zustand-store.js +121 -63
  151. package/dist/zustand-store.js.map +1 -1
  152. package/package.json +3 -3
  153. package/src/actions/builtin.ts +62 -7
  154. package/src/actions/column-controls.ts +185 -0
  155. package/src/actions/column-filter.ts +104 -0
  156. package/src/actions/edit-actions.ts +67 -0
  157. package/src/actions/filters.ts +54 -28
  158. package/src/actions/index.ts +4 -0
  159. package/src/actions/selection.ts +73 -54
  160. package/src/actions/table-controls.ts +46 -0
  161. package/src/actions/toolbar.module.sass +22 -2
  162. package/src/actions/toolbar.ts +113 -16
  163. package/src/actions/types.ts +49 -4
  164. package/src/cell-renderer.ts +211 -16
  165. package/src/components/cell-detail.ts +30 -0
  166. package/src/components/editor-popup.ts +39 -25
  167. package/src/components/index.ts +2 -2
  168. package/src/components/text-areas.ts +80 -16
  169. package/src/components/view-info.ts +63 -4
  170. package/src/core.ts +321 -103
  171. package/src/filters/operators.ts +127 -0
  172. package/src/index.ts +2 -0
  173. package/src/main.module.sass +6 -0
  174. package/src/postgrest-table/data-loaders.ts +459 -209
  175. package/src/postgrest-table/index.ts +81 -254
  176. package/src/postgrest-table/main.module.sass +11 -0
  177. package/src/provider.ts +109 -12
  178. package/src/renderers/column-header-cell.ts +60 -272
  179. package/src/renderers/index.ts +0 -1
  180. package/src/types.ts +76 -2
  181. package/src/utils/column-spec.ts +112 -1
  182. package/src/utils/debug-overlay.ts +22 -0
  183. package/src/utils/index.ts +2 -0
  184. package/src/utils/key-handling.ts +70 -6
  185. package/src/utils/validation.ts +72 -0
  186. package/src/zustand-store.ts +167 -60
  187. package/dist/components/actions.cjs +0 -12
  188. package/dist/components/actions.cjs.map +0 -1
  189. package/dist/components/actions.d.ts +0 -2
  190. package/dist/components/actions.js +0 -12
  191. package/dist/components/actions.js.map +0 -1
  192. package/dist/components/actions.module.sass.cjs +0 -12
  193. package/dist/components/actions.module.sass.cjs.map +0 -1
  194. package/dist/components/actions.module.sass.js +0 -11
  195. package/dist/components/actions.module.sass.js.map +0 -1
  196. package/dist/postgrest-table/lazy-loader-table.cjs +0 -27
  197. package/dist/postgrest-table/lazy-loader-table.cjs.map +0 -1
  198. package/dist/postgrest-table/lazy-loader-table.d.ts +0 -3
  199. package/dist/postgrest-table/lazy-loader-table.js +0 -27
  200. package/dist/postgrest-table/lazy-loader-table.js.map +0 -1
  201. package/dist/renderers/column-header-cell-postgrest.cjs +0 -216
  202. package/dist/renderers/column-header-cell-postgrest.cjs.map +0 -1
  203. package/dist/renderers/column-header-cell-postgrest.d.ts +0 -13
  204. package/dist/renderers/column-header-cell-postgrest.js +0 -216
  205. package/dist/renderers/column-header-cell-postgrest.js.map +0 -1
  206. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +0 -23
  207. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +0 -1
  208. package/dist/renderers/column-header-cell-postgrest.module.sass.js +0 -22
  209. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +0 -1
  210. package/src/components/actions.ts +0 -9
  211. package/src/postgrest-table/lazy-loader-table.ts +0 -25
  212. package/src/renderers/column-header-cell-postgrest.ts +0 -282
@@ -7,15 +7,115 @@ interface ColumnSpecGenerateOptions {
7
7
  /** Inferred data type of a column, used to select appropriate
8
8
  * sort/filter operators. */
9
9
  export type ColumnDataType = "text" | "string" | "number" | "integer" | "boolean" | "object" | "array";
10
+ /** Severity of a cell validation result. `warning` is soft (flagged, doesn't
11
+ * block saving); `error` is hard (blocks saving). */
12
+ export type ValidationSeverity = "warning" | "error";
13
+ /** The result of validating a cell. `null` means valid. */
14
+ export interface CellValidation {
15
+ severity: ValidationSeverity;
16
+ message?: string;
17
+ }
18
+ /**
19
+ * Context handed to per-cell renderers (`valueRenderer`, and — as the
20
+ * `cellContext` prop — a custom `cellComponent`). It lets a renderer draw
21
+ * based on the row/column position and the cell's edit status, and, combined
22
+ * with the editing API, write edits back for a specific cell.
23
+ *
24
+ * `rowIndex` is the underlying **data-row index** (the value edit methods
25
+ * expect), so it stays stable when a sort or filter reorders the view.
26
+ */
27
+ export interface CellRenderContext<T = any> {
28
+ /** The cell's current value (the edited value if present, else the base). */
29
+ value: any;
30
+ /** Underlying data-row index — stable under sort/filter. */
31
+ rowIndex: number;
32
+ /** Column index within the active column spec. */
33
+ colIndex: number;
34
+ /** The full column spec for this cell's column. */
35
+ column: ColumnSpec;
36
+ /** The full row object backing this cell (may be undefined while loading). */
37
+ row: T | undefined;
38
+ /** Whether this cell has an uncommitted edit. */
39
+ isEdited: boolean;
40
+ /** Whether this cell's row is marked for deletion. */
41
+ isDeleted: boolean;
42
+ /** Validation result for this cell, or `null` when valid. Orthogonal to the
43
+ * edit status — a cell can be both edited and invalid. */
44
+ validation: CellValidation | null;
45
+ }
46
+ /**
47
+ * The editor configuration for a cell. Mirrors the static `dataEditor` /
48
+ * `inlineEditor` column fields so a per-cell resolver (`editorForCell`) can
49
+ * override either one for an individual cell.
50
+ */
51
+ export interface CellEditors {
52
+ dataEditor?: any;
53
+ inlineEditor?: boolean | React.ComponentType<any> | string | null;
54
+ }
55
+ /**
56
+ * Context for a cell's detail surface (`cellDetail`). Extends the render
57
+ * context with the edit affordances and controls a surface needs, so one
58
+ * component can act as an editor (when `editable`) or a read-only viewer
59
+ * (otherwise). This is the write side of the read/write contract, per-cell.
60
+ */
61
+ export interface CellDetailContext<T = any> extends CellRenderContext<T> {
62
+ /** Whether this cell is editable (column × table × not-deleted). */
63
+ editable: boolean;
64
+ /** Commit a new value for this cell. */
65
+ onChange: (value: any) => void;
66
+ /** Reset this cell (and selection) to its base value. */
67
+ resetValue: () => void;
68
+ /** Close the surface and return focus to the table. */
69
+ close: () => void;
70
+ }
71
+ /** How a cell's detail surface is presented. Orthogonal to what it renders. */
72
+ export type DetailPresentation = "popover" | "modal" | "inline";
10
73
  export interface ColumnSpec {
11
74
  name: string;
12
75
  key: string;
76
+ /** When true, an empty value is an error (sugar over `validate`). */
13
77
  required?: boolean;
78
+ /** Validate a cell's value. Return a `{ severity, message }` for a
79
+ * warning/error, or `null` when valid. Runs after the `required` check.
80
+ * `warning` flags the cell but allows saving; `error` blocks saving. */
81
+ validate?: (value: any, row: any, ctx: {
82
+ rowIndex: number;
83
+ }) => CellValidation | null;
84
+ /** @deprecated Prefer `validate`, which carries severity + a message.
85
+ * A falsy result maps to an `error`. */
14
86
  isValid?: (d: any) => boolean;
15
87
  transformValue?: (d: any) => any;
16
- valueRenderer?: (d: any) => string | React.ReactNode;
88
+ valueRenderer?: (d: any, ctx?: CellRenderContext) => string | React.ReactNode;
17
89
  headerRenderer?: (d: any) => string | React.ReactNode;
18
90
  dataEditor?: any;
91
+ /**
92
+ * Choose the editor for an individual cell from its render context,
93
+ * overriding the static `dataEditor` / `inlineEditor` for that cell. Return
94
+ * `undefined` (or omit a key) to fall back to the static configuration —
95
+ * e.g. show a textarea only for cells whose value is long. A returned key is
96
+ * respected even when its value is `false`/`null` (i.e. "no editor here").
97
+ */
98
+ editorForCell?: (ctx: CellRenderContext) => CellEditors | undefined;
99
+ /**
100
+ * Render a read-only detail panel for a cell. When set, selecting the cell
101
+ * opens a popover with this content (following the table's `cellInteraction`
102
+ * mode). The panel never takes keyboard focus, so arrow keys keep navigating
103
+ * the table; Escape closes it. Mutually distinct from `dataEditor` — use this
104
+ * for non-editable surfaces (previews, summaries, links).
105
+ *
106
+ * @deprecated Prefer `cellDetail`, which unifies editor and viewer.
107
+ */
108
+ detailRenderer?: (ctx: CellRenderContext) => React.ReactNode;
109
+ /**
110
+ * The unified cell surface (Workstream A): one renderer that acts as an
111
+ * editor when `ctx.editable` and a read-only viewer otherwise, superseding
112
+ * `dataEditor` / `detailRenderer` / `editorForCell`. Presentation is chosen
113
+ * separately via `detailPresentation` (popover / modal / inline), so the
114
+ * same component works in any container.
115
+ */
116
+ cellDetail?: (ctx: CellDetailContext) => React.ReactNode;
117
+ /** How `cellDetail` is presented. Defaults to `"popover"`. */
118
+ detailPresentation?: DetailPresentation;
19
119
  cellComponent?: any;
20
120
  category?: string;
21
121
  editable?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"column-spec.js","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\n/** Inferred data type of a column, used to select appropriate\n * sort/filter operators. */\nexport type ColumnDataType =\n | \"text\"\n | \"string\"\n | \"number\"\n | \"integer\"\n | \"boolean\"\n | \"object\"\n | \"array\";\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n required?: boolean;\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (d: any) => string | React.ReactNode;\n headerRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n /** Column-specific table actions (TableAction[]). These appear in the\n * actions toolbar when this column is part of the current selection. */\n actions?: any[];\n /** Column-specific table filters (TableFilter[]). These appear in the\n * filter bar and apply to this column's values. */\n filters?: any[];\n /** Whether this column supports server-side sorting. */\n sortable?: boolean;\n /** Whether this column supports server-side filtering.\n * Set to `true` for default operators, or provide a config object\n * with a custom set of operators. */\n filterable?: boolean | { operators?: string[] };\n /** Inferred data type, used to select context-appropriate filter\n * operators when `filterable` is `true` without explicit operators. */\n dataType?: ColumnDataType;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n // Check if it is strictly a string, if so, we can explicitly mark it as text\n if (type === \"string\") {\n type = \"text\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n const dataType = (types.get(key) ?? \"string\") as ColumnDataType;\n\n // Auto-infer sortable/filterable based on data type.\n // Scalar types (string, number, integer, boolean) get sort/filter;\n // complex types (object, array) do not.\n const isScalar =\n dataType === \"text\" ||\n dataType === \"string\" ||\n dataType === \"number\" ||\n dataType === \"integer\" ||\n dataType === \"boolean\";\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n dataType,\n sortable: isScalar,\n filterable: isScalar,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {\n overrides = {},\n nRows = 10,\n omitColumns,\n includeColumns,\n } = options ?? {};\n\n if (data == null) return [];\n\n const _nRows = Math.min(nRows, data.length);\n\n let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));\n let filteredSpec = columnSpec.filter((col) => {\n if (omitColumns != null && omitColumns.includes(col.key)) {\n return false;\n }\n if (includeColumns != null && !includeColumns.includes(col.key)) {\n return false;\n }\n return true;\n });\n\n // Apply overrides\n return filteredSpec.map((col) => {\n let ovr = overrides[col.key];\n if (ovr == null) return col;\n if (typeof ovr === \"string\") {\n return { ...col, name: ovr };\n }\n return { ...col, ...ovr };\n });\n}\n"],"names":[],"mappings":"AAEA,MAAM,mBAAmB;AAAA,EACvB,QAAQ,CAAC,MAAM;AAAA,EACf,QAAQ,CAAC,MAAM,GAAG,QAAQ,CAAC;AAAA,EAC3B,SAAS,CAAC,MAAO,IAAI,MAAM;AAAA,EAC3B,QAAQ,CAAC,MAAM,KAAK,UAAU,CAAC;AAAA,EAC/B,SAAS,CAAC,MAAM,GAAG,QAAQ,CAAC;AAAA,EAC5B,OAAO,CAAC,MAAM,GAAG,KAAK,IAAI;AAC5B;AAQA,MAAM,uBAAuB,CAAC,QAAQ,OAAO,GAAG,EAAE,SAAS;AAqDpD,SAAS,0BACd,MACA,UAAqC,IACvB;AAEd,MAAI,QAAQ,KAAM,QAAO,CAAA;AAGzB,QAAM;AAAA,IACJ,eAAe;AAAA,IACf,kBAAkB;AAAA,IAClB,mBAAmB;AAAA,EAAA,IACjB;AAEJ,QAAM,2BAAW,IAAA;AACjB,QAAM,4BAAY,IAAA;AAElB,QAAM,mCAAmB,IAAA;AAEzB,aAAW,OAAO,MAAM;AACtB,QAAI,OAAO,KAAM;AACjB,eAAW,OAAO,OAAO,KAAK,GAAG,GAAG;AAClC,YAAM,MAAM,IAAI,GAAG;AACnB,WAAK,IAAI,GAAG;AAEZ,UAAI,OAAO,KAAM;AAEjB,UAAI,iBAAiB;AACnB,YAAI,QAAQ,iBAAiB,GAAG;AAChC,YAAI,SAAS,QAAQ,SAAS,GAAG;AAC/B,uBAAa,IAAI,KAAK,KAAK,IAAI,OAAO,aAAa,IAAI,GAAG,KAAK,CAAC,CAAC;AAAA,QACnE;AAAA,MACF;AAEA,UAAI,OAAe,OAAO;AAE1B,UAAI,OAAO,UAAU,GAAG,GAAG;AAC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IACE,WAAW,CAAA;AAEf,MAAI,QAAQ,KAAM,QAAO,CAAA;AAEzB,QAAM,SAAS,KAAK,IAAI,OAAO,KAAK,MAAM;AAE1C,MAAI,aAAa,0BAA0B,KAAK,MAAM,GAAG,MAAM,CAAC;AAChE,MAAI,eAAe,WAAW,OAAO,CAAC,QAAQ;AAC5C,QAAI,eAAe,QAAQ,YAAY,SAAS,IAAI,GAAG,GAAG;AACxD,aAAO;AAAA,IACT;AACA,QAAI,kBAAkB,QAAQ,CAAC,eAAe,SAAS,IAAI,GAAG,GAAG;AAC/D,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT,CAAC;AAGD,SAAO,aAAa,IAAI,CAAC,QAAQ;AAC/B,QAAI,MAAM,UAAU,IAAI,GAAG;AAC3B,QAAI,OAAO,KAAM,QAAO;AACxB,QAAI,OAAO,QAAQ,UAAU;AAC3B,aAAO,EAAE,GAAG,KAAK,MAAM,IAAA;AAAA,IACzB;AACA,WAAO,EAAE,GAAG,KAAK,GAAG,IAAA;AAAA,EACtB,CAAC;AACH;"}
1
+ {"version":3,"file":"column-spec.js","sources":["../../src/utils/column-spec.ts"],"sourcesContent":["import React from \"react\";\n\nconst defaultRenderers = {\n string: (d) => d,\n number: (d) => d?.toFixed(2),\n boolean: (d) => (d ? \"T\" : \"F\"),\n object: (d) => JSON.stringify(d),\n integer: (d) => d?.toFixed(0),\n array: (d) => d?.join(\", \"),\n};\n\ninterface ColumnSpecGenerateOptions {\n calculateWidths?: boolean;\n defaultWidth?: number;\n getWidthForValue?: (value: any) => number;\n}\n\nconst defaultWidthForValue = (val) => String(val).length * 8;\n\n/** Inferred data type of a column, used to select appropriate\n * sort/filter operators. */\nexport type ColumnDataType =\n | \"text\"\n | \"string\"\n | \"number\"\n | \"integer\"\n | \"boolean\"\n | \"object\"\n | \"array\";\n\n/** Severity of a cell validation result. `warning` is soft (flagged, doesn't\n * block saving); `error` is hard (blocks saving). */\nexport type ValidationSeverity = \"warning\" | \"error\";\n\n/** The result of validating a cell. `null` means valid. */\nexport interface CellValidation {\n severity: ValidationSeverity;\n message?: string;\n}\n\n/**\n * Context handed to per-cell renderers (`valueRenderer`, and — as the\n * `cellContext` prop — a custom `cellComponent`). It lets a renderer draw\n * based on the row/column position and the cell's edit status, and, combined\n * with the editing API, write edits back for a specific cell.\n *\n * `rowIndex` is the underlying **data-row index** (the value edit methods\n * expect), so it stays stable when a sort or filter reorders the view.\n */\nexport interface CellRenderContext<T = any> {\n /** The cell's current value (the edited value if present, else the base). */\n value: any;\n /** Underlying data-row index — stable under sort/filter. */\n rowIndex: number;\n /** Column index within the active column spec. */\n colIndex: number;\n /** The full column spec for this cell's column. */\n column: ColumnSpec;\n /** The full row object backing this cell (may be undefined while loading). */\n row: T | undefined;\n /** Whether this cell has an uncommitted edit. */\n isEdited: boolean;\n /** Whether this cell's row is marked for deletion. */\n isDeleted: boolean;\n /** Validation result for this cell, or `null` when valid. Orthogonal to the\n * edit status — a cell can be both edited and invalid. */\n validation: CellValidation | null;\n}\n\n/**\n * The editor configuration for a cell. Mirrors the static `dataEditor` /\n * `inlineEditor` column fields so a per-cell resolver (`editorForCell`) can\n * override either one for an individual cell.\n */\nexport interface CellEditors {\n dataEditor?: any;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n}\n\n/**\n * Context for a cell's detail surface (`cellDetail`). Extends the render\n * context with the edit affordances and controls a surface needs, so one\n * component can act as an editor (when `editable`) or a read-only viewer\n * (otherwise). This is the write side of the read/write contract, per-cell.\n */\nexport interface CellDetailContext<T = any> extends CellRenderContext<T> {\n /** Whether this cell is editable (column × table × not-deleted). */\n editable: boolean;\n /** Commit a new value for this cell. */\n onChange: (value: any) => void;\n /** Reset this cell (and selection) to its base value. */\n resetValue: () => void;\n /** Close the surface and return focus to the table. */\n close: () => void;\n}\n\n/** How a cell's detail surface is presented. Orthogonal to what it renders. */\nexport type DetailPresentation = \"popover\" | \"modal\" | \"inline\";\n\nexport interface ColumnSpec {\n name: string;\n key: string;\n /** When true, an empty value is an error (sugar over `validate`). */\n required?: boolean;\n /** Validate a cell's value. Return a `{ severity, message }` for a\n * warning/error, or `null` when valid. Runs after the `required` check.\n * `warning` flags the cell but allows saving; `error` blocks saving. */\n validate?: (\n value: any,\n row: any,\n ctx: { rowIndex: number },\n ) => CellValidation | null;\n /** @deprecated Prefer `validate`, which carries severity + a message.\n * A falsy result maps to an `error`. */\n isValid?: (d: any) => boolean;\n transformValue?: (d: any) => any;\n valueRenderer?: (\n d: any,\n ctx?: CellRenderContext,\n ) => string | React.ReactNode;\n headerRenderer?: (d: any) => string | React.ReactNode;\n dataEditor?: any;\n /**\n * Choose the editor for an individual cell from its render context,\n * overriding the static `dataEditor` / `inlineEditor` for that cell. Return\n * `undefined` (or omit a key) to fall back to the static configuration —\n * e.g. show a textarea only for cells whose value is long. A returned key is\n * respected even when its value is `false`/`null` (i.e. \"no editor here\").\n */\n editorForCell?: (ctx: CellRenderContext) => CellEditors | undefined;\n /**\n * Render a read-only detail panel for a cell. When set, selecting the cell\n * opens a popover with this content (following the table's `cellInteraction`\n * mode). The panel never takes keyboard focus, so arrow keys keep navigating\n * the table; Escape closes it. Mutually distinct from `dataEditor` — use this\n * for non-editable surfaces (previews, summaries, links).\n *\n * @deprecated Prefer `cellDetail`, which unifies editor and viewer.\n */\n detailRenderer?: (ctx: CellRenderContext) => React.ReactNode;\n /**\n * The unified cell surface (Workstream A): one renderer that acts as an\n * editor when `ctx.editable` and a read-only viewer otherwise, superseding\n * `dataEditor` / `detailRenderer` / `editorForCell`. Presentation is chosen\n * separately via `detailPresentation` (popover / modal / inline), so the\n * same component works in any container.\n */\n cellDetail?: (ctx: CellDetailContext) => React.ReactNode;\n /** How `cellDetail` is presented. Defaults to `\"popover\"`. */\n detailPresentation?: DetailPresentation;\n cellComponent?: any;\n category?: string;\n editable?: boolean;\n inlineEditor?: boolean | React.ComponentType<any> | string | null;\n style?: React.CSSProperties;\n width?: number;\n /** Column-specific table actions (TableAction[]). These appear in the\n * actions toolbar when this column is part of the current selection. */\n actions?: any[];\n /** Column-specific table filters (TableFilter[]). These appear in the\n * filter bar and apply to this column's values. */\n filters?: any[];\n /** Whether this column supports server-side sorting. */\n sortable?: boolean;\n /** Whether this column supports server-side filtering.\n * Set to `true` for default operators, or provide a config object\n * with a custom set of operators. */\n filterable?: boolean | { operators?: string[] };\n /** Inferred data type, used to select context-appropriate filter\n * operators when `filterable` is `true` without explicit operators. */\n dataType?: ColumnDataType;\n}\n\nexport interface ColumnSpecOptions<T> {\n overrides: Record<string, Partial<ColumnSpec> | string>;\n data?: T[]; // Data to use for type inference\n nRows?: number; // Number of rows to use for type inference\n omitColumns?: string[]; // Columns to omit. Takes precedence over includeColumns.\n includeColumns?: string[]; // Columns to include.\n}\n\nexport function generateDefaultColumnSpec<T>(\n data: Array<T>,\n options: ColumnSpecGenerateOptions = {},\n): ColumnSpec[] {\n /** Build a default column spec from a dataset based on the first n rows */\n if (data == null) return [];\n // Get the keys\n\n const {\n defaultWidth = 150,\n calculateWidths = true,\n getWidthForValue = defaultWidthForValue,\n } = options;\n\n const keys = new Set<string>();\n const types = new Map();\n\n const columnWidths = new Map<string, number>();\n\n for (const row of data) {\n if (row == null) continue;\n for (const key of Object.keys(row)) {\n const val = row[key];\n keys.add(key);\n\n if (val == null) continue;\n\n if (calculateWidths) {\n let width = getWidthForValue(val);\n if (width != null && width >= 0) {\n columnWidths.set(key, Math.max(width, columnWidths.get(key) ?? 0));\n }\n }\n\n let type: string = typeof val;\n // Special 'type' for integers\n if (Number.isInteger(val)) {\n type = \"integer\";\n }\n\n // Special 'type' for arrays of simple values\n if (\n Array.isArray(val) &&\n val.every((d) => typeof d === \"string\" || typeof d === \"number\")\n ) {\n type = \"array\";\n }\n\n // Check if it is strictly a string, if so, we can explicitly mark it as text\n if (type === \"string\") {\n type = \"text\";\n }\n\n if (types.has(key)) {\n if (types.get(key) !== type) {\n if (type === \"number\" && types.get(key) === \"integer\") {\n types.set(key, \"number\");\n }\n if (type === \"object\" && types.get(key) === \"array\") {\n types.set(key, \"object\");\n }\n\n types.set(key, \"string\");\n }\n } else {\n types.set(key, type);\n }\n }\n }\n\n // Build a column spec\n const spec = [];\n for (const key of keys) {\n let width = null;\n if (calculateWidths) {\n // If we are calculating widths, use the value lengths\n width = Math.min(\n Math.max(defaultWidth / 2, columnWidths.get(key) ?? defaultWidth),\n defaultWidth * 1.5,\n );\n }\n\n const dataType = (types.get(key) ?? \"string\") as ColumnDataType;\n\n // Auto-infer sortable/filterable based on data type.\n // Scalar types (string, number, integer, boolean) get sort/filter;\n // complex types (object, array) do not.\n const isScalar =\n dataType === \"text\" ||\n dataType === \"string\" ||\n dataType === \"number\" ||\n dataType === \"integer\" ||\n dataType === \"boolean\";\n\n spec.push({\n name: key,\n key,\n valueRenderer: defaultRenderers[types.get(key)],\n width,\n dataType,\n sortable: isScalar,\n filterable: isScalar,\n });\n }\n return spec;\n}\n\nexport function generateColumnSpec<T>(\n data: T[],\n options: ColumnSpecOptions<T>,\n): ColumnSpec[] {\n /** Generate a column spec from a dataset */\n const {\n overrides = {},\n nRows = 10,\n omitColumns,\n includeColumns,\n } = options ?? {};\n\n if (data == null) return [];\n\n const _nRows = Math.min(nRows, data.length);\n\n let columnSpec = generateDefaultColumnSpec(data.slice(0, _nRows));\n let filteredSpec = columnSpec.filter((col) => {\n if (omitColumns != null && omitColumns.includes(col.key)) {\n return false;\n }\n if (includeColumns != null && !includeColumns.includes(col.key)) {\n return false;\n }\n return true;\n });\n\n // Apply overrides\n return filteredSpec.map((col) => {\n let ovr = overrides[col.key];\n if (ovr == null) return col;\n if (typeof ovr === \"string\") {\n return { ...col, name: ovr };\n }\n return { ...col, ...ovr };\n });\n}\n"],"names":[],"mappings":"AAEA,MAAM,mBAAmB;AAAA,EACvB,QAAQ,CAAC,MAAM;AAAA,EACf,QAAQ,CAAC,MAAM,GAAG,QAAQ,CAAC;AAAA,EAC3B,SAAS,CAAC,MAAO,IAAI,MAAM;AAAA,EAC3B,QAAQ,CA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IACT;AACA,WAAO;AAAA,EACT,CAAC;AAGD,SAAO,aAAa,IAAI,CAAC,QAAQ;AAC/B,QAAI,MAAM,UAAU,IAAI,GAAG;AAC3B,QAAI,OAAO,KAAM,QAAO;AACxB,QAAI,OAAO,QAAQ,UAAU;AAC3B,aAAO,EAAE,GAAG,KAAK,MAAM,IAAA;AAAA,IACzB;AACA,WAAO,EAAE,GAAG,KAAK,GAAG,IAAA;AAAA,EACtB,CAAC;AACH;"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const react = require("react");
4
+ function CellRendererDebugOverlay({ cellRendererDependencies, names }) {
5
+ const lastRenderDependencies = react.useRef(cellRendererDependencies);
6
+ react.useEffect(() => {
7
+ let changeDepNames = [];
8
+ for (const [i, dep] of cellRendererDependencies.entries()) {
9
+ if (dep !== lastRenderDependencies.current[i]) {
10
+ changeDepNames.push(names[i]);
11
+ }
12
+ }
13
+ if (changeDepNames.length > 0) {
14
+ console.log(
15
+ "Cell renderer dependencies changed:",
16
+ changeDepNames.join(", ")
17
+ );
18
+ }
19
+ lastRenderDependencies.current = cellRendererDependencies;
20
+ }, cellRendererDependencies);
21
+ return null;
22
+ }
23
+ exports.CellRendererDebugOverlay = CellRendererDebugOverlay;
24
+ //# sourceMappingURL=debug-overlay.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug-overlay.cjs","sources":["../../src/utils/debug-overlay.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\n\nexport function CellRendererDebugOverlay({ cellRendererDependencies, names }) {\n /** Debug overlay for cell renderer dependencies */\n const lastRenderDependencies = useRef<any[]>(cellRendererDependencies);\n useEffect(() => {\n let changeDepNames = [];\n for (const [i, dep] of cellRendererDependencies.entries()) {\n if (dep !== lastRenderDependencies.current[i]) {\n changeDepNames.push(names[i]);\n }\n }\n if (changeDepNames.length > 0) {\n console.log(\n \"Cell renderer dependencies changed:\",\n changeDepNames.join(\", \"),\n );\n }\n lastRenderDependencies.current = cellRendererDependencies;\n }, cellRendererDependencies);\n return null;\n}\n"],"names":["useRef","useEffect"],"mappings":";;;AAEO,SAAS,yBAAyB,EAAE,0BAA0B,SAAS;AAE5E,QAAM,yBAAyBA,MAAAA,OAAc,wBAAwB;AACrEC,QAAAA,UAAU,MAAM;AACd,QAAI,iBAAiB,CAAA;AACrB,eAAW,CAAC,GAAG,GAAG,KAAK,yBAAyB,WAAW;AACzD,UAAI,QAAQ,uBAAuB,QAAQ,CAAC,GAAG;AAC7C,uBAAe,KAAK,MAAM,CAAC,CAAC;AAAA,MAC9B;AAAA,IACF;AACA,QAAI,eAAe,SAAS,GAAG;AAC7B,cAAQ;AAAA,QACN;AAAA,QACA,eAAe,KAAK,IAAI;AAAA,MAAA;AAAA,IAE5B;AACA,2BAAuB,UAAU;AAAA,EACnC,GAAG,wBAAwB;AAC3B,SAAO;AACT;;"}
@@ -0,0 +1,4 @@
1
+ export declare function CellRendererDebugOverlay({ cellRendererDependencies, names }: {
2
+ cellRendererDependencies: any;
3
+ names: any;
4
+ }): null;
@@ -0,0 +1,24 @@
1
+ import { useRef, useEffect } from "react";
2
+ function CellRendererDebugOverlay({ cellRendererDependencies, names }) {
3
+ const lastRenderDependencies = useRef(cellRendererDependencies);
4
+ useEffect(() => {
5
+ let changeDepNames = [];
6
+ for (const [i, dep] of cellRendererDependencies.entries()) {
7
+ if (dep !== lastRenderDependencies.current[i]) {
8
+ changeDepNames.push(names[i]);
9
+ }
10
+ }
11
+ if (changeDepNames.length > 0) {
12
+ console.log(
13
+ "Cell renderer dependencies changed:",
14
+ changeDepNames.join(", ")
15
+ );
16
+ }
17
+ lastRenderDependencies.current = cellRendererDependencies;
18
+ }, cellRendererDependencies);
19
+ return null;
20
+ }
21
+ export {
22
+ CellRendererDebugOverlay
23
+ };
24
+ //# sourceMappingURL=debug-overlay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug-overlay.js","sources":["../../src/utils/debug-overlay.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\n\nexport function CellRendererDebugOverlay({ cellRendererDependencies, names }) {\n /** Debug overlay for cell renderer dependencies */\n const lastRenderDependencies = useRef<any[]>(cellRendererDependencies);\n useEffect(() => {\n let changeDepNames = [];\n for (const [i, dep] of cellRendererDependencies.entries()) {\n if (dep !== lastRenderDependencies.current[i]) {\n changeDepNames.push(names[i]);\n }\n }\n if (changeDepNames.length > 0) {\n console.log(\n \"Cell renderer dependencies changed:\",\n changeDepNames.join(\", \"),\n );\n }\n lastRenderDependencies.current = cellRendererDependencies;\n }, cellRendererDependencies);\n return null;\n}\n"],"names":[],"mappings":";AAEO,SAAS,yBAAyB,EAAE,0BAA0B,SAAS;AAE5E,QAAM,yBAAyB,OAAc,wBAAwB;AACrE,YAAU,MAAM;AACd,QAAI,iBAAiB,CAAA;AACrB,eAAW,CAAC,GAAG,GAAG,KAAK,yBAAyB,WAAW;AACzD,UAAI,QAAQ,uBAAuB,QAAQ,CAAC,GAAG;AAC7C,uBAAe,KAAK,MAAM,CAAC,CAAC;AAAA,MAC9B;AAAA,IACF;AACA,QAAI,eAAe,SAAS,GAAG;AAC7B,cAAQ;AAAA,QACN;AAAA,QACA,eAAe,KAAK,IAAI;AAAA,MAAA;AAAA,IAE5B;AACA,2BAAuB,UAAU;AAAA,EACnC,GAAG,wBAAwB;AAC3B,SAAO;AACT;"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/utils/index.ts"],"sourcesContent":["export * from \"./column-spec\";\nexport * from \"./key-handling\";\n\nexport function range(arr: number[]) {\n if (arr.length != 2) throw new Error(\"Range must have two elements\");\n const [start, end] = arr;\n return Array.from({ length: end - start + 1 }, (_, i) => i + start);\n}\n"],"names":[],"mappings":";;AAGO,SAAS,MAAM,KAAe;AACnC,MAAI,IAAI,UAAU,EAAG,OAAM,IAAI,MAAM,8BAA8B;AACnE,QAAM,CAAC,OAAO,GAAG,IAAI;AACrB,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,QAAQ,EAAA,GAAK,CAAC,GAAG,MAAM,IAAI,KAAK;AACpE;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/utils/index.ts"],"sourcesContent":["export * from \"./column-spec\";\nexport * from \"./key-handling\";\nexport * from \"./validation\";\nexport { CellRendererDebugOverlay } from \"./debug-overlay.ts\";\n\nexport function range(arr: number[]) {\n if (arr.length != 2) throw new Error(\"Range must have two elements\");\n const [start, end] = arr;\n return Array.from({ length: end - start + 1 }, (_, i) => i + start);\n}\n"],"names":[],"mappings":";;AAKO,SAAS,MAAM,KAAe;AACnC,MAAI,IAAI,UAAU,EAAG,OAAM,IAAI,MAAM,8BAA8B;AACnE,QAAM,CAAC,OAAO,GAAG,IAAI;AACrB,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,QAAQ,EAAA,GAAK,CAAC,GAAG,MAAM,IAAI,KAAK;AACpE;;"}
@@ -1,3 +1,5 @@
1
1
  export * from './column-spec';
2
2
  export * from './key-handling';
3
+ export * from './validation';
4
+ export { CellRendererDebugOverlay } from './debug-overlay.ts';
3
5
  export declare function range(arr: number[]): number[];
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/utils/index.ts"],"sourcesContent":["export * from \"./column-spec\";\nexport * from \"./key-handling\";\n\nexport function range(arr: number[]) {\n if (arr.length != 2) throw new Error(\"Range must have two elements\");\n const [start, end] = arr;\n return Array.from({ length: end - start + 1 }, (_, i) => i + start);\n}\n"],"names":[],"mappings":"AAGO,SAAS,MAAM,KAAe;AACnC,MAAI,IAAI,UAAU,EAAG,OAAM,IAAI,MAAM,8BAA8B;AACnE,QAAM,CAAC,OAAO,GAAG,IAAI;AACrB,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,QAAQ,EAAA,GAAK,CAAC,GAAG,MAAM,IAAI,KAAK;AACpE;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/utils/index.ts"],"sourcesContent":["export * from \"./column-spec\";\nexport * from \"./key-handling\";\nexport * from \"./validation\";\nexport { CellRendererDebugOverlay } from \"./debug-overlay.ts\";\n\nexport function range(arr: number[]) {\n if (arr.length != 2) throw new Error(\"Range must have two elements\");\n const [start, end] = arr;\n return Array.from({ length: end - start + 1 }, (_, i) => i + start);\n}\n"],"names":[],"mappings":"AAKO,SAAS,MAAM,KAAe;AACnC,MAAI,IAAI,UAAU,EAAG,OAAM,IAAI,MAAM,8BAA8B;AACnE,QAAM,CAAC,OAAO,GAAG,IAAI;AACrB,SAAO,MAAM,KAAK,EAAE,QAAQ,MAAM,QAAQ,EAAA,GAAK,CAAC,GAAG,MAAM,IAAI,KAAK;AACpE;"}
@@ -70,13 +70,28 @@ const tableHotkeysAtom = jotai.atom((get) => {
70
70
  }
71
71
  };
72
72
  }).filter(Boolean);
73
+ const clearOrDeleteSelection = keyHandler((e, state) => {
74
+ const sel = state.selection ?? [];
75
+ const isRowSelection = sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);
76
+ if (isRowSelection) {
77
+ state.deleteSelectedRows();
78
+ } else {
79
+ state.clearSelection();
80
+ }
81
+ });
73
82
  return [
74
83
  {
75
84
  combo: "esc",
76
- label: "Clear selection",
85
+ label: "Exit editing / clear selection",
77
86
  group: "Selection",
78
87
  allowInInput: true,
79
88
  onKeyDown: keyHandler((e, state) => {
89
+ if (state.cellSurfaceOpen) {
90
+ state.closeCellSurface();
91
+ e.preventDefault();
92
+ e.stopPropagation();
93
+ return;
94
+ }
80
95
  if (state.selection.length == 0) {
81
96
  return;
82
97
  }
@@ -87,13 +102,19 @@ const tableHotkeysAtom = jotai.atom((get) => {
87
102
  },
88
103
  {
89
104
  combo: "backspace",
90
- label: "Clear selection",
105
+ label: "Clear cells / delete rows",
91
106
  group: "Selection",
92
107
  allowInInput: true,
93
108
  disabled: !editable,
94
- onKeyDown: keyHandler((e, state) => {
95
- state.clearSelection();
96
- })
109
+ onKeyDown: clearOrDeleteSelection
110
+ },
111
+ {
112
+ combo: "del",
113
+ label: "Clear cells / delete rows",
114
+ group: "Selection",
115
+ allowInInput: true,
116
+ disabled: !editable,
117
+ onKeyDown: clearOrDeleteSelection
97
118
  },
98
119
  {
99
120
  combo: "tab",
@@ -106,13 +127,29 @@ const tableHotkeysAtom = jotai.atom((get) => {
106
127
  },
107
128
  {
108
129
  combo: "enter",
109
- label: "Move focus down",
130
+ label: "Edit cell / move focus down",
110
131
  group: "Navigation",
111
132
  allowInInput: true,
112
133
  onKeyDown: keyHandler((e, state) => {
134
+ if (!state.cellSurfaceOpen) {
135
+ state.armAutoActivate();
136
+ state.openCellSurface();
137
+ e.preventDefault();
138
+ return;
139
+ }
113
140
  state.moveFocusedCell("down");
114
141
  })
115
142
  },
143
+ {
144
+ combo: "f2",
145
+ label: "Edit / open cell",
146
+ group: "Editing",
147
+ onKeyDown: keyHandler((e, state) => {
148
+ state.armAutoActivate();
149
+ state.openCellSurface();
150
+ e.preventDefault();
151
+ })
152
+ },
116
153
  ...directionCombos,
117
154
  ...actionsCombos
118
155
  ];
@@ -126,7 +163,16 @@ const isSingleCellSelectionAtom = jotai.atom((get) => {
126
163
  return zustandStore.singleFocusedCell(selection) != null;
127
164
  });
128
165
  const editorKeyHandlerAtom = jotai.atom((get) => {
166
+ const store = get(provider.storeAPIAtom);
129
167
  return (e) => {
168
+ if (e.key === "Escape") {
169
+ const state = store?.getState();
170
+ state?.closeCellSurface();
171
+ state?.tableElement?.focus();
172
+ e.preventDefault();
173
+ e.stopPropagation();
174
+ return;
175
+ }
130
176
  const isSingleCellSelection = get(isSingleCellSelectionAtom);
131
177
  editorKeyHandler(e, isSingleCellSelection);
132
178
  };
@@ -1 +1 @@
1
- {"version":3,"file":"key-handling.cjs","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom, tableActionsAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\nimport { buildActionContext, runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n const state = store.getState();\n const setState = store.setState;\n runActionWrapper(action, state, setState, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n return [\n {\n combo: \"esc\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: keyHandler((e, state) => {\n state.clearSelection();\n }),\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(\"down\");\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n});\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: KeyboardEvent): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["atom","storeAtom","store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AClE;AACF;AAEA,SAAS,yBAAyB,KAA6B;AAC7D,MAAI,IAAI,OAAO,kBAAkB,IAAI,OAAO,cAAc;AAGxD,QAAI,IAAI,QAAQ,OAAO,IAAI,QAAQ,OAAO,IAAI,QAAQ,KAAK;AACzD,UAAI,IAAI,WAAW,IAAI,SAAS;AAC9B,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,aAAa,IAAI,QAAQ,aAAa;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;;;;"}
1
+ {"version":3,"file":"key-handling.cjs","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom, tableActionsAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\nimport { buildActionContext, runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n const state = store.getState();\n const setState = store.setState;\n runActionWrapper(action, state, setState, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n});\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: KeyboardEvent): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["atom","storeAtom","storeAPIAtom","toasterAtom","tableActionsAtom","toaster","runActionWrapper","singleFocusedCell"],"mappings":";;;;;;;AAeA,MAAM,iBAAiBA,MAAAA,KAAK,CAAC,QAAQ;AACnC,QAAM,QAAQ,IAAIC,kBAAS;AAC3B,SAAO,OAAO,YAAY;AAC5B,CAAC;AAEwBD,MAAAA,KAAK,CAAC,QAAQ;AACrC,QAAM,QAAQ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,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;;;;"}
@@ -68,13 +68,28 @@ const tableHotkeysAtom = atom((get) => {
68
68
  }
69
69
  };
70
70
  }).filter(Boolean);
71
+ const clearOrDeleteSelection = keyHandler((e, state) => {
72
+ const sel = state.selection ?? [];
73
+ const isRowSelection = sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);
74
+ if (isRowSelection) {
75
+ state.deleteSelectedRows();
76
+ } else {
77
+ state.clearSelection();
78
+ }
79
+ });
71
80
  return [
72
81
  {
73
82
  combo: "esc",
74
- label: "Clear selection",
83
+ label: "Exit editing / clear selection",
75
84
  group: "Selection",
76
85
  allowInInput: true,
77
86
  onKeyDown: keyHandler((e, state) => {
87
+ if (state.cellSurfaceOpen) {
88
+ state.closeCellSurface();
89
+ e.preventDefault();
90
+ e.stopPropagation();
91
+ return;
92
+ }
78
93
  if (state.selection.length == 0) {
79
94
  return;
80
95
  }
@@ -85,13 +100,19 @@ const tableHotkeysAtom = atom((get) => {
85
100
  },
86
101
  {
87
102
  combo: "backspace",
88
- label: "Clear selection",
103
+ label: "Clear cells / delete rows",
89
104
  group: "Selection",
90
105
  allowInInput: true,
91
106
  disabled: !editable,
92
- onKeyDown: keyHandler((e, state) => {
93
- state.clearSelection();
94
- })
107
+ onKeyDown: clearOrDeleteSelection
108
+ },
109
+ {
110
+ combo: "del",
111
+ label: "Clear cells / delete rows",
112
+ group: "Selection",
113
+ allowInInput: true,
114
+ disabled: !editable,
115
+ onKeyDown: clearOrDeleteSelection
95
116
  },
96
117
  {
97
118
  combo: "tab",
@@ -104,13 +125,29 @@ const tableHotkeysAtom = atom((get) => {
104
125
  },
105
126
  {
106
127
  combo: "enter",
107
- label: "Move focus down",
128
+ label: "Edit cell / move focus down",
108
129
  group: "Navigation",
109
130
  allowInInput: true,
110
131
  onKeyDown: keyHandler((e, state) => {
132
+ if (!state.cellSurfaceOpen) {
133
+ state.armAutoActivate();
134
+ state.openCellSurface();
135
+ e.preventDefault();
136
+ return;
137
+ }
111
138
  state.moveFocusedCell("down");
112
139
  })
113
140
  },
141
+ {
142
+ combo: "f2",
143
+ label: "Edit / open cell",
144
+ group: "Editing",
145
+ onKeyDown: keyHandler((e, state) => {
146
+ state.armAutoActivate();
147
+ state.openCellSurface();
148
+ e.preventDefault();
149
+ })
150
+ },
114
151
  ...directionCombos,
115
152
  ...actionsCombos
116
153
  ];
@@ -124,7 +161,16 @@ const isSingleCellSelectionAtom = atom((get) => {
124
161
  return singleFocusedCell(selection) != null;
125
162
  });
126
163
  const editorKeyHandlerAtom = atom((get) => {
164
+ const store = get(storeAPIAtom);
127
165
  return (e) => {
166
+ if (e.key === "Escape") {
167
+ const state = store?.getState();
168
+ state?.closeCellSurface();
169
+ state?.tableElement?.focus();
170
+ e.preventDefault();
171
+ e.stopPropagation();
172
+ return;
173
+ }
128
174
  const isSingleCellSelection = get(isSingleCellSelectionAtom);
129
175
  editorKeyHandler(e, isSingleCellSelection);
130
176
  };
@@ -1 +1 @@
1
- {"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom, tableActionsAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\nimport { buildActionContext, runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n const state = store.getState();\n const setState = store.setState;\n runActionWrapper(action, state, setState, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n return [\n {\n combo: \"esc\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: keyHandler((e, state) => {\n state.clearSelection();\n }),\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(\"down\");\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n});\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: KeyboardEvent): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["toaster"],"mappings":";;;;;AAeA,MAAM,iBAAiB,KAAK,CAAC,QAAQ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I,SAAS;AAC9B,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,aAAa,IAAI,QAAQ,aAAa;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;"}
1
+ {"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom, tableActionsAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\nimport { buildActionContext, runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n const state = store.getState();\n const setState = store.setState;\n runActionWrapper(action, state, setState, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n // Backspace/Delete: on a whole-row selection, delete the row(s); otherwise\n // clear the selected cells.\n const clearOrDeleteSelection = keyHandler((e, state) => {\n const sel = state.selection ?? [];\n const isRowSelection =\n sel.length > 0 && sel.every((r) => r.cols == null && r.rows != null);\n if (isRowSelection) {\n state.deleteSelectedRows();\n } else {\n state.clearSelection();\n }\n });\n\n return [\n {\n combo: \"esc\",\n label: \"Exit editing / clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // First Escape closes the focused cell's open surface (editor/panel)\n // while keeping the selection, so arrow keys keep working — in both\n // auto and manual modes. A second Escape (nothing open) clears the\n // selection.\n if (state.cellSurfaceOpen) {\n state.closeCellSurface();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"del\",\n label: \"Clear cells / delete rows\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: clearOrDeleteSelection,\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Edit cell / move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n // In navigation mode (no surface open), Enter enters edit mode —\n // opening and focusing the focused cell's surface (Google Sheets\n // style). While a surface is open, Enter advances downward as usual\n // (an inline editor blurs and this moves to the next row).\n if (!state.cellSurfaceOpen) {\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n return;\n }\n state.moveFocusedCell(\"down\");\n }),\n },\n {\n combo: \"f2\",\n label: \"Edit / open cell\",\n group: \"Editing\",\n onKeyDown: keyHandler((e, state) => {\n // Enter edit mode from the keyboard: re-arm auto-activation and open\n // the focused cell's surface (which focuses the editor).\n state.armAutoActivate();\n state.openCellSurface();\n e.preventDefault();\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n});\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n // Escape exits the inline editor: close the surface (which enters\n // navigation mode in auto), keep the selection, and return focus to the\n // table so arrow keys navigate. Stop propagation so the table's own\n // Escape hotkey doesn't also fire.\n const state = store?.getState();\n state?.closeCellSurface();\n state?.tableElement?.focus();\n e.preventDefault();\n e.stopPropagation();\n return;\n }\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: KeyboardEvent): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["toaster"],"mappings":";;;;;AAeA,MAAM,iBAAiB,KAAK,CAAC,QAAQ;AACnC,QAAM,QAAQ,IAAI,SAAS;AAC3B,SAAO,OAAO,YAAY;AAC5B,CAAC;AAEwB,KAAK,CAAC,QAAQ;AACrC,QAAM,QAAQ,IAAI,SAAS;AAC3B,QAAM,YAAY,OAAO,aAAa,CAAA;AACtC,SAAO,UAAU,SAAS;AAC5B,CAAC;AAEM,MAAM,mBAAmB,KAAqB,CAAC,QAAQ;AAC5D,QAAM,QAAQ,IAAI,YAAY;AAC9B,QAAM,UAAU,IAAI,WAAW;AAC/B,QAAM,WAAW,IAAI,cAAc;AAGnC,QAAM,eAAe,IAAI,gBAAgB;AAEzC,MAAI,SAAS,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