summaryrefslogtreecommitdiff
path: root/web/src/types.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/types.ts')
-rw-r--r--web/src/types.ts40
1 files changed, 40 insertions, 0 deletions
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 });
+ }),
+ };
+ }),
+ };
+ }),
+ };
+}