|
| 1 | +/** Frontend staging buffer — Flutter / Rust `StagingBuffer` parity (cell layer). */ |
| 2 | + |
| 3 | +export const NULL_SENTINEL = "\u0000__QUERYA_NULL__\u0000"; |
| 4 | + |
| 5 | +export type StagedRowStatus = |
| 6 | + | "unchanged" |
| 7 | + | "modified" |
| 8 | + | "inserted" |
| 9 | + | "deleted"; |
| 10 | + |
| 11 | +export type StagedCellStatus = "clean" | "modified"; |
| 12 | + |
| 13 | +export class StagingBuffer { |
| 14 | + private columns: string[]; |
| 15 | + private originalRows: string[][]; |
| 16 | + private primaryKeys: string[]; |
| 17 | + private modifiedCells = new Map<number, Map<number, string>>(); |
| 18 | + private insertedRows: string[][] = []; |
| 19 | + private deletedRowIndices = new Set<number>(); |
| 20 | + private revision = 0; |
| 21 | + |
| 22 | + constructor( |
| 23 | + columns: string[], |
| 24 | + rows: string[][], |
| 25 | + primaryKeys: string[] = [], |
| 26 | + ) { |
| 27 | + this.columns = [...columns]; |
| 28 | + this.originalRows = rows.map((r) => [...r]); |
| 29 | + this.primaryKeys = [...primaryKeys]; |
| 30 | + } |
| 31 | + |
| 32 | + /** Replace baseline (e.g. after a new query) and clear staged edits. */ |
| 33 | + replaceBaseline( |
| 34 | + columns: string[], |
| 35 | + rows: string[][], |
| 36 | + primaryKeys: string[] = [], |
| 37 | + ): void { |
| 38 | + this.columns = [...columns]; |
| 39 | + this.originalRows = rows.map((r) => [...r]); |
| 40 | + this.primaryKeys = [...primaryKeys]; |
| 41 | + this.modifiedCells.clear(); |
| 42 | + this.insertedRows = []; |
| 43 | + this.deletedRowIndices.clear(); |
| 44 | + this.revision += 1; |
| 45 | + } |
| 46 | + |
| 47 | + getRevision(): number { |
| 48 | + return this.revision; |
| 49 | + } |
| 50 | + |
| 51 | + getColumns(): string[] { |
| 52 | + return this.columns; |
| 53 | + } |
| 54 | + |
| 55 | + getPrimaryKeys(): string[] { |
| 56 | + return this.primaryKeys; |
| 57 | + } |
| 58 | + |
| 59 | + totalRowCount(): number { |
| 60 | + return this.originalRows.length + this.insertedRows.length; |
| 61 | + } |
| 62 | + |
| 63 | + isDirty(): boolean { |
| 64 | + return ( |
| 65 | + this.modifiedCells.size > 0 || |
| 66 | + this.insertedRows.length > 0 || |
| 67 | + this.deletedRowIndices.size > 0 |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + changeCount(): number { |
| 72 | + let cells = 0; |
| 73 | + for (const m of this.modifiedCells.values()) cells += m.size; |
| 74 | + return cells + this.insertedRows.length + this.deletedRowIndices.size; |
| 75 | + } |
| 76 | + |
| 77 | + getCellValue(row: number, col: number): string { |
| 78 | + if (row < this.originalRows.length) { |
| 79 | + const staged = this.modifiedCells.get(row)?.get(col); |
| 80 | + if (staged !== undefined) return sanitizeNull(staged); |
| 81 | + return this.originalRows[row]?.[col] ?? ""; |
| 82 | + } |
| 83 | + const insertIdx = row - this.originalRows.length; |
| 84 | + return sanitizeNull(this.insertedRows[insertIdx]?.[col] ?? ""); |
| 85 | + } |
| 86 | + |
| 87 | + rowStatus(row: number): StagedRowStatus { |
| 88 | + if (row >= this.originalRows.length) { |
| 89 | + return row < this.totalRowCount() ? "inserted" : "unchanged"; |
| 90 | + } |
| 91 | + if (this.deletedRowIndices.has(row)) return "deleted"; |
| 92 | + const mods = this.modifiedCells.get(row); |
| 93 | + if (mods && mods.size > 0) return "modified"; |
| 94 | + return "unchanged"; |
| 95 | + } |
| 96 | + |
| 97 | + cellStatus(row: number, col: number): StagedCellStatus { |
| 98 | + if ( |
| 99 | + row < this.originalRows.length && |
| 100 | + this.modifiedCells.get(row)?.has(col) |
| 101 | + ) { |
| 102 | + return "modified"; |
| 103 | + } |
| 104 | + return "clean"; |
| 105 | + } |
| 106 | + |
| 107 | + setCell(row: number, col: number, value: string): void { |
| 108 | + if (row < 0 || col < 0) return; |
| 109 | + if (row < this.originalRows.length) { |
| 110 | + const orig = this.originalRows[row]?.[col] ?? ""; |
| 111 | + if (value === orig) { |
| 112 | + const map = this.modifiedCells.get(row); |
| 113 | + if (map) { |
| 114 | + map.delete(col); |
| 115 | + if (map.size === 0) this.modifiedCells.delete(row); |
| 116 | + this.revision += 1; |
| 117 | + } |
| 118 | + } else { |
| 119 | + let map = this.modifiedCells.get(row); |
| 120 | + if (!map) { |
| 121 | + map = new Map(); |
| 122 | + this.modifiedCells.set(row, map); |
| 123 | + } |
| 124 | + if (map.get(col) !== value) { |
| 125 | + map.set(col, value); |
| 126 | + this.revision += 1; |
| 127 | + } |
| 128 | + } |
| 129 | + return; |
| 130 | + } |
| 131 | + const insertIdx = row - this.originalRows.length; |
| 132 | + const ins = this.insertedRows[insertIdx]; |
| 133 | + if (!ins) return; |
| 134 | + while (ins.length <= col) ins.push(""); |
| 135 | + if (ins[col] !== value) { |
| 136 | + ins[col] = value; |
| 137 | + this.revision += 1; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + setCellNull(row: number, col: number): void { |
| 142 | + this.setCell(row, col, NULL_SENTINEL); |
| 143 | + } |
| 144 | + |
| 145 | + toggleDeleteRow(row: number): void { |
| 146 | + if (row < 0) return; |
| 147 | + if (row < this.originalRows.length) { |
| 148 | + if (this.deletedRowIndices.has(row)) this.deletedRowIndices.delete(row); |
| 149 | + else this.deletedRowIndices.add(row); |
| 150 | + this.revision += 1; |
| 151 | + } else { |
| 152 | + const insertIdx = row - this.originalRows.length; |
| 153 | + if (insertIdx >= 0 && insertIdx < this.insertedRows.length) { |
| 154 | + this.insertedRows.splice(insertIdx, 1); |
| 155 | + this.revision += 1; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + revertAll(): void { |
| 161 | + if (!this.isDirty()) return; |
| 162 | + this.modifiedCells.clear(); |
| 163 | + this.insertedRows = []; |
| 164 | + this.deletedRowIndices.clear(); |
| 165 | + this.revision += 1; |
| 166 | + } |
| 167 | + |
| 168 | + /** Effective grid values (deleted rows kept for strike-through). */ |
| 169 | + effectiveRows(): string[][] { |
| 170 | + const out: string[][] = []; |
| 171 | + for (let r = 0; r < this.originalRows.length; r++) { |
| 172 | + const mods = this.modifiedCells.get(r); |
| 173 | + if (mods) { |
| 174 | + const row = [...(this.originalRows[r] ?? [])]; |
| 175 | + for (const [c, v] of mods) { |
| 176 | + if (c < row.length) row[c] = sanitizeNull(v); |
| 177 | + } |
| 178 | + out.push(row); |
| 179 | + } else { |
| 180 | + out.push([...(this.originalRows[r] ?? [])]); |
| 181 | + } |
| 182 | + } |
| 183 | + for (const ins of this.insertedRows) { |
| 184 | + out.push(ins.map(sanitizeNull)); |
| 185 | + } |
| 186 | + return out; |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +function sanitizeNull(value: string): string { |
| 191 | + return value === NULL_SENTINEL ? "NULL" : value; |
| 192 | +} |
| 193 | + |
| 194 | +/** Coerce query result cells to staging strings. */ |
| 195 | +export function rowsToStrings(rows: unknown[][]): string[][] { |
| 196 | + return rows.map((row) => |
| 197 | + row.map((cell) => { |
| 198 | + if (cell === null || cell === undefined) return "NULL"; |
| 199 | + if (typeof cell === "string") return cell; |
| 200 | + if (typeof cell === "number" || typeof cell === "boolean") { |
| 201 | + return String(cell); |
| 202 | + } |
| 203 | + return JSON.stringify(cell); |
| 204 | + }), |
| 205 | + ); |
| 206 | +} |
0 commit comments