From ca7edbcc6b201df7b5070cde205e66f576948475 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Sun, 26 Oct 2025 21:04:24 +0200 Subject: feat(web): add subdivideSelection method --- web/src/types.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'web/src/types.ts') diff --git a/web/src/types.ts b/web/src/types.ts index d932389..8b1b650 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -44,3 +44,43 @@ export function cellRefEquals(a: CellRef, b: CellRef): boolean { a.cellIndex === b.cellIndex ); } + +export function mapRowsInRange( + doc: Doc, + gridId: string, + startRef: CellRef, + endRef: CellRef, + mapFn: (row: Row, ref: RowRef) => Row, +): Doc { + const firstPartIndex = Math.min(startRef.partIndex, endRef.partIndex); + const lastPartIndex = Math.max(startRef.partIndex, endRef.partIndex); + const firstRowIndex = Math.min(startRef.rowIndex, endRef.rowIndex); + const lastRowIndex = Math.max(startRef.rowIndex, endRef.rowIndex); + + return { + ...doc, + grids: doc.grids.map((grid) => { + if (grid.id !== gridId) return grid; + + return { + ...grid, + parts: grid.parts.map((part, partIndex) => { + if (partIndex < firstPartIndex || partIndex > lastPartIndex) { + return part; + } + + return { + ...part, + rows: part.rows.map((row, rowIndex) => { + if (rowIndex < firstRowIndex || rowIndex > lastRowIndex) { + return row; + } + + return mapFn(row, { partIndex, rowIndex }); + }), + }; + }), + }; + }), + }; +} -- cgit v1.2.3